Skip to content

Commit c735bb6

Browse files
committed
do not apply -l if not generating an executable or a library
1 parent 30f484d commit c735bb6

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

emcc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ for i in range(len(sys.argv)-1):
540540
sys.argv = sys.argv[:i] + sys.argv[i+2:]
541541
break
542542

543+
specified_target = target
544+
target = specified_target if specified_target is not None else 'a.out.js' # specified_target is the user-specified one, target is what we will generate
545+
target_basename = unsuffixed_basename(target)
546+
547+
if '.' in target:
548+
final_suffix = target.split('.')[-1]
549+
else:
550+
final_suffix = ''
551+
543552
if header: # header or such
544553
if len(sys.argv) >= 3: # if there is a source and a target, then copy, otherwise do nothing
545554
sys.argv = filter(lambda arg: not arg.startswith('-I'), sys.argv)
@@ -789,6 +798,17 @@ try:
789798

790799
newargs = [ arg for arg in newargs if arg is not '' ]
791800

801+
# -c means do not link in gcc, and for us, the parallel is to not go all the way to JS, but stop at bitcode
802+
has_dash_c = '-c' in newargs
803+
if has_dash_c:
804+
assert has_source_inputs, 'Must have source code inputs to use -c'
805+
target = target_basename + '.o'
806+
final_suffix = 'o'
807+
808+
# do not link in libs when just generating object code (not an 'executable', i.e. JS, or a library)
809+
if ('.' + final_suffix) in BITCODE_SUFFIXES:
810+
libs = []
811+
792812
# Find library files
793813
for lib in libs:
794814
if DEBUG: print >> sys.stderr, 'emcc: looking for library "%s"' % lib
@@ -816,22 +836,6 @@ try:
816836

817837
newargs += CC_ADDITIONAL_ARGS
818838

819-
specified_target = target
820-
target = specified_target if specified_target is not None else 'a.out.js' # specified_target is the user-specified one, target is what we will generate
821-
822-
target_basename = unsuffixed_basename(target)
823-
824-
# -c means do not link in gcc, and for us, the parallel is to not go all the way to JS, but stop at bitcode
825-
has_dash_c = '-c' in newargs
826-
if has_dash_c:
827-
assert has_source_inputs, 'Must have source code inputs to use -c'
828-
target = target_basename + '.o'
829-
830-
if '.' in target:
831-
final_suffix = target.split('.')[-1]
832-
else:
833-
final_suffix = ''
834-
835839
assert not (Compression.on and final_suffix != 'html'), 'Compression only works when generating HTML'
836840

837841
# If we are using embind and generating JS, now is the time to link in bind.cpp
@@ -904,7 +908,7 @@ try:
904908
assert len(original_input_files) == 1 or not has_dash_c, 'fatal error: cannot specify -o with -c with multiple files' + str(sys.argv) + ':' + str(original_input_files)
905909
# We have a specified target (-o <target>), which is not JavaScript or HTML, and
906910
# we have multiple files: Link them
907-
if DEBUG: print >> sys.stderr, 'emcc: link: ' + str(temp_files)
911+
if DEBUG: print >> sys.stderr, 'emcc: link: ' + str(temp_files), specified_target
908912
shared.Building.link(temp_files, specified_target, remove_duplicates=remove_duplicates)
909913
exit(0)
910914

tests/runner.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7850,6 +7850,36 @@ def test_l_link(self):
78507850
self.assertContained('hello from lib', run_js(os.path.join(self.get_dir(), 'a.out.js')))
78517851
assert not os.path.exists('a.out') and not os.path.exists('a.exe'), 'Must not leave unneeded linker stubs'
78527852

7853+
def test_multiply_defined_libsymbols(self):
7854+
lib = "int mult() { return 1; }"
7855+
lib_name = os.path.join(self.get_dir(), 'libA.c')
7856+
open(lib_name, 'w').write(lib)
7857+
a2 = "void x() {}"
7858+
a2_name = os.path.join(self.get_dir(), 'a2.c')
7859+
open(a2_name, 'w').write(a2)
7860+
b2 = "void y() {}"
7861+
b2_name = os.path.join(self.get_dir(), 'b2.c')
7862+
open(b2_name, 'w').write(b2)
7863+
main = r'''
7864+
#include <stdio.h>
7865+
int mult();
7866+
int main() {
7867+
printf("result: %d\n", mult());
7868+
return 0;
7869+
}
7870+
'''
7871+
main_name = os.path.join(self.get_dir(), 'main.c')
7872+
open(main_name, 'w').write(main)
7873+
7874+
Building.emcc(lib_name, output_filename='libA.so')
7875+
7876+
Building.emcc(a2_name, ['-L.', '-lA'])
7877+
Building.emcc(b2_name, ['-L.', '-lA'])
7878+
7879+
Building.emcc(main_name, ['-L.', '-lA', a2_name+'.o', b2_name+'.o'], output_filename='a.out.js')
7880+
7881+
self.assertContained('result: 1', run_js(os.path.join(self.get_dir(), 'a.out.js')))
7882+
78537883
def test_abspaths(self):
78547884
# Includes with absolute paths are generally dangerous, things like -I/usr/.. will get to system local headers, not our portable ones.
78557885

0 commit comments

Comments
 (0)