Skip to content

Commit b3984ea

Browse files
committed
refactor all
1 parent 8816b64 commit b3984ea

File tree

20 files changed

+2306
-268
lines changed

20 files changed

+2306
-268
lines changed

.bazelrc

Lines changed: 132 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,146 @@
1-
# build
2-
# ld: warning: -undefined dynamic_lookup may not work with chained fixups
3-
# temporary solution: https://github.com/bazelbuild/bazel/issues/16413
4-
# build --linkopt=-Wl,-no_fixup_chains
5-
build --platforms=//config/platforms:macos_arm64_platform
6-
7-
# Gcov config
8-
# build:gcov --copt -fprofile-arcs
9-
# build:gcov --copt -ftest-coverage
10-
1+
####################################
2+
# General Bazel Configuration
3+
####################################
114

12-
###############################
13-
# test #
14-
###############################
5+
common --color=yes
6+
common --curses=yes
157

16-
test --test_verbose_timeout_warnings
17-
test --verbose_failures
18-
test --keep_going
19-
test --test_output=all
8+
####################################
9+
# Default Platform (macOS ARM64)
10+
####################################
2011

21-
###############################
22-
# sanitizer #
23-
###############################
12+
build --platforms=//config/platforms:macos_arm64_platform
2413

25-
build:asan --strip=never
26-
build:asan --copt -fsanitize=address
27-
build:asan --copt -DADDRESS_SANITIZER
28-
build:asan --copt -O1
29-
build:asan --copt -g
30-
build:asan --copt -fno-omit-frame-pointer
31-
build:asan --linkopt -fsanitize=address
14+
####################################
15+
# Compiler & Linker Warnings
16+
####################################
3217

33-
# do not use in production code:
34-
build --config=asan
18+
build --copt=-Wall
19+
build --copt=-Wextra
20+
build --copt=-Wpedantic
21+
build --copt=-Wno-unused-parameter
22+
build --copt=-Werror=implicit-function-declaration
3523

36-
###############################
37-
# Profile #
38-
###############################
24+
####################################
25+
# Cache Configuration
26+
####################################
3927

40-
build --generate_json_trace_profile
28+
build --disk_cache=.cache/bazel_cache
29+
build --workspace_status_command=tools/scripts/workspace_status.sh
4130

42-
###############################
43-
# macOS SDK Version #
44-
###############################
31+
####################################
32+
# Test Configuration
33+
####################################
4534

46-
# build --macos_sdk_version=15.1
35+
test --test_verbose_timeout_warnings
36+
test --verbose_failures
37+
test --keep_going
38+
test --test_output=all
4739

48-
###############################
49-
# Output #
50-
###############################
40+
####################################
41+
# Debug Build (with ASAN/UBSAN)
42+
####################################
43+
44+
build:debug --compilation_mode=dbg
45+
build:debug --strip=never
46+
build:debug --copt=-O0
47+
build:debug --copt=-g
48+
build:debug --copt=-fno-omit-frame-pointer
49+
build:debug --copt=-fsanitize=address
50+
build:debug --copt=-fsanitize=undefined
51+
build:debug --linkopt=-fsanitize=address
52+
build:debug --linkopt=-fsanitize=undefined
53+
build:debug --define=DEBUG=1
54+
build:debug --features=layering_check
55+
build:debug --sandbox_debug
56+
57+
test:debug --config=debug
58+
59+
####################################
60+
# Release Build (Optimized)
61+
####################################
62+
63+
build:opt --compilation_mode=opt
64+
build:opt --strip=always
65+
build:opt --copt=-O3
66+
build:opt --define=NDEBUG=1
67+
build:opt --copt=-march=native
68+
69+
####################################
70+
# Profile/Report Build
71+
####################################
72+
73+
build:profile --compilation_mode=opt
74+
build:profile --copt=-O2
75+
build:profile --copt=-g
76+
build:profile --copt=-ftime-report
77+
build:profile --profile=profile.gz
78+
build:profile --generate_json_trace_profile
79+
80+
####################################
81+
# Benchmark Build
82+
####################################
83+
84+
build:benchmark --compilation_mode=opt
85+
build:benchmark --strip=always
86+
build:benchmark --define=NDEBUG=1
87+
build:benchmark --copt=-O3
88+
build:benchmark --copt=-march=native
89+
90+
test:benchmark --config=benchmark
91+
92+
####################################
93+
# Code Coverage (LLVM Coverage)
94+
####################################
95+
96+
coverage --combined_report=lcov
97+
coverage --instrumentation_filter="^lib/.*"
98+
coverage --experimental_use_llvm_covmap
99+
coverage --copt=-fprofile-instr-generate
100+
coverage --copt=-fcoverage-mapping
101+
coverage --copt=-ffile-prefix-map=$(PWD)=.
102+
coverage --copt=-fdebug-prefix-map=$(PWD)=.
103+
coverage --linkopt=-fprofile-instr-generate
104+
coverage --linkopt=-fcoverage-mapping
105+
coverage --experimental_generate_llvm_lcov
106+
107+
####################################
108+
# Static Analysis (clang-tidy)
109+
####################################
110+
111+
build:tidy --aspects=@bazel_tools//tools/cpp:cc_code_coverage.aspect
112+
build:tidy --features=layering_check
113+
build:tidy --copt=-fdiagnostics-color=always
114+
115+
####################################
116+
# Linux with Homebrew LLVM
117+
####################################
118+
119+
build:linux_llvm --action_env=CC=/opt/homebrew/opt/llvm/bin/clang
120+
build:linux_llvm --action_env=CXX=/opt/homebrew/opt/llvm/bin/clang++
121+
build:linux_llvm --action_env=BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
122+
build:linux_llvm --cxxopt=-stdlib=libc++
123+
build:linux_llvm --linkopt=-fuse-ld=lld
124+
build:linux_llvm --platforms=@platforms//os:linux
125+
build:linux_llvm --cpu=x86_64
126+
127+
test:linux_llvm --config=linux_llvm
128+
129+
####################################
130+
# Heap Dump on OOM
131+
####################################
132+
133+
build --heap_dump_on_oom
134+
135+
####################################
136+
# Query Output Format
137+
####################################
51138

52-
# A more useful default output mode for bazel query, which
53-
# prints "ng_module rule //foo:bar" instead of just "//foo:bar".
54139
query --output=label_kind
55140

56-
###############################
57-
# Build Debug #
58-
###############################
59-
60-
# build --compilation_mode=dbg --spawn_strategy=local
61-
62-
###############################
63-
# Coverage #
64-
###############################
141+
####################################
142+
# Output Filter
143+
####################################
65144

66-
# # Use Clang as the compiler
67-
build --compiler=clang
68-
build --collect_code_coverage
69-
build --copt=-fprofile-instr-generate
70-
build --copt=-fcoverage-mapping
71-
build --linkopt=-fprofile-instr-generate
72-
build --linkopt=-fcoverage-mapping
145+
# Suppresses most of the build output, leaving just the summary line
146+
# build --output_filter=^$ # Uncomment for cleaner output

.envrc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# direnv configuration for automatic environment setup
2+
# Install direnv: https://direnv.net/docs/installation.html
3+
# Usage: direnv allow (after editing this file)
4+
5+
# Load environment from .env if it exists
6+
if [ -f .env ]; then
7+
source .env
8+
fi
9+
10+
# Set default compiler to clang/clang++ from Homebrew
11+
export CC=$(which clang)
12+
export CXX=$(which clang++)
13+
14+
# Add Bazel output to PATH (for easy access to built binaries)
15+
export PATH="$(pwd)/bazel-bin:$PATH"
16+
17+
# Optional: Set Bazel flags for this workspace
18+
# export BAZELFLAGS="--config=debug"
19+
20+
# Show direnv status
21+
echo "✓ C Template environment loaded (direnv)"

BUILD

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
# identify missing license headers "bazel run //:license_test"
2-
license_test(
3-
name = "license_test",
4-
timeout = "short",
5-
ignore = [".dir-locals.el"],
6-
marker = "//:MODULE.bazel",
7-
)
1+
"""Root build file for C Template project."""
2+
3+
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")
84

9-
# installer like make install: bazel run //:matrix_installer -- /your/installation/path
10-
installer(
11-
name = "matrix_installer",
12-
data = [
13-
"//:LICENSE.txt", # if available
14-
"//src:matrix", # the target to be installed
15-
"//src:matrix_header", # must be collected in a filegroup
16-
],
17-
system_integration = False, # set "True" symlink to /usr/local/lib and /usr/local/include
5+
# Refresh compile_commands.json for IDE integration
6+
# Usage: bazel run //:refresh_compile_commands
7+
refresh_compile_commands(
8+
name = "refresh_compile_commands",
9+
targets = {
10+
"//lib/...": "",
11+
"//app/...": "",
12+
"//tests/...": "",
13+
},
1814
)

MODULE.bazel

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,46 @@ For more details, please check https://github.com/bazelbuild/bazel/issues/18958
1313

1414
module(
1515
name = "template",
16-
version = "0.0.2",
16+
version = "0.1.0",
1717
)
1818

19+
######################################################################
20+
# Core Build Tools & Platforms
21+
######################################################################
22+
1923
bazel_dep(name = "platforms", version = "0.0.11")
20-
bazel_dep(name = "rules_cc", version = "0.1.1")
24+
bazel_dep(name = "rules_cc", version = "0.1.2")
2125
bazel_dep(name = "bazel_skylib", version = "1.7.1")
2226
bazel_dep(name = "cgrindel_bazel_starlib", version = "0.25.0")
2327
bazel_dep(name = "rules_shell", version = "0.4.0")
24-
2528
bazel_dep(name = "depend_on_what_you_use", version = "0.8.0")
2629

30+
######################################################################
31+
# LLVM Toolchain (for consistent C/C++ across macOS & Linux)
32+
######################################################################
33+
34+
bazel_dep(name = "toolchains_llvm", version = "1.6.0")
35+
36+
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
37+
llvm.toolchain(llvm_version = "20.1.0")
38+
use_repo(llvm, "llvm_toolchain")
39+
use_repo(llvm, "llvm_toolchain_llvm")
40+
41+
register_toolchains("@llvm_toolchain//:all")
42+
43+
######################################################################
44+
# Apple Support (for macOS/iOS)
45+
######################################################################
46+
47+
bazel_dep(name = "apple_support", version = "1.23.1")
48+
49+
######################################################################
50+
# Testing & Development Tools
51+
######################################################################
52+
53+
bazel_dep(name = "google_benchmark", version = "1.9.2")
54+
bazel_dep(name = "rules_foreign_cc", version = "0.14.0")
55+
2756
#############################
2857
# Hedron's Compile Commands Extractor for Bazel
2958
#############################
@@ -33,8 +62,8 @@ bazel_dep(name = "depend_on_what_you_use", version = "0.8.0")
3362
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
3463
git_override(
3564
module_name = "hedron_compile_commands",
36-
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
3765
commit = "4f28899228fb3ad0126897876f147ca15026151e",
66+
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
3867
)
3968

4069
#############################
@@ -44,15 +73,16 @@ git_override(
4473
bazel_dep(name = "phst_license_test", version = "0", dev_dependency = True)
4574
git_override(
4675
module_name = "phst_license_test",
47-
tag = "0.0.1",
4876
remote = "https://github.com/phst/license_test.git",
77+
tag = "0.0.1",
4978
)
5079

51-
#############################
52-
# Load external non-Bazel libraries
53-
#############################
80+
######################################################################
81+
# Load external non-Bazel libraries (Unity, dbg-macro, etc)
82+
######################################################################
5483

5584
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
85+
5686
git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
5787

5888
# unity - A simple unit testing library for C.
@@ -69,7 +99,7 @@ http_archive(
6999
# dbg-macro: A simple debug macro for C.
70100
git_repository(
71101
name = "dbg-macro",
102+
build_file = "@//:third_party/dbg-macro.BUILD",
72103
commit = "b949858d9dff4886e68f1049270aad1b3fa7ab81",
73104
remote = "https://github.com/eerimoq/dbg-macro.git",
74-
build_file = "@//:third_party/dbg-macro.BUILD",
75105
)

0 commit comments

Comments
 (0)