Skip to content

Fix sysroot installation when emscripten itself is read-only #24405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions test/test_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import platform
import shutil
import stat
import time
import re
import tempfile
Expand Down Expand Up @@ -35,6 +36,14 @@ def restore():
shutil.copyfile(EM_CONFIG + '_backup', EM_CONFIG)


def for_all_files(dir, callback):
for root, dirs, files in os.walk(dir):
for d in dirs:
callback(os.path.join(dir, root, d))
for f in files:
callback(os.path.join(dir, root, f))


# restore the config file and set it up for our uses
def restore_and_set_up():
restore()
Expand Down Expand Up @@ -460,6 +469,23 @@ def test_emcc_multiprocess_cache_access(self):
# Exactly one child process should have triggered libc build!
self.assertEqual(num_times_libc_was_built, 1)

# Test that sysroot headers can be installed from a read-only
# emscripten tree.
def test_readonly_sysroot_install(self):
restore_and_set_up()

def make_readonly(filename):
old_mode = stat.S_IMODE(os.stat(filename).st_mode)
os.chmod(filename, old_mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH))

try:
for_all_files(path_from_root('system/include'), make_readonly)

with env_modify({'EM_CACHE': self.in_dir('test_cache')}):
self.run_process([EMCC, test_file('hello_world.c'), '-c'])
finally:
for_all_files(path_from_root('system/include'), shared.make_writable)

@parameterized({
'': [False, False],
'response_files': [True, False],
Expand Down
2 changes: 1 addition & 1 deletion tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def get_file_suffix(filename):


def make_writable(filename):
assert os.path.isfile(filename)
assert os.path.exists(filename)
old_mode = stat.S_IMODE(os.stat(filename).st_mode)
os.chmod(filename, old_mode | stat.S_IWUSR)

Expand Down
23 changes: 18 additions & 5 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2453,8 +2453,21 @@ def calculate(options):
return ret


def copytree_exist_ok(src, dst):
shutil.copytree(src, dst, dirs_exist_ok=True)
def safe_copytree(src, dst):
# We cannot use `shutil.copytree` there because we need to ensure the
# output tree is writable, and in some cases the emscripten tree
# itself is readonly (e.g. NixOS).
# Even if we pass copy_function=safe_copy python's `shutil.copytree`
# will use its internal logic for copying directories and it will
# unconditionally copy the source directory's mode bits.
os.makedirs(dst, exist_ok=True)
for entry in os.scandir(src):
srcname = os.path.join(src, entry.name)
dstname = os.path.join(dst, entry.name)
if entry.is_dir():
safe_copytree(srcname, dstname)
else:
shared.safe_copy(srcname, dstname)


def install_system_headers(stamp):
Expand All @@ -2477,15 +2490,15 @@ def install_system_headers(stamp):
for src, dest in install_dirs.items():
src = utils.path_from_root('system', *src)
dest = os.path.join(target_include_dir, dest)
copytree_exist_ok(src, dest)
safe_copytree(src, dest)

pkgconfig_src = utils.path_from_root('system/lib/pkgconfig')
pkgconfig_dest = cache.get_sysroot_dir('lib/pkgconfig')
copytree_exist_ok(pkgconfig_src, pkgconfig_dest)
safe_copytree(pkgconfig_src, pkgconfig_dest)

bin_src = utils.path_from_root('system/bin')
bin_dest = cache.get_sysroot_dir('bin')
copytree_exist_ok(bin_src, bin_dest)
safe_copytree(bin_src, bin_dest)

# Create a version header based on the emscripten-version.txt
version_file = cache.get_include_dir('emscripten/version.h')
Expand Down