2016-09-27 22:01:08 +03:00
|
|
|
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
# file Copyright.txt or https://cmake.org/licensing for details.
|
|
|
|
|
2013-10-15 19:17:36 +04:00
|
|
|
#.rst:
|
|
|
|
# UseSWIG
|
|
|
|
# -------
|
|
|
|
#
|
2014-04-03 10:28:10 +04:00
|
|
|
# Defines the following macros for use with SWIG:
|
2013-10-15 19:17:36 +04:00
|
|
|
#
|
|
|
|
# ::
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
2014-04-03 10:28:10 +04:00
|
|
|
# Source files properties on module files can be set before the invocation
|
|
|
|
# of the SWIG_ADD_MODULE macro to specify special behavior of SWIG.
|
|
|
|
#
|
|
|
|
# The source file property CPLUSPLUS calls SWIG in c++ mode, e.g.::
|
|
|
|
#
|
|
|
|
# set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
|
|
|
|
# swig_add_module(mymod python mymod.i)
|
|
|
|
#
|
|
|
|
# The source file property SWIG_FLAGS adds custom flags to the SWIG executable.
|
|
|
|
#
|
|
|
|
# The source-file property SWIG_MODULE_NAME have to be provided to specify the actual
|
|
|
|
# import name of the module in the target language if it cannot be scanned automatically
|
|
|
|
# from source or different from the module file basename.::
|
|
|
|
#
|
|
|
|
# set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)
|
|
|
|
#
|
|
|
|
# To get the name of the swig module target library, use: ${SWIG_MODULE_${name}_REAL_NAME}.
|
|
|
|
#
|
|
|
|
# Also some variables can be set to specify special behavior of SWIG.
|
|
|
|
#
|
|
|
|
# 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::
|
|
|
|
#
|
|
|
|
# set_source_files_properties( ${swig_generated_file_fullname}
|
|
|
|
# PROPERTIES COMPILE_FLAGS "-bla")
|
2004-06-28 19:14:40 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
set(SWIG_CXX_EXTENSION "cxx")
|
|
|
|
set(SWIG_EXTRA_LIBRARIES "")
|
2004-04-30 20:11:02 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
set(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
|
2004-04-30 20:11:02 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# For given swig module initialize variables associated with it
|
|
|
|
#
|
2012-08-13 21:47:32 +04:00
|
|
|
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}")
|
2014-06-25 21:52:52 +04:00
|
|
|
if (";${CMAKE_SWIG_FLAGS};" MATCHES ";-noproxy;")
|
2013-11-26 14:15:33 +04:00
|
|
|
set (SWIG_MODULE_${name}_NOPROXY TRUE)
|
|
|
|
endif ()
|
2015-03-09 23:32:43 +03:00
|
|
|
if("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xUNKNOWN")
|
2012-08-13 21:47:32 +04:00
|
|
|
message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
|
2015-03-09 23:32:43 +03:00
|
|
|
elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
|
2013-11-26 14:15:33 +04:00
|
|
|
# swig will produce a module.py containing an 'import _modulename' statement,
|
|
|
|
# which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
|
|
|
|
# unless the -noproxy flag is used
|
2012-08-13 21:47:32 +04:00
|
|
|
set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
|
2015-03-09 23:32:43 +03:00
|
|
|
elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPERL")
|
2012-08-13 21:47:32 +04:00
|
|
|
set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
|
2015-03-09 23:32:43 +03:00
|
|
|
elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xCSHARP")
|
2013-11-24 20:54:59 +04:00
|
|
|
# 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}")
|
2012-08-13 21:47:32 +04:00
|
|
|
endif()
|
2012-08-13 21:50:14 +04:00
|
|
|
endmacro()
|
2004-04-30 20:11:02 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# For a given language, input file, and output file, determine extra files that
|
2004-06-28 19:14:40 +04:00
|
|
|
# will be generated. This is internal swig macro.
|
2004-04-30 20:11:02 +04:00
|
|
|
#
|
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
|
|
|
|
set(${outfiles} "")
|
|
|
|
get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
|
2008-01-03 01:12:33 +03:00
|
|
|
${infile} SWIG_MODULE_NAME)
|
2014-03-20 13:03:56 +04:00
|
|
|
if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
|
2014-04-01 14:12:52 +04:00
|
|
|
|
|
|
|
# try to get module name from "%module foo" syntax
|
|
|
|
if ( EXISTS ${infile} )
|
|
|
|
file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
|
|
|
|
endif ()
|
|
|
|
if ( _MODULE_NAME )
|
|
|
|
string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
|
|
|
|
set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
|
|
|
|
|
|
|
|
else ()
|
|
|
|
# try to get module name from "%module (options=...) foo" syntax
|
|
|
|
if ( EXISTS ${infile} )
|
|
|
|
file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
|
|
|
|
endif ()
|
|
|
|
if ( _MODULE_NAME )
|
|
|
|
string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
|
|
|
|
set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
|
|
|
|
|
|
|
|
else ()
|
|
|
|
# fallback to file basename
|
|
|
|
get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename ${infile} NAME_WE)
|
|
|
|
endif ()
|
|
|
|
endif ()
|
|
|
|
|
2014-03-20 13:03:56 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
|
|
|
|
set(${outfiles} ${${outfiles}}
|
2008-01-03 01:12:33 +03:00
|
|
|
"${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
|
2012-08-13 21:50:14 +04:00
|
|
|
endforeach()
|
|
|
|
endmacro()
|
2004-04-30 20:11:02 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Take swig (*.i) file and add proper custom commands for it
|
|
|
|
#
|
2012-08-13 21:47:32 +04:00
|
|
|
macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
|
|
|
|
set(swig_full_infile ${infile})
|
|
|
|
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 "")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2013-11-23 15:51:44 +04:00
|
|
|
get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
|
2012-08-13 21:47:32 +04:00
|
|
|
|
2007-03-05 23:21:49 +03:00
|
|
|
# If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
|
2012-08-13 21:47:32 +04:00
|
|
|
if(CMAKE_SWIG_OUTDIR)
|
|
|
|
set(swig_outdir ${CMAKE_SWIG_OUTDIR})
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2015-09-05 00:33:37 +03:00
|
|
|
if (NOT SWIG_MODULE_${name}_NOPROXY)
|
|
|
|
SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
|
|
|
|
swig_extra_generated_files
|
|
|
|
"${swig_outdir}"
|
|
|
|
"${swig_source_file_fullname}")
|
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_generated_file_fullname
|
2013-11-23 15:51:44 +04:00
|
|
|
"${swig_outdir}/${swig_source_file_name_we}")
|
2005-02-11 22:18:45 +03:00
|
|
|
# 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
|
2016-07-28 01:41:13 +03:00
|
|
|
string(APPEND swig_generated_file_fullname
|
|
|
|
"${SWIG_MODULE_${name}_LANGUAGE}_wrap")
|
2004-04-30 20:11:02 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
if(swig_source_file_cplusplus)
|
2016-07-28 01:41:13 +03:00
|
|
|
string(APPEND swig_generated_file_fullname
|
|
|
|
".${SWIG_CXX_EXTENSION}")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2016-07-28 01:41:13 +03:00
|
|
|
string(APPEND swig_generated_file_fullname
|
|
|
|
".c")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2004-04-30 20:11:02 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
#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)
|
2013-11-23 11:57:04 +04:00
|
|
|
list(REMOVE_DUPLICATES cmake_include_directories)
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_include_dirs)
|
|
|
|
foreach(it ${cmake_include_directories})
|
|
|
|
set(swig_include_dirs ${swig_include_dirs} "-I${it}")
|
2012-08-13 21:50:14 +04:00
|
|
|
endforeach()
|
2004-04-30 20:11:02 +04:00
|
|
|
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_special_flags)
|
2005-04-04 23:51:48 +04:00
|
|
|
# default is c, so add c++ flag if it is c++
|
2012-08-13 21:47:32 +04:00
|
|
|
if(swig_source_file_cplusplus)
|
|
|
|
set(swig_special_flags ${swig_special_flags} "-c++")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_extra_flags)
|
|
|
|
if(SWIG_MODULE_${name}_EXTRA_FLAGS)
|
|
|
|
set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2012-08-13 21:47:32 +04:00
|
|
|
add_custom_command(
|
2007-03-05 23:21:49 +03:00
|
|
|
OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
|
2011-03-28 20:12:26 +04:00
|
|
|
# Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
|
2007-03-05 23:21:49 +03:00
|
|
|
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}"
|
2015-04-29 16:09:24 +03:00
|
|
|
MAIN_DEPENDENCY "${swig_source_file_fullname}"
|
|
|
|
DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
|
2012-08-13 21:42:58 +04:00
|
|
|
COMMENT "Swig source")
|
2012-08-13 21:47:32 +04:00
|
|
|
set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
|
2004-04-30 20:11:02 +04:00
|
|
|
PROPERTIES GENERATED 1)
|
2012-08-13 21:47:32 +04:00
|
|
|
set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
|
2012-08-13 21:50:14 +04:00
|
|
|
endmacro()
|
2004-04-30 20:11:02 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Create Swig module
|
|
|
|
#
|
2012-08-13 21:47:32 +04:00
|
|
|
macro(SWIG_ADD_MODULE name language)
|
2004-04-30 20:11:02 +04:00
|
|
|
SWIG_MODULE_INITIALIZE(${name} ${language})
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_dot_i_sources)
|
|
|
|
set(swig_other_sources)
|
|
|
|
foreach(it ${ARGN})
|
2014-04-11 20:17:46 +04:00
|
|
|
if(${it} MATCHES "\\.i$")
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_other_sources ${swig_other_sources} "${it}")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
|
|
|
endforeach()
|
2012-08-13 21:47:32 +04:00
|
|
|
|
|
|
|
set(swig_generated_sources)
|
|
|
|
foreach(it ${swig_dot_i_sources})
|
2004-04-30 20:11:02 +04:00
|
|
|
SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
|
2012-08-13 21:47:32 +04:00
|
|
|
set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
|
2012-08-13 21:50:14 +04:00
|
|
|
endforeach()
|
2012-08-13 21:47:32 +04:00
|
|
|
get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
|
|
|
|
set_directory_properties(PROPERTIES
|
2005-04-05 00:13:41 +04:00
|
|
|
ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
|
2012-08-13 21:47:32 +04:00
|
|
|
add_library(${SWIG_MODULE_${name}_REAL_NAME}
|
2004-04-30 20:11:02 +04:00
|
|
|
MODULE
|
|
|
|
${swig_generated_sources}
|
|
|
|
${swig_other_sources})
|
2014-03-20 12:57:38 +04:00
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES NO_SONAME ON)
|
2012-08-13 21:47:32 +04:00
|
|
|
string(TOLOWER "${language}" swig_lowercase_language)
|
2013-11-29 13:13:27 +04:00
|
|
|
if ("${swig_lowercase_language}" STREQUAL "octave")
|
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
|
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
|
2014-04-22 10:29:26 +04:00
|
|
|
elseif ("${swig_lowercase_language}" STREQUAL "go")
|
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
|
2013-11-29 13:13:27 +04:00
|
|
|
elseif ("${swig_lowercase_language}" STREQUAL "java")
|
2012-08-13 21:47:32 +04:00
|
|
|
if (APPLE)
|
2009-10-30 18:07:10 +03:00
|
|
|
# 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
|
2012-08-13 21:47:32 +04:00
|
|
|
set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif ()
|
2014-04-22 10:29:26 +04:00
|
|
|
elseif ("${swig_lowercase_language}" STREQUAL "lua")
|
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
|
2013-11-28 13:26:14 +04:00
|
|
|
elseif ("${swig_lowercase_language}" STREQUAL "python")
|
2009-10-30 18:07:10 +03:00
|
|
|
# this is only needed for the python case where a _modulename.so is generated
|
2012-08-13 21:47:32 +04:00
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
|
2009-10-30 18:07:10 +03:00
|
|
|
# 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>
|
2012-08-13 21:47:32 +04:00
|
|
|
if(WIN32 AND NOT CYGWIN)
|
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
2014-10-14 16:09:53 +04:00
|
|
|
elseif ("${swig_lowercase_language}" STREQUAL "r")
|
|
|
|
set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
|
2013-11-28 13:26:14 +04:00
|
|
|
elseif ("${swig_lowercase_language}" STREQUAL "ruby")
|
|
|
|
# In ruby you want:
|
|
|
|
# require 'LIBRARY'
|
|
|
|
# then ruby will look for a library whose name is platform dependent, namely
|
|
|
|
# MacOS : LIBRARY.bundle
|
|
|
|
# Windows: LIBRARY.dll
|
|
|
|
# Linux : LIBRARY.so
|
|
|
|
set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
|
|
|
|
if (APPLE)
|
|
|
|
set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".bundle")
|
|
|
|
endif ()
|
2012-08-13 21:50:14 +04:00
|
|
|
endif ()
|
|
|
|
endmacro()
|
2004-04-30 20:11:02 +04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Like TARGET_LINK_LIBRARIES but for swig modules
|
|
|
|
#
|
2012-08-13 21:47:32 +04:00
|
|
|
macro(SWIG_LINK_LIBRARIES name)
|
|
|
|
if(SWIG_MODULE_${name}_REAL_NAME)
|
|
|
|
target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
|
2012-08-13 21:50:14 +04:00
|
|
|
else()
|
2012-08-13 21:47:32 +04:00
|
|
|
message(SEND_ERROR "Cannot find Swig library \"${name}\".")
|
2012-08-13 21:50:14 +04:00
|
|
|
endif()
|
|
|
|
endmacro()
|
2005-09-08 19:38:14 +04:00
|
|
|
|