Edoardo Pasca & Alin M Elena
ukri stfc Scientific Computing
February 2025
https://gitlab.com/drFaustroll/cmake-tutorialdot product and will
add OpenMPCMakeLists.txt in the source tree.CMAKE_GENERATORCMAKE_SOURCE_DIRCMAKE_BINARY_DIRCMAKE_INSTALL_PREFIXLeibnitz formula for \(\pi\)
\[\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \frac{1}{11} + \dots\]
cmake_minimum_required(VERSION 3.10)
cmake_policy(SET CMP0048 NEW)
project(testdot LANGUAGES CXX VERSION 0.1.0.1)
set(AUTHOR "Alin Elena;Edoardo Pasca")
message(STATUS "building ${PROJECT_NAME},
version ${PROJECT_VERSION}")
set(CMAKE_CXX_STANDARD 11)
add_executable(testdot
src/algebra.cxx
src/testdot.cxx
)
target_include_directories(testdot PRIVATE
${PROJECT_SOURCE_DIR}/include)add_library(algebra STATIC algebra.cxx)
add_executable(testdot testdot.cxx )
target_include_directories(algebra PUBLIC
"${PROJECT_SOURCE_DIR}/include")
target_link_libraries(testdot algebra)Intermezzo CMake Language
option(BUILD_DOCS "Build with API Docs" OFF)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
if(BUILD_DOCS)
find_package(Doxygen REQUIRED)
configure_file(${PROJECT_SOURCE_DIR}/cmake/Doxyfile.cmake ${PROJECT_BINARY_DIR}/Doxyfile)
add_custom_target(docs
${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/Doxyfile)
endif()CMake Language source files in a project are organized into:
CMakeLists.txt),cmake -P <script>.cmakeinclude() command to load them in the scope of the
including contextCMake source code is saved in text files encoded as 7bit ASCII for max portability.
UTF-8 is OK.
A command invocation is a name followed by paren-enclosed arguments separated by whitespace
Command names are case-insensitive.
Nested unquoted parentheses in the arguments must balance.
It may not contain any whitespace, ( ) # \ " except when
escaped by a backslash
Content is enclosed between opening and closing ” double-quote .
Both Escape Sequences and Variable References are evaluated.
A quoted argument is one argument for the command.
Content is enclosed between opening and closing brackets of the same length
Evaluation of the enclosed content is not performed, such as Escape Sequences or Variable References.
message([=[
This is the first line in a bracket argument with bracket length 1.
No \-escape sequences or ${variable} references are evaluated.
This is always one argument even though it contains a ; character.
The text does not end on a closing bracket of length 0 like ]].
It does end in a closing bracket of length 1.
]=])${<variable>}${outer_${inner_variable}_variable}$ENV{<variable>}A list of elements is represented as a string by concatenating the
elements separated by ;.
The cache is best thought of as a configuration file.
The first time CMake is run, it produces a
CMakeCache.txt file which contains entries added in
response to certain CMake commands as find_package
After CMake has been run, and created CMakeCache.txt –
you may edit it.
variable references can be in the short form
<variable> instead of
${<variable>}. Not valid for environment and cache
variables.
Evaluate a group of commands for each value in a list.
What does it return?
Evaluate a group of commands while a condition is true
Are pieces of code for later execution.
In a function, ARGN, ARGC, ARGV and ARGV0, ARGV1, … are true variables in the usual CMake sense. In a macro, they are not, they are string replacements which makes normal CMake syntax cumbersome.
A macro is executed as if the macro body were pasted in place of the calling statement.
Back to 02.3 Adding Build Options
if pkg-config is installed by conda then one needs to set the path to it
requires BLAS library and includes to be available in default locations. In Linux this means you need to add:
LD_LIBRARY_PATHCPATH or
C_INCLUDE_PATH and CPLUS_INCLUDE_PATHoption(BUILD_TESTING "Build with tests" ON)
if (BUILD_TESTING)
include(CTest)
#testing this macro in big projects may go in its own file
macro (do_test testname n ns result)
add_test(${testname} ${CMAKE_INSTALL_BINDIR}/testdot ${n} ${ns})
set_tests_properties (${testname}
PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)
do_test(test1 10 10 "Last value of pi: 3.19419")
add_test(NAME run_unittest COMMAND unittest)
endif()include(ExternalProject)
ExternalProject_Add(algebra
GIT_REPOSITORY https://gitlab.com/drFaustroll/algebra_cxx.git
GIT_TAG v0.1.0.1
GIT_SHALLOW true
GIT_PROGRESS true
PREFIX external-algebra
BINARY_DIR external-algebra/algebra-build
SOURCE_DIR external-algebra/algebra
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${PROJECT_BINARY_DIR}/external
-DBUILD_TESTING=off -DBUILD_SHARED_LIBS=off
)enable_language(CUDA)
add_executable(cudadottest ${CMAKE_CURRENT_SOURCE_DIR}/testdot.cu)
if (CMAKE_VERSION VERSION_LESS "3.17")
find_package(CUDA REQUIRED)
set (dottest_link_libraries ${CUDA_CUBLAS_LIBRARIES})
else()
find_package(CUDAToolkit REQUIRED)
set (dottest_link_libraries CUDA::cublas)
endif()
add_definitions(-DCUDABLAS)
target_link_libraries(cudadottest ${dottest_link_libraries})time to convert a project to CMake