3
0
mirror of https://github.com/triqs/dft_tools synced 2024-12-22 20:34:38 +01:00

[cmake] Handle dependencies with external_dependency(...) function + doc

This commit is contained in:
Nils Wentzell 2020-04-24 17:52:27 -04:00
parent 1ff66fd1f3
commit c99ee1820e
4 changed files with 79 additions and 20 deletions

View File

@ -55,9 +55,6 @@ if(NOT IS_SUBPROJECT)
endif()
set(APP4TRIQS_BINARY_DIR ${PROJECT_BINARY_DIR} CACHE STRING "Binary directory of the APP4TRIQS Project")
# ############
# Dependencies
add_subdirectory(deps)
# ############
# Options
@ -102,6 +99,9 @@ install(TARGETS project_warnings EXPORT app4triqs-targets)
# #############
# Build Project
# Find / Build dependencies
add_subdirectory(deps)
# Build and install the app4triqs library
add_subdirectory(c++/app4triqs)

53
deps/CMakeLists.txt vendored
View File

@ -1,18 +1,39 @@
# Each dependency should be either
# * a cmake subproject in this directory
# * found in the system using find_package
# Provide either a download.sh script to get all sources
# or use e.g. git subtree/submodule or softlinks
include(external_dependency.cmake)
# Add all subdirectories as CMake Sub-Projects
file(GLOB allfiles CONFIGURE_DEPENDS *)
foreach(file ${allfiles})
if(IS_DIRECTORY ${file})
add_subdirectory(${file})
endif()
endforeach()
# Add your dependencies with the function
#
# external_dependency(name
# [VERSION <version-number>]
# [GIT_REPO <url>]
# [TAG <tag>]
# [EXCLUDE_FROM_ALL]
# )
#
# Resolve the dependency using the following steps in order.
# If a step was successful, skip the remaining ones.
#
# 1. Use find_package(name [<version-number>])
# to locate the package in the system.
# Skip this step if Build_Deps option is set.
# 2. Try to find a directory containing the sources
# at ${CMAKE_SOURCE_DIR}/deps/name. If found
# build it as a cmake sub-project.
# 3. If GIT_REPO is provided, git clone the sources,
# and build them as a cmake sub-project.
#
# Addtional options:
#
# TAG - Use this keyword to specify the git tag or branch
#
# EXCLUDE_FROM_ALL - If set, targets of the dependency cmake subproject
# will not be included in the ALL target of the project.
# In particular the dependency will not be installed.
# -- googletest --
if(NOT TARGET gtest)
find_package(GTest REQUIRED)
endif()
option(Build_Deps "Build dependencies from source" OFF)
# -- GTest --
external_dependency(GTest
GIT_REPO github:google/googletest
TAG release-1.10.0
EXCLUDE_FROM_ALL
)

1
deps/download.sh vendored
View File

@ -1 +0,0 @@
[ ! -d 'googletest' ] && git clone https://github.com/google/googletest --branch v1.10.x --depth 1

39
deps/external_dependency.cmake vendored Normal file
View File

@ -0,0 +1,39 @@
function(external_dependency)
cmake_parse_arguments(ARG "EXCLUDE_FROM_ALL" "VERSION;GIT_REPO;TAG" "" ${ARGN})
# Was dependency already found?
if(${ARGV0}_FOUND)
message(STATUS "Dependency ${ARGV0} was already resolved")
endif()
if(NOT Build_Deps)
find_package(${ARGV0} ${${ARGV0}_VERSION} QUIET)
endif()
if(${ARGV0}_FOUND)
message(STATUS "Found dependency ${ARGV0} with find_package command")
else()
message(STATUS "Building Dependency ${ARGV0} from source.")
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})
set(${ARGV0}_FOUND TRUE CACHE BOOL "Was dependency ${ARGV0} found?")
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})
set(${ARGV0}_FOUND TRUE CACHE BOOL "Was dependency ${ARGV0} found?")
else()
message(FATAL_ERROR "Could not find dependency ${ARGV0}")
endif()
endif()
endfunction()