From 5e5132e1b1a090b8d9f58e0316fced47479c9a53 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 11 Sep 2023 19:11:54 -0400 Subject: MinGW: Search for packages in standard MSYSTEM environment prefixes MSYS2 and similar MinGW/MSYS distributions define development environments with a `MSYSTEM` environment variable. Each such environment has a documented installation prefix for its packages, often provided by a `MSYSTEM_PREFIX` environment variable. Since commit 84a25fc263 (cmake_host_system_information: Add MSYSTEM_PREFIX query, 2023-09-08) we can look up this prefix. Add `$MSYSTEM_PREFIX/local` and `$MSYSTEM_PREFIX` to our system search prefixes when targeting MinGW under `MSYSTEM` environments. This is their equivalent to `/usr/local` and `/usr`, which we search by default on UNIX systems. Issue: #24216 --- Help/release/dev/mingw-search-prefixes.rst | 6 ++++++ Help/variable/CMAKE_SYSTEM_PREFIX_PATH.rst | 6 ++++++ Modules/Platform/Windows-GNU.cmake | 19 +++++++++++++++++++ Tests/RunCMake/CMakeLists.txt | 8 ++++---- Tests/RunCMake/find_file/MSYSTEM_PREFIX-stdout.txt | 1 + Tests/RunCMake/find_file/MSYSTEM_PREFIX.cmake | 5 +++++ .../find_file/MSYSTEM_PREFIX/include/msystem_prefix.h | 0 Tests/RunCMake/find_file/RunCMakeTest.cmake | 4 ++++ Tests/RunCMake/find_library/MSYSTEM_PREFIX-stdout.txt | 1 + Tests/RunCMake/find_library/MSYSTEM_PREFIX.cmake | 5 +++++ .../MSYSTEM_PREFIX/lib/libmsystem_prefix.a | 0 Tests/RunCMake/find_library/RunCMakeTest.cmake | 4 ++++ Tests/RunCMake/find_package/MSYSTEM_PREFIX-stdout.txt | 1 + Tests/RunCMake/find_package/MSYSTEM_PREFIX.cmake | 5 +++++ .../find_package/MSYSTEM_PREFIX/MsysPfxConfig.cmake | 0 Tests/RunCMake/find_package/RunCMakeTest.cmake | 4 ++++ Tests/RunCMake/find_path/MSYSTEM_PREFIX-stdout.txt | 1 + Tests/RunCMake/find_path/MSYSTEM_PREFIX.cmake | 5 +++++ .../find_path/MSYSTEM_PREFIX/include/msystem_prefix.h | 0 Tests/RunCMake/find_path/RunCMakeTest.cmake | 4 ++++ 20 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 Help/release/dev/mingw-search-prefixes.rst create mode 100644 Tests/RunCMake/find_file/MSYSTEM_PREFIX-stdout.txt create mode 100644 Tests/RunCMake/find_file/MSYSTEM_PREFIX.cmake create mode 100644 Tests/RunCMake/find_file/MSYSTEM_PREFIX/include/msystem_prefix.h create mode 100644 Tests/RunCMake/find_library/MSYSTEM_PREFIX-stdout.txt create mode 100644 Tests/RunCMake/find_library/MSYSTEM_PREFIX.cmake create mode 100644 Tests/RunCMake/find_library/MSYSTEM_PREFIX/lib/libmsystem_prefix.a create mode 100644 Tests/RunCMake/find_package/MSYSTEM_PREFIX-stdout.txt create mode 100644 Tests/RunCMake/find_package/MSYSTEM_PREFIX.cmake create mode 100644 Tests/RunCMake/find_package/MSYSTEM_PREFIX/MsysPfxConfig.cmake create mode 100644 Tests/RunCMake/find_path/MSYSTEM_PREFIX-stdout.txt create mode 100644 Tests/RunCMake/find_path/MSYSTEM_PREFIX.cmake create mode 100644 Tests/RunCMake/find_path/MSYSTEM_PREFIX/include/msystem_prefix.h diff --git a/Help/release/dev/mingw-search-prefixes.rst b/Help/release/dev/mingw-search-prefixes.rst new file mode 100644 index 0000000..1b5c3c7 --- /dev/null +++ b/Help/release/dev/mingw-search-prefixes.rst @@ -0,0 +1,6 @@ +mingw-search-prefixes +--------------------- + +* When using MinGW tools in a ``MSYSTEM`` environment on Windows, + the ``$MSYSTEM_PREFIX/local`` and ``$MSYSTEM_PREFIX`` prefixes are + now added to :variable:`CMAKE_SYSTEM_PREFIX_PATH`. diff --git a/Help/variable/CMAKE_SYSTEM_PREFIX_PATH.rst b/Help/variable/CMAKE_SYSTEM_PREFIX_PATH.rst index c8b5815..8dfcdc5 100644 --- a/Help/variable/CMAKE_SYSTEM_PREFIX_PATH.rst +++ b/Help/variable/CMAKE_SYSTEM_PREFIX_PATH.rst @@ -38,6 +38,12 @@ OpenBSD: Unix: * ``ENV{CONDA_PREFIX}`` when using a conda compiler +MSYSTEM environment with MinGW toolchain: + .. versionadded:: 3.28 + + * ``ENV{MSYSTEM_PREFIX}/local`` + * ``ENV{MSYSTEM_PREFIX}`` + Windows: * ``ENV{ProgramW6432}`` * ``ENV{ProgramFiles}`` diff --git a/Modules/Platform/Windows-GNU.cmake b/Modules/Platform/Windows-GNU.cmake index 088b238..412af6b 100644 --- a/Modules/Platform/Windows-GNU.cmake +++ b/Modules/Platform/Windows-GNU.cmake @@ -10,6 +10,25 @@ set(__WINDOWS_GNU 1) set(MINGW 1) +# On Windows hosts, in MSYSTEM environments, search standard prefixes. +if(CMAKE_HOST_WIN32) + # Bootstrap CMake does not have cmake_host_system_information. + if(COMMAND cmake_host_system_information) + cmake_host_system_information(RESULT _MSYSTEM_PREFIX QUERY MSYSTEM_PREFIX) + elseif(IS_DIRECTORY "$ENV{MSYSTEM_PREFIX}") + set(_MSYSTEM_PREFIX "$ENV{MSYSTEM_PREFIX}") + endif() + + # Search this MSYSTEM environment's equivalent to /usr/local and /usr. + if(_MSYSTEM_PREFIX) + list(PREPEND CMAKE_SYSTEM_PREFIX_PATH "${_MSYSTEM_PREFIX}") + if(IS_DIRECTORY "${_MSYSTEM_PREFIX}/local") + list(PREPEND CMAKE_SYSTEM_PREFIX_PATH "${_MSYSTEM_PREFIX}/local") + endif() + endif() + unset(_MSYSTEM_PREFIX) +endif() + set(CMAKE_IMPORT_LIBRARY_PREFIX "lib") set(CMAKE_SHARED_LIBRARY_PREFIX "lib") set(CMAKE_SHARED_MODULE_PREFIX "lib") diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 02efb25..bf93042 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -518,10 +518,10 @@ foreach(var endforeach() add_RunCMake_test(file-DOWNLOAD) add_RunCMake_test(file-RPATH -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}) -add_RunCMake_test(find_file) -add_RunCMake_test(find_library -DCYGWIN=${CYGWIN} -DMSYS=${MSYS}) -add_RunCMake_test(find_package -DMSYS=${MSYS}) -add_RunCMake_test(find_path) +add_RunCMake_test(find_file -DMINGW=${MINGW}) +add_RunCMake_test(find_library -DMINGW=${MINGW} -DCYGWIN=${CYGWIN} -DMSYS=${MSYS}) +add_RunCMake_test(find_package -DMINGW=${MINGW} -DMSYS=${MSYS}) +add_RunCMake_test(find_path -DMINGW=${MINGW}) add_RunCMake_test(find_program -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}) add_RunCMake_test(foreach) add_RunCMake_test(function) diff --git a/Tests/RunCMake/find_file/MSYSTEM_PREFIX-stdout.txt b/Tests/RunCMake/find_file/MSYSTEM_PREFIX-stdout.txt new file mode 100644 index 0000000..ab9b9d5 --- /dev/null +++ b/Tests/RunCMake/find_file/MSYSTEM_PREFIX-stdout.txt @@ -0,0 +1 @@ +-- MSYSTEM_PREFIX_H='[^']*/Tests/RunCMake/find_file/MSYSTEM_PREFIX/include/msystem_prefix.h' diff --git a/Tests/RunCMake/find_file/MSYSTEM_PREFIX.cmake b/Tests/RunCMake/find_file/MSYSTEM_PREFIX.cmake new file mode 100644 index 0000000..08e4127 --- /dev/null +++ b/Tests/RunCMake/find_file/MSYSTEM_PREFIX.cmake @@ -0,0 +1,5 @@ +set(ENV{MSYSTEM} "FAKEMSYS") +set(ENV{MSYSTEM_PREFIX} "${CMAKE_CURRENT_LIST_DIR}/MSYSTEM_PREFIX") +enable_language(C) +find_file(MSYSTEM_PREFIX_H NAMES msystem_prefix.h) +message(STATUS "MSYSTEM_PREFIX_H='${MSYSTEM_PREFIX_H}'") diff --git a/Tests/RunCMake/find_file/MSYSTEM_PREFIX/include/msystem_prefix.h b/Tests/RunCMake/find_file/MSYSTEM_PREFIX/include/msystem_prefix.h new file mode 100644 index 0000000..e69de29 diff --git a/Tests/RunCMake/find_file/RunCMakeTest.cmake b/Tests/RunCMake/find_file/RunCMakeTest.cmake index 296bb71..2d59e38 100644 --- a/Tests/RunCMake/find_file/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_file/RunCMakeTest.cmake @@ -12,6 +12,10 @@ run_cmake(VALIDATOR-undefined-function) run_cmake(VALIDATOR-specify-macro) run_cmake(VALIDATOR) +if(CMAKE_HOST_WIN32 AND MINGW) + run_cmake(MSYSTEM_PREFIX) +endif() + run_cmake_with_options(FromPATHEnvDebugVar --debug-find-var=PrefixInPATH_File) if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") diff --git a/Tests/RunCMake/find_library/MSYSTEM_PREFIX-stdout.txt b/Tests/RunCMake/find_library/MSYSTEM_PREFIX-stdout.txt new file mode 100644 index 0000000..b19bcce --- /dev/null +++ b/Tests/RunCMake/find_library/MSYSTEM_PREFIX-stdout.txt @@ -0,0 +1 @@ +-- MSYSTEM_PREFIX_LIB='[^']*/Tests/RunCMake/find_library/MSYSTEM_PREFIX/lib/libmsystem_prefix.a' diff --git a/Tests/RunCMake/find_library/MSYSTEM_PREFIX.cmake b/Tests/RunCMake/find_library/MSYSTEM_PREFIX.cmake new file mode 100644 index 0000000..ef4ce2f --- /dev/null +++ b/Tests/RunCMake/find_library/MSYSTEM_PREFIX.cmake @@ -0,0 +1,5 @@ +set(ENV{MSYSTEM} "FAKEMSYS") +set(ENV{MSYSTEM_PREFIX} "${CMAKE_CURRENT_LIST_DIR}/MSYSTEM_PREFIX") +enable_language(C) +find_library(MSYSTEM_PREFIX_LIB NAMES msystem_prefix) +message(STATUS "MSYSTEM_PREFIX_LIB='${MSYSTEM_PREFIX_LIB}'") diff --git a/Tests/RunCMake/find_library/MSYSTEM_PREFIX/lib/libmsystem_prefix.a b/Tests/RunCMake/find_library/MSYSTEM_PREFIX/lib/libmsystem_prefix.a new file mode 100644 index 0000000..e69de29 diff --git a/Tests/RunCMake/find_library/RunCMakeTest.cmake b/Tests/RunCMake/find_library/RunCMakeTest.cmake index 8b223b4..0bed252 100644 --- a/Tests/RunCMake/find_library/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_library/RunCMakeTest.cmake @@ -20,6 +20,10 @@ run_cmake(VALIDATOR-undefined-function) run_cmake(VALIDATOR-specify-macro) run_cmake(VALIDATOR) +if(CMAKE_HOST_WIN32 AND MINGW) + run_cmake(MSYSTEM_PREFIX) +endif() + run_cmake_script(FromScriptMode "-DTEMP_DIR=${RunCMake_BINARY_DIR}/FromScriptMode-temp") run_cmake_with_options(FromPATHEnvDebugVar --debug-find-var=CREATED_LIBRARY) diff --git a/Tests/RunCMake/find_package/MSYSTEM_PREFIX-stdout.txt b/Tests/RunCMake/find_package/MSYSTEM_PREFIX-stdout.txt new file mode 100644 index 0000000..b1c8346 --- /dev/null +++ b/Tests/RunCMake/find_package/MSYSTEM_PREFIX-stdout.txt @@ -0,0 +1 @@ +-- MsysPfx_DIR='[^']*/Tests/RunCMake/find_package/MSYSTEM_PREFIX' diff --git a/Tests/RunCMake/find_package/MSYSTEM_PREFIX.cmake b/Tests/RunCMake/find_package/MSYSTEM_PREFIX.cmake new file mode 100644 index 0000000..2826e53 --- /dev/null +++ b/Tests/RunCMake/find_package/MSYSTEM_PREFIX.cmake @@ -0,0 +1,5 @@ +set(ENV{MSYSTEM} "FAKEMSYS") +set(ENV{MSYSTEM_PREFIX} "${CMAKE_CURRENT_LIST_DIR}/MSYSTEM_PREFIX") +enable_language(C) +find_package(MsysPfx QUIET) +message(STATUS "MsysPfx_DIR='${MsysPfx_DIR}'") diff --git a/Tests/RunCMake/find_package/MSYSTEM_PREFIX/MsysPfxConfig.cmake b/Tests/RunCMake/find_package/MSYSTEM_PREFIX/MsysPfxConfig.cmake new file mode 100644 index 0000000..e69de29 diff --git a/Tests/RunCMake/find_package/RunCMakeTest.cmake b/Tests/RunCMake/find_package/RunCMakeTest.cmake index 006757a..a93b811 100644 --- a/Tests/RunCMake/find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_package/RunCMakeTest.cmake @@ -72,6 +72,10 @@ run_cmake(REGISTRY_VIEW-no-view) run_cmake(REGISTRY_VIEW-wrong-view) run_cmake(REGISTRY_VIEW-propagated) +if(CMAKE_HOST_WIN32 AND MINGW) + run_cmake(MSYSTEM_PREFIX) +endif() + if(CMAKE_HOST_WIN32) run_cmake(CMP0144-WARN-CaseInsensitive) run_cmake(CMP0144-OLD-CaseInsensitive) diff --git a/Tests/RunCMake/find_path/MSYSTEM_PREFIX-stdout.txt b/Tests/RunCMake/find_path/MSYSTEM_PREFIX-stdout.txt new file mode 100644 index 0000000..87ee490 --- /dev/null +++ b/Tests/RunCMake/find_path/MSYSTEM_PREFIX-stdout.txt @@ -0,0 +1 @@ +-- MSYSTEM_PREFIX_INCLUDE_DIR='[^']*/Tests/RunCMake/find_path/MSYSTEM_PREFIX/include' diff --git a/Tests/RunCMake/find_path/MSYSTEM_PREFIX.cmake b/Tests/RunCMake/find_path/MSYSTEM_PREFIX.cmake new file mode 100644 index 0000000..d17a52f --- /dev/null +++ b/Tests/RunCMake/find_path/MSYSTEM_PREFIX.cmake @@ -0,0 +1,5 @@ +set(ENV{MSYSTEM} "FAKEMSYS") +set(ENV{MSYSTEM_PREFIX} "${CMAKE_CURRENT_LIST_DIR}/MSYSTEM_PREFIX") +enable_language(C) +find_path(MSYSTEM_PREFIX_INCLUDE_DIR NAMES msystem_prefix.h) +message(STATUS "MSYSTEM_PREFIX_INCLUDE_DIR='${MSYSTEM_PREFIX_INCLUDE_DIR}'") diff --git a/Tests/RunCMake/find_path/MSYSTEM_PREFIX/include/msystem_prefix.h b/Tests/RunCMake/find_path/MSYSTEM_PREFIX/include/msystem_prefix.h new file mode 100644 index 0000000..e69de29 diff --git a/Tests/RunCMake/find_path/RunCMakeTest.cmake b/Tests/RunCMake/find_path/RunCMakeTest.cmake index 9c76f2e..2ed4d7d 100644 --- a/Tests/RunCMake/find_path/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_path/RunCMakeTest.cmake @@ -12,6 +12,10 @@ run_cmake(VALIDATOR-undefined-function) run_cmake(VALIDATOR-specify-macro) run_cmake(VALIDATOR) +if(CMAKE_HOST_WIN32 AND MINGW) + run_cmake(MSYSTEM_PREFIX) +endif() + if(APPLE) run_cmake(FrameworksWithSubdirs) endif() -- cgit v0.12