ctkMacroBuildLib.cmake 5.2 KB

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