@@ -7,12 +7,19 @@ const path = require("node:path");
77const archs = [ "arm64" , "x86_64" ] ;
88
99async function main ( ) {
10- const scriptDir = __dirname ;
11- const libffiDir = path . resolve ( scriptDir , ".." , "libffi" ) ;
12- process . chdir ( libffiDir ) ;
13-
14- process . env . CC = "clang" ;
15- process . env . CFLAGS = "-w" ;
10+ const libffiDir = path . resolve ( __dirname , ".." , "libffi" ) ;
11+ const libffiPath = ( ...parts ) => path . join ( libffiDir , ...parts ) ;
12+ const env = {
13+ ...process . env ,
14+ CC : "clang" ,
15+ CFLAGS : "-w" ,
16+ } ;
17+
18+ try {
19+ await fs . access ( libffiPath ( "configure" ) ) ;
20+ } catch {
21+ run ( "sh" , [ "./autogen.sh" ] , { cwd : libffiDir , env } ) ;
22+ }
1623
1724 const skipGenerateSource =
1825 Boolean ( process . env . SKIP_GENERATE_SOURCE ) ||
@@ -23,7 +30,7 @@ async function main() {
2330 run ( "python" , [
2431 "generate-darwin-source-and-headers.py" ,
2532 `--only-${ platform } ` ,
26- ] ) ;
33+ ] , { cwd : libffiDir , env } ) ;
2734 }
2835 }
2936
@@ -34,54 +41,57 @@ async function main() {
3441 "macosx-x86_64" ,
3542 "macosx-arm64" ,
3643 ] ) {
37- run ( "make" , [ "-C" , `build_${ dir } ` , "install" ] ) ;
44+ run ( "make" , [ "-C" , `build_${ dir } ` , "install" ] , { cwd : libffiDir , env } ) ;
3845 }
3946
40- await prepareDir ( "prebuilt/ iphoneos-arm64/ include" ) ;
47+ await prepareDir ( libffiPath ( "prebuilt" , " iphoneos-arm64" , " include") ) ;
4148 await fs . copyFile (
42- "build_iphoneos-arm64/ include/ ffi.h" ,
43- "prebuilt/ iphoneos-arm64/ include/ ffi.h" ,
49+ libffiPath ( "build_iphoneos-arm64" , " include" , " ffi.h") ,
50+ libffiPath ( "prebuilt" , " iphoneos-arm64" , " include" , " ffi.h") ,
4451 ) ;
4552 await fs . copyFile (
46- "build_iphoneos-arm64/ include/ ffitarget.h" ,
47- "prebuilt/ iphoneos-arm64/ include/ ffitarget.h" ,
53+ libffiPath ( "build_iphoneos-arm64" , " include" , " ffitarget.h") ,
54+ libffiPath ( "prebuilt" , " iphoneos-arm64" , " include" , " ffitarget.h") ,
4855 ) ;
4956 await fs . copyFile (
50- "build_iphoneos-arm64/ .libs/ libffi_convenience.a" ,
51- "prebuilt/ iphoneos-arm64/ libffi.a" ,
57+ libffiPath ( "build_iphoneos-arm64" , " .libs" , " libffi_convenience.a") ,
58+ libffiPath ( "prebuilt" , " iphoneos-arm64" , " libffi.a") ,
5259 ) ;
5360
54- await prepareDir ( "prebuilt/ macosx-universal/ include" ) ;
55- await combineHeaders ( "macosx" ) ;
61+ await prepareDir ( libffiPath ( "prebuilt" , " macosx-universal" , " include") ) ;
62+ await combineHeaders ( "macosx" , libffiPath ) ;
5663 run ( "lipo" , [
5764 "-create" ,
5865 "-output" ,
5966 "prebuilt/macosx-universal/libffi.a" ,
6067 "build_macosx-x86_64/.libs/libffi_convenience.a" ,
6168 "build_macosx-arm64/.libs/libffi_convenience.a" ,
62- ] ) ;
69+ ] , { cwd : libffiDir , env } ) ;
6370
64- await prepareDir ( "prebuilt/iphonesimulator-universal/include" ) ;
71+ await prepareDir (
72+ libffiPath ( "prebuilt" , "iphonesimulator-universal" , "include" ) ,
73+ ) ;
6574 run ( "lipo" , [
6675 "-create" ,
6776 "-output" ,
6877 "prebuilt/iphonesimulator-universal/libffi.a" ,
6978 "build_iphonesimulator-x86_64/.libs/libffi_convenience.a" ,
7079 "build_iphonesimulator-arm64/.libs/libffi_convenience.a" ,
71- ] ) ;
72- await combineHeaders ( "iphonesimulator" ) ;
80+ ] , { cwd : libffiDir , env } ) ;
81+ await combineHeaders ( "iphonesimulator" , libffiPath ) ;
7382}
7483
75- function run ( command , args ) {
84+ function run ( command , args , options = { } ) {
7685 console . log ( `$ ${ command } ${ args . join ( " " ) } ` ) ;
7786 const result = spawnSync ( command , args , {
7887 stdio : "inherit" ,
79- env : process . env ,
88+ env : options . env ?? process . env ,
89+ cwd : options . cwd ,
8090 } ) ;
8191
8292 if ( result . status !== 0 ) {
8393 throw new Error (
84- `Command failed: ${ command } ${ args . join ( " " ) } (exit code ${ result . status } )` ,
94+ `Command failed: ${ command } ${ args . join ( " " ) } (exit code ${ result . status } )${ options . cwd ? ` in ${ options . cwd } ` : "" } ` ,
8595 ) ;
8696 }
8797}
@@ -92,21 +102,21 @@ async function prepareDir(includePath) {
92102 await fs . mkdir ( includePath , { recursive : true } ) ;
93103}
94104
95- async function combineHeaders ( target ) {
105+ async function combineHeaders ( target , libffiPath ) {
96106 const ffi_h_arm64 = await fs . readFile (
97- `build_${ target } -${ archs [ 0 ] } / include/ ffi.h` ,
107+ libffiPath ( `build_${ target } -${ archs [ 0 ] } ` , " include" , " ffi.h" ) ,
98108 "utf8" ,
99109 ) ;
100110 const ffi_h_x86_64 = await fs . readFile (
101- `build_${ target } -${ archs [ 1 ] } / include/ ffi.h` ,
111+ libffiPath ( `build_${ target } -${ archs [ 1 ] } ` , " include" , " ffi.h" ) ,
102112 "utf8" ,
103113 ) ;
104114 const ffitarget_h_arm64 = await fs . readFile (
105- `build_${ target } -${ archs [ 0 ] } / include/ ffitarget.h` ,
115+ libffiPath ( `build_${ target } -${ archs [ 0 ] } ` , " include" , " ffitarget.h" ) ,
106116 "utf8" ,
107117 ) ;
108118 const ffitarget_h_x86_64 = await fs . readFile (
109- `build_${ target } -${ archs [ 1 ] } / include/ ffitarget.h` ,
119+ libffiPath ( `build_${ target } -${ archs [ 1 ] } ` , " include" , " ffitarget.h" ) ,
110120 "utf8" ,
111121 ) ;
112122
@@ -134,9 +144,12 @@ async function combineHeaders(target) {
134144 #endif
135145 ` ;
136146
137- await fs . writeFile ( `prebuilt/${ target } -universal/include/ffi.h` , ffi_h_universal ) ;
138147 await fs . writeFile (
139- `prebuilt/${ target } -universal/include/ffitarget.h` ,
148+ libffiPath ( "prebuilt" , `${ target } -universal` , "include" , "ffi.h" ) ,
149+ ffi_h_universal ,
150+ ) ;
151+ await fs . writeFile (
152+ libffiPath ( "prebuilt" , `${ target } -universal` , "include" , "ffitarget.h" ) ,
140153 ffitarget_h_universal ,
141154 ) ;
142155}
0 commit comments