Skip to content

Commit 4248bd4

Browse files
committed
wip
1 parent 4c3d6cf commit 4248bd4

18 files changed

+913
-203
lines changed

.DS_Store

6 KB
Binary file not shown.

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module(
2020
)
2121

2222
bazel_dep(name = "bazel_skylib", version = "1.5.0")
23-
bazel_dep(name = "rules_cc", version = "0.0.17")
23+
bazel_dep(name = "rules_cc", version = "0.1.1")
2424
bazel_dep(name = "platforms", version = "0.0.8")
2525

2626
# TODO: Remove when protobuf is released with a version of rules_python that supports 8.x

tests/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ cc_library(
2222
hdrs = ["stdlib.h"],
2323
)
2424

25+
cc_binary(
26+
name = "hello",
27+
srcs = ["main.c"]
28+
)
29+
2530
# We want to emulate the behavior of cc_binary but be able to run the target as
2631
# a test, so we use a cc_test target with linkstatic.
2732
cc_test(

tests/MODULE.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ local_path_override(
2222

2323
bazel_dep(name = "bazel_skylib", version = "1.7.1")
2424
bazel_dep(name = "platforms", version = "0.0.11")
25-
bazel_dep(name = "rules_cc", version = "0.0.9")
25+
bazel_dep(name = "rules_cc", version = "0.1.1")
26+
local_path_override(
27+
module_name = "rules_cc",
28+
path = "../../rules_cc"
29+
)
2630
bazel_dep(name = "rules_go", version = "0.50.1", repo_name = "io_bazel_rules_go")
2731
bazel_dep(name = "rules_rust", version = "0.54.1")
2832
bazel_dep(name = "rules_foreign_cc", version = "0.13.0")

tests/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
main:
2+
gcc main.c -lmagic -o main \
3+
-L $(shell brew --prefix)/lib -I $(shell brew --prefix)/include
4+

tests/main

33 KB
Binary file not shown.

tests/main.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <magic.h>
4+
5+
int main(int argc, char *argv[]) {
6+
// Check if filename is provided
7+
if (argc != 2) {
8+
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
9+
return 1;
10+
}
11+
12+
// Initialize magic handle
13+
magic_t magic = magic_open(MAGIC_MIME_TYPE);
14+
if (magic == NULL) {
15+
fprintf(stderr, "Failed to initialize libmagic\n");
16+
return 1;
17+
}
18+
19+
// Load magic database
20+
if (magic_load(magic, NULL) != 0) {
21+
fprintf(stderr, "Cannot load magic database: %s\n", magic_error(magic));
22+
magic_close(magic);
23+
return 1;
24+
}
25+
26+
// Get MIME type
27+
const char *mime_type = magic_file(magic, argv[1]);
28+
if (mime_type == NULL) {
29+
fprintf(stderr, "Error determining MIME type: %s\n", magic_error(magic));
30+
magic_close(magic);
31+
return 1;
32+
}
33+
34+
// Print result
35+
printf("MIME type: %s\n", mime_type);
36+
37+
// Cleanup
38+
magic_close(magic);
39+
return 0;
40+
}

toolchain/BUILD.llvm_repo

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

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

17+
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
18+
load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory")
19+
20+
# Directory-based rules in this toolchain only referece things in
21+
# lib/ or include/ subdirectories.
22+
directory(
23+
name = "toolchain_root",
24+
srcs = glob([
25+
"lib/**",
26+
"lib64/**",
27+
"include/**",
28+
], allow_empty = True),
29+
)
30+
31+
subdirectory(
32+
name = "include-c++-v1",
33+
parent = ":toolchain_root",
34+
path = "include/c++/v1",
35+
)
36+
37+
# subdirectory(
38+
# name = "include-c++-v1-arch",
39+
# parent = ":toolchain_root",
40+
# # TODO: fix this.
41+
# path = "include/aarch64-apple-macosx/c++/v1",
42+
# visibility = ["//visibility:public"]
43+
# )
44+
45+
subdirectory(
46+
name = "lib-clang-include",
47+
parent = ":toolchain_root",
48+
# TODO
49+
# "lib/clang/{llvm_version}/include",
50+
# "lib64/clang/{major_llvm_version}/include",
51+
# "lib64/clang/{llvm_version}/include"
52+
path = "lib/clang/{major_llvm_version}/include",
53+
visibility = ["//visibility:public"]
54+
)
55+
56+
subdirectory(
57+
name = "lib-clang-share",
58+
parent = ":toolchain_root",
59+
# TODO:
60+
# "lib/clang/{llvm_version}/share"
61+
path = "lib/clang/{major_llvm_version}/share",
62+
visibility = ["//visibility:public"]
63+
)
64+
1765
# Some targets may need to directly depend on these files.
1866
exports_files(glob(
1967
[

toolchain/BUILD.toolchain.tpl

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,49 @@
1515
package(default_visibility = ["//visibility:public"])
1616

1717
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
18-
load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
1918
load("@toolchains_llvm//toolchain/internal:system_module_map.bzl", "system_module_map")
20-
load("%{cc_toolchain_config_bzl}", "cc_toolchain_config")
19+
load("@toolchains_llvm//toolchain/internal:host_sysroot_directory.bzl", "host_sysroot_directory")
20+
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
21+
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
22+
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")
23+
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
24+
load("@rules_cc//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint")
25+
load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
26+
load("@rules_cc//cc/toolchains/args:sysroot.bzl", "cc_sysroot")
27+
28+
29+
cc_feature_constraint(
30+
name = "constraint_opt",
31+
all_of = ["@rules_cc//cc/toolchains/features:opt"],
32+
)
33+
34+
cc_feature_constraint(
35+
name = "constraint_dbg",
36+
all_of = ["@rules_cc//cc/toolchains/features:dbg"],
37+
)
38+
39+
cc_feature_constraint(
40+
name = "constraint_fastbuild",
41+
all_of = ["@rules_cc//cc/toolchains/features:fastbuild"],
42+
)
43+
44+
# TODO: what's the non-legacy way of this doing this?
45+
cc_feature_constraint(
46+
name = "constraint_unfiltered_compile_flags",
47+
all_of = ["@rules_cc//cc/toolchains/features/legacy:unfiltered_compile_flags"]
48+
)
49+
50+
51+
# Do not resolve our symlinked resource prefixes to real paths.
52+
cc_args(
53+
name = "no_absolute_paths_for_builtins",
54+
actions = [
55+
"@rules_cc//cc/toolchains/actions:compile_actions",
56+
"@rules_cc//cc/toolchains/actions:ar_actions",
57+
],
58+
args = ["-no-canonical-prefixes"],
59+
visibility = ["//visibility:public"],
60+
)
2161

2262
# Following filegroup targets are used when not using absolute paths and shared
2363
# between different toolchains.

toolchain/deps.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def bazel_toolchain_dependencies():
1919
if not native.existing_rule("rules_cc"):
2020
http_archive(
2121
name = "rules_cc",
22-
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.17/rules_cc-0.0.17.tar.gz"],
23-
sha256 = "abc605dd850f813bb37004b77db20106a19311a96b2da1c92b789da529d28fe1",
24-
strip_prefix = "rules_cc-0.0.17",
22+
urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.1.1/rules_cc-0.1.1.tar.gz"],
23+
sha256 = "712d77868b3152dd618c4d64faaddefcc5965f90f5de6e6dd1d5ddcd0be82d42",
24+
strip_prefix = "rules_cc-0.1.1",
2525
)
2626

2727
# Load bazel_skylib if the user has not defined them.

toolchain/extensions/llvm.bzl

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""LLVM extension for use with bzlmod"""
22

3-
load("@toolchains_llvm//toolchain:rules.bzl", "llvm_toolchain")
3+
load("//toolchain:toolchain.bzl", _toolchain = "toolchain")
4+
load("//toolchain:llvm.bzl", _llvm = "llvm")
45
load(
5-
"@toolchains_llvm//toolchain/internal:repo.bzl",
6+
"//toolchain/internal:repo.bzl",
7+
_common_attrs = "common_attrs",
68
_llvm_config_attrs = "llvm_config_attrs",
79
_llvm_repo_attrs = "llvm_repo_attrs",
810
)
@@ -72,9 +74,25 @@ def _llvm_impl_(module_ctx):
7274
name,
7375
)
7476

75-
llvm_toolchain(
76-
**attrs
77-
)
77+
if not attrs.get("toolchain_roots"):
78+
llvm_args = {
79+
k: v
80+
for k, v in attrs.items()
81+
if (k not in _llvm_config_attrs.keys()) or (k in _common_attrs.keys())
82+
}
83+
llvm_args["name"] = name + "_llvm"
84+
_llvm(**llvm_args)
85+
86+
if not attrs.get("llvm_versions"):
87+
attrs.update(llvm_versions = {"": attrs.get("llvm_version")})
88+
89+
toolchain_args = {
90+
k: v
91+
for k, v in attrs.items()
92+
if (k not in _llvm_repo_attrs.keys()) or (k in _common_attrs.keys())
93+
}
94+
_toolchain(**toolchain_args)
95+
7896

7997
# Check that every defined toolchain_root or sysroot has a corresponding toolchain.
8098
for root in mod.tags.toolchain_root:

0 commit comments

Comments
 (0)