-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from armandomontanez:rules-based-toolchain-ex…
…ample PiperOrigin-RevId: 684024413 Change-Id: I36767bb7452d83749a48342ff447f792979da576
- Loading branch information
Showing
21 changed files
with
778 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
examples/rule_based_toolchain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/bazel-* | ||
bazel-* | ||
MODULE.bazel.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
examples/rule_based_toolchain/dynamic_answer/dynamic_answer.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/rule_based_toolchain/dynamic_answer/public/dynamic_answer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
28 changes: 28 additions & 0 deletions
28
examples/rule_based_toolchain/static_answer/public/static_answer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
examples/rule_based_toolchain/static_answer/static_answer.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.