autoconf_linkopts
Resolve AC_SEARCH_LIBS / AC_SUBST results into linker flags at build time.
This rule reads the JSON result files from autoconf subst variables and produces a linker response file containing only the non-empty flags. The response file is returned via CcInfo so that cc_binary/cc_library targets can depend on it to get the correct platform-specific linkopts.
On GCC/Clang the flags are passed via a @response-file in user_link_flags. On MSVC/clang-cl (where nested @file is unsupported), a .c file with #pragma comment(lib, ...) directives is compiled and the resulting object is linked in, achieving the same effect through MSVC's native mechanism.
Example:
load("@rules_cc_autoconf//autoconf:autoconf_linkopts.bzl", "autoconf_linkopts")
autoconf_linkopts(
name = "linkopts",
vars = [
"LIBPTHREAD",
"FDATASYNC_LIB",
"CLOCK_TIME_LIB",
],
deps = [":autoconf"],
)
cc_binary(
name = "my_binary",
srcs = ["main.c"],
deps = [":gnulib", ":linkopts"],
)
On modern Linux (glibc 2.34+) where pthread/rt functions are in libc, AC_SEARCH_LIBS produces empty values and no extra -l flags are added. On older systems, the appropriate flags (-lpthread, -lrt) are added automatically.
Rules
autoconf_linkopts
load("@rules_cc_autoconf//autoconf:autoconf_linkopts.bzl", "autoconf_linkopts")
autoconf_linkopts(name, deps, defaults, vars)
Resolve AC_SEARCH_LIBS / AC_SUBST results into linker flags.
Reads subst result JSON files from autoconf deps and produces a linker response file. Returns CcInfo with the flags, so consumers can simply add this target to their deps list.
On GCC/Clang the flags are delivered via a @response-file. On MSVC/clang-cl a .c file with #pragma comment(lib, ...) directives is compiled instead, since MSVC's link.exe does not support nested response files.
Variables not found in the deps are silently ignored (they may be provided by a different module or not needed on this platform).
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| deps | List of autoconf targets providing check results. | List of labels | required | |
| defaults | Whether to include toolchain defaults. | Boolean | optional | False |
| vars | List of AC_SUBST variable names to resolve (e.g., ["LIBPTHREAD", "FDATASYNC_LIB"]). Each variable is looked up in the subst results from deps. Non-empty values (like "-lpthread") are added to linkopts; empty values are skipped. | List of strings | required |