Skip to content

Commit 971d4f6

Browse files
authored
Use consistent prefix map when building deterministic system libraries (#23222)
When `deterministic_paths` is set, we are currently using `-ffile-prefix-map` to produce the same path in data and debug info. In the case of absolute paths, their emscripten path is replaced with a fake path `/emsdk/emscripten`, and in the case of relative paths, all path relative to the emscripten directory is removed, so `../../system/lib/somefile.c` becomes `system/lib/somefiles.c`. https://github.com/emscripten-core/emscripten/blob/f66b5d706e174d9e5cc6122c06ea29dcd2735cd0/tools/system_libs.py#L472-L477 https://github.com/emscripten-core/emscripten/blob/f66b5d706e174d9e5cc6122c06ea29dcd2735cd0/tools/system_libs.py#L495-L501 So this does not make relative paths and absolute paths the same, which can lead to different builds depending on whether the command line uses absolute paths vs. relative ones. Currently we use relative paths when `EMCC_BATCH_BUILD` is set. And Ninja builds cannot use relative paths. This is also what was suggested in #23195 (comment) while discussins `__FILE__` problem in #23195. This does not change any code size tests because no code size tests happens to contain `__FILE__` at the moment. (One will be added in #22994)
1 parent 0186a3f commit 971d4f6

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

test/test_other.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10409,7 +10409,7 @@ def test_dwarf_system_lib(self):
1040910409
[LLVM_DWARFDUMP, libc, '-debug-info', '-debug-line', '--recurse-depth=0'],
1041010410
stdout=PIPE).stdout
1041110411
# Check that the embedded location of the source file is correct.
10412-
self.assertIn('DW_AT_name\t("system/lib/emmalloc.c")', dwdump)
10412+
self.assertIn('DW_AT_name\t("/emsdk/emscripten/system/lib/emmalloc.c")', dwdump)
1041310413
self.assertIn('DW_AT_comp_dir\t("/emsdk/emscripten")', dwdump)
1041410414

1041510415
@parameterized({

tools/system_libs.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
# link time.
4343
USE_NINJA = int(os.environ.get('EMCC_USE_NINJA', '0'))
4444

45+
# A (fake) deterministic emscripten path to use in __FILE__ macro and debug info
46+
# to produce reproducible builds across platforms.
47+
DETERMINISITIC_PREFIX = '/emsdk/emscripten'
48+
4549

4650
def files_in_path(path, filenames):
4751
srcdir = utils.path_from_root(path)
@@ -472,9 +476,9 @@ def generate_ninja(self, build_dir, libname):
472476
if self.deterministic_paths:
473477
source_dir = utils.path_from_root()
474478
relative_source_dir = os.path.relpath(source_dir, build_dir)
475-
cflags += [f'-ffile-prefix-map={source_dir}=/emsdk/emscripten',
476-
f'-ffile-prefix-map={relative_source_dir}/=',
477-
'-fdebug-compilation-dir=/emsdk/emscripten']
479+
cflags += [f'-ffile-prefix-map={source_dir}={DETERMINISITIC_PREFIX}',
480+
f'-ffile-prefix-map={relative_source_dir}={DETERMINISITIC_PREFIX}',
481+
f'-fdebug-compilation-dir={DETERMINISITIC_PREFIX}']
478482
asflags = get_base_cflags(preprocess=False)
479483
input_files = self.get_files()
480484
ninja_file = os.path.join(build_dir, 'build.ninja')
@@ -496,9 +500,9 @@ def build_objects(self, build_dir):
496500
source_dir = utils.path_from_root()
497501
if batch_inputs:
498502
relative_source_dir = os.path.relpath(source_dir, build_dir)
499-
cflags += [f'-ffile-prefix-map={relative_source_dir}/=']
500-
cflags += [f'-ffile-prefix-map={source_dir}=/emsdk/emscripten',
501-
'-fdebug-compilation-dir=/emsdk/emscripten']
503+
cflags += [f'-ffile-prefix-map={relative_source_dir}={DETERMINISITIC_PREFIX}']
504+
cflags += [f'-ffile-prefix-map={source_dir}={DETERMINISITIC_PREFIX}',
505+
f'-fdebug-compilation-dir={DETERMINISITIC_PREFIX}']
502506
case_insensitive = is_case_insensitive(build_dir)
503507
for src in self.get_files():
504508
ext = shared.suffix(src)

0 commit comments

Comments
 (0)