diff --git a/cc/BUILD.bazel b/cc/BUILD.bazel index 116c691..dbfedd8 100644 --- a/cc/BUILD.bazel +++ b/cc/BUILD.bazel @@ -29,6 +29,18 @@ config_setting( visibility = ["//visibility:public"], ) +bool_flag( + name = "strong_stack_protector", + build_setting_default = False, + visibility = ["//visibility:public"], +) + +config_setting( + name = "_strong_stack_protector", + flag_values = {":strong_stack_protector": "true"}, + visibility = ["//visibility:public"], +) + # Enable warnings in tests for deprecated declarations with --@rules_swiftnav//cc:tests_warn_deprecated_declarations=true bool_flag( name = "tests_warn_deprecated_declarations", @@ -162,6 +174,7 @@ config_setting( visibility = ["//visibility:public"], ) + selects.config_setting_group( name = "enable_symbolizer_x86_64_linux", match_all = [ @@ -224,3 +237,12 @@ py_binary( ], visibility = ["//visibility:public"], ) + +py_binary( + name = "checktags", + srcs = [ + "checktags.py", + ], + args = [], + visibility = ["//visibility:public"], +) diff --git a/cc/checktags.py b/cc/checktags.py new file mode 100755 index 0000000..4a3633a --- /dev/null +++ b/cc/checktags.py @@ -0,0 +1,195 @@ +#!/bin/env python + +import subprocess +import json +import os +import re + +os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"]) + +targets = {} + +def run_bazel(args): + #print(args) + proc = subprocess.run(["bazel"] + args, capture_output=True, text=True) + return str(proc.stdout) + + +def get_target_list(kind): + stdout = run_bazel(["cquery", f"kind(\"{kind}\", //...)", "--output=label"]) + lines = stdout.split('\n') + ret = [l.split(' ')[0] for l in lines] + return ret + + +def maybe_add_target(json): + global targets + if 'target' not in json: + return + if 'rule' not in json['target']: + return + t = json['target']['rule'] + + if t['ruleClass'] not in ["cc_library", "cc_binary", "cc_test", "cmake"]: + return + + name = t['name'] + + + if name in targets: + return + + #print(name) + + targets[name] = {} + targets[name]['tags'] = [] + targets[name]['deps'] = [] + + for a in t['attribute']: + if a['name'] == 'tags': + if 'stringListValue' in a: + targets[name]['tags'] = a['stringListValue'] + + if a['name'] == 'deps': + if 'stringListValue' in a: + targets[name]['deps'] = a['stringListValue'] + + +def process_deps_for_target(name): + global targets + if name == '': + return + if name in targets: + return + + stdout = run_bazel(["cquery", f"\"{name}\"", "--output=jsonproto"]) + info = json.loads(stdout) + maybe_add_target(info['results'][0]) + + stdout = run_bazel(["cquery", f"kind(\"cc_library\", deps(\"{name}\"))", "--output=jsonproto"]) + info = json.loads(stdout) + + for lib in info['results']: + maybe_add_target(lib) + +#for t in get_target_list("cc_binary"): + #process_deps_for_target(t) +#for t in get_target_list("cc_test"): + #process_deps_for_target(t) +#for t in get_target_list("cc_library"): + #process_deps_for_target(t) + +def process_target_list(json): + if 'results' not in json: + return + for t in json['results']: + maybe_add_target(t) + +def add_all_from_query(key): + stdout = run_bazel(["cquery", key, "--output=jsonproto"]) + process_target_list(json.loads(stdout)) + +print("Loading targets") +add_all_from_query("kind(\"cc_binary\", //...)") +add_all_from_query("kind(\"cc_test\", //...)") +add_all_from_query("kind(\"cc_library\", //...)") +add_all_from_query("deps(kind(\"cc_binary\", //...))") +add_all_from_query("deps(kind(\"cc_test\", //...))") +add_all_from_query("deps(kind(\"cc_library\", //...))") + +def get_level_as_num(name): + if 'internal' in targets[name]['tags']: + return 0 + if 'prod' in targets[name]['tags']: + return 1 + if 'safe' in targets[name]['tags']: + return 2 + return None + +def level_to_str(level): + if level == 0: + return "internal" + if level == 1: + return "prod" + if level == 2: + return "safe" + assert False + +# Targets which don't belong to swift and won't follow the same tagging scheme in bazel +whitelist = [ + r"@eigen//.*", + r"@g(oogle)*test//.*", + r"@rules_fuzzing//.*", + r"@benchmark//.*", + r"@benchmark//.*", + r"@rapidcheck//.*", + r"@gmock-global//.*", + r"@check//.*", + r"@nanopb//.*", + r"@@.*", + r"@bazel.*", + r"@gflags//.*", + r"@json//.*", + r"@fast_csv//.*", + r"@yaml-cpp//.*", + r"@nlopt//.*", + r"@com_google_protobuf//.*", + r"@hdf5//.*", + r"@netcdf.*", + r"@variant//.*", + r"@zlib//.*", + r"@zstd//.*", + r"@optional//.*", + r"@ionosphere_models//.*", + r"@rules_cc//.*", + r"@suitesparse//.*", + r"@ThreadPool//.*", + r"@cereal//.*", + r"@gzip//.*", + r"@boringssl//.*", + # libfuzzer targets + r".*_raw_", +] + +def is_whitelisted(name): + for r in whitelist: + if re.match(r, name): + return True + + return False + +def validate_target(name): + if is_whitelisted(name): + return + + level = get_level_as_num(name) + portable = False + if 'portable' in targets[name]['tags']: + portable = True + + if level is None: + print(f"ERROR: Target {name} doesn't have a coding standard level") + else: + for d in targets[name]['deps']: + if d not in targets: + print(f"{d} not found in bazel....") + elif not is_whitelisted(d): + dep_level = get_level_as_num(d) + if dep_level is None: + print(f"ERROR: Target {name} has a coding standard level but depends on {d} which doesn't") + else: + if dep_level < level: + print(f"ERROR: Target {name} depends on {d} which has a lower coding standard level ({level_to_str(level)} vs {level_to_str(dep_level)}") + + if portable: + if 'portable' not in targets[d]['tags']: + print(f"ERROR: Target {name} is a portable target but depends on {d} which is not portable") + +for t in sorted(targets.keys()): + validate_target(t) + + + + + + diff --git a/cc/constraints/BUILD.bazel b/cc/constraints/BUILD.bazel index c517a5a..ba7c499 100644 --- a/cc/constraints/BUILD.bazel +++ b/cc/constraints/BUILD.bazel @@ -19,6 +19,11 @@ constraint_value( constraint_setting = ":toolchain", ) +constraint_value( + name = "gcc_11_system_toolchain", + constraint_setting = ":toolchain", +) + constraint_value( name = "yocto_generic_toolchain", constraint_setting = ":toolchain", diff --git a/cc/defs2.bzl b/cc/defs2.bzl new file mode 100644 index 0000000..84f44f4 --- /dev/null +++ b/cc/defs2.bzl @@ -0,0 +1,631 @@ +"""Swift wrappers for native cc rules.""" + +load("//stamp:stamp_file.bzl", "stamp_file") +load(":cc_static_library.bzl", _cc_static_library = "cc_static_library") +load(":utils.bzl", "construct_local_include") + +# Broad target catagories + +# A unit/integration tests. Executable during `bazel test` invocations +TEST = "test" + +# A compiled library +LIBRARY = "library" + +# An executable +BINARY = "binary" + +# Unit test, probably using a unit testing framework such as +# gtest or check. Tests a small software component, typically +# short lived. Not long running, not end-to-end tests +UNIT = "unit" + +# Name for a integration test +INTEGRATION = "integration" + +TEST_SRCS = "test_srcs" + +# Coding Standard levels +# +# Throughout this file and others there are references to coding +# standard levels. In Swift code there are 3 levels, 2 of which +# each have a sub variant. +# +# The levels are: +# - Safe: Code to be used in safety of life situations +# - Production: Code used in production environments but which does not require ISO26262 assessment +# - Internal: All other code, including test suites, internal utilities. Code which doesn't see production use +# +# The Safe and Production levels each have a variant: +# - Portable: Code which is expected to run on a wide variant of software/hardware targets and must be extremely portable +# +# These combine to make 5 different kinds of targets: +# +# safe: +# Safety of life code which must be highly portable. +# Strictest set of compiler warnings +# Highest level of static analysis +# Uses full Autosar coding standards +# C++14, C99 only +# Compiler extensions disabled but may be explicitly requested +# Not allowed to remove compiler warnings but may add them +# Permitted to link against "safe" and "safe-portable" targets only +# +# safe-portable: +# The same as "safe" plus: +# No compiler extensions +# Requires strict compliance with language spec (-pedantic used with GCC/Clang) +# Permitted to link against "safe-portable" targets only +# +# prod: +# Enables most compiler warnings and static analysis checks +# except any which pose a drag on developer workflow and do +# not have a noticeable effect on code quality +# Language standards may be chosen but default to C++14, C99 +# Compiler extensions disabled but may be explicitly requested +# Not allowed to remove compiler warnings but may add them +# Permitted to link against all target types except "internal" +# +# prod-portable: +# Same as "prod" plus: +# No compiler extensions +# Requires strict compliance with language spec (-pedantic used with GCC/Clang) +# Permitted to link against "prod-portable" and "safe-portable" targets only +# +# internal: +# Basic sanity compiler warnings +# Basic static analysis checks +# Fairly permissive with only major errors checked by default +# C++14, C99 default +# Compiler extensions disabled but may be explicitly requested +# Allowed to add or remove compiler flags +# Permitted to link against any other target type +# +# +# Tests are created through a separate macro but they all take the +# "internal" level. A notable difference for test suites is that they +# have exceptions and RTTI enabled by default (all other levels both +# features are disabled by default) + +# Internal coding standard level +INTERNAL = "internal" + +# Production coding standard level +PROD = "prod" + +# Safe coding standard level +SAFE = "safe" + +def _create_srcs(**kwargs): + native.filegroup( + name = kwargs.get("name") + ".srcs", + srcs = kwargs.get("srcs", []), + visibility = kwargs.get("visibility", ["//visibility:public"]), + ) + +def _create_hdrs(**kwargs): + native.filegroup( + name = kwargs.get("name") + ".hdrs", + srcs = kwargs.get("hdrs", []), + visibility = kwargs.get("visibility", ["//visibility:public"]), + ) + +def _any_of_in(keys, features): + for k in keys: + if k in features: + return True + return False + +def _check_features_misuse(features): + if "gnu_extensions" in features: + fail("Do not enable gnu_extensions features manually, pass extensions=True instead") + if _any_of_in(["c89", "c90", "c99", "c11", "c17", "c++98", "c++11", "c++14", "c++17", "c++20", "c++23"], features): + fail("Do not set language standards manually, pass standard=n instead") + if _any_of_in(["prod_coding_standard", "internal_coding_standard", "safe_coding_standard", "portable_coding_standard"], features): + fail("Do not try to control coding standard manually, use the correct macro to create a target") + +def _get_lang_features(lang, extensions, standard, portable): + """ + Get toolchain features which need to be enable for given language standard and extensions + gnu_extensions may not be the best choice of names here + """ + + lang_standard_feature = "" + lang_extensions_feature = [] + if lang == "c": + if standard == None: + standard = 99 + if standard not in [89, 90, 99, 11, 17]: + fail("Invalid C standard") + lang_standard_feature = ["c" + str(standard)] + elif standard != None: + if standard not in [98, 11, 14, 17, 20]: + fail("Invalid CXX standard") + lang_standard_feature = ["c++" + str(standard)] + else: + lang_standard_feature = select({ + Label("@rules_swiftnav//cc:cxx17"): ["c++17"], + Label("@rules_swiftnav//cc:cxx20"): ["c++20"], + Label("@rules_swiftnav//cc:cxx23"): ["c++23"], + "//conditions:default": ["c++14"], + }) + + if extensions and portable: + fail("Compiler extensions may not be enabled on portable targets") + + if extensions: + lang_extensions_feature.append("gnu_extensions") + + if portable: + lang_extensions_feature.append("portable_coding_standard") + + return (lang_standard_feature, lang_extensions_feature) + +def _get_exceptions_rtti_features(level, lang, exceptions, rtti): + if lang == "c": + if rtti: + fail("Enabling RTTI is meaningless with C targets") + if exceptions: + fail("Enabling exceptions is meaningless with C targets") + return [] + + if level == "test": + # RTTI and exceptions default to on for tests and test libraries + if rtti == None: + rtti = True + if exceptions == None: + exceptions = True + + return select({ + Label("@rules_swiftnav//cc:_enable_exceptions"): ["exceptions_feature"], + "//conditions:default": ["exceptions_feature" if exceptions else "noexceptions_feature"], + }) + select({ + Label("@rules_swiftnav//cc:_enable_rtti"): ["rtti_feature"], + "//conditions:default": ["rtti_feature" if rtti else "nortti_feature"], + }) + +def _get_warnings_features(level): + if level == "test": + return ["internal_coding_standard"] + return [level + "_coding_standard"] + +def _get_stack_protector_feature(value): + if value == None: + return select({ + Label("@rules_swiftnav//cc:_strong_stack_protector"): ["strong_stack_protector"], + "//conditions:default": ["stack_protector"], + }) + + if value == "strong": + return "strong_stack_protector" + fail("Invalid stack protector option, must either \"strong\" or not specified") + +def _check_copts_misuse(copt_flag, key, copts, nocopts): + """ + Check for misuse of copts and nocopts options to target macros + + Certain features such as exceptions or RTTI as controlled through + options to the target macros. Many developers will try to pass + compiler flags directly through copts or nocopts, this helper + function will catch this and fail the build + """ + if copt_flag in copts or copt_flag in nocopts: + fail("Do not try to pass {} in copts or nocopts, use the {} key instead".format(copt_flag, key)) + +def _validate_copts_nocopts(level, portable, copts, nocopts): + _check_copts_misuse("-frtti", "rtti", copts, nocopts) + _check_copts_misuse("-fno-rtti", "rtti", copts, nocopts) + _check_copts_misuse("-fexceptions", "exceptions", copts, nocopts) + _check_copts_misuse("-fno-exceptions", "exceptions", copts, nocopts) + _check_copts_misuse("-pedantic", "language standard compliance", copts, nocopts) + + if level in [PROD, SAFE]: + if len(nocopts) > 0: + fail("Passing nocopts to production or safe targets is not allowed. Only adding flags via copts is permitted") + + if portable: + if copts != None and copts != []: + fail("Don't use copts for portable targets, use a method which isn't going to break compatibility with other compilers such as moving them to .bazelrc or environment variables") + if nocopts != None and nocopts != []: + fail("Don't use copts for portable targets, use a method which isn't going to break compatibility with other compilers such as moving them to .bazelrc or environment variables") + + +def _build_copts(copts, nocopts): + return [f for f in copts if f not in nocopts] + +# Handle various nuances of local include paths +def _construct_local_includes(local_includes): + ret = [] + for path in local_includes: + ret += construct_local_include(path) + return ret + +# Handle whether to link statically +def _link_static(linkstatic = True): + return select({ + Label("@rules_swiftnav//cc:_enable_shared"): False, + "//conditions:default": linkstatic, + }) + +# Disable building when --//:disable_tests=true or when building on windows +def _test_compatible_with(): + return select({ + Label("@rules_swiftnav//cc:_disable_tests"): ["@platforms//:incompatible"], + "@platforms//os:windows": ["@platforms//:incompatible"], + "//conditions:default": [], + }) + +def swift_add_library(**kwargs): + """ + Add a library target. Generic function to be called from a wrapper macro + which properly sets the coding standard level + """ + + features = kwargs.pop("features", []) + + _check_features_misuse(features) + + level = kwargs.pop("level") + portable = kwargs.pop("portable", False) + lang = kwargs.pop("lang") + extensions = kwargs.pop("extensions", False) + standard = kwargs.pop("standard", None) + + (lang_standard_feature, lang_extensions_feature) = _get_lang_features(lang, extensions, standard, portable) + + stack_protector_feature = _get_stack_protector_feature(kwargs.pop("stack_protector", None)) + + exceptions_rtti_features = _get_exceptions_rtti_features(level, lang, kwargs.pop("exceptions", None), kwargs.pop("rtti", None)) + + warnings_features = _get_warnings_features(level) + + copts = kwargs.pop("copts", []) + nocopts = kwargs.pop("nocopts", []) + + _validate_copts_nocopts(level, portable, copts, nocopts) + + copts = _build_copts(copts, nocopts) + + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + for i in local_includes: + copts.append(i) + + kwargs["copts"] = copts + kwargs["features"] = warnings_features + lang_standard_feature + lang_extensions_feature + stack_protector_feature + exceptions_rtti_features + select({ + Label("//cc:_disable_warnings_as_errors"): [], + "//conditions:default": ["treat_warnings_as_errors"], + }) + features + + is_test_library = level == "test" + + kwargs["tags"] = [LIBRARY, "internal" if level == "test" else level] + (["portable"] if portable else []) + kwargs.get("tags", []) + (["test_library"] if is_test_library else []) + if level == "test": + kwargs["target_compatible_with"] = kwargs.get("target_compatible_with", []) + _test_compatible_with() + else: + kwargs["target_compatible_with"] = kwargs.get("target_compatible_with", []) + + kwargs["linkstatic"] = _link_static(kwargs.get("linkstatic", True)) + + _create_srcs(**kwargs) + _create_hdrs(**kwargs) + + native.cc_library(**kwargs) + +def swift_add_binary(**kwargs): + """ + Add an executable target. Generic function to be called from a wrapper macro + which properly sets the coding standard level + """ + + features = kwargs.pop("features", []) + + _check_features_misuse(features) + + level = kwargs.pop("level") + portable = kwargs.pop("portable", False) + lang = kwargs.pop("lang") + extensions = kwargs.pop("extensions", False) + standard = kwargs.pop("standard", None) + + (lang_standard_feature, lang_extensions_feature) = _get_lang_features(lang, extensions, standard, portable) + + stack_protector_feature = _get_stack_protector_feature(kwargs.pop("stack_protector", None)) + + exceptions_rtti_features = _get_exceptions_rtti_features(level, lang, kwargs.pop("exceptions", None), kwargs.pop("rtti", None)) + + warnings_features = _get_warnings_features(level) + + copts = kwargs.pop("copts", []) + nocopts = kwargs.pop("nocopts", []) + + _validate_copts_nocopts(level, portable, copts, nocopts) + + copts = _build_copts(copts, nocopts) + + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + for i in local_includes: + copts.append(i) + + kwargs["copts"] = copts + kwargs["features"] = warnings_features + lang_standard_feature + lang_extensions_feature + stack_protector_feature + exceptions_rtti_features + select({ + Label("//cc:_disable_warnings_as_errors"): [], + "//conditions:default": ["treat_warnings_as_errors"], + }) + features + + kwargs["tags"] = [BINARY, "internal" if level == "test" else level] + (["portable"] if portable else []) + kwargs.get("tags", []) + kwargs["target_compatible_with"] = kwargs.get("target_compatible_with", []) + + kwargs["linkstatic"] = _link_static(kwargs.get("linkstatic", True)) + + _create_srcs(**kwargs) + _create_hdrs(**kwargs) + + native.cc_binary(**kwargs) + +def swift_add_test(**kwargs): + """ + Add a library target. Generic function to be called from a wrapper macro + which properly sets the coding standard level + """ + + features = kwargs.pop("features", []) + + _check_features_misuse(features) + + level = TEST + portable = kwargs.pop("portable", False) + lang = kwargs.pop("lang") + extensions = kwargs.pop("extensions", False) + standard = kwargs.pop("standard", None) + + (lang_standard_feature, lang_extensions_feature) = _get_lang_features(lang, extensions, standard, portable) + + stack_protector_feature = _get_stack_protector_feature(kwargs.pop("stack_protector", None)) + + exceptions_rtti_features = _get_exceptions_rtti_features(level, lang, kwargs.pop("exceptions", True), kwargs.pop("rtti", True)) + + warnings_features = _get_warnings_features(level) + + copts = kwargs.pop("copts", []) + nocopts = kwargs.pop("nocopts", []) + + _validate_copts_nocopts(level, portable, copts, nocopts) + + copts = _build_copts(copts, nocopts) + + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + for i in local_includes: + copts.append(i) + + kwargs["copts"] = copts + kwargs["features"] = warnings_features + lang_standard_feature + lang_extensions_feature + stack_protector_feature + exceptions_rtti_features + select({ + Label("//cc:_disable_warnings_as_errors"): [], + "//conditions:default": ["treat_warnings_as_errors"], + }) + ["disable_warnings_for_test_targets"] + features + + if "type" not in kwargs: + fail("Type must be given for tests, either UNIT or INTEGRATION") + + type = kwargs.pop("type") + if not (type == UNIT or type == INTEGRATION): + fail("The 'type' attribute must be either UNIT or INTEGRATION") + + kwargs["tags"] = [TEST, "internal" if level == "test" else level, type, "test_srcs"] + kwargs.get("tags", []) + + kwargs["linkstatic"] = _link_static(kwargs.get("linkstatic", True)) + + kwargs["target_compatible_with"] = kwargs.get("target_compatible_with", []) + _test_compatible_with() + + _create_srcs(**kwargs) + _create_hdrs(**kwargs) + + native.cc_test(**kwargs) + +def swift_add_cc_test(**kwargs): + swift_add_test(lang = "cc", **kwargs) + +def swift_add_c_test(**kwargs): + swift_add_test(lang = "c", **kwargs) + +STAMPED_LIB_SUFFIX = ".stamped" + +def swift_add_cc_stamped_library(name, out, template, hdrs, defaults, **kwargs): + """Creates a cc_library stamped with non-hermetic build metadata. + + Creates a cc_library from the input template with values of the form @VAL@ + substituted with values from the workspace status program. This typically + includes version control information, timestamps, and other similiar + data. The output file is only compiled into the final resulting binary. + + Also creates an additional library target appended with ".stamped". This + variant has the stamped symbols included directly into the resulting + artifact. Its only intended to be used when creating a static archive + bundle with cc_static_archive. + + Currently only stable status variables are supported. + + See https://bazel.build/docs/user-manual#workspace-status for more. + + Args: + name: The name of the target + out: The expanded source file + template: The input template + hdrs: See https://bazel.build/reference/be/c-cpp#cc_library.hdrs + includes: See https://bazel.build/reference/be/c-cpp#cc_library.includes + defaults: Dict of default values when stamping is not enabled + visibility: See https://bazel.build/reference/be/common-definitions#common.visibility + """ + + source_name = name + "_" + + stamp_file(name = source_name, out = out, defaults = defaults, template = template) + + visibility = kwargs.pop("visibility", []) + + # This variant has the stamped symbols in the archive + swift_add_safe_portable_cc_library( + name = name + STAMPED_LIB_SUFFIX, + srcs = [source_name], + visibility = visibility, + ) + + # This variant forwards the stamped symbols to the final link + swift_add_safe_portable_cc_library( + name = name, + hdrs = hdrs, + linkstamp = source_name, + visibility = visibility, + **kwargs + ) + +def _assert_no_reserved_keys(**kwargs): + if "lang" in kwargs: + fail("Do not try to specify language manually") + if "level" in kwargs: + fail("Do not try to specify coding standard manually") + if "portable" in kwargs: + fail("Do not try to control portability manually") + +def swift_add_safe_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = SAFE, portable = False, **kwargs) + +def swift_add_prod_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = PROD, portable = False, **kwargs) + +def swift_add_internal_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = INTERNAL, portable = False, **kwargs) + +def swift_add_test_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = TEST, portable = False, **kwargs) + +def swift_add_safe_portable_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = SAFE, portable = True, **kwargs) + +def swift_add_prod_portable_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = PROD, portable = True, **kwargs) + +def swift_add_internal_portable_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = INTERNAL, portable = True, **kwargs) + +def swift_add_test_portable_cc_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "cc", level = TEST, portable = True, **kwargs) + +def swift_add_safe_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = SAFE, portable = False, **kwargs) + +def swift_add_prod_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = PROD, portable = False, **kwargs) + +def swift_add_internal_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = INTERNAL, portable = False, **kwargs) + +def swift_add_test_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = TEST, portable = False, **kwargs) + +def swift_add_safe_portable_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = SAFE, portable = True, **kwargs) + +def swift_add_prod_portable_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = PROD, portable = True, **kwargs) + +def swift_add_internal_portable_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = INTERNAL, portable = True, **kwargs) + +def swift_add_test_portable_c_library(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_library(lang = "c", level = TEST, portable = True, **kwargs) + + +def swift_add_safe_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = SAFE, portable = False, **kwargs) + +def swift_add_prod_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = PROD, portable = False, **kwargs) + +def swift_add_internal_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = INTERNAL, portable = False, **kwargs) + +def swift_add_test_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = TEST, portable = False, **kwargs) + +def swift_add_safe_portable_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = SAFE, portable = True, **kwargs) + +def swift_add_prod_portable_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = PROD, portable = True, **kwargs) + +def swift_add_internal_portable_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = INTERNAL, portable = True, **kwargs) + +def swift_add_test_portable_cc_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "cc", level = TEST, portable = True, **kwargs) + +def swift_add_safe_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = SAFE, portable = False, **kwargs) + +def swift_add_prod_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = PROD, portable = False, **kwargs) + +def swift_add_internal_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = INTERNAL, portable = False, **kwargs) + +def swift_add_test_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = TEST, portable = False, **kwargs) + +def swift_add_safe_portable_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = SAFE, portable = True, **kwargs) + +def swift_add_prod_portable_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = PROD, portable = True, **kwargs) + +def swift_add_internal_portable_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = INTERNAL, portable = True, **kwargs) + +def swift_add_test_portable_c_binary(**kwargs): + _assert_no_reserved_keys(**kwargs) + swift_add_binary(lang = "c", level = TEST, portable = True, **kwargs) + + +def swift_add_cc_static_library(name, deps, visibility = ["//visibility:private"]): + _cc_static_library( + name = name, + deps = deps, + target_compatible_with = select({ + # Creating static libraries is not supported by macos yet. + "@platforms//os:macos": ["@platforms//:incompatible"], + "//conditions:default": [], + }), + visibility = visibility, + ) + + diff --git a/cc/toolchains/gcc_11/x86_64-linux/BUILD.bazel b/cc/toolchains/gcc_11/x86_64-linux/BUILD.bazel index 5175b26..49669aa 100644 --- a/cc/toolchains/gcc_11/x86_64-linux/BUILD.bazel +++ b/cc/toolchains/gcc_11/x86_64-linux/BUILD.bazel @@ -1,74 +1,30 @@ +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@rules_cc//cc/private/toolchain:unix_cc_toolchain_config.bzl", "cc_toolchain_config") load("@rules_cc//cc/toolchains:cc_toolchain.bzl", "cc_toolchain") +load(":config.bzl", "config") -package(default_visibility = ["//visibility:public"]) +_all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, +] -cc_toolchain_config( - name = "local-x86_64-linux", - abi_libc_version = "unknown", - abi_version = "unknown", - compiler = "gcc-11", - coverage_compile_flags = ["--coverage"], - coverage_link_flags = ["--coverage"], - cpu = "k8", - cxx_builtin_include_directories = [ - "/usr/include", - "/usr/include/c++/11", - "/usr/include/x86_64-linux-gnu/c++/11", - "/usr/include/c++/11/backward", - "/usr/lib/gcc/x86_64-linux-gnu/11/include", - "/usr/lib/gcc/x86_64-linux-gnu/11/include-fixed", - ], - cxx_flags = ["-std=c++17"], - dbg_compile_flags = ["-g"], - host_system_name = "local", - link_flags = [ - "-lstdc++", - "-fuse-ld=gold", - "-Wl,-no-as-needed", - "-Wl,-z,relro,-z,now", - "-B/usr/bin", - "-pass-exit-codes", - "-lm", - ], - opt_compile_flags = [ - "-g0", - "-O2", - "-D_FORTIFY_SOURCE=1", - "-DNDEBUG", - "-ffunction-sections", - "-fdata-sections", - ], - opt_link_flags = ["-Wl,--gc-sections"], - supports_start_end_lib = True, - target_libc = "glibc", - target_system_name = "local", - tool_paths = { - "ar": "/usr/bin/ar", - "as": "/usr/bin/as", - "cpp": "/usr/bin/cpp-11", - "gcc": "/usr/bin/gcc-11", - "g++": "/usr/bin/g++-11", - "gcov": "/usr/bin/gcov-11", - "ld": "/usr/bin/g++-11", - "nm": "/usr/bin/nm", - "objcopy": "/usr/bin/objcopy", - "objdump": "/usr/bin/objdump", - "ranlib": "/usr/bin/ranlib", - "strip": "/usr/bin/strip", - }, - toolchain_identifier = "cc-gcc-x86_64-linux", - unfiltered_compile_flags = [ - "-fno-canonical-system-headers", - "-Wno-builtin-macro-redefined", - "-D__DATE__=\"redacted\"", - "-D__TIMESTAMP__=\"redacted\"", - "-D__TIME__=\"redacted\"", - ], -) +package(default_visibility = ["//visibility:public"]) filegroup(name = "empty") +config( + name = "config", + link_flags = ["-lm", "-latomic", "-lstdc++"], +) + cc_toolchain( name = "cc-gcc-x86_64-linux", all_files = ":empty", @@ -80,12 +36,12 @@ cc_toolchain( objcopy_files = ":empty", strip_files = ":empty", supports_param_files = 0, - toolchain_config = ":local-x86_64-linux", + toolchain_config = ":config", toolchain_identifier = "cc-gcc-x86_64-linux", ) toolchain( - name = "cc-toolchain-x86_64-linux", + name = "gcc_11_system_toolchain", exec_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", @@ -93,6 +49,7 @@ toolchain( target_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", + "@rules_swiftnav//cc/constraints:gcc_11_system_toolchain", ], toolchain = ":cc-gcc-x86_64-linux", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", diff --git a/cc/toolchains/gcc_11/x86_64-linux/config.bzl b/cc/toolchains/gcc_11/x86_64-linux/config.bzl new file mode 100644 index 0000000..8183b3c --- /dev/null +++ b/cc/toolchains/gcc_11/x86_64-linux/config.bzl @@ -0,0 +1,635 @@ +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "artifact_name_pattern", + "feature", + "feature_set", + "flag_group", + "flag_set", + "tool", + "tool_path", + "variable_with_value", + "with_feature_set", +) +load( + "swift_custom_features.bzl", + "c11_standard_feature", + "c17_standard_feature", + "c89_standard_feature", + "c90_standard_feature", + "c99_standard_feature", + "cxx11_standard_feature", + "cxx14_standard_feature", + "cxx17_standard_feature", + "cxx20_standard_feature", + "cxx98_standard_feature", + "gnu_extensions_feature", + "swift_disable_conversion_warning_feature", + "swift_disable_warnings_for_test_targets_feature", + "swift_exceptions_feature", + "swift_internal_coding_standard_feature", + "swift_noexceptions_feature", + "swift_nortti_feature", + "swift_portable_coding_standard_feature", + "swift_prod_coding_standard_feature", + "swift_relwdbg_feature", + "swift_rtti_feature", + "swift_safe_coding_standard_feature", + "stack_protector_feature", + "strong_stack_protector_feature", +) + +all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, +] + +all_cpp_compile_actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, +] + +preprocessor_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.clif_match, +] + +codegen_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, +] + +all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +lto_index_actions = [ + ACTION_NAMES.lto_index_for_executable, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, +] +def _impl(ctx): + # Note that we also set --coverage for c++-link-nodeps-dynamic-library. The + # generated code contains references to gcov symbols, and the dynamic linker + # can't resolve them unless the library is linked against gcov. + coverage_feature = feature( + name = "coverage", + provides = ["profile"], + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ], + flag_groups = ([ + flag_group(flags = ctx.attr.coverage_compile_flags), + ] if ctx.attr.coverage_compile_flags else []), + ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group(flags = ctx.attr.coverage_link_flags), + ] if ctx.attr.coverage_link_flags else []), + ), + ], + ) + + cxx_builtin_include_directories = [ + "/usr/include", + "/usr/include/c++/11", + "/usr/include/x86_64-linux-gnu/c++/11", + "/usr/include/c++/11/backward", + "/usr/lib/gcc/x86_64-linux-gnu/11/include", + "/usr/lib/gcc/x86_64-linux-gnu/11/include-fixed", + ] + + default_compile_flags_feature = feature( + name = "default_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + # Security hardening requires optimization. + # We need to undef it as some distributions now have it enabled by default. + flags = [ + "-U_FORTIFY_SOURCE", + "-no-canonical-prefixes", + "-fno-canonical-system-headers", + ], + ), + ], + with_features = [ + with_feature_set( + not_features = ["thin_lto"], + ), + ], + ), + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.compile_flags, + ), + ] if ctx.attr.compile_flags else []), + ), + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.dbg_compile_flags, + ), + ] if ctx.attr.dbg_compile_flags else []), + with_features = [with_feature_set(features = ["dbg"])], + ), + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.opt_compile_flags, + ), + ] if ctx.attr.opt_compile_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ), + flag_set( + actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend], + flag_groups = ([ + flag_group( + flags = ctx.attr.cxx_flags, + ), + ] if ctx.attr.cxx_flags else []), + ), + ], + ) + + is_linux = ctx.attr.target_libc != "macosx" + + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group( + flags = ["-lm", "-latomic"] + ctx.attr.link_flags, + ), + ] if ctx.attr.link_flags else []), + ), + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.opt_link_flags, + ), + ] if ctx.attr.opt_link_flags else []), + with_features = [with_feature_set(features = ["opt"])], + ), + ] + ([flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = ([ + flag_group( + flags = [ + # implies static linking. + "-l:libc++.a", + "-l:libc++abi.a", + "-l:libunwind.a", + "-rtlib=compiler-rt", + ], + ), + ]), + with_features = [with_feature_set(features = ["libcpp"])], + )] if is_linux else []), + ) + + dbg_feature = feature(name = "dbg") + + opt_feature = feature(name = "opt") + + supports_pic_feature = feature( + name = "supports_pic", + enabled = True, + ) + supports_start_end_lib_feature = feature( + name = "supports_start_end_lib", + enabled = True, + ) + + + tool_paths = [ + tool_path(name = "ar", path = "/usr/bin/ar"), + tool_path(name = "as", path = "/usr/bin/as"), + tool_path(name = "cpp", path = "/usr/bin/cpp-11"), + tool_path(name = "gcc", path = "/usr/bin/gcc-11"), + tool_path(name = "g++", path = "/usr/bin/g++-11"), + tool_path(name = "gcov", path = "/usr/bin/gcov-11"), + tool_path(name = "ld", path = "/usr/bin/g++-11"), + tool_path(name = "nm", path = "/usr/bin/nm"), + tool_path(name = "objcopy", path = "/usr/bin/objcopy"), + tool_path(name = "objdump", path = "/usr/bin/objdump"), + tool_path(name = "ranlib", path = "/usr/bin/ranlib"), + tool_path(name = "strip", path = "/usr/bin/strip"), + ] + + features = [ + gnu_extensions_feature, + c89_standard_feature, + c90_standard_feature, + c99_standard_feature, + c11_standard_feature, + c17_standard_feature, + cxx98_standard_feature, + cxx11_standard_feature, + cxx14_standard_feature, + cxx17_standard_feature, + cxx20_standard_feature, + swift_relwdbg_feature, + swift_rtti_feature, + swift_nortti_feature, + swift_exceptions_feature, + swift_noexceptions_feature, + swift_internal_coding_standard_feature, + swift_prod_coding_standard_feature, + swift_safe_coding_standard_feature, + swift_portable_coding_standard_feature, + swift_disable_conversion_warning_feature, + swift_disable_warnings_for_test_targets_feature, + ] + toolchain_identifier = "cc-gcc-x86_64-linux" + unfiltered_compile_flags_feature = feature( + name = "unfiltered_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.unfiltered_compile_flags, + ), + ] if ctx.attr.unfiltered_compile_flags else []), + ), + ], + ) + + sysroot_feature = feature( + name = "sysroot", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, + ] + all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["--sysroot=%{sysroot}"], + expand_if_available = "sysroot", + ), + ], + ), + ], + ) + + libtool_feature = feature( + name = "libtool", + enabled = ctx.attr.use_libtool, + ) + + archiver_flags_feature = feature( + name = "archiver_flags", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group(flags = ["rcsD"]), + flag_group( + flags = ["%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], + with_features = [ + with_feature_set( + not_features = ["libtool"], + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group(flags = ["-no_warning_for_no_symbols", "-static", "-s"]), + flag_group( + flags = ["-o", "%{output_execpath}"], + expand_if_available = "output_execpath", + ), + ], + with_features = [ + with_feature_set( + features = ["libtool"], + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = [ + flag_group( + iterate_over = "libraries_to_link", + flag_groups = [ + flag_group( + flags = ["%{libraries_to_link.name}"], + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file", + ), + ), + flag_group( + flags = ["%{libraries_to_link.object_files}"], + iterate_over = "libraries_to_link.object_files", + expand_if_equal = variable_with_value( + name = "libraries_to_link.type", + value = "object_file_group", + ), + ), + ], + expand_if_available = "libraries_to_link", + ), + ], + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_static_library], + flag_groups = ([ + flag_group( + flags = ctx.attr.archive_flags, + ), + ] if ctx.attr.archive_flags else []), + ), + ], + ) + + user_link_flags_feature = feature( + name = "user_link_flags", + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [ + flag_group( + flags = ["%{user_link_flags}"], + iterate_over = "user_link_flags", + expand_if_available = "user_link_flags", + ), + ], + ), + ], + ) + + fdo_optimize_feature = feature( + name = "fdo_optimize", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile], + flag_groups = [ + flag_group( + flags = [ + "-fprofile-use=%{fdo_profile_path}", + "-fprofile-correction", + ], + expand_if_available = "fdo_profile_path", + ), + ], + ), + ], + provides = ["profile"], + ) + + supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) + + user_compile_flags_feature = feature( + name = "user_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = ["%{user_compile_flags}"], + iterate_over = "user_compile_flags", + expand_if_available = "user_compile_flags", + ), + ], + ), + ], + ) + + unfiltered_compile_flags_feature = feature( + name = "unfiltered_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_compile_actions, + flag_groups = ([ + flag_group( + flags = ctx.attr.unfiltered_compile_flags, + ), + ] if ctx.attr.unfiltered_compile_flags else []), + ), + ], + ) + + archive_param_file_feature = feature( + name = "archive_param_file", + enabled = True, + ) + + treat_warnings_as_errors_feature = feature( + name = "treat_warnings_as_errors", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-Werror"])], + ), + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["-Wl,-fatal-warnings"])], + ), + ], + ) + + external_include_paths_feature = feature( + name = "external_include_paths", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.clif_match, + ACTION_NAMES.objc_compile, + ACTION_NAMES.objcpp_compile, + ], + flag_groups = [ + flag_group( + flags = ["-isystem", "%{external_include_paths}"], + iterate_over = "external_include_paths", + expand_if_available = "external_include_paths", + ), + ], + ), + ], + ) + + default_link_libs_feature = feature( + name = "default_link_libs", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions + lto_index_actions, + flag_groups = [flag_group(flags = ctx.attr.link_libs)] if ctx.attr.link_libs else [], + ), + ], + ) + + features = [ + libtool_feature, + archiver_flags_feature, + supports_pic_feature, + ] + ( + [ + supports_start_end_lib_feature, + ] if ctx.attr.supports_start_end_lib else [] + ) + [ + default_compile_flags_feature, + default_link_flags_feature, + user_link_flags_feature, + default_link_libs_feature, + external_include_paths_feature, + fdo_optimize_feature, + supports_dynamic_linker_feature, + dbg_feature, + opt_feature, + user_compile_flags_feature, + unfiltered_compile_flags_feature, + treat_warnings_as_errors_feature, + archive_param_file_feature, + sysroot_feature, + ] + [ + # append swiftnavs custom features here + gnu_extensions_feature, + c89_standard_feature, + c90_standard_feature, + c99_standard_feature, + c11_standard_feature, + c17_standard_feature, + cxx98_standard_feature, + cxx11_standard_feature, + cxx14_standard_feature, + cxx17_standard_feature, + cxx20_standard_feature, + swift_relwdbg_feature, + swift_rtti_feature, + swift_nortti_feature, + swift_exceptions_feature, + swift_noexceptions_feature, + swift_internal_coding_standard_feature, + swift_prod_coding_standard_feature, + swift_safe_coding_standard_feature, + swift_portable_coding_standard_feature, + swift_disable_conversion_warning_feature, + swift_disable_warnings_for_test_targets_feature, + stack_protector_feature, + strong_stack_protector_feature, + ] + + action_configs = [] + artifact_name_patterns = [] + + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + action_configs = action_configs, + artifact_name_patterns = artifact_name_patterns, + cxx_builtin_include_directories = cxx_builtin_include_directories, + toolchain_identifier = ctx.attr.toolchain_identifier, + host_system_name = ctx.attr.host_system_name, + target_system_name = ctx.attr.target_system_name, + target_cpu = ctx.attr.cpu, + target_libc = ctx.attr.target_libc, + compiler = ctx.attr.compiler, + abi_version = ctx.attr.abi_version, + abi_libc_version = ctx.attr.abi_libc_version, + tool_paths = tool_paths, + builtin_sysroot = ctx.attr.builtin_sysroot, + ) + +config = rule( + implementation = _impl, + attrs = { + "cpu": attr.string(), + "compiler": attr.string(), + "toolchain_identifier": attr.string(), + "host_system_name": attr.string(), + "target_system_name": attr.string(), + "target_libc": attr.string(), + "abi_version": attr.string(), + "abi_libc_version": attr.string(), + "cxx_builtin_include_directories": attr.string_list(), + "tool_paths": attr.string_dict(), + "compile_flags": attr.string_list(), + "dbg_compile_flags": attr.string_list(), + "opt_compile_flags": attr.string_list(), + "cxx_flags": attr.string_list(), + "link_flags": attr.string_list(), + "archive_flags": attr.string_list(), + "link_libs": attr.string_list(), + "opt_link_flags": attr.string_list(), + "unfiltered_compile_flags": attr.string_list(), + "coverage_compile_flags": attr.string_list(), + "coverage_link_flags": attr.string_list(), + "supports_start_end_lib": attr.bool(), + "builtin_sysroot": attr.string(), + "use_libtool": attr.bool(), + "_xcode_config": attr.label(default = configuration_field( + fragment = "apple", + name = "xcode_config_label", + )), + }, + provides = [CcToolchainConfigInfo], +) + diff --git a/cc/toolchains/gcc_11/x86_64-linux/swift_custom_features.bzl b/cc/toolchains/gcc_11/x86_64-linux/swift_custom_features.bzl new file mode 100644 index 0000000..47167a3 --- /dev/null +++ b/cc/toolchains/gcc_11/x86_64-linux/swift_custom_features.bzl @@ -0,0 +1,621 @@ +"""Swiftnav custom toolchain features + +Our unix_cc_toolchain_config.bzl is a fork of upstream at: + https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_toolchain_config.bzl + +For the time being we would like to keep this a super set of the features defined there +or pull in fixes and new features without upgrading bazel. + +In order to keep this maintainable all of our custom additions will be added +to this file under the swift_ prefix. + +Bug fixes, ports of features from newer versions, etc.. should be put as they +appear in upstream in unix_cc_toolchain_config.bzl. +""" + +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "feature", + "flag_group", + "flag_set", + "with_feature_set", +) +load( + "@rules_swiftnav//cc/toolchains:gcc_llvm_flags.bzl", + "disable_conversion_warning_flags", + "disable_warnings_for_test_targets_flags", + "get_flags_for_lang_and_level", +) + +_invalid_flags = [ + "-Wstring-compare", + "-Wbool-compare", + "-Wmaybe-uninitialized", + "-Wclobbered", + "-Wmemset-elt-size", + "-Wmismatched-dealloc", + "-Wmissing-attributes", + "-Wmultistatement-macros", + "-Wrestrict", + "-Wenum-int-mismatch", + "-Wformat-overflow", + "-Wformat-truncation", + "-Wnonnull-compare", + "-Wopenmp-simd", + "-Wvla-parameter", + "-Wzero-length-bounds", + "-Wself-move", + "-Wtautological-unsigned-zero-compare", + "-Wno-tautological-unsigned-zero-compare", +] + +_extra_flags = [ + # Technically correct but very overzealous + "-Wno-unused-const-variable", +] + +_all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, +] + +_all_cpp_compile_actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, +] + +_preprocessor_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.clif_match, +] + +_codegen_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, +] + +_all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +_lto_index_actions = [ + ACTION_NAMES.lto_index_for_executable, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, +] + +gnu_extensions_feature = feature(name = "gnu_extensions") + +c89_standard_feature = feature( + name = "c89", + provides = ["c_standard"], + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=c89"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu89"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +c90_standard_feature = feature( + name = "c90", + provides = ["c_standard"], + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=c90"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu90"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +c99_standard_feature = feature( + name = "c99", + provides = ["c_standard"], + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=c99"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu99"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +c11_standard_feature = feature( + name = "c11", + provides = ["c_standard"], + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=c11"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu11"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +c17_standard_feature = feature( + name = "c17", + provides = ["c_standard"], + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=c17"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = [ACTION_NAMES.c_compile], + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu17"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +cxx98_standard_feature = feature( + name = "c++98", + provides = ["cxx_standard"], + flag_sets = [ + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=c++98"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++98"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +cxx11_standard_feature = feature( + name = "c++11", + provides = ["cxx_standard"], + flag_sets = [ + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=c++0x"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++0x"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +cxx14_standard_feature = feature( + name = "c++14", + provides = ["cxx_standard"], + flag_sets = [ + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=c++14"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++14"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +cxx17_standard_feature = feature( + name = "c++17", + provides = ["cxx_standard"], + flag_sets = [ + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=c++17"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++17"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + ], +) + +cxx20_standard_feature = feature( + name = "c++20", + provides = ["cxx_standard"], + flag_sets = [ + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=c++20"], + ), + ] + ), + with_features = [ + with_feature_set(not_features = ["gnu_extensions"]), + ], + ), + flag_set( + actions = _all_cpp_compile_actions, + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++20"], + ), + ] + ), + with_features = [ + with_feature_set(features = ["gnu_extensions"]), + ], + ), + # clang is aggresive about removing features from the standard. + flag_set( + actions = _preprocessor_compile_actions, + flag_groups = ( + [ + flag_group( + flags = [ + # This is a workaround for memory_resource being experimental only + # in the llvm-14 libc++ impelmentation. + "-DSWIFTNAV_EXPERIMENTAL_MEMORY_RESOURCE", + "-D_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES", + ], + ), + ] + ), + with_features = [ + with_feature_set(features = ["libcpp"]), + ], + ), + ], +) + +swift_relwdbg_feature = feature( + name = "relwdbg", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-O2", "-g"])], + ), + ], +) + +swift_rtti_feature = feature( + name = "rtti_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-frtti"])], + ), + ], +) +swift_nortti_feature = feature( + name = "nortti_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fno-rtti"])], + ), + ], +) + +swift_exceptions_feature = feature( + name = "exceptions_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fexceptions"])], + ), + ], +) +swift_noexceptions_feature = feature( + name = "noexceptions_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fno-exceptions"])], + ), + ], +) + +swift_internal_coding_standard_feature = feature( + name = "internal_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [ + flag_group( + flags = get_flags_for_lang_and_level( + "cxx", + "internal", + _invalid_flags, + _extra_flags, + ), + ), + ], + ), + ], +) + +swift_prod_coding_standard_feature = feature( + name = "prod_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [ + flag_group( + flags = get_flags_for_lang_and_level( + "cxx", + "prod", + _invalid_flags, + _extra_flags, + ), + ), + ], + ), + ], +) + +swift_safe_coding_standard_feature = feature( + name = "safe_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [ + flag_group( + flags = get_flags_for_lang_and_level( + "cxx", + "prod", + _invalid_flags, + _extra_flags, + ), + ), + ], + ), + ], +) + +swift_portable_coding_standard_feature = feature( + name = "portable_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-pedantic"])], + ), + ], +) + +swift_disable_conversion_warning_feature = feature( + name = "disable_conversion_warnings", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = disable_conversion_warning_flags)], + ), + ], +) + +swift_disable_warnings_for_test_targets_feature = feature( + name = "disable_warnings_for_test_targets", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = disable_warnings_for_test_targets_flags)], + ), + ], +) + +stack_protector_feature = feature( + name = "stack_protector", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-fstack-protector"])], + with_features = [ + with_feature_set( + not_features = ["strong_stack_protector"], + ), + ], + ), + ], +) + +strong_stack_protector_feature = feature( + name = "strong_stack_protector", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-fstack-protector-strong"])], + ), + ], +) diff --git a/cc/toolchains/gcc_arm_embedded/config.bzl b/cc/toolchains/gcc_arm_embedded/config.bzl index 1d87402..4daf10c 100644 --- a/cc/toolchains/gcc_arm_embedded/config.bzl +++ b/cc/toolchains/gcc_arm_embedded/config.bzl @@ -1,5 +1,31 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool_path", "with_feature_set") +load( + "swift_custom_features.bzl", + "c11_standard_feature", + "c17_standard_feature", + "c89_standard_feature", + "c90_standard_feature", + "c99_standard_feature", + "cxx11_standard_feature", + "cxx14_standard_feature", + "cxx17_standard_feature", + "cxx20_standard_feature", + "cxx98_standard_feature", + "gnu_extensions_feature", + "swift_relwdbg_feature", + "swift_rtti_feature", + "swift_nortti_feature", + "swift_exceptions_feature", + "swift_noexceptions_feature", + "swift_internal_coding_standard_feature", + "swift_prod_coding_standard_feature", + "swift_safe_coding_standard_feature", + "swift_portable_coding_standard_feature", + "swift_disable_conversion_warning_feature", + "stack_protector_feature", + "strong_stack_protector_feature", +) def _impl(ctx): SDK_PATH_PREFIX = "wrappers/arm-none-eabi-{}" @@ -146,6 +172,39 @@ def _impl(ctx): ], ), opt_feature, + feature( + name = "treat_warnings_as_errors", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-Werror"])], + ), + ], + ) + ] + [ + # append swiftnavs custom features here + gnu_extensions_feature, + c89_standard_feature, + c90_standard_feature, + c99_standard_feature, + c11_standard_feature, + c17_standard_feature, + cxx98_standard_feature, + cxx11_standard_feature, + cxx14_standard_feature, + cxx17_standard_feature, + cxx20_standard_feature, + swift_relwdbg_feature, + swift_rtti_feature, + swift_nortti_feature, + swift_exceptions_feature, + swift_noexceptions_feature, + swift_internal_coding_standard_feature, + swift_prod_coding_standard_feature, + swift_safe_coding_standard_feature, + swift_portable_coding_standard_feature, + stack_protector_feature, + strong_stack_protector_feature, ] return cc_common.create_cc_toolchain_config_info( diff --git a/cc/toolchains/gcc_arm_embedded/swift_custom_features.bzl b/cc/toolchains/gcc_arm_embedded/swift_custom_features.bzl new file mode 100644 index 0000000..a369f81 --- /dev/null +++ b/cc/toolchains/gcc_arm_embedded/swift_custom_features.bzl @@ -0,0 +1,594 @@ +"""Swiftnav custom toolchain features + +Our unix_cc_toolchain_config.bzl is a fork of upstream at: + https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_toolchain_config.bzl + +For the time being we would like to keep this a super set of the features defined there +or pull in fixes and new features without upgrading bazel. + +In order to keep this maintainable all of our custom additions will be added +to this file under the swift_ prefix. + +Bug fixes, ports of features from newer versions, etc.. should be put as they +appear in upstream in unix_cc_toolchain_config.bzl. +""" + +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load( + "@rules_swiftnav//cc/toolchains:gcc_llvm_flags.bzl", + "get_flags_for_lang_and_level", + "disable_conversion_warning_flags", +) +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "feature", + "flag_group", + "flag_set", + "with_feature_set", +) + +_invalid_flags = [ + "-Wmismatched-dealloc", + "-Wsizeof-array-div", + "-Wenum-int-mismatch", + "-Wvla-parameter", + "-Wenum-conversion", + "-Wself-move", + "-Wtautological-unsigned-zero-compare", + # Really overzealous on this toolchain + "-Wunused-const-variable", + "-Wconversion", + "-Wsign-conversion", + # This is just broken on this toolchain + "-Wchar-subscripts", +] + +_extra_flags = [ +] + +_all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, +] + +_all_cpp_compile_actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, +] + +_preprocessor_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.clif_match, +] + +_codegen_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, +] + +_all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +_lto_index_actions = [ + ACTION_NAMES.lto_index_for_executable, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, +] + +gnu_extensions_feature = feature(name="gnu_extensions") + +c89_standard_feature = feature( + name="c89", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c89"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu89"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c90_standard_feature = feature( + name="c90", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c90"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu90"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c99_standard_feature = feature( + name="c99", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c99"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu99"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c11_standard_feature = feature( + name="c11", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c11"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu11"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c17_standard_feature = feature( + name="c17", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c17"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu17"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx98_standard_feature = feature( + name="c++98", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++98"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++98"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx11_standard_feature = feature( + name="c++11", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++0x"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++0x"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx14_standard_feature = feature( + name="c++14", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++14"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++14"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx17_standard_feature = feature( + name="c++17", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++17"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++17"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx20_standard_feature = feature( + name="c++20", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++20"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++20"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + # clang is aggresive about removing features from the standard. + flag_set( + actions=_preprocessor_compile_actions, + flag_groups=( + [ + flag_group( + flags=[ + # This is a workaround for memory_resource being experimental only + # in the llvm-14 libc++ impelmentation. + "-DSWIFTNAV_EXPERIMENTAL_MEMORY_RESOURCE", + "-D_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES", + ], + ), + ] + ), + with_features=[ + with_feature_set(features=["libcpp"]), + ], + ), + ], +) + +swift_relwdbg_feature = feature( + name="relwdbg", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[flag_group(flags=["-O2", "-g"])], + ), + ], +) + +swift_rtti_feature = feature( + name="rtti_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-frtti"])], + ), + ], +) +swift_nortti_feature = feature( + name="nortti_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-fno-rtti"])], + ), + ], +) + +swift_exceptions_feature = feature( + name="exceptions_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-fexceptions"])], + ), + ], +) +swift_noexceptions_feature = feature( + name="noexceptions_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-fno-exceptions"])], + ), + ], +) + +swift_internal_coding_standard_feature = feature( + name="internal_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[ + flag_group( + flags=get_flags_for_lang_and_level( + "cxx", "internal", _invalid_flags, _extra_flags + ) + ) + ], + ), + ], +) + +swift_prod_coding_standard_feature = feature( + name="prod_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[ + flag_group( + flags=get_flags_for_lang_and_level( + "cxx", "prod", _invalid_flags, _extra_flags + ) + ) + ], + ), + ], +) + +swift_safe_coding_standard_feature = feature( + name="safe_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[ + flag_group( + flags=get_flags_for_lang_and_level( + "cxx", "prod", _invalid_flags, _extra_flags + ) + ) + ], + ), + ], +) + +swift_portable_coding_standard_feature = feature( + name="portable_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[flag_group(flags=["-pedantic"])], + ), + ], +) + +swift_disable_conversion_warning_feature = feature( + name="disable_conversion_warnings", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[flag_group(flags=disable_conversion_warning_flags)], + ), + ], +) + + +stack_protector_feature = feature( + name = "stack_protector", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-fstack-protector"])], + with_features = [ + with_feature_set( + not_features = ["strong_stack_protector"], + ), + ], + ), + ], +) + +strong_stack_protector_feature = feature( + name = "strong_stack_protector", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-fstack-protector-strong"])], + ), + ], +) diff --git a/cc/toolchains/gcc_arm_gnu_8_3/config.bzl b/cc/toolchains/gcc_arm_gnu_8_3/config.bzl index f19336e..5ec6177 100644 --- a/cc/toolchains/gcc_arm_gnu_8_3/config.bzl +++ b/cc/toolchains/gcc_arm_gnu_8_3/config.bzl @@ -1,5 +1,29 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "feature", "flag_group", "flag_set", "tool_path", "with_feature_set") +load("swift_custom_features.bzl", + "gnu_extensions_feature", + "c89_standard_feature", + "c90_standard_feature", + "c99_standard_feature", + "c11_standard_feature", + "c17_standard_feature", + "cxx98_standard_feature", + "cxx11_standard_feature", + "cxx14_standard_feature", + "cxx17_standard_feature", + "cxx20_standard_feature", + "swift_relwdbg_feature", + "swift_rtti_feature", + "swift_nortti_feature", + "swift_exceptions_feature", + "swift_noexceptions_feature", + "swift_internal_coding_standard_feature", + "swift_prod_coding_standard_feature", + "swift_safe_coding_standard_feature", + "swift_portable_coding_standard_feature", + "stack_protector_feature", + "strong_stack_protector_feature", +) def _impl(ctx): SDK_PATH_PREFIX = "wrappers/aarch64-linux-gnu-{}" @@ -82,6 +106,7 @@ def _impl(ctx): flags = [ "--sysroot={}".format(ctx.attr.sysroot), "-no-canonical-prefixes", + "-fno-canonical-system-headers", # Reproducibility "-Wno-builtin-macro-redefined", "-D__DATE__=\"redacted\"", @@ -124,7 +149,40 @@ def _impl(ctx): ], ), opt_feature, - ] + feature( + name = "treat_warnings_as_errors", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-Werror"])], + ), + ], + ) + ] + [ + # append swiftnavs custom features here + gnu_extensions_feature, + c89_standard_feature, + c90_standard_feature, + c99_standard_feature, + c11_standard_feature, + c17_standard_feature, + cxx98_standard_feature, + cxx11_standard_feature, + cxx14_standard_feature, + cxx17_standard_feature, + cxx20_standard_feature, + swift_relwdbg_feature, + swift_rtti_feature, + swift_nortti_feature, + swift_exceptions_feature, + swift_noexceptions_feature, + swift_internal_coding_standard_feature, + swift_prod_coding_standard_feature, + swift_safe_coding_standard_feature, + swift_portable_coding_standard_feature, + stack_protector_feature, + strong_stack_protector_feature, + ] return cc_common.create_cc_toolchain_config_info( ctx = ctx, diff --git a/cc/toolchains/gcc_arm_gnu_8_3/swift_custom_features.bzl b/cc/toolchains/gcc_arm_gnu_8_3/swift_custom_features.bzl new file mode 100644 index 0000000..d4f681a --- /dev/null +++ b/cc/toolchains/gcc_arm_gnu_8_3/swift_custom_features.bzl @@ -0,0 +1,598 @@ +"""Swiftnav custom toolchain features + +Our unix_cc_toolchain_config.bzl is a fork of upstream at: + https://github.com/bazelbuild/bazel/blob/master/tools/cpp/unix_cc_toolchain_config.bzl + +For the time being we would like to keep this a super set of the features defined there +or pull in fixes and new features without upgrading bazel. + +In order to keep this maintainable all of our custom additions will be added +to this file under the swift_ prefix. + +Bug fixes, ports of features from newer versions, etc.. should be put as they +appear in upstream in unix_cc_toolchain_config.bzl. +""" + +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load( + "@rules_swiftnav//cc/toolchains:gcc_llvm_flags.bzl", + "get_flags_for_lang_and_level", + "disable_conversion_warning_flags", +) +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "feature", + "flag_group", + "flag_set", + "with_feature_set", +) +_invalid_flags = [ + "-Wmismatched-dealloc", + "-Wsizeof-array-div", + "-Wstring-compare", + "-Wenum-conversion", + "-Wenum-int-mismatch", + "-Wvla-parameter", + "-Wzero-length-bounds", + "-Wself-move", + "-Wtautological-unsigned-zero-compare", + "-Wno-tautological-unsigned-zero-compare", + # Really overzealous on this toolchain, especially with C code + "-Wunused-const-variable", + "-Wconversion", + "-Wsign-conversion", + "-Wfloat-conversion", +] + +_extra_flags = [ + "-Wno-float-conversion", + "-Wno-aggressive-loop-optimizations", +] + + +_all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, +] + +_all_cpp_compile_actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, +] + +_preprocessor_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.clif_match, +] + +_codegen_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, +] + +_all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + +_lto_index_actions = [ + ACTION_NAMES.lto_index_for_executable, + ACTION_NAMES.lto_index_for_dynamic_library, + ACTION_NAMES.lto_index_for_nodeps_dynamic_library, +] + +gnu_extensions_feature = feature(name="gnu_extensions") + +c89_standard_feature = feature( + name="c89", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c89"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu89"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c90_standard_feature = feature( + name="c90", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c90"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu90"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c99_standard_feature = feature( + name="c99", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c99"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu99"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c11_standard_feature = feature( + name="c11", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c11"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu11"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +c17_standard_feature = feature( + name="c17", + provides=["c_standard"], + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c17"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu17"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx98_standard_feature = feature( + name="c++98", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++98"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++98"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx11_standard_feature = feature( + name="c++11", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++0x"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++0x"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx14_standard_feature = feature( + name="c++14", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++14"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++14"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx17_standard_feature = feature( + name="c++17", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++17"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++17"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + ], +) + +cxx20_standard_feature = feature( + name="c++20", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++20"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++20"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + # clang is aggresive about removing features from the standard. + flag_set( + actions=_preprocessor_compile_actions, + flag_groups=( + [ + flag_group( + flags=[ + # This is a workaround for memory_resource being experimental only + # in the llvm-14 libc++ impelmentation. + "-DSWIFTNAV_EXPERIMENTAL_MEMORY_RESOURCE", + "-D_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES", + ], + ), + ] + ), + with_features=[ + with_feature_set(features=["libcpp"]), + ], + ), + ], +) + +swift_relwdbg_feature = feature( + name="relwdbg", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[flag_group(flags=["-O2", "-g"])], + ), + ], +) + +swift_rtti_feature = feature( + name="rtti_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-frtti"])], + ), + ], +) +swift_nortti_feature = feature( + name="nortti_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-fno-rtti"])], + ), + ], +) + +swift_exceptions_feature = feature( + name="exceptions_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-fexceptions"])], + ), + ], +) +swift_noexceptions_feature = feature( + name="noexceptions_feature", + flag_sets=[ + flag_set( + actions=[ACTION_NAMES.cpp_compile], + flag_groups=[flag_group(flags=["-fno-exceptions"])], + ), + ], +) + +swift_internal_coding_standard_feature = feature( + name="internal_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[ + flag_group( + flags=get_flags_for_lang_and_level( + "cxx", "internal", _invalid_flags, _extra_flags + ) + ) + ], + ), + ], +) + +swift_prod_coding_standard_feature = feature( + name="prod_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[ + flag_group( + flags=get_flags_for_lang_and_level( + "cxx", "prod", _invalid_flags, _extra_flags + ) + ) + ], + ), + ], +) + +swift_safe_coding_standard_feature = feature( + name="safe_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[ + flag_group( + flags=get_flags_for_lang_and_level( + "cxx", "prod", _invalid_flags, _extra_flags + ) + ) + ], + ), + ], +) + +swift_portable_coding_standard_feature = feature( + name="portable_coding_standard", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[flag_group(flags=["-pedantic"])], + ), + ], +) + +swift_disable_conversion_warning_feature = feature( + name="disable_conversion_warnings", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[flag_group(flags=disable_conversion_warning_flags)], + ), + ], +) + + +stack_protector_feature = feature( + name = "stack_protector", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-fstack-protector"])], + with_features = [ + with_feature_set( + not_features = ["strong_stack_protector"], + ), + ], + ), + ], +) + +strong_stack_protector_feature = feature( + name = "strong_stack_protector", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-fstack-protector-strong"])], + ), + ], +) diff --git a/cc/toolchains/gcc_llvm_flags.bzl b/cc/toolchains/gcc_llvm_flags.bzl new file mode 100644 index 0000000..d74e48b --- /dev/null +++ b/cc/toolchains/gcc_llvm_flags.bzl @@ -0,0 +1,149 @@ +_internal_coding_standard_flags = { + "common": [ + "-Waddress", + "-Warray-bounds", + "-Wbool-compare", + "-Wbool-operation", + "-Wcast-function-type", + "-Wchar-subscripts", + "-Wdangling-else", + "-Wignored-qualifiers", + "-Winit-self", + "-Winvalid-pch", + "-Wlogical-not-parentheses", + "-Wmaybe-uninitialized", + "-Wmemset-elt-size", + "-Wmemset-transposed-args", + "-Wmisleading-indentation", + "-Wmismatched-dealloc", + "-Wmissing-attributes", + "-Wmissing-field-initializers", + "-Wmultistatement-macros", + "-Wparentheses", + "-Wrestrict", + "-Wsequence-point", + "-Wshift-negative-value", + "-Wsizeof-array-div", + "-Wsizeof-pointer-div", + "-Wsizeof-pointer-memaccess", + "-Wstring-compare", + "-Wtrigraphs", + "-Wtype-limits", + "-Wuninitialized", + "-Wunknown-pragmas", + "-Wwrite-strings", + "-Wno-error=deprecated-declarations", + "-Wno-tautological-unsigned-zero-compare", + "-Wno-stack-protector", + ], + "c": [], + "cxx": [], +} + +_prod_coding_standard_flags = { + "common": _internal_coding_standard_flags["common"] + + [ + "-Wall", + "-Wextra", + "-Wcast-qual", + "-Wcomment", + "-Wclobbered", + "-Wconversion", + "-Wdisabled-optimization", + "-Wempty-body", + "-Wenum-compare", + "-Wfloat-equal", + "-Wenum-conversion", + "-Wenum-int-mismatch", + "-Wfloat-conversion", + "-Wformat", + "-Wformat-extra-args", + "-Wformat-overflow", + "-Wformat-security", + "-Wformat-truncation", + "-Wformat-y2k", + "-Wimplicit-fallthrough", + "-Wimport", + "-Wint-in-bool-context", + "-Wmissing-braces", + "-Wnarrowing", + "-Wnonnull", + "-Wnonnull-compare", + "-Wopenmp-simd", + "-Wredundant-decls", + "-Wreturn-type", + "-Wself-move", + "-Wshadow", + "-Wsign-compare", + "-Wsign-conversion", + "-Wstrict-aliasing", + "-Wstrict-overflow=1", + "-Wswitch", + "-Wswitch-default", + "-Wswitch-enum", + "-Wtautological-compare", + "-Wtautological-unsigned-zero-compare", + "-Wunreachable-code", + "-Wunused-but-set-parameter", + "-Wunused-but-set-variable", + "-Wunused-const-variable", + "-Wunused-function", + "-Wunused-label", + "-Wunused-local-typedefs", + "-Wunused-macros", + "-Wunused-parameter", + "-Wunused-result", + "-Wunused-value", + "-Wunused-variable", + "-Wvla-parameter", + "-Wvolatile-register-var", + "-Wzero-length-bounds", + ], + "c": _internal_coding_standard_flags["c"] + [], + "cxx": _internal_coding_standard_flags["cxx"] + [], +} + +_safe_coding_standard_flags = { + "common": _prod_coding_standard_flags["common"] + + [ + "-Wpointer-arith", + ], + "c": _prod_coding_standard_flags["c"] + [], + "cxx": _prod_coding_standard_flags["cxx"] + [], +} + +_portable_coding_standard_flags = [ + "-pedantic", +] + +disable_conversion_warning_flags = [ + "-Wno-conversion", + "-Wno-float-conversion", + "-Wno-sign-conversion", + "-Wno-implicit-int-conversion", + "-Wno-implicit-int-float-conversion", + "-Wno-shorten-64-to-32", +] + +disable_warnings_for_test_targets_flags = [ + "-Wno-unused", +] + disable_conversion_warning_flags + + +def filter_flags(flags, invalid_flags): + return [f for f in flags if f not in invalid_flags] + + +def get_flags_for_lang_and_level(lang, level, invalid_flags=[], extra_flags=[]): + if level == "internal": + flags = _internal_coding_standard_flags + elif level == "prod": + flags = _prod_coding_standard_flags + elif level == "safe": + flags = _safe_coding_standard_flags + else: + fail + + flags = flags["common"] + flags[lang] + extra_flags + + return filter_flags(flags, invalid_flags) diff --git a/cc/toolchains/llvm/cc_toolchain_config.bzl b/cc/toolchains/llvm/cc_toolchain_config.bzl index 089ca8e..2a38e23 100644 --- a/cc/toolchains/llvm/cc_toolchain_config.bzl +++ b/cc/toolchains/llvm/cc_toolchain_config.bzl @@ -44,7 +44,6 @@ def cc_toolchain_config( "--target=" + target_system_name, # Security "-U_FORTIFY_SOURCE", # https://github.com/google/sanitizers/issues/247 - "-fstack-protector", "-fno-omit-frame-pointer", # Math # This controls whether the compiler allows contracting floating point operations. diff --git a/cc/toolchains/llvm/swift_custom_features.bzl b/cc/toolchains/llvm/swift_custom_features.bzl index 0c1ec89..8684e04 100644 --- a/cc/toolchains/llvm/swift_custom_features.bzl +++ b/cc/toolchains/llvm/swift_custom_features.bzl @@ -21,6 +21,34 @@ load( "flag_set", "with_feature_set", ) +load( + "@rules_swiftnav//cc/toolchains:gcc_llvm_flags.bzl", + "disable_conversion_warning_flags", + "disable_warnings_for_test_targets_flags", + "get_flags_for_lang_and_level", +) + +_invalid_flags = [ + "-Wstring-compare", + "-Wbool-compare", + "-Wmaybe-uninitialized", + "-Wclobbered", + "-Wmemset-elt-size", + "-Wmismatched-dealloc", + "-Wmissing-attributes", + "-Wmultistatement-macros", + "-Wrestrict", + "-Wenum-int-mismatch", + "-Wformat-overflow", + "-Wformat-truncation", + "-Wnonnull-compare", + "-Wopenmp-simd", + "-Wvla-parameter", + "-Wzero-length-bounds", +] + +_extra_flags = [ +] _all_compile_actions = [ ACTION_NAMES.c_compile, @@ -84,22 +112,26 @@ c89_standard_feature = feature( flag_sets = [ flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c89"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c89"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu89"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu89"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -113,22 +145,26 @@ c90_standard_feature = feature( flag_sets = [ flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c90"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c90"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu90"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu90"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -142,22 +178,26 @@ c99_standard_feature = feature( flag_sets = [ flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c99"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c99"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu99"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu99"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -171,22 +211,26 @@ c11_standard_feature = feature( flag_sets = [ flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c11"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c11"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu11"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu11"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -200,22 +244,26 @@ c17_standard_feature = feature( flag_sets = [ flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c17"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c17"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu17"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu17"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -229,22 +277,26 @@ cxx98_standard_feature = feature( flag_sets = [ flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++98"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c++98"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++98"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++98"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -258,22 +310,26 @@ cxx11_standard_feature = feature( flag_sets = [ flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++0x"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c++0x"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++0x"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++0x"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -287,22 +343,26 @@ cxx14_standard_feature = feature( flag_sets = [ flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++14"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c++14"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++14"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++14"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -316,22 +376,26 @@ cxx17_standard_feature = feature( flag_sets = [ flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++17"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c++17"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++17"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++17"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -345,22 +409,26 @@ cxx20_standard_feature = feature( flag_sets = [ flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++20"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=c++20"], + ), + ] + ), with_features = [ with_feature_set(not_features = ["gnu_extensions"]), ], ), flag_set( actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++20"], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = ["-std=gnu++20"], + ), + ] + ), with_features = [ with_feature_set(features = ["gnu_extensions"]), ], @@ -368,16 +436,18 @@ cxx20_standard_feature = feature( # clang is aggresive about removing features from the standard. flag_set( actions = _preprocessor_compile_actions, - flag_groups = ([ - flag_group( - flags = [ - # This is a workaround for memory_resource being experimental only - # in the llvm-14 libc++ impelmentation. - "-DSWIFTNAV_EXPERIMENTAL_MEMORY_RESOURCE", - "-D_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES", - ], - ), - ]), + flag_groups = ( + [ + flag_group( + flags = [ + # This is a workaround for memory_resource being experimental only + # in the llvm-14 libc++ impelmentation. + "-DSWIFTNAV_EXPERIMENTAL_MEMORY_RESOURCE", + "-D_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES", + ], + ), + ] + ), with_features = [ with_feature_set(features = ["libcpp"]), ], @@ -395,28 +465,152 @@ swift_relwdbg_feature = feature( ], ) -# Clang has a set of warnings that are always enabled by default -# which gets noisey for third party code. This feature allows disabling -# these warnings for code we likely never intend to patch to focus on -# warnings we care about. -swift_no_default_warnings = feature( - name = "no_default_warnings", +swift_rtti_feature = feature( + name = "rtti_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-frtti"])], + ), + ], +) +swift_nortti_feature = feature( + name = "nortti_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fno-rtti"])], + ), + ], +) + +swift_exceptions_feature = feature( + name = "exceptions_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fexceptions"])], + ), + ], +) +swift_noexceptions_feature = feature( + name = "noexceptions_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fno-exceptions"])], + ), + ], +) + +swift_internal_coding_standard_feature = feature( + name = "internal_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [ + flag_group( + flags = get_flags_for_lang_and_level( + "cxx", + "internal", + _invalid_flags, + _extra_flags, + ), + ), + ], + ), + ], +) + +swift_prod_coding_standard_feature = feature( + name = "prod_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [ + flag_group( + flags = get_flags_for_lang_and_level( + "cxx", + "prod", + _invalid_flags, + _extra_flags, + ), + ), + ], + ), + ], +) + +swift_safe_coding_standard_feature = feature( + name = "safe_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [ + flag_group( + flags = get_flags_for_lang_and_level( + "cxx", + "prod", + _invalid_flags, + _extra_flags, + ), + ), + ], + ), + ], +) + +swift_portable_coding_standard_feature = feature( + name = "portable_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-pedantic"])], + ), + ], +) + +swift_disable_conversion_warning_feature = feature( + name = "disable_conversion_warnings", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = disable_conversion_warning_flags)], + ), + ], +) + +swift_disable_warnings_for_test_targets_feature = feature( + name = "disable_warnings_for_test_targets", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = disable_warnings_for_test_targets_flags)], + ), + ], +) + +stack_protector_feature = feature( + name = "stack_protector", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-fstack-protector"])], + with_features = [ + with_feature_set( + not_features = ["strong_stack_protector"], + ), + ], + ), + ], +) + +strong_stack_protector_feature = feature( + name = "strong_stack_protector", flag_sets = [ flag_set( actions = _all_compile_actions, - flag_groups = [flag_group(flags = [ - "-Wno-fortify-source", - "-Wno-absolute-value", - "-Wno-format", - "-Wno-deprecated-declarations", - "-Wno-unused-but-set-variable", - "-Wno-pointer-bool-conversion", - "-Wno-unused-variable", - "-Wno-incompatible-pointer-types-discards-qualifiers", - "-Wno-implicit-const-int-float-conversion", - "-Wno-implicit-function-declaration", - "-Wno-mismatched-new-delete", - ])], + flag_groups = [flag_group(flags = ["-fstack-protector-strong"])], ), ], ) diff --git a/cc/toolchains/llvm/unix_cc_toolchain_config.bzl b/cc/toolchains/llvm/unix_cc_toolchain_config.bzl index 2689564..1d66270 100644 --- a/cc/toolchains/llvm/unix_cc_toolchain_config.bzl +++ b/cc/toolchains/llvm/unix_cc_toolchain_config.bzl @@ -41,8 +41,19 @@ load( "cxx20_standard_feature", "cxx98_standard_feature", "gnu_extensions_feature", - "swift_no_default_warnings", "swift_relwdbg_feature", + "swift_rtti_feature", + "swift_nortti_feature", + "swift_exceptions_feature", + "swift_noexceptions_feature", + "swift_internal_coding_standard_feature", + "swift_prod_coding_standard_feature", + "swift_safe_coding_standard_feature", + "swift_portable_coding_standard_feature", + "swift_disable_conversion_warning_feature", + "swift_disable_warnings_for_test_targets_feature", + "stack_protector_feature", + "strong_stack_protector_feature", ) def _target_os_version(ctx): @@ -1453,10 +1464,21 @@ def _impl(ctx): cxx14_standard_feature, cxx17_standard_feature, cxx20_standard_feature, - swift_no_default_warnings, swift_stdlib_feature, swift_libcpp_feature, swift_relwdbg_feature, + swift_rtti_feature, + swift_nortti_feature, + swift_exceptions_feature, + swift_noexceptions_feature, + swift_internal_coding_standard_feature, + swift_prod_coding_standard_feature, + swift_safe_coding_standard_feature, + swift_portable_coding_standard_feature, + swift_disable_conversion_warning_feature, + swift_disable_warnings_for_test_targets_feature, + stack_protector_feature, + strong_stack_protector_feature, ] else: # macOS artifact name patterns differ from the defaults only for dynamic @@ -1509,10 +1531,21 @@ def _impl(ctx): cxx14_standard_feature, cxx17_standard_feature, cxx20_standard_feature, - swift_no_default_warnings, swift_stdlib_feature, swift_libcpp_feature, swift_relwdbg_feature, + swift_rtti_feature, + swift_nortti_feature, + swift_exceptions_feature, + swift_noexceptions_feature, + swift_internal_coding_standard_feature, + swift_prod_coding_standard_feature, + swift_safe_coding_standard_feature, + swift_portable_coding_standard_feature, + swift_disable_conversion_warning_feature, + swift_disable_warnings_for_test_targets_feature, + stack_protector_feature, + stack_protector_feature, ] return cc_common.create_cc_toolchain_config_info( diff --git a/cc/toolchains/llvm20/cc_toolchain_config.bzl b/cc/toolchains/llvm20/cc_toolchain_config.bzl index ad8d957..ff1a3a8 100644 --- a/cc/toolchains/llvm20/cc_toolchain_config.bzl +++ b/cc/toolchains/llvm20/cc_toolchain_config.bzl @@ -44,7 +44,6 @@ def cc_toolchain_config( "--target=" + target_system_name, # Security "-U_FORTIFY_SOURCE", # https://github.com/google/sanitizers/issues/247 - "-fstack-protector", "-fno-omit-frame-pointer", # Math # This controls whether the compiler allows contracting floating point operations. diff --git a/cc/toolchains/llvm20/swift_custom_features.bzl b/cc/toolchains/llvm20/swift_custom_features.bzl index 0c1ec89..115981a 100644 --- a/cc/toolchains/llvm20/swift_custom_features.bzl +++ b/cc/toolchains/llvm20/swift_custom_features.bzl @@ -21,6 +21,31 @@ load( "flag_set", "with_feature_set", ) +load( + "@rules_swiftnav//cc/toolchains:gcc_llvm_flags.bzl", + "disable_conversion_warning_flags", + "disable_warnings_for_test_targets_flags", + "get_flags_for_lang_and_level", +) + +_invalid_flags = [ + "-Wbool-compare", + "-Wmaybe-uninitialized", + "-Wmemset-elt-size", + "-Wmismatched-dealloc", + "-Wmissing-attributes", + "-Wmultistatement-macros", + "-Wrestrict", + "-Wclobbered", + "-Wenum-int-mismatch", + "-Wnonnull-compare", + "-Wopenmp-simd", + "-Wvla-parameter", + "-Wzero-length-bounds", +] + +_extra_flags = [ +] _all_compile_actions = [ ACTION_NAMES.c_compile, @@ -76,347 +101,513 @@ _lto_index_actions = [ ACTION_NAMES.lto_index_for_nodeps_dynamic_library, ] -gnu_extensions_feature = feature(name = "gnu_extensions") +gnu_extensions_feature = feature(name="gnu_extensions") c89_standard_feature = feature( - name = "c89", - provides = ["c_standard"], - flag_sets = [ + name="c89", + provides=["c_standard"], + flag_sets=[ flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c89"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c89"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu89"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu89"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) c90_standard_feature = feature( - name = "c90", - provides = ["c_standard"], - flag_sets = [ + name="c90", + provides=["c_standard"], + flag_sets=[ flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c90"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c90"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu90"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu90"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) c99_standard_feature = feature( - name = "c99", - provides = ["c_standard"], - flag_sets = [ + name="c99", + provides=["c_standard"], + flag_sets=[ flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c99"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c99"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu99"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu99"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) c11_standard_feature = feature( - name = "c11", - provides = ["c_standard"], - flag_sets = [ + name="c11", + provides=["c_standard"], + flag_sets=[ flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c11"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c11"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu11"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu11"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) c17_standard_feature = feature( - name = "c17", - provides = ["c_standard"], - flag_sets = [ + name="c17", + provides=["c_standard"], + flag_sets=[ flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=c17"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=c17"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = [ACTION_NAMES.c_compile], - flag_groups = ([ - flag_group( - flags = ["-std=gnu17"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=[ACTION_NAMES.c_compile], + flag_groups=( + [ + flag_group( + flags=["-std=gnu17"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) cxx98_standard_feature = feature( - name = "c++98", - provides = ["cxx_standard"], - flag_sets = [ + name="c++98", + provides=["cxx_standard"], + flag_sets=[ flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++98"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++98"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++98"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++98"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) cxx11_standard_feature = feature( - name = "c++11", - provides = ["cxx_standard"], - flag_sets = [ + name="c++11", + provides=["cxx_standard"], + flag_sets=[ flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++0x"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++0x"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++0x"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++0x"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) cxx14_standard_feature = feature( - name = "c++14", - provides = ["cxx_standard"], - flag_sets = [ + name="c++14", + provides=["cxx_standard"], + flag_sets=[ flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++14"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++14"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++14"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++14"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) cxx17_standard_feature = feature( - name = "c++17", - provides = ["cxx_standard"], - flag_sets = [ + name="c++17", + provides=["cxx_standard"], + flag_sets=[ flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=c++17"], - ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++17"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), ], ), flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ - flag_group( - flags = ["-std=gnu++17"], - ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++17"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), ], ), ], ) cxx20_standard_feature = feature( - name = "c++20", - provides = ["cxx_standard"], + name="c++20", + provides=["cxx_standard"], + flag_sets=[ + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=c++20"], + ), + ] + ), + with_features=[ + with_feature_set(not_features=["gnu_extensions"]), + ], + ), + flag_set( + actions=_all_cpp_compile_actions, + flag_groups=( + [ + flag_group( + flags=["-std=gnu++20"], + ), + ] + ), + with_features=[ + with_feature_set(features=["gnu_extensions"]), + ], + ), + # clang is aggresive about removing features from the standard. + flag_set( + actions=_preprocessor_compile_actions, + flag_groups=( + [ + flag_group( + flags=[ + # This is a workaround for memory_resource being experimental only + # in the llvm-14 libc++ impelmentation. + "-DSWIFTNAV_EXPERIMENTAL_MEMORY_RESOURCE", + "-D_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES", + ], + ), + ] + ), + with_features=[ + with_feature_set(features=["libcpp"]), + ], + ), + ], +) + +swift_relwdbg_feature = feature( + name="relwdbg", + flag_sets=[ + flag_set( + actions=_all_compile_actions, + flag_groups=[flag_group(flags=["-O2", "-g"])], + ), + ], +) + +swift_rtti_feature = feature( + name = "rtti_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-frtti"])], + ), + ], +) +swift_nortti_feature = feature( + name = "nortti_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fno-rtti"])], + ), + ], +) + +swift_exceptions_feature = feature( + name = "exceptions_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fexceptions"])], + ), + ], +) +swift_noexceptions_feature = feature( + name = "noexceptions_feature", + flag_sets = [ + flag_set( + actions = [ACTION_NAMES.cpp_compile], + flag_groups = [flag_group(flags = ["-fno-exceptions"])], + ), + ], +) + +swift_internal_coding_standard_feature = feature( + name = "internal_coding_standard", flag_sets = [ flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ + actions = _all_compile_actions, + flag_groups = [ flag_group( - flags = ["-std=c++20"], + flags = get_flags_for_lang_and_level( + "cxx", + "internal", + _invalid_flags, + _extra_flags, + ), ), - ]), - with_features = [ - with_feature_set(not_features = ["gnu_extensions"]), ], ), + ], +) + +swift_prod_coding_standard_feature = feature( + name = "prod_coding_standard", + flag_sets = [ flag_set( - actions = _all_cpp_compile_actions, - flag_groups = ([ + actions = _all_compile_actions, + flag_groups = [ flag_group( - flags = ["-std=gnu++20"], + flags = get_flags_for_lang_and_level( + "cxx", + "prod", + _invalid_flags, + _extra_flags, + ), ), - ]), - with_features = [ - with_feature_set(features = ["gnu_extensions"]), ], ), - # clang is aggresive about removing features from the standard. + ], +) + +swift_safe_coding_standard_feature = feature( + name = "safe_coding_standard", + flag_sets = [ flag_set( - actions = _preprocessor_compile_actions, - flag_groups = ([ + actions = _all_compile_actions, + flag_groups = [ flag_group( - flags = [ - # This is a workaround for memory_resource being experimental only - # in the llvm-14 libc++ impelmentation. - "-DSWIFTNAV_EXPERIMENTAL_MEMORY_RESOURCE", - "-D_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES", - ], + flags = get_flags_for_lang_and_level( + "cxx", + "prod", + _invalid_flags, + _extra_flags, + ), ), - ]), - with_features = [ - with_feature_set(features = ["libcpp"]), ], ), ], ) -swift_relwdbg_feature = feature( - name = "relwdbg", +swift_portable_coding_standard_feature = feature( + name = "portable_coding_standard", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = ["-pedantic"])], + ), + ], +) + +swift_disable_conversion_warning_feature = feature( + name = "disable_conversion_warnings", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = disable_conversion_warning_flags)], + ), + ], +) + +swift_disable_warnings_for_test_targets_feature = feature( + name = "disable_warnings_for_test_targets", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [flag_group(flags = disable_warnings_for_test_targets_flags)], + ), + ], +) + +stack_protector_feature = feature( + name = "stack_protector", flag_sets = [ flag_set( actions = _all_compile_actions, - flag_groups = [flag_group(flags = ["-O2", "-g"])], + flag_groups = [flag_group(flags = ["-fstack-protector"])], + with_features = [ + with_feature_set( + not_features = ["strong_stack_protector"], + ), + ], ), ], ) -# Clang has a set of warnings that are always enabled by default -# which gets noisey for third party code. This feature allows disabling -# these warnings for code we likely never intend to patch to focus on -# warnings we care about. -swift_no_default_warnings = feature( - name = "no_default_warnings", +strong_stack_protector_feature = feature( + name = "strong_stack_protector", flag_sets = [ flag_set( actions = _all_compile_actions, - flag_groups = [flag_group(flags = [ - "-Wno-fortify-source", - "-Wno-absolute-value", - "-Wno-format", - "-Wno-deprecated-declarations", - "-Wno-unused-but-set-variable", - "-Wno-pointer-bool-conversion", - "-Wno-unused-variable", - "-Wno-incompatible-pointer-types-discards-qualifiers", - "-Wno-implicit-const-int-float-conversion", - "-Wno-implicit-function-declaration", - "-Wno-mismatched-new-delete", - ])], + flag_groups = [flag_group(flags = ["-fstack-protector-strong"])], ), ], ) diff --git a/cc/toolchains/llvm20/unix_cc_toolchain_config.bzl b/cc/toolchains/llvm20/unix_cc_toolchain_config.bzl index 2689564..1297646 100644 --- a/cc/toolchains/llvm20/unix_cc_toolchain_config.bzl +++ b/cc/toolchains/llvm20/unix_cc_toolchain_config.bzl @@ -41,8 +41,19 @@ load( "cxx20_standard_feature", "cxx98_standard_feature", "gnu_extensions_feature", - "swift_no_default_warnings", "swift_relwdbg_feature", + "swift_rtti_feature", + "swift_nortti_feature", + "swift_exceptions_feature", + "swift_noexceptions_feature", + "swift_internal_coding_standard_feature", + "swift_prod_coding_standard_feature", + "swift_safe_coding_standard_feature", + "swift_portable_coding_standard_feature", + "swift_disable_conversion_warning_feature", + "swift_disable_warnings_for_test_targets_feature", + "stack_protector_feature", + "strong_stack_protector_feature", ) def _target_os_version(ctx): @@ -1453,10 +1464,21 @@ def _impl(ctx): cxx14_standard_feature, cxx17_standard_feature, cxx20_standard_feature, - swift_no_default_warnings, swift_stdlib_feature, swift_libcpp_feature, swift_relwdbg_feature, + swift_rtti_feature, + swift_nortti_feature, + swift_exceptions_feature, + swift_noexceptions_feature, + swift_internal_coding_standard_feature, + swift_prod_coding_standard_feature, + swift_safe_coding_standard_feature, + swift_portable_coding_standard_feature, + swift_disable_conversion_warning_feature, + swift_disable_warnings_for_test_targets_feature, + stack_protector_feature, + strong_stack_protector_feature, ] else: # macOS artifact name patterns differ from the defaults only for dynamic @@ -1509,7 +1531,6 @@ def _impl(ctx): cxx14_standard_feature, cxx17_standard_feature, cxx20_standard_feature, - swift_no_default_warnings, swift_stdlib_feature, swift_libcpp_feature, swift_relwdbg_feature, diff --git a/clang_tidy/clang_tidy.bzl b/clang_tidy/clang_tidy.bzl index f522d96..615cd39 100644 --- a/clang_tidy/clang_tidy.bzl +++ b/clang_tidy/clang_tidy.bzl @@ -1,6 +1,6 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") -load("//cc:defs.bzl", "BINARY", "LIBRARY") +load("//cc:defs.bzl", "BINARY", "LIBRARY", "TEST_LIBRARY") load("//cc_files:get_cc_files.bzl", "get_cc_srcs") def _flatten(input_list): @@ -150,6 +150,9 @@ def _clang_tidy_aspect_impl(target, ctx): if not LIBRARY in tags and not BINARY in tags: return [] + if TEST_LIBRARY in tags: + return [] + srcs = get_cc_srcs(ctx) wrapper = ctx.attr._clang_tidy_wrapper.files_to_run diff --git a/platforms/BUILD.bazel b/platforms/BUILD.bazel index fde54ec..890fc9d 100644 --- a/platforms/BUILD.bazel +++ b/platforms/BUILD.bazel @@ -211,3 +211,14 @@ platform( ], visibility = ["//visibility:public"], ) + +# GCC 11 provided by the host system +platform( + name = "gcc_11_system", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + "@rules_swiftnav//cc/constraints:gcc_11_system_toolchain", + ], + visibility = ["//visibility:public"], +)