load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@rules_cc//cc:cc_binary.bzl", "cc_binary") load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_cc_autoconf//autoconf:autoconf.bzl", "autoconf") load("@rules_cc_autoconf//autoconf:autoconf_hdr.bzl", "autoconf_hdr") load("@rules_cc_autoconf//autoconf:autoconf_srcs.bzl", "autoconf_srcs") load("@rules_cc_autoconf//autoconf:checks.bzl", "checks") load("@rules_cc_autoconf//autoconf:package_info.bzl", "package_info") LOCAL_DEFINES = [ "HAVE_CONFIG_H", ] + select({ "@platforms//os:macos": [ "_DARWIN_C_SOURCE", ], "//conditions:default": [], }) GENERATED_SRCS = { "lib/alloca.h": "lib/alloca.in.h", "lib/fcntl.h": "lib/fcntl.in.h", "lib/getopt-cdefs.h": "lib/getopt-cdefs.in.h", "lib/getopt.h": "lib/getopt.in.h", "lib/iconv.h": "lib/iconv.in.h", "lib/inttypes.h": "lib/inttypes.in.h", "lib/limits.h": "lib/limits.in.h", "lib/locale.h": "lib/locale.in.h", "lib/malloc/scratch_buffer.gl.h": "lib/malloc/scratch_buffer.h", "lib/math.h": "lib/math.in.h", "lib/sched.h": "lib/sched.in.h", "lib/signal.h": "lib/signal.in.h", "lib/spawn.h": "lib/spawn.in.h", "lib/stdalign.h": "lib/stdalign.in.h", "lib/stddef.h": "lib/stddef.in.h", "lib/stdint.h": "lib/stdint.in.h", "lib/stdio.h": "lib/stdio.in.h", "lib/stdlib.h": "lib/stdlib.in.h", "lib/string.h": "lib/string.in.h", "lib/strings.h": "lib/strings.in.h", "lib/sys/ioctl.h": "lib/sys_ioctl.in.h", "lib/sys/resource.h": "lib/sys_resource.in.h", "lib/sys/stat.h": "lib/sys_stat.in.h", "lib/sys/time.h": "lib/sys_time.in.h", "lib/sys/times.h": "lib/sys_times.in.h", "lib/sys/types.h": "lib/sys_types.in.h", "lib/sys/wait.h": "lib/sys_wait.in.h", "lib/termios.h": "lib/termios.in.h", "lib/textstyle.h": "lib/textstyle.in.h", "lib/time.h": "lib/time.in.h", "lib/unistd.h": "lib/unistd.in.h", "lib/unistr.h": "lib/unistr.in.h", "lib/unitypes.h": "lib/unitypes.in.h", "lib/uniwidth.h": "lib/uniwidth.in.h", "lib/wchar.h": "lib/wchar.in.h", "lib/wctype.h": "lib/wctype.in.h", } BISON_UNIQUE_M4_FILES = [ "m4/bison-check-compiler-flag.m4", "m4/bison-cxx-std.m4", "m4/c-working.m4", "m4/cxx.m4", "m4/flex.m4", "m4/gnulib-comp.m4", "m4/m4.m4", "m4/printf-posix-rpl.m4", "m4/wchar_t.m4", "m4/year2038.m4", ] # Gnulib m4 modules used by bison (from m4/gnulib-comp.m4) # Note: 00gnulib is required for proper template processing GNULIB_M4_DEPS = [ "@rules_cc_autoconf//gnulib/m4/{}".format(file[len("m4/"):-len(".m4")]) for file in glob( include = ["m4/*.m4"], exclude = BISON_UNIQUE_M4_FILES, ) ] # Package information from configure.ac package_info( name = "bison_package_info", package_name = "GNU Bison", aliases = { "VERSION": "PACKAGE_VERSION", }, module_bazel = "MODULE.bazel", package_bugreport = "bug-bison@gnu.org", package_url = "https://www.gnu.org/software/bison/", strip_bcr_version = True, ) autoconf( name = "m4_bison-check-compiler-flag", checks = [], ) autoconf( name = "m4_bison-cxx-std", checks = [ # BISON_CXXSTD from m4/bison-cxx-std.m4 (configure.ac lines 77-83) # Tests compiler flags and AC_SUBSTs CXX_CXXFLAGS # In Bazel, C++ standard is set via toolchain, but we provide flags for Makefile compatibility checks.AC_SUBST("CXX98_CXXFLAGS", "-std=c++98"), checks.AC_SUBST("CXX03_CXXFLAGS", "-std=c++03"), checks.AC_SUBST("CXX11_CXXFLAGS", "-std=c++11"), checks.AC_SUBST("CXX14_CXXFLAGS", "-std=c++14"), checks.AC_SUBST("CXX17_CXXFLAGS", "-std=c++17"), checks.AC_SUBST("CXX20_CXXFLAGS", "-std=c++20"), checks.AC_SUBST("CXX2B_CXXFLAGS", "-std=c++2b"), ], ) # Checks from m4/c-working.m4 # BISON_TEST_FOR_WORKING_C_COMPILER - line 233 # BISON_C_COMPILER_POSIXLY_CORRECT - line 234 autoconf( name = "m4_c-working", checks = [ # BISON_TEST_FOR_WORKING_C_COMPILER # Test that C compiler can compile a simple program # AC_SUBST BISON_C_WORKS to ":" (colon means true in shell) checks.AC_SUBST("BISON_C_WORKS", ":"), # BISON_C_COMPILER_POSIXLY_CORRECT # Check if C compiler supports -g in POSIXLY_CORRECT mode # AC_SUBST C_COMPILER_POSIXLY_CORRECT to "true" or "false" checks.AC_SUBST("C_COMPILER_POSIXLY_CORRECT", "true"), ], ) # Checks from m4/cxx.m4 # BISON_TEST_FOR_WORKING_CXX_COMPILER - line 235 # BISON_CXX_COMPILER_POSIXLY_CORRECT - line 236 autoconf( name = "m4_cxx", checks = [ # BISON_TEST_FOR_WORKING_CXX_COMPILER # Test that C++ compiler can compile and link a simple program # AC_SUBST BISON_CXX_WORKS to ":" (if works), "false" (if doesn't), or ":" (if cross-compiling) checks.AC_SUBST("BISON_CXX_WORKS", ":"), # BISON_CXX_COMPILER_POSIXLY_CORRECT # Check if C++ compiler supports -g in POSIXLY_CORRECT mode # AC_SUBST CXX_COMPILER_POSIXLY_CORRECT to "true" or "false" checks.AC_SUBST("CXX_COMPILER_POSIXLY_CORRECT", "true"), ], ) # Note: m4_bison_cxx_std checks are now in m4_bison-cxx-std (the canonical target name) # Checks from m4/flex.m4 # AC_PROG_LEX - line 258 autoconf( name = "m4_flex", checks = [ # Look for flex or lex program # AC_SUBST LEX - path to lex/flex program checks.AC_SUBST("LEX", "flex"), # AC_SUBST LEX_IS_FLEX - true or false checks.AC_SUBST("LEX_IS_FLEX", "true"), # AC_SUBST FLEX_SUPPORTS_HEADER_OPT - true or false checks.AC_SUBST("FLEX_SUPPORTS_HEADER_OPT", "true"), # AC_SUBST LEX_OUTPUT_ROOT - lex.yy or lexyy checks.AC_SUBST("LEX_OUTPUT_ROOT", "lex.yy"), # AC_SUBST LEXLIB - -lfl, -ll, or empty checks.AC_SUBST("LEXLIB", ""), # AC_DEFINE YYTEXT_POINTER if yytext is a pointer checks.AC_DEFINE("YYTEXT_POINTER", "1"), ], ) autoconf( name = "m4_gnulib-comp", checks = [ checks.AC_DEFINE( "GNULIB_XALLOC", 1, subst = True, ), checks.AC_DEFINE( "GNULIB_XALLOC_DIE", 1, subst = True, ), checks.AC_DEFINE( "GNULIB_STDIO_SINGLE_THREAD", 1, subst = True, ), checks.AC_DEFINE( "USE_UNLOCKED_IO", "GNULIB_STDIO_SINGLE_THREAD", ), # ====================================================================== # Module indicators from gnulib-comp.m4 # These override the defaults (0) set by upstream header modules to # activate gnulib module functionality in generated replacement headers. # ====================================================================== # --- gl_MODULE_INDICATOR (AC_DEFINE in config.h) --- checks.AC_DEFINE("GNULIB_CANONICALIZE", 1), checks.AC_DEFINE("GNULIB_CANONICALIZE_LGPL", 1), checks.AC_DEFINE("GNULIB_CLOEXEC", 1), checks.AC_DEFINE("GNULIB_CLOSE_STREAM", 1), checks.AC_DEFINE("GNULIB_DIRNAME", 1), checks.AC_DEFINE("GNULIB_FD_SAFER_FLAG", 1), checks.AC_DEFINE("GNULIB_FOPEN_GNU", 1), checks.AC_DEFINE("GNULIB_FOPEN_SAFER", 1), checks.AC_DEFINE("GNULIB_GETCWD", 1), checks.AC_DEFINE("GNULIB_LOCK", 1), checks.AC_DEFINE("GNULIB_MSVC_NOTHROW", 1), checks.AC_DEFINE("GNULIB_OPENAT", 1), checks.AC_DEFINE("GNULIB_PIPE2_SAFER", 1), checks.AC_DEFINE("GNULIB_REALLOCARRAY", 1), checks.AC_DEFINE("GNULIB_SNPRINTF", 1), checks.AC_DEFINE("GNULIB_STRERROR", 1), checks.AC_DEFINE("GNULIB_STRERROR_R_POSIX", 1), checks.AC_DEFINE("GNULIB_UNISTR_U8_MBTOUCR", 1), checks.AC_DEFINE("GNULIB_UNISTR_U8_UCTOMB", 1), checks.AC_DEFINE("GNULIB_FSCANF", 1), checks.AC_DEFINE("GNULIB_SCANF", 1), checks.AC_DEFINE("GNULIB_VFPRINTF_POSIX", 1), checks.AC_DEFINE("GNULIB_FDOPENDIR", 1), # --- gl_STDIO_MODULE_INDICATOR (AC_SUBST for stdio.in.h) --- checks.AC_SUBST("GNULIB_FOPEN", 1), checks.AC_SUBST("GNULIB_FPRINTF", 1), checks.AC_SUBST("GNULIB_FPRINTF_POSIX", 1), checks.AC_SUBST("GNULIB_FGETC", 1), checks.AC_SUBST("GNULIB_FGETS", 1), checks.AC_SUBST("GNULIB_FPUTC", 1), checks.AC_SUBST("GNULIB_FPUTS", 1), checks.AC_SUBST("GNULIB_FREAD", 1), checks.AC_SUBST("GNULIB_FSCANF", 1), checks.AC_SUBST("GNULIB_FWRITE", 1), checks.AC_SUBST("GNULIB_GETC", 1), checks.AC_SUBST("GNULIB_GETCHAR", 1), checks.AC_SUBST("GNULIB_GETDELIM", 1), checks.AC_SUBST("GNULIB_GETLINE", 1), checks.AC_SUBST("GNULIB_OBSTACK_PRINTF", 1), checks.AC_SUBST("GNULIB_PERROR", 1), checks.AC_SUBST("GNULIB_PRINTF", 1), checks.AC_SUBST("GNULIB_PRINTF_POSIX", 1), checks.AC_SUBST("GNULIB_PUTC", 1), checks.AC_SUBST("GNULIB_PUTCHAR", 1), checks.AC_SUBST("GNULIB_PUTS", 1), checks.AC_SUBST("GNULIB_RENAME", 1), checks.AC_SUBST("GNULIB_SCANF", 1), checks.AC_SUBST("GNULIB_SNPRINTF", 1), checks.AC_SUBST("GNULIB_SPRINTF_POSIX", 1), checks.AC_SUBST("GNULIB_VASPRINTF", 1), checks.AC_SUBST("GNULIB_VFPRINTF", 1), checks.AC_SUBST("GNULIB_VFPRINTF_POSIX", 1), checks.AC_SUBST("GNULIB_VPRINTF", 1), checks.AC_SUBST("GNULIB_VSNPRINTF", 1), checks.AC_SUBST("GNULIB_VSPRINTF_POSIX", 1), # --- gl_UNISTD_MODULE_INDICATOR (AC_SUBST for unistd.in.h) --- # Note: GNULIB_ACCESS omitted because bison 3.8.2's lib/access.c is # Windows-only, but upstream rules_cc_autoconf sets REPLACE_ACCESS=1 # on macOS (for newer gnulib). Leaving GNULIB_ACCESS=0 prevents the # rpl_access redirect while keeping system access() available. checks.AC_SUBST("GNULIB_CHDIR", 1), checks.AC_SUBST("GNULIB_CLOSE", 1), checks.AC_SUBST("GNULIB_DUP2", 1), checks.AC_SUBST("GNULIB_ENVIRON", 1), checks.AC_SUBST("GNULIB_FCHDIR", 1), checks.AC_SUBST("GNULIB_FSYNC", 1), checks.AC_SUBST("GNULIB_GETCWD", 1), checks.AC_SUBST("GNULIB_GETDTABLESIZE", 1), checks.AC_SUBST("GNULIB_GETOPT_POSIX", 1), checks.AC_SUBST("GNULIB_PIPE", 1), checks.AC_SUBST("GNULIB_PIPE2", 1), checks.AC_SUBST("GNULIB_READLINK", 1), checks.AC_SUBST("GNULIB_RMDIR", 1), checks.AC_SUBST("GNULIB_UNLINK", 1), checks.AC_SUBST("GNULIB_UNISTD_H_GETOPT", 1), # --- gl_STDLIB_MODULE_INDICATOR (AC_SUBST for stdlib.in.h) --- checks.AC_SUBST("GNULIB_CALLOC_POSIX", 1), checks.AC_SUBST("GNULIB_CANONICALIZE_FILE_NAME", 1), checks.AC_SUBST("GNULIB_FREE_POSIX", 1), checks.AC_SUBST("GNULIB_MALLOC_POSIX", 1), checks.AC_SUBST("GNULIB_REALLOCARRAY", 1), checks.AC_SUBST("GNULIB_REALLOC_POSIX", 1), checks.AC_SUBST("GNULIB_REALPATH", 1), checks.AC_SUBST("GNULIB_STRTOD", 1), checks.AC_SUBST("GNULIB_UNSETENV", 1), # --- gl_STRING_MODULE_INDICATOR (AC_SUBST for string.in.h) --- checks.AC_SUBST("GNULIB_FFSL", 1), checks.AC_SUBST("GNULIB_MEMCHR", 1), checks.AC_SUBST("GNULIB_MEMPCPY", 1), checks.AC_SUBST("GNULIB_MEMRCHR", 1), checks.AC_SUBST("GNULIB_RAWMEMCHR", 1), checks.AC_SUBST("GNULIB_STPCPY", 1), checks.AC_SUBST("GNULIB_STPNCPY", 1), checks.AC_SUBST("GNULIB_STRCHRNUL", 1), checks.AC_SUBST("GNULIB_STRDUP", 1), checks.AC_SUBST("GNULIB_STRERROR", 1), checks.AC_SUBST("GNULIB_STRERROR_R", 1), checks.AC_SUBST("GNULIB_STRNDUP", 1), checks.AC_SUBST("GNULIB_STRNLEN", 1), checks.AC_SUBST("GNULIB_STRVERSCMP", 1), # --- gl_MATH_MODULE_INDICATOR (AC_SUBST for math.in.h) --- checks.AC_SUBST("GNULIB_FREXP", 1), checks.AC_SUBST("GNULIB_FREXPL", 1), checks.AC_SUBST("GNULIB_ISNAN", 1), checks.AC_SUBST("GNULIB_ISNAND", 1), checks.AC_SUBST("GNULIB_ISNANF", 1), checks.AC_SUBST("GNULIB_ISNANL", 1), checks.AC_SUBST("GNULIB_LDEXPL", 1), checks.AC_SUBST("GNULIB_SIGNBIT", 1), # --- gl_WCHAR_MODULE_INDICATOR (AC_SUBST for wchar.in.h) --- checks.AC_SUBST("GNULIB_MBRTOWC", 1), checks.AC_SUBST("GNULIB_MBSINIT", 1), checks.AC_SUBST("GNULIB_WCWIDTH", 1), # --- gl_WCTYPE_MODULE_INDICATOR (AC_SUBST for wctype.in.h) --- checks.AC_SUBST("GNULIB_ISWBLANK", 1), checks.AC_SUBST("GNULIB_ISWDIGIT", 1), checks.AC_SUBST("GNULIB_ISWXDIGIT", 1), # --- gl_SPAWN_MODULE_INDICATOR (AC_SUBST for spawn.in.h) --- checks.AC_SUBST("GNULIB_POSIX_SPAWN", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWN_FILE_ACTIONS_ADDCHDIR", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWN_FILE_ACTIONS_DESTROY", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWN_FILE_ACTIONS_INIT", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWNATTR_DESTROY", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWNATTR_INIT", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWNATTR_SETFLAGS", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWNATTR_SETPGROUP", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWNATTR_SETSIGMASK", 1), checks.AC_SUBST("GNULIB_POSIX_SPAWNP", 1), # --- gl_SIGNAL_MODULE_INDICATOR (AC_SUBST for signal.in.h) --- checks.AC_SUBST("GNULIB_RAISE", 1), checks.AC_SUBST("GNULIB_SIGACTION", 1), checks.AC_SUBST("GNULIB_SIGPROCMASK", 1), # --- gl_SYS_STAT_MODULE_INDICATOR (AC_SUBST for sys_stat.in.h) --- checks.AC_SUBST("GNULIB_FSTAT", 1), checks.AC_SUBST("GNULIB_FSTATAT", 1), checks.AC_SUBST("GNULIB_LSTAT", 1), checks.AC_SUBST("GNULIB_STAT", 1), # --- gl_SYS_TIME_MODULE_INDICATOR (AC_SUBST for sys_time.in.h) --- checks.AC_SUBST("GNULIB_GETTIMEOFDAY", 1), # --- gl_SYS_RESOURCE_MODULE_INDICATOR --- checks.AC_SUBST("GNULIB_GETRUSAGE", 1), # --- gl_SYS_WAIT_MODULE_INDICATOR (AC_SUBST for sys_wait.in.h) --- checks.AC_SUBST("GNULIB_WAITPID", 1), # --- gl_FCNTL_MODULE_INDICATOR (AC_SUBST for fcntl.in.h) --- checks.AC_SUBST("GNULIB_FCNTL", 1), checks.AC_SUBST("GNULIB_OPEN", 1), checks.AC_SUBST("GNULIB_OPENAT", 1), # --- gl_ICONV_MODULE_INDICATOR --- checks.AC_SUBST("GNULIB_ICONV", 1), # --- gl_DIRENT_MODULE_INDICATOR --- checks.AC_SUBST("GNULIB_CLOSEDIR", 1), checks.AC_SUBST("GNULIB_DIRFD", 1), checks.AC_SUBST("GNULIB_FDOPENDIR", 1), checks.AC_SUBST("GNULIB_OPENDIR", 1), checks.AC_SUBST("GNULIB_READDIR", 1), checks.AC_SUBST("GNULIB_REWINDDIR", 1), # --- gl_LOCALE_MODULE_INDICATOR --- checks.AC_SUBST("GNULIB_SETLOCALE_NULL", 1), # --- gl_STRINGS_MODULE_INDICATOR --- checks.AC_SUBST("GNULIB_FFS", 1), # ====================================================================== # Compatibility substitutions for older gnulib variable names used by # bison 3.8.2 templates (newer gnulib uses _FOR_*_POSIX suffixed names) # ====================================================================== checks.AC_SUBST("REPLACE_CALLOC", 0), checks.AC_SUBST("REPLACE_MALLOC", 0), checks.AC_SUBST("REPLACE_REALLOC", 0), ], ) # Checks from m4/m4.m4 # AC_PROG_GNU_M4 - line 268 autoconf( name = "m4_m4", checks = [ # Look for GNU M4 1.4.6 or later # AC_SUBST M4 - path to m4 program (used in Makefiles) checks.AC_SUBST("M4", "m4"), # AC_SUBST M4_GNU - --gnu or empty (used in Makefiles) checks.AC_SUBST("M4_GNU", "--gnu"), # AC_SUBST M4_DEBUGFILE - --debugfile or --error-output (used in Makefiles) checks.AC_SUBST("M4_DEBUGFILE", "--debugfile"), # AC_DEFINE_UNQUOTED M4_GNU_OPTION (configure.ac line 270-271) # Note: M4 itself is AC_DEFINE_UNQUOTED in configure.ac, but we use AC_SUBST for Makefile compatibility checks.AC_DEFINE("M4_GNU_OPTION", '"--gnu"'), ], ) # Checks from m4/printf-posix-rpl.m4 # gl_FUNC_PRINTF_POSIX - may be called via gnulib modules autoconf( name = "m4_printf-posix-rpl", checks = [ # Check if printf needs POSIX replacement # AC_SUBST REPLACE_PRINTF - 0 or 1 checks.AC_SUBST("REPLACE_PRINTF", "0"), # AC_DEFINE REPLACE_PRINTF_POSIX - 0 or 1 checks.AC_DEFINE("REPLACE_PRINTF_POSIX", "0"), ], ) # Checks from m4/wchar_t.m4 # gt_TYPE_WCHAR_T - may be called via gnulib autoconf( name = "m4_wchar_t", checks = [ # Check if has the wchar_t type # AC_DEFINE HAVE_WCHAR_T - 1 if found (for C preprocessor) checks.AC_DEFINE("HAVE_WCHAR_T", "1"), # AC_SUBST for stddef.in.h template (@HAVE_WCHAR_T@ substitution) checks.AC_SUBST("HAVE_WCHAR_T", 1), ], ) # Checks from m4/year2038.m4 # gl_YEAR2038_EARLY and gl_YEAR2038_BODY - may be called via gnulib autoconf( name = "m4_year2038", checks = [ # Check for time_t past year 2038 support # On mingw, AC_DEFINE __MINGW_USE_VC2005_COMPAT # For 32-bit systems, may need _TIME_BITS=64 and _FILE_OFFSET_BITS=64 # Most 64-bit systems already have 64-bit time_t, so these are typically not needed # AC_DEFINE _TIME_BITS and _FILE_OFFSET_BITS if needed (left empty for most systems) ] + select({ "@platforms//os:windows": [ checks.AC_DEFINE("__MINGW_USE_VC2005_COMPAT", "1"), ], "//conditions:default": [], }), ) BISON_UNIQUE_AUTOCONF_TARGETS = [ # Strip `m4/` prefix and `.m4` suffix. "m4_{}".format(file[3:-3]) for file in BISON_UNIQUE_M4_FILES ] # Standard autoconf checks from configure.ac # AC_CHECK_HEADERS_ONCE([locale.h]) - line 278 # AC_C_INLINE - line 281 # AC_CHECK_FUNCS_ONCE([setlocale]) - line 288 autoconf( name = "configure_ac", checks = [ # checks.AC_C_INLINE(), checks.AC_CHECK_FUNC("setlocale"), checks.AC_SUBST("GUARD_PREFIX", "GL"), checks.AC_SUBST("GNULIB_LOCALENAME", 0), checks.AC_DEFINE("M4", '"/usr/bin/m4"'), checks.AC_DEFINE("PACKAGE_COPYRIGHT_YEAR", 2026), # Checks from m4/bison-i18n.m4 (BISON_I18N - configure.ac line 295) # Internationalization support (simplified - assumes no NLS for cross-platform build) checks.AC_DEFINE("YYENABLE_NLS", "0"), checks.AC_SUBST("BISON_USE_NLS", "no"), checks.AC_SUBST("BISON_LOCALEDIR", ""), # ================================================================= # Gnulib conditional source compilation gates (COMPILE_* variables) # These determine which replacement sources to compile based on # function availability. COMPILE_*=1 means the function is MISSING # and the gnulib replacement should be compiled. # ================================================================= # rawmemchr — GNU extension, missing on macOS checks.AC_TRY_LINK( name = "bison_cv_func_rawmemchr", code = """\ #define _GNU_SOURCE 1 #include int main(void) { (void)rawmemchr; return 0; } """, ), checks.AC_SUBST( "COMPILE_RAWMEMCHR", condition = "!bison_cv_func_rawmemchr", ), checks.AC_SUBST( "COMPILE_MEMPCPY", condition = "!ac_cv_func_mempcpy", ), checks.AC_SUBST("COMPILE_REALLOCARRAY", "1"), checks.AC_TRY_LINK( name = "bison_cv_func_strverscmp", code = """\ #include int main(void) { return strverscmp("1.0", "2.0"); } """, ), checks.AC_SUBST( "COMPILE_STRVERSCMP", condition = "!bison_cv_func_strverscmp", ), checks.AC_TRY_LINK( name = "bison_cv_func_canonicalize_file_name", code = """\ #ifdef __cplusplus extern "C" #endif char *canonicalize_file_name(const char *); int main(void) { return canonicalize_file_name(".") != 0; } """, ), checks.AC_SUBST( "COMPILE_CANONICALIZE_LGPL", condition = "!bison_cv_func_canonicalize_file_name", ), # error.c — compiled when error_at_line() is missing (macOS) checks.AC_CHECK_FUNC("error_at_line"), checks.AC_SUBST( "COMPILE_ERROR", condition = "!ac_cv_func_error_at_line", ), # fpending.c — compiled when __fpending() is missing (macOS) checks.AC_TRY_LINK( name = "bison_cv_func___fpending", code = """\ #include #if HAVE_STDIO_EXT_H # include #endif int main(void) { return ! __fpending(stdin); } """, ), checks.AC_SUBST( "COMPILE_FPENDING", condition = "!bison_cv_func___fpending", ), # fseterr.c — compiled when __fseterr() is missing (macOS) checks.AC_TRY_LINK( name = "bison_cv_func___fseterr", code = """\ #include #ifdef __cplusplus extern "C" #endif void __fseterr(FILE *); int main(void) { __fseterr(stdin); return 0; } """, ), checks.AC_SUBST( "COMPILE_FSETERR", condition = "!bison_cv_func___fseterr", ), checks.AC_SUBST("COMPILE_OBSTACK_PRINTF", "1"), ] + select({ "@platforms//os:linux": [ # getopt: __GETOPT_PREFIX=rpl_ always set → replacement always needed checks.AC_SUBST("COMPILE_GETOPT", "1"), checks.AC_SUBST("COMPILE_OPEN", "0"), checks.AC_SUBST("COMPILE_UNLINK", "0"), checks.AC_SUBST("COMPILE_STRERROR_OVERRIDE", "0"), checks.AC_SUBST("COMPILE_MBRTOWC", "1"), checks.AC_SUBST("COMPILE_SPAWN_FACTION_ADDCHDIR", "1"), ], "@platforms//os:macos": [ checks.AC_SUBST("COMPILE_GETOPT", "1"), checks.AC_SUBST("COMPILE_OPEN", "1"), checks.AC_SUBST("COMPILE_UNLINK", "1"), checks.AC_SUBST("COMPILE_STRERROR_OVERRIDE", "1"), checks.AC_SUBST("COMPILE_MBRTOWC", "0"), checks.AC_SUBST("COMPILE_SPAWN_FACTION_ADDCHDIR", "1"), ], "@platforms//os:windows": [ checks.AC_SUBST("COMPILE_GETOPT", "1"), checks.AC_SUBST("COMPILE_OPEN", "0"), checks.AC_SUBST("COMPILE_UNLINK", "0"), checks.AC_SUBST("COMPILE_STRERROR_OVERRIDE", "0"), checks.AC_SUBST("COMPILE_MBRTOWC", "0"), checks.AC_SUBST("COMPILE_SPAWN_FACTION_ADDCHDIR", "1"), # Override string_h toolchain defaults for POSIX functions missing # on Windows. Without these the generated string.h skips their # declarations, causing implicit-int errors on MSVC. checks.AC_SUBST("HAVE_STPCPY", 0), checks.AC_SUBST("HAVE_STPNCPY", 0), ], "//conditions:default": [ checks.AC_SUBST("COMPILE_GETOPT", "1"), checks.AC_SUBST("COMPILE_OPEN", "0"), checks.AC_SUBST("COMPILE_UNLINK", "0"), checks.AC_SUBST("COMPILE_STRERROR_OVERRIDE", "0"), checks.AC_SUBST("COMPILE_MBRTOWC", "0"), checks.AC_SUBST("COMPILE_SPAWN_FACTION_ADDCHDIR", "1"), ], }), deps = [ ":bison_package_info", ] + BISON_UNIQUE_AUTOCONF_TARGETS + GNULIB_M4_DEPS, ) # Generate header files from templates using autoconf_hdr # These templates contain @PLACEHOLDER@ markers that need to be processed [ autoconf_hdr( name = hdr[:-len(".h")] + "_h", out = hdr, defaults = True, inlines = { "/* The definition of _GL_ARG_NONNULL is copied here. */": "lib/arg-nonnull.h", "/* The definition of _GL_WARN_ON_USE is copied here. */": "lib/warn-on-use.h", "/* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */": "lib/c++defs.h", }, mode = "subst", substitutions = { "libc_hidden_proto": "// libc_hidden_proto", "__always_inline": "inline _GL_ATTRIBUTE_ALWAYS_INLINE", "__glibc_likely": "_GL_LIKELY", "__glibc_unlikely": "_GL_UNLIKELY", }, template = template, deps = [":configure_ac"], ) for hdr, template in GENERATED_SRCS.items() ] autoconf_hdr( name = "lib/config_h", out = "lib/config.h", template = "lib/config.in.h", deps = [":configure_ac"], ) write_file( name = "configmake_h_src", out = "lib/configmake.h", content = [ "#define LOCALEDIR \"\"", "#define PKGDATADIR \"bison/data\"", "", ], newline = "unix", tags = ["manual"], ) BISON_COPTS = select({ "@rules_cc//cc/compiler:clang": [ "-w", "-std=c11", "-Wno-unused-but-set-variable", ], "@rules_cc//cc/compiler:gcc": [ "-w", "-std=c11", ], "@rules_cc//cc/compiler:msvc-cl": [ # C4116: unnamed type definition in parentheses "/wd4116", ], "//conditions:default": [], }) # Gnulib conditional sources — each file is compiled only when its condition # variable evaluates to true. Conditions come from three sources: # - REPLACE_*: upstream gnulib modules (function exists but is buggy/missing) # - COMPILE_*: configure_ac checks (function absent on platform) # - ac_cv_define_REPLACE_POSIX_SPAWN: spawn subsystem needs full replacement autoconf_srcs( name = "gnulib_conditional_srcs", srcs = { # --- REPLACE_* gated (upstream gnulib decides) --- "lib/asprintf.c": "REPLACE_VASPRINTF", # --- COMPILE_* gated (function absent → replacement compiled) --- "lib/canonicalize-lgpl.c": "COMPILE_CANONICALIZE_LGPL", "lib/error.c": "COMPILE_ERROR", "lib/fcntl.c": "REPLACE_FCNTL", "lib/fopen.c": "REPLACE_FOPEN", "lib/fpending.c": "COMPILE_FPENDING", "lib/fprintf.c": "REPLACE_FPRINTF", "lib/free.c": "REPLACE_FREE", "lib/fseterr.c": "COMPILE_FSETERR", "lib/fstat.c": "REPLACE_FSTAT", "lib/getopt.c": "COMPILE_GETOPT", "lib/getopt1.c": "COMPILE_GETOPT", "lib/iconv_open.c": "REPLACE_ICONV_OPEN", "lib/lstat.c": "REPLACE_LSTAT", "lib/mbrtowc.c": "COMPILE_MBRTOWC", "lib/mempcpy.c": "COMPILE_MEMPCPY", "lib/obstack_printf.c": "COMPILE_OBSTACK_PRINTF", "lib/open.c": "COMPILE_OPEN", "lib/perror.c": "REPLACE_PERROR", "lib/printf.c": "REPLACE_PRINTF", "lib/rawmemchr.c": "COMPILE_RAWMEMCHR", "lib/readlink.c": "REPLACE_READLINK", "lib/reallocarray.c": "COMPILE_REALLOCARRAY", "lib/rename.c": "REPLACE_RENAME", "lib/snprintf.c": "REPLACE_SNPRINTF", # --- Spawn replacement (all-or-nothing when REPLACE_POSIX_SPAWN=1) --- "lib/spawn.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawn_faction_addchdir.c": "COMPILE_SPAWN_FACTION_ADDCHDIR", "lib/spawn_faction_addclose.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawn_faction_adddup2.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawn_faction_addopen.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawn_faction_destroy.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawn_faction_init.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawnattr_destroy.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawnattr_init.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawnattr_setflags.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawnattr_setpgroup.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawnattr_setsigmask.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawni.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/spawnp.c": "ac_cv_define_REPLACE_POSIX_SPAWN", "lib/sprintf.c": "REPLACE_SPRINTF", "lib/stat.c": "REPLACE_STAT", "lib/strchrnul.c": "REPLACE_STRCHRNUL", "lib/strerror-override.c": "COMPILE_STRERROR_OVERRIDE", "lib/strerror.c": "REPLACE_STRERROR", "lib/strerror_r.c": "REPLACE_STRERROR_R", "lib/strverscmp.c": "COMPILE_STRVERSCMP", "lib/unlink.c": "COMPILE_UNLINK", "lib/vasprintf.c": "REPLACE_VASPRINTF", "lib/vfprintf.c": "REPLACE_VFPRINTF", "lib/vsnprintf.c": "REPLACE_VSNPRINTF", "lib/vsprintf.c": "REPLACE_VSPRINTF", }, defaults = True, deps = [":configure_ac"], ) # Shared headers for lib/ targets (mirrors autotools AM_CPPFLAGS = -I./lib) cc_library( name = "headers", hdrs = ["lib/config.h"] + [ ":{}".format(hdr) for hdr in GENERATED_SRCS.keys() ] + glob( include = ["lib/**/*.h"], exclude = GENERATED_SRCS.keys() + [ "lib/config.h", "lib/configmake.h", ], ) + [ "src/scan-code.c", "src/scan-gram.c", "src/scan-skel.c", ":configmake_h_src", ], includes = ["lib"], textual_hdrs = [ "lib/timevar.def", "lib/printf-frexp.c", ], ) alias( name = "libbison", actual = ":lib/bison", ) # Unconditional sources are always compiled on all platforms. # Conditional sources are gated by autoconf_srcs targets based on actual # check results (REPLACE_*, HAVE_*), matching native autoconf behavior. cc_library( name = "lib/bison", srcs = [ # === Unconditional sources (from lib_libbison_a_SOURCES in gnulib.mk) === "lib/allocator.c", "lib/areadlink.c", "lib/argmatch.c", "lib/asnprintf.c", "lib/basename.c", "lib/basename-lgpl.c", "lib/binary-io.c", "lib/bitrotate.c", "lib/bitset.c", "lib/bitset/array.c", "lib/bitset/list.c", "lib/bitset/stats.c", "lib/bitset/table.c", "lib/bitset/vector.c", "lib/bitsetv.c", "lib/c-ctype.c", "lib/c-strcasecmp.c", "lib/c-strncasecmp.c", "lib/canonicalize.c", "lib/careadlinkat.c", "lib/cloexec.c", "lib/close-stream.c", "lib/closeout.c", "lib/concat-filename.c", "lib/dirname.c", "lib/dirname-lgpl.c", "lib/dup-safer.c", "lib/dup-safer-flag.c", "lib/execute.c", "lib/exitfail.c", "lib/fatal-signal.c", "lib/fd-safer.c", "lib/fd-safer-flag.c", "lib/file-set.c", "lib/findprog-in.c", "lib/fopen-safer.c", "lib/fstrcmp.c", "lib/get-errno.c", "lib/gethrxtime.c", "lib/getprogname.c", "lib/gettime.c", "lib/gl_array_list.c", "lib/gl_hash_map.c", "lib/gl_linked_list.c", "lib/gl_list.c", "lib/gl_map.c", "lib/gl_oset.c", "lib/gl_rbtree_oset.c", "lib/gl_rbtreehash_list.c", "lib/gl_xlist.c", "lib/gl_xmap.c", "lib/glthread/lock.c", "lib/glthread/threadlib.c", "lib/glthread/tls.c", "lib/hash.c", "lib/hash-pjw.c", "lib/hash-triple-simple.c", "lib/ialloc.c", "lib/integer_length.c", "lib/integer_length_l.c", "lib/localcharset.c", "lib/malloc/scratch_buffer_dupfree.c", "lib/malloc/scratch_buffer_grow.c", "lib/malloc/scratch_buffer_grow_preserve.c", "lib/malloc/scratch_buffer_set_array_size.c", "lib/math.c", "lib/mbchar.c", "lib/mbfile.c", "lib/mbswidth.c", "lib/obstack.c", "lib/path-join.c", "lib/pipe-safer.c", "lib/pipe2.c", "lib/pipe2-safer.c", "lib/printf-args.c", "lib/printf-frexp.c", "lib/printf-frexpl.c", "lib/printf-parse.c", "lib/progname.c", "lib/quotearg.c", "lib/readline.c", "lib/sig-handler.c", "lib/spawn-pipe.c", "lib/stripslash.c", "lib/timespec.c", "lib/timevar.c", "lib/unicodeio.c", "lib/unistd.c", "lib/unistr/u8-mbtoucr.c", "lib/unistr/u8-uctomb.c", "lib/unistr/u8-uctomb-aux.c", "lib/uniwidth/width.c", "lib/vasnprintf.c", "lib/wait-process.c", "lib/wctype-h.c", "lib/xalloc-die.c", "lib/xconcat-filename.c", "lib/xhash.c", "lib/xmalloc.c", "lib/xmemdup0.c", "lib/xreadlink.c", "lib/xsize.c", "lib/xstrndup.c", "lib/xtime.c", # Always-enabled gnulib modules (gl_GNULIB_ENABLED_* always true for bison) "lib/hard-locale.c", "lib/malloca.c", "lib/setlocale_null.c", "lib/stat-time.c", ] + [ ":gnulib_conditional_srcs", ] + select({ "@platforms//os:windows": [ # POSIX function replacements (missing on Windows) "lib/ffsl.c", "lib/fsync.c", "lib/getdtablesize.c", "lib/getrusage.c", "lib/gettimeofday.c", "lib/pipe.c", "lib/sigaction.c", "lib/sigprocmask.c", "lib/stpcpy.c", "lib/stpncpy.c", "lib/strndup.c", "lib/waitpid.c", "lib/wcwidth.c", # Windows-specific gnulib support "lib/msvc-inval.c", "lib/msvc-nothrow.c", "lib/stat-w32.c", "lib/windows-mutex.c", "lib/windows-once.c", "lib/windows-recmutex.c", "lib/windows-rwlock.c", "lib/windows-spawn.c", "lib/windows-tls.c", ], "//conditions:default": [], }), copts = BISON_COPTS, local_defines = LOCAL_DEFINES + [ "DEFAULT_TEXT_DOMAIN=\\\"bison-gnulib\\\"", ], deps = [":headers"], ) alias( name = "liby", actual = ":lib/y", ) cc_library( name = "lib/y", srcs = [ "lib/main.c", "lib/yyerror.c", ], implementation_deps = [":headers"], local_defines = LOCAL_DEFINES, ) BISON_SRC_SRCS = glob( include = [ "src/*.c", ], exclude = [ "src/bazel_runfiles.cc", "src/i18n-strings.c", "src/scan-code.c", "src/scan-gram.c", "src/scan-skel.c", "src/main.c", ], ) BISON_HDRS_SRCS = glob( include = ["src/*.h"], ) cc_library( name = "bison_bazel_runfiles", srcs = ["src/bazel_runfiles.cc"], deps = ["@rules_cc//cc/runfiles"], ) # Runfiles for bison binary filegroup( name = "bison_data", srcs = glob(["data/**/*"]) + [ "@rules_m4//m4:current_m4_toolchain", ], ) # Link options for different compilers BISON_LINKOPTS = select({ "@rules_cc//cc/compiler:msvc-cl": [ # LNK4001: no object files specified; libraries used "/IGNORE:4001", ], "//conditions:default": [], }) # Bison binary cc_binary( name = "bin/bison", srcs = BISON_SRC_SRCS + ["src/main.c"] + BISON_HDRS_SRCS, copts = BISON_COPTS, data = [":bison_data"], includes = ["src"], linkopts = BISON_LINKOPTS + select({ "@platforms//os:linux": ["-lpthread"], "@platforms//os:macos": ["-liconv"], "//conditions:default": [], }), local_defines = LOCAL_DEFINES, visibility = ["//visibility:public"], deps = [ ":bison_bazel_runfiles", ":headers", ":libbison", ":liby", ], ) alias( name = "bison", actual = ":bin/bison", visibility = ["//visibility:public"], ) # Yacc binary (alias for bison) alias( name = "yacc", actual = ":bin/bison", visibility = ["//visibility:public"], ) genrule( name = "bison_help", testonly = True, outs = ["bison_help.txt"], cmd = "$(execpath :bison) --help > $@", cmd_bat = "$(execpath :bison) --help > $@", tools = [":bison"], )