Skip to content

Commit

Permalink
Add feature to set cxx standard
Browse files Browse the repository at this point in the history
  • Loading branch information
jungleraptor committed Oct 26, 2022
1 parent b098434 commit 3cbda3a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
1 change: 1 addition & 0 deletions tools/cpp/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ cc_toolchain_config(
opt_compile_flags = [%{opt_compile_flags}],
dbg_compile_flags = [%{dbg_compile_flags}],
cxx_flags = [%{cxx_flags}],
cxx_standard = "%{cxx_standard}",
link_flags = [%{link_flags}],
link_libs = [%{link_libs}],
opt_link_flags = [%{opt_link_flags}],
Expand Down
23 changes: 11 additions & 12 deletions tools/cpp/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,6 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
},
)

cxx_opts = split_escaped(get_env_var(
repository_ctx,
"BAZEL_CXXOPTS",
"-std=c++0x",
False,
), ":")

use_libcpp = darwin or bsd
bazel_linklibs = "-lc++:-lm" if use_libcpp else "-lstdc++:-lm"
bazel_linkopts = ""
Expand Down Expand Up @@ -447,12 +440,12 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
coverage_compile_flags, coverage_link_flags = _coverage_flags(repository_ctx, darwin)
builtin_include_directories = _uniq(
_get_cxx_include_directories(repository_ctx, cc, "-xc") +
_get_cxx_include_directories(repository_ctx, cc, "-xc++", cxx_opts) +
_get_cxx_include_directories(repository_ctx, cc, "-xc++") +
_get_cxx_include_directories(
repository_ctx,
cc,
"-xc++",
cxx_opts + ["-stdlib=libc++"],
["-stdlib=libc++"],
) +
_get_cxx_include_directories(
repository_ctx,
Expand All @@ -464,13 +457,13 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
repository_ctx,
cc,
"-xc++",
cxx_opts + _get_no_canonical_prefixes_opt(repository_ctx, cc),
_get_no_canonical_prefixes_opt(repository_ctx, cc),
) +
_get_cxx_include_directories(
repository_ctx,
cc,
"-xc++",
cxx_opts + _get_no_canonical_prefixes_opt(repository_ctx, cc) + ["-stdlib=libc++"],
_get_no_canonical_prefixes_opt(repository_ctx, cc) + ["-stdlib=libc++"],
),
)

Expand Down Expand Up @@ -528,6 +521,12 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
cpu_value,
False,
)),
"%{cxx_standard}": escape_string(get_env_var(
repository_ctx,
"BAZEL_CXXOPTS",
"-std=c++0x",
False,
)),
"%{target_system_name}": escape_string(get_env_var(
repository_ctx,
"BAZEL_TARGET_SYSTEM",
Expand Down Expand Up @@ -560,7 +559,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
"-fno-omit-frame-pointer",
],
),
"%{cxx_flags}": get_starlark_list(cxx_opts + _escaped_cplus_include_paths(repository_ctx)),
"%{cxx_flags}": get_starlark_list(_escaped_cplus_include_paths(repository_ctx)),
"%{link_flags}": get_starlark_list((
["-fuse-ld=" + gold_or_lld_linker_path] if gold_or_lld_linker_path else []
) + _add_linker_option_if_supported(
Expand Down
84 changes: 84 additions & 0 deletions tools/cpp/unix_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ load(
"with_feature_set",
)
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
"@bazel_tools//tools/cpp:lib_cc_configure.bzl",
"get_env_var",
"split_escaped",
)

def layering_check_features(compiler):
if compiler != "clang":
Expand Down Expand Up @@ -144,6 +149,7 @@ lto_index_actions = [
]

def _impl(ctx):
print("I'm HERE")
tool_paths = [
tool_path(name = name, path = path)
for name, path in ctx.attr.tool_paths.items()
Expand Down Expand Up @@ -226,6 +232,75 @@ def _impl(ctx):
],
)

default_cxx_standard_feature = feature(
name = "default_cxx_standard",
enabled = True,
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = ([
flag_group(
flags = [ctx.attr.cxx_standard],
),
]),
with_features = [
with_feature_set(
not_features = [
"cxx14_standard",
"cxx17_standard",
"cxx20_standard",
],
),
],
),
],
)

cxx14_standard_feature = feature(
name = "cxx14_standard",
provides = ["cxx_standard"],
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = ([
flag_group(
flags = ["-std=c++14"],
),
]),
),
],
)

cxx17_standard_feature = feature(
name = "cxx17_standard",
provides = ["cxx_standard"],
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = ([
flag_group(
flags = ["-std=c++17"],
),
]),
),
],
)

cxx20_standard_feature = feature(
name = "cxx20_standard",
provides = ["cxx_standard"],
flag_sets = [
flag_set(
actions = all_cpp_compile_actions,
flag_groups = ([
flag_group(
flags = ["-std=c++20"],
),
]),
),
],
)

default_link_flags_feature = feature(
name = "default_link_flags",
enabled = True,
Expand Down Expand Up @@ -1271,6 +1346,10 @@ def _impl(ctx):
] if ctx.attr.supports_start_end_lib else []
) + [
default_compile_flags_feature,
default_cxx_standard_feature,
cxx14_standard_feature,
cxx17_standard_feature,
cxx20_standard_feature,
default_link_flags_feature,
libraries_to_link_feature,
user_link_flags_feature,
Expand Down Expand Up @@ -1307,6 +1386,10 @@ def _impl(ctx):
) + [
coverage_feature,
default_compile_flags_feature,
default_cxx_standard_feature,
cxx14_standard_feature,
cxx17_standard_feature,
cxx20_standard_feature,
default_link_flags_feature,
user_link_flags_feature,
default_link_libs_feature,
Expand Down Expand Up @@ -1356,6 +1439,7 @@ cc_toolchain_config = rule(
"dbg_compile_flags": attr.string_list(),
"opt_compile_flags": attr.string_list(),
"cxx_flags": attr.string_list(),
"cxx_standard": attr.string(mandatory = True),
"link_flags": attr.string_list(),
"archive_flags": attr.string_list(),
"link_libs": attr.string_list(),
Expand Down

0 comments on commit 3cbda3a

Please sign in to comment.