CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkMacroBuildLib.cmake
Go to the documentation of this file.
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 #
22 # Depends on:
23 # CTK/CMake/ctkMacroParseArguments.cmake
24 #
25 
26 #! \brief Build a CTK library.
27 #!
28 #! \ingroup CMakeAPI
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"
32  "ENABLE_QTTESTING"
33  ${ARGN}
34  )
35 
36  # Keep parameter 'INCLUDE_DIRECTORIES' for backward compatiblity
37 
38  # Sanity checks
39  if(NOT DEFINED MY_NAME)
40  message(FATAL_ERROR "NAME is mandatory")
41  endif()
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 !")
45  endif()
46  if(NOT DEFINED MY_EXPORT_DIRECTIVE)
47  message(FATAL_ERROR "EXPORT_DIRECTIVE is mandatory")
48  endif()
49  if(NOT DEFINED MY_LIBRARY_TYPE)
50  set(MY_LIBRARY_TYPE "SHARED")
51  endif()
52 
53  # Define library name
54  set(lib_name ${MY_NAME})
55 
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.")
59  endif()
60 
61  # --------------------------------------------------------------------------
62  # Include dirs
63  set(my_includes
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
68  )
69 
70  # Add the include directories from the library dependencies
71  ctkFunctionGetIncludeDirs(my_includes ${lib_name})
72 
73  include_directories(
74  ${my_includes}
75  )
76 
77  if(CTK_QT_VERSION VERSION_LESS "5")
78  # Add Qt include dirs and defines
79  include(${QT_USE_FILE})
80  endif()
81 
82  # Add the library directories from the external project
83  ctkFunctionGetLibraryDirs(my_library_dirs ${lib_name})
84 
85  link_directories(
86  ${my_library_dirs}
87  )
88 
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})
93 
94  configure_file(
95  ${CTK_SOURCE_DIR}/Libs/ctkExport.h.in
96  ${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h
97  )
98  set(dynamicHeaders
99  "${dynamicHeaders};${CMAKE_CURRENT_BINARY_DIR}/${MY_EXPORT_HEADER_PREFIX}Export.h")
100 
101  # Make sure variable are cleared
102  set(MY_MOC_CPP)
103  set(MY_UI_CPP)
104  set(MY_QRC_SRCS)
105 
106  # Wrap
107  if(MY_MOC_SRCS)
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)
113  endforeach()
114  else()
115  foreach(moc_src ${MY_MOC_SRCS})
116  QT4_WRAP_CPP(MY_MOC_CPP ${moc_src} OPTIONS -f${moc_src})
117  endforeach()
118  endif()
119  endif()
120  if(MY_GENERATE_MOC_SRCS)
121  QT4_GENERATE_MOCS(${MY_GENERATE_MOC_SRCS})
122  endif()
123  if(CTK_QT_VERSION VERSION_GREATER "4")
124  if(Qt5Widgets_FOUND)
125  qt5_wrap_ui(MY_UI_CPP ${MY_UI_FORMS})
126  elseif(MY_UI_FORMS)
127  message(WARNING "Argument UI_FORMS ignored because Qt5Widgets module was not specified")
128  endif()
129  else()
130  QT4_WRAP_UI(MY_UI_CPP ${MY_UI_FORMS})
131  endif()
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})
135  else()
136  QT4_ADD_RESOURCES(MY_QRC_SRCS ${MY_RESOURCES})
137  endif()
138  endif()
139 
140  source_group("Resources" FILES
141  ${MY_RESOURCES}
142  ${MY_UI_FORMS}
143  )
144 
145  source_group("Generated" FILES
146  ${MY_QRC_SRCS}
147  ${MY_MOC_CPP}
148  ${MY_UI_CPP}
149  ${MOC_CPP_DECORATOR}
150  )
151 
152  add_library(${lib_name} ${MY_LIBRARY_TYPE}
153  ${MY_SRCS}
154  ${MY_MOC_CPP}
155  ${MY_UI_CPP}
156  ${MY_QRC_SRCS}
157  )
158 
159  # Set labels associated with the target.
160  set_target_properties(${lib_name} PROPERTIES LABELS ${lib_name})
161 
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})
165  endif()
166  set_target_properties(${lib_name} PROPERTIES CTK_LIB_TARGET_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
167 
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")
172  endif()
173  endif()
174 
175  # Install rules
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)
181  endif()
182 
183  set(my_libs
184  ${MY_TARGET_LIBRARIES}
185  )
186 
187  if(MINGW)
188  list(APPEND my_libs ssp) # add stack smash protection lib
189  endif()
190  target_link_libraries(${lib_name} ${my_libs})
191 
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)
195 
196  # Install headers
197  file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.tpp")
198  install(FILES
199  ${headers}
200  ${dynamicHeaders}
201  DESTINATION ${CTK_INSTALL_INCLUDE_DIR} COMPONENT Development
202  )
203 
204 endmacro()
205 
206