From 50e5186099dca9553e83367aea35993aa0bbc102 Mon Sep 17 00:00:00 2001 From: Aymen Jebri Date: Tue, 4 Nov 2025 14:33:17 +0000 Subject: [PATCH 1/3] Add default warning flags to gcc toolchain --- extentions/gcc.bzl | 94 +++++++++++++++++++++++++++++++++++++++++----- test/.bazelversion | 2 +- 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/extentions/gcc.bzl b/extentions/gcc.bzl index 9051096..92b0a75 100644 --- a/extentions/gcc.bzl +++ b/extentions/gcc.bzl @@ -21,9 +21,76 @@ def _gcc_impl(mctx): if not mod.is_root: fail("Only the root module can use the 'gcc' extension") + DEFAULT_FEATURES = [ + "minimal_warnings", + "strict_warnings", + "treat_warnings_as_errors", + ] + + + DEFAULT_WARNING_FLAGS = { + "minimal_warnings": [ + "-Wall", + "-Wextra", + "-Wundef", + "-Wwrite-strings", + "-Wpointer-arith", + "-Wcast-align", + "-Wshadow", + "-Wswitch-bool", + "-Wredundant-decls", + "-Wswitch-enum", + "-Wtype-limits", + "-Wformat", + "-Wformat-security", + "-Wconversion", + "-Wlogical-op", + "-Wreturn-local-addr", + "-Wunused-but-set-variable", + "-Wunused-parameter", + "-Wunused-but-set-parameter", + "-Wunused-variable", + "-Wunused", + "-Wparentheses", + "-Wuninitialized", + "-Wsequence-point", + "-Wsign-compare", + "-Wignored-qualifiers", + "-Wcast-qual", + "-Wreturn-type", + "-Wcomment" + ], + "strict_warnings": [ + "-Wpedantic", + "-Wbad-function-cast", + "-Wstrict-prototypes", + "-Wodr", + "-Wlogical-not-parentheses", + "-Wsizeof-array-argument", + "-Wbool-compare", + "-Winvalid-pch", + "-Wformat=2", + "-Wmissing-format-attribute", + "-Wformat-nonliteral", + "-Wformat-signedness", + "-Wvla", + "-Wuseless-cast", + "-Wdouble-promotion", + "-Wmissing-prototypes", + "-Wreorder", + "-Wnarrowing" + ], + "treat_warnings_as_errors": [] + } + + toolchain_info = None - features = [] - warning_flags = None + features = list(DEFAULT_FEATURES) + warning_flags = { + "minimal_warnings": list(DEFAULT_WARNING_FLAGS["minimal_warnings"]), + "strict_warnings": list(DEFAULT_WARNING_FLAGS["strict_warnings"]), + "treat_warnings_as_errors": list(DEFAULT_WARNING_FLAGS["treat_warnings_as_errors"]), + } for mod in mctx.modules: for tag in mod.tags.toolchain: @@ -34,16 +101,23 @@ def _gcc_impl(mctx): "sha256": tag.sha256, } - for tag in mod.tags.extra_features: - for feature in tag.features: - features.append(feature) + for tag in mod.tags.extra_features: + for feature in tag.features: + if feature not in features: + features.append(feature) for tag in mod.tags.warning_flags: - warning_flags = { - "minimal_warnings":tag.minimal_warnings, - "strict_warnings":tag.strict_warnings, - "treat_warnings_as_errors":tag.treat_warnings_as_errors - } + for flag in tag.minimal_warnings: + if flag not in warning_flags["minimal_warnings"]: + warning_flags["minimal_warnings"].append(flag) + + for flag in tag.strict_warnings: + if flag not in warning_flags["strict_warnings"]: + warning_flags["strict_warnings"].append(flag) + + for flag in tag.treat_warnings_as_errors: + if flag not in warning_flags["treat_warnings_as_errors"]: + warning_flags["treat_warnings_as_errors"].append(flag) if toolchain_info: http_archive( diff --git a/test/.bazelversion b/test/.bazelversion index 0e79152..2bf50aa 100644 --- a/test/.bazelversion +++ b/test/.bazelversion @@ -1 +1 @@ -8.1.1 +8.3.0 From afada50b2d8308700bf399a4cd63d3e350e98f3d Mon Sep 17 00:00:00 2001 From: Aymen Jebri Date: Wed, 5 Nov 2025 09:59:15 +0000 Subject: [PATCH 2/3] Extra features can be disabled by the user using "-" prefix --- extentions/gcc.bzl | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/extentions/gcc.bzl b/extentions/gcc.bzl index 92b0a75..03192f8 100644 --- a/extentions/gcc.bzl +++ b/extentions/gcc.bzl @@ -101,10 +101,18 @@ def _gcc_impl(mctx): "sha256": tag.sha256, } - for tag in mod.tags.extra_features: - for feature in tag.features: - if feature not in features: - features.append(feature) + for tag in mod.tags.extra_features: + for feature in tag.features: + f = feature.strip() + if not f: + continue + if f.startswith("-"): + remove_feature = f[1:].strip() + if remove_feature in features: + features.remove(remove_feature) + else: + if f not in features: + features.append(f) for tag in mod.tags.warning_flags: for flag in tag.minimal_warnings: From 065af0e6fa138c03c67fbf76419eae0f2f9ca628 Mon Sep 17 00:00:00 2001 From: Aymen Jebri Date: Wed, 5 Nov 2025 13:07:43 +0000 Subject: [PATCH 3/3] Update docu: ReadMe + user examples in test folder --- README.md | 27 +++++++++++++++------------ test/MODULE.bazel | 8 ++++---- test/WORKSPACE | 4 ++-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 3693827..606547d 100644 --- a/README.md +++ b/README.md @@ -28,13 +28,16 @@ toolchain_gcc_dependencies() ## Configuring the toolchain to a project needs The `score_toolchains_gcc` module currently supports only GNU GCC version **12.2.0**. Support for multiple GCC versions is planned, with future versions expected to be selectable via an extended toolchain interface. As of now, version 12.2.0 is the sole supported target. -The module exposes an API that allows consumers to enable or disable specific toolchain behaviors related to compilation diagnostics. The following features can be individually configured: +The module exposes an API that allows consumers to enable or disable specific toolchain behaviors related to compilation diagnostics. +By default, the toolchain activates the following features using predefined compiler flags: -1. Minimal warning flags — enables a basic set of compiler warnings -2. Strict warning flags — enables a more aggressive set of warnings (e.g., `-Wall, -Wextra`) +1. Minimal warning flags — enables a basic set of compiler warnings (e.g., `-Wall, -Wextra`) +2. Strict warning flags — enables a more aggressive set of warnings (e.g., `-Wpedantic`) 3. A `treat warnings as errors` flags — promotes all warnings to errors (e.g., `-Werror`) -These features provide fine-grained control over the compiler's warning policy. If no features are explicitly selected, the toolchain will apply no additional warning-related flags by default. +Consumers can disable any of these features by prefixing the feature name with a dash (e.g., `-minimal_warnings`). When a feature is disabled this way, neither the default nor any user-defined flags associated with it will be applied. +Additionally, consumers can adjust the compiler's warning behavior by adding custom flags or disabling specific warnings using the `-Wno-` prefix. +These features provide fine-grained control over the compiler's warning policy. To set wanted flags, the following API needs to be used: ```python @@ -42,18 +45,18 @@ gcc = use_extension("@score_toolchains_gcc//extentions:gcc.bzl", "gcc") gcc.extra_features( features = [ "minimal_warnings", - "treat_warnings_as_errors", + "-treat_warnings_as_errors", ], ) gcc.warning_flags( - minimal_warnings = ["-Wall", "-Wno-error=deprecated-declarations"], - strict_warnings = ["-Wextra", "-Wpedantic"], + minimal_warnings = ["-Wno-error=deprecated-declarations"], + strict_warnings = ["-Wno-bool-compare"], treat_warnings_as_errors = ["-Werror"], ) use_repo(gcc, "gcc_toolchain", "gcc_toolchain_gcc") ``` -* `extra_features` - This will enable all features which are set in the list. -* `warning_flags` - This will set flags for selected features. +* `extra_features` - Enables or disables features listed by the consumer. +* `warning_flags` - Sets compiler flags for the features that are currently enabled. ### Using WORKSPACE file The same approuch needs to be done when configuring toolchain over WORKSPACE file: @@ -64,11 +67,11 @@ gcc_toolchain( gcc_repo = "gcc_toolchain_gcc", extra_features = [ "minimal_warnings", - "treat_warnings_as_errors", + "-treat_warnings_as_errors", ], warning_flags = { - "minimal_warnings": ["-Wall", "-Wno-error=deprecated-declarations"], - "strict_warnings": ["-Wextra", "-Wpedantic"], + "minimal_warnings": ["-Wno-error=deprecated-declarations"], + "strict_warnings": ["-Wno-bool-compare"], "treat_warnings_as_errors": ["-Werror"], }, ) diff --git a/test/MODULE.bazel b/test/MODULE.bazel index 2da31e3..a76dd5d 100644 --- a/test/MODULE.bazel +++ b/test/MODULE.bazel @@ -32,12 +32,12 @@ gcc.toolchain( gcc.extra_features( features = [ "minimal_warnings", - "treat_warnings_as_errors", + "-treat_warnings_as_errors", ], -) +) gcc.warning_flags( - minimal_warnings = ["-Wall", "-Wno-error=deprecated-declarations"], - strict_warnings = ["-Wextra", "-Wpedantic"], + minimal_warnings = ["-Wno-error=deprecated-declarations", "-Wno-unused-variable"], + strict_warnings = ["-Wno-bool-compare"], treat_warnings_as_errors = ["-Werror"], ) use_repo(gcc, "gcc_toolchain", "gcc_toolchain_gcc") diff --git a/test/WORKSPACE b/test/WORKSPACE index 617396b..8add252 100644 --- a/test/WORKSPACE +++ b/test/WORKSPACE @@ -31,8 +31,8 @@ gcc_toolchain( "treat_warnings_as_errors", ], warning_flags = { - "minimal_warnings": ["-Wall", "-Wno-error=deprecated-declarations"], - "strict_warnings": ["-Wextra", "-Wpedantic"], + "minimal_warnings": ["-Wno-error=deprecated-declarations"], + "strict_warnings": ["-Wno-bool-compare"], "treat_warnings_as_errors": ["-Werror"], }, )