Skip to content

Commit 57c61a5

Browse files
authored
Remove some code duplication between the ports (#17513)
There was a bunch of redundant calls to `try_delete` and by having `clear_project_build()` return the build directory we can avoid recalculating the build directory. Split out from #17512
1 parent ceb41cf commit 57c61a5

24 files changed

+38
-78
lines changed

tools/ports/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ def up_to_date():
266266
def clear_project_build(name):
267267
port = ports_by_name[name]
268268
port.clear(Ports, settings, shared)
269-
shared.try_delete(os.path.join(Ports.get_build_dir(), name))
269+
build_dir = os.path.join(Ports.get_build_dir(), name)
270+
shared.try_delete(build_dir)
271+
return build_dir
270272

271273

272274
def dependency_order(port_list):

tools/ports/boost_headers.py

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

66
import logging
77
import os
8-
import shutil
98

109
TAG = '1.75.0'
1110
HASH = '8c38be1ebef1b8ada358ad6b7c9ec17f5e0a300e8085db3473a13e19712c95eeb3c3defacd3c53482eb96368987c4b022efa8da2aac2431a154e40153d3c3dcd'
@@ -21,17 +20,15 @@ def get(ports, settings, shared):
2120

2221
def create(final):
2322
logging.info('building port: boost_headers')
24-
ports.clear_project_build('boost_headers')
23+
build_dir = ports.clear_project_build('boost_headers')
2524

2625
# includes
2726
source_path_include = os.path.join(ports.get_dir(), 'boost_headers', 'boost')
28-
dest_path_include = ports.get_include_dir('boost')
29-
shared.try_delete(dest_path_include)
30-
shutil.copytree(source_path_include, dest_path_include)
27+
ports.install_header_dir(source_path_include, 'boost')
3128

3229
# write out a dummy cpp file, to create an empty library
3330
# this is needed as emscripted ports expect this, even if it is not used
34-
dummy_file = os.path.join(ports.get_build_dir(), 'boost_headers', 'dummy.cpp')
31+
dummy_file = os.path.join(build_dir, 'dummy.cpp')
3532
shared.safe_ensure_dirs(os.path.dirname(dummy_file))
3633
with open(dummy_file, 'w') as f:
3734
f.write('static void dummy() {}')

tools/ports/bullet.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ def create(final):
2222
logging.info('building port: bullet')
2323

2424
source_path = os.path.join(ports.get_dir(), 'bullet', 'Bullet-' + TAG)
25-
dest_path = os.path.join(ports.get_build_dir(), 'bullet')
26-
27-
shutil.rmtree(dest_path, ignore_errors=True)
25+
dest_path = ports.clear_project_build('bullet')
2826
shutil.copytree(source_path, dest_path)
2927
src_path = os.path.join(dest_path, 'bullet', 'src')
3028
src_path = os.path.join(dest_path, 'bullet', 'src')

tools/ports/bzip2.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@ def get(ports, settings, shared):
1818
ports.fetch_project('bzip2', 'https://github.com/emscripten-ports/bzip2/archive/' + VERSION + '.zip', 'bzip2-' + VERSION, sha512hash=HASH)
1919

2020
def create(final):
21-
ports.clear_project_build('bzip2')
22-
21+
dest_path = ports.clear_project_build('bzip2')
2322
source_path = os.path.join(ports.get_dir(), 'bzip2', 'bzip2-' + VERSION)
24-
dest_path = os.path.join(ports.get_build_dir(), 'bzip2')
25-
shared.try_delete(dest_path)
26-
os.makedirs(dest_path)
27-
shutil.rmtree(dest_path, ignore_errors=True)
2823
shutil.copytree(source_path, dest_path)
2924

3025
# build
@@ -35,7 +30,7 @@ def create(final):
3530
commands = []
3631
o_s = []
3732
for src in srcs:
38-
o = os.path.join(ports.get_build_dir(), 'bzip2', src + '.o')
33+
o = os.path.join(dest_path, shared.replace_suffix(src, '.o'))
3934
shared.safe_ensure_dirs(os.path.dirname(o))
4035
commands.append([shared.EMCC, '-c', os.path.join(dest_path, src), '-O2', '-o', o, '-I' + dest_path, '-w', ])
4136
o_s.append(o)

tools/ports/cocos2d.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def create(final):
3131
cocos2dx_src = make_source_list(cocos2d_root, cocos2dx_root)
3232
cocos2dx_includes = make_includes(cocos2d_root)
3333

34-
cocos2d_build = os.path.join(ports.get_build_dir(), 'cocos2d')
35-
shared.try_delete(os.path.join(cocos2d_build, 'samples'))
34+
cocos2d_build = ports.clear_project_build('cocos2d')
3635
shutil.copytree(os.path.join(cocos2d_root, 'samples', 'Cpp'),
3736
os.path.join(cocos2d_build, 'samples'))
3837

tools/ports/freetype.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@ def get(ports, settings, shared):
1919
ports.fetch_project('freetype', 'https://github.com/emscripten-ports/FreeType/archive/' + TAG + '.zip', 'FreeType-' + TAG, sha512hash=HASH)
2020

2121
def create(final):
22-
ports.clear_project_build('freetype')
23-
22+
dest_path = ports.clear_project_build('freetype')
2423
source_path = os.path.join(ports.get_dir(), 'freetype', 'FreeType-' + TAG)
25-
dest_path = os.path.join(ports.get_build_dir(), 'freetype')
26-
shared.try_delete(dest_path)
27-
os.makedirs(dest_path)
28-
shutil.rmtree(dest_path, ignore_errors=True)
2924
shutil.copytree(source_path, dest_path)
3025
Path(dest_path, 'include/ftconfig.h').write_text(ftconf_h)
3126

@@ -85,7 +80,7 @@ def create(final):
8580
commands = []
8681
o_s = []
8782
for src in srcs:
88-
o = os.path.join(ports.get_build_dir(), 'freetype', src + '.o')
83+
o = os.path.join(dest_path, shared.replace_suffix(src, '.o'))
8984
shared.safe_ensure_dirs(os.path.dirname(o))
9085
commands.append([shared.EMCC, '-c', os.path.join(dest_path, src), '-o', o,
9186
'-DFT2_BUILD_LIBRARY', '-O2',
@@ -103,8 +98,7 @@ def create(final):
10398
o_s.append(o)
10499

105100
ports.run_commands(commands)
106-
shared.try_delete(final)
107-
shared.run_process([shared.LLVM_AR, 'rc', final] + o_s)
101+
ports.create_lib(final, o_s)
108102

109103
ports.install_header_dir(os.path.join(dest_path, 'include'),
110104
target=os.path.join('freetype2', 'freetype'))

tools/ports/giflib.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ def create(final):
2222
logging.info('building port: giflib')
2323

2424
source_path = os.path.join(ports.get_dir(), 'giflib', f'giflib-{VERSION}')
25-
dest_path = os.path.join(ports.get_build_dir(), 'giflib')
26-
27-
shutil.rmtree(dest_path, ignore_errors=True)
25+
dest_path = ports.clear_project_build('giflib')
2826
shutil.copytree(source_path, dest_path)
2927

3028
ports.install_headers(dest_path)

tools/ports/harfbuzz.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ def get(ports, settings, shared):
8686

8787
def create(final):
8888
logging.info('building port: harfbuzz')
89-
ports.clear_project_build('harfbuzz')
89+
build_path = ports.clear_project_build('harfbuzz')
9090

9191
source_path = os.path.join(ports.get_dir(), 'harfbuzz', 'harfbuzz-' + VERSION)
92-
build_path = os.path.join(ports.get_build_dir(), 'harfbuzz')
9392
freetype_include = ports.get_include_dir('freetype2/freetype')
9493
ports.install_headers(os.path.join(source_path, 'src'), target='harfbuzz')
9594

tools/ports/icu.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ def get(ports, settings, shared):
3131

3232
def prepare_build():
3333
source_path = os.path.join(ports.get_dir(), 'icu', 'icu') # downloaded icu4c path
34-
dest_path = os.path.join(ports.get_build_dir(), 'icu') # icu build path
34+
dest_path = ports.clear_project_build('icu') # icu build path
3535
logging.debug(f'preparing for icu build: {source_path} -> {dest_path}')
36-
shutil.rmtree(dest_path, ignore_errors=True)
3736
shutil.copytree(source_path, dest_path)
3837

3938
def build_lib(lib_output, lib_src, other_includes, build_flags):

tools/ports/libjpeg.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ def create(final):
2626
logging.info('building port: libjpeg')
2727

2828
source_path = os.path.join(ports.get_dir(), 'libjpeg', 'jpeg-9c')
29-
dest_path = os.path.join(ports.get_build_dir(), 'libjpeg')
30-
31-
shutil.rmtree(dest_path, ignore_errors=True)
29+
dest_path = ports.clear_project_build('libjpeg')
3230
shutil.copytree(source_path, dest_path)
3331

3432
Path(dest_path, 'jconfig.h').write_text(jconfig_h)

0 commit comments

Comments
 (0)