Skip to content

Commit

Permalink
fail builds with duplicate module names
Browse files Browse the repository at this point in the history
Summary: When compiling Swift with explicit modules, its possible for the module map to contain multiple entries with the same name. This should be added as a compiler warning, but until that is upstreamed add a validation check to the Swift wrapper to fix any existing cases.

Reviewed By: milend

Differential Revision: D67203279

fbshipit-source-id: 97563ddbf68a7341bdfd45d3fac7c4f3fde73df0
  • Loading branch information
rmaz authored and facebook-github-bot committed Dec 17, 2024
1 parent ec3363e commit cdd4882
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions prelude/apple/swift/swift_module_map.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ def write_swift_module_map_with_deps(
return ctx.actions.write_json(
module_name + ".swift_module_map.json",
all_deps.project_as_json("swift_module_map"),
pretty = True,
with_inputs = True,
)
49 changes: 49 additions & 0 deletions prelude/apple/tools/swift_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

import json
import os
import subprocess
import sys
Expand All @@ -14,6 +15,42 @@
_FILE_WRITE_FAILURE_MARKER = "could not write"


def expand_argsfile(command):
for arg in command:
if arg.startswith("@"):
with open(arg[1:]) as f:
return f.read().splitlines()

return []


def validate_swift_module_map(path):
seen_clang_modules = set()
seen_swift_modules = set()
with open(path, "r") as f:
j = json.load(f)

for entry in j:
is_clang_module = "clangModulePath" in entry
module_name = entry["moduleName"]
if is_clang_module:
if module_name in seen_clang_modules:
print(
f"Duplicate clang module {module_name} found in {path}",
file=sys.stderr,
)
sys.exit(1)
seen_clang_modules.add(module_name)
else:
if module_name in seen_swift_modules:
print(
f"Duplicate Swift module {module_name} found in {path}",
file=sys.stderr,
)
sys.exit(1)
seen_swift_modules.add(module_name)


def main():
env = os.environ.copy()
if "INSIDE_RE_WORKER" in env and _RE_TMPDIR_ENV_VAR in env:
Expand All @@ -35,6 +72,18 @@ def main():

command = sys.argv[1:]

# T209485965: swift module maps allow for duplicate entries
# and the first one will be picked. Until we have a compiler
# warning for this case we need to validate here.
expanded_args = expand_argsfile(command)
for i in range(len(expanded_args)):
if expanded_args[i] == "-explicit-swift-module-map-file":
if expanded_args[i + 1] == "-Xfrontend":
validate_swift_module_map(expanded_args[i + 2])
else:
validate_swift_module_map(expanded_args[i + 1])
break

# Use relative paths for debug information and index information,
# so we generate relocatable files.
#
Expand Down

0 comments on commit cdd4882

Please sign in to comment.