bazel: Add initial config for binary distribution (see #3757)

Add support for building the CEF binary distribution using Bazel
and the default platform toolchain. Tested to work for Windows
x64, MacOS ARM64 and x64 (cross-compile from ARM64), and
Linux x64. Windows x86 (cross-compile from x64) is known to
be broken, see https://github.com/bazelbuild/bazel/issues/22164.

Includes minor changes to tests directory structure to meet
Bazel build requirements.
This commit is contained in:
Marshall Greenblatt
2024-07-28 18:19:47 +00:00
parent 69b884d39c
commit a6c00b2ff6
82 changed files with 3405 additions and 196 deletions

8
bazel/linux/BUILD.bazel Executable file
View File

@@ -0,0 +1,8 @@
# 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.
# Allow access from targets in other packages.
package(default_visibility = [
"//visibility:public",
])

58
bazel/linux/exe_helpers.bzl Executable file
View File

@@ -0,0 +1,58 @@
# 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: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")
load("@rules_cc//cc:defs.bzl", "cc_binary")
def declare_exe(name, srcs=[], deps=[], linkopts=[], copts=[], defines=[], data=[]):
# Copy SOs and resources into the current project.
copy_target = "{}_sos_and_resources".format(name)
copy_filegroups(
name = copy_target,
filegroups = [
"//:sos",
"//:resources",
],
remove_prefixes = [
"Debug",
"Release",
"Resources",
],
)
# Executable target.
binary_target = "{}_incorrect_rpath".format(name)
cc_binary(
name = binary_target,
srcs = srcs,
deps = [
"//:cef_wrapper",
"//:cef",
"//:cef_sandbox",
] + deps,
linkopts = COMMON_LINKOPTS + linkopts,
copts = select({
"//:linux_dbg": COMMON_COPTS_DEBUG,
"//conditions:default": COMMON_COPTS_RELEASE,
}) + COMMON_COPTS + copts,
defines = defines,
data = [
":{}".format(copy_target),
] + data,
target_compatible_with = ["@platforms//os:linux"],
)
# Set rpath to $ORIGIN so that libraries can be loaded from next to the
# executable.
fix_rpath(
name = "{}_fixed_rpath".format(name),
src = ":{}".format(binary_target),
out = name,
target_compatible_with = ["@platforms//os:linux"],
)

41
bazel/linux/fix_rpath.bzl Normal file
View File

@@ -0,0 +1,41 @@
# 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.
def _fix_rpath_impl(ctx):
inputs = ctx.runfiles(files = [ctx.file.src])
# Bring over 'data' dependencies from the input.
inputs = inputs.merge_all([ctx.attr.src[DefaultInfo].default_runfiles])
src = ctx.file.src.path
out = ctx.outputs.out.path
ctx.actions.run_shell(
outputs = [ctx.outputs.out],
inputs = inputs.files,
arguments = [src, out],
command = "cp $1 $2 && " +
"chmod +w $2 && " +
"patchelf --remove-rpath $2 && " +
"patchelf --set-rpath '$ORIGIN' $2"
)
return [DefaultInfo(files = depset([ctx.outputs.out]))]
# Set rpath to $ORIGIN so that libraries can be loaded from next to the
# executable. The result can be confirmed with:
# $ objdump -x ./bazel-bin/path/to/binary | grep 'R.*PATH'
#
# Alternatively, define a custom CC toolchain that overrides
# 'runtime_library_search_directories'.
#
# This rule requires preinstallation of the patchelf package:
# $ sudo apt install patchelf
fix_rpath = rule(
implementation = _fix_rpath_impl,
attrs = {
"src": attr.label(allow_single_file = True),
"out": attr.output(mandatory = True),
},
)

View File

@@ -0,0 +1,7 @@
package(default_visibility = ["//visibility:public"])
exports_files([
"pkg_config.bzl",
"BUILD.tmpl",
])

View File

@@ -0,0 +1,32 @@
# vi: ft=bzl
package(default_visibility = ["//visibility:private"])
_imports = [p[:len(p)-2] for p in glob(["{}/**/*.a".format(d) for d in [%{deps}]])]
[cc_import(
name = i.replace("/", "_"),
hdrs = glob([%{hdrs}]),
# TODO: library extension for platform.
static_library = "{}.a".format(i),
shared_library = "{}.dylib".format(i),
) for i in _imports]
cc_library(
name = "internal_lib",
hdrs = glob([%{hdrs}]),
copts = [%{copts}],
includes = [%{includes}],
linkopts = [%{linkopts}],
deps = [(":" + i.replace("/", "_")) for i in _imports],
)
cc_library(
name = "lib",
hdrs = glob(["%{strip_include}/**/*.h"]),
copts = [%{extra_copts}],
linkopts = [%{extra_linkopts}],
deps = [":internal_lib"] + [%{extra_deps}],
visibility = ["//visibility:public"],
strip_include_prefix = "%{strip_include}",
include_prefix = "%{include_prefix}",
)

View File

@@ -0,0 +1,11 @@
Name: pkg_config
URL: https://github.com/cherrry/bazel_pkg_config
Version: 284219a
Description:
Bazel rules for pkg-config tools.
CEF-specific changes:
- Fix failure with duplicate symlinks.
- Remove `--static` flag from pkg-config invocation.

View File

@@ -0,0 +1,2 @@
workspace(name = "pkg_config")

View File

@@ -0,0 +1,194 @@
def _success(value):
return struct(error = None, value = value)
def _error(message):
return struct(error = message, value = None)
def _split(result, delimeter = " "):
if result.error != None:
return result
return _success([arg for arg in result.value.strip().split(delimeter) if arg])
def _find_binary(ctx, binary_name):
binary = ctx.which(binary_name)
if binary == None:
return _error("Unable to find binary: {}".format(binary_name))
return _success(binary)
def _execute(ctx, binary, args):
result = ctx.execute([binary] + args)
if result.return_code != 0:
return _error("Failed execute {} {}".format(binary, args))
return _success(result.stdout)
def _pkg_config(ctx, pkg_config, pkg_name, args):
return _execute(ctx, pkg_config, [pkg_name] + args)
def _check(ctx, pkg_config, pkg_name):
exist = _pkg_config(ctx, pkg_config, pkg_name, ["--exists"])
if exist.error != None:
return _error("Package {} does not exist".format(pkg_name))
if ctx.attr.version != "":
version = _pkg_config(ctx, pkg_config, pkg_name, ["--exact-version", ctx.attr.version])
if version.error != None:
return _error("Require {} version = {}".format(pkg_name, ctx.attr.version))
if ctx.attr.min_version != "":
version = _pkg_config(ctx, pkg_config, pkg_name, ["--atleast-version", ctx.attr.min_version])
if version.error != None:
return _error("Require {} version >= {}".format(pkg_name, ctx.attr.min_version))
if ctx.attr.max_version != "":
version = _pkg_config(ctx, pkg_config, pkg_name, ["--max-version", ctx.attr.max_version])
if version.error != None:
return _error("Require {} version <= {}".format(pkg_name, ctx.attr.max_version))
return _success(None)
def _extract_prefix(flags, prefix, strip = True):
stripped, remain = [], []
for arg in flags:
if arg.startswith(prefix):
if strip:
stripped += [arg[len(prefix):]]
else:
stripped += [arg]
else:
remain += [arg]
return stripped, remain
def _includes(ctx, pkg_config, pkg_name):
includes = _split(_pkg_config(ctx, pkg_config, pkg_name, ["--cflags-only-I"]))
if includes.error != None:
return includes
includes, unused = _extract_prefix(includes.value, "-I", strip = True)
return _success(includes)
def _copts(ctx, pkg_config, pkg_name):
return _split(_pkg_config(ctx, pkg_config, pkg_name, [
"--cflags-only-other",
"--libs-only-L",
]))
def _linkopts(ctx, pkg_config, pkg_name):
return _split(_pkg_config(ctx, pkg_config, pkg_name, [
"--libs-only-other",
"--libs-only-l",
]))
def _ignore_opts(opts, ignore_opts):
remain = []
for opt in opts:
if opt not in ignore_opts:
remain += [opt]
return remain
def _symlinks(ctx, basename, srcpaths):
result = []
root = ctx.path("")
base = root.get_child(basename)
rootlen = len(str(base)) - len(basename)
for src in [ctx.path(p) for p in srcpaths]:
dest = base.get_child(src.basename)
if not dest.exists:
ctx.symlink(src, dest)
result += [str(dest)[rootlen:]]
return result
def _deps(ctx, pkg_config, pkg_name):
deps = _split(_pkg_config(ctx, pkg_config, pkg_name, [
"--libs-only-L",
"--static",
]))
if deps.error != None:
return deps
deps, unused = _extract_prefix(deps.value, "-L", strip = True)
result = []
for dep in {dep: True for dep in deps}.keys():
base = "deps_" + dep.replace("/", "_").replace(".", "_")
result += _symlinks(ctx, base, [dep])
return _success(result)
def _fmt_array(array):
return ",".join(['"{}"'.format(a) for a in array])
def _fmt_glob(array):
return _fmt_array(["{}/**/*.h".format(a) for a in array])
def _pkg_config_impl(ctx):
pkg_name = ctx.attr.pkg_name
if pkg_name == "":
pkg_name = ctx.attr.name
pkg_config = _find_binary(ctx, "pkg-config")
if pkg_config.error != None:
return pkg_config
pkg_config = pkg_config.value
check = _check(ctx, pkg_config, pkg_name)
if check.error != None:
return check
includes = _includes(ctx, pkg_config, pkg_name)
if includes.error != None:
return includes
includes = includes.value
includes = _symlinks(ctx, "includes", includes)
strip_include = "includes"
if len(includes) == 1:
strip_include = includes[0]
if ctx.attr.strip_include != "":
strip_include += "/" + ctx.attr.strip_include
ignore_opts = ctx.attr.ignore_opts
copts = _copts(ctx, pkg_config, pkg_name)
if copts.error != None:
return copts
copts = _ignore_opts(copts.value, ignore_opts)
linkopts = _linkopts(ctx, pkg_config, pkg_name)
if linkopts.error != None:
return linkopts
linkopts = _ignore_opts(linkopts.value, ignore_opts)
deps = _deps(ctx, pkg_config, pkg_name)
if deps.error != None:
return deps
deps = deps.value
include_prefix = ctx.attr.name
if ctx.attr.include_prefix != "":
include_prefix = ctx.attr.include_prefix + "/" + ctx.attr.name
build = ctx.template("BUILD", Label("//:BUILD.tmpl"), substitutions = {
"%{name}": ctx.attr.name,
"%{hdrs}": _fmt_glob(includes),
"%{includes}": _fmt_array(includes),
"%{copts}": _fmt_array(copts),
"%{extra_copts}": _fmt_array(ctx.attr.copts),
"%{deps}": _fmt_array(deps),
"%{extra_deps}": _fmt_array(ctx.attr.deps),
"%{linkopts}": _fmt_array(linkopts),
"%{extra_linkopts}": _fmt_array(ctx.attr.linkopts),
"%{strip_include}": strip_include,
"%{include_prefix}": include_prefix,
}, executable = False)
pkg_config = repository_rule(
attrs = {
"pkg_name": attr.string(doc = "Package name for pkg-config query, default to name."),
"include_prefix": attr.string(doc = "Additional prefix when including file, e.g. third_party. Compatible with strip_include option to produce desired include paths."),
"strip_include": attr.string(doc = "Strip prefix when including file, e.g. libs, files not included will be invisible. Compatible with include_prefix option to produce desired include paths."),
"version": attr.string(doc = "Exact package version."),
"min_version": attr.string(doc = "Minimum package version."),
"max_version": attr.string(doc = "Maximum package version."),
"deps": attr.string_list(doc = "Dependency targets."),
"linkopts": attr.string_list(doc = "Extra linkopts value."),
"copts": attr.string_list(doc = "Extra copts value."),
"ignore_opts": attr.string_list(doc = "Ignore listed opts in copts or linkopts."),
},
local = True,
implementation = _pkg_config_impl,
)

68
bazel/linux/variables.bzl Executable file
View File

@@ -0,0 +1,68 @@
# 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.
#
# Distribution SOs.
#
SOS = [
"libcef.so",
"libEGL.so",
"libGLESv2.so",
"libvk_swiftshader.so",
"libvulkan.so.1",
]
#
# Common 'linkopts' for cc_binary targets.
#
# Standard link libraries.
STANDARD_LIBS = [
"X11",
]
COMMON_LINKOPTS_DEBUG = [
]
COMMON_LINKOPTS_RELEASE = [
]
COMMON_LINKOPTS = [
"-l{}".format(lib) for lib in STANDARD_LIBS
] + select({
"//:linux_dbg": COMMON_LINKOPTS_DEBUG,
"//conditions:default": COMMON_LINKOPTS_RELEASE,
})
#
# Common 'copts' for cc_libary and cc_binary targets.
#
COMMON_COPTS = [
]
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",
]