ctkMacroBuildLib.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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;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. # Add Qt include dirs and defines
  68. include(${QT_USE_FILE})
  69. # Add the library directories from the external project
  70. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  71. link_directories(
  72. ${my_library_dirs}
  73. )
  74. set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  75. set(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
  76. string(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  77. set(MY_LIBNAME ${lib_name})
  78. configure_file(
  79. ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
  80. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  81. )
  82. set(dynamicHeaders
  83. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  84. # Make sure variable are cleared
  85. set(MY_MOC_CPP)
  86. set(MY_UI_CPP)
  87. set(MY_QRC_SRCS)
  88. # Wrap
  89. if(MY_MOC_SRCS)
  90. # this is a workaround for Visual Studio. The relative include paths in the generated
  91. # moc files can get very long and can't be resolved by the MSVC compiler.
  92. foreach(moc_src ${MY_MOC_SRCS})
  93. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  94. endforeach()
  95. endif()
  96. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  97. if(DEFINED MY_RESOURCES)
  98. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  99. endif()
  100. source_group("Resources" FILES
  101. ${MY_RESOURCES}
  102. ${MY_UI_FORMS}
  103. )
  104. source_group("Generated" FILES
  105. ${MY_QRC_SRCS}
  106. ${MY_MOC_CPP}
  107. ${MY_UI_CPP}
  108. ${MOC_CPP_DECORATOR}
  109. )
  110. add_library(${lib_name} ${MY_LIBRARY_TYPE}
  111. ${MY_SRCS}
  112. ${MY_MOC_CPP}
  113. ${MY_UI_CPP}
  114. ${MY_QRC_SRCS}
  115. )
  116. # Set labels associated with the target.
  117. set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})
  118. # Apply user-defined properties to the library target.
  119. if(CTK_LIBRARY_PROPERTIES AND MY_LIBRARY_TYPE STREQUAL "SHARED")
  120. set_target_properties(${lib_name} PROPERTIES ${CTK_LIBRARY_PROPERTIES})
  121. endif()
  122. set_target_properties(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  123. # Library properties specific to STATIC build
  124. if(MY_LIBRARY_TYPE STREQUAL "STATIC")
  125. if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  126. set_target_properties(${lib_name} PROPERTIES COMPILE_FLAGS "-fPIC")
  127. endif()
  128. endif()
  129. # Install rules
  130. if(MY_LIBRARY_TYPE STREQUAL "SHARED")
  131. install(TARGETS ${lib_name} EXPORT CTKExports
  132. RUNTIME DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  133. LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
  134. ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  135. endif()
  136. set(my_libs
  137. ${MY_TARGET_LIBRARIES}
  138. )
  139. if(MINGW)
  140. list(APPEND my_libs ssp) # add stack smash protection lib
  141. endif()
  142. target_link_libraries(${lib_name} ${my_libs})
  143. # Update CTK_BASE_LIBRARIES
  144. set(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
  145. set(CTK_LIBRARIES ${CTK_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK libraries" FORCE)
  146. # Install headers
  147. file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
  148. install(FILES
  149. ${headers}
  150. ${dynamicHeaders}
  151. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  152. )
  153. endmacro()