From 841b61e3642b208a8400ac9dbbb3e2eb8b9685cb Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 15 Mar 2023 17:00:09 -0600 Subject: [PATCH] add standard override strings --- cc/BUILD.bazel | 33 ++++++++++++++++++++++++++++++++- cc/defs.bzl | 24 ++++++++++++++++-------- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/cc/BUILD.bazel b/cc/BUILD.bazel index 1301fc1..8d23f41 100644 --- a/cc/BUILD.bazel +++ b/cc/BUILD.bazel @@ -8,13 +8,14 @@ # EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. -load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag") exports_files( glob(["*.bzl"]), visibility = ["//visibility:public"], ) +# Disable tests and test libraries with --@rules_swiftnav//:disable_test=true bool_flag( name = "disable_tests", build_setting_default = False, @@ -27,6 +28,7 @@ config_setting( visibility = ["//visibility:public"], ) +# Enable exceptions with --@rules_swiftnav//:enable_exceptions=true bool_flag( name = "enable_exceptions", build_setting_default = False, @@ -39,6 +41,7 @@ config_setting( visibility = ["//visibility:public"], ) +# Enable rtti with --@rules_swiftnav//:enable_exceptions=true bool_flag( name = "enable_rtti", build_setting_default = False, @@ -50,3 +53,31 @@ config_setting( flag_values = {":enable_rtti": "true"}, visibility = ["//visibility:public"], ) + +# Allows us to experiment with building the codebase with different standards. +string_flag( + name = "cxx_standard", + build_setting_default = "", #inactive by default + visibility = ["//visibility:public"], +) + +# Enable with --@rules_swiftnav//:cxx_standard=17 +config_setting( + name = "cxx17", + flag_values = {":cxx_standard": "17"}, + visibility = ["//visibility:public"], +) + +# Enable with --@rules_swiftnav//:cxx_standard=20 +config_setting( + name = "cxx20", + flag_values = {":cxx_standard": "20"}, + visibility = ["//visibility:public"], +) + +# Enable with --@rules_swiftnav//:cxx_standard=23 +config_setting( + name = "cxx23", + flag_values = {":cxx_standard": "23"}, + visibility = ["//visibility:public"], +) diff --git a/cc/defs.bzl b/cc/defs.bzl index 8ee4d23..3fd83d7 100644 --- a/cc/defs.bzl +++ b/cc/defs.bzl @@ -35,11 +35,15 @@ TEST = "test" # Name for test sources TEST_SRCS = "test_srcs" -# Options for setting the c standard +# Form the c standard string def _c_standard(extensions = False, standard = 99): extensions = "gnu" if extensions else "c" return ["-std={}{}".format(extensions, standard)] +# Form the c++ standard string. +def _cxx_standard(default, override): + return default if not override else "-std=c++{}".format(override) + # Options common to both c and c++ code def _common_cc_opts(nocopts, pedantic = False): return select({ @@ -48,14 +52,18 @@ def _common_cc_opts(nocopts, pedantic = False): }) + ["-pedantic"] if pedantic else [] # Options specific to c++ code (exceptions, rtti, etc..) -def _common_cxx_opts(exceptions = False, rtti = False, standard = 14): - standard = "-std=c++{}".format(standard) - return [standard] + select({ +def _common_cxx_opts(exceptions = False, rtti = False, standard = None): + return select({ Label("//cc:_enable_exceptions"): ["-fexceptions"], "//conditions:default": ["-fno-exceptions" if not exceptions else "-fexceptions"], }) + select({ Label("//cc:_enable_rtti"): ["-frtti"], "//conditions:default": ["-fno-rtti" if not rtti else "-frtti"], + }) + select({ + Label("//cc:cxx17"): [_cxx_standard("-std=c++17", standard)], + Label("//cc:cxx20"): [_cxx_standard("-std=c++20", standard)], + Label("//cc:cxx23"): [_cxx_standard("-std=c++23", standard)], + "//conditions:default": [_cxx_standard("-std=c++14", standard)], }) # Handle various nuances of local include paths @@ -195,7 +203,7 @@ def swift_cc_library(**kwargs): exceptions = kwargs.pop("exceptions", False) rtti = kwargs.pop("rtti", False) - standard = kwargs.pop("standard", 14) + standard = kwargs.pop("standard", None) cxxopts = _common_cxx_opts(exceptions, rtti, standard) @@ -288,7 +296,7 @@ def swift_cc_tool_library(**kwargs): exceptions = kwargs.pop("exceptions", False) rtti = kwargs.pop("rtti", False) - standard = kwargs.pop("standard", 14) + standard = kwargs.pop("standard", None) cxxopts = _common_cxx_opts(exceptions, rtti, standard) @@ -378,7 +386,7 @@ def swift_cc_binary(**kwargs): exceptions = kwargs.pop("exceptions", False) rtti = kwargs.pop("rtti", False) - standard = kwargs.pop("standard", 14) + standard = kwargs.pop("standard", None) cxxopts = _common_cxx_opts(exceptions, rtti, standard) @@ -465,7 +473,7 @@ def swift_cc_tool(**kwargs): exceptions = kwargs.pop("exceptions", False) rtti = kwargs.pop("rtti", False) - standard = kwargs.pop("standard", 14) + standard = kwargs.pop("standard", None) cxxopts = _common_cxx_opts(exceptions, rtti, standard)