From 46ac308249263dd2fcdb2abaf113bee29d828213 Mon Sep 17 00:00:00 2001 From: Brad King Date: Sat, 9 Nov 2024 09:08:54 -0500 Subject: [PATCH] cmSystemTools: Factor out helpers from FindCMakeResources Factor out helpers to: - Find our own executable - Find resources in the install tree or the build tree --- Source/cmSystemTools.cxx | 70 ++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index a1ea8e64ea..d404f5a0dd 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -2687,9 +2687,8 @@ bool IsCMakeAppBundleExe(std::string const& exe) return cmHasLiteralSuffix(cmSystemTools::LowerCase(exe), "/macos/cmake"); } #endif -} -void cmSystemTools::FindCMakeResources(const char* argv0) +std::string FindOwnExecutable(const char* argv0) { #if defined(_WIN32) && !defined(__CYGWIN__) static_cast(argv0); @@ -2738,10 +2737,12 @@ void cmSystemTools::FindCMakeResources(const char* argv0) } exe = cmSystemTools::GetRealPath(exe); #endif + return exe; +} + #ifndef CMAKE_BOOTSTRAP - // Find resources relative to our own executable. - std::string exe_dir = cmSystemTools::GetFilenamePath(exe); - bool found = false; +bool FindCMakeResourcesInInstallTree(std::string const& exe_dir) +{ // Install tree has // - "/cmake" // - "" @@ -2759,35 +2760,50 @@ void cmSystemTools::FindCMakeResources(const char* argv0) cmStrCat(prefix, CMAKE_DOC_DIR "/html/index.html"))) { cmSystemToolsHTMLDoc = cmStrCat(prefix, CMAKE_DOC_DIR "/html"); } - found = true; + return true; } } + return false; +} - if (!found) { - // Build tree has "/bin[/]/cmake" and - // "/CMakeFiles/CMakeSourceDir.txt". - std::string dir = cmSystemTools::GetFilenamePath(exe_dir); - std::string src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt"); - cmsys::ifstream fin(src_dir_txt.c_str()); - std::string src_dir; - if (fin && cmSystemTools::GetLineFromStream(fin, src_dir) && +void FindCMakeResourcesInBuildTree(std::string const& exe_dir) +{ + // Build tree has "/bin[/]/cmake" and + // "/CMakeFiles/CMakeSourceDir.txt". + std::string dir = cmSystemTools::GetFilenamePath(exe_dir); + std::string src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt"); + cmsys::ifstream fin(src_dir_txt.c_str()); + std::string src_dir; + if (fin && cmSystemTools::GetLineFromStream(fin, src_dir) && + cmSystemTools::FileIsDirectory(src_dir)) { + cmSystemToolsCMakeRoot = src_dir; + } else { + dir = cmSystemTools::GetFilenamePath(dir); + src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt"); + cmsys::ifstream fin2(src_dir_txt.c_str()); + if (fin2 && cmSystemTools::GetLineFromStream(fin2, src_dir) && cmSystemTools::FileIsDirectory(src_dir)) { cmSystemToolsCMakeRoot = src_dir; - } else { - dir = cmSystemTools::GetFilenamePath(dir); - src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt"); - cmsys::ifstream fin2(src_dir_txt.c_str()); - if (fin2 && cmSystemTools::GetLineFromStream(fin2, src_dir) && - cmSystemTools::FileIsDirectory(src_dir)) { - cmSystemToolsCMakeRoot = src_dir; - } - } - if (!cmSystemToolsCMakeRoot.empty() && cmSystemToolsHTMLDoc.empty() && - cmSystemTools::FileExists( - cmStrCat(dir, "/Utilities/Sphinx/html/index.html"))) { - cmSystemToolsHTMLDoc = cmStrCat(dir, "/Utilities/Sphinx/html"); } } + if (!cmSystemToolsCMakeRoot.empty() && cmSystemToolsHTMLDoc.empty() && + cmSystemTools::FileExists( + cmStrCat(dir, "/Utilities/Sphinx/html/index.html"))) { + cmSystemToolsHTMLDoc = cmStrCat(dir, "/Utilities/Sphinx/html"); + } +} +#endif +} + +void cmSystemTools::FindCMakeResources(const char* argv0) +{ + std::string exe = FindOwnExecutable(argv0); +#ifndef CMAKE_BOOTSTRAP + // Find resources relative to our own executable. + std::string exe_dir = cmSystemTools::GetFilenamePath(exe); + if (!FindCMakeResourcesInInstallTree(exe_dir)) { + FindCMakeResourcesInBuildTree(exe_dir); + } cmSystemToolsCMakeCommand = cmStrCat(exe_dir, "/cmake", cmSystemTools::GetExecutableExtension()); #else