diff --git a/agents/build.py b/agents/build.py index 4dfe07f0..40037d2a 100644 --- a/agents/build.py +++ b/agents/build.py @@ -6,7 +6,21 @@ from pathlib import Path -def build(inputs, output_js, priv_dir): +def main(argv: list[str]): + npm = argv[1] + paths = [Path(p).resolve() for p in argv[2:]] + inputs = paths[:-2] + output_js = paths[-2] + priv_dir = paths[-1] + + try: + build(npm, inputs, output_js, priv_dir) + except Exception as e: + print(e, file=sys.stderr) + sys.exit(1) + + +def build(npm: Path, inputs: list[Path], output_js: Path, priv_dir: Path): pkg_file = next((f for f in inputs if f.name == "package.json")) pkg_parent = pkg_file.parent entrypoint = inputs[0].relative_to(pkg_parent) @@ -19,44 +33,18 @@ def build(inputs, output_js, priv_dir): if not dstdir.exists(): dstdir.mkdir() - shutil.copyfile(srcfile, dstfile) + shutil.copy(srcfile, dstfile) - npm = os.environ.get("NPM", "npm") - try: - subprocess.run([npm, "install"], capture_output=True, cwd=priv_dir, check=True) - except Exception as e: - message = "\n".join( - [ - "", - "***", - f"Failed to build {inputs[0].name}:", - "\t" + str(e), - "This is most likely because Node.js is not installed.", - "We need it for processing JavaScript code at build-time.", - "Check PATH or set NPM to the absolute path of your npm binary.", - "***\n", - ] - ) - raise EnvironmentError(message) - - frida_compile = Path("node_modules") / ".bin" / ("frida-compile" + script_suffix()) + subprocess.run([npm, "install"], capture_output=True, cwd=priv_dir, check=True) + + frida_compile = Path("node_modules") / ".bin" / f"frida-compile{script_suffix()}" subprocess.run([frida_compile, entrypoint, "-c", "-o", output_js], cwd=priv_dir, check=True) -def script_suffix(): - build_os = platform.system().lower() - return ".cmd" if build_os == "windows" else "" +def script_suffix() -> str: + return ".cmd" if platform.system() == "Windows" else "" if __name__ == "__main__": - paths = [Path(p).resolve() for p in sys.argv[1:]] - inputs = paths[:-2] - output_js = paths[-2] - priv_dir = paths[-1] - - try: - build(inputs, output_js, priv_dir) - except Exception as e: - print(e, file=sys.stderr) - sys.exit(1) + main(sys.argv) diff --git a/agents/meson.build b/agents/meson.build index 34108ea2..142d0254 100644 --- a/agents/meson.build +++ b/agents/meson.build @@ -1,4 +1,4 @@ -build_agent = [python, files('build.py')] +build_agent = [python, files('build.py'), npm] subdir('fs') subdir('tracer') diff --git a/meson.build b/meson.build index f0d96844..ab212ebb 100644 --- a/meson.build +++ b/meson.build @@ -9,6 +9,15 @@ subproject('frida-python') python = import('python').find_installation() +node = find_program('node', version: '>=18.0.0', native: true, required: false) +if not node.found() + error('Need Node.js >= 18.0.0 to process JavaScript code at build time') +endif +npm = find_program('npm', native: true, required: false) +if not npm.found() + error('Need npm to process JavaScript code at build time') +endif + subdir('agents') subdir('frida_tools') subdir('scripts')