From 12466716c8731ee09ecaa2de79dcc5685f92945a Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Wed, 18 Dec 2024 23:35:59 +0000 Subject: [PATCH] Use /emsdk/emscripten for relative path substitution 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 https://github.com/emscripten-core/emscripten/issues/23195#issuecomment-2549784273 while discussins `__FILE__` problem in #23915. --- .../codesize/test_codesize_cxx_except.size | 2 +- .../test_codesize_cxx_except_wasm.size | 2 +- .../test_codesize_cxx_except_wasm_exnref.size | 2 +- .../codesize/test_codesize_cxx_mangle.size | 2 +- tools/system_libs.py | 19 ++++++++++++------- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/test/other/codesize/test_codesize_cxx_except.size b/test/other/codesize/test_codesize_cxx_except.size index 7a6716bb2a43a..16bb179be3c7a 100644 --- a/test/other/codesize/test_codesize_cxx_except.size +++ b/test/other/codesize/test_codesize_cxx_except.size @@ -1 +1 @@ -171008 +171030 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm.size b/test/other/codesize/test_codesize_cxx_except_wasm.size index ccdf567408898..f63fa8e1ee181 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm.size +++ b/test/other/codesize/test_codesize_cxx_except_wasm.size @@ -1 +1 @@ -142223 +142234 diff --git a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.size b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.size index 9fd3641491ec2..bc2fc197a02a7 100644 --- a/test/other/codesize/test_codesize_cxx_except_wasm_exnref.size +++ b/test/other/codesize/test_codesize_cxx_except_wasm_exnref.size @@ -1 +1 @@ -144810 +144821 diff --git a/test/other/codesize/test_codesize_cxx_mangle.size b/test/other/codesize/test_codesize_cxx_mangle.size index e1078b8c5934e..d6c1bcdbf748d 100644 --- a/test/other/codesize/test_codesize_cxx_mangle.size +++ b/test/other/codesize/test_codesize_cxx_mangle.size @@ -1 +1 @@ -232517 +232548 diff --git a/tools/system_libs.py b/tools/system_libs.py index e6bbf1c3435b1..bbedd8ad00db4 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -42,6 +42,10 @@ # link time. USE_NINJA = int(os.environ.get('EMCC_USE_NINJA', '0')) +# A fake emscripten path to use in __FILE__ macro and debug info to produce +# reproducible builds across platforms. +FAKE_EMSCRIPTEN_PATH = '/emsdk/emscripten' + def files_in_path(path, filenames): srcdir = utils.path_from_root(path) @@ -472,9 +476,9 @@ def generate_ninja(self, build_dir, libname): if self.deterministic_paths: source_dir = utils.path_from_root() relative_source_dir = os.path.relpath(source_dir, build_dir) - cflags += [f'-ffile-prefix-map={source_dir}=/emsdk/emscripten', - f'-ffile-prefix-map={relative_source_dir}/=', - '-fdebug-compilation-dir=/emsdk/emscripten'] + cflags += [f'-ffile-prefix-map={source_dir}={FAKE_EMSCRIPTEN_PATH}', + f'-ffile-prefix-map={relative_source_dir}={FAKE_EMSCRIPTEN_PATH}', + '-fdebug-compilation-dir={FAKE_EMSCRIPTEN_PATH}'] asflags = get_base_cflags(preprocess=False) input_files = self.get_files() ninja_file = os.path.join(build_dir, 'build.ninja') @@ -492,13 +496,14 @@ def build_objects(self, build_dir): commands = [] objects = set() cflags = self.get_cflags() - if self.deterministic_paths: + if True: + #if self.deterministic_paths: source_dir = utils.path_from_root() if batch_inputs: relative_source_dir = os.path.relpath(source_dir, build_dir) - cflags += [f'-ffile-prefix-map={relative_source_dir}/='] - cflags += [f'-ffile-prefix-map={source_dir}=/emsdk/emscripten', - '-fdebug-compilation-dir=/emsdk/emscripten'] + cflags += [f'-ffile-prefix-map={relative_source_dir}={FAKE_EMSCRIPTEN_PATH}'] + cflags += [f'-ffile-prefix-map={source_dir}={FAKE_EMSCRIPTEN_PATH}', + '-fdebug-compilation-dir={FAKE_EMSCRIPTEN_PATH}'] case_insensitive = is_case_insensitive(build_dir) for src in self.get_files(): ext = shared.suffix(src)