Skip to content

Commit

Permalink
add standard override strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jungleraptor committed Mar 15, 2023
1 parent 16989f7 commit 9f94824
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
33 changes: 32 additions & 1 deletion cc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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"],
)
24 changes: 16 additions & 8 deletions cc/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 9f94824

Please sign in to comment.