Skip to content

Instantly share code, notes, and snippets.

@battleguard
Last active December 12, 2019 14:57
Show Gist options
  • Save battleguard/b3ca1f7837c0303b6bcad69a91440c88 to your computer and use it in GitHub Desktop.
Save battleguard/b3ca1f7837c0303b6bcad69a91440c88 to your computer and use it in GitHub Desktop.
utilizing cmake to print out cmake cache variables
cmake_minimum_required(VERSION 3.12)
function(print_list lines header)
message(STATUS "** ${header} ***")
foreach(line ${lines})
message(STATUS "\t" ${line})
endforeach()
message(STATUS "** End ${header} ***")
endfunction(print_list)
file(READ CMakeCache.txt cache)
string(REPLACE ";" "," cache "${cache}")
string(REPLACE "\n" ";" allCacheLines "${cache}")
list(FIND allCacheLines "# INTERNAL cache entries" idx)
list(SUBLIST allCacheLines 0 ${idx} externalCacheEntries)
list(FILTER externalCacheEntries EXCLUDE REGEX "^[/#]")
list(FILTER externalCacheEntries EXCLUDE REGEX "^CMAKE")
#print_list("${externalCacheEntries}" "External Cache Variables")
set(BOOST_CACHE_VARIABLES "${externalCacheEntries}")
list(FILTER BOOST_CACHE_VARIABLES INCLUDE REGEX "^Boost")
print_list("${BOOST_CACHE_VARIABLES}" "BOOST Setup with Following Variables")
set(PROJECT_CACHE_VARIABLES "${externalCacheEntries}")
list(FILTER PROJECT_CACHE_VARIABLES INCLUDE REGEX "^Project2")
print_list("${PROJECT_CACHE_VARIABLES}" "Project Setup with Following Variables")
cmake_minimum_required(VERSION 3.12)
function(get_cache_variables cmakeCacheFile outCacheVariables)
if(NOT EXISTS "${cmakeCacheFile}")
message(FATAL_ERROR "Cannot find CMakeCache.txt file at: \"${cmakeCacheFile}\"")
endif()
file(READ "${cmakeCacheFile}" cache)
string(REPLACE ";" "," cache "${cache}")
string(REPLACE "\n" ";" allCacheLines "${cache}")
list(FIND allCacheLines "# INTERNAL cache entries" idx)
list(SUBLIST allCacheLines 0 ${idx} externalCacheEntries)
#list(FILTER externalCacheEntries EXCLUDE REGEX "^[/#]")
set(${outCacheVariables} ${externalCacheEntries} PARENT_SCOPE)
endfunction()
get_cache_variables("CMakeCache.txt" EXTERNAL_CACHE_VARIABLES)
function(print_list lines header)
message(STATUS "** ${header} ***")
foreach(line ${lines})
message(STATUS "\t" ${line})
endforeach()
message(STATUS "** End ${header} ***")
endfunction(print_list)
function(print_cache_variable cacheVariableName)
set(comment_lines)
foreach(cachLine ${EXTERNAL_CACHE_VARIABLES})
if(${cachLine} MATCHES "^${cacheVariableName}.*")
foreach(line ${comment_lines} ${cachLine})
message(STATUS ${line})
endforeach()
set(comment_lines "")
elseif(${cachLine} MATCHES "^//.*")
list(APPEND comment_lines ${cachLine})
else()
set(comment_lines "")
endif()
endforeach()
endfunction(print_cache_variable)
print_cache_variable("Boost_")
example output
-- //The directory containing a CMake configuration file for Boost.
-- //Test Multi Line Comment on Boost_DIR
-- Boost_DIR:PATH=Boost_DIR-NOTFOUND
-- //Path to a file.
-- Boost_INCLUDE_DIR:PATH=C:/Boost/include/boost-1_71
-- //Boost library directory DEBUG
-- Boost_LIBRARY_DIR_DEBUG:PATH=C:/Boost/lib
-- //Boost library directory RELEASE
-- Boost_LIBRARY_DIR_RELEASE:PATH=C:/Boost/lib
-- //Boost regex library (debug)
-- Boost_REGEX_LIBRARY_DEBUG:FILEPATH=C:/Boost/lib/boost_regex-vc141-mt-gd-x32-1_71.lib
-- //Boost regex library (release)
-- Boost_REGEX_LIBRARY_RELEASE:FILEPATH=C:/Boost/lib/boost_regex-vc141-mt-x32-1_71.lib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment