Skip to content

Commit

Permalink
bazel: Limit define scope to specific cc targets (see #3757)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
magreenblatt committed Aug 6, 2024
1 parent 166eec8 commit 9e9ba2c
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 104 deletions.
90 changes: 90 additions & 0 deletions bazel/library_helpers.bzl
Original file line number Diff line number Diff line change
@@ -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
)
15 changes: 10 additions & 5 deletions bazel/linux/exe_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
26 changes: 8 additions & 18 deletions bazel/mac/variables.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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",
]

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",
"-DCEF_USE_SANDBOX",
]

COMMON_DEFINES_DEBUG = [
COMMON_COPTS_DEBUG = [
]

COMMON_DEFINES_RELEASE = [
COMMON_COPTS_RELEASE = [
# Not a debug build
"NDEBUG",
"-DNDEBUG",
]
20 changes: 13 additions & 7 deletions bazel/win/exe_helpers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
70 changes: 10 additions & 60 deletions tools/distrib/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
[
Expand All @@ -137,15 +120,15 @@ cc_library(
),
)

objc_library(
declare_objc_library(
name = "cef_wrapper_apple",
srcs = [
"libcef_dll/wrapper/cef_library_loader_mac.mm",
],
implementation_deps = [":cef_wrapper_headers"],
)

cc_library(
declare_cc_library(
name = "cef_wrapper",
srcs = glob(
[
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
7 changes: 4 additions & 3 deletions tools/distrib/bazel/tests-cefclient-BUILD.bazel.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [
Expand All @@ -65,7 +66,7 @@ objc_library(
],
)

objc_library(
declare_objc_library(
name = "RendererLibMac",
srcs = srcs_common + srcs_renderer,
target_compatible_with = [
Expand Down
7 changes: 4 additions & 3 deletions tools/distrib/bazel/tests-cefsimple-BUILD.bazel.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [
Expand All @@ -51,7 +52,7 @@ objc_library(
],
)

cc_library(
declare_cc_library(
name = "RendererLibMac",
srcs = srcs_renderer_mac,
target_compatible_with = [
Expand Down
Loading

0 comments on commit 9e9ba2c

Please sign in to comment.