Skip to content

refactor: migrate to rules based toolchain #497

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module(
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "rules_cc", version = "0.0.17")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "platforms", version = "0.0.8")

# TODO: Remove when protobuf is released with a version of rules_python that supports 8.x
Expand Down
5 changes: 5 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ cc_library(
hdrs = ["stdlib.h"],
)

cc_binary(
name = "hello",
srcs = ["main.c"]
)

# We want to emulate the behavior of cc_binary but be able to run the target as
# a test, so we use a cc_test target with linkstatic.
cc_test(
Expand Down
6 changes: 5 additions & 1 deletion tests/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ local_path_override(

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_cc", version = "0.1.1")
local_path_override(
module_name = "rules_cc",
path = "../../rules_cc"
)
bazel_dep(name = "rules_go", version = "0.50.1", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_rust", version = "0.54.1")
bazel_dep(name = "rules_foreign_cc", version = "0.13.0")
Expand Down
4 changes: 4 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
main:
gcc main.c -lmagic -o main \
-L $(shell brew --prefix)/lib -I $(shell brew --prefix)/include

Binary file added tests/main
Binary file not shown.
40 changes: 40 additions & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <magic.h>

int main(int argc, char *argv[]) {
// Check if filename is provided
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}

// Initialize magic handle
magic_t magic = magic_open(MAGIC_MIME_TYPE);
if (magic == NULL) {
fprintf(stderr, "Failed to initialize libmagic\n");
return 1;
}

// Load magic database
if (magic_load(magic, NULL) != 0) {
fprintf(stderr, "Cannot load magic database: %s\n", magic_error(magic));
magic_close(magic);
return 1;
}

// Get MIME type
const char *mime_type = magic_file(magic, argv[1]);
if (mime_type == NULL) {
fprintf(stderr, "Error determining MIME type: %s\n", magic_error(magic));
magic_close(magic);
return 1;
}

// Print result
printf("MIME type: %s\n", mime_type);

// Cleanup
magic_close(magic);
return 0;
}
48 changes: 48 additions & 0 deletions toolchain/BUILD.llvm_repo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,54 @@

package(default_visibility = ["//visibility:public"])

load("@bazel_skylib//rules/directory:directory.bzl", "directory")
load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory")

# Directory-based rules in this toolchain only referece things in
# lib/ or include/ subdirectories.
directory(
name = "toolchain_root",
srcs = glob([
"lib/**",
"lib64/**",
"include/**",
], allow_empty = True),
)

subdirectory(
name = "include-c++-v1",
parent = ":toolchain_root",
path = "include/c++/v1",
)

# subdirectory(
# name = "include-c++-v1-arch",
# parent = ":toolchain_root",
# # TODO: fix this.
# path = "include/aarch64-apple-macosx/c++/v1",
# visibility = ["//visibility:public"]
# )

subdirectory(
name = "lib-clang-include",
parent = ":toolchain_root",
# TODO
# "lib/clang/{llvm_version}/include",
# "lib64/clang/{major_llvm_version}/include",
# "lib64/clang/{llvm_version}/include"
path = "lib/clang/{major_llvm_version}/include",
visibility = ["//visibility:public"]
)

subdirectory(
name = "lib-clang-share",
parent = ":toolchain_root",
# TODO:
# "lib/clang/{llvm_version}/share"
path = "lib/clang/{major_llvm_version}/share",
visibility = ["//visibility:public"]
)

# Some targets may need to directly depend on these files.
exports_files(glob(
[
Expand Down
44 changes: 42 additions & 2 deletions toolchain/BUILD.toolchain.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,49 @@
package(default_visibility = ["//visibility:public"])

load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
load("@toolchains_llvm//toolchain/internal:system_module_map.bzl", "system_module_map")
load("%{cc_toolchain_config_bzl}", "cc_toolchain_config")
load("@toolchains_llvm//toolchain/internal:host_sysroot_directory.bzl", "host_sysroot_directory")
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
load("@rules_cc//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint")
load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
load("@rules_cc//cc/toolchains/args:sysroot.bzl", "cc_sysroot")


cc_feature_constraint(
name = "constraint_opt",
all_of = ["@rules_cc//cc/toolchains/features:opt"],
)

cc_feature_constraint(
name = "constraint_dbg",
all_of = ["@rules_cc//cc/toolchains/features:dbg"],
)

cc_feature_constraint(
name = "constraint_fastbuild",
all_of = ["@rules_cc//cc/toolchains/features:fastbuild"],
)

# TODO: what's the non-legacy way of this doing this?
cc_feature_constraint(
name = "constraint_unfiltered_compile_flags",
all_of = ["@rules_cc//cc/toolchains/features/legacy:unfiltered_compile_flags"]
)


# Do not resolve our symlinked resource prefixes to real paths.
cc_args(
name = "no_absolute_paths_for_builtins",
actions = [
"@rules_cc//cc/toolchains/actions:compile_actions",
"@rules_cc//cc/toolchains/actions:ar_actions",
],
args = ["-no-canonical-prefixes"],
visibility = ["//visibility:public"],
)

# Following filegroup targets are used when not using absolute paths and shared
# between different toolchains.
Expand Down
6 changes: 3 additions & 3 deletions toolchain/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def bazel_toolchain_dependencies():
if not native.existing_rule("rules_cc"):
http_archive(
name = "rules_cc",
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.17/rules_cc-0.0.17.tar.gz"],
sha256 = "abc605dd850f813bb37004b77db20106a19311a96b2da1c92b789da529d28fe1",
strip_prefix = "rules_cc-0.0.17",
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.1.1/rules_cc-0.1.1.tar.gz"],
sha256 = "712d77868b3152dd618c4d64faaddefcc5965f90f5de6e6dd1d5ddcd0be82d42",
strip_prefix = "rules_cc-0.1.1",
)

# Load bazel_skylib if the user has not defined them.
Expand Down
28 changes: 23 additions & 5 deletions toolchain/extensions/llvm.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""LLVM extension for use with bzlmod"""

load("@toolchains_llvm//toolchain:rules.bzl", "llvm_toolchain")
load("//toolchain:toolchain.bzl", _toolchain = "toolchain")
load("//toolchain:llvm.bzl", _llvm = "llvm")
load(
"@toolchains_llvm//toolchain/internal:repo.bzl",
"//toolchain/internal:repo.bzl",
_common_attrs = "common_attrs",
_llvm_config_attrs = "llvm_config_attrs",
_llvm_repo_attrs = "llvm_repo_attrs",
)
Expand Down Expand Up @@ -72,9 +74,25 @@ def _llvm_impl_(module_ctx):
name,
)

llvm_toolchain(
**attrs
)
if not attrs.get("toolchain_roots"):
llvm_args = {
k: v
for k, v in attrs.items()
if (k not in _llvm_config_attrs.keys()) or (k in _common_attrs.keys())
}
llvm_args["name"] = name + "_llvm"
_llvm(**llvm_args)

if not attrs.get("llvm_versions"):
attrs.update(llvm_versions = {"": attrs.get("llvm_version")})

toolchain_args = {
k: v
for k, v in attrs.items()
if (k not in _llvm_repo_attrs.keys()) or (k in _common_attrs.keys())
}
_toolchain(**toolchain_args)


# Check that every defined toolchain_root or sysroot has a corresponding toolchain.
for root in mod.tags.toolchain_root:
Expand Down
Loading