You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Last week I was working on setting up rules_rust's crate_repository() in a project of mine. One of the crates, openssl-sys, has a dependency on OpenSSL. In my case my project already had a copy of OpenSSL laying around, which I'm cc_import()ing. I wanted to reuse that one for the Rust crate, but ran into a slight issue: I can set build_script_env = {"OPENSSL_DIR": ...}, but that expects the library to be placed in a UNIX-style directory layout. So header files in include/, and the libraries themselves in lib/. I ended up solving this by writing a rule like this:
load("@bazel_skylib//lib:paths.bzl", "paths")
def_gather_library_impl(ctx):
cc_info=ctx.attr.lib[CcInfo]
files= []
# Copy over all header files.forheaderincc_info.compilation_context.headers.to_list():
forincludeincc_info.compilation_context.includes.to_list():
ifheader.path.startswith(include+"/"):
header_symlink=ctx.actions.declare_file(paths.join(ctx.attr.name, "include", paths.relativize(header.path, include)))
ctx.actions.symlink(
output=header_symlink,
target_file=header,
)
files.append(header_symlink)
# Copy over all shared libraryes.forlinker_inputincc_info.linking_context.linker_inputs.to_list():
forlibraryinlinker_input.libraries:
# Remove version number from shared object filenames, as# most compilers will only search for files whose names end# with ".so".name=paths.basename(library.dynamic_library.path)
if".so."inname:
base_name=name.split(".so.")[0] +".so"library_symlink=ctx.actions.declare_file(paths.join(ctx.attr.name, "lib", base_name))
ctx.actions.symlink(
output=library_symlink,
target_file=library.dynamic_library,
)
files.append(library_symlink)
library_symlink=ctx.actions.declare_file(paths.join(ctx.attr.name, "lib", name))
ctx.actions.symlink(
output=library_symlink,
target_file=library.dynamic_library,
)
files.append(library_symlink)
return [DefaultInfo(files=depset(files))]
# Rule for turning a cc_library() into a UNIX-style directory layout.# The "include" subdirectory contains all header files, while the "lib"# subdirectory contains all shared objects.gather_library=rule(
implementation=_gather_library_impl,
attrs= {
"lib": attr.label(mandatory=True),
},
)
Now I'm perfectly happy to hold on to that gather_directory() rule of mine. But at the same I was wondering whether it would make sense to add something like this to rules_foreign_cc as a utility function. Seems like a useful addition. Just let me know, and I'll send out a PR.
The text was updated successfully, but these errors were encountered:
Hey! 👋
Last week I was working on setting up
rules_rust
'scrate_repository()
in a project of mine. One of the crates,openssl-sys
, has a dependency on OpenSSL. In my case my project already had a copy of OpenSSL laying around, which I'mcc_import()
ing. I wanted to reuse that one for the Rust crate, but ran into a slight issue: I can setbuild_script_env = {"OPENSSL_DIR": ...}
, but that expects the library to be placed in a UNIX-style directory layout. So header files ininclude/
, and the libraries themselves inlib/
. I ended up solving this by writing a rule like this:Pretty slick, because now I can just write:
Now I just need to turn it into a TreeArtifact:
And now I can build the
openssl-sys
crate by placing this inWORKSPACE
:Now I'm perfectly happy to hold on to that
gather_directory()
rule of mine. But at the same I was wondering whether it would make sense to add something like this torules_foreign_cc
as a utility function. Seems like a useful addition. Just let me know, and I'll send out a PR.The text was updated successfully, but these errors were encountered: