1 ###########################################################################
5 # Copyright (c) Kitware Inc.
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
11 # http://www.apache.org/licenses/LICENSE-2.0.txt
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.
19 ###########################################################################
23 # CTK/CMake/ctkMacroParseArguments.cmake
26 #! \brief Build a CTK library.
29 macro(ctkMacroBuildLib)
30 ctkMacroParseArguments(MY
31 "NAME;EXPORT_DIRECTIVE;SRCS;MOC_SRCS;GENERATE_MOC_SRCS;UI_FORMS;INCLUDE_DIRECTORIES;TARGET_LIBRARIES;RESOURCES;LIBRARY_TYPE"
36 # Keep parameter 'INCLUDE_DIRECTORIES' for backward compatiblity
39 if(NOT DEFINED MY_NAME)
40 message(FATAL_ERROR "NAME is mandatory")
42 string(REGEX MATCH "^CTK.+" valid_library_name ${MY_NAME})
43 if(NOT valid_library_name)
44 message(FATAL_ERROR "CTK library name [${MY_NAME}] should start with 'CTK' uppercase !")
46 if(NOT DEFINED MY_EXPORT_DIRECTIVE)
47 message(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
49 if(NOT DEFINED MY_LIBRARY_TYPE)
50 set(MY_LIBRARY_TYPE "SHARED")
54 set(lib_name ${MY_NAME})
56 # Library target names must not contain a '_' (reserved for plug-in target names)
57 if(lib_name MATCHES _)
58 message(FATAL_ERROR "The library name ${lib_name} must not contain a '_' character.")
61 # --------------------------------------------------------------------------
64 ${CMAKE_CURRENT_SOURCE_DIR}
65 ${CMAKE_CURRENT_BINARY_DIR}
66 # with CMake >2.9, use QT4_MAKE_OUTPUT_FILE instead ?
67 ${CMAKE_CURRENT_BINARY_DIR}/Resources/UI
70 # Add the include directories from the library dependencies
71 ctkFunctionGetIncludeDirs(my_includes ${lib_name})
77 if(CTK_QT_VERSION VERSION_LESS "5")
78 # Add Qt include dirs and defines
79 include(${QT_USE_FILE})
82 # Add the library directories from the external project
83 ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
89 set(MY_LIBRARY_EXPORT_DIRECTIVE ${MY_EXPORT_DIRECTIVE})
90 set(MY_EXPORT_HEADER_PREFIX ${MY_NAME})
91 string(REGEX REPLACE "^CTK" "ctk" MY_EXPORT_HEADER_PREFIX ${MY_EXPORT_HEADER_PREFIX})
92 set(MY_LIBNAME ${lib_name})
95 ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
96 ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
99 "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
101 # Make sure variable are cleared
108 # this is a workaround for Visual Studio. The relative include paths in the generated
109 # moc files can get very long and can't be resolved by the MSVC compiler.
110 if(CTK_QT_VERSION VERSION_GREATER "4")
111 foreach(moc_src ${MY_MOC_SRCS})
112 qt5_wrap_cpp(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src} OPTIONS -DHAVE_QT5)
115 foreach(moc_src ${MY_MOC_SRCS})
116 QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
120 if(MY_GENERATE_MOC_SRCS)
121 QT4_GENERATE_MOCS(${MY_GENERATE_MOC_SRCS})
123 if(CTK_QT_VERSION VERSION_GREATER "4")
125 qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
127 message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
130 QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
132 if(DEFINED MY_RESOURCES AND NOT MY_RESOURCES STREQUAL "")
133 if(CTK_QT_VERSION VERSION_GREATER "4")
134 qt5_add_resources(MY_QRC_SRCS ${MY_RESOURCES})
136 QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
140 source_group("Resources" FILES
145 source_group("Generated" FILES
152 add_library(${lib_name} ${MY_LIBRARY_TYPE}
159 # Set labels associated with the target.
160 set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})
162 # Apply user-defined properties to the library target.
163 if(CTK_LIBRARY_PROPERTIES AND MY_LIBRARY_TYPE STREQUAL "SHARED")
164 set_target_properties(${lib_name} PROPERTIES ${CTK_LIBRARY_PROPERTIES})
166 set_target_properties(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
168 # Library properties specific to STATIC build
169 if(MY_LIBRARY_TYPE STREQUAL "STATIC")
170 if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit
171 set_target_properties(${lib_name} PROPERTIES COMPILE_FLAGS "-fPIC")
176 if(MY_LIBRARY_TYPE STREQUAL "SHARED")
177 install(TARGETS ${lib_name} EXPORT CTKExports
178 RUNTIME DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
179 LIBRARY DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
180 ARCHIVE DESTINATION ${CTK_INSTALL_LIB_DIR} COMPONENT Development)
184 ${MY_TARGET_LIBRARIES}
188 list(APPEND my_libs ssp) # add stack smash protection lib
190 target_link_libraries(${lib_name} ${my_libs})
192 # Update CTK_BASE_LIBRARIES
193 set(CTK_BASE_LIBRARIES ${my_libs} ${lib_name} CACHE INTERNAL "CTK base libraries" FORCE)
194 set(CTK_LIBRARIES ${CTK_LIBRARIES} ${lib_name} CACHE INTERNAL "CTK libraries" FORCE)
197 file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
201 DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development