ctkMacroBuildLib.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0.txt
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. ###########################################################################
  20. #
  21. # Depends on:
  22. # CTK/CMake/ctkMacroParseArguments.cmake
  23. #
  24. #! \brief Build a CTK library.
  25. #!
  26. #! \ingroup CMakeAPI
  27. macro(ctkMacroBuildLib)
  28. ctkMacroParseArguments(MY
  29. "NAME;EXPORT_DIRECTIVE;SRCS;MOC_SRCS;GENERATE_MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;QT5_MODULES;TARGET_LIBRARIES;RESOURCES;LIBRARY_TYPE"
  30. "ENABLE_QTTESTING"
  31. ${ARGN}
  32. )
  33. # Sanity checks
  34. if(NOT DEFINED MY_NAME)
  35. message(FATAL_ERROR "NAME is mandatory")
  36. endif()
  37. string(REGEX MATCH "^CTK.+" valid_library_name ${MY_NAME})
  38. if(NOT valid_library_name)
  39. message(FATAL_ERROR "CTK library name [${MY_NAME}] should start with 'CTK' uppercase !")
  40. endif()
  41. if(NOT DEFINED MY_EXPORT_DIRECTIVE)
  42. message(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
  43. endif()
  44. if(NOT DEFINED MY_LIBRARY_TYPE)
  45. set(MY_LIBRARY_TYPE "SHARED")
  46. endif()
  47. if(MY_QT5_MODULES)
  48. set(qt5_use_modules_list)
  49. if(NOT CTK_USE_QT5)
  50. message(WARNING "Argument QT5_MODULES ignored because CTK_USE_QT5 is not set.")
  51. else()
  52. foreach(qt5_module ${MY_QT5_MODULES})
  53. find_package(${qt5_module} REQUIRED)
  54. # strip the "Qt5" string from the module name
  55. string(SUBSTRING ${qt5_module} 3 -1 _qt5_module_name)
  56. list(APPEND qt5_use_modules_list ${_qt5_module_name})
  57. endforeach()
  58. endif()
  59. endif()
  60. # Define library name
  61. set(lib_name ${MY_NAME})
  62. # Library target names must not contain a '_' (reserved for plug-in target names)
  63. if(lib_name MATCHES _)
  64. message(FATAL_ERROR "The library name ${lib_name} must not contain a '_' character.")
  65. endif()
  66. # --------------------------------------------------------------------------
  67. # Include dirs
  68. set(my_includes
  69. ${CMAKE_CURRENT_SOURCE_DIR}
  70. ${CMAKE_CURRENT_BINARY_DIR}
  71. # with CMake >2.9, use QT4_MAKE_OUTPUT_FILE instead ?
  72. ${CMAKE_CURRENT_BINARY_DIR}/Resources/UI
  73. ${MY_INCLUDE_DIRECTORIES}
  74. )
  75. # Add the include directories from the library dependencies
  76. ctkFunctionGetIncludeDirs(my_includes ${lib_name})
  77. include_directories(
  78. ${my_includes}
  79. )
  80. #message(lib_name:${lib_name})
  81. #foreach(i ${my_includes})
  82. # message(i:${i})
  83. #endforeach()
  84. # Add the library directories from the external project
  85. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  86. link_directories(
  87. ${my_library_dirs}
  88. )
  89. set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  90. set(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
  91. string(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  92. set(MY_LIBNAME ${lib_name})
  93. configure_file(
  94. ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
  95. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  96. )
  97. set(dynamicHeaders
  98. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  99. # Make sure variable are cleared
  100. set(MY_MOC_CPP)
  101. set(MY_UI_CPP)
  102. set(MY_QRC_SRCS)
  103. # Wrap
  104. if(MY_MOC_SRCS)
  105. # this is a workaround for Visual Studio. The relative include paths in the generated
  106. # moc files can get very long and can't be resolved by the MSVC compiler.
  107. if(Qt5Core_FOUND)
  108. foreach(moc_src ${MY_MOC_SRCS})
  109. qt5_wrap_cpp(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  110. endforeach()
  111. else()
  112. foreach(moc_src ${MY_MOC_SRCS})
  113. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  114. endforeach()
  115. endif()
  116. endif()
  117. if(MY_GENERATE_MOC_SRCS)
  118. QT4_GENERATE_MOCS(${MY_GENERATE_MOC_SRCS})
  119. endif()
  120. if(CTK_USE_QT5)
  121. if(Qt5Widgets_FOUND)
  122. qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
  123. elseif(MY_UI_FORMS)
  124. message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
  125. endif()
  126. else()
  127. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  128. endif()
  129. if(DEFINED MY_RESOURCES)
  130. if(Qt5Core_FOUND)
  131. qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
  132. else()
  133. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  134. endif()
  135. endif()
  136. source_group("Resources" FILES
  137. ${MY_RESOURCES}
  138. ${MY_UI_FORMS}
  139. )
  140. source_group("Generated" FILES
  141. ${MY_QRC_SRCS}
  142. ${MY_MOC_CPP}
  143. ${MY_UI_CPP}
  144. ${MOC_CPP_DECORATOR}
  145. )
  146. add_library(${lib_name} ${MY_LIBRARY_TYPE}
  147. ${MY_SRCS}
  148. ${MY_MOC_CPP}
  149. ${MY_UI_CPP}
  150. ${MY_QRC_SRCS}
  151. )
  152. if(CTK_USE_QT5 AND qt5_use_modules_list)
  153. qt5_use_modules(${lib_name} ${qt5_use_modules_list})
  154. endif()
  155. # Set labels associated with the target.
  156. set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})
  157. # Apply user-defined properties to the library target.
  158. if(CTK_LIBRARY_PROPERTIES AND MY_LIBRARY_TYPE STREQUAL "SHARED")
  159. set_target_properties(${lib_name} PROPERTIES ${CTK_LIBRARY_PROPERTIES})
  160. endif()
  161. set_target_properties(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  162. # Library properties specific to STATIC build
  163. if(MY_LIBRARY_TYPE STREQUAL "STATIC")
  164. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  165. set_target_properties(${lib_name} PROPERTIES COMPILE_FLAGS "-fPIC")
  166. endif()
  167. endif()
  168. # Install rules
  169. if(MY_LIBRARY_TYPE STREQUAL "SHARED")
  170. install(TARGETS ${lib_name}
  171. RUNTIME DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  172. LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  173. ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  174. endif()
  175. set(my_libs
  176. ${MY_TARGET_LIBRARIES}
  177. )
  178. if(MINGW)
  179. list(APPEND my_libs ssp) # add stack smash protection lib
  180. endif()
  181. target_link_libraries(${lib_name} ${my_libs})
  182. # Update CTK_BASE_LIBRARIES
  183. set(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
  184. set(CTK_LIBRARIES ${CTK_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK libraries" FORCE)
  185. set(CTK_BASE_INCLUDE_DIRS ${CTK_BASE_INCLUDE_DIRS} ${my_includes} CACHE INTERNAL "CTK includes" FORCE)
  186. # Install headers
  187. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
  188. install(FILES
  189. ${headers}
  190. ${dynamicHeaders}
  191. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  192. )
  193. endmacro()