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

Add LLVM/Clang #20

Merged
merged 15 commits into from
Mar 25, 2024
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
12 changes: 11 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# To debug bazel options, uncomment next line.
# build --announce_rc

build --noenable_bzlmod

# Define the Apple OS
build --apple_platform_type=macos

# Allow platform specific control and add the missing 'CoreFoundation' lib.
build --enable_platform_specific_config
build:macos --features=-supports_dynamic_linker --linkopt=-framework --linkopt=CoreFoundation --host_linkopt=-framework --host_linkopt=CoreFoundation --linkopt=-Wl,-undefined,error
build:macos --features=-supports_dynamic_linker --linkopt=-framework --linkopt=CoreFoundation --host_linkopt=-framework --host_linkopt=CoreFoundation
# --linkopt=-Wl,-undefined,error

# Define absl=1 to tell googletest to use absl for backtrace.
build --define absl=1
Expand All @@ -28,3 +31,10 @@ build:asan --build_tag_filters=-no_san # skip san targets
build:asan --test_tag_filters=-no_san # skip san targets
build:asan --define san=true # skip san targets
build:asan --features=asan # https://github.com/bazelbuild/bazel/issues/6932

build --noincompatible_enable_cc_toolchain_resolution

build:clang --incompatible_enable_cc_toolchain_resolution
build:clang --//mbo/mope:clang_format=
build:clang --cxxopt=-gmlt
build:clang --host_cxxopt=-gmlt
15 changes: 7 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ jobs:
~/.cache/bazel
key: ${{ runner.os }}-{{ env.version }}-bazel-cache

- name: Install LLVM and Clang
uses: KyleMayes/install-llvm-action@v1
with:
version: "17.0"
- name: Test Clang/ASAN
run: |
bazel test --config=clang --config=asan -c dbg //...

- name: Test ASAN
- name: Test Clang/OPT
run: |
bazel test --//mbo/mope:clang_format="${LLVM_PATH}/bin/clang-format" --config=asan -c dbg //...
bazel test --config=clang -c opt //...

- name: Test OPT
- name: Test Fastbuild
run: |
bazel test --//mbo/mope:clang_format="${LLVM_PATH}/bin/clang-format" -c opt //...
CC=gcc bazel test -c fastbuild //...
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.3

* Add llvm/clang which can be triggered with `bazel ... --config=clang`.

# 0.2.22

* Change the way `mbo::types::Extend` types are constructed to support more complex and deeper nested types.
Expand Down
63 changes: 48 additions & 15 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,63 @@ load(":workspace.bzl", "mbo_workspace_load_modules")

mbo_workspace_load_modules()

#load("@com_grail_bazel_toolchain//toolchain:deps.bzl", "bazel_toolchain_dependencies")
###########################################################################

#bazel_toolchain_dependencies()
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

#load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "llvm_toolchain")
bazel_skylib_workspace()

#llvm_toolchain(
# name = "llvm_toolchain",
# llvm_versions = {
# "": "15.0.6",
# "darwin-aarch64": "15.0.7",
# "darwin-x86_64": "15.0.7",
# },
#)
###########################################################################

#load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains")
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

#llvm_register_toolchains()
rules_foreign_cc_dependencies()

#load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
###########################################################################

#bazel_skylib_workspace()
load("@toolchains_llvm//toolchain:deps.bzl", "bazel_toolchain_dependencies")

bazel_toolchain_dependencies()

load("@toolchains_llvm//toolchain:rules.bzl", "llvm_toolchain")

llvm_toolchain(
name = "llvm_toolchain",
llvm_versions = {
"linux-aarch64": "17.0.6",
"linux-x86_64": "17.0.6",
"darwin-aarch64": "17.0.6",
"darwin-x86_64": "15.0.7", # For Intel based macs.
},
sha256 = {
"linux-aarch64": "6dd62762285326f223f40b8e4f2864b5c372de3f7de0731cb7cd55ca5287b75a",
"limux-x86_64": "884ee67d647d77e58740c1e645649e29ae9e8a6fe87c1376be0f3a30f3cc9ab3",
"darwin-aarch64": "1264eb3c2a4a6d5e9354c3e5dc5cb6c6481e678f6456f36d2e0e566e9400fcad",
"darwin-x86_64": "d16b6d536364c5bec6583d12dd7e6cf841b9f508c4430d9ee886726bd9983f1c",
},
strip_prefix = {
"linux-aarch64": "clang+llvm-17.0.6-aarch64-linux-gnu",
"linux-x86_64": "clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04",
"darwin-aarch64": "clang+llvm-17.0.6-arm64-apple-darwin22.0",
"darwin-x86_64": "clang+llvm-15.0.7-x86_64-apple-darwin21.0",
},
urls = {
"linux-aarch64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.6/clang+llvm-17.0.6-aarch64-linux-gnu.tar.xz"],
"linux-x86_64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.6/clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz"],
"darwin-aarch64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.6/clang+llvm-17.0.6-arm64-apple-darwin22.0.tar.xz"],
"darwin-x86_64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.7/clang+llvm-15.0.7-x86_64-apple-darwin21.0.tar.xz"],
},
)

load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains")

llvm_register_toolchains()

###########################################################################

# Run: ./compile_commands-update.sh
load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()

###########################################################################
19 changes: 12 additions & 7 deletions mbo/mope/mope.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("//mbo/diff:diff.bzl", "diff_test")

# The `clang-format` tool is selected as follows:
# 1) If custom bazel flag `--mbo/mope:clang_format` is set, then that value will be used.
# 1) If custom bazel flag `--//mbo/mope:clang_format` is set, then that value will be used.
# 2) If the custom flag is empty and this variable is a non empty string, then use its value.
# Otherwise use `@llvm_toolchain_llvm//:bin/clang-format`.
# 3) If the resulting value is `clang-format-auto`, then the rule tries to find the tool:
# 3) If the resulting value is `clang-format-auto` (or the above result is not found or not
# executable), then the rule tries to find the tool:
# a) `${LLVM_PATH}/bin/clang-format`
# b) `$(which "clang_format")`
# c) `clang-format-18` ... `clang-format-14`
# c) `clang-format-19` ... `clang-format-14`
# d) `clang-format` will lastly be picked as a fallback.
CLANG_FORMAT_BINARY = "clang-format-auto"
CLANG_FORMAT_BINARY = "" # Ignore clang-format from repo with: "clang-format-auto"

def _get_clang_format(ctx):
"""Get the selected clang-format from `--//mbo/mope:clang_format` bazel flag."""
Expand Down Expand Up @@ -63,11 +64,15 @@ def _clang_format_impl(ctx, src, dst):
tools = clang_format_tool,
command = """
CLANG_FORMAT="{clang_format}"
if [ "{clang_format}" == "clang-format-auto" ]; then
if [ -x "${{LLVM_PATH}}/bin/clang-format" ]; then
if [ "{clang_format}" == "clang-format-auto" ] || [ -x "${{CLANG_FORMAT}}" ]; then
if [ -x "external/llvm_toolchain_llvm/bin/clang-format" ]; then
CLANG_FORMAT="external/llvm_toolchain_llvm/bin/clang-format"
elif [ -x "${{LLVM_PATH}}/bin/clang-format" ]; then
CLANG_FORMAT="${{LLVM_PATH}}/bin/clang-format"
elif [ $(which "clang_format") ]; then
CLANG_FORMAT="clang_format"
elif [ $(which "clang-format-19") ]; then
CLANG_FORMAT="clang-format-19"
elif [ $(which "clang-format-18") ]; then
CLANG_FORMAT="clang-format-18"
elif [ $(which "clang-format-17") ]; then
Expand All @@ -89,7 +94,7 @@ def _clang_format_impl(ctx, src, dst):
--sort-includes={sort_includes} \\
--style=file:{clang_config} \\
--Werror \\
< {src} > {dst}
< {src} > {dst} || (echo "CLANG($("${{CLANG_FORMAT}}" --version)) = '${{CLANG_FORMAT}}'" ; false)
""".format(
assume_filename = dst.short_path.removesuffix(".gen"),
clang_format = clang_format,
Expand Down
3 changes: 2 additions & 1 deletion mbo/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ cc_library(
"//mbo/status:status_macros_cc",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
],
visibility = ["//visibility:public"],
Expand All @@ -61,4 +62,4 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
size = "small",
)
)
13 changes: 7 additions & 6 deletions mbo/testing/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mbo/status/status_macros.h"
Expand Down Expand Up @@ -255,18 +256,18 @@ ::testing::PolymorphicMatcher<testing_internal::StatusIsMatcher> StatusIs(
#define MBO_EXPECT_OK(expression) EXPECT_THAT(expression, mbo::testing::IsOk())

#ifndef EXPECT_OK
#define EXPECT_OK(expr) MBO_EXPECT_OK(expr)
# define EXPECT_OK(expr) MBO_EXPECT_OK(expr)
#elif __cplusplus >= 202'302L
#warning "EXPECT_OK already defined"
# warning "EXPECT_OK already defined"
#endif

#undef MBO_ASSERT_OK
#define MBO_ASSERT_OK(expression) ASSERT_THAT(expression, mbo::testing::IsOk())

#ifndef ASSERT_OK
#define ASSERT_OK(expr) MBO_ASSERT_OK(expr)
# define ASSERT_OK(expr) MBO_ASSERT_OK(expr)
#elif __cplusplus >= 202'302L
#warning "ASSERT_OK already defined"
# warning "ASSERT_OK already defined"
#endif

#undef MBO_ASSERT_OK_AND_ASSIGN_IMPL_
Expand All @@ -281,9 +282,9 @@ ::testing::PolymorphicMatcher<testing_internal::StatusIsMatcher> StatusIs(
MBO_ASSERT_OK_AND_ASSIGN_IMPL_(MBO_STATUS_MACROS_IMPL_CONCAT_(_status_or_value, __LINE__), lhs, rexpr)

#ifndef ASSERT_OK_AND_ASSIGN
#define ASSERT_OK_AND_ASSIGN(lhs, rexpr) MBO_ASSERT_OK_AND_ASSIGN(lhs, rexpr)
# define ASSERT_OK_AND_ASSIGN(lhs, rexpr) MBO_ASSERT_OK_AND_ASSIGN(lhs, rexpr)
#elif __cplusplus >= 202'302L
#warning "ASSERT_OK_AND_ASSIGN already defined"
# warning "ASSERT_OK_AND_ASSIGN already defined"
#endif

// NOLINTEND(bugprone-macro-parentheses)
Expand Down
4 changes: 2 additions & 2 deletions mbo/types/extend_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ TEST_F(ExtendTest, StreamableComplexFields) {
R"({25, {{"Hugo", "Meyer"}, 42}, *{{"bar", "foo"}}})"));
#ifdef __clang__
std::cout << "Person:\n";
EXPECT_THAT(debug::Print(&person, HasFailure()), 621);
EXPECT_THAT(debug::Print(&person, HasFailure()), Le(621));
std::cout << "Person::person.name:\n";
EXPECT_THAT(debug::Print(&person.person.name, HasFailure()), 603);
EXPECT_THAT(debug::Print(&person.person.name, HasFailure()), Le(603));
#endif // __clang__
}

Expand Down
51 changes: 29 additions & 22 deletions workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ def mbo_workspace_load_modules():
sha256 = "4531deccb913639c30e5c7512a054d5d875698daeb75d8cf90f284375fe7c360",
)

# Load and configure a LLVM based C/C++ toolchain.
#github_archive(
# name = "com_grail_bazel_toolchain",
# repo = "https://github.com/grailbio/bazel-toolchain",
# commit = "41ff2a05c0beff439bad7acfd564e6827e34b13b",
# sha256 = "52b09a61b9a03f4e0994402243a03018a858da6a5774de898f99e344520e9a25",
#)

http_archive(
name = "bazel_skylib",
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
Expand All @@ -56,6 +48,13 @@ def mbo_workspace_load_modules():
],
)

http_archive(
name = "rules_foreign_cc",
sha256 = "476303bd0f1b04cc311fc258f1708a5f6ef82d3091e53fd1977fa20383425a6a",
strip_prefix = "rules_foreign_cc-0.10.1",
url = "https://github.com/bazelbuild/rules_foreign_cc/releases/download/0.10.1/rules_foreign_cc-0.10.1.tar.gz",
)

# Used for absl/GoogleTest
# Note GoogleTest uses "com_googlesource_code_re2" rather than "com_google_re2"
if not native.existing_rule("com_googlesource_code_re2"):
Expand All @@ -66,22 +65,22 @@ def mbo_workspace_load_modules():
sha256 = "4ccdd5aafaa1bcc24181e6dd3581c3eee0354734bb9f3cb4306273ffa434b94f",
)

# Abseil, LTS 20230125
# Abseil, LTS
# Used for GoogleTest through .bazelrc "build --define absl=1"
github_archive(
name = "com_google_absl",
repo = "https://github.com/abseil/abseil-cpp",
tag = "20230125.3",
sha256 = "5366d7e7fa7ba0d915014d387b66d0d002c03236448e1ba9ef98122c13b35c36",
tag = "20240116.1",
sha256 = "3c743204df78366ad2eaf236d6631d83f6bc928d1705dd0000b872e53b73dc6a",
)

# GoogleTest
github_archive(
name = "com_google_googletest",
repo = "https://github.com/google/googletest",
strip_prefix = "googletest-1.13.0",
tag = "v1.13.0",
sha256 = "ad7fdba11ea011c1d925b3289cf4af2c66a352e18d4c7264392fead75e919363",
strip_prefix = "googletest-1.14.0",
sha256 = "8ad598c73ad796e0d8280b082cebd82a630d73e73cd3c70057938a6501bba5d7",
tag = "v1.14.0",
)

github_archive(
Expand All @@ -92,16 +91,24 @@ def mbo_workspace_load_modules():
tag = "v1.8.2",
)

# hedron_compile_commands
github_archive(
name = "hedron_compile_commands",
commit = "6f63be6e2ccfdb6a1f248abbb3614107106de4a9",
commit = "daae6f40adfa5fdb7c89684cbe4d88b691c63b2d",
repo = "https://github.com/helly25/bazel-compile-commands-extractor",
sha256 = "22aa86db4c1d7c9b417f19b9a4477017d505df58eaed024e68c3452bd1a26b74",
sha256 = "43451a32bf271e7ba4635a07f7996d535501f066c0fe8feab04fb0c91dd5986e",
)

# Cannot yet support toolchains_llvm 1.0.0. It enables C++20 modules in a broken way.
github_archive(
name = "toolchains_llvm",
commit = "f9666fff585fdb53ea97ccec07ef1bc5d4aa5098",
repo = "https://github.com/bazel-contrib/toolchains_llvm",
integrity = "sha256-avzZXfOiNvw7oTvEJbmyny9Tz97qEoayYDvibYYJJW0=",
)
#github_archive(
# name = "hedron_compile_commands",
# repo = "https://github.com/hedronvision/bazel-compile-commands-extractor",
# commit = "ed994039a951b736091776d677f324b3903ef939",
# sha256 = "085bde6c5212c8c1603595341ffe7133108034808d8c819f8978b2b303afc9e7",
#http_archive(
# name = "toolchains_llvm",
# sha256 = "",
# strip_prefix = "toolchains_llvm-1.0.0",
# canonical_id = "1.0.0",
# url = "https://github.com/bazel-contrib/toolchains_llvm/releases/download/1.0.0/toolchains_llvm-1.0.0.tar.gz",
#)
Loading