Skip to content

Commit 953c281

Browse files
authored
Add macros support for Linux (#1108)
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.
1 parent 03e9abf commit 953c281

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Diff for: swift/internal/swift_autoconfiguration.bzl

+29
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ load(
4040
"SWIFT_FEATURE_USE_MODULE_WRAP",
4141
"SWIFT_FEATURE_USE_OLD_DRIVER",
4242
"SWIFT_FEATURE_USE_RESPONSE_FILES",
43+
"SWIFT_FEATURE__SUPPORTS_MACROS",
4344
)
4445

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

174+
def _fetch_supported_features(repository_ctx, swiftc_path):
175+
"""Fetch the json config of supported features from Swift
176+
177+
This can be used to flip rules specific features
178+
179+
Args:
180+
repository_ctx: The repository context.
181+
swiftc_path: The `path` to the `swiftc` executable.
182+
183+
Returns:
184+
The list of supported features, or an empty array if it fails
185+
"""
186+
repository_ctx.file("empty.swift")
187+
result = repository_ctx.execute([
188+
swiftc_path,
189+
"-frontend",
190+
"-emit-supported-features",
191+
"empty.swift",
192+
])
193+
if result.return_code == 0:
194+
return json.decode(result.stdout.strip()).get("SupportedArguments", [])
195+
196+
return []
197+
173198
def _compute_feature_values(repository_ctx, swiftc_path):
174199
"""Computes a list of supported/unsupported features by running checks.
175200
@@ -252,6 +277,10 @@ def _create_linux_toolchain(repository_ctx):
252277
feature_values.append(SWIFT_FEATURE_USE_AUTOLINK_EXTRACT)
253278
feature_values.append(SWIFT_FEATURE_USE_MODULE_WRAP)
254279

280+
swift_features_config = _fetch_supported_features(repository_ctx, path_to_swiftc)
281+
if "load-plugin-executable" in swift_features_config:
282+
feature_values.append(SWIFT_FEATURE__SUPPORTS_MACROS)
283+
255284
repository_ctx.file(
256285
"BUILD",
257286
"""

Diff for: tools/worker/swift_runner.cc

+11
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,18 @@ bool SwiftRunner::ProcessArgument(
301301
} else if (StripPrefix("-macro-expansion-dir=", new_arg)) {
302302
changed = true;
303303
std::filesystem::create_directories(new_arg);
304+
#if __APPLE__
304305
job_env_["TMPDIR"] = new_arg;
306+
#else
307+
// TEMPDIR is read by C++ but not Swift. Swift requires the temprorary
308+
// directory to be an absolute path and otherwise fails (or ignores it
309+
// silently on macOS) so we need to set one that Swift does not read.
310+
// C++ prioritizes TMPDIR over TEMPDIR so we need to wipe out the other
311+
// one. The downside is that anything else reading TMPDIR will not use
312+
// the one potentially set by the user.
313+
job_env_["TEMPDIR"] = new_arg;
314+
job_env_.erase("TMPDIR");
315+
#endif
305316
} else if (new_arg == "-ephemeral-module-cache") {
306317
// Create a temporary directory to hold the module cache, which will be
307318
// deleted after compilation is finished.

0 commit comments

Comments
 (0)