Skip to content

Commit

Permalink
Rollforward of c++ toolchain-relevant BUILD file and Bazel mocking ch…
Browse files Browse the repository at this point in the history
…anges. That is, a c++ toolchain is added, but a Bazel dependency on that toolchain is not.

PiperOrigin-RevId: 167006332
  • Loading branch information
calpeyser authored and vladmos committed Aug 31, 2017
1 parent f12402b commit 2b983bd
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/create_embedded_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

output_paths = [
('*tools/jdk/BUILD*', lambda x: 'tools/jdk/BUILD'),
('*tools/platforms/platforms.BUILD', lambda x: 'platforms/BUILD'),
('*tools/platforms/*', lambda x: 'platforms/' + os.path.basename(x)),
('*tools/platforms/platforms.BUILD', lambda x: 'tools/platforms/BUILD'),
('*tools/platforms/*', lambda x: 'tools/platforms/' + os.path.basename(x)),
('*JavaBuilder*_deploy.jar', lambda x: 'tools/jdk/' + os.path.basename(x)),
('*JacocoCoverage*_deploy.jar',
lambda x: 'tools/jdk/JacocoCoverage_deploy.jar'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,41 +182,60 @@ public Metadata getMetadata() {
* Share common attributes across both base and Skylark base rules.
*/
public static RuleClass.Builder commonCoreAndSkylarkAttributes(RuleClass.Builder builder) {
return builder
return PlatformSemantics.platformAttributes(builder)
// The visibility attribute is special: it is a nodep label, and loading the
// necessary package groups is handled by {@link LabelVisitor#visitTargetVisibility}.
// Package groups always have the null configuration so that they are not duplicated
// needlessly.
.add(attr("visibility", NODEP_LABEL_LIST).orderIndependent().cfg(HOST)
.nonconfigurable("special attribute integrated more deeply into Bazel's core logic"))
.add(attr("deprecation", STRING).value(deprecationDefault)
.nonconfigurable("Used in core loading phase logic with no access to configs"))
.add(attr("tags", STRING_LIST).orderIndependent().taggable()
.nonconfigurable("low-level attribute, used in TargetUtils without configurations"))
.add(attr("generator_name", STRING).undocumented("internal")
.nonconfigurable("static structure of a rule"))
.add(attr("generator_function", STRING).undocumented("internal")
.nonconfigurable("static structure of a rule"))
.add(attr("generator_location", STRING).undocumented("internal")
.nonconfigurable("static structure of a rule"))
.add(attr("testonly", BOOLEAN).value(testonlyDefault)
.nonconfigurable("policy decision: rules testability should be consistent"))
.add(
attr("visibility", NODEP_LABEL_LIST)
.orderIndependent()
.cfg(HOST)
.nonconfigurable(
"special attribute integrated more deeply into Bazel's core logic"))
.add(
attr("deprecation", STRING)
.value(deprecationDefault)
.nonconfigurable("Used in core loading phase logic with no access to configs"))
.add(
attr("tags", STRING_LIST)
.orderIndependent()
.taggable()
.nonconfigurable("low-level attribute, used in TargetUtils without configurations"))
.add(
attr("generator_name", STRING)
.undocumented("internal")
.nonconfigurable("static structure of a rule"))
.add(
attr("generator_function", STRING)
.undocumented("internal")
.nonconfigurable("static structure of a rule"))
.add(
attr("generator_location", STRING)
.undocumented("internal")
.nonconfigurable("static structure of a rule"))
.add(
attr("testonly", BOOLEAN)
.value(testonlyDefault)
.nonconfigurable("policy decision: rules testability should be consistent"))
.add(attr("features", STRING_LIST).orderIndependent())
.add(attr(":action_listener", LABEL_LIST).cfg(HOST).value(ACTION_LISTENER))
.add(attr(RuleClass.COMPATIBLE_ENVIRONMENT_ATTR, LABEL_LIST)
.allowedRuleClasses(EnvironmentRule.RULE_NAME)
.cfg(Attribute.ConfigurationTransition.HOST)
.allowedFileTypes(FileTypeSet.NO_FILE)
.dontCheckConstraints()
.nonconfigurable("special logic for constraints and select: see ConstraintSemantics")
)
.add(attr(RuleClass.RESTRICTED_ENVIRONMENT_ATTR, LABEL_LIST)
.allowedRuleClasses(EnvironmentRule.RULE_NAME)
.cfg(Attribute.ConfigurationTransition.HOST)
.allowedFileTypes(FileTypeSet.NO_FILE)
.dontCheckConstraints()
.nonconfigurable("special logic for constraints and select: see ConstraintSemantics")
);
.add(
attr(RuleClass.COMPATIBLE_ENVIRONMENT_ATTR, LABEL_LIST)
.allowedRuleClasses(EnvironmentRule.RULE_NAME)
.cfg(Attribute.ConfigurationTransition.HOST)
.allowedFileTypes(FileTypeSet.NO_FILE)
.dontCheckConstraints()
.nonconfigurable(
"special logic for constraints and select: see ConstraintSemantics"))
.add(
attr(RuleClass.RESTRICTED_ENVIRONMENT_ATTR, LABEL_LIST)
.allowedRuleClasses(EnvironmentRule.RULE_NAME)
.cfg(Attribute.ConfigurationTransition.HOST)
.allowedFileTypes(FileTypeSet.NO_FILE)
.dontCheckConstraints()
.nonconfigurable(
"special logic for constraints and select: see ConstraintSemantics"));
}

public static RuleClass.Builder nameAttribute(RuleClass.Builder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class PlatformOptions extends FragmentOptions {
@Option(
name = "experimental_host_platform",
converter = BuildConfiguration.LabelConverter.class,
defaultValue = "@bazel_tools//platforms:host_platform",
defaultValue = "@bazel_tools//tools/platforms:host_platform",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.UNKNOWN},
metadataTags = {OptionMetadataTag.HIDDEN},
Expand All @@ -49,7 +49,7 @@ public class PlatformOptions extends FragmentOptions {
@Option(
name = "experimental_platforms",
converter = BuildConfiguration.LabelListConverter.class,
defaultValue = "@bazel_tools//platforms:target_platform",
defaultValue = "@bazel_tools//tools/platforms:target_platform",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.UNKNOWN},
metadataTags = {OptionMetadataTag.HIDDEN},
Expand Down Expand Up @@ -98,6 +98,8 @@ public class PlatformOptions extends FragmentOptions {
public PlatformOptions getHost(boolean fallback) {
PlatformOptions host = (PlatformOptions) getDefault();
host.platforms = ImmutableList.of(this.hostPlatform);
host.hostPlatform = this.hostPlatform;
host.extraToolchains = this.extraToolchains;
return host;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
import java.util.List;
import javax.annotation.Nullable;

/** Helper class to manage rules' use of platforms. */
public class PlatformSemantics {
Expand All @@ -40,19 +41,22 @@ public class PlatformSemantics {
@Override
public List<Label> resolve(
Rule rule, AttributeMap attributes, BuildConfiguration configuration) {
if (rule.getRuleClassObject().getRequiredToolchains().isEmpty()) {
return null;
// rule may be null for tests
if (rule == null || rule.getRuleClassObject().getRequiredToolchains().isEmpty()) {
return ImmutableList.of();
}
return configuration.getFragment(PlatformConfiguration.class).getTargetPlatforms();
}
};

/** Implementation for the :execution_platform attribute. */
@Nullable
public static final Attribute.LateBoundLabel<BuildConfiguration> EXECUTION_PLATFORM =
new Attribute.LateBoundLabel<BuildConfiguration>(PlatformConfiguration.class) {
@Override
public Label resolve(Rule rule, AttributeMap attributes, BuildConfiguration configuration) {
if (rule.getRuleClassObject().getRequiredToolchains().isEmpty()) {
// rule may be null for tests
if (rule == null || rule.getRuleClassObject().getRequiredToolchains().isEmpty()) {
return null;
}
return configuration.getFragment(PlatformConfiguration.class).getExecutionPlatform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ private static ImmutableMap<Label, ToolchainInfo> findToolchains(
prerequisiteMap
.keys()
.stream()
// Keys in prerequisiteMap can be null, see {@link DependencyResolver#dependentNodeMap}.
.filter(attribute -> attribute != null)
.filter(attribute -> attribute.getName().equals(PlatformSemantics.TOOLCHAINS_ATTR))
.findFirst();
Preconditions.checkState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.DefaultInfo;
import com.google.devtools.build.lib.analysis.OutputGroupProvider;
import com.google.devtools.build.lib.analysis.PlatformSemantics;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.skylark.SkylarkAttr.Descriptor;
import com.google.devtools.build.lib.analysis.test.TestConfiguration;
Expand Down Expand Up @@ -129,10 +128,9 @@ public Label load(String from) throws Exception {
/** Parent rule class for non-executable non-test Skylark rules. */
public static final RuleClass baseRule =
BaseRuleClasses.commonCoreAndSkylarkAttributes(
PlatformSemantics.platformAttributes(
BaseRuleClasses.nameAttribute(
new RuleClass.Builder("$base_rule", RuleClassType.ABSTRACT, true))
.add(attr("expect_failure", STRING))))
BaseRuleClasses.nameAttribute(
new RuleClass.Builder("$base_rule", RuleClassType.ABSTRACT, true))
.add(attr("expect_failure", STRING)))
.build();

/** Parent rule class for executable non-test Skylark rules. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,8 @@ public void testTopLevelTargetsAreTrimmedWithDynamicConfigurations() throws Exce
useConfiguration("--experimental_dynamic_configs=on");
AnalysisResult res = update("//foo:x");
ConfiguredTarget topLevelTarget = Iterables.getOnlyElement(res.getTargetsToBuild());
assertThat(topLevelTarget.getConfiguration().getAllFragments().keySet()).containsExactly(
ruleClassProvider.getUniversalFragment());
assertThat(topLevelTarget.getConfiguration().getAllFragments().keySet())
.containsExactly(ruleClassProvider.getUniversalFragment(), PlatformConfiguration.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public void setup(MockToolsConfig config) throws IOException {
config.create(
"/bazel_tools_workspace/tools/cpp/BUILD",
"package(default_visibility=['//visibility:public'])",
"toolchain_type(name = 'toolchain_type')",
"cc_library(name = 'stl')",
"cc_library(name = 'malloc')",
"cc_toolchain_suite(",
Expand Down Expand Up @@ -123,7 +122,7 @@ public void setup(MockToolsConfig config) throws IOException {
" linker_files = ':empty',",
" module_map = 'crosstool.cppmap', supports_header_parsing = 1,",
" objcopy_files = ':empty', static_runtime_libs = [':empty'], strip_files = ':empty',",
")",
")",
"cc_toolchain(name = 'cc-compiler-armeabi-v7a', all_files = ':empty', ",
" compiler_files = ':empty',",
" cpu = 'local', dwp_files = ':empty', dynamic_runtime_libs = [':empty'], ",
Expand All @@ -145,8 +144,27 @@ public void setup(MockToolsConfig config) throws IOException {
"filegroup(",
" name = 'link_dynamic_library',",
" srcs = ['link_dynamic_library.sh'],",
")");

")",
"toolchain_type(name = 'toolchain_type')",
"toolchain(",
" name = 'toolchain_cc-compiler-piii',",
" toolchain_type = ':toolchain_type',",
" toolchain = '//third_party/crosstool/mock:cc-compiler-piii',",
" target_compatible_with = [':mock_value'],",
")",
"toolchain(",
" name = 'dummy_cc_toolchain',",
" toolchain_type = ':toolchain_type',",
" toolchain = ':dummy_cc_toolchain_impl',",
")",
"load(':dummy_toolchain.bzl', 'dummy_toolchain')",
"dummy_toolchain(name = 'dummy_cc_toolchain_impl')");
config.create(
"/bazel_tools_workspace/tools/cpp/dummy_toolchain.bzl",
"def _dummy_toolchain_impl(ctx):",
" toolchain = platform_common.ToolchainInfo()",
" return [toolchain]",
"dummy_toolchain = rule(_dummy_toolchain_impl, attrs = {})");
config.create(
"/bazel_tools_workspace/tools/cpp/CROSSTOOL",
readCrosstoolFile());
Expand All @@ -156,6 +174,7 @@ public void setup(MockToolsConfig config) throws IOException {
config.create("tools/cpp/link_dynamic_library.sh", "");
}
MockObjcSupport.setup(config);
MockPlatformSupport.setup(config, "/bazel_tools_workspace/tools/platforms");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2017 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.
package com.google.devtools.build.lib.packages.util;

import java.io.IOException;

/** Mocking support for platforms and toolchains. */
public class MockPlatformSupport {

/** Adds mocks for basic host and target platform. */
public static void setup(MockToolsConfig mockToolsConfig, String platformsPath)
throws IOException {
mockToolsConfig.create(
platformsPath + "/BUILD",
"package(default_visibility=['//visibility:public'])",
"platform(",
" name = 'target_platform',",
" target_platform = True,",
")",
"platform(",
" name = 'host_platform',",
" host_platform = True,",
")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ public ConfiguredRuleClassProvider createRuleClassProvider() {
BazelRuleClassProvider.BAZEL_SETUP.init(builder);
CoreRules.INSTANCE.init(builder);
BazelRuleClassProvider.CORE_WORKSPACE_RULES.init(builder);
BazelRuleClassProvider.PLATFORM_RULES.init(builder);
BazelRuleClassProvider.GENERIC_RULES.init(builder);
BazelRuleClassProvider.CPP_RULES.init(builder);
builder.addRuleDefinition(new OnlyCppToolchainTypeRule());
Expand Down
2 changes: 1 addition & 1 deletion tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ filegroup(
"//tools/jdk:package-srcs",
"//tools/jdk:srcs",
"//tools/launcher:srcs",
"//tools/platforms:package-srcs",
"//tools/platforms:srcs",
"//tools/objc:srcs",
"//tools/python:srcs",
"//tools/test:srcs",
Expand Down
15 changes: 15 additions & 0 deletions tools/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,18 @@ filegroup(
name = "link_dynamic_library",
srcs = ["link_dynamic_library.sh"],
)

toolchain_type(name = "toolchain_type")

# A dummy toolchain is necessary to satisfy toolchain resolution until platforms
# are used in c++ by default.
# TODO(b/64754003): Remove once platforms are used in c++ by default.
toolchain(
name = "dummy_cc_toolchain",
toolchain = "dummy_cc_toolchain_impl",
toolchain_type = ":toolchain_type",
)

load(":dummy_toolchain.bzl", "dummy_toolchain")

dummy_toolchain(name = "dummy_cc_toolchain_impl")
15 changes: 15 additions & 0 deletions tools/cpp/BUILD.static
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,18 @@ filegroup(
name = "link_dynamic_library",
srcs = ["link_dynamic_library.sh"],
)

toolchain_type(name = "toolchain_type")

# A dummy toolchain is necessary to satisfy toolchain resolution until platforms
# are used in c++ by default.
# TODO(b/64754003): Remove once platforms are used in c++ by default.
toolchain(
name = "dummy_cc_toolchain",
toolchain = "dummy_cc_toolchain_impl",
toolchain_type = ":toolchain_type",
)

load(":dummy_toolchain.bzl", "dummy_toolchain")

dummy_toolchain(name = "dummy_cc_toolchain_impl")
15 changes: 15 additions & 0 deletions tools/cpp/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,18 @@ cc_toolchain(
strip_files = ":empty",
supports_param_files = 1,
)

toolchain_type(name = "toolchain_type")

# A dummy toolchain is necessary to satisfy toolchain resolution until platforms
# are used in c++ by default.
# TODO(b/64754003): Remove once platforms are used in c++ by default.
toolchain(
name = "dummy_cc_toolchain",
toolchain = "dummy_cc_toolchain_impl",
toolchain_type = ":toolchain_type",
)

load(":dummy_toolchain.bzl", "dummy_toolchain")

dummy_toolchain(name = "dummy_cc_toolchain_impl")
2 changes: 2 additions & 0 deletions tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain"
load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")

def _impl(repository_ctx):
repository_ctx.symlink(
Label("@bazel_tools//tools/cpp:dummy_toolchain.bzl"), "dummy_toolchain.bzl")
cpu_value = get_cpu_value(repository_ctx)
if cpu_value == "freebsd":
# This is defaulting to the static crosstool, we should eventually
Expand Down
Loading

0 comments on commit 2b983bd

Please sign in to comment.