ctkMacroBuildLib.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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;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. # Define library name
  48. set(lib_name ${MY_NAME})
  49. # Library target names must not contain a '_' (reserved for plug-in target names)
  50. if(lib_name MATCHES _)
  51. message(FATAL_ERROR "The library name ${lib_name} must not contain a '_' character.")
  52. endif()
  53. # --------------------------------------------------------------------------
  54. # Include dirs
  55. set(my_includes
  56. ${CMAKE_CURRENT_SOURCE_DIR}
  57. ${CMAKE_CURRENT_BINARY_DIR}
  58. # with CMake >2.9, use QT4_MAKE_OUTPUT_FILE instead ?
  59. ${CMAKE_CURRENT_BINARY_DIR}/Resources/UI
  60. ${MY_INCLUDE_DIRECTORIES}
  61. )
  62. # Add the include directories from the library dependencies
  63. ctkFunctionGetIncludeDirs(my_includes ${lib_name})
  64. include_directories(
  65. ${my_includes}
  66. )
  67. #message(lib_name:${lib_name})
  68. #foreach(i ${my_includes})
  69. # message(i:${i})
  70. #endforeach()
  71. if(CTK_QT_VERSION VERSION_LESS "5")
  72. # Add Qt include dirs and defines
  73. include(${QT_USE_FILE})
  74. endif()
  75. # Add the library directories from the external project
  76. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  77. link_directories(
  78. ${my_library_dirs}
  79. )
  80. set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  81. set(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
  82. string(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  83. set(MY_LIBNAME ${lib_name})
  84. configure_file(
  85. ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
  86. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  87. )
  88. set(dynamicHeaders
  89. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  90. # Make sure variable are cleared
  91. set(MY_MOC_CPP)
  92. set(MY_UI_CPP)
  93. set(MY_QRC_SRCS)
  94. # Wrap
  95. if(MY_MOC_SRCS)
  96. # this is a workaround for Visual Studio. The relative include paths in the generated
  97. # moc files can get very long and can't be resolved by the MSVC compiler.
  98. if(CTK_QT_VERSION VERSION_GREATER "4")
  99. foreach(moc_src ${MY_MOC_SRCS})
  100. qt5_wrap_cpp(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  101. endforeach()
  102. else()
  103. foreach(moc_src ${MY_MOC_SRCS})
  104. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  105. endforeach()
  106. endif()
  107. endif()
  108. if(MY_GENERATE_MOC_SRCS)
  109. QT4_GENERATE_MOCS(${MY_GENERATE_MOC_SRCS})
  110. endif()
  111. if(CTK_QT_VERSION VERSION_GREATER "4")
  112. if(Qt5Widgets_FOUND)
  113. qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
  114. elseif(MY_UI_FORMS)
  115. message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
  116. endif()
  117. else()
  118. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  119. endif()
  120. if(DEFINED MY_RESOURCES)
  121. if(CTK_QT_VERSION VERSION_GREATER "4")
  122. qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
  123. else()
  124. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  125. endif()
  126. endif()
  127. source_group("Resources" FILES
  128. ${MY_RESOURCES}
  129. ${MY_UI_FORMS}
  130. )
  131. source_group("Generated" FILES
  132. ${MY_QRC_SRCS}
  133. ${MY_MOC_CPP}
  134. ${MY_UI_CPP}
  135. ${MOC_CPP_DECORATOR}
  136. )
  137. add_library(${lib_name} ${MY_LIBRARY_TYPE}
  138. ${MY_SRCS}
  139. ${MY_MOC_CPP}
  140. ${MY_UI_CPP}
  141. ${MY_QRC_SRCS}
  142. )
  143. # Set labels associated with the target.
  144. set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})
  145. # Apply user-defined properties to the library target.
  146. if(CTK_LIBRARY_PROPERTIES AND MY_LIBRARY_TYPE STREQUAL "SHARED")
  147. set_target_properties(${lib_name} PROPERTIES ${CTK_LIBRARY_PROPERTIES})
  148. endif()
  149. set_target_properties(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  150. # Library properties specific to STATIC build
  151. if(MY_LIBRARY_TYPE STREQUAL "STATIC")
  152. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  153. set_target_properties(${lib_name} PROPERTIES COMPILE_FLAGS "-fPIC")
  154. endif()
  155. endif()
  156. # Install rules
  157. if(MY_LIBRARY_TYPE STREQUAL "SHARED")
  158. install(TARGETS ${lib_name} EXPORT CTKExports
  159. RUNTIME DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  160. LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  161. ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  162. endif()
  163. set(my_libs
  164. ${MY_TARGET_LIBRARIES}
  165. )
  166. if(MINGW)
  167. list(APPEND my_libs ssp) # add stack smash protection lib
  168. endif()
  169. target_link_libraries(${lib_name} ${my_libs})
  170. # Update CTK_BASE_LIBRARIES
  171. set(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
  172. set(CTK_LIBRARIES ${CTK_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK libraries" FORCE)
  173. set(CTK_BASE_INCLUDE_DIRS ${CTK_BASE_INCLUDE_DIRS} ${my_includes} CACHE INTERNAL "CTK includes" FORCE)
  174. # Install headers
  175. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
  176. install(FILES
  177. ${headers}
  178. ${dynamicHeaders}
  179. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  180. )
  181. endmacro()