Skip to content

Commit 391775a

Browse files
committed
partial link fix
1 parent aae1349 commit 391775a

File tree

1 file changed

+121
-118
lines changed

1 file changed

+121
-118
lines changed

emsdk-cc

Lines changed: 121 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -147,156 +147,159 @@ NINJA = env("NINJA", false)
147147

148148
out = []
149149

150-
# fix rust calling
151-
for argc, arg in enumerate(sys.argv):
152-
153-
# clean up rustc way of passing args.
154-
155-
if arg in ("-l", "-L", "-I"):
156-
sys.argv[argc] += sys.argv[argc + 1]
157-
sys.argv[argc + 1] = ""
158-
RUSTC = True
159-
160-
while "" in sys.argv:
161-
sys.argv.remove("")
150+
# special partial linking mode
151+
if "-r" in sys.argv:
152+
SKIP = True
153+
else:
154+
# fix rust calling
155+
for argc, arg in enumerate(sys.argv):
162156

163-
for argc, arg in enumerate(sys.argv):
164-
if arg.startswith("CMakeFiles/") or arg.startswith("@CMakeFiles/"):
165-
CMAKE = True
157+
# clean up rustc way of passing args.
166158

167-
if arg.startswith("--preload-file") or arg.startswith("--embed-file"):
168-
USE_RAWFS = False
159+
if arg in ("-l", "-L", "-I"):
160+
sys.argv[argc] += sys.argv[argc + 1]
161+
sys.argv[argc + 1] = ""
162+
RUSTC = True
169163

170-
if arg.find("MAIN_MODULE") > 0:
171-
MAIN_MODULE = True
164+
while "" in sys.argv:
165+
sys.argv.remove("")
172166

173-
if arg == "-static":
174-
STATIC = True
167+
for argc, arg in enumerate(sys.argv):
168+
if arg.startswith("CMakeFiles/") or arg.startswith("@CMakeFiles/"):
169+
CMAKE = True
175170

176-
if arg.startswith("-sENVIRONMENT"):
177-
SIZEOPT = False
178-
USE_RAWFS = False
171+
if arg.startswith("--preload-file") or arg.startswith("--embed-file"):
172+
USE_RAWFS = False
179173

180-
if arg == "-sENVIRONMENT=web":
181-
EXE = False
182-
HTML = True
174+
if arg.find("MAIN_MODULE") > 0:
175+
MAIN_MODULE = True
183176

177+
if arg == "-static":
178+
STATIC = True
184179

185-
for argc, arg in enumerate(sys.argv):
186-
if arg in ("-v", "--version"):
187-
SKIP = True
188-
break
180+
if arg.startswith("-sENVIRONMENT"):
181+
SIZEOPT = False
182+
USE_RAWFS = False
189183

190-
# THEY ARE NOT SAFE TO CHANGE !
191-
if arg in ("-O0", "-O1", "-O2", "-O3", "-Os", "-Oz"):
192-
continue
193-
if arg in ("-g0", "-g1", "-g2", "-g3", "-g4"):
194-
continue
184+
if arg == "-sENVIRONMENT=web":
185+
EXE = False
186+
HTML = True
195187

196-
if arg.endswith("/libpq/libpq.so"):
197-
arg = "-lpq"
198-
# for wasm-ld
199-
sys.argv[argc] = arg
188+
for argc, arg in enumerate(sys.argv):
189+
if arg in ("-v", "--version"):
190+
SKIP = True
191+
break
200192

201-
if not MAIN_MODULE:
202-
# https://github.com/emscripten-core/emscripten/issues/22742
203-
# https://github.com/hoodmane/emscripten/commit/34144634026c91a73bd3e1db85627132d3a37a6d
204-
if arg == "-lc":
193+
# THEY ARE NOT SAFE TO CHANGE !
194+
if arg in ("-O0", "-O1", "-O2", "-O3", "-Os", "-Oz"):
195+
continue
196+
if arg in ("-g0", "-g1", "-g2", "-g3", "-g4"):
205197
continue
206198

207-
# only html may not exit runtime.
208-
if not HTML and arg.find("EXIT_RUNTIME") > 0:
209-
continue
210-
211-
if arg.lower() in ("-fpic", "-latomic"):
212-
continue
199+
if arg.endswith("/libpq/libpq.so"):
200+
arg = "-lpq"
201+
# for wasm-ld
202+
sys.argv[argc] = arg
213203

214-
if arg in ("-Wl,--as-needed", "-Wl,--eh-frame-hdr", "-Wl,-znoexecstack", "-Wl,-znow", "-Wl,-zrelro", "-Wl,-zrelro,-znow"):
215-
continue
204+
if not MAIN_MODULE:
205+
# https://github.com/emscripten-core/emscripten/issues/22742
206+
# https://github.com/hoodmane/emscripten/commit/34144634026c91a73bd3e1db85627132d3a37a6d
207+
if arg == "-lc":
208+
continue
216209

217-
if arg in ("-lgcc", "-lgcc_s", "-fallow-argument-mismatch"):
218-
continue
210+
# only html may not exit runtime.
211+
if not HTML and arg.find("EXIT_RUNTIME") > 0:
212+
continue
219213

220-
if arg == "-pthread":
221-
if MVP:
214+
if arg.lower() in ("-fpic", "-latomic"):
222215
continue
223216

224-
# FAILSAFE
225-
# that is for some very bad known setup.py behaviour regarding cross compiling and some old codebases.
226-
# should not be needed ..
227-
if arg.startswith("-I/"):
228-
if arg.startswith("-I/usr/"):
217+
if arg in ("-Wl,--as-needed", "-Wl,--eh-frame-hdr", "-Wl,-znoexecstack", "-Wl,-znow", "-Wl,-zrelro", "-Wl,-zrelro,-znow"):
229218
continue
230219

231-
if arg.startswith("-L/"):
232-
if arg.startswith("-L/usr/"):
220+
if arg in ("-lgcc", "-lgcc_s", "-fallow-argument-mismatch"):
233221
continue
234222

235-
if arg.find("ASSERTIONS") > 0:
236-
continue
223+
if arg == "-pthread":
224+
if MVP:
225+
continue
237226

238-
# rustc has an habit of "-l" "c" instead of "-lc"
239-
if arg.startswith("-l"):
240-
if len(arg) > 2:
241-
LINKING = True
242-
# prevent duplicate lib when linking
243-
if arg in out:
227+
# FAILSAFE
228+
# that is for some very bad known setup.py behaviour regarding cross compiling and some old codebases.
229+
# should not be needed ..
230+
if arg.startswith("-I/"):
231+
if arg.startswith("-I/usr/"):
244232
continue
245233

246-
elif arg in ("-o", "-c"):
247-
MODE = arg
248-
MODE_POS = argc
249-
if arg == "-c":
250-
COMPILE = True
251-
# TODO maybe add node runner for a .cjs
252-
elif arg == "-o":
253-
out_pos = argc + 1
254-
if IS_SHARED:
255-
SHARED_TARGET = sys.argv[out_pos]
256-
elif not AOUT:
257-
AOUT = sys.argv[out_pos]
258-
259-
elif not STATIC:
260-
if arg.endswith(".so"):
261-
if arg.startswith("/usr/lib"):
262-
arg = f"-l{arg.rsplit('/',1)[-1][3:-3]}"
263-
if arg in ("-lportmidi", "-lporttime"):
264-
continue
234+
if arg.startswith("-L/"):
235+
if arg.startswith("-L/usr/"):
236+
continue
265237

266-
if arg.find("wasm32-emscripten.so") > 0 or arg.find("abi3.so") > 0:
267-
PY_MODULE = true
268-
SHARED_TARGET = arg
269-
270-
# FIX linking .so when it should have been .a for final exe without MAIN_MODULE set
271-
# should be "LINKING" state
272-
# is this arg the -o ? if yes do not even try static
273-
elif out_pos != argc:
274-
# if there is no static version let wasm-ld handle it.
275-
if os.path.isfile(arg[:3] + ".a"):
276-
arg = arg[:3] + ".a"
277-
sys.argv[argc] = arg
278-
elif IS_SHARED:
279-
dbg("WARNING maybe should use static here :", arg)
280-
# raise SystemExit(666)
281-
282-
# TODO check is -shared implies -sSIDE_MODULE=1
283-
elif arg == "-shared":
284-
IS_SHARED = True
285-
SHARED = f"-shared -sASSERTIONS=0 -sSIDE_MODULE=1 -L{os.environ['PREFIX']}/lib"
238+
if arg.find("ASSERTIONS") > 0:
286239
continue
287240

288-
# duplicates can happen on cmake/rustc but they are expected to be here for a reason so skip them
289-
if not (CMAKE or NINJA or RUSTC):
290-
# prevent duplicates objects/archives files on cmdline when linking
291-
if LINKING or MODE == "-o":
292-
if arg.endswith(".a") or arg.endswith(".o"):
241+
# rustc has an habit of "-l" "c" instead of "-lc"
242+
if arg.startswith("-l"):
243+
if len(arg) > 2:
244+
LINKING = True
245+
# prevent duplicate lib when linking
293246
if arg in out:
294247
continue
295248

296-
# fix sysroot is not default to PIC
297-
arg = arg.replace("/lib/wasm32-emscripten/lib", "/lib/wasm32-emscripten/pic/lib")
249+
elif arg in ("-o", "-c"):
250+
MODE = arg
251+
MODE_POS = argc
252+
if arg == "-c":
253+
COMPILE = True
254+
# TODO maybe add node runner for a .cjs
255+
elif arg == "-o":
256+
out_pos = argc + 1
257+
if IS_SHARED:
258+
SHARED_TARGET = sys.argv[out_pos]
259+
elif not AOUT:
260+
AOUT = sys.argv[out_pos]
261+
262+
elif not STATIC:
263+
if arg.endswith(".so"):
264+
if arg.startswith("/usr/lib"):
265+
arg = f"-l{arg.rsplit('/',1)[-1][3:-3]}"
266+
if arg in ("-lportmidi", "-lporttime"):
267+
continue
268+
269+
if arg.find("wasm32-emscripten.so") > 0 or arg.find("abi3.so") > 0:
270+
PY_MODULE = true
271+
SHARED_TARGET = arg
272+
273+
# FIX linking .so when it should have been .a for final exe without MAIN_MODULE set
274+
# should be "LINKING" state
275+
# is this arg the -o ? if yes do not even try static
276+
elif out_pos != argc:
277+
# if there is no static version let wasm-ld handle it.
278+
if os.path.isfile(arg[:3] + ".a"):
279+
arg = arg[:3] + ".a"
280+
sys.argv[argc] = arg
281+
elif IS_SHARED:
282+
dbg("WARNING maybe should use static here :", arg)
283+
# raise SystemExit(666)
284+
285+
# TODO check is -shared implies -sSIDE_MODULE=1
286+
elif arg == "-shared":
287+
IS_SHARED = True
288+
SHARED = f"-shared -sASSERTIONS=0 -sSIDE_MODULE=1 -L{os.environ['PREFIX']}/lib"
289+
continue
290+
291+
# duplicates can happen on cmake/rustc but they are expected to be here for a reason so skip them
292+
if not (CMAKE or NINJA or RUSTC):
293+
# prevent duplicates objects/archives files on cmdline when linking
294+
if LINKING or MODE == "-o":
295+
if arg.endswith(".a") or arg.endswith(".o"):
296+
if arg in out:
297+
continue
298+
299+
# fix sysroot is not default to PIC
300+
arg = arg.replace("/lib/wasm32-emscripten/lib", "/lib/wasm32-emscripten/pic/lib")
298301

299-
out.append(arg)
302+
out.append(arg)
300303

301304
os.environ.pop("_EMCC_CCACHE", "")
302305

0 commit comments

Comments
 (0)