Aug 26, 2011

[CMake] CMakeLists.txt Template and explained

cmake_minimum_required (VERSION 2.8)
project (KDE Project)


# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
)

#In TutorialConfig.h.in : this is used for mapping previous {
# set (Tutorial_VERSION_MAJOR 1)
# set (Tutorial_VERSION_MINOR 0)
# }
# Cmake will generate a TutorialConfig.h under PROJECT_BINARY_DIR
# So we need to use include_directories to include this path for g++ to use as a include search path.
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#

#
# Include path that g++ will include it as the .h search path
include_directories("${PROJECT_BINARY_DIR}")

# In MathFunctions directory, we need another CmakeLists file contain
# add_library(MathFunctions mysqrt.cxx)
# MathFunctions is a directory, mysqrt.cxx is the .cxx file in that directory ready for compile.


# ask g++ to include  MathFunctions directory for header search.
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")

# ask g++ to build this directory
#The CMakeLists.txt file in the specified source directory will be processed immediately by CMake before processing in the current input file continues beyond this command.
add_subdirectory (MathFunctions)


#-------------------
# should we use our own math functions?
option (USE_MYMATH
        "Use tutorial provided math implementation" ON)
# in TutorialConfig.h.in we write:
# #cmakedefine USE_MYMATH
# to make our source file use this USE_MYMATH define also! (defined in TutorialConfig.h, so our source code should include this file)



# add the MathFunctions library?
#
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

add_executable(RunMe main.cpp)
# ask g++ to use -lMathFunction , that is, to use this library to build RunMe application.
target_link_libraries (RunMe ${EXTRA_LIBS})



#---INSTALL----  make install will do the following:
# In MathFunctions directory's CMakeLists file we add:
# install (TARGETS MathFunctions DESTINATION bin)
# install (FILES MathFunctions.h DESTINATION include)

#for RunMe application:
install (TARGETS RunMe DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"      
         DESTINATION include)

# The CMake variable CMAKE_INSTALL_PREFIX is used to determine the root of where the files will be installed.



#---TEST----
# TutorialRuns : Variable
# RunMe : Application
# 25 : arguments
add_test (TutorialRuns RunMe 25)


set_tests_properties (TutorialRuns
  PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")

#---TEST Macro ---
#define a macro to simplify adding tests, then use it
macro (do_test arg result)
  add_test (TutorialComp${arg} Tutorial ${arg})
  set_tests_properties (TutorialComp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)

# do a bunch of result based tests
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.