CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkMacroBuildLibWrapper.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 
27 #! When CTK is built as shared library, the following macro builds a python module
28 #! associated with the generated PythonQt wrappers. When loaded, it will
29 #! dynamically loads both the (1) generated decorators and the (2) hand written one.
30 #! On the other hand, when CTK is built statically, it creates a
31 #! static library providing a initialization function that will allow to load
32 #! both (1) and (2).
33 
34 #! \ingroup CMakeAPI
35 macro(ctkMacroBuildLibWrapper)
36  ctkMacroParseArguments(MY
37  "NAMESPACE;TARGET;SRCS;WRAPPER_LIBRARY_TYPE;ARCHIVE_OUTPUT_DIRECTORY;LIBRARY_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY;INSTALL_BIN_DIR;INSTALL_LIB_DIR"
38  "NO_INSTALL"
39  ${ARGN}
40  )
41 
42  # Sanity checks
43  if(NOT DEFINED MY_TARGET)
44  message(FATAL_ERROR "NAME is mandatory")
45  endif()
46  if(NOT DEFINED MY_WRAPPER_LIBRARY_TYPE OR "${MY_WRAPPER_LIBRARY_TYPE}" STREQUAL "SHARED")
47  set(MY_WRAPPER_LIBRARY_TYPE "MODULE")
48  endif()
49 
50  if(NOT DEFINED MY_NAMESPACE)
51  set(MY_NAMESPACE "org.commontk")
52  endif()
53  foreach(type RUNTIME LIBRARY ARCHIVE)
54  if(NOT DEFINED MY_${type}_OUTPUT_DIRECTORY)
55  set(MY_${type}_OUTPUT_DIRECTORY ${CMAKE_${type}_OUTPUT_DIRECTORY})
56  endif()
57  endforeach()
58  if(NOT DEFINED MY_INSTALL_BIN_DIR)
59  set(MY_INSTALL_BIN_DIR ${CTK_INSTALL_BIN_DIR})
60  endif()
61  if(NOT DEFINED MY_INSTALL_LIB_DIR)
62  set(MY_INSTALL_LIB_DIR ${CTK_INSTALL_LIB_DIR})
63  endif()
64 
65  # Define library name
66  set(lib_name ${MY_TARGET})
67 
68  # Include dirs
69  set(my_includes
70  ${CMAKE_CURRENT_SOURCE_DIR}
71  ${CMAKE_CURRENT_BINARY_DIR}
72  )
73 
74  # Since the PythonQt decorator depends on PythonQt, Python and VTK, let's link against
75  # these ones to avoid complaints of MSVC
76  # Note: "LINK_DIRECTORIES" has to be invoked before "ADD_LIBRARY"
77  set(my_EXTRA_PYTHON_LIBRARIES ${PYTHON_LIBRARY} ${PYTHONQT_LIBRARIES})
78 
79  # Does a header having the expected filename exists ?
80  string(REGEX REPLACE "^CTK" "ctk" lib_name_lc_ctk ${lib_name})
81  set(DECORATOR_HEADER ${lib_name_lc_ctk}PythonQtDecorators.h)
82  set(HAS_DECORATOR FALSE)
83 
84  set(_msg "Looking for decorator header ${DECORATOR_HEADER}")
85  message(STATUS "${_msg}")
86  set(_status "Not found")
87  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER})
88  set(HAS_DECORATOR TRUE)
89  set(DECORATOR_HEADER ${DECORATOR_HEADER})
90  set_source_files_properties(${DECORATOR_HEADER} WRAP_EXCLUDE)
91  set(_status "Found")
92  endif()
93  message(STATUS "${_msg} - ${_status}")
94  #message("path/to/DECORATOR_HEADER:${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER}")
95 
96  set(KIT_PYTHONQT_SRCS) # Clear variable
97  ctkMacroWrapPythonQt(${MY_NAMESPACE} ${lib_name}
98  KIT_PYTHONQT_SRCS "${MY_SRCS}" FALSE ${HAS_DECORATOR})
99  if(HAS_DECORATOR)
100  list(APPEND KIT_PYTHONQT_SRCS ${DECORATOR_HEADER})
101  if (CTK_QT_VERSION VERSION_GREATER "4")
102  qt5_wrap_cpp(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
103  else()
104  QT4_WRAP_CPP(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
105  endif()
106  endif()
107  add_library(${lib_name}PythonQt ${MY_WRAPPER_LIBRARY_TYPE} ${KIT_PYTHONQT_SRCS})
108  target_link_libraries(${lib_name}PythonQt ${lib_name} ${my_EXTRA_PYTHON_LIBRARIES})
109  if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "STATIC")
110  if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit
111  set_target_properties(${lib_name}PythonQt PROPERTIES COMPILE_FLAGS "-fPIC")
112  endif()
113  endif()
114  if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
115  # Make sure that no prefix is set on the library
116  set_target_properties(${lib_name}PythonQt PROPERTIES PREFIX "")
117  # Python extension modules on Windows must have the extension ".pyd"
118  # instead of ".dll" as of Python 2.5. Older python versions do support
119  # this suffix.
120  # See http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
121  if(WIN32 AND NOT CYGWIN)
122  set_target_properties(${lib_name}PythonQt PROPERTIES SUFFIX ".pyd")
123  endif()
124  endif()
125  set_target_properties(${lib_name}PythonQt PROPERTIES
126  RUNTIME_OUTPUT_DIRECTORY "${MY_RUNTIME_OUTPUT_DIRECTORY}"
127  LIBRARY_OUTPUT_DIRECTORY "${MY_LIBRARY_OUTPUT_DIRECTORY}"
128  ARCHIVE_OUTPUT_DIRECTORY "${MY_ARCHIVE_OUTPUT_DIRECTORY}"
129  )
130 
131  # Set labels associated with the target.
132  set_target_properties(${lib_name}PythonQt PROPERTIES LABELS ${lib_name})
133 
134  # Update list of libraries wrapped with PythonQt
135  set(CTK_WRAPPED_LIBRARIES_PYTHONQT
136  ${CTK_WRAPPED_LIBRARIES_PYTHONQT} ${lib_name}
137  CACHE INTERNAL "CTK libraries wrapped using PythonQt" FORCE)
138 
139  # Install rules
140  if(NOT MY_NO_INSTALL AND MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
141  install(TARGETS ${lib_name}PythonQt
142  RUNTIME DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
143  LIBRARY DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
144  ARCHIVE DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT Development)
145  endif()
146 
147 endmacro()
148 
149