Skip to content

Commit 42986e0

Browse files
committed
Always use -fmacro-prefix-map
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 But this does not make relative paths and absolute paths the same, which can be a problem when data generated by `__FILE__` macro is included in one of code size tests. This problem is discussed in emscripten-core#23195. This PR makes `__FILE__` macro produce the same data in all cases by using the fake path `/emsdk/emscripten` as its base, so that it wouldn't change any results for code size tests. This is done by `-fmacro-prefix-map`. This differs from the current behavior because we don't handle relative and absolute paths differently. For the debug info, when `deterministic_paths` is set, this uses a fake path `/emsdk/emscripten` as a base emscripten path. When `deterministic_paths` is not set, this uses real local absolute paths in the debug info. This allows local developers to see their real paths in the debug info while continuing to use the same (fake) path `/emsdk/emscripten` we have used so far for the release binaries. Users can set their debug base path to whatever path they like, but given that we have used `/emsdk/emscripten` in release binaries for a while, it is possible that some users have set their configuration with this directory, so it would be better not to break them by changing it. This is done by `-ffile-prefix-map` as we have done so far, which is an alias for both `-fdebug-prefix-map` and `-fmacro-prefix-map`. This is basically implementing what's suggested in emscripten-core#23195 (comment) and emscripten-core#23195 (comment) This also turns `deterministic_paths` on for the Ninja path in embuilder for consistency with the non-Ninja path. Fixes emscripten-core#23915.
1 parent f66b5d7 commit 42986e0

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

embuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def main():
291291
library.erase()
292292
if do_build:
293293
if USE_NINJA:
294-
library.generate()
294+
library.generate(deterministic_paths=True)
295295
else:
296296
library.build(deterministic_paths=True)
297297
elif what == 'sysroot':

tools/system_libs.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
# link time.
4343
USE_NINJA = int(os.environ.get('EMCC_USE_NINJA', '0'))
4444

45+
FAKE_EMSCRIPTEN_PATH = '/emsdk/emscripten'
46+
4547

4648
def files_in_path(path, filenames):
4749
srcdir = utils.path_from_root(path)
@@ -427,8 +429,8 @@ def build(self, deterministic_paths=False):
427429
self.deterministic_paths = deterministic_paths
428430
return cache.get(self.get_path(), self.do_build, force=USE_NINJA == 2, quiet=USE_NINJA)
429431

430-
def generate(self):
431-
self.deterministic_paths = False
432+
def generate(self, deterministic_paths=False):
433+
self.deterministic_paths = deterministic_paths
432434
return cache.get(self.get_path(), self.do_generate, force=USE_NINJA == 2, quiet=USE_NINJA,
433435
deferred=True)
434436

@@ -469,12 +471,15 @@ def generate_ninja(self, build_dir, libname):
469471
utils.safe_ensure_dirs(build_dir)
470472

471473
cflags = self.get_cflags()
474+
source_dir = utils.path_from_root()
475+
relative_source_dir = os.path.relpath(source_dir, build_dir)
472476
if self.deterministic_paths:
473-
source_dir = utils.path_from_root()
474-
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']
477+
cflags += [f'-ffile-prefix-map={source_dir}={FAKE_EMSCRIPTEN_PATH}',
478+
f'-ffile-prefix-map={relative_source_dir}={FAKE_EMSCRIPTEN_PATH}',
479+
'-fdebug-compilation-dir={FAKE_EMSCRIPTEN_PATH}']
480+
else:
481+
cflags += [f'-fmacro-prefix-map={source_dir}={FAKE_EMSCRIPTEN_PATH}',
482+
f'-fmacro-prefix-map={relative_source_dir}={FAKE_EMSCRIPTEN_PATH}']
478483
asflags = get_base_cflags(preprocess=False)
479484
input_files = self.get_files()
480485
ninja_file = os.path.join(build_dir, 'build.ninja')
@@ -492,13 +497,15 @@ def build_objects(self, build_dir):
492497
commands = []
493498
objects = set()
494499
cflags = self.get_cflags()
500+
source_dir = utils.path_from_root()
501+
relative_source_dir = os.path.relpath(source_dir, build_dir)
495502
if self.deterministic_paths:
496-
source_dir = utils.path_from_root()
497-
if batch_inputs:
498-
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}={FAKE_EMSCRIPTEN_PATH}']
504+
cflags += [f'-ffile-prefix-map={source_dir}={FAKE_EMSCRIPTEN_PATH}',
505+
'-fdebug-compilation-dir={FAKE_EMSCRIPTEN_PATH}']
506+
else:
507+
cflags += [f'-fmacro-prefix-map={relative_source_dir}={FAKE_EMSCRIPTEN_PATH}']
508+
cflags += [f'-fmacro-prefix-map={source_dir}={FAKE_EMSCRIPTEN_PATH}']
502509
case_insensitive = is_case_insensitive(build_dir)
503510
for src in self.get_files():
504511
ext = shared.suffix(src)

0 commit comments

Comments
 (0)