Skip to content

Commit

Permalink
Add --incompatible_use_platforms_repo_for_constraints
Browse files Browse the repository at this point in the history
This change adds an incompatible flag to disable constrains bundled with Bazel
in @bazel_tools.

Incompatible change issue:
bazelbuild#8622

Tracking issue:
bazelbuild#6516

RELNOTES: Incompatible change `--incompatible_use_platforms_repo_for_constraints` has been added. See bazelbuild#8622 for details.

Closes bazelbuild#8625.

PiperOrigin-RevId: 253242115
  • Loading branch information
hlopko authored and copybara-github committed Jun 14, 2019
1 parent 1dbbc01 commit 332379a
Show file tree
Hide file tree
Showing 23 changed files with 416 additions and 223 deletions.
3 changes: 3 additions & 0 deletions scripts/bootstrap/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ EOF
# Set up @bazel_tools//platforms properly
mkdir -p ${BAZEL_TOOLS_REPO}/platforms
cp tools/platforms/BUILD.tools ${BAZEL_TOOLS_REPO}/platforms/BUILD
link_file \
"${PWD}/tools/platforms/fail_with_incompatible_use_platforms_repo_for_constraints.bzl" \
"${BAZEL_TOOLS_REPO}/platforms/fail_with_incompatible_use_platforms_repo_for_constraints.bzl"

# Overwrite tools.WORKSPACE, this is only for the bootstrap binary
chmod u+w "${OUTPUT_DIR}/classes/com/google/devtools/build/lib/bazel/rules/tools.WORKSPACE"
Expand Down
29 changes: 15 additions & 14 deletions site/docs/platforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,36 @@ glibc version of 2.25. (See below for more on Bazel's built-in constraints.)
platform(
name = "linux_x86",
constraint_values = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
"@platforms//os:linux",
"@platforms//cpu:x86_64",
":glibc_2_25",
],
)
```

Note that it is an error for a platform to specify more than one value of the
same constraint setting, such as `@bazel_tools//platforms:x86_64` and
`@bazel_tools//platforms:arm` for `@bazel_tools//platforms:cpu`.
same constraint setting, such as `@platforms//cpu:x86_64` and
`@platforms//cpu:arm` for `@platforms//cpu:cpu`.

## Built-in constraints and platforms

Bazel ships with constraint definitions for the most popular CPU architectures
and operating systems. These are all located in the package
`@bazel_tools//platforms`:
and operating systems. These are all located in the repository
`@platforms`. This repository is developed at
[github.com/bazelbuild/platforms](https://github.com/bazelbuild/platforms):

* `:cpu` for the CPU architecture, with values `:x86_32`, `:x86_64`, `:ppc`,
`:arm`, `:s390x`
* `:os` for the operating system, with values `:android`, `:freebsd`, `:ios`,
`:linux`, `:osx`, `:windows`
* `//cpu:cpu` for the CPU architecture, with values `//cpu:x86_32`,
`//cpu:x86_64`, `//cpu:ppc`, `//cpu:arm`, `//cpu:s390x`
* `//os:os` for the operating system, with values `//os:android`,
`//os:freebsd`, `//os:ios`, `//os:linux`, `//os:osx`, `//os:windows`

There are also the following special platform definitions:

* `:host_platform` - represents the CPU and operating system for the host
environment
* `@bazel_tools//platforms:host_platform` - represents the CPU and operating
system for the host environment

* `:target_platform` - represents the CPU and operating system for the target
environment
* `@bazel_tools//platforms:target_platform` - represents the CPU and operating
system for the target environment

The CPU values used by these two platforms can be specified with the
`--host_cpu` and `--cpu` flags.
Expand Down
24 changes: 12 additions & 12 deletions site/docs/toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ We can improve on this solution by using `select` to choose the `compiler`
config_setting(
name = "on_linux",
constraint_values = [
"@bazel_tools//platforms:linux",
"@platforms//os:linux",
],
)

config_setting(
name = "on_windows",
constraint_values = [
"@bazel_tools//platforms:windows",
"@platforms//os:windows",
],
)

Expand Down Expand Up @@ -276,12 +276,12 @@ appropriate for a given platform.
toolchain(
name = "barc_linux_toolchain",
exec_compatible_with = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
target_compatible_with = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:x86_64",
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
toolchain = ":barc_linux",
toolchain_type = ":toolchain_type",
Expand All @@ -290,12 +290,12 @@ toolchain(
toolchain(
name = "barc_windows_toolchain",
exec_compatible_with = [
"@bazel_tools//platforms:windows",
"@bazel_tools//platforms:x86_64",
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
target_compatible_with = [
"@bazel_tools//platforms:windows",
"@bazel_tools//platforms:x86_64",
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
toolchain = ":barc_windows",
toolchain_type = ":toolchain_type",
Expand Down Expand Up @@ -336,7 +336,7 @@ toolchain will be selected based on the target and execution platforms.
platform(
name = "my_target_platform",
constraint_values = [
"@bazel_tools//platforms:linux",
"@platforms//os:linux",
],
)

Expand All @@ -351,7 +351,7 @@ bazel build //my_pkg:my_bar_binary --platforms=//my_pkg:my_target_platform
```

Bazel will see that `//my_pkg:my_bar_binary` is being built with a platform that
has `@bazel_tools//platforms:linux` and therefore resolve the
has `@platforms//os:linux` and therefore resolve the
`//bar_tools:toolchain_type` reference to `//bar_tools:barc_linux_toolchain`.
This will end up building `//bar_tools:barc_linux` but not
`//barc_tools:barc_windows`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public class CoreOptions extends FragmentOptions implements Cloneable {
help = "If true, the genfiles directory is folded into the bin directory.")
public boolean mergeGenfilesDirectory;

@Option(
name = "incompatible_use_platforms_repo_for_constraints",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
metadataTags = {
OptionMetadataTag.INCOMPATIBLE_CHANGE,
OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
},
help = "If true, constraint settings from @bazel_tools are removed.")
public boolean usePlatformsRepoForConstraints;

@Option(
name = "define",
converter = Converters.AssignmentConverter.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ public RepositoryDirectoryValue.Builder fetch(
static String cpuToConstraint(CPU cpu) {
switch (cpu) {
case X86_32:
return "@bazel_tools//platforms:x86_32";
return "@platforms//cpu:x86_32";
case X86_64:
return "@bazel_tools//platforms:x86_64";
return "@platforms//cpu:x86_64";
case PPC:
return "@bazel_tools//platforms:ppc";
return "@platforms//cpu:ppc";
case ARM:
return "@bazel_tools//platforms:arm";
return "@platforms//cpu:arm";
case AARCH64:
return "@bazel_tools//platforms:aarch64";
return "@platforms//cpu:aarch64";
case S390X:
return "@bazel_tools//platforms:s390x";
return "@platforms//cpu:s390x";
default:
// Unknown, so skip it.
return null;
Expand All @@ -102,13 +102,13 @@ static String cpuToConstraint(CPU cpu) {
static String osToConstraint(OS os) {
switch (os) {
case DARWIN:
return "@bazel_tools//platforms:osx";
return "@platforms//os:osx";
case FREEBSD:
return "@bazel_tools//platforms:freebsd";
return "@platforms//os:freebsd";
case LINUX:
return "@bazel_tools//platforms:linux";
return "@platforms//os:linux";
case WINDOWS:
return "@bazel_tools//platforms:windows";
return "@platforms//os:windows";
default:
// Unknown, so skip it.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ Matches an expected configuration state (expressed as Bazel flags or platform co
config_setting(
name = "64bit_glibc_2_25",
constraint_values = [
"@bazel_tools//platforms:x86_64",
"@platforms//cpu:x86_64",
"//example:glibc_2_25",
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public RuleDefinition.Metadata getMetadata() {
<p>Each <code>constraint_setting</code> has an extensible set of associated
<code>constraint_value</code>s. Usually these are defined in the same package, but sometimes a
different package will introduce new values for an existing setting. For instance, the predefined
setting <code>@bazel_tools//platforms:cpu</code> can be extended with a custom value in order to
setting <code>@platforms//cpu:cpu</code> can be extended with a custom value in order to
define a platform targeting an obscure cpu architecture.
<!-- #END_BLAZE_RULE -->*/
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Metadata getMetadata() {
<pre class="code">
constraint_value(
name = "mips",
constraint_setting = "@bazel_tools//platforms:cpu",
constraint_setting = "@platforms//cpu:cpu",
)
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
<p>Each <code>constraint_value</code> in this list must be for a different
<code>constraint_setting</code>. For example, you cannot define a platform that requires the
cpu architecture to be both <code>@bazel_tools//platforms:x86_64</code> and
cpu architecture to be both <code>@platforms//cpu:x86_64</code> and
<code>@bazel_tools//platforms:arm</code>.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(
Expand Down Expand Up @@ -130,8 +130,8 @@ public Metadata getMetadata() {
platform(
name = "linux_arm",
constraint_values = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:arm",
"@platforms//os:linux",
"@platforms//cpu:arm",
],
)
</pre>
Expand Down Expand Up @@ -177,8 +177,8 @@ public Metadata getMetadata() {
platform(
name = "parent",
constraint_values = [
"@bazel_tools//platforms:linux",
"@bazel_tools//platforms:arm",
"@platforms//os:linux",
"@platforms//cpu:arm",
],
remote_execution_properties = """
parent properties
Expand All @@ -188,7 +188,7 @@ public Metadata getMetadata() {
name = "child_a",
parents = [":parent"],
constraint_values = [
"@bazel_tools//platforms:x86_64",
"@platforms//cpu:x86_64",
],
remote_execution_properties = """
child a properties
Expand All @@ -210,8 +210,8 @@ public Metadata getMetadata() {
<ul>
<li>
"child_a" has the constraint values "@bazel_tools//platforms:linux" (inherited from the
parent) and "@bazel_tools//platforms:x86_64" (set directly on the platform). It has the
"child_a" has the constraint values "@platforms//os:linux" (inherited from the
parent) and "@platforms//cpu:x86_64" (set directly on the platform). It has the
"remote_execution_properties" set to "child a properties"
</li>
<li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ private BazelAnalysisMock() {
@Override
public List<String> getWorkspaceContents(MockToolsConfig config) {
String bazelToolWorkspace = config.getPath("/bazel_tools_workspace").getPathString();
String bazelPlatformsWorkspace = config.getPath("/platforms").getPathString();
String localConfigPlatformWorkspace =
config.getPath("/local_config_platform_workspace").getPathString();

return new ArrayList<>(
ImmutableList.of(
"local_repository(name = 'bazel_tools', path = '" + bazelToolWorkspace + "')",
"local_repository(name = 'platforms', path = '" + bazelPlatformsWorkspace + "')",
"local_repository(name = 'local_config_xcode', path = '/local_config_xcode')",
"local_repository(name = 'com_google_protobuf', path = '/protobuf')",
"bind(name = 'android/sdk', actual='@bazel_tools//tools/android:sdk')",
Expand All @@ -99,6 +101,25 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
config.create("/local_config_xcode/WORKSPACE");
config.create("/protobuf/WORKSPACE");
config.overwrite("WORKSPACE", workspaceContents.toArray(new String[workspaceContents.size()]));
config.create("/platforms/WORKSPACE", "workspace(name = 'platforms')");
config.create("/platforms/BUILD");
config.create(
"/platforms/cpu/BUILD",
"constraint_setting(name = 'cpu')",
"constraint_value(name = 'x86_32', constraint_setting = ':cpu')",
"constraint_value(name = 'x86_64', constraint_setting = ':cpu')",
"constraint_value(name = 'ppc', constraint_setting = ':cpu')",
"constraint_value(name = 'arm', constraint_setting = ':cpu')",
"constraint_value(name = 'aarch64', constraint_setting = ':cpu')",
"constraint_value(name = 's390x', constraint_setting = ':cpu')");

config.create(
"/platforms/os/BUILD",
"constraint_setting(name = 'os')",
"constraint_value(name = 'linux', constraint_setting = ':os')",
"constraint_value(name = 'osx', constraint_setting = ':os')",
"constraint_value(name = 'freebsd', constraint_setting = ':os')",
"constraint_value(name = 'windows', constraint_setting = ':os')");
config.create("/bazel_tools_workspace/WORKSPACE", "workspace(name = 'bazel_tools')");
Runfiles runfiles = Runfiles.create();
for (String filename :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public static class CpuConstraintTest {
public static Collection createInputValues() {
return ImmutableList.of(
// CPU value tests.
new Object[] {CPU.X86_64, "@bazel_tools//platforms:x86_64"},
new Object[] {CPU.X86_32, "@bazel_tools//platforms:x86_32"},
new Object[] {CPU.PPC, "@bazel_tools//platforms:ppc"},
new Object[] {CPU.ARM, "@bazel_tools//platforms:arm"},
new Object[] {CPU.AARCH64, "@bazel_tools//platforms:aarch64"},
new Object[] {CPU.S390X, "@bazel_tools//platforms:s390x"});
new Object[] {CPU.X86_64, "@platforms//cpu:x86_64"},
new Object[] {CPU.X86_32, "@platforms//cpu:x86_32"},
new Object[] {CPU.PPC, "@platforms//cpu:ppc"},
new Object[] {CPU.ARM, "@platforms//cpu:arm"},
new Object[] {CPU.AARCH64, "@platforms//cpu:aarch64"},
new Object[] {CPU.S390X, "@platforms//cpu:s390x"});
}

private final CPU testCpu;
Expand Down Expand Up @@ -81,10 +81,10 @@ public static class OsConstraintTest {
public static Collection createInputValues() {
return ImmutableList.of(
// OS value tests.
new Object[] {OS.LINUX, "@bazel_tools//platforms:linux"},
new Object[] {OS.DARWIN, "@bazel_tools//platforms:osx"},
new Object[] {OS.FREEBSD, "@bazel_tools//platforms:freebsd"},
new Object[] {OS.WINDOWS, "@bazel_tools//platforms:windows"});
new Object[] {OS.LINUX, "@platforms//os:linux"},
new Object[] {OS.DARWIN, "@platforms//os:osx"},
new Object[] {OS.FREEBSD, "@platforms//os:freebsd"},
new Object[] {OS.WINDOWS, "@platforms//os:windows"});
}

private final OS testOs;
Expand Down Expand Up @@ -112,9 +112,9 @@ public void unknownOsConstraint() {
@RunWith(JUnit4.class)
public static class FunctionTest extends BuildViewTestCase {
private static final ConstraintSettingInfo CPU_CONSTRAINT =
ConstraintSettingInfo.create(Label.parseAbsoluteUnchecked("@bazel_tools//platforms:cpu"));
ConstraintSettingInfo.create(Label.parseAbsoluteUnchecked("@platforms//cpu:cpu"));
private static final ConstraintSettingInfo OS_CONSTRAINT =
ConstraintSettingInfo.create(Label.parseAbsoluteUnchecked("@bazel_tools//platforms:os"));
ConstraintSettingInfo.create(Label.parseAbsoluteUnchecked("@platforms//os:os"));

@Test
public void generateConfigRepository() throws Exception {
Expand Down
7 changes: 7 additions & 0 deletions src/test/shell/bazel/platforms_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,12 @@ EOF
fail "Bazel failed to build @platforms"
}

function test_incompatible_use_platforms_repo_for_constraints() {
# We test that a built-in @platforms repository is buildable.
bazel build --incompatible_use_platforms_repo_for_constraints @bazel_tools//platforms:all &> \
$TEST_log && fail "Build passed when we expected an error."
expect_log "Constraints from @bazel_tools//platforms have been removed."
}

run_suite "platform mapping test"

2 changes: 1 addition & 1 deletion src/test/shell/integration/discard_graph_edges_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function test_packages_cleared() {
package_count="$(extract_histogram_count "$histo_file" \
'devtools\.build\.lib\..*\.Package$')"
# A few packages aren't cleared.
[[ "$package_count" -le 17 ]] \
[[ "$package_count" -le 18 ]] \
|| fail "package count $package_count too high"
glob_count="$(extract_histogram_count "$histo_file" "GlobValue$")"
[[ "$glob_count" -le 1 ]] \
Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/testenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ toolchain(
name = "py_toolchain",
toolchain = ":py_runtime_pair",
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
target_compatible_with = ["@bazel_tools//platforms:windows"],
target_compatible_with = ["@platforms//os:windows"],
)
EOF
}
Expand Down
Loading

0 comments on commit 332379a

Please sign in to comment.