Skip to content

Commit

Permalink
meson: Fail early if npm is missing
Browse files Browse the repository at this point in the history
Also ensure Node.js is recent, and refactor a little bit.
  • Loading branch information
oleavr committed Apr 24, 2024
1 parent 9125616 commit fdcbf66
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
56 changes: 22 additions & 34 deletions agents/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
2 changes: 1 addition & 1 deletion agents/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build_agent = [python, files('build.py')]
build_agent = [python, files('build.py'), npm]

subdir('fs')
subdir('tracer')
Expand Down
9 changes: 9 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit fdcbf66

Please sign in to comment.