We will start with the main CMakeLists.txt and later move to deps/CMakeLists.txt:
- As before, we declare a C++11 project:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-04 LANGUAGES CXX Fortran)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
- At this point, we move on to deps/CMakeLists.txt. This is achieved with the add_subdirectory command:
add_subdirectory(deps)
- Inside deps/CMakeLists.txt, we first locate the necessary libraries (BLAS and LAPACK):
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
- Then, we collect the contents of the tarball archive into a variable, MATH_SRCS:
set(MATH_SRCS
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.cpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.cpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.hpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.hpp
)
- Having listed the sources that are to be extracted, we define a custom target and a custom command. This combination extracts the archive in ${CMAKE_CURRENT_BINARY_DIR}. However, here we are in a different scope and refer to deps/CMakeLists.txt, and therefore the tarball will be extracted into a deps subdirectory below the main project build directory:
add_custom_target(BLAS_LAPACK_wrappers
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${MATH_SRCS}
COMMENT
"Intermediate BLAS_LAPACK_wrappers target"
VERBATIM
)
add_custom_command(
OUTPUT
${MATH_SRCS}
COMMAND
${CMAKE_COMMAND} -E tar xzf ${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
COMMENT
"Unpacking C++ wrappers for BLAS/LAPACK"
)
- Then, we add our math library as a target and specify corresponding sources, include directories, and link libraries:
add_library(math "")
target_sources(math
PRIVATE
${MATH_SRCS}
)
target_include_directories(math
INTERFACE
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK
)
# BLAS_LIBRARIES are included in LAPACK_LIBRARIES
target_link_libraries(math
PUBLIC
${LAPACK_LIBRARIES}
)
- Once the execution of the commands in deps/CMakeLists.txt is done, we move back to the parent scope, define the executable target, and link it against the math library that we have defined one directory below:
add_executable(linear-algebra linear-algebra.cpp)
target_link_libraries(linear-algebra
PRIVATE
math
)