ctkMacroBuildLib.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. # 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})
  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. # Add Qt include dirs and defines
  116. include(${QT_USE_FILE})
  117. endif()
  118. if(DEFINED MY_RESOURCES)
  119. if(Qt5Core_FOUND)
  120. qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
  121. else()
  122. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  123. endif()
  124. endif()
  125. source_group("Resources" FILES
  126. ${MY_RESOURCES}
  127. ${MY_UI_FORMS}
  128. )
  129. source_group("Generated" FILES
  130. ${MY_QRC_SRCS}
  131. ${MY_MOC_CPP}
  132. ${MY_UI_CPP}
  133. ${MOC_CPP_DECORATOR}
  134. )
  135. add_library(${lib_name} ${MY_LIBRARY_TYPE}
  136. ${MY_SRCS}
  137. ${MY_MOC_CPP}
  138. ${MY_UI_CPP}
  139. ${MY_QRC_SRCS}
  140. )
  141. # Set labels associated with the target.
  142. set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})
  143. # Apply user-defined properties to the library target.
  144. if(CTK_LIBRARY_PROPERTIES AND MY_LIBRARY_TYPE STREQUAL "SHARED")
  145. set_target_properties(${lib_name} PROPERTIES ${CTK_LIBRARY_PROPERTIES})
  146. endif()
  147. set_target_properties(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  148. # Library properties specific to STATIC build
  149. if(MY_LIBRARY_TYPE STREQUAL "STATIC")
  150. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  151. set_target_properties(${lib_name} PROPERTIES COMPILE_FLAGS "-fPIC")
  152. endif()
  153. endif()
  154. # Install rules
  155. if(MY_LIBRARY_TYPE STREQUAL "SHARED")
  156. install(TARGETS ${lib_name} EXPORT CTKExports
  157. RUNTIME DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  158. LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  159. ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  160. endif()
  161. set(my_libs
  162. ${MY_TARGET_LIBRARIES}
  163. )
  164. if(MINGW)
  165. list(APPEND my_libs ssp) # add stack smash protection lib
  166. endif()
  167. target_link_libraries(${lib_name} ${my_libs})
  168. # Update CTK_BASE_LIBRARIES
  169. set(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
  170. set(CTK_LIBRARIES ${CTK_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK libraries" FORCE)
  171. set(CTK_BASE_INCLUDE_DIRS ${CTK_BASE_INCLUDE_DIRS} ${my_includes} CACHE INTERNAL "CTK includes" FORCE)
  172. # Install headers
  173. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
  174. install(FILES
  175. ${headers}
  176. ${dynamicHeaders}
  177. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  178. )
  179. endmacro()