2020-08-07 16:53:16 +02:00
# Copyright (c) 2020 Simons Foundation
2020-04-29 21:57:00 +02:00
#
2020-08-07 16:53:16 +02:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2020-04-29 21:57:00 +02:00
#
2020-08-07 16:53:16 +02:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2020-04-29 21:57:00 +02:00
#
2020-08-07 16:53:16 +02:00
# You may obtain a copy of the License at
# https://www.gnu.org/licenses/gpl-3.0.txt
# Consider ROOT env variables in find_package
if ( POLICY CMP0074 )
cmake_policy ( SET CMP0074 NEW )
endif ( )
# Make sure that imported targets are always global
get_property ( IMPORTED_ALWAYS_GLOBAL GLOBAL PROPERTY IMPORTED_ALWAYS_GLOBAL )
if ( NOT IMPORTED_ALWAYS_GLOBAL )
function ( add_library )
set ( _args ${ ARGN } )
if ( "${_args}" MATCHES ";IMPORTED" )
list ( APPEND _args GLOBAL )
endif ( )
_add_library ( ${ _args } )
endfunction ( )
set_property ( GLOBAL PROPERTY IMPORTED_ALWAYS_GLOBAL TRUE )
endif ( )
2020-04-24 23:52:27 +02:00
2020-08-07 16:53:16 +02:00
# Define External Dependency Function
2020-04-24 23:52:27 +02:00
function ( external_dependency )
2020-04-28 23:18:25 +02:00
cmake_parse_arguments ( ARG "EXCLUDE_FROM_ALL;BUILD_ALWAYS" "VERSION;GIT_REPO;GIT_TAG" "" ${ ARGN } )
2020-04-24 23:52:27 +02:00
2020-04-28 22:23:27 +02:00
# -- Was dependency already found?
get_property ( ${ ARGV0 } _FOUND GLOBAL PROPERTY ${ ARGV0 } _FOUND )
2020-04-24 23:52:27 +02:00
if ( ${ ARGV0 } _FOUND )
2020-04-28 22:23:27 +02:00
message ( STATUS "Dependency ${ARGV0} was already resolved." )
return ( )
endif ( )
# -- Try to find package in system.
2020-04-28 23:18:25 +02:00
if ( NOT ARG_BUILD_ALWAYS AND NOT Build_Deps STREQUAL "Always" )
2020-05-26 16:54:01 +02:00
find_package ( ${ ARGV0 } ${ ARG_VERSION } QUIET HINTS ${ CMAKE_INSTALL_PREFIX } )
2020-04-28 16:51:06 +02:00
if ( ${ ARGV0 } _FOUND )
2020-05-28 16:56:57 +02:00
message ( STATUS "Found dependency ${ARGV0} in system ${${ARGV0}_ROOT}" )
2020-04-28 22:23:27 +02:00
return ( )
elseif ( Build_Deps STREQUAL "Never" )
2020-04-28 23:19:13 +02:00
message ( FATAL_ERROR "Could not find dependency ${ARGV0} in system. Please install the dependency manually or use -DBuild_Deps=IfNotFound during cmake configuration to automatically build all dependencies that are not found." )
2020-04-28 22:23:27 +02:00
endif ( )
endif ( )
# -- Build package from source
message ( STATUS " =============== Configuring Dependency ${ARGV0} =============== " )
if ( ARG_EXCLUDE_FROM_ALL )
set ( subdir_opts EXCLUDE_FROM_ALL )
2020-04-29 23:56:09 +02:00
set ( Build_Tests OFF )
2020-05-07 16:41:30 +02:00
set ( Build_Documentation OFF )
2020-04-28 22:23:27 +02:00
endif ( )
if ( IS_DIRECTORY ${ CMAKE_CURRENT_SOURCE_DIR } / ${ ARGV0 } )
2020-04-29 23:57:16 +02:00
message ( STATUS "Found sources for dependency ${ARGV0} at ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0}" )
2020-04-28 22:23:27 +02:00
add_subdirectory ( ${ ARGV0 } ${ subdir_opts } )
2020-06-24 14:57:44 +02:00
elseif ( IS_DIRECTORY ${ CMAKE_SOURCE_DIR } /deps/ ${ ARGV0 } )
message ( STATUS "Found sources for dependency ${ARGV0} at ${CMAKE_SOURCE_DIR}/deps/${ARGV0}" )
add_subdirectory ( ${ ARGV0 } ${ subdir_opts } )
2020-04-28 22:23:27 +02:00
elseif ( ARG_GIT_REPO )
set ( bin_dir ${ CMAKE_CURRENT_BINARY_DIR } / ${ ARGV0 } )
set ( src_dir ${ bin_dir } _src )
if ( NOT IS_DIRECTORY ${ src_dir } )
2020-04-28 22:25:28 +02:00
if ( ARG_GIT_TAG )
set ( clone_opts --branch ${ ARG_GIT_TAG } -c advice.detachedHead=false )
2020-04-28 16:51:06 +02:00
endif ( )
2020-04-28 22:23:27 +02:00
execute_process ( COMMAND git clone ${ ARG_GIT_REPO } --depth 1 ${ clone_opts } ${ src_dir } )
2020-04-24 23:52:27 +02:00
endif ( )
2020-04-28 22:23:27 +02:00
add_subdirectory ( ${ src_dir } ${ bin_dir } ${ subdir_opts } )
else ( )
2020-04-29 23:57:16 +02:00
message ( FATAL_ERROR "Could not find or build dependency ${ARGV0}" )
2020-04-24 23:52:27 +02:00
endif ( )
2020-05-01 20:33:42 +02:00
message ( STATUS " =============== End ${ARGV0} Configuration =============== " )
2020-04-28 22:23:27 +02:00
set_property ( GLOBAL PROPERTY ${ ARGV0 } _FOUND TRUE )
2020-04-24 23:52:27 +02:00
endfunction ( )