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

How to make derived header-only libraries? #244

Closed
rnburn opened this issue Apr 7, 2019 · 7 comments
Closed

How to make derived header-only libraries? #244

rnburn opened this issue Apr 7, 2019 · 7 comments
Labels
Can Close? question Further information is requested

Comments

@rnburn
Copy link

rnburn commented Apr 7, 2019

I'm using bazel to build an apache module. I have dependencies set up like this

configure_make(
    name = "apr",
    configure_options = [
        "--enable-shared=no",
        "--with-pic",
    ],
    lib_source = "@org_apache_apr//:all",
    static_libraries = ["libapr-1.a"],
)
...
configure_make(
    name = "apache_httpd",
    configure_options = [
        "--with-apr=$EXT_BUILD_DEPS/apr",
        "--with-apr-util=$EXT_BUILD_DEPS/apr_util",
        "--with-pcre=$EXT_BUILD_DEPS/pcre",
        "--enable-mods-static='proxy proxy_balancer unixd authz_core slotmem_shm'",
    ],
    lib_source = "@org_apache_httpd//:all",
    out_bin_dir = ".",
    binaries = [
        "bin/httpd",
        "bin/apachectl",
    ],
    deps = [
        ":apr",
        ":apr_util",
        ":pcre",
    ],
    visibility = [
        "//visibility:public",
    ],
)

When I build a .so apache module, I need to include the apr dependency headers but not link to libapr-1.a because those symbols are resolved when the module is loaded into the apache binary. Is there a way that I can make a derived cc_library from :apr that only has the headers?

I'm currently adding an additional configure_make like this

configure_make(
    name = "apr_headers",
    configure_options = [
        "--enable-shared=no",
        "--with-pic",
    ],
    lib_source = "@org_apache_apr//:all",
    out_include_dir = "include/apr-1",
    headers_only = True,
)

But that hardly seems optimal as I have to configure_make apr twice.

@rnburn rnburn changed the title How to make a derived header-only libraries? How to make derived header-only libraries? Apr 7, 2019
@irengrig
Copy link
Contributor

irengrig commented May 6, 2019

Hi @rnburn, you can have only one configure_make target, with different paths to select only headers or headers and libraries together.
Here is the example how I was able to select headers:
`
filegroup(
name = "httpd_dir",
srcs = [
":apache_httpd",
],
output_group = "gen_dir"
)
genrule(
name = "select_headers",
srcs = [":httpd_dir"],
cmd = "cp -r $(location httpd_dir)/include $(location out_dir)",
outs = ["out_dir"]
)

genrule(
name = "list_headers",
srcs = [":select_headers"],
cmd = "ls $(location select_headers) > $(location list.txt)",
outs = ["list.txt"]
)
`

  • I used list_headers only to make select_headers to be built and demonstrate it works, you do not need it in the real build.
  • You can also have a custom rule for filtering and do everything with Starlark, because with genrule it is platform-bound.
  • Unfortunately, it seems to not work just woth filegroup like: srcs = glob([":target/subdir"]) - probably, glob syntax will always look into the current directory where the BUILD file is and not at the target...

@irengrig irengrig added the question Further information is requested label May 9, 2019
@rnburn
Copy link
Author

rnburn commented May 15, 2019

Thanks @irengrig. I'm still having some trouble getting this to work. Here's the setup I have to produce a cpython_header_only library to use with a C++ python module

configure_make(
    name = "openssl",
    configure_command = "config",
    configure_options = [
        "no-shared",
    ],
    lib_source = "@com_github_openssl_openssl//:all",
    static_libraries = [
        "libcrypto.a",
        "libssl.a",
    ],
)

configure_make(
    name = "cpython",
    configure_options = [
        "--with-openssl=$EXT_BUILD_DEPS/openssl",
        # Hack to work around https://github.com/bazelbuild/rules_foreign_cc/issues/239
        "CFLAGS='-Dredacted=\"redacted\"'",
    ],
    lib_source = "@com_github_python_cpython//:all",
    out_include_dir = "include/python3.7m",
    binaries = [
        "python3",
    ],
    visibility = [
        "//visibility:public",
    ],
    deps = [
        ":openssl",
    ],
)

filegroup(
    name = "cpython_files",
    srcs = [
        ":cpython",
    ],
    visibility = [
        "//visibility:public",
    ],
    output_group = "gen_dir",
)

genrule(
    name = "cpython_includes",
    srcs = [
        ":cpython_files",
    ],
    cmd = "cp -r $(location cpython_files)/include/python3.7m $(location include)",
    outs = ["include"],
)

cc_library(
    name = "cpython_header_only",
    srcs = [
        ":cpython_includes",
    ],
    copts = ["-isystem include"],
)

But I get the error

root@193498bdc412:/src# bazel build //...
ERROR: /src/3rd_party/BUILD:65:12: in srcs attribute of cc_library rule //3rd_party:cpython_header_only: '//3rd_party:cpython_includes' does not produce any cc_library srcs files (expected .cc, .cpp, .cxx, .c++, .C, .c, .h, .hh, .hpp, .ipp, .hxx, .inc, .inl, .H, .S, .s, .asm, .a, .lib, .pic.a, .lo, .lo.lib, .pic.lo, .so, .dylib, .dll, .o, .obj or .pic.o)
ERROR: Analysis of target '//3rd_party:cpython_header_only' failed; build aborted: Analysis of target '//3rd_party:cpython_header_only' failed; build aborted
INFO: Elapsed time: 0.638s
INFO: 0 processes.

Any ideas?

@rnburn
Copy link
Author

rnburn commented May 15, 2019

I was able to get things to work with this variant

cc_library(
    name = "cpython_header_only",
    hdrs = [
        ":cpython_includes",
    ],
    copts = ["-isystem include"],
)

though not sure why that fixed the problem

@rnburn
Copy link
Author

rnburn commented May 15, 2019

Another curious error

If I use this version (which works better when the project is used externally bazelbuild/bazel#2670 )

cc_library(
    name = "cpython_header_only",
    hdrs = [
        ":cpython_includes",
    ],
    includes = ["include"],
)

I get

ERROR: /src/src/lib/BUILD:9:1: undeclared inclusion(s) in rule '//src/lib:bridge_tracer_lib':
this rule is missing dependency declarations for the following files included by 'src/lib/python_object_wrapper.cpp':
  'bazel-out/k8-fastbuild/genfiles/3rd_party/include/Python.h'
  'bazel-out/k8-fastbuild/genfiles/3rd_party/include/patchlevel.h'
...

@rnburn
Copy link
Author

rnburn commented May 16, 2019

@irengrig - this approach doesn't quite work. It still links in dependencies. This is my setup for a cpython_header_only library.

But when I build I get

bazel build -s //:bridge_tracer.so
...
/usr/bin/gcc -shared -o bazel-out/k8-fastbuild/bin/bridge_tracer.so -Wl,-S '-fuse-ld=gold' -Wl,-no-as-needed -Wl,-z,relro,-z,now -B/usr/bin -pass-exit-codes -Wl,@bazel-out/k8-fastbuild/bin/bridge_tracer.so-2.params

And bazel-out/k8-fastbuild/bin/bridge_tracer.so-2.params has the cpython dependencies linked in

cat bazel-out/k8-fastbuild/bin/bridge_tracer.so-2.params
...
-whole-archive
bazel-out/k8-fastbuild/genfiles/3rd_party/openssl/lib/libssl.a
-no-whole-archive

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_foreign_cc!

@github-actions
Copy link

This issue was automatically closed because it went 30 days without a reply since it was labeled "Can Close?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Can Close? question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants