Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bzlmod compatibility #522

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("//:boost/boost.bzl", "create_aliases")

exports_files([
"config.lzma-android.h",
"config.lzma-linux.h",
Expand All @@ -8,3 +10,175 @@ exports_files([
"config.lzma-ios-i386.h",
"config.lzma-windows.h",
])

# List of targets for which to create aliases
targets = [
"context",
"fiber",
"pool",
"accumulators",
"algorithm",
"align",
"any",
"assign",
"atomic",
"archive",
"array",
"asio",
"assert",
"beast",
"bimap",
"bind",
"callable_traits",
"call_traits",
"cerrno",
"checked_delete",
"chrono",
"circular_buffer",
"compute",
"concept_archetype",
"concept_check",
"config",
"concept",
"container",
"container_hash",
"conversion",
"core",
"coroutine",
"coroutine2",
"crc",
"cstdint",
"current_function",
"date_time",
"detail",
"describe",
"dll",
"dynamic_bitset",
"enable_shared_from_this",
"endian",
"exception",
"exception_ptr",
"filesystem",
"flyweight",
"foreach",
"format",
"function",
"function_types",
"functional",
"fusion",
"geometry",
"gil",
"property_tree",
"get_pointer",
"heap",
"icl",
"implicit_cast",
"is_placeholder",
"integer",
"interprocess",
"iterator",
"intrusive",
"io",
"iostreams",
"lexical_cast",
"limits",
"locale",
"lockfree",
"math",
"mem_fn",
"move",
"mp11",
"mpl",
"hana",
"msm",
"multi_index",
"multiprecision",
"nowide",
"noncopyable",
"none",
"numeric_conversion",
"numeric_ublas",
"numeric_odeint",
"multi_array",
"units",
"operators",
"optional",
"outcome",
"parameter",
"polygon",
"predef",
"preprocessor",
"process",
"program_options",
"ptr_container",
"qvm",
"random",
"range",
"ratio",
"rational",
"ref",
"regex",
"safe_numerics",
"scope_exit",
"scoped_array",
"scoped_ptr",
"shared_ptr",
"shared_array",
"signals2",
"serialization",
"smart_ptr",
"spirit",
"stacktrace",
"static_assert",
"static_string",
"stl_interfaces",
"system",
"swap",
"throw_exception",
"thread",
"tokenizer",
"tribool",
"tti",
"type",
"type_index",
"type_traits",
"typeof",
"tuple",
"unordered",
"utility",
"uuid",
"variant",
"variant2",
"visit_each",
"vmd",
"timer",
"numeric_interval",
"test",
"test.a",
"proto",
"phoenix",
"log",
"statechart",
"winapi",
"xpressive",
"property_map",
"graph",
"lambda",
"lambda2",
"sort",
"contract",
"json",
"leaf",
"pfr",
"histogram",
"poly_collection",
"type_erasure",
"url",
"test.so", # Note - This target is a boost_so_library()
]

# Call the macro with the list of targets to generate the aliases
create_aliases(
alias_target = "@boost",
target_list = targets,
)
16 changes: 16 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module(name = "boost", repo_name = "com_github_nelhage_rules_boost")

bazel_dep(
name = "platforms",
version = "0.0.7",
)

boost_libs = use_extension("//:boost/boost.bzl", "boost_deps_extension")

use_repo(boost_libs, "boost")
use_repo(boost_libs, "bazel_skylib")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bazel_skylib, zlib, bzip2, zstd, openssl are all availble in the official registry. How about using those versions? (Didn't look yet into the details how they are built TBH.)

@Attempt3035 Any plans finishing this PR?

Copy link
Author

@Attempt3035 Attempt3035 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mvukov!

This PR does work as it stands, however:
I've been doing a bunch of work over the last couple of months trying to get the entirety of boost into bzlmod. It's still a bit of a mess and work in progress, but made a PR so you can check it out here: bazelbuild/bazel-central-registry#1280
Hopefully that will be the way forward! Will try and get it tidied up and some readme instructions so you can give it a go, most of the modules are working, just adding tests!

Ideally, we'd all be working on the boost stuff in the same place. Any thoughts on the transition to bzlmod for this entire repository? Would we expect users to just switch when they turn their projects to bzlmod instead of workspace, or would it be valuable to have a module adaptor that adds aliases to the project and requires all boost libraries, so that effectively, users here could just add that bzlmod module to their project and all the targets would still work? Food for thought anyway...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a look at bazelbuild/bazel-central-registry#1280. How do you maintain all those patches? (I really hope not by hand). Unless I am mistaken, for each boost module a separate archive is downloaded. What is the benefit compared to a single archive downloaded here?

Didn't dive into all the details, but this PR here and approach of this repo seem much more maintainable than bazelbuild/bazel-central-registry#1280 TBH. If you go bazelbuild/bazel-central-registry#1280 route, does that mean this repo becomes deprecated?

Any objection to finishing up this PR? seems almost ready.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvukov Off course, you need some scripts to generate all this stuff - we could place those scripts in this repo here. I also started for other libs to have some helper scripts to be able to switch to newer versions without all the boilerplate. Some people only want to use a single header library of boost - why would you then download 300 MB complete boost? I see many people only using boost.asio. Also in the long term, we could try to bring Bazel support directly to the boost repo itself. Then we could easily copy & paste our BUILD and MODULE since boost itself is organized in this "multirepo" approach. Smaller modules == less CI build traffic, less disk usage, etc. - there are some benefits

use_repo(boost_libs, "zlib")
use_repo(boost_libs, "org_bzip_bzip2")
use_repo(boost_libs, "org_lzma_lzma")
use_repo(boost_libs, "com_github_facebook_zstd")
use_repo(boost_libs, "openssl")
31 changes: 30 additions & 1 deletion boost/boost.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def boost_so_library(
**kwargs
)

def boost_deps():
def boost_deps(ctx = None):
# ctx defaulted to None for compatibility with workspace setup
maybe(
http_archive,
name = "bazel_skylib",
Expand Down Expand Up @@ -172,3 +173,31 @@ def boost_deps():
sha256 = "6c37680fc9a37c59a4f8ece3d675a8300deda76246aa2a13f4d5976a34e5f595",
strip_prefix = "boringssl-91339236c06b1dcf7d9b43ee8bf5dda54eda4422",
)

def create_aliases(target_list, alias_target, name = None):
""" Create aliases for a list of targets

Args:
target_list: List of targets to be aliased from the dep the the main module
alias_target: Original target to be aliased to
name: Not used
"""
for target in target_list:
# Extract the name for the alias from the target string
name = target.split(":")[-1]
if not name:
continue # Skip if the target string is not valid

# Define the alias
native.alias(
name = name,
actual = alias_target + "//:" + target,
visibility = ["//visibility:public"],
)

boost_deps_extension = module_extension(
implementation = boost_deps,

# This extension is automatically loaded when using bzlmod (from MODULE.bazel) and will run the same function as WORKSPACE setup did - boost_deps(),
# but passes in a module_ctx object for advanced context of the whole project, allowing for complex, project wide modifiying extensions and distinguishing between WORKSPACE and bzlmod setups.
)
117 changes: 98 additions & 19 deletions lzma.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# lzma is a general purpose data compression library https://tukaani.org/xz/
# Public Domain

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")

# Hopefully, the need for these OSxCPU config_setting()s will be obviated by a fix to https://github.com/bazelbuild/platforms/issues/36

Expand Down Expand Up @@ -54,7 +54,6 @@ cc_library(
srcs = [
"src/common/tuklib_cpucores.c",
"src/common/tuklib_physmem.c",
"src/liblzma/api/config.h", # Generated, so missed by glob.
] + glob(
[
"src/**/*.h",
Expand All @@ -65,30 +64,17 @@ cc_library(
"src/liblzma/**/*_tablegen.c",
],
),
hdrs = glob(["src/liblzma/api/**/*.h"]),
hdrs = [
"src/liblzma/api/lzma.h", # Publicly exported header
],
copts = select({
"@platforms//os:windows": [],
"//conditions:default": ["-std=c99"],
}) + [
# Replace with local_includes if it's ever added https://github.com/bazelbuild/bazel/issues/16472
"-I external/org_lzma_lzma/src/common",
"-I external/org_lzma_lzma/src/liblzma",
"-I external/org_lzma_lzma/src/liblzma/api",
"-I external/org_lzma_lzma/src/liblzma/common",
"-I external/org_lzma_lzma/src/liblzma/check",
"-I external/org_lzma_lzma/src/liblzma/delta",
"-I external/org_lzma_lzma/src/liblzma/lz",
"-I external/org_lzma_lzma/src/liblzma/lzma",
"-I external/org_lzma_lzma/src/liblzma/rangecoder",
"-I external/org_lzma_lzma/src/liblzma/simple",
],
}),
defines = select({
"@platforms//os:windows": ["LZMA_API_STATIC"],
"//conditions:default": [],
}),
includes = [
"src/liblzma/api",
],
linkopts = select({
"@platforms//os:android": [],
"//conditions:default": ["-lpthread"],
Expand All @@ -100,5 +86,98 @@ cc_library(
local_defines = [
"HAVE_CONFIG_H",
],
strip_include_prefix = "src/liblzma/api", # Allows public header without the path and without COPTS -I or includes = []
visibility = ["//visibility:public"],
deps = [
"lzma_src_common",
"lzma_src_liblzma",
"lzma_src_liblzma_api",
"lzma_src_liblzma_check",
"lzma_src_liblzma_common",
"lzma_src_liblzma_delta",
"lzma_src_liblzma_lz",
"lzma_src_liblzma_lzma",
"lzma_src_liblzma_rangecoder",
"lzma_src_liblzma_simpler",
],
)

cc_library(
name = "lzma_src_common",
hdrs = glob(["src/common/*.h"]),
strip_include_prefix = "src/common",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma",
hdrs = glob(["src/liblzma/*.h"]),
strip_include_prefix = "src/liblzma",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_api",
srcs = [],
hdrs = [
"src/liblzma/api/config.h", # Generated, so missed by glob. In srcs so it's not public like the other headers
] + glob(
[
"src/liblzma/api/**/*.h",
],
exclude = [
"src/liblzma/api/lzma.h", # The public header, only used in hdrs of main lib (//visibility:public)
],
),
strip_include_prefix = "src/liblzma/api",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_check",
hdrs = glob(["src/liblzma/check/*.h"]),
strip_include_prefix = "src/liblzma/check",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_common",
hdrs = glob(["src/liblzma/common/*.h"]),
strip_include_prefix = "src/liblzma/common",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_delta",
hdrs = glob(["src/liblzma/delta/*.h"]),
strip_include_prefix = "src/liblzma/delta",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_lz",
hdrs = glob(["src/liblzma/lz/*.h"]),
strip_include_prefix = "src/liblzma/lz",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_lzma",
hdrs = glob(["src/liblzma/lzma/*.h"]),
strip_include_prefix = "src/liblzma/lzma",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_rangecoder",
hdrs = glob(["src/liblzma/rangecoder/*.h"]),
strip_include_prefix = "src/liblzma/rangecoder",
visibility = ["//visibility:private"],
)

cc_library(
name = "lzma_src_liblzma_simpler",
hdrs = glob(["src/liblzma/simple/*.h"]),
strip_include_prefix = "src/liblzma/simple",
visibility = ["//visibility:private"],
)