Skip to content

Commit 0e2b95f

Browse files
authored
Simplify self.emcc() test method. NFC (#16550)
This avoids adding a default `-o` and therefore falls back to emcc/clang defaults or `foo.o` rather than `foo.c.o`. I also simplified some of the tests that call this function to make them (I think) more readable.
1 parent b2b97c2 commit 0e2b95f

File tree

3 files changed

+33
-49
lines changed

3 files changed

+33
-49
lines changed

tests/common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,10 +904,10 @@ def run_process(self, cmd, check=True, **args):
904904
self.fail(f'subprocess exited with non-zero return code({e.returncode}): `{shared.shlex_join(cmd)}`')
905905

906906
def emcc(self, filename, args=[], output_filename=None, **kwargs):
907-
if output_filename is None:
908-
output_filename = filename + '.o'
909-
try_delete(output_filename)
910-
self.run_process([compiler_for(filename), filename] + args + ['-o', output_filename], **kwargs)
907+
cmd = [compiler_for(filename), filename] + args
908+
if output_filename:
909+
cmd += ['-o', output_filename]
910+
self.run_process(cmd, **kwargs)
911911

912912
# Shared test code between main suite and others
913913

tests/test_core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,10 @@ def test_multiply_defined_symbols(self):
853853
self.emcc('b2.c', ['-c'])
854854
self.emcc('main.c', ['-c'])
855855

856-
building.emar('cr', 'liba.a', ['a1.c.o', 'a2.c.o'])
857-
building.emar('cr', 'libb.a', ['b1.c.o', 'b2.c.o'])
856+
building.emar('cr', 'liba.a', ['a1.o', 'a2.o'])
857+
building.emar('cr', 'libb.a', ['b1.o', 'b2.o'])
858858

859-
building.link_to_object(['main.c.o', 'liba.a', 'libb.a'], 'all.o')
859+
building.link_to_object(['main.o', 'liba.a', 'libb.a'], 'all.o')
860860

861861
self.emcc('all.o', self.get_emcc_args(), 'all.js')
862862
self.do_run('all.js', 'result: 1', no_build=True)

tests/test_other.py

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,10 @@ def test_symlink_has_bad_suffix(self):
992992
self.assertContained('unknown file type: foobar.xxx', err)
993993

994994
def test_multiply_defined_libsymbols(self):
995-
lib_name = 'libA.c'
996-
a2_name = 'a2.c'
997-
b2_name = 'b2.c'
998-
main_name = 'main.c'
999-
create_file(lib_name, 'int mult() { return 1; }')
1000-
create_file(a2_name, 'void x() {}')
1001-
create_file(b2_name, 'void y() {}')
1002-
create_file(main_name, r'''
995+
create_file('libA.c', 'int mult() { return 1; }')
996+
create_file('a2.c', 'void x() {}')
997+
create_file('b2.c', 'void y() {}')
998+
create_file('main.c', r'''
1003999
#include <stdio.h>
10041000
int mult();
10051001
int main() {
@@ -1008,26 +1004,20 @@ def test_multiply_defined_libsymbols(self):
10081004
}
10091005
''')
10101006

1011-
self.emcc(lib_name, ['-shared'], output_filename='libA.so')
1007+
self.emcc('libA.c', ['-shared'], output_filename='libA.so')
10121008

1013-
self.emcc(a2_name, ['-r', '-L.', '-lA'])
1014-
self.emcc(b2_name, ['-r', '-L.', '-lA'])
1009+
self.emcc('a2.c', ['-r', '-L.', '-lA', '-o', 'a2.o'])
1010+
self.emcc('b2.c', ['-r', '-L.', '-lA', '-o', 'b2.o'])
10151011

1016-
self.emcc(main_name, ['-L.', '-lA', a2_name + '.o', b2_name + '.o'], output_filename='a.out.js')
1012+
self.emcc('main.c', ['-L.', '-lA', 'a2.o', 'b2.o'])
10171013

10181014
self.assertContained('result: 1', self.run_js('a.out.js'))
10191015

10201016
def test_multiply_defined_libsymbols_2(self):
1021-
a = "int x() { return 55; }"
1022-
a_name = 'a.c'
1023-
create_file(a_name, a)
1024-
b = "int y() { return 2; }"
1025-
b_name = 'b.c'
1026-
create_file(b_name, b)
1027-
c = "int z() { return 5; }"
1028-
c_name = 'c.c'
1029-
create_file(c_name, c)
1030-
main = r'''
1017+
create_file('a.c', "int x() { return 55; }")
1018+
create_file('b.c', "int y() { return 2; }")
1019+
create_file('c.c', "int z() { return 5; }")
1020+
create_file('main.c', r'''
10311021
#include <stdio.h>
10321022
int x();
10331023
int y();
@@ -1036,18 +1026,15 @@ def test_multiply_defined_libsymbols_2(self):
10361026
printf("result: %d\n", x() + y() + z());
10371027
return 0;
10381028
}
1039-
'''
1040-
main_name = 'main.c'
1041-
create_file(main_name, main)
1029+
''')
10421030

1043-
self.emcc(a_name, ['-c']) # a.c.o
1044-
self.emcc(b_name, ['-c']) # b.c.o
1045-
self.emcc(c_name, ['-c']) # c.c.o
1046-
lib_name = 'libLIB.a'
1047-
building.emar('cr', lib_name, [a_name + '.o', b_name + '.o']) # libLIB.a with a and b
1031+
self.emcc('a.c', ['-c']) # a.o
1032+
self.emcc('b.c', ['-c']) # b.o
1033+
self.emcc('c.c', ['-c']) # c.o
1034+
building.emar('cr', 'libLIB.a', ['a.o', 'b.o']) # libLIB.a with a and b
10481035

10491036
# a is in the lib AND in an .o, so should be ignored in the lib. We do still need b from the lib though
1050-
self.emcc(main_name, [a_name + '.o', c_name + '.o', '-L.', '-lLIB'], output_filename='a.out.js')
1037+
self.emcc('main.c', ['a.o', 'c.o', '-L.', '-lLIB'])
10511038

10521039
self.assertContained('result: 62', self.run_js('a.out.js'))
10531040

@@ -1063,9 +1050,9 @@ def test_link_group(self):
10631050
}
10641051
''')
10651052

1066-
self.emcc('lib.c', ['-c']) # lib.c.o
1053+
self.emcc('lib.c', ['-c']) # lib.o
10671054
lib_name = 'libLIB.a'
1068-
building.emar('cr', lib_name, ['lib.c.o']) # libLIB.a with lib.c.o
1055+
building.emar('cr', lib_name, ['lib.o']) # libLIB.a with lib.o
10691056

10701057
def test(compiler, main_name, lib_args, err_expected):
10711058
print(err_expected)
@@ -1350,9 +1337,7 @@ def test_identical_basenames(self):
13501337

13511338
def test_main_a(self):
13521339
# if main() is in a .a, we need to pull in that .a
1353-
1354-
main_name = 'main.c'
1355-
create_file(main_name, r'''
1340+
create_file('main.c', r'''
13561341
#include <stdio.h>
13571342
extern int f();
13581343
int main() {
@@ -1361,18 +1346,17 @@ def test_main_a(self):
13611346
}
13621347
''')
13631348

1364-
other_name = 'other.c'
1365-
create_file(other_name, r'''
1349+
create_file('other.c', r'''
13661350
#include <stdio.h>
13671351
int f() { return 12346; }
13681352
''')
13691353

1370-
self.run_process([EMCC, main_name, '-c', '-o', main_name + '.o'])
1371-
self.run_process([EMCC, other_name, '-c', '-o', other_name + '.o'])
1354+
self.run_process([EMCC, '-c', 'main.c'])
1355+
self.run_process([EMCC, '-c', 'other.c'])
13721356

1373-
self.run_process([EMAR, 'cr', main_name + '.a', main_name + '.o'])
1357+
self.run_process([EMAR, 'cr', 'libmain.a', 'main.o'])
13741358

1375-
self.run_process([EMCC, other_name + '.o', main_name + '.a'])
1359+
self.run_process([EMCC, 'other.o', 'libmain.a'])
13761360

13771361
self.assertContained('result: 12346.', self.run_js('a.out.js'))
13781362

0 commit comments

Comments
 (0)