Skip to content

Commit 4057c4d

Browse files
authored
C++ test example (#352)
- Builds up on #350 to include a test target for cpp. - Includes definition of remote test toolchain as well as action keys for RE
1 parent d2ad2ea commit 4057c4d

File tree

11 files changed

+156
-6
lines changed

11 files changed

+156
-6
lines changed

buck2/cpp/.buckconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2022 EngFlow Inc. 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+
115
[cells]
216
root = .
317
prelude = prelude

buck2/cpp/BUCK

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,33 @@ cxx_binary(
1919
exec_compatible_with = ["//platforms:remote_platform"],
2020
default_target_platform = "//platforms:remote_platform",
2121
)
22+
23+
cxx_binary(
24+
name = "cpp",
25+
srcs = ["main.cc"],
26+
deps = [":cpp_lib"],
27+
exec_compatible_with = ["//platforms:remote_platform"],
28+
default_target_platform = "//platforms:remote_platform",
29+
)
30+
31+
cxx_library(
32+
name = "cpp_lib",
33+
srcs = [
34+
"hello.cc",
35+
],
36+
exported_headers = glob(["**/*.h"]),
37+
visibility = ["PUBLIC"],
38+
exec_compatible_with = ["//platforms:remote_platform"],
39+
default_target_platform = "//platforms:remote_platform",
40+
)
41+
42+
cxx_test(
43+
name = "cpp_test",
44+
srcs = ["hello_test.cc"],
45+
deps = [
46+
":cpp_lib",
47+
],
48+
exec_compatible_with = ["//platforms:remote_platform"],
49+
default_target_platform = "//platforms:remote_platform",
50+
remote_execution_action_key_providers = "//platforms:remote_execution_action_keys",
51+
)

buck2/cpp/README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ It is based on two existing samples in the Buck2 upstream repo:
1111

1212
In the `platforms` cell we specify:
1313
* The platform used for remote execution in this project `root//platforms:remote_platform`, which includes the definition of the Docker image used for remote execution, and that defines constraints for targets to run in the remote execution environment. This platform provides an `ExecutionPlatformRegistrationInfo` a `ConfigurationInfo` and a `PlatformInfo` to be able to be used in the `.buckconfig`, and in the `exec_compatible_with` and `default_target_platform` of `cxx_*` rules.
14+
* The `root//platforms:remote_execution_action_keys` target that provides a `BuildModeInfo` which is necessary for the prelude to correctly configure remote execution of tests. It defines two attributes that can be used as cache silo keys.
1415

1516
In the `toolchains` cell we specify:
1617

1718
* The c++ toolchain `root//toolchains:cxx_tools_info_toolchain` that is compatible with the remote execution environment.
1819
* The clang tools, `root//toolchains:path_clang_tools, which is used by the c++ toolchain, and specifies the tools installed in the Docker image.
20+
* The remote test execution toolchain, `root//toolchains:remote_test_execution_toolchain`. This toolchain defines platform options in the form of `capabilities`. Critically these include the `container-image`.
1921

2022
The main `BUCK` file defines:
2123

22-
* A `cxx_binary` binary target that has the `exec_compatible_with` attr pointing to the `root//platforms:remote_platform` target and the `default_target_platform` attr pointing to the `root//platforms:remote` target.
24+
* A `cxx_binary` target that has the `exec_compatible_with` as well as the `default_target_platform` attrs pointing to the `root//platforms:remote_platform`.
25+
* A `cxx_library`target that has the `exec_compatible_with` as well as the `default_target_platform` attrs pointing to the `root//platforms:remote_platform`.
26+
* A `cxx_test` target that has the `exec_compatible_with` as well as the `default_target_platform` attrs pointing to the `root//platforms:remote_platform`. It also has a `remote_execution_action_key_providers` attr that points to the `root//platforms:remote_execution_action_keys` target.
2327

2428
### Relevant configs in `.buckconfig`
2529

@@ -40,9 +44,14 @@ http_headers = x-engflow-auth-method:jwt-v0,x-engflow-auth-token:LONG_JW
4044

4145
Clone the repository and replace the relevant configs in `.buckconfig`.
4246

43-
Build and run the example:
47+
Build the example:
4448

4549
```
46-
buck2 build //:main
47-
buck2 run -v 4 //:main
50+
buck2 build //:cpp_lib
51+
```
52+
53+
Test the example:
54+
55+
```
56+
buck2 test //:cpp_test
4857
```

buck2/cpp/hello.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "hello.h"
2+
3+
#include <string>
4+
5+
std::string hello() {
6+
return "hello";
7+
}

buck2/cpp/hello.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef cpp_hello_h
2+
#define cpp_hello_h
3+
4+
#include <string>
5+
6+
std::string hello();
7+
8+
#endif

buck2/cpp/hello_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <iostream>
2+
#include "hello.h"
3+
4+
int main() {
5+
auto got = hello();
6+
if (got != "hello") {
7+
std::cerr << "got '" << got << "', want 'hello'" << std::endl;
8+
return 1;
9+
}
10+
return 0;
11+
}

buck2/cpp/main.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
#include "hello.h"
3+
4+
int main() {
5+
std::cout << hello() << std::endl;
6+
return 0;
7+
}

buck2/cpp/platforms/BUCK

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
1+
# Copyright 2022 EngFlow Inc. 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+
115
load(":defs.bzl", "platforms")
16+
load(":defs.bzl", "action_keys")
217

318
# This platform configures details of remote execution.
419
platforms(
520
name = "remote_platform",
621
cpu_configuration = "config//cpu:x86_64",
722
os_configuration = "config//os:linux",
823
)
24+
25+
# This action_key provides a default BuildModeInfo that is needed for RE of tests to function properly.
26+
# The values in `cell` and `mode` can be used, in practice, to create cache silos. Any values can be given to these attributes.
27+
action_keys(
28+
name = "remote_execution_action_keys",
29+
cell = "standard",
30+
mode = "standard",
31+
visibility = ["PUBLIC"],
32+
)

buck2/cpp/platforms/defs.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
# This platform is essentially the same as the one provided in https://github.com/facebook/buck2/blob/804d62242214455d51787f7c8c96a1e12c75ec32/examples/remote_execution/engflow/platforms/defs.bzl
1616
# The main difference is we enable passing CPU and OS constraints and we use the sample EngFlow RE image.
17+
load("@prelude//:build_mode.bzl", "BuildModeInfo")
18+
1719
def _platforms(ctx):
1820
constraints = dict()
1921
constraints.update(ctx.attrs.cpu_configuration[ConfigurationInfo].constraints)
@@ -49,10 +51,24 @@ def _platforms(ctx):
4951
PlatformInfo(label = str(name), configuration = configuration),
5052
]
5153

54+
def _action_keys(ctx):
55+
return [
56+
DefaultInfo(),
57+
BuildModeInfo(cell = ctx.attrs.cell, mode = ctx.attrs.mode),
58+
]
59+
5260
platforms = rule(
5361
attrs = {
5462
"cpu_configuration": attrs.dep(providers = [ConfigurationInfo]),
5563
"os_configuration": attrs.dep(providers = [ConfigurationInfo]),
5664
},
5765
impl = _platforms
5866
)
67+
68+
action_keys = rule(
69+
attrs = {
70+
"cell": attrs.string(),
71+
"mode": attrs.string(),
72+
},
73+
impl = _action_keys
74+
)

buck2/cpp/toolchains/BUCK

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414
load("@prelude//toolchains:python.bzl", "system_python_bootstrap_toolchain")
1515
load("@prelude//toolchains:cxx.bzl", "cxx_tools_info_toolchain")
16+
load("@prelude//toolchains:remote_test_execution.bzl", "remote_test_execution_toolchain")
17+
load("@prelude//tests:test_toolchain.bzl", "noop_test_toolchain")
1618
load("tools.bzl", "path_clang_tools")
1719

1820
# We use the default system python toolchain in this example, focusing only on configuring the cpp toolchain.
@@ -33,6 +35,28 @@ cxx_tools_info_toolchain(
3335
name = "cxx",
3436
target_compatible_with = ["config//os:linux"],
3537
cxx_tools_info = ":clang_tools",
36-
link_style = "static",
38+
visibility = ["PUBLIC"],
39+
)
40+
41+
# Default toolchain for remote execution of tests.
42+
# Note it defines a profile with a capability that defines the `container-image` that matches the one defined in //platforms:remote_platform.
43+
# Capabilities are passed to the RE service to find workers that match them as Platform options.
44+
remote_test_execution_toolchain(
45+
name = "remote_test_execution",
46+
visibility = ["PUBLIC"],
47+
default_profile = "cxx_re_toolchain",
48+
profiles = {
49+
"cxx_re_toolchain": {
50+
"use_case": "cxx-testing",
51+
"capabilities": {
52+
"container-image" : "docker://gcr.io/bazel-public/ubuntu2004-java11@sha256:69a78f121230c6d5cbfe2f4af8ce65481aa3f2acaaaf8e899df335f1ac1b35b5",
53+
},
54+
}
55+
},
56+
)
57+
58+
# In newer versions of buck2 this toolchain is needed `noop_test_toolchain`.
59+
noop_test_toolchain(
60+
name = "test",
3761
visibility = ["PUBLIC"],
3862
)

buck2/cpp/toolchains/tools.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ def _path_clang_tools_impl(_ctx) -> list[Provider]:
3838
path_clang_tools = rule(
3939
impl = _path_clang_tools_impl,
4040
attrs = {},
41-
)
41+
)

0 commit comments

Comments
 (0)