Skip to content

Commit

Permalink
Add //lib/aom.
Browse files Browse the repository at this point in the history
Also fix Go & Gazelle libs, update CC & Foreign CC toolchains to
properly handle -x c++, which should fix the AOM and X265 builds.
  • Loading branch information
attilaolah committed Dec 15, 2023
1 parent a50262f commit b9a82f6
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 58 deletions.
4 changes: 0 additions & 4 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
Expand All @@ -24,9 +23,6 @@ rust_shared_library(
deps = all_crate_deps(normal = True),
)

# gazelle:prefix github.com/attilaolah/wasm
gazelle(name = "gazelle")

buildifier(name = "buildifier")

exports_files(
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use_repo(
go_deps,
"com_github_moby_buildkit",
"com_github_rican7_conjson",
"in_gopkg_yaml",
"in_gopkg_yaml_v3",
"net_starlark_go",
"org_golang_x_mod",
)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ toolchain, pass `--config=wasm`. Pass `-c opt` for an optimised build.
| Build Label | Version |
|-------------|---------|
[`//lib/aec`](https://github.com/attilaolah/wasm/blob/main/lib/aec/BUILD.bazel) | 1.0.6 [🔗](https://gitlab.dkrz.de/k202009/libaec/-/archive/v1.0.6/libaec-v1.0.6.tar.bz2)
[`//lib/aom`](https://github.com/attilaolah/wasm/blob/main/lib/aom/BUILD.bazel) | 3.8.0 [🔗](https://github.com/m-ab-s/aom/archive/refs/tags/v3.8.0.tar.gz)
[`//lib/bison`](https://github.com/attilaolah/wasm/blob/main/lib/bison/BUILD.bazel) | 3.8.2 [🔗](https://ftp.gnu.org/gnu/bison/bison-3.8.2.tar.xz)
[`//lib/blas`](https://github.com/attilaolah/wasm/blob/main/lib/blas/BUILD.bazel) | 3.10.0 [🔗](http://www.netlib.org/blas/blas-3.10.0.tgz)
[`//lib/boost`](https://github.com/attilaolah/wasm/blob/main/lib/boost/BUILD.bazel) | 1.80.0 [🔗](https://boostorg.jfrog.io/artifactory/main/release/1.80.0/source/boost_1_80_0.tar.bz2)
Expand Down
7 changes: 0 additions & 7 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

## Gazelle dependencies.

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

# gazelle:repo bazel_gazelle
gazelle_dependencies()

## C/C++ dependencies (external). Provides @emsdk.

external_dependencies()
Expand Down
2 changes: 1 addition & 1 deletion cmd/write_me/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "write_me_lib",
Expand Down
30 changes: 30 additions & 0 deletions lib/aom/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("//lib:lib.bzl", "package_lib")
load("//toolchains/cmake:cmake.bzl", "cache_entries", "cmake_lib")
load(":package.bzl", "NAME")

package(default_visibility = ["//:__subpackages__"])

package_lib()

cmake_lib(
name = NAME,
build_data = {
"//cond:emscripten": [],
"//conditions:default": ["@yasm//:yasm"],
},
cache_entries = cache_entries(
aom_target_cpu = "generic", # disable asm
enable_docs = False,
enable_examples = False,
enable_testdata = False,
enable_tests = False,
enable_tools = False,
),
copts = select({
"//cond:emscripten": [
"-msimd128",
"-msse4.2",
],
"//conditions:default": [],
}),
)
18 changes: 18 additions & 0 deletions lib/aom/package.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Workspace rule for downloading package dependencies."""

load("//lib:http_archive.bzl", "http_archive")

NAME = "aom"
VERSION = "3.8.0"
SHA256 = "d8e0cb0157410e97ffcf01f4fe24e6447303c46cc4103d6597ba30ef508afe05"

URL = "https://github.com/m-ab-s/{name}/archive/refs/tags/v{version}.tar.gz"

def download():
http_archive(
name = NAME,
version = VERSION,
urls = [URL],
sha256 = SHA256,
strip_prefix = "{name}-{version}",
)
2 changes: 2 additions & 0 deletions lib/package.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Workspace rule for downloading subpackage dependencies."""

load("//lib/aec:package.bzl", download_aec = "download")
load("//lib/aom:package.bzl", download_aom = "download")
load("//lib/bison:package.bzl", download_bison = "download")
load("//lib/blas:package.bzl", download_blas = "download")
load("//lib/boost:package.bzl", download_boost = "download")
Expand Down Expand Up @@ -75,6 +76,7 @@ load("//lib/zstd:package.bzl", download_zstd = "download")
def download_lib():
"""Download all library sources."""
download_aec()
download_aom()
download_bison()
download_blas()
download_boost()
Expand Down
205 changes: 203 additions & 2 deletions rules_foreign_cc.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
diff --git examples/third_party/mesa/BUILD.mesa.bazel examples/third_party/mesa/BUILD.mesa.bazel
index 074f666..0e4ef2a 100644
--- examples/third_party/mesa/BUILD.mesa.bazel
+++ examples/third_party/mesa/BUILD.mesa.bazel
@@ -39,6 +39,12 @@ meson_with_requirements(
},
}),
lib_source = ":all_srcs",
+ linkopts = select({
+ "@openssl//:msvc_compiler": [
+ "ws2_32.lib",
+ ],
+ "//conditions:default": [],
+ }),
options = select({
"@platforms//os:linux": {
# Disable LLVM support, as building LLVM in rules_foreign_cc CI would drastically increase the build time
diff --git foreign_cc/boost_build.bzl foreign_cc/boost_build.bzl
index d8e489f..2462117 100644
--- foreign_cc/boost_build.bzl
Expand All @@ -11,7 +28,7 @@ index d8e489f..2462117 100644
return cc_external_rule_impl(ctx, attrs)

diff --git foreign_cc/configure.bzl foreign_cc/configure.bzl
index ddfc379..38eec7d 100644
index ddfc379..33f0e3d 100644
--- foreign_cc/configure.bzl
+++ foreign_cc/configure.bzl
@@ -98,6 +98,7 @@ def _create_configure_script(configureParameters):
Expand All @@ -22,6 +39,14 @@ index ddfc379..38eec7d 100644
configure_in_place = ctx.attr.configure_in_place,
prefix_flag = ctx.attr.prefix_flag,
autoconf = ctx.attr.autoconf,
@@ -224,6 +225,7 @@ configure_make = rule(
attrs = _attrs(),
fragments = CC_EXTERNAL_RULE_FRAGMENTS,
output_to_genfiles = True,
+ provides = [CcInfo],
implementation = _configure_make,
toolchains = [
"@rules_foreign_cc//toolchains:autoconf_toolchain",
diff --git foreign_cc/make.bzl foreign_cc/make.bzl
index fcdd8a5..c7c6862 100644
--- foreign_cc/make.bzl
Expand All @@ -34,6 +59,96 @@ index fcdd8a5..c7c6862 100644
make_commands = make_commands,
)

diff --git foreign_cc/meson.bzl foreign_cc/meson.bzl
index 7b26f75..33f38a3 100644
--- foreign_cc/meson.bzl
+++ foreign_cc/meson.bzl
@@ -2,6 +2,12 @@

load("//foreign_cc:utils.bzl", "full_label")
load("//foreign_cc/built_tools:meson_build.bzl", "meson_tool")
+load(
+ "//foreign_cc/private:cc_toolchain_util.bzl",
+ "absolutize_path_in_str",
+ "get_flags_info",
+ "get_tools_info",
+)
load(
"//foreign_cc/private:detect_root.bzl",
"detect_root",
@@ -61,8 +67,34 @@ def _create_meson_script(configureParameters):
attrs = configureParameters.attrs
inputs = configureParameters.inputs

+ tools = get_tools_info(ctx)
script = pkgconfig_script(inputs.ext_build_dirs)

+ # CFLAGS and CXXFLAGS are also set in foreign_cc/private/cmake_script.bzl, so that meson
+ # can use the intended tools.
+ # However, they are split by meson on whitespace. For Windows it's common to have spaces in path
+ # https://github.com/mesonbuild/meson/issues/3565
+ # Skip setting them in this case.
+ if " " not in tools.cc:
+ script.append("##export_var## CC {}".format(_absolutize(ctx.workspace_name, tools.cc)))
+ if " " not in tools.cxx:
+ script.append("##export_var## CXX {}".format(_absolutize(ctx.workspace_name, tools.cxx)))
+
+ # set flags same as foreign_cc/private/cc_toolchain_util.bzl
+ # cannot use get_flags_info() because bazel adds additional flags that
+ # aren't compatible with compiler or linker above
+ copts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts + getattr(ctx.attr, "copts", [])) or []
+ cxxopts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.cxxopts + getattr(ctx.attr, "copts", [])) or []
+
+ if copts:
+ script.append("##export_var## CFLAGS \"{}\"".format(" ".join(copts).replace("\"", "'")))
+ if cxxopts:
+ script.append("##export_var## CXXFLAGS \"{}\"".format(" ".join(cxxopts).replace("\"", "'")))
+
+ flags = get_flags_info(ctx)
+ if flags.cxx_linker_executable:
+ script.append("##export_var## LDFLAGS \"{}\"".format(" ".join(flags.cxx_linker_executable).replace("\"", "'")))
+
script.append("##export_var## CMAKE {}".format(attrs.cmake_path))
script.append("##export_var## NINJA {}".format(attrs.ninja_path))
script.append("##export_var## PKG_CONFIG {}".format(attrs.pkg_config_path))
@@ -201,3 +233,9 @@ def meson_with_requirements(name, requirements, **kwargs):
toolchain = full_label("built_meson_toolchain_for_{}".format(name)),
**kwargs
)
+
+def _absolutize(workspace_name, text, force = False):
+ if text.strip(" ").startswith("C:") or text.strip(" ").startswith("c:"):
+ return "\"{}\"".format(text)
+
+ return absolutize_path_in_str(workspace_name, "$EXT_BUILD_ROOT/", text, force)
diff --git foreign_cc/private/cmake_script.bzl foreign_cc/private/cmake_script.bzl
index bfe21bd..7c2782f 100644
--- foreign_cc/private/cmake_script.bzl
+++ foreign_cc/private/cmake_script.bzl
@@ -200,7 +200,9 @@ def _create_crosstool_file_text(toolchain_dict, user_cache, user_env):
_move_dict_values(toolchain_dict, env_vars, _CMAKE_ENV_VARS_FOR_CROSSTOOL)
_move_dict_values(toolchain_dict, cache_entries, _CMAKE_CACHE_ENTRIES_CROSSTOOL)

- lines = []
+ lines = [
+ "add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-xc++>)",
+ ]
crosstool_vars = []

# The __var_* bash variables that are set here are a method to avoid
@@ -209,8 +211,10 @@ def _create_crosstool_file_text(toolchain_dict, user_cache, user_env):
# but then we loose the ability to expand other variables such as
# $EXT_BUILD_DEPS and so we use this trick to leave expansion turned on in
# the HEREDOC for the crosstool
- for key in toolchain_dict:
- crosstool_vars.append("__var_{}=\"{}\"".format(key, _escape_dquote_bash_crosstool(toolchain_dict[key])))
+ for key, val in toolchain_dict.items():
+ if key == "CMAKE_CXX_FLAGS_INIT":
+ val = val.replace("-x c++", "")
+ crosstool_vars.append("__var_{}=\"{}\"".format(key, _escape_dquote_bash_crosstool(val)))
if ("CMAKE_AR" == key):
lines.append('set({} "$$__var_{}$$" {})'.format(
key,
diff --git foreign_cc/private/configure_script.bzl foreign_cc/private/configure_script.bzl
index 06c516e..de4f470 100644
--- foreign_cc/private/configure_script.bzl
Expand Down Expand Up @@ -73,8 +188,58 @@ index 9c263c0..56f1ae3 100644
"includes": attr.string_list(
doc = (
"Optional list of include dirs to be passed to the dependencies of this library. " +
diff --git foreign_cc/private/framework/helpers.bzl foreign_cc/private/framework/helpers.bzl
index 4796813..b4de856 100644
--- foreign_cc/private/framework/helpers.bzl
+++ foreign_cc/private/framework/helpers.bzl
@@ -191,6 +191,7 @@ def do_function_call(text, shell_context):
return replace_exports(after, shell_context)

arguments = split_arguments(after.strip(" ")) if after else []
+
return call_shell(shell_context, funname, *arguments)

# buildifier: disable=function-docstring
@@ -199,22 +200,28 @@ def split_arguments(text):
current = text.strip(" ")

for _ in range(1, 2147483647):
+ current = current.strip(" ")
if not current:
break

# we are ignoring escaped quotes
- (before, separator, after) = current.partition("\"")
- if not separator:
- parts += current.split(" ")
+ s_quote = current.find("\"")
+ if s_quote < 0:
+ for e in current.split(" "):
+ if len(e) > 0:
+ parts.append(e)
break
- (quoted, separator2, after2) = after.partition("\"")
- if not separator2:
+
+ e_quote = current.find("\"", s_quote + 1)
+ if e_quote < 0:
fail("Incorrect quoting in fragment: {}".format(current))

- before = before.strip(" ")
- if before:
+ # backtrack to first space, from here to e_quote is a token
+ e_before = current.rfind(" ", 0, s_quote)
+ if e_before >= 0:
+ before = current[0:e_before].strip(" ")
parts += before.split(" ")
- parts.append("\"" + quoted + "\"")
- current = after2
+ parts.append(current[e_before + 1:e_quote + 1])
+ current = current[e_quote + 1:]

return parts
diff --git foreign_cc/private/make_env_vars.bzl foreign_cc/private/make_env_vars.bzl
index b5177c7..e70c974 100644
index b5177c7..4d819a2 100644
--- foreign_cc/private/make_env_vars.bzl
+++ foreign_cc/private/make_env_vars.bzl
@@ -9,9 +9,10 @@ def get_make_env_vars(
Expand Down Expand Up @@ -131,3 +296,39 @@ index d86ff60..039b0cf 100644
script.extend(["{env_vars} {command}".format(
env_vars = configure_vars,
command = command,
diff --git test/convert_shell_script_test.bzl test/convert_shell_script_test.bzl
index 88ff647..1fe14ed 100644
--- test/convert_shell_script_test.bzl
+++ test/convert_shell_script_test.bzl
@@ -84,6 +84,7 @@ def _split_arguments_test(ctx):
" 1 2 3": ["1", "2", "3"],
" usual \"quoted argument\"": ["usual", "\"quoted argument\""],
"1 2": ["1", "2"],
+ "var -flag1=\"redacted\" -flag2=\"redacted\"": ["var", "-flag1=\"redacted\"", "-flag2=\"redacted\""],
}
for case in cases:
result = split_arguments(case)
diff --git toolchains/built_toolchains.bzl toolchains/built_toolchains.bzl
index 4f32252..2379782 100644
--- toolchains/built_toolchains.bzl
+++ toolchains/built_toolchains.bzl
@@ -166,6 +166,7 @@ def _meson_toolchain(version, register_toolchains):
http_archive,
name = "meson_src",
build_file_content = _MESON_BUILD_FILE_CONTENT,
+ sha256 = "d04b541f97ca439fb82fab7d0d480988be4bd4e62563a5ca35fadb5400727b1c",
strip_prefix = "meson-1.1.1",
url = "https://github.com/mesonbuild/meson/releases/download/1.1.1/meson-1.1.1.tar.gz",
)
diff --git toolchains/native_tools/native_tools_toolchain.bzl toolchains/native_tools/native_tools_toolchain.bzl
index 4613b3e..736d1ec 100644
--- toolchains/native_tools/native_tools_toolchain.bzl
+++ toolchains/native_tools/native_tools_toolchain.bzl
@@ -86,6 +86,7 @@ native_tool_toolchain = rule(
"If the tool is built as part of the build, the corresponding build target, " +
"which should produce the tree artifact with the binary to call."
),
+ allow_files = True,
),
},
incompatible_use_toolchain_transition = True,
Loading

0 comments on commit b9a82f6

Please sign in to comment.