Skip to content

Commit

Permalink
Add macros support for Linux (#1108)
Browse files Browse the repository at this point in the history
This uses Swift's builtin feature detection to check what flags are
available. This probably means previous toolchains where macros weren't
complete would return true for this, but that's probably fine.

This also requires absolutizing TMPDIR since that is validated in a case
on Linux and errors otherwise. Hoping this doesn't cause issues.
  • Loading branch information
keith committed Sep 15, 2023
1 parent 03e9abf commit 953c281
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
29 changes: 29 additions & 0 deletions swift/internal/swift_autoconfiguration.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ load(
"SWIFT_FEATURE_USE_MODULE_WRAP",
"SWIFT_FEATURE_USE_OLD_DRIVER",
"SWIFT_FEATURE_USE_RESPONSE_FILES",
"SWIFT_FEATURE__SUPPORTS_MACROS",
)

def _scratch_file(repository_ctx, temp_dir, name, content = ""):
Expand Down Expand Up @@ -170,6 +171,30 @@ def _write_swift_version(repository_ctx, swiftc_path):
repository_ctx.file(filename, contents, executable = False)
return filename

def _fetch_supported_features(repository_ctx, swiftc_path):
"""Fetch the json config of supported features from Swift
This can be used to flip rules specific features
Args:
repository_ctx: The repository context.
swiftc_path: The `path` to the `swiftc` executable.
Returns:
The list of supported features, or an empty array if it fails
"""
repository_ctx.file("empty.swift")
result = repository_ctx.execute([
swiftc_path,
"-frontend",
"-emit-supported-features",
"empty.swift",
])
if result.return_code == 0:
return json.decode(result.stdout.strip()).get("SupportedArguments", [])

return []

def _compute_feature_values(repository_ctx, swiftc_path):
"""Computes a list of supported/unsupported features by running checks.
Expand Down Expand Up @@ -252,6 +277,10 @@ def _create_linux_toolchain(repository_ctx):
feature_values.append(SWIFT_FEATURE_USE_AUTOLINK_EXTRACT)
feature_values.append(SWIFT_FEATURE_USE_MODULE_WRAP)

swift_features_config = _fetch_supported_features(repository_ctx, path_to_swiftc)
if "load-plugin-executable" in swift_features_config:
feature_values.append(SWIFT_FEATURE__SUPPORTS_MACROS)

repository_ctx.file(
"BUILD",
"""
Expand Down
11 changes: 11 additions & 0 deletions tools/worker/swift_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,18 @@ bool SwiftRunner::ProcessArgument(
} else if (StripPrefix("-macro-expansion-dir=", new_arg)) {
changed = true;
std::filesystem::create_directories(new_arg);
#if __APPLE__
job_env_["TMPDIR"] = new_arg;
#else
// TEMPDIR is read by C++ but not Swift. Swift requires the temprorary
// directory to be an absolute path and otherwise fails (or ignores it
// silently on macOS) so we need to set one that Swift does not read.
// C++ prioritizes TMPDIR over TEMPDIR so we need to wipe out the other
// one. The downside is that anything else reading TMPDIR will not use
// the one potentially set by the user.
job_env_["TEMPDIR"] = new_arg;
job_env_.erase("TMPDIR");
#endif
} else if (new_arg == "-ephemeral-module-cache") {
// Create a temporary directory to hold the module cache, which will be
// deleted after compilation is finished.
Expand Down

0 comments on commit 953c281

Please sign in to comment.