add additional function argument where we can supply our own CMakeLists.txt file

This commit is contained in:
Axel Kohlmeyer
2021-11-03 11:50:39 -04:00
parent 6479116419
commit b95e12bb6c
5 changed files with 51 additions and 7 deletions

View File

@ -2,7 +2,7 @@
# The sources will be unpacked to ${CMAKE_BINARY_DIR}/_deps/${target}-src
# The binaries will be built in ${CMAKE_BINARY_DIR}/_deps/${target}-build
#
function(ExternalCMakeProject target url hash basedir cmakedir)
function(ExternalCMakeProject target url hash basedir cmakedir cmakefile)
# change settings locally
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@ -23,6 +23,9 @@ function(ExternalCMakeProject target url hash basedir cmakedir)
endif()
file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/_deps/${target}-src)
file(RENAME ${TARGET_SOURCE} ${CMAKE_BINARY_DIR}/_deps/${target}-src)
if(NOT (cmakefile STREQUAL ""))
file(COPY ${cmakefile} ${CMAKE_BINARY_DIR}/_deps/${target}-src/${cmakedir}/CMakeLists.txt)
endif()
add_subdirectory("${CMAKE_BINARY_DIR}/_deps/${target}-src/${cmakedir}"
"${CMAKE_BINARY_DIR}/_deps/${target}-build")
else()
@ -30,6 +33,14 @@ function(ExternalCMakeProject target url hash basedir cmakedir)
message(STATUS "Downloading ${url}")
FetchContent_Declare(${target} URL ${url} URL_HASH MD5=${hash} SOURCE_SUBDIR ${cmakedir})
message(STATUS "Unpacking and configuring ${archive}")
FetchContent_MakeAvailable(${target})
FetchContent_GetProperties(${target})
FetchContent_Populate(${target})
if(NOT (cmakefile STREQUAL ""))
file(COPY "${cmakefile}" DESTINATION "${${target}_SOURCE_DIR}/${cmakedir}/")
get_filename_component(_cmakefile "${cmakefile}" NAME)
file(RENAME "${${target}_SOURCE_DIR}/${cmakedir}/${_cmakefile}"
"${${target}_SOURCE_DIR}/${cmakedir}/CMakeLists.txt")
endif()
add_subdirectory("${${target}_SOURCE_DIR}/${cmakedir}" "${${target}_BINARY_DIR}")
endif()
endfunction(ExternalCMakeProject)

View File

@ -13,7 +13,7 @@ if(DOWNLOAD_MSCG)
mark_as_advanced(MSCG_MD5)
include(ExternalCMakeProject)
ExternalCMakeProject(mscg ${MSCG_URL} ${MSCG_MD5} MSCG-release src/CMake)
ExternalCMakeProject(mscg ${MSCG_URL} ${MSCG_MD5} MSCG-release src/CMake "")
# set include and link library
target_include_directories(lammps PRIVATE "${CMAKE_BINARY_DIR}/_deps/mscg-src/src")

View File

@ -12,7 +12,7 @@ mark_as_advanced(GTEST_MD5)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
include(ExternalCMakeProject)
ExternalCMakeProject(googletest ${GTEST_URL} ${GTEST_MD5} googletest .)
ExternalCMakeProject(googletest ${GTEST_URL} ${GTEST_MD5} googletest . "")
add_library(GTest::GTest ALIAS gtest)
add_library(GTest::GMock ALIAS gmock)
add_library(GTest::GTestMain ALIAS gtest_main)

View File

@ -0,0 +1,33 @@
# Custom CMake file for libyaml
cmake_minimum_required(VERSION 3.10)
project(libyaml VERSION 0.2.5
DESCRIPTION "LibYAML a YAML parser and emitter library"
LANGUAGES C
HOMEPAGE_URL https://pyyaml.org/wiki/LibYAML)
# compilation settings and options
option(BUILD_SHARED_LIBS "Build libYAML as a shared library" OFF)
option(CMAKE_POSITION_INDEPENDENT_CODE "Create objects compatible with shared libraries" ON)
include(GNUInstallDirs)
add_library(yaml
src/api.c
src/dumper.c
src/emitter.c
src/loader.c
src/parser.c
src/reader.c
src/scanner.c
src/writer.c
)
set (YAML_VERSION_MAJOR ${libyaml_VERSION_MAJOR})
set (YAML_VERSION_MINOR ${libyaml_VERSION_MINOR})
set (YAML_VERSION_PATCH ${libyaml_VERSION_PATCH})
set (YAML_VERSION_STRING "${YAML_VERSION_MAJOR}.${YAML_VERSION_MINOR}.${YAML_VERSION_PATCH}")
configure_file(cmake/config.h.in config.h)
target_compile_definitions(yaml PRIVATE YAML_DECLARE_STATIC HAVE_CONFIG_H)
target_include_directories(yaml PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR})

View File

@ -1,14 +1,14 @@
find_package(YAML)
if(NOT YAML_FOUND)
set(YAML_URL "https://github.com/akohlmey/libyaml/archive/refs/heads/new-cmake.tar.gz" CACHE STRING "URL for libyaml tarball")
set(YAML_MD5 "1683f5957a79d7a823e09575b0488df2" CACHE STRING "MD5 checksum of libyaml tarball")
set(YAML_URL "https://pyyaml.org/download/libyaml/yaml-0.2.5.tar.gz" CACHE STRING "URL for libyaml tarball")
set(YAML_MD5 "bb15429d8fb787e7d3f1c83ae129a999" CACHE STRING "MD5 checksum of libyaml tarball")
mark_as_advanced(YAML_URL)
mark_as_advanced(YAML_MD5)
# download and build a local copy of libyaml
include(ExternalCMakeProject)
ExternalCMakeProject(libyaml ${YAML_URL} ${YAML_MD5} libyaml .)
ExternalCMakeProject(libyaml ${YAML_URL} ${YAML_MD5} libyaml . CMakeLists.libyaml)
add_library(Yaml::Yaml ALIAS yaml)
endif()