Skip to content

Commit

Permalink
meson: Fail early if Node.js or npm are missing or outdated
Browse files Browse the repository at this point in the history
And improve consistency of argument ordering in the scripts.
  • Loading branch information
oleavr committed Apr 19, 2024
1 parent fec91ce commit 76c2a86
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 63 deletions.
11 changes: 11 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,17 @@ foreach b : [['local', have_local_backend],
endif
endforeach

if have_barebone_backend or have_compiler_backend
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
endif

gi_dep = dependency('gobject-introspection-1.0', required: false)
if gi_dep.found()
girdir = gi_dep.get_variable('girdir')
Expand Down
30 changes: 4 additions & 26 deletions src/barebone/generate-script-runtime.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
#!/usr/bin/env python3

import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys


def main(argv):
input_dir, output_dir, priv_dir = [Path(d).resolve() for d in argv[1:]]
output_dir, priv_dir, input_dir, npm = [Path(d).resolve() for d in argv[1:]]

try:
generate_runtime(input_dir, output_dir, priv_dir)
generate_runtime(output_dir, priv_dir, input_dir, npm)
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)


def generate_runtime(input_dir, output_dir, priv_dir):
def generate_runtime(output_dir, priv_dir, input_dir, npm):
priv_dir.mkdir(exist_ok=True)

shutil.copy(input_dir / "package.json", priv_dir)
Expand All @@ -31,30 +29,10 @@ def generate_runtime(input_dir, output_dir, priv_dir):
shutil.rmtree(runtime_intdir)
shutil.copytree(runtime_srcdir, runtime_intdir)

npm = os.environ.get("NPM", make_script_filename("npm"))
try:
subprocess.run([npm, "install"], capture_output=True, cwd=priv_dir, check=True)
except Exception as e:
message = "\n".join([
"",
"***",
"Failed to bootstrap the Barebone backend script runtime:",
"\t" + str(e),
"It appears 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)
subprocess.run([npm, "install"], capture_output=True, cwd=priv_dir, check=True)

shutil.copy(priv_dir / "script-runtime.js", output_dir)


def make_script_filename(name):
build_os = platform.system().lower()
extension = ".cmd" if build_os == "windows" else ""
return name + extension


if __name__ == "__main__":
main(sys.argv)
3 changes: 2 additions & 1 deletion src/barebone/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ barebone_script_runtime = custom_target('frida-barebone-script-runtime',
],
command: [
find_program('generate-script-runtime.py'),
meson.current_source_dir(),
meson.current_build_dir(),
'@PRIVATE_DIR@',
meson.current_source_dir(),
npm,
],
)
backend_sources += custom_target('frida-data-barebone',
Expand Down
40 changes: 6 additions & 34 deletions src/compiler/generate-agent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python3

import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
Expand All @@ -21,16 +19,11 @@


def main(argv):
input_dir, output_dir, priv_dir = [Path(d).resolve() for d in argv[1:4]]
host_os_family, host_arch, host_cpu_mode = argv[4:7]
v8_mksnapshot = argv[7]
if v8_mksnapshot != "":
v8_mksnapshot = Path(v8_mksnapshot)
else:
v8_mksnapshot = None
output_dir, priv_dir, input_dir, npm, v8_mksnapshot = [Path(d).resolve() if d else None for d in argv[1:6]]
host_os_family, host_arch, host_cpu_mode = argv[6:9]

try:
generate_agent(input_dir, output_dir, priv_dir, host_os_family, host_arch, host_cpu_mode, v8_mksnapshot)
generate_agent(output_dir, priv_dir, input_dir, npm, v8_mksnapshot, host_os_family, host_arch, host_cpu_mode)
except subprocess.CalledProcessError as e:
print(e, file=sys.stderr)
print("Output:\n\t| " + "\n\t| ".join(e.output.strip().split("\n")), file=sys.stderr)
Expand All @@ -40,9 +33,7 @@ def main(argv):
sys.exit(1)


def generate_agent(input_dir, output_dir, priv_dir, host_os_family, host_arch, host_cpu_mode, v8_mksnapshot):
npm = os.environ.get("NPM", make_script_filename("npm"))

def generate_agent(output_dir, priv_dir, input_dir, npm, v8_mksnapshot, host_os_family, host_arch, host_cpu_mode):
entrypoint = input_dir / "agent-entrypoint.js"
priv_dir.mkdir(exist_ok=True)
for name in INPUTS:
Expand All @@ -58,21 +49,8 @@ def generate_agent(input_dir, output_dir, priv_dir, host_os_family, host_arch, h
"check": True,
}

try:
subprocess.run([npm, "install"], **run_kwargs)
#subprocess.run([npm, "link", "/Users/oleavr/src/frida-compile"], **run_kwargs)
except Exception as e:
message = "\n".join([
"",
"***",
"Failed to bootstrap the compiler agent:",
"\t" + str(e),
"It appears 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)
subprocess.run([npm, "install"], **run_kwargs)
#subprocess.run([npm, "link", "/Users/oleavr/src/frida-compile"], **run_kwargs)

components = ["typescript", "agent-core"]
for component in components:
Expand Down Expand Up @@ -111,11 +89,5 @@ def generate_agent(input_dir, output_dir, priv_dir, host_os_family, host_arch, h
snapshot.write_bytes(b"")


def make_script_filename(name):
build_os = platform.system().lower()
extension = ".cmd" if build_os == "windows" else ""
return name + extension


if __name__ == "__main__":
main(sys.argv)
5 changes: 3 additions & 2 deletions src/compiler/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ if have_compiler_backend
],
command: [
find_program('generate-agent.py'),
meson.current_source_dir(),
meson.current_build_dir(),
'@PRIVATE_DIR@',
meson.current_source_dir(),
npm,
compiler_mksnapshot,
host_os_family,
host_arch_gumjs,
host_cpu_mode,
compiler_mksnapshot,
],
)
compiler_data = custom_target('frida-data-compiler',
Expand Down

0 comments on commit 76c2a86

Please sign in to comment.