aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZach Yu <zachyu@google.com>2023-07-07 13:25:39 -0700
committerZach Yu <zachyu@google.com>2023-07-07 13:25:39 -0700
commit82363d90fdca800d71831cda0409bd0e0d5724f0 (patch)
tree345fe6584d7f0273cd07afd0f162852d52a629c8
parenta20c893266723576f22a54f467549f13c1dac7bd (diff)
downloadbazel-emu-33-dev.tar.gz
Add opt and dbg features.emu-33-dev
This enables build modes with different flags. To make it consistent with current emulator builds, debug symbols are being generated in all modes (enabled in bazelrc). Change-Id: Ic7e2130b5043c107a1bcc3e9d25708908eecd407
-rw-r--r--tool.bazelrc8
-rw-r--r--toolchains/cc/linux_clang/features.bzl71
-rw-r--r--toolchains/cc/mac_clang/features.bzl50
-rw-r--r--toolchains/cc/windows_clang/features.bzl55
4 files changed, 173 insertions, 11 deletions
diff --git a/tool.bazelrc b/tool.bazelrc
index f8f24685..c35cae9a 100644
--- a/tool.bazelrc
+++ b/tool.bazelrc
@@ -5,8 +5,12 @@ build --incompatible_enable_cc_toolchain_resolution
# (https://github.com/bazelbuild/bazel/issues/17673)
build --strict_proto_deps=off
-build:windows --features='-supports_start_end_lib'
-
build:linux --host_platform='//build/bazel/platforms:host_clang'
+build:linux --features='generate_debug_symbols'
+
build:macos --host_platform='//build/bazel/platforms:host_clang'
+build:macos --features='generate_debug_symbols'
+
build:windows --host_platform='//build/bazel/platforms:host_clang-cl'
+build:windows --features='-supports_start_end_lib'
+build:windows --features='generate_pdb_file'
diff --git a/toolchains/cc/linux_clang/features.bzl b/toolchains/cc/linux_clang/features.bzl
index d3c611c7..331176e5 100644
--- a/toolchains/cc/linux_clang/features.bzl
+++ b/toolchains/cc/linux_clang/features.bzl
@@ -559,7 +559,6 @@ force_pic_feature = feature(
# https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/cpp/CppActionConfigs.java;drc=feea781b30788997c0b97ad9103a13fdc3f627c8;l=924
strip_debug_symbols_feature = feature(
name = "strip_debug_symbols",
- enabled = True,
flag_sets = [
flag_set(
actions = LINK_ACTIONS,
@@ -652,6 +651,66 @@ compiler_output_feature = feature(
],
)
+generate_debug_symbols_feature = feature(
+ name = "generate_debug_symbols",
+ flag_sets = [
+ flag_set(
+ actions = C_COMPILE_ACTIONS + CPP_COMPILE_ACTIONS,
+ flag_groups = [
+ flag_group(flags = ["-g"]),
+ ],
+ ),
+ ],
+)
+
+opt_feature = feature(
+ name = "opt",
+ flag_sets = [
+ flag_set(
+ actions = C_COMPILE_ACTIONS + CPP_COMPILE_ACTIONS,
+ flag_groups = [
+ flag_group(flags = [
+ "-O2",
+ # Buffer overrun detection.
+ "-D_FORTIFY_SOURCE=1",
+ # Disable assertions
+ "-DNDEBUG",
+ # Allow removal of unused sections and code folding at link
+ # time.
+ "-ffunction-sections",
+ "-fdata-sections",
+ # Needed by --icf=safe
+ "-faddrsig",
+ ]),
+ ],
+ ),
+ flag_set(
+ actions = LINK_ACTIONS,
+ flag_groups = [
+ flag_group(flags = [
+ "-Wl,--gc-sections",
+ "-Wl,--icf=safe",
+ ]),
+ ],
+ ),
+ ],
+)
+
+dbg_feature = feature(
+ name = "dbg",
+ flag_sets = [
+ flag_set(
+ actions = C_COMPILE_ACTIONS + CPP_COMPILE_ACTIONS,
+ flag_groups = [
+ flag_group(flags = [
+ "-O0",
+ ]),
+ ],
+ ),
+ ],
+ implies = ["generate_debug_symbols"],
+)
+
def _cc_features_impl(ctx):
import_config = toolchain_import_configs(
ctx.attr.toolchain_imports,
@@ -682,15 +741,21 @@ def _cc_features_impl(ctx):
lib_search_paths_feature,
get_toolchain_lib_search_paths_feature(import_config),
archiver_flags_feature,
+ generate_debug_symbols_feature,
+ # Start flag ordering: the order of following features impacts how
+ # flags override each other.
+ opt_feature,
+ dbg_feature,
libraries_to_link_feature,
- get_toolchain_libraries_to_link_feature(import_config),
- force_pic_feature,
get_toolchain_link_flags_feature(ctx.attr.link_flags),
user_link_flags_feature,
+ get_toolchain_libraries_to_link_feature(import_config),
+ force_pic_feature,
strip_debug_symbols_feature,
get_toolchain_compile_flags_feature(ctx.attr.compile_flags),
get_toolchain_cxx_flags_feature(ctx.attr.cxx_flags),
user_compile_flags_feature,
+ ### End flag ordering ##
sysroot_feature,
linker_param_file_feature,
compiler_input_feature,
diff --git a/toolchains/cc/mac_clang/features.bzl b/toolchains/cc/mac_clang/features.bzl
index 934bd074..c45228f1 100644
--- a/toolchains/cc/mac_clang/features.bzl
+++ b/toolchains/cc/mac_clang/features.bzl
@@ -14,6 +14,8 @@ load(
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
"@//build/bazel/toolchains/cc:actions.bzl",
+ "CPP_COMPILE_ACTIONS",
+ "C_COMPILE_ACTIONS",
"LINK_ACTIONS",
)
load(
@@ -44,7 +46,9 @@ load(
"archiver_flags_feature",
"compiler_input_feature",
"compiler_output_feature",
+ "dbg_feature",
"dependency_file_feature",
+ "generate_debug_symbols_feature",
"get_toolchain_include_paths_feature",
"get_toolchain_lib_search_paths_feature",
"get_toolchain_libraries_to_link_feature",
@@ -78,8 +82,7 @@ rpath_feature = feature(
],
),
],
- expand_if_available =
- "runtime_library_search_directories",
+ expand_if_available = "runtime_library_search_directories",
),
],
),
@@ -239,7 +242,6 @@ force_pic_feature = feature(
flag_groups = [
flag_group(
expand_if_available = "force_pic",
- iterate_over = "user_link_flags",
flags = ["-Wl,-pie"],
),
],
@@ -247,6 +249,38 @@ force_pic_feature = feature(
],
)
+opt_feature = feature(
+ name = "opt",
+ flag_sets = [
+ flag_set(
+ actions = C_COMPILE_ACTIONS + CPP_COMPILE_ACTIONS,
+ flag_groups = [
+ flag_group(flags = [
+ "-O2",
+ # Buffer overrun detection.
+ "-D_FORTIFY_SOURCE=1",
+ # Disable assertions
+ "-DNDEBUG",
+ # Allow removal of unused sections at link time.
+ "-ffunction-sections",
+ "-fdata-sections",
+ # Needed by --icf=safe
+ "-faddrsig",
+ ]),
+ ],
+ ),
+ flag_set(
+ actions = LINK_ACTIONS,
+ flag_groups = [
+ flag_group(flags = [
+ "-Wl,-dead_strip",
+ "-Wl,--icf=safe",
+ ]),
+ ],
+ ),
+ ],
+)
+
def _cc_features_impl(ctx):
import_config = toolchain_import_configs(
ctx.attr.toolchain_imports,
@@ -277,15 +311,21 @@ def _cc_features_impl(ctx):
lib_search_paths_feature,
get_toolchain_lib_search_paths_feature(import_config),
archiver_flags_feature,
+ generate_debug_symbols_feature,
+ # Start flag ordering: the order of following features impacts how
+ # flags override each other.
+ opt_feature,
+ dbg_feature,
libraries_to_link_feature,
- get_toolchain_libraries_to_link_feature(import_config),
- force_pic_feature,
get_toolchain_link_flags_feature(ctx.attr.link_flags),
user_link_flags_feature,
+ get_toolchain_libraries_to_link_feature(import_config),
+ force_pic_feature,
strip_debug_symbols_feature,
get_toolchain_compile_flags_feature(ctx.attr.compile_flags),
get_toolchain_cxx_flags_feature(ctx.attr.cxx_flags),
user_compile_flags_feature,
+ ### End flag ordering ##
sysroot_feature,
linker_param_file_feature,
compiler_input_feature,
diff --git a/toolchains/cc/windows_clang/features.bzl b/toolchains/cc/windows_clang/features.bzl
index 62fa262f..df851494 100644
--- a/toolchains/cc/windows_clang/features.bzl
+++ b/toolchains/cc/windows_clang/features.bzl
@@ -119,6 +119,25 @@ copy_dynamic_libraries_to_binary_feature = feature(
enabled = True,
)
+dbg_feature = feature(
+ name = "dbg",
+ flag_sets = [
+ flag_set(
+ actions = C_COMPILE_ACTIONS + CPP_COMPILE_ACTIONS,
+ flag_groups = [
+ flag_group(flags = ["/Od", "/Z7"]),
+ ],
+ ),
+ flag_set(
+ actions = LINK_ACTIONS,
+ flag_groups = [
+ flag_group(flags = ["/INCREMENTAL:NO"]),
+ ],
+ ),
+ ],
+ implies = ["generate_pdb_file"],
+)
+
def_file_feature = feature(
name = "def_file",
enabled = True,
@@ -452,6 +471,35 @@ libraries_to_link_feature = feature(
no_windows_export_all_symbols_feature = feature(name = "no_windows_export_all_symbols")
+opt_feature = feature(
+ name = "opt",
+ flag_sets = [
+ flag_set(
+ actions = C_COMPILE_ACTIONS + CPP_COMPILE_ACTIONS,
+ flag_groups = [
+ flag_group(flags = [
+ "/O2",
+ # Allow removal of unused sections and code folding at link
+ # time.
+ "/Gy",
+ "/Gw",
+ ]),
+ ],
+ ),
+ flag_set(
+ actions = LINK_ACTIONS,
+ flag_groups = [
+ flag_group(flags = [
+ # Control flow guards
+ "/GUARD:CF",
+ "/OPT:REF",
+ "/OPT:ICF",
+ ]),
+ ],
+ ),
+ ],
+)
+
output_execpath_feature = feature(
name = "output_execpath_flags",
enabled = True,
@@ -589,13 +637,18 @@ def _cc_features_impl(ctx):
generate_pdb_file_feature,
get_toolchain_lib_search_paths_feature(import_config),
get_archiver_flags_feature(ctx.attr.archive_flags),
+ # Start flag ordering: the order of following features impacts how
+ # flags override each other.
+ opt_feature,
+ dbg_feature,
libraries_to_link_feature,
- get_toolchain_libraries_to_link_feature(import_config),
get_toolchain_link_flags_feature(ctx.attr.link_flags),
user_link_flags_feature,
+ get_toolchain_libraries_to_link_feature(import_config),
get_toolchain_compile_flags_feature(ctx.attr.compile_flags),
get_toolchain_cxx_flags_feature(ctx.attr.cxx_flags),
user_compile_flags_feature,
+ ### End flag ordering ##
linker_param_file_feature,
compiler_input_feature,
compiler_output_feature,