From bdde55a2967a7e08e24ae807ffccc8d466f6d0fb Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Tue, 6 Aug 2024 11:57:15 -0400 Subject: [PATCH] bazel: Limit define scope to specific cc targets (see #3757) - Add `declare_[cc|objc]_library` macros to configure common `copts` and `local_defines` (where supported) on `[cc|objc]_library` targets. This limits the scope of defines to the specific target without inheritance by dependent targets. - `objc_library` does not currently support `local_defines` so we use `copts` instead of MacOS. - Use `**kwargs` to pass all other arguments to the actual cc target declaration. --- bazel/library_helpers.bzl | 90 +++++++++++++++++++ bazel/linux/exe_helpers.bzl | 15 ++-- bazel/mac/variables.bzl | 26 ++---- bazel/win/exe_helpers.bzl | 20 +++-- tools/distrib/bazel/BUILD.bazel | 70 +++------------ .../bazel/tests-cefclient-BUILD.bazel.in | 7 +- .../bazel/tests-cefsimple-BUILD.bazel.in | 7 +- .../bazel/tests-ceftests-BUILD.bazel.in | 7 +- .../distrib/bazel/tests-shared-BUILD.bazel.in | 11 +-- 9 files changed, 149 insertions(+), 104 deletions(-) create mode 100644 bazel/library_helpers.bzl diff --git a/bazel/library_helpers.bzl b/bazel/library_helpers.bzl new file mode 100644 index 000000000..ad05a6a93 --- /dev/null +++ b/bazel/library_helpers.bzl @@ -0,0 +1,90 @@ +# Copyright (c) 2024 The Chromium Embedded Framework Authors. All rights +# reserved. Use of this source code is governed by a BSD-style license that +# can be found in the LICENSE file. + +load("//bazel/win:variables.bzl", + WIN_COMMON_COPTS="COMMON_COPTS", + WIN_COMMON_COPTS_RELEASE="COMMON_COPTS_RELEASE", + WIN_COMMON_COPTS_DEBUG="COMMON_COPTS_DEBUG", + WIN_COMMON_DEFINES="COMMON_DEFINES", + WIN_COMMON_DEFINES_RELEASE="COMMON_DEFINES_RELEASE", + WIN_COMMON_DEFINES_DEBUG="COMMON_DEFINES_DEBUG") +load("//bazel/linux:variables.bzl", + LINUX_COMMON_COPTS="COMMON_COPTS", + LINUX_COMMON_COPTS_RELEASE="COMMON_COPTS_RELEASE", + LINUX_COMMON_COPTS_DEBUG="COMMON_COPTS_DEBUG", + LINUX_COMMON_DEFINES="COMMON_DEFINES", + LINUX_COMMON_DEFINES_RELEASE="COMMON_DEFINES_RELEASE", + LINUX_COMMON_DEFINES_DEBUG="COMMON_DEFINES_DEBUG") +load("//bazel/mac:variables.bzl", + MAC_COMMON_COPTS="COMMON_COPTS", + MAC_COMMON_COPTS_RELEASE="COMMON_COPTS_RELEASE", + MAC_COMMON_COPTS_DEBUG="COMMON_COPTS_DEBUG") +load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library") + +def declare_cc_library(copts=[], local_defines=[], **kwargs): + """ + cc_library wrapper that applies common copts and local_defines. + """ + # NOTE: objc_library does not support local_defines on MacOS, so on + # that platform we put the defines in copts instead. + cc_library( + copts = select({ + "@platforms//os:windows": WIN_COMMON_COPTS, + "@platforms//os:linux": LINUX_COMMON_COPTS, + "@platforms//os:macos": MAC_COMMON_COPTS, + "//conditions:default": None, + }) + select({ + "@cef//:windows_opt": WIN_COMMON_COPTS_RELEASE, + "@cef//:windows_dbg": WIN_COMMON_COPTS_DEBUG, + "@cef//:windows_fastbuild": WIN_COMMON_COPTS_RELEASE, + "@cef//:linux_opt": LINUX_COMMON_COPTS_RELEASE, + "@cef//:linux_dbg": LINUX_COMMON_COPTS_DEBUG, + "@cef//:linux_fastbuild": LINUX_COMMON_COPTS_RELEASE, + "@cef//:macos_opt": MAC_COMMON_COPTS_RELEASE, + "@cef//:macos_dbg": MAC_COMMON_COPTS_DEBUG, + "@cef//:macos_fastbuild": MAC_COMMON_COPTS_RELEASE, + "//conditions:default": None, + }) + copts, + local_defines = select({ + "@platforms//os:windows": WIN_COMMON_DEFINES, + "@platforms//os:linux": LINUX_COMMON_DEFINES, + "//conditions:default": None, + }) + select({ + "@cef//:windows_opt": WIN_COMMON_DEFINES_RELEASE, + "@cef//:windows_dbg": WIN_COMMON_DEFINES_DEBUG, + "@cef//:windows_fastbuild": WIN_COMMON_DEFINES_RELEASE, + "@cef//:linux_opt": LINUX_COMMON_DEFINES_RELEASE, + "@cef//:linux_dbg": LINUX_COMMON_DEFINES_DEBUG, + "@cef//:linux_fastbuild": LINUX_COMMON_DEFINES_RELEASE, + "//conditions:default": None, + }) + local_defines, + **kwargs + ) + +def declare_objc_library(copts=[], **kwargs): + """ + objc_library wrapper that applies common copts. + """ + # NOTE: objc_library does not support local_defines on MacOS, so on + # that platform we put the defines in copts instead. + objc_library( + copts = select({ + "@platforms//os:windows": WIN_COMMON_COPTS, + "@platforms//os:linux": LINUX_COMMON_COPTS, + "@platforms//os:macos": MAC_COMMON_COPTS, + "//conditions:default": None, + }) + select({ + "@cef//:windows_opt": WIN_COMMON_COPTS_RELEASE, + "@cef//:windows_dbg": WIN_COMMON_COPTS_DEBUG, + "@cef//:windows_fastbuild": WIN_COMMON_COPTS_RELEASE, + "@cef//:linux_opt": LINUX_COMMON_COPTS_RELEASE, + "@cef//:linux_dbg": LINUX_COMMON_COPTS_DEBUG, + "@cef//:linux_fastbuild": LINUX_COMMON_COPTS_RELEASE, + "@cef//:macos_opt": MAC_COMMON_COPTS_RELEASE, + "@cef//:macos_dbg": MAC_COMMON_COPTS_DEBUG, + "@cef//:macos_fastbuild": MAC_COMMON_COPTS_RELEASE, + "//conditions:default": None, + }) + copts, + **kwargs + ) diff --git a/bazel/linux/exe_helpers.bzl b/bazel/linux/exe_helpers.bzl index e4f54e21b..0f7c99442 100755 --- a/bazel/linux/exe_helpers.bzl +++ b/bazel/linux/exe_helpers.bzl @@ -6,10 +6,11 @@ load("//bazel:copy_filegroups.bzl", "copy_filegroups") load("//bazel/linux:fix_rpath.bzl", "fix_rpath") load("//bazel/linux:variables.bzl", "COMMON_LINKOPTS", - "COMMON_COPTS", "COMMON_COPTS_RELEASE", "COMMON_COPTS_DEBUG") + "COMMON_COPTS", "COMMON_COPTS_RELEASE", "COMMON_COPTS_DEBUG", + "COMMON_DEFINES", "COMMON_DEFINES_RELEASE", "COMMON_DEFINES_DEBUG") load("@rules_cc//cc:defs.bzl", "cc_binary") -def declare_exe(name, srcs=[], deps=[], linkopts=[], copts=[], defines=[], data=[]): +def declare_exe(name, srcs=[], deps=[], linkopts=[], copts=[], local_defines=[], data=[], **kwargs): # Copy SOs and resources into the current project. copy_target = "{}_sos_and_resources".format(name) copy_filegroups( @@ -36,15 +37,19 @@ def declare_exe(name, srcs=[], deps=[], linkopts=[], copts=[], defines=[], data= "@cef//:cef_sandbox", ] + deps, linkopts = COMMON_LINKOPTS + linkopts, - copts = select({ + copts = COMMON_COPTS + select({ "@cef//:linux_dbg": COMMON_COPTS_DEBUG, "//conditions:default": COMMON_COPTS_RELEASE, - }) + COMMON_COPTS + copts, - defines = defines, + }) + copts, + local_defines = COMMON_DEFINES + select({ + "@cef//:linux_dbg": COMMON_DEFINES_DEBUG, + "//conditions:default": COMMON_DEFINES_RELEASE, + }) + local_defines, data = [ ":{}".format(copy_target), ] + data, target_compatible_with = ["@platforms//os:linux"], + **kwargs ) # Set rpath to $ORIGIN so that libraries can be loaded from next to the diff --git a/bazel/mac/variables.bzl b/bazel/mac/variables.bzl index a15230c79..31c750a38 100644 --- a/bazel/mac/variables.bzl +++ b/bazel/mac/variables.bzl @@ -7,7 +7,7 @@ MACOS_BUNDLE_ID_BASE="org.cef" CEF_FRAMEWORK_NAME="Chromium Embedded Framework" # -# Common 'linkopts' for cc_binary targets. +# Common 'linkopts' for macos_application targets. # # Standard link frameworks. @@ -29,34 +29,24 @@ COMMON_LINKOPTS = [ }) # -# Common 'copts' for cc_libary and cc_binary targets. +# Common 'copts' for cc_libary, objc_library and macos_application targets. +# We include defines in 'copts' because objc_library does not support +# 'local_defines'. See https://github.com/bazelbuild/bazel/issues/17482. # COMMON_COPTS = [ "-Wno-undefined-var-template", "-Wno-missing-field-initializers", "-Wno-deprecated-copy", + + # Used by apps to test if the sandbox is enabled + "-DCEF_USE_SANDBOX", ] COMMON_COPTS_DEBUG = [ ] COMMON_COPTS_RELEASE = [ -] - -# -# Common 'defines' for cc_libary targets. -# - -COMMON_DEFINES = [ - # Used by apps to test if the sandbox is enabled - "CEF_USE_SANDBOX", -] - -COMMON_DEFINES_DEBUG = [ -] - -COMMON_DEFINES_RELEASE = [ # Not a debug build - "NDEBUG", + "-DNDEBUG", ] diff --git a/bazel/win/exe_helpers.bzl b/bazel/win/exe_helpers.bzl index 88b1ff7a2..ba4576c07 100644 --- a/bazel/win/exe_helpers.bzl +++ b/bazel/win/exe_helpers.bzl @@ -7,11 +7,13 @@ load("//bazel/win:mt.bzl", "add_manifest") load("//bazel/win:rc.bzl", "compile_rc") load("//bazel/win:variables.bzl", "COMMON_LINKOPTS", - "COMMON_COPTS", "COMMON_COPTS_RELEASE", "COMMON_COPTS_DEBUG") + "COMMON_COPTS", "COMMON_COPTS_RELEASE", "COMMON_COPTS_DEBUG", + "COMMON_DEFINES", "COMMON_DEFINES_RELEASE", "COMMON_DEFINES_DEBUG") load("@rules_cc//cc:defs.bzl", "cc_binary") def declare_exe(name, srcs, manifest_srcs, rc_file, resources_srcs, resources_deps=[], - deps=[], linkopts=[], copts=[], defines=[], data=[]): + deps=[], linkopts=[], copts=[], local_defines=[], data=[], + additional_linker_inputs=[], features=[], **kwargs): # Resource file. res_target = "{}_res".format(name) compile_rc( @@ -51,19 +53,23 @@ def declare_exe(name, srcs, manifest_srcs, rc_file, resources_srcs, resources_de linkopts = [ "$(location :{})".format(res_target), ] + COMMON_LINKOPTS + linkopts, - copts = select({ + copts = COMMON_COPTS + select({ "@cef//:windows_dbg": COMMON_COPTS_DEBUG, "//conditions:default": COMMON_COPTS_RELEASE, - }) + COMMON_COPTS + copts, - defines = defines, + }) + copts, + local_defines = COMMON_DEFINES + select({ + "@cef//:windows_dbg": COMMON_DEFINES_DEBUG, + "//conditions:default": COMMON_DEFINES_RELEASE, + }) + local_defines, additional_linker_inputs = [ ":{}".format(res_target), - ], + ] + additional_linker_inputs, data = [ ":{}".format(copy_target), ] + data, - features = ["generate_pdb_file"], + features = ["generate_pdb_file"] + features, target_compatible_with = ["@platforms//os:windows"], + **kwargs ) # Add manifest and rename to final executable. diff --git a/tools/distrib/bazel/BUILD.bazel b/tools/distrib/bazel/BUILD.bazel index b50046a84..a44b8ba86 100755 --- a/tools/distrib/bazel/BUILD.bazel +++ b/tools/distrib/bazel/BUILD.bazel @@ -9,33 +9,16 @@ package(default_visibility = [ load("@bazel_skylib//lib:selects.bzl", "selects") load("@build_bazel_rules_apple//apple:apple.bzl", "apple_dynamic_framework_import") +load("//bazel:library_helpers.bzl", "declare_cc_library", "declare_objc_library") load("//bazel/win:variables.bzl", WIN_DLLS="DLLS", WIN_DLLS_X64="DLLS_X64", - WIN_SANDBOX_LIBS="SANDBOX_LIBS", - WIN_COMMON_COPTS="COMMON_COPTS", - WIN_COMMON_COPTS_RELEASE="COMMON_COPTS_RELEASE", - WIN_COMMON_COPTS_DEBUG="COMMON_COPTS_DEBUG", - WIN_COMMON_DEFINES="COMMON_DEFINES", - WIN_COMMON_DEFINES_RELEASE="COMMON_DEFINES_RELEASE", - WIN_COMMON_DEFINES_DEBUG="COMMON_DEFINES_DEBUG") + WIN_SANDBOX_LIBS="SANDBOX_LIBS") load("//bazel/linux:variables.bzl", - LINUX_SOS="SOS", - LINUX_COMMON_COPTS="COMMON_COPTS", - LINUX_COMMON_COPTS_RELEASE="COMMON_COPTS_RELEASE", - LINUX_COMMON_COPTS_DEBUG="COMMON_COPTS_DEBUG", - LINUX_COMMON_DEFINES="COMMON_DEFINES", - LINUX_COMMON_DEFINES_RELEASE="COMMON_DEFINES_RELEASE", - LINUX_COMMON_DEFINES_DEBUG="COMMON_DEFINES_DEBUG") + LINUX_SOS="SOS") load("//bazel/mac:variables.bzl", - "CEF_FRAMEWORK_NAME", - MAC_COMMON_COPTS="COMMON_COPTS", - MAC_COMMON_COPTS_RELEASE="COMMON_COPTS_RELEASE", - MAC_COMMON_COPTS_DEBUG="COMMON_COPTS_DEBUG", - MAC_COMMON_DEFINES="COMMON_DEFINES", - MAC_COMMON_DEFINES_RELEASE="COMMON_DEFINES_RELEASE", - MAC_COMMON_DEFINES_DEBUG="COMMON_DEFINES_DEBUG") -load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library", "objc_library") + "CEF_FRAMEWORK_NAME") +load("@rules_cc//cc:defs.bzl", "cc_import") # # Define supported configurations. @@ -124,7 +107,7 @@ selects.config_setting_group( # Public headers for cef_wrapper here. # Seperated from the actual cef_wrapper since the *.mm file needs access to these as well. -cc_library( +declare_cc_library( name = "cef_wrapper_headers", hdrs = glob( [ @@ -137,7 +120,7 @@ cc_library( ), ) -objc_library( +declare_objc_library( name = "cef_wrapper_apple", srcs = [ "libcef_dll/wrapper/cef_library_loader_mac.mm", @@ -145,7 +128,7 @@ objc_library( implementation_deps = [":cef_wrapper_headers"], ) -cc_library( +declare_cc_library( name = "cef_wrapper", srcs = glob( [ @@ -159,42 +142,9 @@ cc_library( "include/test/*.h", ], ), - copts = select({ - "@platforms//os:windows": WIN_COMMON_COPTS, - "@platforms//os:linux": LINUX_COMMON_COPTS, - "@platforms//os:macos": MAC_COMMON_COPTS, - "//conditions:default": None, - }) + select({ - "@cef//:windows_opt": WIN_COMMON_COPTS_RELEASE, - "@cef//:windows_dbg": WIN_COMMON_COPTS_DEBUG, - "@cef//:windows_fastbuild": WIN_COMMON_COPTS_RELEASE, - "@cef//:linux_opt": LINUX_COMMON_COPTS_RELEASE, - "@cef//:linux_dbg": LINUX_COMMON_COPTS_DEBUG, - "@cef//:linux_fastbuild": LINUX_COMMON_COPTS_RELEASE, - "@cef//:macos_opt": MAC_COMMON_COPTS_RELEASE, - "@cef//:macos_dbg": MAC_COMMON_COPTS_DEBUG, - "@cef//:macos_fastbuild": MAC_COMMON_COPTS_RELEASE, - "//conditions:default": None, - }), defines = [ "WRAPPING_CEF_SHARED", - ] + select({ - "@platforms//os:windows": WIN_COMMON_DEFINES, - "@platforms//os:linux": LINUX_COMMON_DEFINES, - "@platforms//os:macos": MAC_COMMON_DEFINES, - "//conditions:default": None, - }) + select({ - "@cef//:windows_opt": WIN_COMMON_DEFINES_RELEASE, - "@cef//:windows_dbg": WIN_COMMON_DEFINES_DEBUG, - "@cef//:windows_fastbuild": WIN_COMMON_DEFINES_RELEASE, - "@cef//:linux_opt": LINUX_COMMON_DEFINES_RELEASE, - "@cef//:linux_dbg": LINUX_COMMON_DEFINES_DEBUG, - "@cef//:linux_fastbuild": LINUX_COMMON_DEFINES_RELEASE, - "@cef//:macos_opt": MAC_COMMON_DEFINES_RELEASE, - "@cef//:macos_dbg": MAC_COMMON_DEFINES_DEBUG, - "@cef//:macos_fastbuild": MAC_COMMON_DEFINES_RELEASE, - "//conditions:default": None, - }), + ], deps = [":cef_wrapper_headers"] + select({ "@platforms//os:macos": [":cef_wrapper_apple"], @@ -206,7 +156,7 @@ cc_library( ) # Only available on MacOS/Windows. -cc_library( +declare_cc_library( name = "cef_sandbox_linkflags", linkopts = select({ "@platforms//os:macos": ["-lsandbox"], diff --git a/tools/distrib/bazel/tests-cefclient-BUILD.bazel.in b/tools/distrib/bazel/tests-cefclient-BUILD.bazel.in index beeebd76d..6b91ad3ad 100644 --- a/tools/distrib/bazel/tests-cefclient-BUILD.bazel.in +++ b/tools/distrib/bazel/tests-cefclient-BUILD.bazel.in @@ -10,7 +10,8 @@ package(default_visibility = [ ":__subpackages__", ]) -load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library") +load("//bazel:library_helpers.bzl", "declare_cc_library", "declare_objc_library") +load("@rules_cc//cc:defs.bzl", "cc_library") # # Source file lists. @@ -53,7 +54,7 @@ filegroup( # MacOS targets. # -objc_library( +declare_objc_library( name = "BrowserLibMac", srcs = srcs_common + srcs_browser + srcs_browser_mac, target_compatible_with = [ @@ -65,7 +66,7 @@ objc_library( ], ) -objc_library( +declare_objc_library( name = "RendererLibMac", srcs = srcs_common + srcs_renderer, target_compatible_with = [ diff --git a/tools/distrib/bazel/tests-cefsimple-BUILD.bazel.in b/tools/distrib/bazel/tests-cefsimple-BUILD.bazel.in index 53ac0b759..9cd343353 100644 --- a/tools/distrib/bazel/tests-cefsimple-BUILD.bazel.in +++ b/tools/distrib/bazel/tests-cefsimple-BUILD.bazel.in @@ -10,7 +10,8 @@ package(default_visibility = [ ":__subpackages__", ]) -load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library") +load("//bazel:library_helpers.bzl", "declare_cc_library", "declare_objc_library") +load("@rules_cc//cc:defs.bzl", "cc_library") # # Source file lists. @@ -40,7 +41,7 @@ srcs_renderer_mac = [ # MacOS targets. # -objc_library( +declare_objc_library( name = "BrowserLibMac", srcs = srcs_browser + srcs_browser_mac, target_compatible_with = [ @@ -51,7 +52,7 @@ objc_library( ], ) -cc_library( +declare_cc_library( name = "RendererLibMac", srcs = srcs_renderer_mac, target_compatible_with = [ diff --git a/tools/distrib/bazel/tests-ceftests-BUILD.bazel.in b/tools/distrib/bazel/tests-ceftests-BUILD.bazel.in index 50ab55fa9..d528e5373 100644 --- a/tools/distrib/bazel/tests-ceftests-BUILD.bazel.in +++ b/tools/distrib/bazel/tests-ceftests-BUILD.bazel.in @@ -10,7 +10,8 @@ package(default_visibility = [ ":__subpackages__", ]) -load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library") +load("//bazel:library_helpers.bzl", "declare_cc_library", "declare_objc_library") +load("@rules_cc//cc:defs.bzl", "cc_library") # # Source file lists. @@ -51,7 +52,7 @@ filegroup( srcs = ["resources/net"], ) -objc_library( +declare_objc_library( name = "BrowserLibMac", srcs = srcs_browser + srcs_browser_mac, target_compatible_with = [ @@ -64,7 +65,7 @@ objc_library( ], ) -objc_library( +declare_objc_library( name = "RendererLibMac", srcs = srcs_renderer_mac, target_compatible_with = [ diff --git a/tools/distrib/bazel/tests-shared-BUILD.bazel.in b/tools/distrib/bazel/tests-shared-BUILD.bazel.in index 09ab38559..bc6b1bb74 100644 --- a/tools/distrib/bazel/tests-shared-BUILD.bazel.in +++ b/tools/distrib/bazel/tests-shared-BUILD.bazel.in @@ -2,7 +2,8 @@ # reserved. Use of this source code is governed by a BSD-style license that # can be found in the LICENSE file. -load("@rules_cc//cc:defs.bzl", "cc_library", "objc_library") +load("//bazel:library_helpers.bzl", "declare_cc_library", "declare_objc_library") +load("@rules_cc//cc:defs.bzl", "cc_library") # Allow access from all //tests packages. package(default_visibility = [ @@ -62,7 +63,7 @@ filegroup( # MacOS targets. # -objc_library( +declare_objc_library( name = "BrowserLibMac", srcs = srcs_common + srcs_browser + srcs_browser_mac, target_compatible_with = [ @@ -73,7 +74,7 @@ objc_library( ], ) -cc_library( +declare_cc_library( name = "RendererLibMac", srcs = srcs_common + srcs_renderer + srcs_renderer_mac, target_compatible_with = [ @@ -89,7 +90,7 @@ cc_library( # Needs to be defined here because Bazel disallows direct access to files # outside of the package directory. -objc_library( +declare_objc_library( name = "BrowserLibMacCefTests", srcs = srcs_browser_mac_ceftests, target_compatible_with = [ @@ -101,7 +102,7 @@ objc_library( ], ) -objc_library( +declare_objc_library( name = "RendererLibMacCefTests", srcs = srcs_renderer_mac_ceftests, target_compatible_with = [