From 8fbfc82cc2a0c95a7fd10fa3051ecad235cdaa66 Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Sat, 19 Sep 2026 19:34:30 +0200 Subject: [PATCH] Fix issues with configuring flex during build on MacOS (#8412) --- CMakeLists.txt | 31 ++++++++++++++++++++++++++++++- ci/ci-build.bash | 7 +++++++ configure.ac | 12 ++++++++++++ docs/guide/install-cmake.rst | 2 +- src/flexfix | 11 ----------- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d356722a..dceca27bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,7 +93,36 @@ if(OBJCACHE_ENABLED) endif() find_package(BISON) -find_package(FLEX) +# Prefer the header installed with the selected generator, including through +# symlinks such as Homebrew's bin/flex. Explicit cache entries take precedence. +find_program( + FLEX_EXECUTABLE + NAMES flex win-flex win_flex + DOC "Path to the flex executable" +) +if(NOT FLEX_EXECUTABLE) + message( + FATAL_ERROR + "Cannot find Flex; set FLEX_EXECUTABLE to its executable path" + ) +endif() +get_filename_component(flex_real_executable "${FLEX_EXECUTABLE}" REALPATH) +get_filename_component(flex_bin_dir "${flex_real_executable}" DIRECTORY) +find_path( + FLEX_INCLUDE_DIR + FlexLexer.h + PATHS "${flex_bin_dir}/../include" "${flex_bin_dir}" + NO_DEFAULT_PATH + DOC "Path to the flex headers" +) +find_path(FLEX_INCLUDE_DIR FlexLexer.h) +if(NOT FLEX_INCLUDE_DIR) + message( + FATAL_ERROR + "Cannot find FlexLexer.h; set FLEX_INCLUDE_DIR to its directory" + ) +endif() +mark_as_advanced(FLEX_EXECUTABLE FLEX_INCLUDE_DIR) # Build #set_property(GLOBAL PROPERTY JOB_POOLS one_job=1) diff --git a/ci/ci-build.bash b/ci/ci-build.bash index f5e384950..008338f9a 100755 --- a/ci/ci-build.bash +++ b/ci/ci-build.bash @@ -79,6 +79,13 @@ fi if [ "$OPT_LIGHT_DEBUG" = 1 ]; then CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-light-debug" fi +if [ "$(uname -s)" = Darwin ]; then + # Force the use of the Homebrew flex over the one included as part of the + # Apple SDK that's preinstalled on the MacOS Github Action runners. + FLEX_PREFIX=$(brew --prefix flex) + export LEX="$FLEX_PREFIX/bin/flex" + export CPPFLAGS="${CPPFLAGS:+$CPPFLAGS }-I$FLEX_PREFIX/include" +fi autoconf ./configure $CONFIGURE_ARGS CXX="$CXX" diff --git a/configure.ac b/configure.ac index 164d5b304..80f51e09a 100644 --- a/configure.ac +++ b/configure.ac @@ -264,6 +264,7 @@ fi python3_version=$($PYTHON3 --version | head -1) AC_MSG_RESULT([$PYTHON3 --version = $python3_version]) +AC_ARG_VAR([LEX], [Flex executable]) AC_CHECK_PROG(LEX,flex,flex) if test "x$LEX" = "x" ; then AC_MSG_ERROR([Cannot find "flex" in your PATH, please install it]) @@ -271,6 +272,17 @@ fi flex_version=$($LEX --version | head -1) AC_MSG_RESULT([$LEX --version = $flex_version]) +# Use the header installed with the selected generator. +flex_include=$($PYTHON3 -c ' +import pathlib +import shutil +import sys +print(pathlib.Path(shutil.which(sys.argv@<:@1@:>@) or sys.argv@<:@1@:>@).resolve().parent.parent / "include") +' "$LEX") +if test -f "$flex_include/FlexLexer.h"; then + CPPFLAGS="$CPPFLAGS -I$flex_include" +fi + AC_CHECK_PROG(YACC,bison,bison) if test "x$YACC" = "x" ; then AC_MSG_ERROR([Cannot find "bison" in your PATH, please install it]) diff --git a/docs/guide/install-cmake.rst b/docs/guide/install-cmake.rst index 6d840a552..f7ce15acf 100644 --- a/docs/guide/install-cmake.rst +++ b/docs/guide/install-cmake.rst @@ -28,7 +28,7 @@ Quick Install and install. #. For build on Windows using MSVC set environment variable WIN_FLEX_BISON to install directory. For build on Windows/Linux/OS-X using ninja set - the environment variable FLEX_INCLUDE to the directory containing + the CMake variable FLEX_INCLUDE_DIR to the directory containing FlexLexer.h and ensure that flex/bison is available within the PATH. To obtain Verilator sources download diff --git a/src/flexfix b/src/flexfix index 0a64beb0c..24b52514c 100755 --- a/src/flexfix +++ b/src/flexfix @@ -14,7 +14,6 @@ import re import sys -import platform for line in sys.stdin: # Fix flex 2.6.1 warning @@ -48,14 +47,4 @@ for line in sys.stdin: line = re.sub(r'(#line \d+ ".*)_pretmp', r'\1', line) # Fix 'register' storage class specifier is deprecated and incompatible with C++17 line = re.sub(r'register ', '', line) - # Change int to size_t on LexerInput/LexerOutput when using macOS Tahoe - if platform.system() == "Darwin": - try: - osv = platform.mac_ver()[0] # Example "26.2" - if len(osv) > 2 and osv[2] == "." and int(osv[:2]) >= 26: - if "::LexerInput(" in line or "::LexerOutput(" in line: - line = re.sub(r'int ', r'size_t ', line) - except IndexError as e: - print(f"Unexpected index error {e}") - print(line, end='')