From d50dc9a461ed96944c3e81666db378804c6a8320 Mon Sep 17 00:00:00 2001 From: Catherine Date: Fri, 5 Jun 2026 15:54:14 +0000 Subject: [PATCH] CMake: add all Homebrew packages to root search path. Homebrew doesn't provide a merged (FHS) prefix and tools installed from it cannot be expected to appear on PATH. Furthermore, XCode provides some tools and headers (Flex, Bison) which must not be used if a Homebrew alternative is installed. --- CMakeLists.txt | 6 ++++++ cmake/UseHomebrew.cmake | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 cmake/UseHomebrew.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 3deacd232..69b16746c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ include(YosysLinkTarget) include(YosysAbc) include(YosysAbcSubmodule) include(YosysVerific) +include(UseHomebrew) # Build options. set(YOSYS_COMPILER_LAUNCHER "" CACHE STRING "Compiler launcher (ccache, sccache)") @@ -162,6 +163,11 @@ if (MINGW AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSIO endif() # Required dependencies. +if (APPLE) + # In practice, we can't expect paths to Homebrew packages to be configured. + use_homebrew() +endif() + find_package(FLEX) set_package_properties(FLEX PROPERTIES URL "https://github.com/westes/flex" diff --git a/cmake/UseHomebrew.cmake b/cmake/UseHomebrew.cmake new file mode 100644 index 000000000..e94441109 --- /dev/null +++ b/cmake/UseHomebrew.cmake @@ -0,0 +1,22 @@ +# Syntax: +# +# use_homebrew([ROOT ]) +# +# Includes all packages installed in `` (`/opt/homebrew/Cellar` if not specified) +# in `CMAKE_FIND_ROOT_PATH`. +# +function(use_homebrew) + cmake_parse_arguments(PARSE_ARGV 0 arg "" "ROOT" "") + if (NOT arg_ROOT) + set(arg_ROOT /opt/homebrew/Cellar) + endif() + + file(GLOB package_roots ${arg_ROOT}/*/*) # e.g. `/opt/homebrew/Cellar/bison/3.8.2/` + foreach (package_root ${package_roots}) + if (IS_DIRECTORY ${package_root}) + list(APPEND CMAKE_FIND_ROOT_PATH ${package_root}) + endif() + endforeach() + + return(PROPAGATE CMAKE_FIND_ROOT_PATH) +endfunction()