UseSWIG.cmake 10.3 KB
Newer Older
1 2 3 4 5 6
#.rst:
# UseSWIG
# -------
#
# SWIG module for CMake
#
7
# Defines the following macros:
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#
# ::
#
#    SWIG_ADD_MODULE(name language [ files ])
#      - Define swig module with given name and specified language
#    SWIG_LINK_LIBRARIES(name [ libraries ])
#      - Link libraries to swig module
#
# All other macros are for internal use only.  To get the actual name of
# the swig module, use: ${SWIG_MODULE_${name}_REAL_NAME}.  Set Source
# files properties such as CPLUSPLUS and SWIG_FLAGS to specify special
# behavior of SWIG.  Also global CMAKE_SWIG_FLAGS can be used to add
# special flags to all swig calls.  Another special variable is
# CMAKE_SWIG_OUTDIR, it allows one to specify where to write all the
# swig generated module (swig -outdir option) The name-specific variable
# SWIG_MODULE_<name>_EXTRA_DEPS may be used to specify extra
# dependencies for the generated modules.  If the source file generated
# by swig need some special flag you can use
26
# set_source_files_properties( ${swig_generated_file_fullname}
27 28 29 30
#
# ::
#
#         PROPERTIES COMPILE_FLAGS "-bla")
31

32

33 34
#=============================================================================
# Copyright 2004-2009 Kitware, Inc.
35
# Copyright 2009 Mathieu Malaterre <mathieu.malaterre@gmail.com>
36 37 38 39 40 41 42 43
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
44
# (To distribute this file outside of CMake, substitute the full
45 46
#  License text for the above reference.)

47 48
set(SWIG_CXX_EXTENSION "cxx")
set(SWIG_EXTRA_LIBRARIES "")
49

50
set(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
51 52 53 54

#
# For given swig module initialize variables associated with it
#
55 56 57 58 59 60 61 62 63 64
macro(SWIG_MODULE_INITIALIZE name language)
  string(TOUPPER "${language}" swig_uppercase_language)
  string(TOLOWER "${language}" swig_lowercase_language)
  set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")

  set(SWIG_MODULE_${name}_REAL_NAME "${name}")
  if("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "UNKNOWN")
    message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PYTHON")
65
    # when swig is used without the -interface it will produce in the module.py
66
    # a 'import _modulename' statement, which implies having a corresponding
67
    # _modulename.so (*NIX), _modulename.pyd (Win32).
68 69 70
    set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PERL")
    set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
71 72 73 74
  elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "CSHARP")
    # This makes sure that the name used in the generated DllImport
    # matches the library name created by CMake
    set(SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport;${name}")
75
  endif()
76
endmacro()
77 78 79

#
# For a given language, input file, and output file, determine extra files that
80
# will be generated. This is internal swig macro.
81 82
#

83 84 85
macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  set(${outfiles} "")
  get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
Bill Hoffman's avatar
Bill Hoffman committed
86
    ${infile} SWIG_MODULE_NAME)
87 88
  if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
    get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${infile}" NAME_WE)
89
  endif()
90 91
  foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
    set(${outfiles} ${${outfiles}}
Bill Hoffman's avatar
Bill Hoffman committed
92
      "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
93 94
  endforeach()
endmacro()
95 96 97 98

#
# Take swig (*.i) file and add proper custom commands for it
#
99 100 101 102 103 104 105 106 107
macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  set(swig_full_infile ${infile})
  get_filename_component(swig_source_file_path "${infile}" PATH)
  get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
    set(swig_source_file_flags "")
108
  endif()
109 110 111
  set(swig_source_file_fullname "${infile}")
  if(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
    string(REGEX REPLACE
112 113 114
      "^${CMAKE_CURRENT_SOURCE_DIR}" ""
      swig_source_file_relative_path
      "${swig_source_file_path}")
115
  else()
116 117
    if(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
      string(REGEX REPLACE
118 119 120
        "^${CMAKE_CURRENT_BINARY_DIR}" ""
        swig_source_file_relative_path
        "${swig_source_file_path}")
121
      set(swig_source_file_generated 1)
122
    else()
123 124 125
      set(swig_source_file_relative_path "${swig_source_file_path}")
      if(swig_source_file_generated)
        set(swig_source_file_fullname "${CMAKE_CURRENT_BINARY_DIR}/${infile}")
126
      else()
127
        set(swig_source_file_fullname "${CMAKE_CURRENT_SOURCE_DIR}/${infile}")
128 129 130
      endif()
    endif()
  endif()
131 132

  set(swig_generated_file_fullname
133
    "${CMAKE_CURRENT_BINARY_DIR}")
134 135
  if(swig_source_file_relative_path)
    set(swig_generated_file_fullname
136
      "${swig_generated_file_fullname}/${swig_source_file_relative_path}")
137
  endif()
Ken Martin's avatar
Ken Martin committed
138
  # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
139 140
  if(CMAKE_SWIG_OUTDIR)
    set(swig_outdir ${CMAKE_SWIG_OUTDIR})
141
  else()
142
    set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
143
  endif()
144 145
  SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
    swig_extra_generated_files
Ken Martin's avatar
Ken Martin committed
146
    "${swig_outdir}"
Bill Hoffman's avatar
Bill Hoffman committed
147
    "${infile}")
148
  set(swig_generated_file_fullname
149
    "${swig_generated_file_fullname}/${swig_source_file_name_we}")
Bill Hoffman's avatar
Bill Hoffman committed
150 151
  # add the language into the name of the file (i.e. TCL_wrap)
  # this allows for the same .i file to be wrapped into different languages
152
  set(swig_generated_file_fullname
Bill Hoffman's avatar
Bill Hoffman committed
153
    "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
154

155 156
  if(swig_source_file_cplusplus)
    set(swig_generated_file_fullname
157
      "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
158
  else()
159
    set(swig_generated_file_fullname
160
      "${swig_generated_file_fullname}.c")
161
  endif()
162

163 164 165 166 167 168
  #message("Full path to source file: ${swig_source_file_fullname}")
  #message("Full path to the output file: ${swig_generated_file_fullname}")
  get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  set(swig_include_dirs)
  foreach(it ${cmake_include_directories})
    set(swig_include_dirs ${swig_include_dirs} "-I${it}")
169
  endforeach()
170

171
  set(swig_special_flags)
Bill Hoffman's avatar
Bill Hoffman committed
172
  # default is c, so add c++ flag if it is c++
173 174
  if(swig_source_file_cplusplus)
    set(swig_special_flags ${swig_special_flags} "-c++")
175
  endif()
176 177 178
  set(swig_extra_flags)
  if(SWIG_MODULE_${name}_EXTRA_FLAGS)
    set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
179
  endif()
180
  add_custom_command(
Ken Martin's avatar
Ken Martin committed
181
    OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
182 183
    # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
    COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
Ken Martin's avatar
Ken Martin committed
184 185 186 187 188 189 190 191 192 193 194 195
    COMMAND "${SWIG_EXECUTABLE}"
    ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
    ${swig_source_file_flags}
    ${CMAKE_SWIG_FLAGS}
    -outdir ${swig_outdir}
    ${swig_special_flags}
    ${swig_extra_flags}
    ${swig_include_dirs}
    -o "${swig_generated_file_fullname}"
    "${swig_source_file_fullname}"
    MAIN_DEPENDENCY "${swig_source_file_fullname}"
    DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
196
    COMMENT "Swig source")
197
  set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
198
    PROPERTIES GENERATED 1)
199
  set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
200
endmacro()
201 202 203 204

#
# Create Swig module
#
205
macro(SWIG_ADD_MODULE name language)
206
  SWIG_MODULE_INITIALIZE(${name} ${language})
207 208 209 210 211
  set(swig_dot_i_sources)
  set(swig_other_sources)
  foreach(it ${ARGN})
    if(${it} MATCHES ".*\\.i$")
      set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
212
    else()
213
      set(swig_other_sources ${swig_other_sources} "${it}")
214 215
    endif()
  endforeach()
216 217 218

  set(swig_generated_sources)
  foreach(it ${swig_dot_i_sources})
219
    SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
220
    set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
221
  endforeach()
222 223
  get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  set_directory_properties(PROPERTIES
Bill Hoffman's avatar
Bill Hoffman committed
224
    ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
225
  add_library(${SWIG_MODULE_${name}_REAL_NAME}
226 227 228
    MODULE
    ${swig_generated_sources}
    ${swig_other_sources})
229 230 231
  string(TOLOWER "${language}" swig_lowercase_language)
  if ("${swig_lowercase_language}" STREQUAL "java")
    if (APPLE)
232 233 234 235 236 237
        # In java you want:
        #      System.loadLibrary("LIBRARY");
        # then JNI will look for a library whose name is platform dependent, namely
        #   MacOS  : libLIBRARY.jnilib
        #   Windows: LIBRARY.dll
        #   Linux  : libLIBRARY.so
238
        set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
239 240
      endif ()
  endif ()
241
  if ("${swig_lowercase_language}" STREQUAL "python")
242
    # this is only needed for the python case where a _modulename.so is generated
243
    set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
244 245 246 247 248 249 250 251
    # Python extension modules on Windows must have the extension ".pyd"
    # instead of ".dll" as of Python 2.5.  Older python versions do support
    # this suffix.
    # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
    # <quote>
    # Windows: .dll is no longer supported as a filename extension for extension modules.
    # .pyd is now the only filename extension that will be searched for.
    # </quote>
252 253
    if(WIN32 AND NOT CYGWIN)
      set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
254 255 256
    endif()
  endif ()
endmacro()
257 258 259 260

#
# Like TARGET_LINK_LIBRARIES but for swig modules
#
261 262 263
macro(SWIG_LINK_LIBRARIES name)
  if(SWIG_MODULE_${name}_REAL_NAME)
    target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
264
  else()
265
    message(SEND_ERROR "Cannot find Swig library \"${name}\".")
266 267
  endif()
endmacro()
268