NiceModules.cmake 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ####################################################################################
  2. # Help function for building the NICE library and its sub-libraries.
  3. # Inspired by the OpenCV cmake build system.
  4. #
  5. # Author: Johannes Ruehle
  6. # Date: 2014-02-17
  7. #
  8. ####################################################################################
  9. # get a list of all sub directories in curdir
  10. MACRO(SUBDIRLIST result curdir)
  11. FILE(GLOB children RELATIVE ${curdir} ${curdir}/*)
  12. SET(dirlist "")
  13. FOREACH(child ${children})
  14. IF(IS_DIRECTORY ${curdir}/${child})
  15. SET(dirlist ${dirlist} ${child})
  16. ENDIF()
  17. ENDFOREACH()
  18. SET(${result} ${dirlist})
  19. ENDMACRO()
  20. # get absolute path with symlinks resolved
  21. macro(nice_get_real_path VAR PATHSTR)
  22. if(CMAKE_VERSION VERSION_LESS 2.8)
  23. get_filename_component(${VAR} "${PATHSTR}" ABSOLUTE)
  24. else()
  25. get_filename_component(${VAR} "${PATHSTR}" REALPATH)
  26. endif()
  27. endmacro()
  28. # get the source files and store them in the variables:
  29. # ${nice_${the_library}_TESTFILES_SRC} (unit test cpps)
  30. # ${nice_${the_library}_PROGFILES_SRC} (progs cpps)
  31. # ${nice_${the_library}_SRC} (library cpp and tcc excluding progs and unit test cpps)
  32. # ${nice_${the_library}_HDR} (all library header files)
  33. #
  34. # Two methods for obtaining the source files:
  35. # 1) recursively scan all subdirectories
  36. # cmake variable NICE_SOURCEFILES_FIND_GLOBALLYRECURSIVE has to be set and to be true (1).
  37. # Notes:
  38. # - To exclude certain source files from appending in the source var, use the file black listing
  39. # in a file "list_exclude_from_build.cmake" by adding it to the variable "list_exclude_from_build_SRC"
  40. # in that file.
  41. # - All *moc_* files will be removed from the source file list variables
  42. # 2) use the given *.cmake files that define variables containing the source and header files to be used for build:
  43. # corefiles.cmake (Source files for Library build)
  44. # progfiles.cmake (source files for progam builds)
  45. # testfiles.cmake (source files for unit test builds)
  46. #
  47. #
  48. #Note: Variable ${the_library} contains the current (sub-)library name that called this function,
  49. # e.g. ${the_library} == 'core', ${the_library}=='vislearning'
  50. #
  51. # Author: Johannes Ruehle
  52. # Date: 2014-02-17
  53. #
  54. macro(nice_get_source_files)
  55. if(NICE_SOURCEFILES_FIND_GLOBALLYRECURSIVE)
  56. message(STATUS "Recursively scanning for source files")
  57. #include local file containing a list of files to be excluded from compilation
  58. #list is in variable list_exclude_from_build_SRC
  59. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/list_exclude_from_build.cmake")
  60. #message(STATUS "exclude list found")
  61. include(list_exclude_from_build.cmake)
  62. message(STATUS "exclude list:${list_exclude_from_build_SRC}")
  63. endif()
  64. ### Get all unit test cpp files recursively
  65. set(nice_${the_library}_TESTFILES_SRC "")
  66. set(nice_${the_library}_PROGFILES_SRC "")
  67. set(nice_${the_library}_SRC"")
  68. #message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
  69. file(GLOB_RECURSE list_tmp1 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" *.cpp *.tcc *.c)
  70. #message(STATUS "list_tmp1: ${list_tmp1}")
  71. foreach( t_SrcFile ${list_tmp1})
  72. if( NOT t_SrcFile MATCHES "moc_" )
  73. if( t_SrcFile MATCHES "tests/" )
  74. #message(STATUS "test: ${t_SrcFile}")
  75. LIST(APPEND nice_${the_library}_TESTFILES_SRC ${t_SrcFile})
  76. elseif( t_SrcFile MATCHES "progs/" )
  77. #message(STATUS "prog: ${t_SrcFile}")
  78. LIST(APPEND nice_${the_library}_PROGFILES_SRC ${t_SrcFile})
  79. else()
  80. LIST(APPEND nice_${the_library}_SRC ${t_SrcFile})
  81. endif()
  82. endif()
  83. endforeach()
  84. ### Get all cpp and tcc files recursively but exclude progs and unit test files
  85. #file(GLOB_RECURSE nice_${the_library}_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp *.tcc)
  86. #filter out source files listed on the blacklist to be excluded from build
  87. if(list_exclude_from_build_SRC)
  88. list( REMOVE_ITEM nice_${the_library}_SRC ${list_exclude_from_build_SRC} )
  89. list( REMOVE_ITEM nice_${the_library}_TESTFILES_SRC ${list_exclude_from_build_SRC} )
  90. endif()
  91. #message(STATUS "globallyrecusive_tests: ${nice_${the_library}_TESTFILES_SRC}")
  92. #message(STATUS "globallyrecusive_progs: ${nice_${the_library}_PROGFILES_SRC}")
  93. ### Get all header files recursively
  94. file(GLOB_RECURSE nice_${the_library}_HDR RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)
  95. else()
  96. message(STATUS "Using source files from file lists (*.cmake)")
  97. #define variable nice_<libname>_HDR and nice_<libname>_SRC for library header and source files (don't include progs and test source files here)
  98. include( corefiles.cmake)
  99. #define variable nice_<libname>_PROGFILES_HDR and nice_<libname>_PROGFILES_SRC for program header and source files (don't include library and test source files here)
  100. include( progfiles.cmake)
  101. #define variable nice_<libname>_TESTFILES_HDR and nice_<libname>_TESTFILES_SRC for unit test header and source files (don't include library and progs source files here)
  102. include( testfiles.cmake)
  103. endif()
  104. endmacro()
  105. macro(nice_build_library)
  106. ADD_LIBRARY("nice_${the_library}" ${nice_${the_library}_HDR} ${nice_${the_library}_SRC})
  107. TARGET_LINK_LIBRARIES("nice_${the_library}" ${nice_${the_library}_LINKING_DEPENDENCIES})
  108. #TARGET_LINK_LIBRARIES("nice_${the_library}" ${nice_${the_library}_LINKING_DEPENDENCIES} ${Boost_LIBRARIES} ${OPENGL_LIBRARY} ${GLUT_LIBRARY} ${QT_LIBRARIES})
  109. SET_PROPERTY(TARGET "nice_${the_library}" PROPERTY FOLDER "library")
  110. INSTALL(TARGETS "nice_${the_library}" DESTINATION lib EXPORT "nice_${the_library}-exports")
  111. INSTALL(EXPORT "nice_${the_library}-exports" DESTINATION lib/exports)
  112. install(DIRECTORY ./ DESTINATION "include/${the_library}"
  113. FILES_MATCHING
  114. PATTERN "*.h"
  115. PATTERN "*.tcc")
  116. configure_file( ../cmake/niceConfig.cmake.in "${PROJECT_BINARY_DIR}/lib/nice_${the_library}Config.cmake" )
  117. endmacro()
  118. # Create builds for all programs that are in the subvariable ${nice_${the_library}_PROGFILES_SRC}
  119. # and build them into "bin/${the_library}"
  120. #
  121. # Variable BUILD_PROGRAMS has to be set and true to invoke progam build. The Variable is controllable
  122. # via the main CMakeLists.txt file (or ccmake)
  123. #
  124. #Note: Variable ${the_library} contains the current (sub-)library name that called this function,
  125. # e.g. ${the_library} == 'core', ${the_library}=='vislearning'
  126. # using subvariables ${nice_${the_library}_PROGFILES_SRC}
  127. #
  128. # Author: Johannes Ruehle
  129. # Date: 2014-02-17
  130. #
  131. macro(nice_add_progs)
  132. if(BUILD_PROGRAMS)
  133. message(STATUS "building progs:")
  134. foreach(__progcpp ${nice_${the_library}_PROGFILES_SRC})
  135. get_filename_component(__progname ${__progcpp} NAME_WE )
  136. #message(STATUS "progname: ${__progname} ${__progcpp}")
  137. set(prog_target_name "${the_library}_${__progname}")
  138. ADD_EXECUTABLE( ${prog_target_name} ${__progcpp})
  139. TARGET_LINK_LIBRARIES(${prog_target_name} "nice_${the_library}")
  140. SET_TARGET_PROPERTIES(${prog_target_name} PROPERTIES OUTPUT_NAME "${__progname}")
  141. INSTALL(TARGETS ${prog_target_name} DESTINATION "bin/${the_library}")
  142. SET_PROPERTY(TARGET ${prog_target_name} PROPERTY FOLDER "programs/${the_library}")
  143. endforeach()
  144. endif()
  145. endmacro()
  146. # Create unit test (using library CppUnitTest) for all cpp files in the subvariable ${nice_${the_library}_TESTFILES_SRC}
  147. # and build them into "bin/${the_library}"
  148. #
  149. # Variable BUILD_UNITTESTS has to be set and true to invoke unit test building.
  150. # The Variable is controllable via the main CMakeLists.txt file (or ccmake)
  151. #
  152. #Note: Variable ${the_library} contains the current (sub-)library name that called this function,
  153. # e.g. ${the_library} == 'core', ${the_library}=='vislearning'
  154. #
  155. # Author: Johannes Ruehle
  156. # Date: 2014-02-17
  157. #
  158. macro(nice_add_unittests)
  159. if(BUILD_UNITTESTS)
  160. INCLUDE_DIRECTORIES(${CPPUNIT_INCLUDE_DIR})
  161. message(STATUS "building tests:")
  162. foreach(__testcpp ${nice_${the_library}_TESTFILES_SRC})
  163. get_filename_component(__testname ${__testcpp} NAME_WE )
  164. nice_get_real_path(__testname_abspath ${__testcpp})
  165. get_filename_component(__testname_dir ${__testname_abspath} PATH)
  166. message(STATUS "unittest: ${__testname} ${__testcpp}")
  167. ADD_EXECUTABLE( ${__testname} ../templates/cppUnitTestRunner.cpp ${__testcpp})
  168. TARGET_LINK_LIBRARIES(${__testname} "nice_${the_library}" ${CPPUNIT_LIBRARIES} )
  169. INSTALL(TARGETS ${__testname} DESTINATION "tests/${the_library}")
  170. SET_PROPERTY(TARGET ${__testname} PROPERTY FOLDER "unittests/${the_library}")
  171. ADD_TEST(NAME ${__testname} WORKING_DIRECTORY ${__testname_dir} COMMAND ${__testname})
  172. endforeach()
  173. endif()
  174. endmacro()
  175. # Provides an option that the user can optionally select.
  176. # Can accept condition to control when option is available for user.
  177. # Usage:
  178. # option(<option_variable> "help string describing the option" <initial value or boolean expression> [IF <condition>])
  179. #
  180. # CopyRight: OpenCV
  181. macro(NICE_OPTION variable description value)
  182. set(__value ${value})
  183. set(__condition "")
  184. set(__varname "__value")
  185. foreach(arg ${ARGN})
  186. if(arg STREQUAL "IF" OR arg STREQUAL "if")
  187. set(__varname "__condition")
  188. else()
  189. list(APPEND ${__varname} ${arg})
  190. endif()
  191. endforeach()
  192. unset(__varname)
  193. if("${__condition}" STREQUAL "")
  194. set(__condition 2 GREATER 1)
  195. endif()
  196. if(${__condition})
  197. if("${__value}" MATCHES ";")
  198. if(${__value})
  199. option(${variable} "${description}" ON)
  200. else()
  201. option(${variable} "${description}" OFF)
  202. endif()
  203. elseif(DEFINED ${__value})
  204. if(${__value})
  205. option(${variable} "${description}" ON)
  206. else()
  207. option(${variable} "${description}" OFF)
  208. endif()
  209. else()
  210. option(${variable} "${description}" ${__value})
  211. endif()
  212. else()
  213. unset(${variable} CACHE)
  214. endif()
  215. unset(__condition)
  216. unset(__value)
  217. endmacro()