-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 bazelbuild/bazel#22164. Includes minor changes to tests directory structure to meet Bazel build requirements.
- Loading branch information
1 parent
69b884d
commit a6c00b2
Showing
82 changed files
with
3,405 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 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 _copy_filegroups_impl(ctx): | ||
inputs = ctx.files.filegroups | ||
remove_prefixes = ctx.attr.remove_prefixes | ||
add_prefix = ctx.attr.add_prefix | ||
|
||
outputs = [] | ||
for f in inputs: | ||
relative_path = f.path | ||
for prefix in remove_prefixes: | ||
# Add trailing forward slash if necessary. | ||
if prefix[-1] != "/": | ||
prefix += "/" | ||
if len(prefix) > 0 and f.path.startswith(prefix): | ||
relative_path = f.path[len(prefix):] | ||
break | ||
|
||
if len(add_prefix) > 0: | ||
# Add trailing forward slash if necessary. | ||
if add_prefix[-1] != "/": | ||
add_prefix += "/" | ||
relative_path = add_prefix + relative_path | ||
|
||
out = ctx.actions.declare_file(relative_path) | ||
outputs.append(out) | ||
|
||
if relative_path.find("/") > 0: | ||
command="mkdir -p $(dirname {}) && cp {} {}".format(out.path, f.path, out.path) | ||
else: | ||
command="cp {} {}".format(f.path, out.path) | ||
|
||
ctx.actions.run_shell( | ||
outputs=[out], | ||
inputs=depset([f]), | ||
command=command | ||
) | ||
|
||
# Small sanity check | ||
if len(inputs) != len(outputs): | ||
fail("Output count should be 1-to-1 with input count.") | ||
|
||
return DefaultInfo( | ||
files=depset(outputs), | ||
runfiles=ctx.runfiles(files=outputs) | ||
) | ||
|
||
# Allows the file contents of |filegroups| to be copied next to a cc_binary | ||
# target via the |data| attribute. | ||
# Implementation based on https://stackoverflow.com/a/57983629 | ||
copy_filegroups = rule( | ||
implementation=_copy_filegroups_impl, | ||
attrs={ | ||
"filegroups": attr.label_list(), | ||
"remove_prefixes": attr.string_list(default = []), | ||
"add_prefix": attr.string(default = ""), | ||
}, | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}, | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files([ | ||
"pkg_config.bzl", | ||
"BUILD.tmpl", | ||
]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}", | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
workspace(name = "pkg_config") | ||
|
Oops, something went wrong.