Skip to content

Commit

Permalink
fix linux build
Browse files Browse the repository at this point in the history
  • Loading branch information
sphw committed Nov 9, 2023
1 parent cd05d0a commit 0eabd4c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ jobs:
- uses: actions/checkout@v3
- run: brew install elixir
- run: mix local.hex --force
- name: use macos static
run: |
mv extension/static-lib-mac.bzl extension/static-lib.bzl
# Setup the compilation environment
- uses: abhinavsingh/setup-bazel@v3
with:
Expand All @@ -92,6 +95,9 @@ jobs:
- uses: actions/checkout@v3
- run: brew install elixir
- run: mix local.hex --force
- name: use macos static
run: |
mv extension/static-lib-mac.bzl extension/static-lib.bzl
# Setup the compilation environment
- uses: abhinavsingh/setup-bazel@v3
with:
Expand Down
71 changes: 71 additions & 0 deletions extension/static-lib-mac.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Provides a rule that outputs a monolithic static library."""

# Reference: https://gist.github.com/oquenchil/3f88a39876af2061f8aad6cdc9d7c045

load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")

TOOLS_CPP_REPO = "@bazel_tools"

def _cc_static_library_impl(ctx):
output_lib = ctx.actions.declare_file("{}.a".format(ctx.attr.name))
output_flags = ctx.actions.declare_file("{}.link".format(ctx.attr.name))

cc_toolchain = find_cpp_toolchain(ctx)

# Aggregate linker inputs of all dependencies
lib_sets = []
for dep in ctx.attr.deps:
lib_sets.append(dep[CcInfo].linking_context.linker_inputs)
input_depset = depset(transitive = lib_sets)

# Collect user link flags and make sure they are unique
unique_flags = {}
for inp in input_depset.to_list():
unique_flags.update({
flag: None
for flag in inp.user_link_flags
})
link_flags = unique_flags.keys()

# Collect static libraries
libs = []
for inp in input_depset.to_list():
for lib in inp.libraries:
if lib.pic_static_library:
libs.append(lib.pic_static_library)
elif lib.static_library:
libs.append(lib.static_library)

lib_paths = [lib.path for lib in libs]

ar_path = cc_toolchain.ar_executable
# FIXME ar_executable returned llvm-lib.exe on my system, but we want llvm-ar.exe
ar_path = ar_path.replace("llvm-lib.exe", "llvm-ar.exe")

ctx.actions.run_shell(
command = "libtool -static -o {0} {1}".format(output_lib.path, " ".join(lib_paths)),
inputs = libs + cc_toolchain.all_files.to_list(),
outputs = [output_lib],
mnemonic = "ArMerge",
progress_message = "Merging static library {}".format(output_lib.path),
)

ctx.actions.write(
output = output_flags,
content = "\n".join(link_flags) + "\n",
)
return [
DefaultInfo(files = depset([output_flags, output_lib])),
]

cc_static_library = rule(
implementation = _cc_static_library_impl,
attrs = {
"deps": attr.label_list(),
"_cc_toolchain": attr.label(
default = TOOLS_CPP_REPO + "//tools/cpp:current_cc_toolchain",
),
},
toolchains = [TOOLS_CPP_REPO + "//tools/cpp:toolchain_type"],
incompatible_use_toolchain_transition = True,
)
5 changes: 2 additions & 3 deletions extension/static-lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ def _cc_static_library_impl(ctx):
ar_path = cc_toolchain.ar_executable
# FIXME ar_executable returned llvm-lib.exe on my system, but we want llvm-ar.exe
ar_path = ar_path.replace("llvm-lib.exe", "llvm-ar.exe")
ar_path = ""

ctx.actions.run_shell(
command = "libtool -static -o {0} {1}".format(output_lib.path, " ".join(lib_paths)),
#command = "\"{0}\" rcT {1} {2} && echo -e 'create {1}\naddlib {1}\nsave\nend' | \"{0}\" -M".format(ar_path, output_lib.path, " ".join(lib_paths)),
command = "\"{0}\" rcT {1} {2} && echo -e 'create {1}\naddlib {1}\nsave\nend' | \"{0}\" -M".format(ar_path, output_lib.path, " ".join(lib_paths)),
inputs = libs + cc_toolchain.all_files.to_list(),
outputs = [output_lib],
mnemonic = "ArMerge",
progress_message = "Merging static library {}".format(output_lib.path),
)

ctx.actions.write(
output = output_flags,
content = "\n".join(link_flags) + "\n",
Expand Down

0 comments on commit 0eabd4c

Please sign in to comment.