1+ #!/usr/bin/env node
2+ /**
3+ * Resolves HYPERLIGHT_CFLAGS for crates that extend hyperlight-js-runtime.
4+ *
5+ * The hyperlight target (x86_64-hyperlight-none) has no libc. QuickJS
6+ * (compiled by rquickjs-sys) needs Hyperlight target stub headers for stdio.h,
7+ * time.h, and unistd.h, plus -D__wasi__=1 to disable pthreads.
8+ * These stubs live in hyperlight-js-runtime/include/.
9+ *
10+ * This script uses cargo metadata to find that include directory
11+ * regardless of whether the crate is a path, git, or registry dep.
12+ *
13+ * Usage:
14+ * export HYPERLIGHT_CFLAGS=$(node resolve-cflags.js --manifest-path=path/to/Cargo.toml)
15+ * cargo hyperlight build --manifest-path path/to/Cargo.toml
16+ *
17+ * Requires: Node.js, cargo
18+ * Output: -I/absolute/path/to/include -D__wasi__=1
19+ */
20+
21+ const { execSync } = require ( "child_process" ) ;
22+ const path = require ( "path" ) ;
23+
24+ const args = process . argv . slice ( 2 ) ;
25+ const manifestArg = args . find ( ( a ) => a . startsWith ( "--manifest-path=" ) ) ;
26+ const manifestPath = manifestArg ? manifestArg . split ( "=" ) [ 1 ] : "Cargo.toml" ;
27+
28+ try {
29+ const meta = JSON . parse (
30+ execSync (
31+ `cargo metadata --format-version 1 --manifest-path ${ manifestPath } ` ,
32+ { encoding : "utf8" , stdio : [ "pipe" , "pipe" , "pipe" ] }
33+ )
34+ ) ;
35+
36+ const pkg = meta . packages . find ( ( p ) => p . name === "hyperlight-js-runtime" ) ;
37+ if ( ! pkg ) {
38+ process . stderr . write (
39+ "Error: hyperlight-js-runtime not found in the dependency tree.\n" +
40+ "Make sure your Cargo.toml depends on hyperlight-js-runtime.\n"
41+ ) ;
42+ process . exit ( 1 ) ;
43+ }
44+
45+ const includeDir = path . join ( path . dirname ( pkg . manifest_path ) , "include" ) ;
46+ console . log ( `-I${ includeDir } -D__wasi__=1` ) ;
47+ } catch ( err ) {
48+ process . stderr . write ( `Failed to resolve CFLAGS: ${ err . message } \n` ) ;
49+ process . exit ( 1 ) ;
50+ }
0 commit comments