NiceModules.cmake 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. PATTERN ".git" EXCLUDE)
  117. configure_file( ../cmake/niceConfig.cmake.in "${PROJECT_BINARY_DIR}/lib/nice_${the_library}Config.cmake" )
  118. endmacro()
  119. # Create builds for all programs that are in the subvariable ${nice_${the_library}_PROGFILES_SRC}
  120. # and build them into "bin/${the_library}"
  121. #
  122. # Variable BUILD_PROGRAMS has to be set and true to invoke progam build. The Variable is controllable
  123. # via the main CMakeLists.txt file (or ccmake)
  124. #
  125. #Note: Variable ${the_library} contains the current (sub-)library name that called this function,
  126. # e.g. ${the_library} == 'core', ${the_library}=='vislearning'
  127. # using subvariables ${nice_${the_library}_PROGFILES_SRC}
  128. #
  129. # Author: Johannes Ruehle
  130. # Date: 2014-02-17
  131. #
  132. macro(nice_add_progs)
  133. if(BUILD_PROGRAMS)
  134. message(STATUS "building progs:")
  135. foreach(__progcpp ${nice_${the_library}_PROGFILES_SRC})
  136. get_filename_component(__progname ${__progcpp} NAME_WE )
  137. #message(STATUS "progname: ${__progname} ${__progcpp}")
  138. set(prog_target_name "${the_library}_${__progname}")
  139. ADD_EXECUTABLE( ${prog_target_name} ${__progcpp})
  140. TARGET_LINK_LIBRARIES(${prog_target_name} "nice_${the_library}")
  141. SET_TARGET_PROPERTIES(${prog_target_name} PROPERTIES OUTPUT_NAME "${__progname}")
  142. INSTALL(TARGETS ${prog_target_name} DESTINATION "bin/${the_library}")
  143. SET_PROPERTY(TARGET ${prog_target_name} PROPERTY FOLDER "programs/${the_library}")
  144. endforeach()
  145. endif()
  146. endmacro()
  147. # Create unit test (using library CppUnitTest) for all cpp files in the subvariable ${nice_${the_library}_TESTFILES_SRC}
  148. # and build them into "bin/${the_library}"
  149. #
  150. # Variable BUILD_UNITTESTS has to be set and true to invoke unit test building.
  151. # The Variable is controllable via the main CMakeLists.txt file (or ccmake)
  152. #
  153. #Note: Variable ${the_library} contains the current (sub-)library name that called this function,
  154. # e.g. ${the_library} == 'core', ${the_library}=='vislearning'
  155. #
  156. # Author: Johannes Ruehle
  157. # Date: 2014-02-17
  158. #
  159. macro(nice_add_unittests)
  160. if(BUILD_UNITTESTS)
  161. INCLUDE_DIRECTORIES(${CPPUNIT_INCLUDE_DIR})
  162. message(STATUS "building tests:")
  163. foreach(__testcpp ${nice_${the_library}_TESTFILES_SRC})
  164. get_filename_component(__testname ${__testcpp} NAME_WE )
  165. nice_get_real_path(__testname_abspath ${__testcpp})
  166. get_filename_component(__testname_dir ${__testname_abspath} PATH)
  167. message(STATUS "unittest: ${__testname} ${__testcpp}")
  168. ADD_EXECUTABLE( ${__testname} ../templates/cppUnitTestRunner.cpp ${__testcpp})
  169. TARGET_LINK_LIBRARIES(${__testname} "nice_${the_library}" ${CPPUNIT_LIBRARIES} )
  170. INSTALL(TARGETS ${__testname} DESTINATION "tests/${the_library}")
  171. SET_PROPERTY(TARGET ${__testname} PROPERTY FOLDER "unittests/${the_library}")
  172. ADD_TEST(NAME ${__testname} WORKING_DIRECTORY ${__testname_dir} COMMAND ${__testname})
  173. endforeach()
  174. endif()
  175. endmacro()
  176. # Provides an option that the user can optionally select.
  177. # Can accept condition to control when option is available for user.
  178. # Usage:
  179. # option(<option_variable> "help string describing the option" <initial value or boolean expression> [IF <condition>])
  180. #
  181. # CopyRight: OpenCV
  182. macro(NICE_OPTION variable description value)
  183. set(__value ${value})
  184. set(__condition "")
  185. set(__varname "__value")
  186. foreach(arg ${ARGN})
  187. if(arg STREQUAL "IF" OR arg STREQUAL "if")
  188. set(__varname "__condition")
  189. else()
  190. list(APPEND ${__varname} ${arg})
  191. endif()
  192. endforeach()
  193. unset(__varname)
  194. if("${__condition}" STREQUAL "")
  195. set(__condition 2 GREATER 1)
  196. endif()
  197. if(${__condition})
  198. if("${__value}" MATCHES ";")
  199. if(${__value})
  200. option(${variable} "${description}" ON)
  201. else()
  202. option(${variable} "${description}" OFF)
  203. endif()
  204. elseif(DEFINED ${__value})
  205. if(${__value})
  206. option(${variable} "${description}" ON)
  207. else()
  208. option(${variable} "${description}" OFF)
  209. endif()
  210. else()
  211. option(${variable} "${description}" ${__value})
  212. endif()
  213. else()
  214. unset(${variable} CACHE)
  215. endif()
  216. unset(__condition)
  217. unset(__value)
  218. endmacro()