ctkMacroBuildLib.cmake 6.2 KB

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