|
| 1 | +// Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.devtools.build.lib.rules.apple; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableMap; |
| 20 | +import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
| 21 | +import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; |
| 22 | +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
| 23 | +import com.google.devtools.build.lib.cmdline.Label; |
| 24 | +import com.google.devtools.build.lib.packages.Provider; |
| 25 | +import com.google.devtools.build.lib.packages.StarlarkProvider; |
| 26 | +import com.google.devtools.build.lib.packages.StructImpl; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.junit.runners.JUnit4; |
| 31 | + |
| 32 | +/** Tests for the action properties on rule configured targets of Apple related rules. */ |
| 33 | +@RunWith(JUnit4.class) |
| 34 | +public class AppleFragmentTest extends BuildViewTestCase { |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setup() throws Exception { |
| 38 | + scratch.file( |
| 39 | + "rules.bzl", |
| 40 | + "MyInfo = provider()", |
| 41 | + "def _my_binary_impl(ctx):", |
| 42 | + " out = ctx.actions.declare_file(ctx.label.name)", |
| 43 | + " ctx.actions.write(out, '')", |
| 44 | + " return [", |
| 45 | + " DefaultInfo(executable = out),", |
| 46 | + " MyInfo(", |
| 47 | + " single_arch_cpu = ctx.fragments.apple.single_arch_cpu,", |
| 48 | + " single_arch_platform = ctx.fragments.apple.single_arch_platform,", |
| 49 | + " ),", |
| 50 | + " ]", |
| 51 | + "my_binary = rule(", |
| 52 | + " fragments = ['apple'],", |
| 53 | + " implementation = _my_binary_impl,", |
| 54 | + ")", |
| 55 | + "def _my_rule_impl(ctx):", |
| 56 | + " out = ctx.actions.declare_file(ctx.label.name)", |
| 57 | + " ctx.actions.run(", |
| 58 | + " executable = ctx.executable._tool,", |
| 59 | + " arguments = [out.path],", |
| 60 | + " outputs = [out],", |
| 61 | + " )", |
| 62 | + " return ctx.attr._tool[MyInfo]", |
| 63 | + "my_rule = rule(", |
| 64 | + " _my_rule_impl,", |
| 65 | + " attrs = {", |
| 66 | + " '_tool': attr.label(", |
| 67 | + " cfg = 'exec',", |
| 68 | + " executable = True,", |
| 69 | + " default = ('//:bin'),", |
| 70 | + " ),", |
| 71 | + " },", |
| 72 | + ")"); |
| 73 | + scratch.file( |
| 74 | + "BUILD", |
| 75 | + "load(':rules.bzl', 'my_binary', 'my_rule')", |
| 76 | + "my_binary(name = 'bin')", |
| 77 | + "my_rule(name = 'a')", |
| 78 | + "platform(", |
| 79 | + " name = 'macos_arm64',", |
| 80 | + " constraint_values = [", |
| 81 | + " '@platforms//cpu:aarch64',", |
| 82 | + " '@platforms//os:osx',", |
| 83 | + " ],", |
| 84 | + ")"); |
| 85 | + scratch.file( |
| 86 | + "/workspace/platform_mappings", |
| 87 | + "platforms:", |
| 88 | + " //:macos_arm64", |
| 89 | + " --cpu=darwin_arm64", |
| 90 | + "flags:", |
| 91 | + " --cpu=darwin_arm64", |
| 92 | + " --apple_platform_type=macos", |
| 93 | + " //:macos_arm64"); |
| 94 | + invalidatePackages(false); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void appleFragmentSingleArchCpu() throws Exception { |
| 99 | + // Test that ctx.fragments.apple.single_arch_cpu returns the execution |
| 100 | + // platform's cpu in a tool's rule context. |
| 101 | + useConfiguration("--extra_execution_platforms=//:macos_arm64"); |
| 102 | + ConfiguredTarget configuredTarget = getConfiguredTarget("//:a"); |
| 103 | + Provider.Key key = |
| 104 | + new StarlarkProvider.Key( |
| 105 | + Label.parseAbsolute("//:rules.bzl", ImmutableMap.of()), "MyInfo"); |
| 106 | + StructImpl myInfo = (StructImpl) configuredTarget.get(key); |
| 107 | + assertThat((String) myInfo.getValue("single_arch_cpu")).isEqualTo("arm64"); |
| 108 | + } |
| 109 | +} |
0 commit comments