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  if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER})
84  set(HAS_DECORATOR TRUE)
85  set(DECORATOR_HEADER ${DECORATOR_HEADER})
86  set_source_files_properties(${DECORATOR_HEADER} WRAP_EXCLUDE)
87  endif()
88  #message("HAS_DECORATOR:${HAS_DECORATOR}")
89  #message("path/to/DECORATOR_HEADER:${CMAKE_CURRENT_SOURCE_DIR}/${DECORATOR_HEADER}")
90 
91  set(KIT_PYTHONQT_SRCS) # Clear variable
92  ctkMacroWrapPythonQt(${MY_NAMESPACE} ${lib_name}
93  KIT_PYTHONQT_SRCS "${MY_SRCS}" FALSE ${HAS_DECORATOR})
94  if(HAS_DECORATOR)
95  list(APPEND KIT_PYTHONQT_SRCS ${DECORATOR_HEADER})
96  if (CTK_QT_VERSION VERSION_GREATER "4")
97  qt5_wrap_cpp(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
98  else()
99  QT4_WRAP_CPP(KIT_PYTHONQT_SRCS ${DECORATOR_HEADER} OPTIONS -f${DECORATOR_HEADER})
100  endif()
101  endif()
102  add_library(${lib_name}PythonQt ${MY_WRAPPER_LIBRARY_TYPE} ${KIT_PYTHONQT_SRCS})
103  target_link_libraries(${lib_name}PythonQt ${lib_name} ${my_EXTRA_PYTHON_LIBRARIES})
104  if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "STATIC")
105  if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit
106  set_target_properties(${lib_name}PythonQt PROPERTIES COMPILE_FLAGS "-fPIC")
107  endif()
108  endif()
109  if(MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
110  # Make sure that no prefix is set on the library
111  set_target_properties(${lib_name}PythonQt PROPERTIES PREFIX "")
112  # Python extension modules on Windows must have the extension ".pyd"
113  # instead of ".dll" as of Python 2.5. Older python versions do support
114  # this suffix.
115  # See http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll
116  if(WIN32 AND NOT CYGWIN)
117  set_target_properties(${lib_name}PythonQt PROPERTIES SUFFIX ".pyd")
118  endif()
119  endif()
120  set_target_properties(${lib_name}PythonQt PROPERTIES
121  RUNTIME_OUTPUT_DIRECTORY "${MY_RUNTIME_OUTPUT_DIRECTORY}"
122  LIBRARY_OUTPUT_DIRECTORY "${MY_LIBRARY_OUTPUT_DIRECTORY}"
123  ARCHIVE_OUTPUT_DIRECTORY "${MY_ARCHIVE_OUTPUT_DIRECTORY}"
124  )
125 
126  # Set labels associated with the target.
127  set_target_properties(${lib_name}PythonQt PROPERTIES LABELS ${lib_name})
128 
129  # Update list of libraries wrapped with PythonQt
130  set(CTK_WRAPPED_LIBRARIES_PYTHONQT
131  ${CTK_WRAPPED_LIBRARIES_PYTHONQT} ${lib_name}
132  CACHE INTERNAL "CTK libraries wrapped using PythonQt" FORCE)
133 
134  # Install rules
135  if(NOT MY_NO_INSTALL AND MY_WRAPPER_LIBRARY_TYPE STREQUAL "MODULE")
136  install(TARGETS ${lib_name}PythonQt
137  RUNTIME DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
138  LIBRARY DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT RuntimePlugins
139  ARCHIVE DESTINATION ${MY_INSTALL_LIB_DIR} COMPONENT Development)
140  endif()
141 
142 endmacro()
143 
144