ctkMacroBuildLib.cmake 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #! \ingroup CMakeAPI
  25. MACRO(ctkMacroBuildLib)
  26. ctkMacroParseArguments(MY
  27. "NAME;EXPORT_DIRECTIVE;SRCS;MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES;LIBRARY_TYPE"
  28. ""
  29. ${ARGN}
  30. )
  31. # Sanity checks
  32. IF(NOT DEFINED MY_NAME)
  33. MESSAGE(FATAL_ERROR "NAME is mandatory")
  34. ENDIF()
  35. STRING(REGEX MATCH "^CTK.+" valid_library_name ${MY_NAME})
  36. IF(NOT valid_library_name)
  37. MESSAGE(FATAL_ERROR "CTK library name [${MY_NAME}] should start with 'CTK' uppercase !")
  38. ENDIF()
  39. IF(NOT DEFINED MY_EXPORT_DIRECTIVE)
  40. MESSAGE(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
  41. ENDIF()
  42. IF(NOT DEFINED MY_LIBRARY_TYPE)
  43. SET(MY_LIBRARY_TYPE "SHARED")
  44. ENDIF()
  45. # Define library name
  46. SET(lib_name ${MY_NAME})
  47. # --------------------------------------------------------------------------
  48. # Include dirs
  49. SET(my_includes
  50. ${CMAKE_CURRENT_SOURCE_DIR}
  51. ${CMAKE_CURRENT_BINARY_DIR}
  52. # with CMake >2.9, use QT4_MAKE_OUTPUT_FILE instead ?
  53. ${CMAKE_CURRENT_BINARY_DIR}/Resources/UI
  54. ${MY_INCLUDE_DIRECTORIES}
  55. )
  56. # Add the include directories from the library dependencies
  57. ctkFunctionGetIncludeDirs(my_includes ${lib_name})
  58. INCLUDE_DIRECTORIES(
  59. ${my_includes}
  60. )
  61. # Add the library directories from the external project
  62. ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
  63. LINK_DIRECTORIES(
  64. ${my_library_dirs}
  65. )
  66. SET(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
  67. SET(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
  68. STRING(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
  69. SET(MY_LIBNAME ${lib_name})
  70. CONFIGURE_FILE(
  71. ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
  72. ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
  73. )
  74. SET(dynamicHeaders
  75. "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
  76. # Make sure variable are cleared
  77. SET(MY_MOC_CPP)
  78. SET(MY_UI_CPP)
  79. SET(MY_QRC_SRCS)
  80. # Wrap
  81. IF(MY_MOC_SRCS)
  82. # this is a workaround for Visual Studio. The relative include paths in the generated
  83. # moc files can get very long and can't be resolved by the MSVC compiler.
  84. FOREACH(moc_src ${MY_MOC_SRCS})
  85. QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
  86. ENDFOREACH()
  87. ENDIF()
  88. QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
  89. IF(DEFINED MY_RESOURCES)
  90. QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
  91. ENDIF()
  92. SOURCE_GROUP("Resources" FILES
  93. ${MY_RESOURCES}
  94. ${MY_UI_FORMS}
  95. )
  96. SOURCE_GROUP("Generated" FILES
  97. ${MY_QRC_SRCS}
  98. ${MY_MOC_CPP}
  99. ${MY_UI_CPP}
  100. ${MOC_CPP_DECORATOR}
  101. )
  102. ADD_LIBRARY(${lib_name} ${MY_LIBRARY_TYPE}
  103. ${MY_SRCS}
  104. ${MY_MOC_CPP}
  105. ${MY_UI_CPP}
  106. ${MY_QRC_SRCS}
  107. )
  108. # Set labels associated with the target.
  109. SET_TARGET_PROPERTIES(${lib_name} PROPERTIES LABELS ${lib_name})
  110. # Apply user-defined properties to the library target.
  111. IF(CTK_LIBRARY_PROPERTIES AND MY_LIBRARY_TYPE STREQUAL "SHARED")
  112. SET_TARGET_PROPERTIES(${lib_name} PROPERTIES ${CTK_LIBRARY_PROPERTIES})
  113. ENDIF()
  114. SET_TARGET_PROPERTIES(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  115. # Library properties specific to STATIC build
  116. IF(MY_LIBRARY_TYPE STREQUAL "STATIC")
  117. IF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
  118. SET_TARGET_PROPERTIES(${lib_name} PROPERTIES COMPILE_FLAGS "-fPIC")
  119. ENDIF()
  120. ENDIF()
  121. # Install rules
  122. IF(MY_LIBRARY_TYPE STREQUAL "SHARED")
  123. INSTALL(TARGETS ${lib_name}
  124. RUNTIME DESTINATION ${CTK_INSTALL_BIN_DIR} COMPONENT Runtime
  125. LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Runtime
  126. ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
  127. ENDIF()
  128. SET(my_libs
  129. ${MY_TARGET_LIBRARIES}
  130. )
  131. IF(MINGW)
  132. LIST(APPEND my_libs ssp) # add stack smash protection lib
  133. ENDIF(MINGW)
  134. TARGET_LINK_LIBRARIES(${lib_name} ${my_libs})
  135. # Update CTK_BASE_LIBRARIES
  136. SET(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
  137. SET(CTK_LIBRARIES ${CTK_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK libraries" FORCE)
  138. SET(CTK_BASE_INCLUDE_DIRS ${CTK_BASE_INCLUDE_DIRS} ${my_includes} CACHE INTERNAL "CTK includes" FORCE)
  139. # Install headers
  140. FILE(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
  141. INSTALL(FILES
  142. ${headers}
  143. ${dynamicHeaders}
  144. DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
  145. )
  146. ENDMACRO()