-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move xcode_support to apple_support.
Adds tests for xcode_support and apple_genrule. RELNOTES: Added xcode_support.bzl, which includes support methods for managing Xcode configurations. PiperOrigin-RevId: 229415609
- Loading branch information
1 parent
80bb990
commit c1e98d4
Showing
5 changed files
with
176 additions
and
2 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,43 @@ | ||
# Copyright 2019 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. | ||
|
||
"""Support functions for working with Xcode configurations.""" | ||
|
||
def _is_xcode_at_least_version(xcode_config, version): | ||
"""Returns True if Xcode version is at least a given version. | ||
This method takes as input an `XcodeVersionConfig` provider, which can be obtained from the | ||
`_xcode_config` attribute (e.g. `ctx.attr._xcode_config[apple_common.XcodeVersionConfig]`). This | ||
provider should contain the Xcode version parameters with which this rule is being built with. | ||
If you need to add this attribute to your rule implementation, please refer to | ||
`apple_support.action_required_attrs`. | ||
Args: | ||
xcode_config: The XcodeVersionConfig provider from the _xcode_config attribute's value. | ||
version: The minimum desired Xcode version, as a dotted version string. | ||
Returns: | ||
True if the given xcode_config version at least as high as the requested version. | ||
""" | ||
current_version = xcode_config.xcode_version() | ||
if not current_version: | ||
fail("Could not determine Xcode version at all. This likely means Xcode isn't available; " + | ||
"if you think this is a mistake, please file an issue.") | ||
|
||
desired_version = apple_common.dotted_version(version) | ||
return current_version >= desired_version | ||
|
||
# Define the loadable module that lists the exported symbols in this file. | ||
xcode_support = struct( | ||
is_xcode_at_least_version = _is_xcode_at_least_version, | ||
) |
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 |
---|---|---|
@@ -1,5 +1,25 @@ | ||
load("@build_bazel_apple_support//rules:apple_genrule.bzl", "apple_genrule") | ||
load(":apple_support_test.bzl", "apple_support_test") | ||
load(":xcode_support_test.bzl", "xcode_support_test") | ||
|
||
licenses(["notice"]) | ||
|
||
apple_support_test(name = "test") | ||
# Custom rules that test rule-context APIs. Check their implementations for more details. | ||
apple_support_test(name = "apple_support_test") | ||
|
||
xcode_support_test(name = "xcode_support_test") | ||
|
||
# Test to ensure the environment variable contract of apple_genrule. | ||
sh_test( | ||
name = "apple_genrule_test", | ||
size = "small", | ||
srcs = ["apple_genrule_test.sh"], | ||
args = ["$(location simple_genrule.txt)"], | ||
data = ["simple_genrule.txt"], | ||
) | ||
|
||
apple_genrule( | ||
name = "simple_genrule", | ||
outs = ["simple_genrule.txt"], | ||
cmd = "printenv | grep \"^\\(DEVELOPER_DIR\\|SDKROOT\\)\" > $(@)", | ||
) |
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,36 @@ | ||
#!/bin/bash | ||
|
||
# Copyright 2019 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. | ||
|
||
set -eu | ||
|
||
# Integration test for apple_genrule. | ||
|
||
# This test checks that the first argument contains a file that contains | ||
# Xcode environment variables. Since the file was generated using an | ||
# apple_genrule using `printenv`, we enforce that the contract of providing the | ||
# DEVELOPER_DIR and SDKROOT environment variables is maintained. | ||
|
||
INPUT_FILE="$1" | ||
|
||
if ! grep -Fq DEVELOPER_DIR "$INPUT_FILE"; then | ||
echo "FAILURE: DEVELOPER_DIR not found." | ||
exit 1 | ||
fi | ||
|
||
if ! grep -Fq SDKROOT "$INPUT_FILE"; then | ||
echo "FAILURE: SDKROOT not found." | ||
exit 1 | ||
fi |
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,59 @@ | ||
# Copyright 2019 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. | ||
"""Definition of a test rule to test xcode_support.""" | ||
|
||
load( | ||
"@build_bazel_apple_support//lib:apple_support.bzl", | ||
"apple_support", | ||
) | ||
load( | ||
"@build_bazel_apple_support//lib:xcode_support.bzl", | ||
"xcode_support", | ||
) | ||
|
||
# Template for the test script used to validate that the action outputs contain the expected | ||
# values. | ||
_TEST_SCRIPT_CONTENTS = """ | ||
#!/bin/bash | ||
set -eu | ||
if [[ "{past_version_is_true}" != "True" ]]; then | ||
echo "FAILURE: 'past_version_is_true' should be True, but it's {past_version_is_true}." | ||
exit 1 | ||
fi | ||
if [[ "{future_version_is_false}" != "False" ]]; then | ||
echo "FAILURE: 'future_version_is_false' should be False, but it's {future_version_is_false}." | ||
exit 1 | ||
fi | ||
""" | ||
|
||
def _xcode_support_test_impl(ctx): | ||
"""Implementation of the xcode_support_test rule.""" | ||
|
||
test_script = ctx.actions.declare_file("{}_test_script".format(ctx.label.name)) | ||
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] | ||
ctx.actions.write(test_script, _TEST_SCRIPT_CONTENTS.format( | ||
past_version_is_true = str(xcode_support.is_xcode_at_least_version(xcode_config, "1.0")), | ||
future_version_is_false = str(xcode_support.is_xcode_at_least_version(xcode_config, "999")), | ||
), is_executable = True) | ||
|
||
return [DefaultInfo(executable = test_script)] | ||
|
||
xcode_support_test = rule( | ||
implementation = _xcode_support_test_impl, | ||
attrs = apple_support.action_required_attrs(), | ||
test = True, | ||
) |