Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rules based toolchain example #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ tasks:
- "--ignore_dev_dependency"
build_targets:
- "//cc/..."
ubuntu_rule_based_toolchains:
name: Ubuntu rule-based toolchains
platform: ubuntu1804
working_directory: examples/rule_based_toolchain
build_flags:
- "--enable_bzlmod"
build_targets:
- "//..."
test_targets:
- "//..."
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
examples/rule_based_toolchain
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/bazel-*
bazel-*
MODULE.bazel.lock
2 changes: 2 additions & 0 deletions examples/rule_based_toolchain/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Do not use the default toolchain.
build --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=0
1 change: 1 addition & 0 deletions examples/rule_based_toolchain/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.2.1
28 changes: 28 additions & 0 deletions examples/rule_based_toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2024 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.

load("@rules_cc//cc:defs.bzl", "cc_test")

licenses(["notice"])

cc_test(
name = "quick_test",
srcs = ["quick_test.cc"],
deps = [
"//dynamic_answer",
"//static_answer",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)
44 changes: 44 additions & 0 deletions examples/rule_based_toolchain/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module(
name = "rule_based_toolchain",
version = "0.0.1",
)

bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "googletest", version = "1.15.2")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "rules_cc")
local_path_override(
module_name = "rules_cc",
path = "../..",
)

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "clang-linux-x86_64",
build_file = "//toolchain:clang.BUILD",
sha256 = "9042f89df9c13a2bf28e16ce34dfe22934b59b5d8390e94b030bb378bdb3c898",
type = "zip",
url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/linux-amd64/+/git_revision:0cfd03ac0d3f9713090a581bda07584754c73a49",
)

http_archive(
name = "clang-linux-aarch64",
build_file = "//toolchain:clang.BUILD",
sha256 = "61abb915821190baddafa973c69a9db9acda5a16ed3a89489ea2b3b030a2330b",
type = "zip",
url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/linux-arm64/+/git_revision:0cfd03ac0d3f9713090a581bda07584754c73a49",
)

http_archive(
name = "linux_sysroot",
build_file = "//toolchain:linux_sysroot.BUILD",
sha256 = "f45ca0d8b46810b94d2a7dbc65f9092337d6a9568b260b51173a5ab9314da25e",
type = "zip",
url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/sysroot/bionic/+/git_revision:702eb9654703a7cec1cadf93a7e3aa269d053943",
)

register_toolchains(
"//toolchain:host_cc_toolchain",
dev_dependency = True,
)
15 changes: 15 additions & 0 deletions examples/rule_based_toolchain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Rule-based toolchains
This example showcases a fully working rule-based toolchain for Linux. This also
serves as an integration test to ensure rule-based toolchains continue to work
as intended.

The complete toolchain configuration lives [here](https://github.com/bazelbuild/rules_cc/tree/main/examples/rule_based_toolchain/toolchain).

# Trying the example
From this directory, you can run example tests that build using this toolchain
with the following command:
```
$ bazel test //...
```

This example currently only supports Linux.
65 changes: 65 additions & 0 deletions examples/rule_based_toolchain/constraint/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2024 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.

load("@bazel_skylib//lib:selects.bzl", "selects")

licenses(["notice"])

selects.config_setting_group(
name = "linux_x86_64",
match_all = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
)

selects.config_setting_group(
name = "linux_aarch64",
match_all = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
)

selects.config_setting_group(
name = "macos_x86_64",
match_all = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
)

selects.config_setting_group(
name = "macos_aarch64",
match_all = [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
)

selects.config_setting_group(
name = "windows_x86_64",
match_all = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
)

selects.config_setting_group(
name = "windows_aarch64",
match_all = [
"@platforms//os:windows",
"@platforms//cpu:aarch64",
],
)
45 changes: 45 additions & 0 deletions examples/rule_based_toolchain/dynamic_answer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2024 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.

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_shared_library")

licenses(["notice"])

cc_library(
name = "headers",
hdrs = ["public/dynamic_answer.h"],
includes = ["public"],
visibility = ["//visibility:private"],
)

cc_library(
name = "answer",
srcs = ["dynamic_answer.c"],
visibility = ["//visibility:private"],
deps = [":headers"],
)

cc_shared_library(
name = "shared_library",
visibility = ["//visibility:private"],
deps = [":answer"],
)

# Forces linkage as a shared library.
cc_library(
name = "dynamic_answer",
srcs = [":shared_library"],
visibility = ["//visibility:public"],
deps = [":headers"],
)
19 changes: 19 additions & 0 deletions examples/rule_based_toolchain/dynamic_answer/dynamic_answer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 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.

#include "dynamic_answer.h"

int dynamic_answer(void) {
return 24;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 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.

#ifndef DYNAMIC_ANSWER_PUBLIC_DYNAMIC_ANSWER_H_
#define DYNAMIC_ANSWER_PUBLIC_DYNAMIC_ANSWER_H_

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int dynamic_answer(void);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#endif // DYNAMIC_ANSWER_PUBLIC_DYNAMIC_ANSWER_H_
26 changes: 26 additions & 0 deletions examples/rule_based_toolchain/quick_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 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.

#include <gtest/gtest.h>

#include "dynamic_answer.h"
#include "static_answer.h"

TEST(Static, ProperlyLinked) {
EXPECT_EQ(static_answer(), 42);
}

TEST(Dynamic, ProperlyLinked) {
EXPECT_EQ(dynamic_answer(), 24);
}
33 changes: 33 additions & 0 deletions examples/rule_based_toolchain/static_answer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2024 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.

load("@rules_cc//cc:defs.bzl", "cc_library")

licenses(["notice"])

cc_library(
name = "answer",
srcs = ["static_answer.cc"],
hdrs = ["public/static_answer.h"],
includes = ["public"],
linkstatic = True,
visibility = ["//visibility:private"],
)

# TODO: This should be a cc_static_library when that's supported.
alias(
name = "static_answer",
actual = ":answer",
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 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.

#ifndef STATIC_ANSWER_PUBLIC_STATIC_ANSWER_H_
#define STATIC_ANSWER_PUBLIC_STATIC_ANSWER_H_

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int static_answer(void);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#endif // STATIC_ANSWER_PUBLIC_STATIC_ANSWER_H_
19 changes: 19 additions & 0 deletions examples/rule_based_toolchain/static_answer/static_answer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 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.

#include "static_answer.h"

extern "C" int static_answer() {
return 42;
}
Loading