-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle swift toolchain and platform placeholds in swift worker.
PiperOrigin-RevId: 462733373
- Loading branch information
1 parent
47b4648
commit fc4c4c7
Showing
6 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2021 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "tools/common/swift_substitutions.h" | ||
|
||
#include "absl/strings/str_replace.h" | ||
#include "absl/strings/substitute.h" | ||
#include "absl/types/optional.h" | ||
|
||
namespace bazel_rules_swift { | ||
namespace { | ||
// Returns the value of the given environment variable, or nullopt if it wasn't | ||
// set. | ||
absl::optional<std::string> GetEnvironmentVariable(absl::string_view name) { | ||
std::string null_terminated_name(name.data(), name.length()); | ||
char* env_value = getenv(null_terminated_name.c_str()); | ||
if (env_value == nullptr) { | ||
return absl::nullopt; | ||
} | ||
return env_value; | ||
} | ||
|
||
} // namespace | ||
|
||
SwiftPlaceholderSubstitutions::SwiftPlaceholderSubstitutions() { | ||
if (absl::optional<std::string> swift_toolchain_override = | ||
GetEnvironmentVariable("SWIFT_TOOLCHAIN_OVERRIDE"); | ||
swift_toolchain_override.has_value()) { | ||
substitutions_[kSwiftToolchainDir] = *swift_toolchain_override; | ||
} else { | ||
substitutions_[kSwiftToolchainDir] = | ||
"__BAZEL_XCODE_DEVELOPER_DIR__/Toolchains/XcodeDefault.xctoolchain"; | ||
} | ||
|
||
if (absl::optional<std::string> swift_platform_override = | ||
GetEnvironmentVariable("SWIFT_PLATFORM_OVERRIDE"); | ||
swift_platform_override.has_value()) { | ||
substitutions_[kSwiftPlatformDir] = *swift_platform_override; | ||
} else if (absl::optional<std::string> apple_sdk_platform = | ||
GetEnvironmentVariable("APPLE_SDK_PLATFORM"); | ||
apple_sdk_platform.has_value()) { | ||
substitutions_[kSwiftPlatformDir] = | ||
absl::Substitute("__BAZEL_XCODE_DEVELOPER_DIR__/Platforms/$0.platform", | ||
*apple_sdk_platform); | ||
} | ||
} | ||
|
||
bool SwiftPlaceholderSubstitutions::Apply(std::string& arg) { | ||
// Order here matters. Swift substitutions must be applied first. | ||
// They can produce bazel substitutions that require further substitution. | ||
bool swift_changed = absl::StrReplaceAll(substitutions_, &arg) > 0; | ||
return bazel_substitutions_.Apply(arg) > 0 || swift_changed; | ||
} | ||
} // namespace bazel_rules_swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2021 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef THIRD_PARTY_BAZEL_RULES_RULES_SWIFT_TOOLS_COMMON_SWIFT_SUBSTITUTIONS_H_ | ||
#define THIRD_PARTY_BAZEL_RULES_RULES_SWIFT_TOOLS_COMMON_SWIFT_SUBSTITUTIONS_H_ | ||
|
||
#include "absl/container/flat_hash_map.h" | ||
#include "absl/strings/string_view.h" | ||
#include "tools/common/bazel_substitutions.h" | ||
|
||
namespace bazel_rules_swift { | ||
|
||
// Manages the substitution of special Swift placeholder strings in command line | ||
// arguments that are used to defer the determination of toolchain and platform | ||
// paths until execution time. | ||
class SwiftPlaceholderSubstitutions { | ||
public: | ||
SwiftPlaceholderSubstitutions(); | ||
|
||
// Applies any necessary substitutions to `arg` and returns true if this | ||
// caused the string to change. | ||
bool Apply(std::string &arg); | ||
|
||
inline static constexpr absl::string_view kSwiftToolchainDir = | ||
"__SWIFT_TOOLCHAIN_DIR__"; | ||
inline static constexpr absl::string_view kSwiftPlatformDir = | ||
"__SWIFT_PLATFORM_DIR__"; | ||
|
||
private: | ||
// A mapping from bazel substitutes to their values. | ||
BazelPlaceholderSubstitutions bazel_substitutions_; | ||
|
||
// A mapping from swift toolchain to their substituted values. | ||
absl::flat_hash_map<std::string, std::string> substitutions_; | ||
}; | ||
|
||
} // namespace bazel_rules_swift | ||
|
||
#endif // THIRD_PARTY_BAZEL_RULES_RULES_SWIFT_TOOLS_COMMON_SWIFT_SUBSTITUTIONS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters