Import("env") from pathlib import Path import os import shlex import shutil import subprocess PROJECT_DIR = Path(env.subst("$PROJECT_DIR")) PIO_SOURCE = PROJECT_DIR / "src" / "lora_uart.pio" PIO_HEADER = PROJECT_DIR / "src" / "lora_uart.pio.h" def _is_executable(path): path = Path(path) return path.is_file() and (os.name == "nt" or os.access(path, os.X_OK)) def _find_pioasm(): candidates = [] configured = env.get("PIOASM") if configured: expanded = env.subst(str(configured)) if expanded: candidates.append(Path(expanded)) platform = env.PioPlatform() for package_name in ( "tool-pioasm-rp2040", "tool-pioasm", "framework-picosdk", ): package_dir = platform.get_package_dir(package_name) if package_dir: base = Path(package_dir) candidates.extend( [ base / "pioasm", base / "pioasm.exe", base / "bin" / "pioasm", base / "bin" / "pioasm.exe", base / "tools" / "pioasm" / "pioasm", base / "tools" / "pioasm" / "pioasm.exe", base / "tools" / "pioasm" / "build" / "pioasm", base / "tools" / "pioasm" / "build" / "pioasm.exe", ] ) packages_dir = env.subst("$PROJECT_PACKAGES_DIR") if packages_dir and "$" not in packages_dir: base = Path(packages_dir) if base.is_dir(): for name in ("pioasm", "pioasm.exe"): candidates.extend(base.glob("**/" + name)) on_path = shutil.which("pioasm") if on_path: candidates.append(Path(on_path)) seen = set() for candidate in candidates: try: candidate = candidate.resolve() except OSError: continue if candidate in seen: continue seen.add(candidate) if _is_executable(candidate): return candidate searched = "\n ".join(str(path) for path in candidates) raise RuntimeError( "Unable to locate the Pico SDK pioasm executable. Searched:\n " + (searched or "") ) def _generate_pio_header(): if not PIO_SOURCE.is_file(): raise RuntimeError("Missing PIO source: {}".format(PIO_SOURCE)) if PIO_HEADER.is_file() and PIO_HEADER.stat().st_mtime >= PIO_SOURCE.stat().st_mtime: print("pioasm: {} is up to date".format(PIO_HEADER)) return pioasm = _find_pioasm() command = [str(pioasm), "-o", "c-sdk", str(PIO_SOURCE), str(PIO_HEADER)] print("pioasm command: {}".format(" ".join(shlex.quote(arg) for arg in command))) completed = subprocess.run( command, cwd=str(PROJECT_DIR), text=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) if completed.stdout: print(completed.stdout, end="" if completed.stdout.endswith("\n") else "\n") if completed.returncode != 0: raise RuntimeError("pioasm failed with exit code {}".format(completed.returncode)) _generate_pio_header()