NiceModules.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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}_MEXFILES_SRC "")
  68. set(nice_${the_library}_SRC"")
  69. #message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")
  70. file(GLOB_RECURSE list_tmp1 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" *.cpp *.tcc *.c)
  71. #message(STATUS "list_tmp1: ${list_tmp1}")
  72. foreach( t_SrcFile ${list_tmp1})
  73. if( NOT t_SrcFile MATCHES "moc_" )
  74. if( t_SrcFile MATCHES "tests/" )
  75. #message(STATUS "test: ${t_SrcFile}")
  76. LIST(APPEND nice_${the_library}_TESTFILES_SRC ${t_SrcFile})
  77. elseif( t_SrcFile MATCHES "progs/" )
  78. #message(STATUS "prog: ${t_SrcFile}")
  79. LIST(APPEND nice_${the_library}_PROGFILES_SRC ${t_SrcFile})
  80. elseif( t_SrcFile MATCHES "Mex[.]" )
  81. message(STATUS "mex: ${t_SrcFile}")
  82. LIST(APPEND nice_${the_library}_MEXFILES_SRC ${t_SrcFile})
  83. else()
  84. LIST(APPEND nice_${the_library}_SRC ${t_SrcFile})
  85. endif()
  86. endif()
  87. endforeach()
  88. ### Get all cpp and tcc files recursively but exclude progs and unit test files
  89. #file(GLOB_RECURSE nice_${the_library}_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp *.tcc)
  90. #filter out source files listed on the blacklist to be excluded from build
  91. if(list_exclude_from_build_SRC)
  92. list( REMOVE_ITEM nice_${the_library}_SRC ${list_exclude_from_build_SRC} )
  93. list( REMOVE_ITEM nice_${the_library}_TESTFILES_SRC ${list_exclude_from_build_SRC} )
  94. endif()
  95. #message(STATUS "globallyrecusive_tests: ${nice_${the_library}_TESTFILES_SRC}")
  96. #message(STATUS "globallyrecusive_progs: ${nice_${the_library}_PROGFILES_SRC}")
  97. ### Get all header files recursively
  98. file(GLOB_RECURSE nice_${the_library}_HDR RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.h)
  99. else()
  100. message(STATUS "Using source files from file lists (*.cmake)")
  101. #define variable nice_<libname>_HDR and nice_<libname>_SRC for library header and source files (don't include progs and test source files here)
  102. include( corefiles.cmake)
  103. #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)
  104. include( progfiles.cmake)
  105. #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)
  106. include( testfiles.cmake)
  107. endif()
  108. endmacro()
  109. macro(nice_build_library)
  110. ADD_LIBRARY("nice_${the_library}" ${nice_${the_library}_HDR} ${nice_${the_library}_SRC})
  111. TARGET_LINK_LIBRARIES("nice_${the_library}" ${nice_${the_library}_LINKING_DEPENDENCIES})
  112. #TARGET_LINK_LIBRARIES("nice_${the_library}" ${nice_${the_library}_LINKING_DEPENDENCIES} ${Boost_LIBRARIES} ${OPENGL_LIBRARY} ${GLUT_LIBRARY} ${QT_LIBRARIES})
  113. SET_PROPERTY(TARGET "nice_${the_library}" PROPERTY FOLDER "library")
  114. INSTALL(TARGETS "nice_${the_library}" DESTINATION lib EXPORT "nice_${the_library}-exports")
  115. INSTALL(EXPORT "nice_${the_library}-exports" DESTINATION lib/exports)
  116. install(DIRECTORY ./ DESTINATION "include/${the_library}"
  117. FILES_MATCHING
  118. PATTERN "*.h"
  119. PATTERN "*.tcc"
  120. PATTERN ".git" EXCLUDE)
  121. configure_file( ../cmake/niceConfig.cmake.in "${PROJECT_BINARY_DIR}/lib/nice_${the_library}Config.cmake" )
  122. endmacro()
  123. # Create builds for all programs that are in the subvariable ${nice_${the_library}_PROGFILES_SRC}
  124. # and build them into "bin/${the_library}"
  125. #
  126. # Variable BUILD_PROGRAMS has to be set and true to invoke progam build. The Variable is controllable
  127. # via the main CMakeLists.txt file (or ccmake)
  128. #
  129. #Note: Variable ${the_library} contains the current (sub-)library name that called this function,
  130. # e.g. ${the_library} == 'core', ${the_library}=='vislearning'
  131. # using subvariables ${nice_${the_library}_PROGFILES_SRC}
  132. #
  133. # Author: Johannes Ruehle
  134. # Date: 2014-02-17
  135. #
  136. macro(nice_add_progs)
  137. if(BUILD_PROGRAMS)
  138. message(STATUS "building progs:")
  139. foreach(__progcpp ${nice_${the_library}_PROGFILES_SRC})
  140. get_filename_component(__progname ${__progcpp} NAME_WE )
  141. #message(STATUS "progname: ${__progname} ${__progcpp}")
  142. set(prog_target_name "${the_library}_${__progname}")
  143. ADD_EXECUTABLE( ${prog_target_name} ${__progcpp})
  144. TARGET_LINK_LIBRARIES(${prog_target_name} "nice_${the_library}")
  145. SET_TARGET_PROPERTIES(${prog_target_name} PROPERTIES OUTPUT_NAME "${__progname}")
  146. INSTALL(TARGETS ${prog_target_name} DESTINATION "bin/${the_library}")
  147. SET_PROPERTY(TARGET ${prog_target_name} PROPERTY FOLDER "programs/${the_library}")
  148. endforeach()
  149. endif()
  150. endmacro()
  151. # Add mex output
  152. macro(nice_add_mexes)
  153. if(WITH_MEX)
  154. message(STATUS "building mexes:")
  155. foreach(__mexcpp ${nice_${the_library}_MEXFILES_SRC})
  156. get_filename_component(__mexname ${__mexcpp} NAME_WE )
  157. message(STATUS "mexname: ${__mexname} ${__mexcpp}")
  158. set(mex_target_name "${the_library}_${__mexname}")
  159. ADD_LIBRARY("${mex_target_name}" SHARED ${__mexcpp})
  160. TARGET_LINK_LIBRARIES(${mex_target_name} "nice_${the_library}")
  161. SET_TARGET_PROPERTIES(${mex_target_name} PROPERTIES OUTPUT_NAME "${__mexname}")
  162. SET_TARGET_PROPERTIES(${mex_target_name} PROPERTIES SUFFIX "${MEX_ENDING}")
  163. SET_TARGET_PROPERTIES(${mex_target_name} PROPERTIES PREFIX "")
  164. INSTALL(TARGETS ${mex_target_name} DESTINATION "bin/${the_library}")
  165. SET_PROPERTY(TARGET ${mex_target_name} PROPERTY FOLDER "programs/${the_library}")
  166. endforeach()
  167. endif()
  168. endmacro()
  169. # Create unit test (using library CppUnitTest) for all cpp files in the subvariable ${nice_${the_library}_TESTFILES_SRC}
  170. # and build them into "bin/${the_library}"
  171. #
  172. # Variable BUILD_UNITTESTS has to be set and true to invoke unit test building.
  173. # The Variable is controllable via the main CMakeLists.txt file (or ccmake)
  174. #
  175. #Note: Variable ${the_library} contains the current (sub-)library name that called this function,
  176. # e.g. ${the_library} == 'core', ${the_library}=='vislearning'
  177. #
  178. # Author: Johannes Ruehle
  179. # Date: 2014-02-17
  180. #
  181. macro(nice_add_unittests)
  182. if(BUILD_UNITTESTS)
  183. INCLUDE_DIRECTORIES(${CPPUNIT_INCLUDE_DIR})
  184. message(STATUS "building tests:")
  185. foreach(__testcpp ${nice_${the_library}_TESTFILES_SRC})
  186. get_filename_component(__testname ${__testcpp} NAME_WE )
  187. nice_get_real_path(__testname_abspath ${__testcpp})
  188. get_filename_component(__testname_dir ${__testname_abspath} PATH)
  189. message(STATUS "unittest: ${__testname} ${__testcpp}")
  190. ADD_EXECUTABLE( ${__testname} ../templates/cppUnitTestRunner.cpp ${__testcpp})
  191. TARGET_LINK_LIBRARIES(${__testname} "nice_${the_library}" ${CPPUNIT_LIBRARIES} )
  192. INSTALL(TARGETS ${__testname} DESTINATION "tests/${the_library}")
  193. SET_PROPERTY(TARGET ${__testname} PROPERTY FOLDER "unittests/${the_library}")
  194. ADD_TEST(NAME ${__testname} WORKING_DIRECTORY ${__testname_dir} COMMAND ${__testname})
  195. endforeach()
  196. endif()
  197. endmacro()
  198. # Provides an option that the user can optionally select.
  199. # Can accept condition to control when option is available for user.
  200. # Usage:
  201. # option(<option_variable> "help string describing the option" <initial value or boolean expression> [IF <condition>])
  202. #
  203. # CopyRight: OpenCV
  204. macro(NICE_OPTION variable description value)
  205. set(__value ${value})
  206. set(__condition "")
  207. set(__varname "__value")
  208. foreach(arg ${ARGN})
  209. if(arg STREQUAL "IF" OR arg STREQUAL "if")
  210. set(__varname "__condition")
  211. else()
  212. list(APPEND ${__varname} ${arg})
  213. endif()
  214. endforeach()
  215. unset(__varname)
  216. if("${__condition}" STREQUAL "")
  217. set(__condition 2 GREATER 1)
  218. endif()
  219. if(${__condition})
  220. if("${__value}" MATCHES ";")
  221. if(${__value})
  222. option(${variable} "${description}" ON)
  223. else()
  224. option(${variable} "${description}" OFF)
  225. endif()
  226. elseif(DEFINED ${__value})
  227. if(${__value})
  228. option(${variable} "${description}" ON)
  229. else()
  230. option(${variable} "${description}" OFF)
  231. endif()
  232. else()
  233. option(${variable} "${description}" ${__value})
  234. endif()
  235. else()
  236. unset(${variable} CACHE)
  237. endif()
  238. unset(__condition)
  239. unset(__value)
  240. endmacro()