This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
forked from bazelbuild/rules_swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out Bazel placeholder substitution functionality into a separa…
…te reusable class. PiperOrigin-RevId: 366461152
- Loading branch information
1 parent
aeb56dc
commit 89c6ab2
Showing
9 changed files
with
195 additions
and
105 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,90 @@ | ||
// 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/bazel_substitutions.h" | ||
|
||
#include <cstdlib> | ||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
|
||
namespace bazel_rules_swift { | ||
namespace { | ||
|
||
// Returns the value of the given environment variable, or the empty string if | ||
// it wasn't set. | ||
std::string GetEnvironmentVariable(const char *name) { | ||
char *env_value = getenv(name); | ||
if (env_value == nullptr) { | ||
return ""; | ||
} | ||
return env_value; | ||
} | ||
|
||
} // namespace | ||
|
||
BazelPlaceholderSubstitutions::BazelPlaceholderSubstitutions() { | ||
// When targeting Apple platforms, replace the magic Bazel placeholders with | ||
// the path in the corresponding environment variable. These should be set by | ||
// the build rules; only attempt to retrieve them if they're actually seen in | ||
// the argument list. | ||
placeholder_resolvers_ = { | ||
{kBazelXcodeDeveloperDir, PlaceholderResolver([]() { | ||
return GetEnvironmentVariable("DEVELOPER_DIR"); | ||
})}, | ||
{kBazelXcodeSdkRoot, | ||
PlaceholderResolver([]() { return GetEnvironmentVariable("SDKROOT"); })}, | ||
}; | ||
} | ||
|
||
BazelPlaceholderSubstitutions::BazelPlaceholderSubstitutions( | ||
const std::string &developer_dir, const std::string &sdk_root) { | ||
placeholder_resolvers_ = { | ||
{kBazelXcodeDeveloperDir, | ||
PlaceholderResolver([=]() { return developer_dir; })}, | ||
{kBazelXcodeSdkRoot, | ||
PlaceholderResolver([=]() { return sdk_root; })}, | ||
}; | ||
} | ||
|
||
bool BazelPlaceholderSubstitutions::Apply(std::string &arg) { | ||
bool changed = false; | ||
|
||
// Replace placeholders in the string with their actual values. | ||
for (auto &[placeholder, env_var] : placeholder_resolvers_) { | ||
changed |= FindAndReplace(placeholder, env_var, arg); | ||
} | ||
|
||
return changed; | ||
} | ||
|
||
bool BazelPlaceholderSubstitutions::FindAndReplace( | ||
const std::string &placeholder, | ||
BazelPlaceholderSubstitutions::PlaceholderResolver &resolver, | ||
std::string &str) { | ||
int start = 0; | ||
bool changed = false; | ||
while ((start = str.find(placeholder, start)) != std::string::npos) { | ||
std::string resolved_value = resolver.get(); | ||
if (resolved_value.empty()) { | ||
return false; | ||
} | ||
changed = true; | ||
str.replace(start, placeholder.length(), resolved_value); | ||
start += resolved_value.length(); | ||
} | ||
return 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,93 @@ | ||
// 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 BUILD_BAZEL_RULES_SWIFT_TOOLS_COMMON_BAZEL_SUBSTITUTIONS_H_ | ||
#define BUILD_BAZEL_RULES_SWIFT_TOOLS_COMMON_BAZEL_SUBSTITUTIONS_H_ | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
namespace bazel_rules_swift { | ||
|
||
// Manages the substitution of special Bazel placeholder strings in command line | ||
// arguments that are used to defer the determination of Apple developer and SDK | ||
// paths until execution time. | ||
class BazelPlaceholderSubstitutions { | ||
public: | ||
// Initializes the substitutions by looking them up in the process's | ||
// environment when they are first requested. | ||
BazelPlaceholderSubstitutions(); | ||
|
||
// Initializes the substitutions with the given fixed strings. Intended to be | ||
// used for testing. | ||
BazelPlaceholderSubstitutions(const std::string &developer_dir, | ||
const std::string &sdk_root); | ||
|
||
// Applies any necessary substitutions to `arg` and returns true if this | ||
// caused the string to change. | ||
bool Apply(std::string &arg); | ||
|
||
// The placeholder string used by Bazel that should be replaced by | ||
// `DEVELOPER_DIR` at runtime. | ||
static constexpr const char kBazelXcodeDeveloperDir[] = | ||
"__BAZEL_XCODE_DEVELOPER_DIR__"; | ||
|
||
// The placeholder string used by Bazel that should be replaced by `SDKROOT` | ||
// at runtime. | ||
static constexpr const char kBazelXcodeSdkRoot[] = "__BAZEL_XCODE_SDKROOT__"; | ||
|
||
private: | ||
// A resolver for a Bazel placeholder string that retrieves and caches the | ||
// value the first time it is requested. | ||
class PlaceholderResolver { | ||
public: | ||
explicit PlaceholderResolver(std::function<std::string()> fn) | ||
: function_(fn), initialized_(false) {} | ||
|
||
// Returns the requested placeholder value, caching it for future | ||
// retrievals. | ||
std::string get() { | ||
if (!initialized_) { | ||
value_ = function_(); | ||
initialized_ = true; | ||
} | ||
return value_; | ||
} | ||
|
||
private: | ||
// The function that returns the value of the placeholder, or the empty | ||
// string if the placeholder should not be replaced. | ||
std::function<std::string()> function_; | ||
|
||
// Indicates whether the value of the placeholder has been requested yet and | ||
// and is therefore initialized. | ||
bool initialized_; | ||
|
||
// The cached value of the placeholder if `initialized_` is true. | ||
std::string value_; | ||
}; | ||
|
||
// Finds and replaces all instances of `placeholder` with the value provided | ||
// by `resolver`, in-place on `str`. Returns true if the string was changed. | ||
bool FindAndReplace(const std::string &placeholder, | ||
PlaceholderResolver &resolver, std::string &str); | ||
|
||
// A mapping from Bazel placeholder strings to resolvers that provide their | ||
// values. | ||
std::map<std::string, PlaceholderResolver> placeholder_resolvers_; | ||
}; | ||
|
||
} // namespace bazel_rules_swift | ||
|
||
#endif // BUILD_BAZEL_RULES_SWIFT_TOOLS_COMMON_BAZEL_SUBSTITUTIONS_H_ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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