diff --git a/CODEOWNERS b/CODEOWNERS index 18baa74fc..d2bc2836b 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1,2 +1,4 @@ -* @laurentlb @brandjon @c-parsons @spomorski +* @laurentlb @brandjon @c-parsons @aiuto android/ @ahumesky @jin +tutorial/android/ @ahumesky @jin +platforms/ @hlopko @katre @agoulti diff --git a/platforms/.gitignore b/platforms/.gitignore new file mode 100644 index 000000000..ac51a054d --- /dev/null +++ b/platforms/.gitignore @@ -0,0 +1 @@ +bazel-* diff --git a/platforms/BUILD b/platforms/BUILD new file mode 100644 index 000000000..f78897718 --- /dev/null +++ b/platforms/BUILD @@ -0,0 +1,37 @@ +# Platform describing a Linux machine. Depending on whether we pass it as +# `--platform` or `--host_platform` we tell Bazel if we care about targetting or +# execution. +platform( + name = "linux_platform", + constraint_values = [ + "@platforms//os:linux", + "//yolo:yolo_lang_1", + ], +) + +# Similarly to the linux platform, this is a platform describing a Windows +# machine. +platform( + name = "windows_platform", + constraint_values = [ + "@platforms//os:windows", + "//yolo:yolo_lang_1", + ], +) + +# Similarly to the linux platform, this is a platform describing Android. +platform( + name = "android_platform", + constraint_values = [ + "@platforms//os:android", + ], +) + +# Platform describing a Linux machine with yolo-lang 3. +platform( + name = "linux_yolo_3_platform", + constraint_values = [ + "@platforms//os:linux", + "//yolo:yolo_lang_3", + ], +) diff --git a/platforms/README.md b/platforms/README.md new file mode 100644 index 000000000..67ddd4e6e --- /dev/null +++ b/platforms/README.md @@ -0,0 +1,44 @@ +# Bazel Platforms Examples + +*This repo requires Bazel 0.28 or later (or built from HEAD).* + +This repo contains a collection of examples demonstrating how to use various +Bazel concepts related to +[platforms](https://docs.bazel.build/versions/master/platforms.html), +[toolchains](https://docs.bazel.build/versions/master/toolchains.html), +[configurations](https://docs.bazel.build/versions/master/skylark/config.html), +and [configurable +attributes](https://docs.bazel.build/versions/master/configurable-attributes.html). + +It also tries to give guidance when each of these concepts is used and should +accompany documentation on [bazel.build](https://bazel.build). Be sure to use +[`--toolchain_resolution_debug`](https://docs.bazel.build/versions/master/command-line-reference.html#flag--toolchain_resolution_debug) +where the resolution isn't obvious. + +## Structure + +``` +├── .bazelrc # Setting defaults until https://github.com/bazelbuild/bazel/issues/7081 is fixed. +│ +├── WORKSPACE # Here we define @platforms repo with constraints. We use common +│   # constraints repository from +│ # https://github.com/bazelbuild/platforms. We also register +│ # toolchains and we register execution platforms there. +│ +├── BUILD # Here we define all needed 'platform' targets that we then use +│   # in examples. +│ +├── examples # Actual examples, one per subpackage. +│ +└── yolo # Yolo-lang rules definition. + │ + ├── BUILD # Here we define all 'toolchain' targets that we then use in + │ # in examples. + │ + └── defs.bzl +``` + +`Yolo-lang` here is obviously not a real programming language, it's just a +simple implementation of Bazel Starlark rules that is meant to demonstrate +examples without confusing us with technical details. + diff --git a/platforms/WORKSPACE b/platforms/WORKSPACE new file mode 100644 index 000000000..470f6e53b --- /dev/null +++ b/platforms/WORKSPACE @@ -0,0 +1,27 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "platforms", + sha256 = "a07fe5e75964361885db725039c2ba673f0ee0313d971ae4f50c9b18cd28b0b5", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/platforms/archive/441afe1bfdadd6236988e9cac159df6b5a9f5a98.zip", + "https://github.com/bazelbuild/platforms/archive/441afe1bfdadd6236988e9cac159df6b5a9f5a98.zip", + ], + strip_prefix = "platforms-441afe1bfdadd6236988e9cac159df6b5a9f5a98" +) +http_archive( + name = "bazel_skylib", + url = "https://github.com/bazelbuild/bazel-skylib/archive/b113ed5d05ccddee3093bb157b9b02ab963c1c32.zip", + sha256 = "cea47b31962206b7ebf2088f749243868d5d9305273205bdd8651567d3e027fc", + strip_prefix = "bazel-skylib-b113ed5d05ccddee3093bb157b9b02ab963c1c32", +) +load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") +bazel_skylib_workspace() + +# Tell Bazel about our toolchains so it can resolve them based on values passed +# in --platform, --host_platform, and --execution_platforms options. +register_toolchains("//yolo:all") + +# Tell Bazel that //:linux_platform is allowed execution platform - that our +# host system or remote execution service can handle that platform. +register_execution_platforms("//:linux_platform") diff --git a/platforms/examples/01_hello_world/BUILD b/platforms/examples/01_hello_world/BUILD new file mode 100644 index 000000000..9e89fd1fb --- /dev/null +++ b/platforms/examples/01_hello_world/BUILD @@ -0,0 +1,5 @@ +load("//yolo:defs.bzl", "yolo_library") + +yolo_library( + name = "a", +) diff --git a/platforms/examples/01_hello_world/README.md b/platforms/examples/01_hello_world/README.md new file mode 100644 index 000000000..5065e36dd --- /dev/null +++ b/platforms/examples/01_hello_world/README.md @@ -0,0 +1,31 @@ +# Example 01: Hello World + +Example demonstrating a workspace with toolchains and platforms setup. + +## Commands + +``` +bazel build //examples/01_hello_world:a + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) +``` + +## Description + +There are a few relevant pieces that have to click in for this example to work. +Be sure to look into top-level `WORKSPACE`, `BUILD`, `yolo/BUILD`, and +`.bazelrc` files. + +We don't tell Bazel which `--platforms` to use, so Bazel takes its defaults, which is +the autodetected host platform at `@local_config_platform//:host`. Bazel selects +the first toolchain that matches that platform. Our toolchain +`//yolo:host_toolchain` has exactly the same constraints, and it indeed matches +and Bazel selected it. diff --git a/platforms/examples/02_using_different_platforms/BUILD b/platforms/examples/02_using_different_platforms/BUILD new file mode 100644 index 000000000..9e89fd1fb --- /dev/null +++ b/platforms/examples/02_using_different_platforms/BUILD @@ -0,0 +1,5 @@ +load("//yolo:defs.bzl", "yolo_library") + +yolo_library( + name = "a", +) diff --git a/platforms/examples/02_using_different_platforms/README.md b/platforms/examples/02_using_different_platforms/README.md new file mode 100644 index 000000000..d29f3335b --- /dev/null +++ b/platforms/examples/02_using_different_platforms/README.md @@ -0,0 +1,42 @@ +# Example 02: Using different platforms + +Example demonstrating how different toolchains are selected for different +platforms. + +## Commands + +``` +bazel build //examples/02_using_different_platforms:a --platforms=//:linux_platform + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'linux', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux', +> }, +> ) + +bazel build //examples/02_using_different_platforms:a --platforms=//:windows_platform + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'windows', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux', +> }, +> ) +``` + +## Description + +Here we tell Bazel that we want to build +`//examples/02_using_different_platforms:a` first for Linux, then for Windows. +Both of these times we want to execute the build on Linux (note the call to +`register_execution_platforms` in the `WORKSPACE` file). + +We see that toolchains used to build the library changed between Bazel +invocations. diff --git a/platforms/examples/03_target_not_compatible_with_constraint/BUILD b/platforms/examples/03_target_not_compatible_with_constraint/BUILD new file mode 100644 index 000000000..3989664a8 --- /dev/null +++ b/platforms/examples/03_target_not_compatible_with_constraint/BUILD @@ -0,0 +1,11 @@ +load("//yolo:defs.bzl", "yolo_library") + +yolo_library( + name = "a", + exec_compatible_with = [ "@platforms//os:linux" ], +) + +yolo_library( + name = "b", + exec_compatible_with = [ "@platforms//os:windows" ], +) diff --git a/platforms/examples/03_target_not_compatible_with_constraint/README.md b/platforms/examples/03_target_not_compatible_with_constraint/README.md new file mode 100644 index 000000000..abb7a1842 --- /dev/null +++ b/platforms/examples/03_target_not_compatible_with_constraint/README.md @@ -0,0 +1,32 @@ +# Example 03: Execution of target not compatible with constraint + +Example demonstrating how to express that a target can only be built on a +platform that has a specific constraint. + +## Commands + +``` +bazel build //examples/03_target_not_compatible_with_constraint:a --platforms=//:linux_platform + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'linux', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux', +> }, +> ) + +bazel build //examples/03_target_not_compatible_with_constraint:b --platforms=//:linux_platform + +> ERROR: While resolving toolchains for target //examples/03_target_not_compatible_with_constraint:b: +> no matching toolchains found for types //yolo:toolchain_type +> ERROR: Analysis of target '//examples/03_target_not_compatible_with_constraint:b' failed; +> build aborted: no matching toolchains found for types //yolo:toolchain_type +``` + +## Description + +In this example we show how to prevent people from building a target on an +unsupported platform (in this case `:b` can only be built on Windows). diff --git a/platforms/examples/04_select_on_constraint/BUILD b/platforms/examples/04_select_on_constraint/BUILD new file mode 100644 index 000000000..3b61561d9 --- /dev/null +++ b/platforms/examples/04_select_on_constraint/BUILD @@ -0,0 +1,30 @@ +load("//yolo:defs.bzl", "yolo_library") + +package(default_visibility = ["//visibility:private"]) + +alias( + name = "a", + actual = select({ + ":is_windows": ":a_windows", + ":is_linux": ":a_linux", + }), + visibility = ["//visibility:public"], +) + +config_setting( + name = "is_windows", + constraint_values = ["@platforms//os:windows"], +) + +config_setting( + name = "is_linux", + constraint_values = ["@platforms//os:linux"], +) + +yolo_library( + name = "a_windows", +) + +yolo_library( + name = "a_linux", +) diff --git a/platforms/examples/04_select_on_constraint/README.md b/platforms/examples/04_select_on_constraint/README.md new file mode 100644 index 000000000..f5b5f3c5e --- /dev/null +++ b/platforms/examples/04_select_on_constraint/README.md @@ -0,0 +1,55 @@ +# Example 04: Select on constraint + +This example demonstrates how to build a target (or set it's attribute) +differently under different configurations. + +## Commands + +``` +bazel build //examples/04_select_on_constraint:a --platforms=//:linux_platform + +> yolo_library( +> name = 'a_linux', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'linux', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux', +> }, +> ) + +bazel build //examples/04_select_on_constraint:a --platforms=//:windows_platform + +> yolo_library( +> name = 'a_windows', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'windows', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux', +> }, +> ) + +bazel build //examples/04_select_on_constraint:a --platforms=//:android_platform + +> .../examples/04_select_on_constraint/BUILD:6:14: +> Configurable attribute "actual" doesn't match this configuration (would a default condition help?). +> Conditions checked: +> //examples/04_select_on_constraint:is_windows +> //examples/04_select_on_constraint:is_linux +``` + +# Description + +In this example we wanted select a specialized target depending on the operating +system. We used +[`alias`](https://docs.bazel.build/versions/master/be/general.html#alias) to +create the entry-point target and made only that target visible to the outside. +We used +[`config_setting.constraint_values`](https://docs.bazel.build/versions/master/be/general.html#config_setting.constraint_values) +attribute to select on constraint values of the currently used platform. And we +used +[`select`](https://docs.bazel.build/versions/master/be/functions.html#select) to +conditionally take the right target. Note we didn't include +`//conditions:default` in the select. As a result, the third Bazel invocation +fails on missing condition. diff --git a/platforms/examples/05_select_on_platform/BUILD b/platforms/examples/05_select_on_platform/BUILD new file mode 100644 index 000000000..1e15e390b --- /dev/null +++ b/platforms/examples/05_select_on_platform/BUILD @@ -0,0 +1,34 @@ +load("//yolo:defs.bzl", "fail_with_msg", "yolo_library") + +package(default_visibility = ["//visibility:private"]) + +alias( + name = "a", + actual = select({ + ":is_windows": ":a_windows", + ":is_linux": ":a_linux", + }), + visibility = ["//visibility:public"], +) + +config_setting( + name = "is_windows", + values = { + "platforms": "//:windows_platform", + }, +) + +config_setting( + name = "is_linux", + values = { + "platforms": "//:linux_platform", + }, +) + +yolo_library( + name = "a_windows", +) + +yolo_library( + name = "a_linux", +) diff --git a/platforms/examples/05_select_on_platform/README.md b/platforms/examples/05_select_on_platform/README.md new file mode 100644 index 000000000..231ea80ab --- /dev/null +++ b/platforms/examples/05_select_on_platform/README.md @@ -0,0 +1,53 @@ +# Example 05: Select on platform + +*Don't do this. Read the reasoning below.* + +## Commands + +``` +bazel build //examples/05_select_on_platform:a --platforms=//:linux_platform + +> yolo_library( +> name = 'a_linux', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'linux', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux', +> }, +> ) + +bazel build //examples/05_select_on_platform:a --platforms=//:windows_platform + +> yolo_library( +> name = 'a_windows', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'windows', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux', +> }, +> ) +``` + +## Description + +In this example we demonstrate that one can also select on bare `--platforms` +option. That's not recommended though: + +1) In this example we select on the target platform. When the target that we + select in is used during the build as a tool to build something else (= our + target will be built in the host configuration), the select will be + incorrect. +2) While `constraint_setting`s and `constraint_value`s tend to be universal, + like operating system or cpu architecture (reasoning behind having one + unified repository for the whole Bazel ecosystem and existence of + [https://github.com/bazelbuild/platforms](https://github.com/bazelbuild/platforms) + shows that), + [`platform`](https://docs.bazel.build/versions/master/be/platform.html#platform) + targets are very project/company specific (e.g. 'description of company + servers', or 'specific mobile device', or 'development board for IoT + project'). If there is any possibility that somebody else will use your + project, select on constraints, not on platforms. By selecting on + `--platforms` you'll force them to use your idea of platforms, or to fork + your project. diff --git a/platforms/examples/06_integer_constraint/BUILD b/platforms/examples/06_integer_constraint/BUILD new file mode 100644 index 000000000..dce0c9316 --- /dev/null +++ b/platforms/examples/06_integer_constraint/BUILD @@ -0,0 +1,35 @@ +load("//yolo:defs.bzl", "fail_with_msg", "yolo_library") +load("@bazel_skylib//lib:selects.bzl", "selects") + +package(default_visibility = ["//visibility:private"]) + +alias( + name = "a", + actual = select({ + ":yolo_lang_greater_or_equal_than_2": ":a_needs_greater_or_equal_than_2", + }), + visibility = ["//visibility:public"], +) + +# Matching both lang 2 and 3. +selects.config_setting_group( + name = "yolo_lang_greater_or_equal_than_2", + match_any = [ + ":yolo_lang_2_setting", + ":yolo_lang_3_setting", + ], +) + +config_setting( + name = "yolo_lang_2_setting", + constraint_values = ["//yolo:yolo_lang_2"], +) + +config_setting( + name = "yolo_lang_3_setting", + constraint_values = ["//yolo:yolo_lang_3"], +) + +yolo_library( + name = "a_needs_greater_or_equal_than_2", +) diff --git a/platforms/examples/06_integer_constraint/README.md b/platforms/examples/06_integer_constraint/README.md new file mode 100644 index 000000000..96b7dfaf5 --- /dev/null +++ b/platforms/examples/06_integer_constraint/README.md @@ -0,0 +1,54 @@ +# Example 06: Matching multiple constraints + +Example that demonstrates how to match multiple constraints of the same +`constraint_setting`. + +## Commands + +``` +bazel build //examples/06_integer_constraint:a --platforms=//:linux_platform + +> .../examples/06_integer_constraint/BUILD:7:14: +> Configurable attribute "actual" doesn't match this configuration (would a default condition help?). +> Conditions checked: +> //examples/06_integer_constraint:yolo_lang_3 + +bazel build //examples/06_integer_constraint:a --platforms=//:linux_yolo_3_platform --host_platform=//:linux_yolo_3_platform + +> yolo_library( +> name = 'a_needs_greater_or_equal_than_2', +> toolchain = { +> 'targetting_cpu': '-', +> 'targetting_os': 'linux_with_yolo_lang_3', +> 'executing_on_cpu': '-', +> 'executing_on_os': 'linux_with_yolo_lang_3', +> }, +> ) +``` + +## Description + +[config_setting](https://docs.bazel.build/versions/master/be/general.html#config_setting) +handles AND conditions natively. In this example we show how to express OR +condition using a little help of macros from +[Skylib](https://github.com/bazelbuild/bazel-skylib). + +In this example we show how to emulate greater-than and less-than using selects. +Similar mechanism can be used to express hierarchical constraints. We can have a +`config_setting` that encodes that 'haswell' cpu implies 'SSE': + +``` +selects.config_setting_group( + name = "has_sse", + match_any = ["//cpu_extensions:sse3", "//cpu:haswell", ...], +) +``` + +Or a `config_setting` that encodes that Ubuntu is Linux: + +``` +selects.config_setting_group( + name = "is_linux", + match_any = ["//distributions:ubuntu", "@platforms//os:linux", ...], +) +``` diff --git a/platforms/examples/07_using_define/BUILD b/platforms/examples/07_using_define/BUILD new file mode 100644 index 000000000..ad33499b9 --- /dev/null +++ b/platforms/examples/07_using_define/BUILD @@ -0,0 +1,24 @@ +load("//yolo:defs.bzl", "yolo_library") + +config_setting( + name = "is_foo_defined", + values = { + "define": "is_foo_defined=true", + }, +) + +filegroup( + name = "everything", + srcs = [":a"] + select({ + ":is_foo_defined": [":only_with_foo"], + "//conditions:default": [], + }), +) + +yolo_library( + name = "a", +) + +yolo_library( + name = "only_with_foo", +) diff --git a/platforms/examples/07_using_define/README.md b/platforms/examples/07_using_define/README.md new file mode 100644 index 000000000..a99de2c3f --- /dev/null +++ b/platforms/examples/07_using_define/README.md @@ -0,0 +1,56 @@ +# Example 07: Using --define + +Example demonstraint the use of +[`--define`](https://docs.bazel.build/versions/master/command-line-reference.html#flag--define). + +## Commands + +``` +bazel build //examples/07_using_define:everything + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) + +bazel build //examples/07_using_define:everything --define is_foo_defined=true + +> yolo_library( +> name = 'only_with_foo', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) + +``` + +# Description + +Here we have a simple example how to use +[`--define`](https://docs.bazel.build/versions/master/command-line-reference.html#flag--define). +`--define` is one of the simplest configurability mechanism in Bazel, it's also +one of the oldest and least expressive. + +`--define` is typically used when more sophistication is not needed. It should +not be used for toolchain selection, for configuring the build with that +logically belongs platforms and constraints, and for configuration state that is +read by rules themselves. + + diff --git a/platforms/examples/08_using_build_setting/BUILD b/platforms/examples/08_using_build_setting/BUILD new file mode 100644 index 000000000..7238a5384 --- /dev/null +++ b/platforms/examples/08_using_build_setting/BUILD @@ -0,0 +1,30 @@ +load("//yolo:defs.bzl", "yolo_library") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") + +bool_flag( + name = "foo_enabled", + build_setting_default = False, +) + +config_setting( + name = "is_foo_enabled", + flag_values = { + ":foo_enabled": "True", + }, +) + +filegroup( + name = "everything", + srcs = [":a"] + select({ + ":is_foo_enabled": [":only_with_foo"], + "//conditions:default": [], + }), +) + +yolo_library( + name = "a", +) + +yolo_library( + name = "only_with_foo", +) diff --git a/platforms/examples/08_using_build_setting/README.md b/platforms/examples/08_using_build_setting/README.md new file mode 100644 index 000000000..9cc8fa967 --- /dev/null +++ b/platforms/examples/08_using_build_setting/README.md @@ -0,0 +1,81 @@ +# Example 08: Using build_setting + +This example demonstrates how to use Starlark configuration options. + +## Commands + +``` +bazel build //examples/08_using_build_setting:a + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) + +bazel build //examples/08_using_build_setting:a --//examples/08_using_build_setting:foo_enabled + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) +> yolo_library( +> name = 'only_with_foo', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) + +bazel build //examples/08_using_build_setting:a --//examples/08_using_build_setting:foo_enabled=0 + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) + +bazel build //examples/08_using_build_setting:a --//examples/08_using_build_setting:foo_enabled=1 + +> yolo_library( +> name = 'a', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) +> yolo_library( +> name = 'only_with_foo', +> toolchain = { +> 'targetting_cpu': 'host', +> 'targetting_os': 'host', +> 'executing_on_cpu': 'host', +> 'executing_on_os': 'host', +> }, +> ) +``` + +## Description + +Here we show how to use Bazel's [Starlark configuration +options](https://docs.bazel.build/versions/master/skylark/config.html) to +express exactly what exercise 07 - Using --define does. To simplify our example +we make use of [Skylib's +common_settings.bzl](https://github.com/bazelbuild/bazel-skylib/blob/master/rules/common_settings.bzl). diff --git a/platforms/examples/BUILD b/platforms/examples/BUILD new file mode 100644 index 000000000..e69de29bb diff --git a/platforms/sanity_check.sh b/platforms/sanity_check.sh new file mode 100755 index 000000000..e7e91ed32 --- /dev/null +++ b/platforms/sanity_check.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -euo pipefail + +_BAZEL_BINARY="${BAZEL_BINARY:-"bazel"}" + +function b() { + $_BAZEL_BINARY build $@ +} + +b //examples/01_hello_world:a +b //examples/02_using_different_platforms:a --platforms=//:linux_platform +b //examples/02_using_different_platforms:a --platforms=//:windows_platform +b //examples/03_target_not_compatible_with_constraint:a --platforms=//:linux_platform +b //examples/03_target_not_compatible_with_constraint:b --platforms=//:linux_platform && (exit 15) || true +b //examples/04_select_on_constraint:a --platforms=//:linux_platform +b //examples/04_select_on_constraint:a --platforms=//:windows_platform +b //examples/04_select_on_constraint:a --platforms=//:android_platform && (exit 18) || true +b //examples/05_select_on_platform:a --platforms=//:linux_platform +b //examples/05_select_on_platform:a --platforms=//:windows_platform +b //examples/06_integer_constraint:a --platforms=//:linux_platform && (exit 21) || true +b //examples/06_integer_constraint:a --platforms=//:linux_yolo_3_platform --host_platform=//:linux_yolo_3_platform +b //examples/07_using_define:everything +b //examples/07_using_define:everything --define is_foo_defined=true +b //examples/08_using_build_setting:everything +b //examples/08_using_build_setting:everything --//examples/08_using_build_setting:foo_enabled +b //examples/08_using_build_setting:everything --//examples/08_using_build_setting:foo_enabled=False +b //examples/08_using_build_setting:everything --//examples/08_using_build_setting:foo_enabled=True diff --git a/platforms/yolo/BUILD b/platforms/yolo/BUILD new file mode 100644 index 000000000..f96b437ec --- /dev/null +++ b/platforms/yolo/BUILD @@ -0,0 +1,115 @@ +# This package contains rule definitions for Yolo-lang. + +load("//yolo:defs.bzl", "yolo_library", "yolo_toolchain") + +# @local_config_platform is autodetecting host platform and generating relevant +# constraints. +load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS") + +package(default_visibility = ["//visibility:public"]) + +# Toolchain type is used to distinguish toolchains per languages. +# +# By convention, toolchain_type targets are named "toolchain_type" and +# distinguished by their package path. So the full path for this would be +# //yolo:toolchain_type. +toolchain_type(name = "toolchain_type") + +# Toolchain that targets Linux, and executes on Linux. +toolchain( + name = "linux_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + ":yolo_lang_1", + ], + target_compatible_with = [ + "@platforms//os:linux", + ":yolo_lang_1", + ], + toolchain = ":linux_yolo_toolchain", + toolchain_type = ":toolchain_type", +) + +yolo_toolchain( + name = "linux_yolo_toolchain", + executing_on_cpu = "-", + executing_on_os = "linux", + targetting_cpu = "-", + targetting_os = "linux", +) + +# Toolchain that targets Windows, and executes on Linux. +toolchain( + name = "windows_toolchain", + exec_compatible_with = ["@platforms//os:linux"], + target_compatible_with = ["@platforms//os:windows"], + toolchain = ":windows_yolo_toolchain", + toolchain_type = ":toolchain_type", +) + +yolo_toolchain( + name = "windows_yolo_toolchain", + executing_on_cpu = "-", + executing_on_os = "linux", + targetting_cpu = "-", + targetting_os = "windows", +) + +# Naive toolchain that targets and executes on the current host platforms as +# autodetected by Bazel. +toolchain( + name = "host_toolchain", + exec_compatible_with = HOST_CONSTRAINTS, + target_compatible_with = HOST_CONSTRAINTS, + toolchain = ":host_yolo_toolchain", + toolchain_type = ":toolchain_type", +) + +yolo_toolchain( + name = "host_yolo_toolchain", + executing_on_cpu = "host", + executing_on_os = "host", + targetting_cpu = "host", + targetting_os = "host", +) + +# Toolchain that targets Linux, and executes on Linux, and requires yolo_lang_3 +# version. +toolchain( + name = "linux_lang_3_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + ":yolo_lang_3", + ], + target_compatible_with = [ + "@platforms//os:linux", + ":yolo_lang_3", + ], + toolchain = ":linux_lang_3_yolo_toolchain", + toolchain_type = ":toolchain_type", +) + +yolo_toolchain( + name = "linux_lang_3_yolo_toolchain", + executing_on_cpu = "-", + executing_on_os = "linux_with_yolo_lang_3", + targetting_cpu = "-", + targetting_os = "linux_with_yolo_lang_3", +) + +constraint_setting(name = "yolo_lang_version") + +constraint_value( + name = "yolo_lang_1", + constraint_setting = ":yolo_lang_version", +) + +constraint_value( + name = "yolo_lang_2", + constraint_setting = ":yolo_lang_version", +) + +constraint_value( + name = "yolo_lang_3", + constraint_setting = ":yolo_lang_version", +) diff --git a/platforms/yolo/defs.bzl b/platforms/yolo/defs.bzl new file mode 100644 index 000000000..aef179bbc --- /dev/null +++ b/platforms/yolo/defs.bzl @@ -0,0 +1,50 @@ +def _yolo_library_impl(ctx): + yolo_toolchain = ctx.toolchains["//yolo:toolchain_type"] + print("\n" + "\n".join([ + "yolo_library(", + " name = '" + ctx.attr.name + "',", + " toolchain = {", + " 'targetting_cpu': '" + yolo_toolchain.targetting_cpu + "',", + " 'targetting_os': '" + yolo_toolchain.targetting_os + "',", + " 'executing_on_cpu': '" + yolo_toolchain.executing_on_cpu + "',", + " 'executing_on_os': '" + yolo_toolchain.executing_on_os + "',", + " },", + ")", + ])) + return [] + +yolo_library = rule( + implementation = _yolo_library_impl, + toolchains = ["//yolo:toolchain_type"], +) + +def _yolo_toolchain_impl(ctx): + toolchain_info = platform_common.ToolchainInfo( + targetting_cpu = ctx.attr.targetting_cpu, + targetting_os = ctx.attr.targetting_os, + executing_on_cpu = ctx.attr.executing_on_cpu, + executing_on_os = ctx.attr.executing_on_os, + ) + return [toolchain_info] + +yolo_toolchain = rule( + implementation = _yolo_toolchain_impl, + attrs = { + "targetting_cpu": attr.string(mandatory = True), + "targetting_os": attr.string(mandatory = True), + "executing_on_cpu": attr.string(mandatory = True), + "executing_on_os": attr.string(mandatory = True), + }, +) + +def _fail_with_msg(ctx): + yolo_toolchain = ctx.toolchains["//yolo:toolchain_type"] + fail(ctx.attr.msg + " Selected toolchain: " + str(yolo_toolchain) + ".") + +fail_with_msg = rule( + implementation = _fail_with_msg, + attrs = { + "msg": attr.string(mandatory = True), + }, + toolchains = ["//yolo:toolchain_type"], +)