From aa3ab3eb925a601e3acd2d9f569fc3183fab2f8b Mon Sep 17 00:00:00 2001 From: Robert Maynard Date: Mon, 12 Jul 2021 13:26:36 -0400 Subject: find_library: Infer library prefix and suffix when in script mode This aligns `find_library` with the documentation that states it can be called from script mode. This is done by infering the proper prefix and suffix values when `find_library` is called when the `CMAKE_FIND_LIBRARY*` are not set. This also means that `find_library` won't produce obscure error messages about unset definitions. Fixes: #22027 --- Source/cmFindLibraryCommand.cxx | 35 +++++++++++++++++++--- .../find_library/FromScriptMode-stderr-darwin.txt | 4 +++ .../find_library/FromScriptMode-stderr-windows.txt | 4 +++ .../find_library/FromScriptMode-stderr.txt | 4 +++ Tests/RunCMake/find_library/FromScriptMode.cmake | 15 ++++++++++ Tests/RunCMake/find_library/RunCMakeTest.cmake | 2 ++ 6 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt create mode 100644 Tests/RunCMake/find_library/FromScriptMode-stderr-windows.txt create mode 100644 Tests/RunCMake/find_library/FromScriptMode-stderr.txt create mode 100644 Tests/RunCMake/find_library/FromScriptMode.cmake diff --git a/Source/cmFindLibraryCommand.cxx b/Source/cmFindLibraryCommand.cxx index 0cbe637..ef960d1 100644 --- a/Source/cmFindLibraryCommand.cxx +++ b/Source/cmFindLibraryCommand.cxx @@ -259,6 +259,34 @@ struct cmFindLibraryHelper }; }; +namespace { + +std::string const& get_prefixes(cmMakefile* mf) +{ +#ifdef _WIN32 + static std::string defaultPrefix = ";lib"; +#else + static std::string defaultPrefix = "lib"; +#endif + cmProp prefixProp = mf->GetDefinition("CMAKE_FIND_LIBRARY_PREFIXES"); + return (prefixProp) ? *prefixProp : defaultPrefix; +} + +std::string const& get_suffixes(cmMakefile* mf) +{ +#ifdef _WIN32 + static std::string defaultSuffix = ".lib;.dll.a;.a"; +#elif defined(__APPLE__) + static std::string defaultSuffix = ".tbd;.dylib;.so;.a"; +#elif defined(__hpux) + static std::string defaultSuffix = ".sl;.so;.a"; +#else + static std::string defaultSuffix = ".so;.a"; +#endif + cmProp suffixProp = mf->GetDefinition("CMAKE_FIND_LIBRARY_SUFFIXES"); + return (suffixProp) ? *suffixProp : defaultSuffix; +} +} cmFindLibraryHelper::cmFindLibraryHelper(std::string debugName, cmMakefile* mf, cmFindBase const* base) : Makefile(mf) @@ -268,10 +296,9 @@ cmFindLibraryHelper::cmFindLibraryHelper(std::string debugName, cmMakefile* mf, this->GG = this->Makefile->GetGlobalGenerator(); // Collect the list of library name prefixes/suffixes to try. - std::string const& prefixes_list = - this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES"); - std::string const& suffixes_list = - this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES"); + std::string const& prefixes_list = get_prefixes(this->Makefile); + std::string const& suffixes_list = get_suffixes(this->Makefile); + cmExpandList(prefixes_list, this->Prefixes, true); cmExpandList(suffixes_list, this->Suffixes, true); this->RegexFromList(this->PrefixRegexStr, this->Prefixes); diff --git a/Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt b/Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt new file mode 100644 index 0000000..185720b --- /dev/null +++ b/Tests/RunCMake/find_library/FromScriptMode-stderr-darwin.txt @@ -0,0 +1,4 @@ +.*find_library considered the following locations.* +.*\(lib\)library_no_exist\(\\.tbd\|\\.dylib\|\\.so\|\\.a\).* +.*The item was found at.* +.*lib/libcreated.a.* diff --git a/Tests/RunCMake/find_library/FromScriptMode-stderr-windows.txt b/Tests/RunCMake/find_library/FromScriptMode-stderr-windows.txt new file mode 100644 index 0000000..501ec0f --- /dev/null +++ b/Tests/RunCMake/find_library/FromScriptMode-stderr-windows.txt @@ -0,0 +1,4 @@ +.*find_library considered the following locations.* +.*\(\|lib\)library_no_exist\(\\.lib\|\\.dll\\.a\|\\.a\).* +.*The item was found at.* +.*lib/libcreated.a.* diff --git a/Tests/RunCMake/find_library/FromScriptMode-stderr.txt b/Tests/RunCMake/find_library/FromScriptMode-stderr.txt new file mode 100644 index 0000000..046f680 --- /dev/null +++ b/Tests/RunCMake/find_library/FromScriptMode-stderr.txt @@ -0,0 +1,4 @@ +.*find_library considered the following locations.* +.*\(lib\)library_no_exist\(\\.so\|\\.a\).* +.*The item was found at.* +.*lib/libcreated.a.* diff --git a/Tests/RunCMake/find_library/FromScriptMode.cmake b/Tests/RunCMake/find_library/FromScriptMode.cmake new file mode 100644 index 0000000..4d3c699 --- /dev/null +++ b/Tests/RunCMake/find_library/FromScriptMode.cmake @@ -0,0 +1,15 @@ + +if(TEMP_DIR) + file(REMOVE_RECURSE "${TEMP_DIR}") + file(MAKE_DIRECTORY "${TEMP_DIR}") + file(MAKE_DIRECTORY "${TEMP_DIR}/lib") + file(WRITE "${TEMP_DIR}/lib/libcreated.a" "created") +endif() + +set(CMAKE_FIND_DEBUG_MODE 1) +find_library(CREATED_LIBRARY NAMES library_no_exist) + +set(CMAKE_PREFIX_PATH "${TEMP_DIR}") +find_library(CREATED_LIBRARY NAMES created) +message(STATUS "CREATED_LIBRARY='${CREATED_LIBRARY}'") +set(CMAKE_FIND_DEBUG_MODE 0) diff --git a/Tests/RunCMake/find_library/RunCMakeTest.cmake b/Tests/RunCMake/find_library/RunCMakeTest.cmake index e297173..ad02c82 100644 --- a/Tests/RunCMake/find_library/RunCMakeTest.cmake +++ b/Tests/RunCMake/find_library/RunCMakeTest.cmake @@ -10,3 +10,5 @@ endif() run_cmake(PrefixInPATH) run_cmake(Required) run_cmake(NO_CACHE) + +run_cmake_script(FromScriptMode "-DTEMP_DIR=${RunCMake_BINARY_DIR}/FromScriptMode-temp") -- cgit v0.12