Skip to content

Commit 6c2e5a6

Browse files
authored
Convert all ports to use build_port. NFC (#17822)
This will allow more easy transition to alternative build systems such as ninja (See #17809). Also, update build_port to allow separate source and build directories. This avoids copying the entire source code of each port from the extracts archive into the build directory.
1 parent f9e8147 commit 6c2e5a6

26 files changed

+186
-360
lines changed

test/test_browser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,8 +3285,8 @@ def test_sdl2_mixer_music(self, formats, flags, music_name):
32853285
def test_cocos2d_hello(self):
32863286
# cocos2d build contains a bunch of warnings about tiff symbols being missing at link time:
32873287
# e.g. warning: undefined symbol: TIFFClientOpen
3288-
cocos2d_root = os.path.join(ports.Ports.get_build_dir(), 'cocos2d')
3289-
preload_file = os.path.join(cocos2d_root, 'samples', 'HelloCpp', 'Resources') + '@'
3288+
cocos2d_root = os.path.join(ports.Ports.get_dir(), 'cocos2d', 'Cocos2d-version_3_3')
3289+
preload_file = os.path.join(cocos2d_root, 'samples', 'Cpp', 'HelloCpp', 'Resources') + '@'
32903290
self.btest('cocos2d_hello.cpp', reference='cocos2d_hello.png', reference_slack=1,
32913291
args=['-sUSE_COCOS2D=3', '-sERROR_ON_UNDEFINED_SYMBOLS=0',
32923292
'-Wno-js-compiler',

tools/ports/__init__.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,45 +108,39 @@ def install_headers(src_dir, pattern='*.h', target=None):
108108
shutil.copyfile(f, os.path.join(dest, os.path.basename(f)))
109109

110110
@staticmethod
111-
def build_port(src_path, output_path, includes=[], flags=[], exclude_files=[], exclude_dirs=[]): # noqa
112-
srcs = []
113-
for root, _, files in os.walk(src_path, topdown=False):
114-
if any((excluded in root) for excluded in exclude_dirs):
115-
continue
116-
for f in files:
117-
ext = shared.suffix(f)
118-
if ext in ('.c', '.cpp') and not any((excluded in f) for excluded in exclude_files):
119-
srcs.append(os.path.join(root, f))
120-
include_commands = ['-I' + src_path]
111+
def build_port(src_dir, output_path, build_dir, includes=[], flags=[], exclude_files=[], exclude_dirs=[], srcs=[]): # noqa
112+
if srcs:
113+
srcs = [os.path.join(src_dir, s) for s in srcs]
114+
else:
115+
srcs = []
116+
for root, _, files in os.walk(src_dir, topdown=False):
117+
if any((excluded in root) for excluded in exclude_dirs):
118+
continue
119+
for f in files:
120+
ext = shared.suffix(f)
121+
if ext in ('.c', '.cpp') and not any((excluded in f) for excluded in exclude_files):
122+
srcs.append(os.path.join(root, f))
123+
124+
cflags = system_libs.get_base_cflags() + ['-O2', '-w', '-I' + src_dir] + flags
121125
for include in includes:
122-
include_commands.append('-I' + include)
126+
cflags.append('-I' + include)
123127

128+
if build_dir:
129+
if not os.path.exists(build_dir):
130+
os.makedirs(build_dir)
131+
build_dir = src_dir
124132
commands = []
125133
objects = []
126134
for src in srcs:
127-
obj = src + '.o'
128-
commands.append([shared.EMCC, '-c', src, '-O2', '-o', obj, '-w'] + include_commands + flags)
135+
relpath = os.path.relpath(src, src_dir)
136+
obj = os.path.join(build_dir, relpath) + '.o'
137+
commands.append([shared.EMCC, '-c', src, '-o', obj] + cflags)
129138
objects.append(obj)
130139

131-
Ports.run_commands(commands)
140+
system_libs.run_build_commands(commands)
132141
system_libs.create_lib(output_path, objects)
133142
return output_path
134143

135-
@staticmethod
136-
def run_commands(commands):
137-
# Runs a sequence of compiler commands, adding importand cflags as defined by get_cflags() so
138-
# that the ports are built in the correct configuration.
139-
def add_args(cmd):
140-
# this must only be called on a standard build command
141-
assert cmd[0] in (shared.EMCC, shared.EMXX)
142-
# add standard cflags, but also allow the cmd to override them
143-
return cmd[:1] + system_libs.get_base_cflags() + cmd[1:]
144-
system_libs.run_build_commands([add_args(c) for c in commands])
145-
146-
@staticmethod
147-
def create_lib(libname, inputs): # make easily available for port objects
148-
system_libs.create_lib(libname, inputs)
149-
150144
@staticmethod
151145
def get_dir():
152146
dirname = config.PORTS

tools/ports/boost_headers.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import logging
77
import os
8+
from pathlib import Path
89

910
TAG = '1.75.0'
1011
HASH = '8c38be1ebef1b8ada358ad6b7c9ec17f5e0a300e8085db3473a13e19712c95eeb3c3defacd3c53482eb96368987c4b022efa8da2aac2431a154e40153d3c3dcd'
@@ -20,27 +21,19 @@ def get(ports, settings, shared):
2021

2122
def create(final):
2223
logging.info('building port: boost_headers')
23-
build_dir = ports.clear_project_build('boost_headers')
2424

2525
# includes
2626
source_path_include = os.path.join(ports.get_dir(), 'boost_headers', 'boost')
2727
ports.install_header_dir(source_path_include, 'boost')
2828

2929
# write out a dummy cpp file, to create an empty library
3030
# this is needed as emscripted ports expect this, even if it is not used
31+
build_dir = ports.clear_project_build('boost_headers')
3132
dummy_file = os.path.join(build_dir, 'dummy.cpp')
3233
shared.safe_ensure_dirs(os.path.dirname(dummy_file))
33-
with open(dummy_file, 'w') as f:
34-
f.write('static void dummy() {}')
35-
36-
commands = []
37-
o_s = []
38-
obj = dummy_file + '.o'
39-
command = [shared.EMCC, '-c', dummy_file, '-o', obj]
40-
commands.append(command)
41-
ports.run_commands(commands)
42-
o_s.append(obj)
43-
ports.create_lib(final, o_s)
34+
Path(dummy_file).write_text('static void dummy() {}')
35+
36+
ports.build_port(build_dir, final, build_dir)
4437

4538
return [shared.Cache.get_lib('libboost_headers.a', create, what='port')]
4639

tools/ports/bullet.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def create(final):
2323

2424
source_path = os.path.join(ports.get_dir(), 'bullet', 'Bullet-' + TAG)
2525
dest_path = ports.clear_project_build('bullet')
26-
shutil.copytree(source_path, dest_path)
27-
src_path = os.path.join(dest_path, 'bullet', 'src')
26+
src_path = os.path.join(source_path, 'bullet', 'src')
2827

2928
dest_include_path = ports.get_include_dir('bullet')
3029
for base, _, files in os.walk(src_path):
@@ -42,8 +41,7 @@ def create(final):
4241
for dir in dirs:
4342
includes.append(os.path.join(base, dir))
4443

45-
ports.build_port(src_path, final, includes=includes, exclude_dirs=['MiniCL'],
46-
flags=['-std=gnu++14'])
44+
ports.build_port(src_path, final, dest_path, includes=includes, exclude_dirs=['MiniCL'], flags=['-std=gnu++14'])
4745

4846
return [shared.Cache.get_lib('libbullet.a', create)]
4947

tools/ports/bzip2.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# found in the LICENSE file.
55

66
import os
7-
import shutil
87

98
VERSION = '1.0.6'
109
HASH = '512cbfde5144067f677496452f3335e9368fd5d7564899cb49e77847b9ae7dca598218276637cbf5ec524523be1e8ace4ad36a148ef7f4badf3f6d5a002a4bb2'
@@ -18,26 +17,16 @@ def get(ports, settings, shared):
1817
ports.fetch_project('bzip2', 'https://github.com/emscripten-ports/bzip2/archive/' + VERSION + '.zip', 'bzip2-' + VERSION, sha512hash=HASH)
1918

2019
def create(final):
21-
dest_path = ports.clear_project_build('bzip2')
2220
source_path = os.path.join(ports.get_dir(), 'bzip2', 'bzip2-' + VERSION)
23-
shutil.copytree(source_path, dest_path)
21+
ports.install_headers(source_path)
2422

2523
# build
2624
srcs = [
2725
'blocksort.c', 'compress.c', 'decompress.c', 'huffman.c',
2826
'randtable.c', 'bzlib.c', 'crctable.c',
2927
]
30-
commands = []
31-
o_s = []
32-
for src in srcs:
33-
o = os.path.join(dest_path, shared.replace_suffix(src, '.o'))
34-
shared.safe_ensure_dirs(os.path.dirname(o))
35-
commands.append([shared.EMCC, '-c', os.path.join(dest_path, src), '-O2', '-o', o, '-I' + dest_path, '-w', ])
36-
o_s.append(o)
37-
ports.run_commands(commands)
38-
39-
ports.create_lib(final, o_s)
40-
ports.install_headers(source_path)
28+
dest_path = ports.clear_project_build('bzip2')
29+
ports.build_port(source_path, final, dest_path, srcs=srcs)
4130

4231
return [shared.Cache.get_lib('libbz2.a', create, what='port')]
4332

tools/ports/cocos2d.py

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# found in the LICENSE file.
55

66
import os
7-
import shutil
87
import logging
98
import re
109

@@ -28,49 +27,35 @@ def create(final):
2827
cocos2d_src = os.path.join(ports.get_dir(), 'cocos2d')
2928
cocos2d_root = os.path.join(cocos2d_src, 'Cocos2d-' + TAG)
3029
cocos2dx_root = os.path.join(cocos2d_root, 'cocos2dx')
31-
cocos2dx_src = make_source_list(cocos2d_root, cocos2dx_root)
32-
cocos2dx_includes = make_includes(cocos2d_root)
33-
34-
cocos2d_build = ports.clear_project_build('cocos2d')
35-
shutil.copytree(os.path.join(cocos2d_root, 'samples', 'Cpp'),
36-
os.path.join(cocos2d_build, 'samples'))
37-
38-
commands = []
39-
o_s = []
40-
for src in cocos2dx_src:
41-
o = os.path.join(cocos2d_build, 'Cocos2d-' + TAG, 'build', src + '.o')
42-
shared.safe_ensure_dirs(os.path.dirname(o))
43-
command = [shared.EMCC,
44-
'-c', src,
45-
'-Wno-overloaded-virtual',
46-
'-Wno-deprecated-declarations',
47-
'-D__CC_PLATFORM_FILEUTILS_CPP__',
48-
'-DCC_ENABLE_CHIPMUNK_INTEGRATION',
49-
'-DCC_KEYBOARD_SUPPORT',
50-
'-DGL_ES=1',
51-
'-DNDEBUG', # '-DCOCOS2D_DEBUG=1' 1 - error/warn, 2 - verbose
52-
# Cocos2d source code hasn't switched to __EMSCRIPTEN__.
53-
# See https://github.com/emscripten-ports/Cocos2d/pull/3
54-
'-DEMSCRIPTEN',
55-
'-DCP_USE_DOUBLES=0',
56-
'-O2',
57-
'-sUSE_ZLIB=1',
58-
'-sUSE_LIBPNG=1',
59-
'-o', o, '-w']
60-
61-
for include in cocos2dx_includes:
62-
command.append('-I' + include)
63-
64-
commands.append(command)
65-
o_s.append(o)
66-
shared.safe_ensure_dirs(os.path.dirname(o_s[0]))
67-
ports.run_commands(commands)
68-
ports.create_lib(final, o_s)
69-
70-
for dirname in cocos2dx_includes:
30+
31+
srcs = make_source_list(cocos2d_root, cocos2dx_root)
32+
includes = make_includes(cocos2d_root)
33+
flags = [
34+
'-Wno-overloaded-virtual',
35+
'-Wno-deprecated-declarations',
36+
'-D__CC_PLATFORM_FILEUTILS_CPP__',
37+
'-DCC_ENABLE_CHIPMUNK_INTEGRATION',
38+
'-DCC_KEYBOARD_SUPPORT',
39+
'-DGL_ES=1',
40+
'-DNDEBUG', # '-DCOCOS2D_DEBUG=1' 1 - error/warn, 2 - verbose
41+
# Cocos2d source code hasn't switched to __EMSCRIPTEN__.
42+
# See https://github.com/emscripten-ports/Cocos2d/pull/3
43+
'-DEMSCRIPTEN',
44+
'-DCP_USE_DOUBLES=0',
45+
'-sUSE_ZLIB=1',
46+
'-sUSE_LIBPNG=1',
47+
]
48+
build_dir = ports.clear_project_build('cocos2d')
49+
50+
for dirname in includes:
7151
target = os.path.join('cocos2d', os.path.relpath(dirname, cocos2d_root))
7252
ports.install_header_dir(dirname, target=target)
7353

54+
ports.build_port(cocos2d_src, final, build_dir,
55+
flags=flags,
56+
includes=includes,
57+
srcs=srcs)
58+
7459
return [shared.Cache.get_lib('libcocos2d.a', create, what='port')]
7560

7661

tools/ports/freetype.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# found in the LICENSE file.
55

66
import os
7-
import shutil
87
from pathlib import Path
98

109
TAG = 'version_1'
@@ -19,10 +18,10 @@ def get(ports, settings, shared):
1918
ports.fetch_project('freetype', 'https://github.com/emscripten-ports/FreeType/archive/' + TAG + '.zip', 'FreeType-' + TAG, sha512hash=HASH)
2019

2120
def create(final):
22-
dest_path = ports.clear_project_build('freetype')
2321
source_path = os.path.join(ports.get_dir(), 'freetype', 'FreeType-' + TAG)
24-
shutil.copytree(source_path, dest_path)
25-
Path(dest_path, 'include/ftconfig.h').write_text(ftconf_h)
22+
Path(source_path, 'include/ftconfig.h').write_text(ftconf_h)
23+
ports.install_header_dir(os.path.join(source_path, 'include'),
24+
target=os.path.join('freetype2'))
2625

2726
# build
2827
srcs = ['src/autofit/autofit.c',
@@ -77,31 +76,23 @@ def create(final):
7776
'src/type1/type1.c',
7877
'src/type42/type42.c',
7978
'src/winfonts/winfnt.c']
80-
commands = []
81-
o_s = []
82-
for src in srcs:
83-
o = os.path.join(dest_path, shared.replace_suffix(src, '.o'))
84-
shared.safe_ensure_dirs(os.path.dirname(o))
85-
commands.append([shared.EMCC, '-c', os.path.join(dest_path, src), '-o', o,
86-
'-DFT2_BUILD_LIBRARY', '-O2',
87-
'-I' + dest_path + '/include',
88-
'-I' + dest_path + '/truetype',
89-
'-I' + dest_path + '/sfnt',
90-
'-I' + dest_path + '/autofit',
91-
'-I' + dest_path + '/smooth',
92-
'-I' + dest_path + '/raster',
93-
'-I' + dest_path + '/psaux',
94-
'-I' + dest_path + '/psnames',
95-
'-I' + dest_path + '/truetype',
96-
'-w',
97-
'-pthread'])
98-
o_s.append(o)
99-
100-
ports.run_commands(commands)
101-
ports.create_lib(final, o_s)
102-
103-
ports.install_header_dir(os.path.join(dest_path, 'include'),
104-
target=os.path.join('freetype2'))
79+
80+
flags = [
81+
'-DFT2_BUILD_LIBRARY',
82+
'-I' + source_path + '/include',
83+
'-I' + source_path + '/truetype',
84+
'-I' + source_path + '/sfnt',
85+
'-I' + source_path + '/autofit',
86+
'-I' + source_path + '/smooth',
87+
'-I' + source_path + '/raster',
88+
'-I' + source_path + '/psaux',
89+
'-I' + source_path + '/psnames',
90+
'-I' + source_path + '/truetype',
91+
'-pthread'
92+
]
93+
94+
dest_path = ports.clear_project_build('freetype')
95+
ports.build_port(source_path, final, dest_path, flags=flags, srcs=srcs)
10596

10697
return [shared.Cache.get_lib('libfreetype.a', create, what='port')]
10798

tools/ports/giflib.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# found in the LICENSE file.
55

66
import os
7-
import shutil
87
import logging
98

109
VERSION = '5.2.1'
@@ -20,14 +19,10 @@ def get(ports, settings, shared):
2019

2120
def create(final):
2221
logging.info('building port: giflib')
23-
2422
source_path = os.path.join(ports.get_dir(), 'giflib', f'giflib-{VERSION}')
2523
dest_path = ports.clear_project_build('giflib')
26-
shutil.copytree(source_path, dest_path)
27-
28-
ports.install_headers(dest_path)
29-
30-
ports.build_port(dest_path, final)
24+
ports.install_headers(source_path)
25+
ports.build_port(source_path, final, dest_path)
3126

3227
return [shared.Cache.get_lib('libgif.a', create, what='port')]
3328

tools/ports/harfbuzz.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def get(ports, settings, shared):
8787

8888
def create(final):
8989
logging.info('building port: harfbuzz')
90-
build_path = ports.clear_project_build('harfbuzz')
9190

9291
source_path = os.path.join(ports.get_dir(), 'harfbuzz', 'harfbuzz-' + VERSION)
9392
freetype_include = ports.get_include_dir('freetype2')
@@ -131,15 +130,8 @@ def create(final):
131130
else:
132131
cflags.append('-DHB_NO_MT')
133132

134-
commands = []
135-
o_s = []
136-
for src in srcs:
137-
o = os.path.join(build_path, src + '.o')
138-
shared.safe_ensure_dirs(os.path.dirname(o))
139-
commands.append([shared.EMCC, '-c', os.path.join(source_path, 'src', src), '-o', o] + cflags)
140-
o_s.append(o)
141-
ports.run_commands(commands)
142-
ports.create_lib(final, o_s)
133+
build_dir = ports.clear_project_build('harfbuzz')
134+
ports.build_port(os.path.join(source_path, 'src'), final, build_dir, flags=cflags, srcs=srcs)
143135

144136
return [shared.Cache.get_lib(get_lib_name(settings), create, what='port')]
145137

0 commit comments

Comments
 (0)