From: Matthias Maier
@@ -100,7 +100,7 @@ DEAL_II_SETUP_TARGET(mycode)
After finding the deal.II project, we fetch a set of cached variables
with the
-
Finally, the last two lines define the executable that is to be produced
and its source code. The
-
-CMAKE_MINIMUM_REQUIRED(VERSION 3.3.0)
+cmake_minimum_required(VERSION 3.13.4)
-FIND_PACKAGE(deal.II 9.4.0 REQUIRED
+find_package(deal.II 9.5.0 REQUIRED
HINTS ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR}
)
-DEAL_II_INITIALIZE_CACHED_VARIABLES()
+deal_ii_initialize_cached_variables()
-PROJECT(myproject)
+project(myproject)
-ADD_EXECUTABLE(mycode mycode.cc)
-DEAL_II_SETUP_TARGET(mycode)
+add_executable(mycode mycode.cc)
+deal_ii_setup_target(mycode)
DEAL_II_INITIALIZE_CACHED_VARIABLES
macro. You
+ deal_ii_initialize_cached_variables
macro. You
can inspect these for instance with ccmake
.
DEAL_II_SETUP_TARGET
macro will set up necessary include
+ deal_ii_setup_target
macro will set up necessary include
directories, compile flags, compile definitions, link flags and the link
interface.
-ADD_EXECUTABLE(mycode2 mycode2.cc) -DEAL_II_SETUP_TARGET(mycode2) +add_executable(mycode2 mycode2.cc) +deal_ii_setup_target(mycode2) -ADD_EXECUTABLE(mycode3 mycode3.cc) -DEAL_II_SETUP_TARGET(mycode3) +add_executable(mycode3 mycode3.cc) +deal_ii_setup_target(mycode3)If the list gets longer, consider using @@ -148,12 +148,12 @@ with GLOB.
-ADD_LIBRARY(mylib libsrc1.cc libsrc2.cc libsrc3.cc) -DEAL_II_SETUP_TARGET(mylib) +add_library(mylib libsrc1.cc libsrc2.cc libsrc3.cc) +deal_ii_setup_target(mylib) -ADD_EXECUTABLE(mycode mycode.cc) -DEAL_II_SETUP_TARGET(mycode) -TARGET_LINK_LIBRARIES(mycode mylib) +add_executable(mycode mycode.cc) +deal_ii_setup_target(mycode) +target_link_libraries(mycode mylib)
When you have multiple targets, @@ -165,11 +165,11 @@ attractive.
code, an alternative to creating a library might be the option:-ADD_EXECUTABLE(mycode mycode.cc common.cc) -DEAL_II_SETUP_TARGET(mycode) +add_executable(mycode mycode.cc common.cc) +deal_ii_setup_target(mycode) -ADD_EXECUTABLE(mycode2 mycode2.cc common.cc) -DEAL_II_SETUP_TARGET(mycode2) +add_executable(mycode2 mycode2.cc common.cc) +deal_ii_setup_target(mycode2)
You should be aware though that in this case common.cc
will
@@ -178,14 +178,14 @@ still don't want to use a shared library or static archive, another option
is to create an OBJECT
"library":
-ADD_LIBRARY(common OBJECT common.cc) -DEAL_II_SETUP_TARGET(common) +add_library(common OBJECT common.cc) +deal_ii_setup_target(common) -ADD_EXECUTABLE(mycode mycode.cc $<TARGET_OBJECTS:common>) -DEAL_II_SETUP_TARGET(mycode) +add_executable(mycode mycode.cc $<TARGET_OBJECTS:common>) +deal_ii_setup_target(mycode) -ADD_EXECUTABLE(mycode2 mycode2.cc $<TARGET_OBJECTS:common>) -DEAL_II_SETUP_TARGET(mycode2) +add_executable(mycode2 mycode2.cc $<TARGET_OBJECTS:common>) +deal_ii_setup_target(mycode2)This will compile
common.cc
once for the object target
common
and link the resulting object file into the two
@@ -213,13 +213,13 @@ Alternatively, you can specify custom targets to switch the build type and
compile automatically:
-ADD_CUSTOM_TARGET(debug +add_custom_target(debug COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR} COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all COMMENT "Switch CMAKE_BUILD_TYPE to Debug" ) -ADD_CUSTOM_TARGET(release +add_custom_target(release COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR} COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all COMMENT "Switch CMAKE_BUILD_TYPE to Release" @@ -241,7 +241,7 @@ tutorial, specify one this way (obviously, a single "run" target can only run a single executable):-ADD_CUSTOM_TARGET(run COMMAND mycode +add_custom_target(run COMMAND mycode COMMENT "Run with ${CMAKE_BUILD_TYPE} configuration" )@@ -271,29 +271,29 @@ mycode/include/*.h In this case the top levelCMakeLists.txt
file may be:# top level CMakeLists.txt -CMAKE_MINIMUM_REQUIRED(VERSION 3.3.0) -FIND_PACKAGE(deal.II 9.4.0 REQUIRED) +cmake_minimum_required(VERSION 3.13.4) +find_package(deal.II 9.5.0 REQUIRED) -DEAL_II_INITIALIZE_CACHED_VARIABLES() -PROJECT(myproject) +deal_ii_initialize_cached_variables() +project(myproject) -ADD_SUBDIRECTORY(mylib) -ADD_SUBDIRECTORY(mycode) +add_subdirectory(mylib) +add_subdirectory(mycode)-TheADD_SUBDIRECTORY
statement will include the CMakeLists.txt +Theadd_subdirectory
statement will include the CMakeLists.txt file in the specified subdirectory. In our case:# mylib/CMakeLists.txt -INCLUDE_DIRECTORIES(include) +include_directories(include) -ADD_LIBRARY(mylib +add_library(mylib source/mylib1.cc source/mylib2.cc ) -DEAL_II_SETUP_TARGET(mylib) +deal_ii_setup_target(mylib)We have to add the directoryinclude
for the header files to @@ -305,22 +305,22 @@ like:# mycode/CMakeLists.txt -INCLUDE_DIRECTORIES( +include_directories( include ${CMAKE_SOURCE_DIR}/mylib/include ) -ADD_EXECUTABLE(mycode source/mycode.cc) -DEAL_II_SETUP_TARGET(mycode) +add_executable(mycode source/mycode.cc) +deal_ii_setup_target(mycode) -TARGET_LINK_LIBRARIES(mycode mylib) +target_link_libraries(mycode mylib)Please note that
CMakeLists.txt
files have directory scope. Any manipulation of properties and variables have only effect in the current directory (and subdirectories, included with -ADD_SUBDIRECTORY
. The level above will not be affected. +add_subdirectory
. The level above will not be affected. Therefore, we have to specify the include directories for "mylib" again in the subdirectorymycode
- this time with full path${CMAKE_SOURCE_DIR}/mylib/include
. @@ -352,36 +352,36 @@ CMAKE_CURRENT_BINARY_DIRControl statements in CMake take the following form:
-IF(<expression>) +if(<expression>) ... -ENDIF() +endif()or in long form:-IF(<expression1>) +if(<expression1>) ... -ELSEIF(<expression2>) +elseif(<expression2>) ... -ELSE() +else() ... -ENDIF() +endif()Please note the (somehow uncommon) empty, opening and closing brackets -behindELSE()
andENDIF()
. +behindelse()
andendif()
.<expression>
can take a multitude of different forms, have a look at the CMake documentation for a complete list. Important examples are:-IF(${variable}) +if(${variable}) - the body will be evaluated if the variable "variable" is defined and synonymous to true, e.g. 1, TRUE, ON, YES (modulo case insensitivity) -IF(variable MATCHES <regular expression>) +if(variable MATCHES <regular expression>) - the body will be evaluated if the variable "variable" is defined and matches the specified regular expression -IF("${variable}" STREQUAL "foobar") +if("${variable}" STREQUAL "foobar") - the body will be evaluated if both strings are equal. Note that "${variable}" will be replaced by the content of the (string) variable "variable" @@ -398,23 +398,23 @@ ENDIF()SET(TARGET step-1) SET(TARGET_SRC step-1.cc) -CMAKE_MINIMUM_REQUIRED(VERSION 3.3.0) -FIND_PACKAGE(deal.II 9.4.0 REQUIRED +CMAKE_MINIMUM_REQUIRED(VERSION 3.13.4) +FIND_PACKAGE(deal.II 9.5.0 REQUIRED HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR} ) @@ -215,8 +215,8 @@ tests/support/my_test_2.outputFOR
statements. The former takes the same<expression>
as theIF
statement:-WHILE(<expression>) +while(<expression>) ... -ENDWHILE() +endwhile()Given a variablelist
containing a list, the individual elementselement
can be accessed with aFOREACH
statement:-FOREACH(element ${list}) +foreach(element ${list}) ... -ENDFOREACH() +endforeach()Note: It is also possible to specify the list directly:-FOREACH(element foo bar baz) +foreach(element foo bar baz) # The variable element will iterate through foo, bar and baz. -ENDFOREACH +endforeach@@ -429,8 +429,8 @@ The following example will pick up every source file underSOURCE_DIR/sources/
and add it to an executable:-FILE(GLOB sources ${CMAKE_SOURCE_DIR}/source/*.cc) -ADD_EXECUTABLE(mycode ${sources}) +file(GLOB sources ${CMAKE_SOURCE_DIR}/source/*.cc) +add_executable(mycode ${sources})Please be aware of one caveat of this approach: Due to the fact that @@ -442,11 +442,11 @@ source file you have to touch a
CMakeLists.txt
file or to run -+
DEAL_II_SETUP_TARGET
revisited
deal_ii_setup_target
revisited- The
DEAL_II_SETUP_TARGET
macro is responsible for setting up + Thedeal_ii_setup_target
macro is responsible for setting up a target to compile and link against deal.II. It will append the -INCLUDE_DIRECTORIES
property with the location of the +include_directories
property with the location of the deal.II include directories, and append the propertiesCOMPILE_FLAGS
,COMPILE_DEFINITIONS
andLINK_FLAGS
by their respective values from the deal.II @@ -460,7 +460,7 @@ source file you have to touch aCMakeLists.txt
file or to run- Optionally, the
DEAL_II_SETUP_TARGET
macro takes an + Optionally, thedeal_ii_setup_target
macro takes an additional argumentDEBUG
, orRELEASE
, after the target name to explicitly state the library flavor the target should be set up for. If the parameter is omitted, the correct choice is deduced @@ -500,11 +500,11 @@ TheDEAL_II_INITIALIZE_CACHED_VARIABLES
macro is responsible for setting up cached variables and has to invoked before thePROJECT
call:-FIND_PACKAGE(deal.II 9.4.0 REQUIRED) +find_package(deal.II 9.5.0 REQUIRED) -DEAL_II_INITIALIZE_CACHED_VARIABLES() +deal_ii_initialize_cached_variables() -PROJECT(myproject) +project(myproject)The macro will set an uninitializedCMAKE_BUILD_TYPE
variable to the build type of deal.II, i.e.DEAL_II_BUILD_TYPE
. If @@ -523,10 +523,10 @@ string.Note: If you wish to override the flags and definitions set by the -
@@ -540,11 +540,11 @@ href="http://cmake.org/cmake/help/v2.8.8/cmake.html">CMake documentation for further details):DEAL_II_SETUP_TARGET
macro you have to override the +deal_ii_setup_target
macro you have to override the correspondingDEAL_II_*
variable instead. See the documentation ofDEAL_II_SETUP_TARGET
+href="#cmakeadvanced.setup_target">deal_ii_setup_target
for further details.-INCLUDE_DIRECTORIES(include1 include2) +include_directories(include1 include2) -ADD_DEFINITIONS(-DFOO -DBAR="BAZ") +add_definitions(-DFOO -DBAR="BAZ") -ADD_EXECUTABLE(...) # or ADD_LIBRARY(...) +add_executable(...) # or add_library(...)@@ -557,7 +557,7 @@ a CMake find module is available, including this external library in your project is more or less straightforward. E.g. to require an external project "foo" at least of version 8.0 write:-FIND_PACKAGE(foo 8.0 REQUIRED) +find_package(foo 8.0 REQUIRED)Alternatively, the version number andREQUIRED
keyword can be omitted. Depending on the external library, the project configuration or @@ -566,16 +566,16 @@ andFOO_LIBRARIES
that can be directly used in yourCMakeLists.txt
file:-INCLUDE_DIRECTORIES(${FOO_INCLUDE_DIRS}) +include_directories(${FOO_INCLUDE_DIRS}) -ADD_EXECUTABLE(mycode mycode.cc) -DEAL_II_SETUP_TARGET(mycode) +add_executable(mycode mycode.cc) +deal_ii_setup_target(mycode) -TARGET_LINK_LIBRARIES(mycode ${FOO_LIBRARIES}) +target_link_libraries(mycode ${FOO_LIBRARIES})The first statement will set up the include directories for the following targets as explained above. The last statement with -TARGET_LINK_LIBRARIES
will add the libraries in the +target_link_libraries
will add the libraries in theFOO_LIBRARIES
variable to the link interface of the targetmycode
. @@ -591,13 +591,13 @@ a copy of a skeletonrun
folder from the source directory:# Copy folder run from the source to the build directory: -FILE(COPY ${CMAKE_SOURCE_DIR}/run DESTINATION ${CMAKE_BINARY_DIR}) +file(COPY ${CMAKE_SOURCE_DIR}/run DESTINATION ${CMAKE_BINARY_DIR}) -ADD_EXECUTABLE(mycode mycode.cc) -SET_PROPERTY(TARGET mycode +add_executable(mycode mycode.cc) +set_property(TARGET mycode PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/run ) -ADD_CUSTOM_TARGET(run +add_custom_target(run COMMAND mycode WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/run ) @@ -639,12 +639,12 @@ INSTALL(DIRECTORY run DESTINATION share/mycode/run) write adiff --git a/doc/users/testsuite.html b/doc/users/testsuite.html index 21fd677cb8..324f1fc2ad 100644 --- a/doc/users/testsuite.html +++ b/doc/users/testsuite.html @@ -110,8 +110,8 @@ tests/my_test.prmCMakeLists.txt
file may be to use an "autopilot" style macro. Here is a minimalistic example for the step-1 tutorial program (plain text version) that can be used for simple + target="_top">plain text version) that can be used for simple projects:-FIND_PACKAGE(deal.II 9.4.0 REQUIRED +find_package(deal.II 9.5.0 REQUIRED HINTS ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR} # You can specify additional hints for search paths here, e.g. @@ -652,18 +652,18 @@ FIND_PACKAGE(deal.II 9.4.0 REQUIRED ) # Set the name of the project and target: -SET(TARGET "step-1") +set(TARGET "step-1") # Declare all source files the target consists of: -SET(TARGET_SRC +set(TARGET_SRC step-1.cc # You can specify additional files here! ) -CMAKE_MINIMUM_REQUIRED(VERSION 3.3.0) -DEAL_II_INITIALIZE_CACHED_VARIABLES() -PROJECT(${TARGET} CXX) -DEAL_II_INVOKE_AUTOPILOT() +cmake_minimum_required(VERSION 3.13.4) +deal_ii_initialize_cached_variables() +project(${TARGET} CXX) +deal_ii_invoke_autopilot()@@ -721,14 +721,14 @@ $ cmake . # Specify a list of files (file globs) that will be removed # with the "make runclean" and "make distclean" targets. # (If empty, sensible default values will be used.) -SET(CLEAN_UP_FILES +set(CLEAN_UP_FILES # a custom list of globs, e.g. *.log *.vtk ) # (Optional) # A custom command line that should be invoked by "make run". # (If empty, ./${TARGET} will be invoked.) -SET(TARGET_RUN +set(TARGET_RUN # a custom command line, e.g. mpirun -np 2 ${TARGET} )
# top-level CMakelists.txt -CMAKE_MINIMUM_REQUIRED(VERSION 3.3.0) -FIND_PACKAGE(deal.II 9.4.0 REQUIRED +CMAKE_MINIMUM_REQUIRED(VERSION 3.13.4) +FIND_PACKAGE(deal.II 9.5.0 REQUIRED HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR} )