3
0
mirror of https://github.com/triqs/dft_tools synced 2024-11-18 12:03:50 +01:00

[cmake] Change Build_Deps option to take either Never/Always/IfNotFound, cleaning

- Use Global Properties to check if a package has been found.
  This information should not go in the cmake cache
This commit is contained in:
Nils Wentzell 2020-04-28 16:23:27 -04:00
parent 8c407831c8
commit cce7b5cd2f
2 changed files with 48 additions and 31 deletions

12
deps/CMakeLists.txt vendored
View File

@ -29,11 +29,19 @@ include(external_dependency.cmake)
# will not be included in the ALL target of the project.
# In particular the dependency will not be installed.
option(Build_Deps "Build dependencies from source" OFF)
if(NOT DEFINED Build_Deps)
set(Build_Deps "Never" CACHE STRING "Do we build dependencies from source? [Never/Always/IfNotFound]")
else()
set(Build_Deps_Opts "Never" "Always" "IfNotFound")
if(NOT ${Build_Deps} IN_LIST Build_Deps_Opts)
message(FATAL_ERROR "Build_Deps option should be either 'Never', 'Always' or 'IfNotFound'")
endif()
set(Build_Deps ${Build_Deps} CACHE STRING "Do we build dependencies from source? [Never/Always/IfNotFound]")
endif()
# -- GTest --
external_dependency(GTest
GIT_REPO github:google/googletest
GIT_REPO https://github.com/google/googletest
TAG release-1.10.0
EXCLUDE_FROM_ALL
)

View File

@ -2,37 +2,46 @@
function(external_dependency)
cmake_parse_arguments(ARG "EXCLUDE_FROM_ALL" "VERSION;GIT_REPO;TAG" "" ${ARGN})
# Was dependency already found?
# -- Was dependency already found?
get_property(${ARGV0}_FOUND GLOBAL PROPERTY ${ARGV0}_FOUND)
if(${ARGV0}_FOUND)
message(STATUS "Dependency ${ARGV0} was already resolved")
else()
if(NOT Build_Deps)
find_package(${ARGV0} ${${ARGV0}_VERSION} QUIET)
endif()
message(STATUS "Dependency ${ARGV0} was already resolved.")
return()
endif()
# -- Try to find package in system.
if(NOT Build_Deps STREQUAL "Always")
find_package(${ARGV0} ${${ARGV0}_VERSION} QUIET)
if(${ARGV0}_FOUND)
message(STATUS "Found dependency ${ARGV0} with find_package command")
else()
message(STATUS " =============== Configuring Dependency ${ARGV0} =============== ")
if(ARG_EXCLUDE_FROM_ALL)
set(subdir_opts EXCLUDE_FROM_ALL)
endif()
if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0})
message(STATUS "Found sources for dependency ${ARGV0} at ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0}.")
add_subdirectory(${ARGV0} ${subdir_opts})
elseif(ARG_GIT_REPO)
set(bin_dir ${CMAKE_CURRENT_BINARY_DIR}/${ARGV0})
set(src_dir ${bin_dir}_src)
if(NOT IS_DIRECTORY ${src_dir})
if(ARG_TAG)
set(clone_opts --branch ${ARG_TAG} -c advice.detachedHead=false)
endif()
execute_process(COMMAND git clone ${ARG_GIT_REPO} --depth 1 ${clone_opts} ${src_dir})
endif()
add_subdirectory(${src_dir} ${bin_dir} ${subdir_opts})
else()
message(FATAL_ERROR "Could not find dependency ${ARGV0}")
endif()
set(${ARGV0}_FOUND TRUE PARENT_SCOPE)
message(STATUS "Found dependency ${ARGV0} in system.")
return()
elseif(Build_Deps STREQUAL "Never")
message(FATAL_ERROR "Could not find dependency ${ARGV0} in system.")
endif()
endif()
# -- Build package from source
message(STATUS " =============== Configuring Dependency ${ARGV0} =============== ")
if(ARG_EXCLUDE_FROM_ALL)
set(subdir_opts EXCLUDE_FROM_ALL)
endif()
if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0})
message(STATUS "Found sources for dependency ${ARGV0} at ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0}.")
add_subdirectory(${ARGV0} ${subdir_opts})
elseif(ARG_GIT_REPO)
set(bin_dir ${CMAKE_CURRENT_BINARY_DIR}/${ARGV0})
set(src_dir ${bin_dir}_src)
if(NOT IS_DIRECTORY ${src_dir})
if(ARG_TAG)
set(clone_opts --branch ${ARG_TAG} -c advice.detachedHead=false)
endif()
execute_process(COMMAND git clone ${ARG_GIT_REPO} --depth 1 ${clone_opts} ${src_dir})
endif()
add_subdirectory(${src_dir} ${bin_dir} ${subdir_opts})
else()
message(FATAL_ERROR "Could not find or build dependency ${ARGV0}.")
endif()
set_property(GLOBAL PROPERTY ${ARGV0}_FOUND TRUE)
endfunction()