Skip to content

Commit

Permalink
Remove unused source module.modulemap files
Browse files Browse the repository at this point in the history
These can cause module redefinition errors when sandboxing is disabled. Since we never want to use them, we remove them from the repository. We keep around modulemaps that we pass to `objc_library` and any inside frameworks.
  • Loading branch information
brentleyjones committed May 24, 2024
1 parent 488aea4 commit 4318ed4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 8 additions & 2 deletions swiftpkg/internal/repository_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,23 @@ def _exclude_paths(path_list, exclude_paths):

return results

def _find_and_delete_files(repository_ctx, path, name):
def _find_and_delete_files(repository_ctx, path, name, exclude_paths = []):
"""Finds files with the specified name under the specified path and deletes them.
Args:
repository_ctx: A `repository_ctx` instance.
path: A path `string` value.
name: A file basename as a `string`.
exclude_paths: Optional. A `list` of path `string` values to exclude
from the search.
"""
find_args = ["find", path, "-type", "f", "-name", name]
exclude_args = lists.flatten([
["-not", "-path", path + "/" + exclude_path]
for exclude_path in exclude_paths
])
rm_args = ["-delete"]
all_args = find_args + rm_args
all_args = find_args + exclude_args + rm_args
exec_result = repository_ctx.execute(all_args, quiet = True)
if exec_result.return_code != 0:
fail("Failed to remove files named {name} under {path}. stderr:\n{stderr}".format(
Expand Down
20 changes: 20 additions & 0 deletions swiftpkg/internal/swift_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ def _remove_bazel_files(repository_ctx, directory):
for file in files:
repository_files.find_and_delete_files(repository_ctx, directory, file)

def _remove_modulemaps(repository_ctx, directory, targets):
repository_files.find_and_delete_files(
repository_ctx,
directory,
"module.modulemap",
exclude_paths = [
# Framework modulemaps don't cause issues, and are needed
"**/*.framework/*",
] + [
# We need to leave any modulemaps that we are passing into
# `objc_library`
target.clang_src_info.modulemap_path
for target in targets
if target.clang_src_info and target.clang_src_info.modulemap_path
],
)

def _swift_package_impl(repository_ctx):
directory = str(repository_ctx.path("."))
env = repo_rules.get_exec_env(repository_ctx)
Expand All @@ -68,6 +85,9 @@ def _swift_package_impl(repository_ctx):
# Remove the git stuff
repository_ctx.delete(repository_ctx.path(".git"))

# Remove unused modulemaps to prevent module redefinition errors
_remove_modulemaps(repository_ctx, directory, pkg_ctx.pkg_info.targets)

# Return attributes that make this reproducible
return _update_git_attrs(repository_ctx.attr, _ALL_ATTRS.keys(), update)

Expand Down

0 comments on commit 4318ed4

Please sign in to comment.