#!/bin/bash # gnulib-cross-fix.sh — Fix gnulib cross-compilation misdetections in config.h # # Usage: gnulib-cross-fix.sh # # gnulib's configure can't run test programs during cross-compilation for # x86_64-unknown-redox (relibc target). It assumes system headers/types are # missing and generates broken fallback #defines and replacement headers. # # This script patches the generated config.h to correct those misdetections, # telling gnulib that relibc provides standard POSIX headers and types. # # Call this AFTER configure but BEFORE make in any gnulib-based recipe: # "${COOKBOOK_CONFIGURE}" "${COOKBOOK_CONFIGURE_FLAGS[@]}" # gnulib-cross-fix.sh "${COOKBOOK_BUILD}/lib/config.h" # "${COOKBOOK_MAKE}" -j "${COOKBOOK_MAKE_JOBS}" set -euo pipefail CONFIG_H="${1:?Usage: gnulib-cross-fix.sh }" if [ ! -f "$CONFIG_H" ]; then echo "gnulib-cross-fix: $CONFIG_H not found, skipping" >&2 exit 0 fi echo "gnulib-cross-fix: patching $CONFIG_H for relibc cross-compilation" # Comment out broken type fallbacks (relibc provides these correctly) perl -pi -e 's,^#define gid_t int\b,/* #undef gid_t -- relibc provides */,' "$CONFIG_H" perl -pi -e 's,^#define uid_t int\b,/* #undef uid_t -- relibc provides */,' "$CONFIG_H" perl -pi -e 's,^#define intmax_t long long\b,/* #undef intmax_t -- relibc provides */,' "$CONFIG_H" perl -pi -e 's,^#define ssize_t int\b,/* #undef ssize_t -- relibc provides */,' "$CONFIG_H" perl -pi -e 's,^#define ptrdiff_t long\b,/* #undef ptrdiff_t -- relibc provides */,' "$CONFIG_H" perl -pi -e 's,^#define nlink_t int\b,/* #undef nlink_t -- relibc provides */,' "$CONFIG_H" perl -pi -e 's,^#define mbstate_t int\b,/* #undef mbstate_t -- relibc provides */,' "$CONFIG_H" # Force HAVE_ macros for standard headers (gnulib can't detect these during cross-compilation) for macro in \ HAVE_INTTYPES_H \ HAVE_INTTYPES_H_WITH_UINTMAX \ HAVE_INTMAX_T \ HAVE_WCHAR_H \ HAVE_WCTYPE_H \ HAVE_STDLIB_H \ HAVE_SPAWN_H \ HAVE_POSIX_SPAWNATTR_T \ HAVE_POSIX_SPAWN_FILE_ACTIONS_T \ HAVE_SIGNED_WCHAR_T \ HAVE_SIGSET_T \ HAVE_SIGACTION \ HAVE_SIGADDSET \ HAVE_SIGDELSET \ HAVE_SIGEMPTYSET \ HAVE_SIGFILLSET \ HAVE_SIGISMEMBER \ HAVE_SIGPENDING \ HAVE_SIGPROCMASK \ HAVE_SIGSUSPEND \ HAVE_BTOWC \ HAVE_MBRTOWC \ HAVE_MBSINIT \ HAVE_WCRTOMB \ HAVE_WCTOB \ HAVE_WCWIDTH \ HAVE_MBSRTOWCS \ HAVE_WCSWIDTH \ ; do perl -pi -e "s,/\\* #undef ${macro} \\*/,#define ${macro} 1," "$CONFIG_H" || true done # Also patch generated replacement headers to disable rpl_ function renames. # gnulib generates lib/wchar.h, lib/signal.h, lib/stdio.h etc. with #define func rpl_func # when REPLACE_* is 1. Since relibc provides these functions, we need to undo the renames. LIB_DIR="$(dirname "$CONFIG_H")" for hdr in "$LIB_DIR"/wchar.h "$LIB_DIR"/signal.h "$LIB_DIR"/stdio.h "$LIB_DIR"/stdlib.h "$LIB_DIR"/string.h; do if [ -f "$hdr" ]; then # Replace rpl_ function names with the originals in #define lines # e.g. #define btowc rpl_btowc → /* #define btowc rpl_btowc -- relibc provides */ perl -pi -e 's,^#(\s*)define (\w+) rpl_\2,/* #$1define $2 rpl_$2 -- relibc provides */,' "$hdr" || true fi done # NOTE: We do NOT replace #include_next directives. The cross-compiler (Clang) # supports #include_next natively and the sysroot include path is set up correctly # so that #include_next skips the build directory and finds relibc's system headers. # # Previous versions of this script replaced #include_next with #include , # but this caused self-recursion in newer gnulib (m4-1.14+, flex) that removed the # _GL_ALREADY_INCLUDING_WCHAR_H guard. The split double-inclusion guard pattern # (_GL_WCHAR_H etc.) prevents re-entry into the normal section, but the special # invocation section (for __need_mbstate_t/__need_wint_t) is unguarded and would # recurse if #include found the gnulib wrapper instead of the system header. echo "gnulib-cross-fix: done"