NiceModules.cmake 12 KB

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