Skip to content

Commit bd94de4

Browse files
committed
Add support for C99
1 parent cc68892 commit bd94de4

8 files changed

Lines changed: 75 additions & 2 deletions

File tree

‎docs/features.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ enabled by default.
2727
- **`supports_header_path_normalization`** (Linux) — Suppresses absolute-path
2828
warnings for system headers.
2929
- **`dbg`** / **`opt`** (both) — Well-known build-mode markers used to select flags.
30+
- **`gnu11`** (Linux) *opt-in* — Switches C compilation from the default
31+
`-std=c11` to `-std=gnu11`.
32+
- **`c99`** (both) *opt-in* — Switches C compilation from the default
33+
`-std=c11` to `-std=c99`.
3034

3135
## Compilation
3236
- **`unfiltered_compile_flags`** (both) — Redacts `__DATE__`/`__TIME__`/`__TIMESTAMP__`
3337
for reproducible builds.
3438
- **`default_compile_flags`** (both) — Core compile flags plus `dbg`/`opt`
35-
build-mode variants (exact flags differ per target).
39+
build-mode variants (exact flags differ per target). C compilation defaults
40+
to `-std=c11`; request `--features=gnu11` (Linux) or `--features=c99` (both)
41+
to switch the C standard. Only one of `c11`/`gnu11`/`c99` should be active
42+
at a time — requesting `gnu11` and `c99` together applies both `-std=` flags,
43+
with the compiler honoring the last one seen.
3644
- **`pic`** (Linux) — Emits `-fPIC` when the `pic` build variable is available.
3745
QNX declares the `supports_pic` capability marker but does not add `-fPIC` in
3846
the default compile flags.

‎features/custom/linux/make_cc_features.bzl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ _LINUX_FEATURES = [
1717
("@score_bazel_cpp_toolchains//features/native/markers:dbg", False), # Bazel auto-toggles via -c dbg
1818
("@score_bazel_cpp_toolchains//features/native/unfiltered_compile_flags", True),
1919
("@score_bazel_cpp_toolchains//features/native/markers:gnu11", False), # opt-in
20+
("@score_bazel_cpp_toolchains//features/native/markers:c99", False), # opt-in
2021
("@score_bazel_cpp_toolchains//features/native/default_compile_flags", True),
2122
("@score_bazel_cpp_toolchains//features/native/random_seed", True),
2223
("@score_bazel_cpp_toolchains//features/native/include_paths", True),

‎features/custom/qnx/make_cc_features.bzl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ _QNX_FEATURES = [
3838
("@score_bazel_cpp_toolchains//features/native/markers:no_legacy_features", True),
3939
("@score_bazel_cpp_toolchains//features/native/unfiltered_compile_flags", True),
4040
("@score_bazel_cpp_toolchains//features/custom/qnx/gcc_version_flags", True),
41+
("@score_bazel_cpp_toolchains//features/native/markers:c99", False), # opt-in
4142
("@score_bazel_cpp_toolchains//features/native/default_compile_flags", True),
4243
("@score_bazel_cpp_toolchains//features/native/random_seed", True),
4344
("@score_bazel_cpp_toolchains//features/native/include_paths", True),

‎features/native/default_compile_flags/BUILD‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ cc_args(
5353

5454
cc_feature_constraint(
5555
name = "not_gnu11",
56-
none_of = ["//features/native/markers:gnu11"],
56+
none_of = [
57+
"//features/native/markers:gnu11",
58+
"//features/native/markers:c99",
59+
],
5760
)
5861

5962
cc_feature_constraint(
6063
name = "is_gnu11",
6164
all_of = ["//features/native/markers:gnu11"],
6265
)
6366

67+
cc_feature_constraint(
68+
name = "is_c99",
69+
all_of = ["//features/native/markers:c99"],
70+
)
71+
6472
cc_args(
6573
name = "c_std_c11_args",
6674
actions = ["@rules_cc//cc/toolchains/actions:c_compile_actions"],
@@ -75,6 +83,13 @@ cc_args(
7583
requires_any_of = [":is_gnu11"],
7684
)
7785

86+
cc_args(
87+
name = "c_std_c99_args",
88+
actions = ["@rules_cc//cc/toolchains/actions:c_compile_actions"],
89+
args = ["-std=c99"],
90+
requires_any_of = [":is_c99"],
91+
)
92+
7893
cc_args(
7994
name = "default_cxx_compile_flags_args",
8095
actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"],
@@ -152,6 +167,7 @@ cc_feature(
152167
":target_cpu_flags_args",
153168
":c_std_c11_args",
154169
":c_std_gnu11_args",
170+
":c_std_c99_args",
155171
":default_cxx_compile_flags_args",
156172
":dbg_compile_flags_args",
157173
":opt_compile_flags_args",

‎features/native/markers/BUILD‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ cc_feature(
7575
visibility = ["//visibility:public"],
7676
)
7777

78+
cc_feature(
79+
name = "c99",
80+
feature_name = "c99",
81+
visibility = ["//visibility:public"],
82+
)
83+
7884
## QNX only
7985
cc_feature(
8086
name = "dependency_file_named_implicitly",

‎tests/BUILD‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Root test suite definition combining all test categories.
2323
test_suite(
2424
name = "language_and_standards_tests",
2525
tests = [
26+
"//language_and_standards:c99_feature_test",
2627
"//language_and_standards:c_lang_test",
2728
"//language_and_standards:cpp11_test",
2829
"//language_and_standards:cpp14_test",

‎tests/language_and_standards/BUILD‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ cc_test(
3838
copts = ["-std=c99"],
3939
)
4040

41+
# Test for: the `c99` toolchain feature (as opposed to explicit -std= copts)
42+
# Verifies: enabling the `c99` feature switches the compiler onto -std=c99;
43+
# a negative-array-size typedef gated on __STDC_VERSION__ only
44+
# compiles under c99, so it fails to build if the feature didn't
45+
# take effect
46+
cc_test(
47+
name = "c99_feature_test",
48+
srcs = ["c99_feature_test.c"],
49+
features = ["c99"],
50+
)
51+
4152
# ============================================================================
4253
# C++ Standard Version Tests
4354
# ============================================================================
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/********************************************************************************
2+
* Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
// Test for: the `c99` toolchain feature (enabled via the `features` attribute
15+
// below, mirroring `--features=c99`). Unlike the other tests in this package,
16+
// this does not pass `-std=` via copts: it relies entirely on the toolchain
17+
// feature to select the C standard.
18+
//
19+
// GCC accepts C11-only keywords (_Generic, _Noreturn, ...) as an extension
20+
// under -std=c99 too, so they don't actually depend on the active standard
21+
// here. Instead this uses the standard-mandated `__STDC_VERSION__` macro in a
22+
// real constraint: an array with a negative size is an ISO C constraint
23+
// violation, so the typedef below only compiles when the active standard is
24+
// exactly C99.
25+
typedef char standard_must_be_c99[(__STDC_VERSION__ == 199901L) ? 1 : -1];
26+
27+
int main(void) {
28+
return sizeof(standard_must_be_c99) - 1;
29+
}

0 commit comments

Comments
 (0)