NiceModules.cmake 9.6 KB

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