@@ -10,9 +10,9 @@ def dbg(*argv, **kw):
10
10
return print (* argv , ** kw )
11
11
12
12
13
- SDKROOT = Path (os .environ .get ("SDKROOT" ,"/opt/python-wasm-sdk" ))
14
- EMSDK = Path (os .environ .get ("EMSDK" ,"/opt/python-wasm-sdk/emsdk" ))
15
- PREFIX = Path (os .environ .get ("PREFIX" ,"/opt/python-wasm-sdk/devices/emsdk/usr" ))
13
+ SDKROOT = Path (os .environ .get ("SDKROOT" , "/opt/python-wasm-sdk" ))
14
+ EMSDK = Path (os .environ .get ("EMSDK" , "/opt/python-wasm-sdk/emsdk" ))
15
+ PREFIX = Path (os .environ .get ("PREFIX" , "/opt/python-wasm-sdk/devices/emsdk/usr" ))
16
16
17
17
sys .argv .pop (0 )
18
18
@@ -120,8 +120,12 @@ COPTS = env("COPTS", "-O0 -g3")
120
120
121
121
122
122
MAIN_MODULE = LINKING = False
123
- EXE = ""
123
+
124
+ EXE = False
125
+ HTML = False
126
+
124
127
MODE = ""
128
+ AOUT = ""
125
129
126
130
SKIP = False
127
131
COMPILE = False
@@ -136,20 +140,17 @@ for argc, arg in enumerate(sys.argv):
136
140
break
137
141
138
142
if arg .startswith ("CMakeFiles/" ):
139
- # SKIP = True
140
- # break
141
143
CMAKE = True
142
144
143
- if arg .startswith (' --preload-file' ) or arg .startswith (' --embed-file' ):
145
+ if arg .startswith (" --preload-file" ) or arg .startswith (" --embed-file" ):
144
146
USE_RAWFS = False
145
147
146
148
if arg .find ("MAIN_MODULE" ) > 0 :
147
149
MAIN_MODULE = True
148
- elif EXE .endswith (".cjs" ) or EXE .endswith (".js" ) or EXE .endswith (".html" ):
149
- MAIN_MODULE = True
150
150
151
151
if arg == "-sENVIRONMENT=web" :
152
152
EXE = False
153
+ HTML = True
153
154
154
155
if arg .lower () in ("-fpic" , "-latomic" ):
155
156
continue
@@ -177,11 +178,11 @@ for argc, arg in enumerate(sys.argv):
177
178
COMPILE = True
178
179
# TODO maybe add node runner for a .cjs
179
180
elif arg == "-o" :
180
- EXE_POS = argc + 1
181
+ out_pos = argc + 1
181
182
if IS_SHARED :
182
- SHARED_TARGET = sys .argv [EXE_POS ]
183
- elif EXE == "" :
184
- EXE = sys .argv [EXE_POS ]
183
+ SHARED_TARGET = sys .argv [out_pos ]
184
+ elif not AOUT :
185
+ AOUT = sys .argv [out_pos ]
185
186
186
187
elif arg .endswith (".so" ) or arg == "-shared" or arg .find ("SIDE_MODULE" ) > 0 :
187
188
IS_SHARED = True
@@ -197,15 +198,18 @@ for argc, arg in enumerate(sys.argv):
197
198
continue
198
199
199
200
if arg .startswith ("-l" ):
200
- LINKING = True
201
-
202
- # prevent duplicates objects/archives files on cmdline when linking
203
- if not CMAKE and (LINKING or MODE == "-o" ):
204
- if arg .endswith (".a" ) or arg .endswith (".o" ) or arg .startswith ("-l" ):
205
- if arg in out :
206
- continue
207
-
208
- # that is for some very bad setup.py behaviour regarding cross compiling.
201
+ LINKING = True
202
+
203
+ # duplicates can happen on cmake but they are expected to be handled correctly so skip
204
+ if not CMAKE :
205
+ # prevent duplicates objects/archives files on cmdline when linking
206
+ if LINKING or MODE == "-o" :
207
+ if arg .endswith (".a" ) or arg .endswith (".o" ) or arg .startswith ("-l" ):
208
+ if arg in out :
209
+ continue
210
+
211
+ # FAILSAFE
212
+ # that is for some very bad known setup.py behaviour regarding cross compiling and some old codebases.
209
213
# should not be needed ..
210
214
if arg .startswith ("-I/usr/" ):
211
215
continue
@@ -251,48 +255,76 @@ if CONFIGURE and len(out) == 1:
251
255
if SKIP :
252
256
final .extend (sys .argv )
253
257
else :
254
- # should not happen
255
- if EXE :
256
- if EXE .endswith ('.o' ) and '-c' not in out :
257
- final .append ('-c' )
258
+ if AOUT :
259
+ # should not happen
260
+ if AOUT .endswith (".o" ) and "-c" not in out :
261
+ final .append ("-c" )
258
262
EXE = False
259
263
MAIN_MODULE = False
260
- elif EXE .endswith ('.html' ):
264
+ elif AOUT .endswith (".html" ):
265
+ MAIN_MODULE = True
266
+ EXE = False
267
+ HTML = True
268
+
269
+ # emscripten aware build
270
+ elif AOUT .endswith (".cjs" ) or AOUT .endswith (".js" ):
271
+ MAIN_MODULE = True
272
+ EXE = True
273
+ # a.out, could be a standalone from config/cmake
274
+ elif not CONFIGURE and not CMAKE and "-c" not in out :
275
+ EXE = True
276
+ else :
261
277
EXE = False
262
-
263
278
if EXE :
264
- if EXE .endswith (".cjs" ) or EXE .endswith (".js" ):
279
+ if AOUT .endswith (".cjs" ) or AOUT .endswith (".js" ):
280
+
265
281
def make_exe (* argv , ** kw ):
266
- global CONFIGURE
267
- if os .path .isfile (EXE ) and not CONFIGURE :
268
- with open (EXE , "r" ) as file :
269
- bin = file .read ()
270
- with open (EXE , "w" ) as file :
271
- file .write ("#!/usr/bin/env node\n " )
272
- file .write (bin )
273
- os .chmod (EXE , 0o766 )
282
+ global AOUT , CONFIGURE
283
+ if os .path .isfile (AOUT ) and not CONFIGURE :
284
+ try :
285
+ with open (AOUT , "r" ) as file :
286
+ bin = file .read ()
287
+ with open (AOUT , "w" ) as file :
288
+ file .write ("#!/usr/bin/env node\n " )
289
+ file .write (bin )
290
+ os .chmod (AOUT , 0o766 )
291
+ except Exception as e :
292
+ dbg ("ERROR: 292" , e )
293
+
274
294
# the build system is old and exe has no suffix from cmake or configure
275
295
else :
296
+
276
297
def make_exe (* argv , ** kw ):
277
- global CONFIGURE
278
- if os .path .isfile (EXE ) and os .path .isfile (EXE + '.wasm' ) and not CONFIGURE :
279
- os .rename (EXE , EXE + ".cjs" )
280
- with open (EXE , "w" ) as file :
281
- file .write ("#!/usr/bin/env bash\n " )
282
- file .write ('node $0.cjs "$@"\n ' )
283
- os .chmod (EXE , 0o766 )
298
+ global AOUT , CONFIGURE
299
+ if os .path .isfile (AOUT ) and os .path .isfile (AOUT + ".wasm" ) and not CONFIGURE :
300
+ os .rename (AOUT , AOUT + ".cjs" )
301
+ try :
302
+ with open (AOUT , "w" ) as file :
303
+ file .write ("#!/usr/bin/env bash\n " )
304
+ file .write ('node $0.cjs "$@"\n ' )
305
+ except Exception as e :
306
+ dbg ("ERROR: 306" , e )
307
+ os .rename (AOUT + ".cjs" , AOUT )
308
+ return
309
+ try :
310
+ os .chmod (AOUT , 0o766 )
311
+ except Exception as e :
312
+ dbg ("ERROR: 312" , e )
284
313
285
314
final .append ("-sENVIRONMENT=node" )
286
315
287
316
# error: explicitly setting EXIT_RUNTIME not compatible with STANDALONE_WASM.
288
317
# EXIT_RUNTIME will always be True for programs (with a main function) and False for reactors (not main function).
289
- #final.append("-sEXIT_RUNTIME")
318
+ # final.append("-sEXIT_RUNTIME")
290
319
291
320
if USE_RAWFS :
292
- #final.append("-sASYNCIFY")
321
+ # final.append("-sASYNCIFY")
293
322
final .append ("-sNODERAWFS" )
294
323
295
324
__import__ ("atexit" ).register (make_exe )
325
+ elif HTML :
326
+ if "-sENVIRONMENT=web" not in out :
327
+ final .append ("-sENVIRONMENT=web" )
296
328
297
329
# do not pass WASM opts when -c/-o but always PIC and opt level
298
330
final .extend (arglist ("-fPIC" , SHARED , COPTS ))
@@ -316,15 +348,16 @@ else:
316
348
final .extend (COMMON )
317
349
318
350
319
-
320
351
sys .path .insert (0 , str (Path (EXEC ).parent ))
321
352
sys .argv .clear ()
322
353
323
354
EMCC_TRACE = env ("EMCC_TRACE" , false )
324
355
if EMCC_TRACE :
325
356
DEBUG_PATTERN = env ("DEBUG_PATTERN" , "main" )
357
+
326
358
def dump ():
327
- dbg (f"""
359
+ dbg (
360
+ f"""
328
361
{ COMMON = }
329
362
330
363
{ CPU = }
@@ -341,20 +374,20 @@ if EMCC_TRACE:
341
374
342
375
{ ' ' .join (sys .argv )}
343
376
"""
344
- )
377
+ )
345
378
346
379
while len (final ):
347
380
arg = final .pop (0 )
348
381
# add debug filters here.
349
382
350
383
sys .argv .append (arg )
351
384
352
-
385
+ # for debugging configure, cmake has its own very detailed log.
353
386
if os .path .isfile ("conftest.c" ):
354
387
__import__ ("shutil" ).copy ("conftest.c" , SDKROOT / "emcc.c" )
355
- if DEBUG_PATTERN not in (False ,True ):
356
- with open ("conftest.c" ,"r" ) as file :
357
- if file .read ().find (DEBUG_PATTERN )> 0 :
388
+ if DEBUG_PATTERN not in (False , True ):
389
+ with open ("conftest.c" , "r" ) as file :
390
+ if file .read ().find (DEBUG_PATTERN ) > 0 :
358
391
dump ()
359
392
360
393
else :
0 commit comments