checks
This module provides Bazel macros that mirror GNU Autoconf's behavior.
Cache Variables, Defines, and Subst Values
Checks (AC_CHECK_FUNC, AC_CHECK_HEADER, etc.) create cache variables by default, following autoconf's naming convention:
- AC_CHECK_HEADER("foo.h") -> cache variable "ac_cv_header_foo_h"
- AC_CHECK_FUNC("foo") -> cache variable "ac_cv_func_foo"
- AC_CHECK_DECL("foo") -> cache variable "ac_cv_have_decl_foo"
Defines and subst values are created explicitly using AC_DEFINE and AC_SUBST.
Condition Resolution
When AC_DEFINE or AC_SUBST use a condition parameter that references a cache variable
(e.g., condition="ac_cv_header_foo_h"), the condition is evaluated as a truthy check:
-
Condition is
trueif:- The cache variable exists (check was run)
- The check succeeded (success = true)
- The value is truthy (non-empty string, not "0")
-
Condition is
falseif:- The cache variable doesn't exist (check wasn't run)
- The check failed (success = false)
- The value is falsy (empty string or "0")
If the condition includes a comparison operator (e.g., condition="ac_cv_header_foo_h==1"),
it performs value comparison instead of truthy check.
Example:
# Check creates cache variable
macros.AC_CHECK_HEADER("alloca.h")
# -> Creates cache variable: "ac_cv_header_alloca_h" with value "1" (if header exists)
# Define with truthy condition
macros.AC_DEFINE(
"HAVE_ALLOCA_H",
condition="ac_cv_header_alloca_h", # Truthy: true if check succeeded and value is truthy
if_true="1",
if_false=None, # Don't define if condition is false
)
# Subst with truthy condition
macros.AC_SUBST(
"HAVE_ALLOCA_H",
condition="ac_cv_header_alloca_h", # Truthy check
if_true="1",
if_false="0", # Always set subst value
)
Default Includes
Default includes used by AC_CHECK_DECL, AC_CHECK_TYPE, etc. when no includes are specified.
GNU Autoconf's AC_INCLUDES_DEFAULT uses #ifdef HAVE_* guards, but that relies on AC_CHECK_HEADERS_ONCE being called earlier to define those macros. In Bazel, each compile test runs independently without access to results from other checks, so we use unconditional includes instead. This matches what would happen on a modern POSIX system where all the standard headers exist.
See: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#Default-Includes
Header / includes parameter
Checks that need to include headers (AC_CHECK_DECL, AC_CHECK_TYPE, AC_CHECK_SIZEOF,
AC_CHECK_MEMBER, etc.) take an includes parameter. AC_CHECK_FUNC does not
take includes (standard Autoconf link test only); use GL_CHECK_FUNCS_ANDROID (or
other GL_CHECK_FUNCS* in gnulib/macros.bzl) when includes are needed. Both of these forms are
supported:
Use include directives as strings, e.g. "#include <stdlib.h>" or
"#include <sys/stat.h>", matching Autoconf/gnulib (e.g.
gl_CHECK_FUNCS_ANDROID([faccessat], [[#include <unistd.h>]])). The list is
joined with newlines to form the prologue of the test program.
The parameter headers is a deprecated alias for includes; prefer includes.
Functions
- checks.AC_CHECK_ALIGNOF
- checks.AC_CHECK_CXX_COMPILER_FLAG
- checks.AC_CHECK_C_COMPILER_FLAG
- checks.AC_CHECK_DECL
- checks.AC_CHECK_FUNC
- checks.AC_CHECK_HEADER
- checks.AC_CHECK_LIB
- checks.AC_CHECK_MEMBER
- checks.AC_CHECK_SIZEOF
- checks.AC_CHECK_TYPE
- checks.AC_COMPUTE_INT
- checks.AC_C_INLINE
- checks.AC_C_RESTRICT
- checks.AC_DEFINE
- checks.AC_DEFINE_UNQUOTED
- checks.AC_PROG_CC
- checks.AC_PROG_CC_C_O
- checks.AC_PROG_CXX
- checks.AC_SUBST
- checks.AC_TRY_COMPILE
- checks.AC_TRY_LINK
- checks.M4_VARIABLE
- macros.AC_CHECK_DECLS
- macros.AC_CHECK_FUNCS
- macros.AC_CHECK_HEADERS
- macros.AC_CHECK_MEMBERS
- macros.AC_CHECK_TYPES
- utils.AC_LANG_PROGRAM
checks.AC_CHECK_ALIGNOF
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_ALIGNOF(type_name, *, define, includes, language, requires, subst)
Check the alignment of a type.
Original m4 example:
AC_CHECK_ALIGNOF([int])
AC_CHECK_ALIGNOF([double], [[#include <stddef.h>]])
Example:
macros.AC_CHECK_ALIGNOF("int")
macros.AC_CHECK_ALIGNOF("double", includes = ["stddef.h"])
Cross-Compile Warning: This macro is NOT cross-compile friendly. It requires compiling and running code to determine the alignment, which doesn't work when cross-compiling.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_CXX_COMPILER_FLAG
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_CXX_COMPILER_FLAG(flag, define, language, requires, subst)
Check if the C++ compiler supports a specific flag.
Original m4 example:
AC_CHECK_CXX_COMPILER_FLAG([-std=c++17], [CXXFLAGS="$CXXFLAGS -std=c++17"])
Example:
macros.AC_CHECK_CXX_COMPILER_FLAG("-std=c++17")
macros.AC_CHECK_CXX_COMPILER_FLAG("-Wall")
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_C_COMPILER_FLAG
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_C_COMPILER_FLAG(flag, define, language, requires, subst)
Check if the C compiler supports a specific flag.
Original m4 example:
AC_CHECK_C_COMPILER_FLAG([-Wall], [CFLAGS="$CFLAGS -Wall"])
Example:
macros.AC_CHECK_C_COMPILER_FLAG("-Wall")
macros.AC_CHECK_C_COMPILER_FLAG("-std=c99")
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_DECL
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_DECL(symbol, *, name, define, includes, compile_defines, language, requires,
if_true, if_false, subst)
Check if a symbol is declared.
Original m4 example:
AC_CHECK_DECL([NULL], [AC_DEFINE([HAVE_DECL_NULL], [1])], [], [[#include <stddef.h>]])
AC_CHECK_DECL([stdout], [AC_DEFINE([HAVE_DECL_STDOUT], [1])], [], [[#include <stdio.h>]])
Example:
# Cache variable only (no define, no subst)
macros.AC_CHECK_DECL("NULL", includes = ["stddef.h"])
# -> Creates cache variable: "ac_cv_have_decl_NULL"
# Cache variable + define (explicit name)
macros.AC_CHECK_DECL("stdout", includes = ["stdio.h"], define = "HAVE_DECL_STDOUT")
# -> Creates cache variable: "ac_cv_have_decl_stdout"
# -> Creates define: "HAVE_DECL_STDOUT" in config.h
Note:
This is different from AC_CHECK_SYMBOL - it checks if something is
declared (not just #defined).
When no includes are specified, the standard default includes
are used (AC_INCLUDES_DEFAULT from GNU Autoconf).
Use header names like `"stdlib.h"` (not `"#include <stdlib.h>"`).
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_FUNC
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_FUNC(function, name, *, define, code, language, compile_defines, requires, subst)
Check for a function.
Original m4 example:
AC_CHECK_FUNC([malloc])
AC_CHECK_FUNC([mmap], [AC_DEFINE([HAVE_MMAP_FEATURE], [1])])
Example:
# Cache variable only (no define, no subst)
macros.AC_CHECK_FUNC("malloc")
# -> Creates cache variable: "ac_cv_func_malloc"
# Cache variable + define (explicit name)
macros.AC_CHECK_FUNC("mmap", define = "HAVE_MMAP")
# -> Creates cache variable: "ac_cv_func_mmap"
# -> Creates define: "HAVE_MMAP" in config.h
# Cache variable + define (using cache var name)
macros.AC_CHECK_FUNC("mmap", define = True)
# -> Creates cache variable: "ac_cv_func_mmap"
# -> Creates define: "ac_cv_func_mmap" in config.h
# Cache variable + subst
macros.AC_CHECK_FUNC("_Exit", subst = True)
# -> Creates cache variable: "ac_cv_func__Exit"
# -> Creates subst: "ac_cv_func__Exit" in subst.h
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_HEADER
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_HEADER(header, name, define, includes, language, compile_defines, requires, subst)
Check for a header file.
Original m4 example:
AC_CHECK_HEADER([stdio.h])
AC_CHECK_HEADER([pthread.h], [AC_CHECK_FUNC([pthread_create])])
Example:
# Cache variable only (no define, no subst)
macros.AC_CHECK_HEADER("stdio.h")
# -> Creates cache variable: "ac_cv_header_stdio_h"
# Cache variable + define (explicit name)
macros.AC_CHECK_HEADER("pthread.h", define = "HAVE_PTHREAD_H")
# -> Creates cache variable: "ac_cv_header_pthread_h"
# -> Creates define: "HAVE_PTHREAD_H" in config.h
# Cache variable + define (using cache var name)
macros.AC_CHECK_HEADER("pthread.h", define = True)
# -> Creates cache variable: "ac_cv_header_pthread_h"
# -> Creates define: "ac_cv_header_pthread_h" in config.h
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_LIB
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_LIB(library, function, *, name, define, code, language, requires, if_true, if_false,
subst)
Check for a function in a library.
Original m4 example:
AC_CHECK_LIB([m], [cos])
AC_CHECK_LIB([pthread], [pthread_create])
Example:
macros.AC_CHECK_LIB("m", "cos")
macros.AC_CHECK_LIB("pthread", "pthread_create")
Note:
This checks if the specified function is available in the given library.
It attempts to link against -l<library> to verify the library provides
the function.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_MEMBER
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_MEMBER(aggregate_member, *, name, define, includes, language, compile_defines,
requires, if_true, if_false, subst)
Check if a struct or union has a member.
Original m4 example:
AC_CHECK_MEMBER([struct stat.st_rdev], [AC_DEFINE([HAVE_STRUCT_STAT_ST_RDEV], [1])], [], [[#include <sys/stat.h>]])
AC_CHECK_MEMBER([struct tm.tm_zone], [AC_DEFINE([HAVE_STRUCT_TM_TM_ZONE], [1])], [], [[#include <time.h>]])
Example:
macros.AC_CHECK_MEMBER("struct stat.st_rdev", includes = ["sys/stat.h"])
macros.AC_CHECK_MEMBER("struct tm.tm_zone", includes = ["time.h"])
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_SIZEOF
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_SIZEOF(type_name, *, name, define, includes, language, requires, if_true, if_false,
subst)
Check the size of a type.
Original m4 example:
AC_CHECK_SIZEOF([int])
AC_CHECK_SIZEOF([size_t], [], [[#include <stddef.h>]])
Example:
# Cache variable only (no define, no subst)
macros.AC_CHECK_SIZEOF("int")
# -> Creates cache variable: "ac_cv_sizeof_int"
# Cache variable + define (explicit name)
macros.AC_CHECK_SIZEOF("size_t", includes = ["#include <stddef.h>"], define = "SIZEOF_SIZE_T")
# -> Creates cache variable: "ac_cv_sizeof_size_t"
# -> Creates define: "SIZEOF_SIZE_T" in config.h
Cross-Compile Warning: This macro is NOT cross-compile friendly. It requires compiling and running code to determine the size, which doesn't work when cross-compiling.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_CHECK_TYPE
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_CHECK_TYPE(type_name, *, name, define, includes, language, compile_defines, requires,
if_true, if_false, subst)
Check for a type.
Original m4 example:
AC_CHECK_TYPE([size_t])
AC_CHECK_TYPE([pthread_t], [], [], [[#include <pthread.h>]])
Example:
# Cache variable only (no define, no subst)
macros.AC_CHECK_TYPE("size_t")
# -> Creates cache variable: "ac_cv_type_size_t"
# Cache variable + define (explicit name)
macros.AC_CHECK_TYPE("pthread_t", includes = ["#include <pthread.h>"], define = "HAVE_PTHREAD_T")
# -> Creates cache variable: "ac_cv_type_pthread_t"
# -> Creates define: "HAVE_PTHREAD_T" in config.h
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_COMPUTE_INT
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_COMPUTE_INT(define, expression, *, includes, language, requires, subst)
Compute an integer value at compile time.
Original m4 example:
AC_COMPUTE_INT([SIZEOF_INT], [sizeof(int)])
AC_COMPUTE_INT([MAX_VALUE], [1 << 16])
Example:
macros.AC_COMPUTE_INT("SIZEOF_INT", "sizeof(int)")
macros.AC_COMPUTE_INT("MAX_VALUE", "1 << 16")
Cross-Compile Warning: This macro is NOT cross-compile friendly. It requires compiling and running code to compute the value, which doesn't work when cross-compiling.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_C_INLINE
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_C_INLINE(define, language, requires, subst)
Check what inline keyword the compiler supports.
Original m4 example:
AC_C_INLINE
Example:
macros.AC_C_INLINE()
Tests inline keyword and defines it to the appropriate value.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_C_RESTRICT
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_C_RESTRICT(define, language, requires, subst)
Check if the compiler supports restrict keyword.
Original m4 example:
AC_C_RESTRICT
Example:
checks.AC_C_RESTRICT()
Note: If restrict is not supported, the define is set to empty string.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_DEFINE
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_DEFINE(define, value, requires, condition, if_true, if_false, subst)
Define a configuration macro.
Original m4 example:
AC_DEFINE([CUSTOM_VALUE], [42])
AC_DEFINE([ENABLE_FEATURE], [1])
AC_DEFINE([PROJECT_NAME], ["MyProject"])
AC_DEFINE([EMPTY_VALUE], [])
Conditional example (m4):
if test $gl_cv_foo = yes; then
AC_DEFINE([FOO_WORKS], [1])
fi
Example:
# Simple (always define)
macros.AC_DEFINE("CUSTOM_VALUE", "42")
# Conditional (define based on another check's result)
macros.AC_DEFINE(
"FOO_WORKS",
condition = "HAVE_FOO",
if_true = "1", # Value when HAVE_FOO is true
if_false = None, # Don't define when HAVE_FOO is false
)
Note:
This is equivalent to GNU Autoconf's AC_DEFINE macro. When used without
condition, it creates a define that will always be set. When used with
condition, the define is set based on the condition's result.
**Condition Resolution**: If `condition` references a cache variable (e.g.,
`condition="ac_cv_header_foo_h"`), it's evaluated as a truthy check:
- Condition is `true` if the check succeeded AND the value is truthy (non-empty, non-zero)
- Condition is `false` if the check failed OR the value is falsy (empty, "0")
- If condition includes a comparison (e.g., `condition="ac_cv_header_foo_h==1"`),
it performs value comparison instead.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_DEFINE_UNQUOTED
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_DEFINE_UNQUOTED(define, value, requires, condition, if_true, if_false, subst)
Define a configuration macro without quoting (AC_DEFINE_UNQUOTED).
Original m4 example:
AC_DEFINE_UNQUOTED([ICONV_CONST], [$iconv_arg1])
AC_DEFINE_UNQUOTED([VERSION], ["$PACKAGE_VERSION"])
This is similar to AC_DEFINE, but with one key difference:
- AC_DEFINE with empty value generates:
#define NAME /**/ - AC_DEFINE_UNQUOTED with empty value generates:
#define NAME(trailing space)
Example:
# Simple (always define)
macros.AC_DEFINE_UNQUOTED("ICONV_CONST", "")
# Conditional (define based on another check's result)
macros.AC_DEFINE_UNQUOTED(
"ICONV_CONST",
condition = "_gl_cv_iconv_nonconst",
if_true = "", # Empty value - will generate "#define ICONV_CONST " (trailing space)
if_false = "const", # Non-empty value
)
Note:
This is equivalent to GNU Autoconf's AC_DEFINE_UNQUOTED macro. The main
difference from AC_DEFINE is how empty values are rendered in config.h:
AC_DEFINE uses /**/ while AC_DEFINE_UNQUOTED uses a trailing space.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_PROG_CC
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_PROG_CC(requires)
Check that a C compiler is available.
Original m4 example:
AC_PROG_CC
AC_PROG_CC([gcc clang])
Example:
macros.AC_PROG_CC()
Note: This is mostly a no-op in Bazel since the toolchain must be configured, but returns a check that will verify the compiler works.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_PROG_CC_C_O
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_PROG_CC_C_O(define, language, requires, subst)
Check if the compiler supports -c and -o flags simultaneously.
Original m4 example:
AC_PROG_CC_C_O
Example:
macros.AC_PROG_CC_C_O()
Note: If the compiler does NOT support both flags together, the define is set.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_PROG_CXX
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_PROG_CXX(requires)
Check that a C++ compiler is available.
Original m4 example:
AC_PROG_CXX
AC_PROG_CXX([g++ clang++])
Example:
macros.AC_PROG_CXX()
Note: This is mostly a no-op in Bazel since the toolchain must be configured, but returns a check that will verify the compiler works.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_SUBST
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_SUBST(variable, value, requires, condition, if_true, if_false)
Substitute a variable value (equivalent to AC_SUBST in GNU Autoconf).
Original m4 example:
HAVE_DECL_WCSDUP=1; AC_SUBST([HAVE_DECL_WCSDUP])
HAVE_DECL_WCWIDTH=1; AC_SUBST([HAVE_DECL_WCWIDTH])
Conditional example (m4):
if test $gl_cv_sys_struct_lconv_ok = no; then
REPLACE_STRUCT_LCONV=1
else
REPLACE_STRUCT_LCONV=0
fi
AC_SUBST([REPLACE_STRUCT_LCONV])
Example:
# Simple (always set)
macros.AC_SUBST("HAVE_DECL_WCSDUP", "1")
# Conditional (set based on another check's result)
macros.AC_SUBST(
"REPLACE_STRUCT_LCONV",
condition = "HAVE_STRUCT_LCONV_OK",
if_true = "0", # Value when condition is true
if_false = "1", # Value when condition is false
)
Note:
Condition Resolution: If condition references a cache variable (e.g.,
condition="ac_cv_header_foo_h"), it's evaluated as a truthy check:
- Condition is true if the check succeeded AND the value is truthy (non-empty, non-zero)
- Condition is false if the check failed OR the value is falsy (empty, "0")
- If condition includes a comparison (e.g., condition="ac_cv_header_foo_h==1"),
it performs value comparison instead.
In GNU Autoconf, `AC_SUBST` is used to substitute shell variables into
Makefiles and template files (replacing `@VAR@` patterns). When used
without `condition`, it always sets the variable. When used with
`condition`, the value depends on the condition's result.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_TRY_COMPILE
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_TRY_COMPILE(*, code, name, define, includes, language, compile_defines, requires, if_true,
if_false, subst)
Try to compile custom code.
Original m4 example:
AC_TRY_COMPILE([#include <stdio.h>], [printf("test");], [AC_DEFINE([HAVE_PRINTF], [1])])
Example:
load("//autoconf:checks.bzl", "utils")
macros.AC_TRY_COMPILE(
code = "#include <stdio.h>\nint main() { printf(\"test\"); return 0; }",
define = "HAVE_PRINTF",
)
macros.AC_TRY_COMPILE(file = ":test.c", define = "CUSTOM_CHECK")
macros.AC_TRY_COMPILE(
includes = ["pthread.h"],
code = "int x = PTHREAD_CREATE_DETACHED; (void)x;",
define = "HAVE_PTHREAD_CREATE_DETACHED",
)
macros.AC_TRY_COMPILE(
code = utils.AC_LANG_PROGRAM(["#include <stdio.h>"], "printf("test");"),
define = "HAVE_PRINTF",
)
Note: This is a rules_cc_autoconf extension. While GNU Autoconf has an obsolete AC_TRY_COMPILE macro (replaced by AC_COMPILE_IFELSE), this version adds support for file-based checks which is useful in Bazel.
When `includes` is provided along with `code`, the code is treated as
the body of main() and the includes are prepended. For the AC_LANG_PROGRAM
pattern (prologue + body), use `utils.AC_LANG_PROGRAM(prologue, body)` and
pass the result as `code`.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.AC_TRY_LINK
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.AC_TRY_LINK(*, code, name, define, includes, language, compile_defines, requires, if_true,
if_false, subst)
Try to compile and link custom code.
This function mirrors GNU Autoconf's AC_LINK_IFELSE macro, which uses
AC_LANG_PROGRAM to construct test programs. In Bazel, use code (optionally
with includes). For the AC_LANG_PROGRAM pattern, pass
code = utils.AC_LANG_PROGRAM(prologue, body).
Comparison with GNU Autoconf:
GNU Autoconf:
AC_LINK_IFELSE(
[AC_LANG_PROGRAM(
[[#include <stdio.h>]], # Prologue (includes/declarations)
[[printf("test");]] # Body (code inside main())
)],
[AC_DEFINE([HAVE_PRINTF], [1])],
[]
)
Bazel (AC_LANG_PROGRAM pattern):
load("//autoconf:checks.bzl", "utils")
checks.AC_TRY_LINK(
code = utils.AC_LANG_PROGRAM(["#include <stdio.h>"], "printf("test");"),
define = "HAVE_PRINTF",
)
Bazel (includes pattern - backward compat):
checks.AC_TRY_LINK(
includes = ["stdio.h"],
code = "printf("test");", # Treated as body of main()
define = "HAVE_PRINTF",
)
Bazel (raw code pattern):
checks.AC_TRY_LINK(
code = "#include <stdio.h>\nint main() { printf(\"test\"); return 0; }",
define = "HAVE_PRINTF",
)
Note: This is similar to AC_TRY_COMPILE but also links the code, which is necessary to verify that functions are available at link time (not just declared).
PARAMETERS
RETURNS
A JSON-encoded check string for use with the autoconf rule.
checks.M4_VARIABLE
load("@rules_cc_autoconf//autoconf:checks.bzl", "checks")
checks.M4_VARIABLE(define, value, requires, condition, if_true, if_false)
Define a configuration M4 variable.
Original m4 example:
REPLACE_FOO=1
Conditional example (m4):
if test $HAVE_FOO = yes; then
REPLACE_FOO=0
else
REPLACE_FOO=1
fi
Example:
# Simple (always set)
macros.M4_VARIABLE("REPLACE_FOO", "1")
# Conditional (set based on another check's result)
macros.M4_VARIABLE(
"REPLACE_FOO",
condition = "HAVE_FOO",
if_true = "0", # Value when HAVE_FOO is true
if_false = "1", # Value when HAVE_FOO is false
)
Note:
This is similar to AC_SUBST but is useful for tracking the difference
between actual AC_DEFINE values and M4 shell variables used in macros.
Unlike AC_SUBST, this does not set subst=true by default.
PARAMETERS
RETURNS
A JSON-encoded check string for use with the m4 rule.
macros.AC_CHECK_DECLS
load("@rules_cc_autoconf//autoconf:checks.bzl", "macros")
macros.AC_CHECK_DECLS(symbols, *, includes, compile_defines, language, requires, if_true, if_false,
subst)
Check multiple declarations, creating HAVE_DECL_
GNU Autoconf's AC_CHECK_DECLS(symbol, ...) checks whether each symbol is
declared (e.g. in a header). This macro runs AC_CHECK_DECL for each symbol
and sets define names HAVE_DECL_
Upstream: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#index-AC_005fCHECK_002dDECL
M4 example (configure.ac): AC_CHECK_DECLS([NULL, stdout, stderr], [], [], [[#include <stdio.h>]]) AC_CHECK_DECL([NULL], [AC_DEFINE([HAVE_DECL_NULL], [1])], [], [[#include <stddef.h>]])
Bazel example (use #include <foo> in includes):
load("//autoconf:checks.bzl", "macros")
macros.AC_CHECK_DECLS(["NULL", "stdout", "stderr"], includes = ["#include <stdio.h>"])
Defines: HAVE_DECL_NULL, HAVE_DECL_STDOUT, HAVE_DECL_STDERR
PARAMETERS
RETURNS
List of JSON-encoded check strings.
macros.AC_CHECK_FUNCS
load("@rules_cc_autoconf//autoconf:checks.bzl", "macros")
macros.AC_CHECK_FUNCS(functions, *, code, language, compile_defines, requires, subst)
Check multiple functions, creating HAVE_
GNU Autoconf's AC_CHECK_FUNCS(function, ...) checks whether each function
is available (linkable). This macro runs AC_CHECK_FUNC for each function
and sets HAVE_
Upstream: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#index-AC_005fCHECK_002dFUNC
M4 example (configure.ac): AC_CHECK_FUNCS([malloc, free, printf])
Bazel example: load("//autoconf:checks.bzl", "macros") macros.AC_CHECK_FUNCS(["malloc", "free", "printf"])
Defines: HAVE_MALLOC, HAVE_FREE, HAVE_PRINTF
PARAMETERS
RETURNS
List of JSON-encoded check strings.
macros.AC_CHECK_HEADERS
load("@rules_cc_autoconf//autoconf:checks.bzl", "macros")
macros.AC_CHECK_HEADERS(headers, *, language, includes, compile_defines, requires)
Check multiple headers, creating HAVE_
GNU Autoconf's AC_CHECK_HEADERS(header, ...) checks whether each header can
be compiled. This macro runs AC_CHECK_HEADER for each header and sets
HAVE_<HEADER_UPPER> (e.g. HAVE_STDIO_H) following autoconf conventions.
Header names are turned into include directives as #include
Upstream: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#index-AC_005fCHECK_002dHEADERS
Original m4 example:
AC_CHECK_HEADERS([stdio.h, stdlib.h, string.h])
Example:
load("//autoconf:checks.bzl", "macros")
macros.AC_CHECK_HEADERS(["stdio.h", "stdlib.h", "string.h"])
# Defines: HAVE_STDIO_H, HAVE_STDLIB_H, HAVE_STRING_H
PARAMETERS
RETURNS
List of JSON-encoded check strings.
macros.AC_CHECK_MEMBERS
load("@rules_cc_autoconf//autoconf:checks.bzl", "macros")
macros.AC_CHECK_MEMBERS(aggregate_members, *, includes, language, compile_defines, requires,
if_true, if_false, subst)
Check multiple structure members, creating HAVE_
GNU Autoconf's AC_CHECK_MEMBER(aggregate.member, ...) checks whether a
structure member exists. This macro runs AC_CHECK_MEMBER for each
"aggregate.member" and sets HAVE_#include <foo> for headers
that declare the aggregate.
Upstream: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#index-AC_005fCHECK_002dMEMBER
M4 example (configure.ac): AC_CHECK_MEMBERS([struct stat.st_rdev, struct tm.tm_zone], [], [], [[#include <sys/stat.h>]]) AC_CHECK_MEMBER([struct stat.st_rdev], [], [], [[#include <sys/stat.h>]])
Bazel example (use #include <foo> in includes):
load("//autoconf:checks.bzl", "macros")
macros.AC_CHECK_MEMBERS(
["struct stat.st_rdev", "struct tm.tm_zone"],
includes = ["#include <sys/stat.h>", "#include <time.h>"],
)
PARAMETERS
RETURNS
List of JSON-encoded check strings.
macros.AC_CHECK_TYPES
load("@rules_cc_autoconf//autoconf:checks.bzl", "macros")
macros.AC_CHECK_TYPES(types, *, includes, language, compile_defines, requires, if_true, if_false,
subst)
Check multiple types, creating HAVE_
GNU Autoconf's AC_CHECK_TYPE(type, ...) checks whether each type is defined.
This macro runs AC_CHECK_TYPE for each type and sets HAVE_#include <foo> for the headers that declare the types.
Upstream: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#index-AC_005fCHECK_002dTYPE
M4 example (configure.ac): AC_CHECK_TYPES([size_t, pthread_t, pid_t], [], [], [[#include <stddef.h>]]) AC_CHECK_TYPE([pthread_t], [], [], [[#include <pthread.h>]])
Bazel example (use #include <foo> in includes):
load("//autoconf:checks.bzl", "macros")
macros.AC_CHECK_TYPES(["size_t", "pthread_t", "pid_t"], includes = ["#include <stddef.h>", "#include <pthread.h>"])
Defines: HAVE_SIZE_T, HAVE_PTHREAD_T, HAVE_PID_T
PARAMETERS
RETURNS
List of JSON-encoded check strings.
utils.AC_LANG_PROGRAM
load("@rules_cc_autoconf//autoconf:checks.bzl", "utils")
utils.AC_LANG_PROGRAM(prologue, body)
Build program code from prologue and main body (AC_LANG_PROGRAM).
GNU Autoconf's AC_LANG_PROGRAM(prologue, body) expands to a complete test
program: prologue (includes/declarations) followed by main() containing body.
Use the result as the code argument to AC_TRY_COMPILE or AC_TRY_LINK.
Upstream: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#index-AC_005fLANG_005fPROGRAM
M4 example (configure.ac): AC_TRY_COMPILE([AC_LANG_PROGRAM([[#include <stdio.h>]], [printf("test");])], ...) AC_TRY_LINK([AC_LANG_PROGRAM([[#include <langinfo.h>]], [char* cs = nl_langinfo(CODESET); return !cs;])], ...)
Bazel example (use #include <foo> in the prologue list):
load("//autoconf:checks.bzl", "utils", "checks")
checks.AC_TRY_COMPILE(
code = utils.AC_LANG_PROGRAM(["#include <stdio.h>"], "printf("test");"),
define = "HAVE_PRINTF",
)
checks.AC_TRY_LINK(
code = utils.AC_LANG_PROGRAM(
["#include <langinfo.h>"],
"char* cs = nl_langinfo(CODESET); return !cs;",
),
define = "HAVE_LANGINFO_CODESET",
)
PARAMETERS
| Name | Description | Default Value |
|---|---|---|
| prologue | List of strings (include directives or declarations), joined with newlines. | none |
| body | Code inside main() — a string, or a list of strings joined with newlines. | none |
RETURNS
A string suitable for the code parameter of AC_TRY_COMPILE or AC_TRY_LINK.