UseSWIG.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #.rst:
  4. # UseSWIG
  5. # -------
  6. #
  7. # Defines the following macros for use with SWIG:
  8. #
  9. # ::
  10. #
  11. # SWIG_ADD_LIBRARY(<name>
  12. # [TYPE <SHARED|MODULE|STATIC|USE_BUILD_SHARED_LIBS>]
  13. # LANGUAGE <language>
  14. # SOURCES <file>...
  15. # )
  16. # - Define swig module with given name and specified language
  17. # SWIG_LINK_LIBRARIES(name [ libraries ])
  18. # - Link libraries to swig module
  19. #
  20. # Source files properties on module files can be set before the invocation
  21. # of the SWIG_ADD_LIBRARY macro to specify special behavior of SWIG.
  22. #
  23. # The source file property CPLUSPLUS calls SWIG in c++ mode, e.g.::
  24. #
  25. # set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
  26. # swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  27. #
  28. # The source file property SWIG_FLAGS adds custom flags to the SWIG executable.
  29. #
  30. # The source-file property SWIG_MODULE_NAME have to be provided to specify the actual
  31. # import name of the module in the target language if it cannot be scanned automatically
  32. # from source or different from the module file basename.::
  33. #
  34. # set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)
  35. #
  36. # To get the name of the swig module target library, use: ${SWIG_MODULE_${name}_REAL_NAME}.
  37. #
  38. # Also some variables can be set to specify special behavior of SWIG.
  39. #
  40. # CMAKE_SWIG_FLAGS can be used to add special flags to all swig calls.
  41. #
  42. # CMAKE_SWIG_OUTDIR allows one to specify where to write
  43. # the language specific files (swig -outdir option).
  44. #
  45. # SWIG_OUTFILE_DIR allows one to specify where to write the output file
  46. # (swig -o option). If not specified, CMAKE_SWIG_OUTDIR is used.
  47. #
  48. # The name-specific variable SWIG_MODULE_<name>_EXTRA_DEPS may be used to specify extra
  49. # dependencies for the generated modules.
  50. #
  51. # If the source file generated by swig need some special flag you can use::
  52. #
  53. # set_source_files_properties( ${swig_generated_file_fullname}
  54. # PROPERTIES COMPILE_FLAGS "-bla")
  55. set(SWIG_CXX_EXTENSION "cxx")
  56. set(SWIG_EXTRA_LIBRARIES "")
  57. set(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
  58. #
  59. # For given swig module initialize variables associated with it
  60. #
  61. macro(SWIG_MODULE_INITIALIZE name language)
  62. string(TOUPPER "${language}" swig_uppercase_language)
  63. string(TOLOWER "${language}" swig_lowercase_language)
  64. set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  65. set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  66. set(SWIG_MODULE_${name}_REAL_NAME "${name}")
  67. if (";${CMAKE_SWIG_FLAGS};" MATCHES ";-noproxy;")
  68. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  69. endif ()
  70. if("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xUNKNOWN")
  71. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  72. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  73. # swig will produce a module.py containing an 'import _modulename' statement,
  74. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  75. # unless the -noproxy flag is used
  76. set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  77. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPERL")
  78. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  79. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xCSHARP")
  80. # This makes sure that the name used in the generated DllImport
  81. # matches the library name created by CMake
  82. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport;${name}")
  83. endif()
  84. endmacro()
  85. #
  86. # For a given language, input file, and output file, determine extra files that
  87. # will be generated. This is internal swig macro.
  88. #
  89. macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  90. set(${outfiles} "")
  91. get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
  92. ${infile} SWIG_MODULE_NAME)
  93. if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  94. # try to get module name from "%module foo" syntax
  95. if ( EXISTS ${infile} )
  96. file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
  97. endif ()
  98. if ( _MODULE_NAME )
  99. string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
  100. set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
  101. else ()
  102. # try to get module name from "%module (options=...) foo" syntax
  103. if ( EXISTS ${infile} )
  104. file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
  105. endif ()
  106. if ( _MODULE_NAME )
  107. string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
  108. set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
  109. else ()
  110. # fallback to file basename
  111. get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename ${infile} NAME_WE)
  112. endif ()
  113. endif ()
  114. endif()
  115. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
  116. set(${outfiles} ${${outfiles}}
  117. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
  118. endforeach()
  119. endmacro()
  120. #
  121. # Take swig (*.i) file and add proper custom commands for it
  122. #
  123. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  124. set(swig_full_infile ${infile})
  125. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  126. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  127. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  128. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  129. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  130. set(swig_source_file_flags "")
  131. endif()
  132. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  133. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  134. if(CMAKE_SWIG_OUTDIR)
  135. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  136. else()
  137. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  138. endif()
  139. if(SWIG_OUTFILE_DIR)
  140. set(swig_outfile_dir ${SWIG_OUTFILE_DIR})
  141. else()
  142. set(swig_outfile_dir ${swig_outdir})
  143. endif()
  144. if (NOT SWIG_MODULE_${name}_NOPROXY)
  145. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  146. swig_extra_generated_files
  147. "${swig_outdir}"
  148. "${swig_source_file_fullname}")
  149. endif()
  150. set(swig_generated_file_fullname
  151. "${swig_outfile_dir}/${swig_source_file_name_we}")
  152. # add the language into the name of the file (i.e. TCL_wrap)
  153. # this allows for the same .i file to be wrapped into different languages
  154. string(APPEND swig_generated_file_fullname
  155. "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  156. if(swig_source_file_cplusplus)
  157. string(APPEND swig_generated_file_fullname
  158. ".${SWIG_CXX_EXTENSION}")
  159. else()
  160. string(APPEND swig_generated_file_fullname
  161. ".c")
  162. endif()
  163. #message("Full path to source file: ${swig_source_file_fullname}")
  164. #message("Full path to the output file: ${swig_generated_file_fullname}")
  165. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  166. list(REMOVE_DUPLICATES cmake_include_directories)
  167. set(swig_include_dirs)
  168. foreach(it ${cmake_include_directories})
  169. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  170. endforeach()
  171. set(swig_special_flags)
  172. # default is c, so add c++ flag if it is c++
  173. if(swig_source_file_cplusplus)
  174. set(swig_special_flags ${swig_special_flags} "-c++")
  175. endif()
  176. set(swig_extra_flags)
  177. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  178. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  179. endif()
  180. add_custom_command(
  181. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  182. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  183. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  184. COMMAND "${SWIG_EXECUTABLE}"
  185. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  186. ${swig_source_file_flags}
  187. ${CMAKE_SWIG_FLAGS}
  188. -outdir ${swig_outdir}
  189. ${swig_special_flags}
  190. ${swig_extra_flags}
  191. ${swig_include_dirs}
  192. -o "${swig_generated_file_fullname}"
  193. "${swig_source_file_fullname}"
  194. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  195. 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. # Create Swig module
  203. #
  204. macro(SWIG_ADD_MODULE name language)
  205. message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  206. swig_add_library(${name}
  207. LANGUAGE ${language}
  208. TYPE MODULE
  209. SOURCES ${ARGN})
  210. endmacro()
  211. macro(SWIG_ADD_LIBRARY name)
  212. include(CMakeParseArguments)
  213. set(options "")
  214. set(oneValueArgs LANGUAGE
  215. TYPE)
  216. set(multiValueArgs SOURCES)
  217. cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  218. if(NOT DEFINED _SAM_LANGUAGE)
  219. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  220. endif()
  221. if(NOT DEFINED _SAM_SOURCES)
  222. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  223. endif()
  224. if(NOT DEFINED _SAM_TYPE)
  225. set(_SAM_TYPE MODULE)
  226. elseif("${_SAM_TYPE}" STREQUAL "USE_BUILD_SHARED_LIBS")
  227. unset(_SAM_TYPE)
  228. endif()
  229. swig_module_initialize(${name} ${_SAM_LANGUAGE})
  230. set(swig_dot_i_sources)
  231. set(swig_other_sources)
  232. foreach(it ${_SAM_SOURCES})
  233. if(${it} MATCHES "\\.i$")
  234. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  235. else()
  236. set(swig_other_sources ${swig_other_sources} "${it}")
  237. endif()
  238. endforeach()
  239. set(swig_generated_sources)
  240. foreach(it ${swig_dot_i_sources})
  241. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  242. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  243. endforeach()
  244. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  245. set_directory_properties(PROPERTIES
  246. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  247. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  248. ${_SAM_TYPE}
  249. ${swig_generated_sources}
  250. ${swig_other_sources})
  251. if("${_SAM_TYPE}" STREQUAL "MODULE")
  252. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES NO_SONAME ON)
  253. endif()
  254. string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
  255. if ("${swig_lowercase_language}" STREQUAL "octave")
  256. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  257. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
  258. elseif ("${swig_lowercase_language}" STREQUAL "go")
  259. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  260. elseif ("${swig_lowercase_language}" STREQUAL "java")
  261. if (APPLE)
  262. # In java you want:
  263. # System.loadLibrary("LIBRARY");
  264. # then JNI will look for a library whose name is platform dependent, namely
  265. # MacOS : libLIBRARY.jnilib
  266. # Windows: LIBRARY.dll
  267. # Linux : libLIBRARY.so
  268. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  269. endif ()
  270. elseif ("${swig_lowercase_language}" STREQUAL "lua")
  271. if("${_SAM_TYPE}" STREQUAL "MODULE")
  272. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  273. endif()
  274. elseif ("${swig_lowercase_language}" STREQUAL "python")
  275. # this is only needed for the python case where a _modulename.so is generated
  276. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  277. # Python extension modules on Windows must have the extension ".pyd"
  278. # instead of ".dll" as of Python 2.5. Older python versions do support
  279. # this suffix.
  280. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  281. # <quote>
  282. # Windows: .dll is no longer supported as a filename extension for extension modules.
  283. # .pyd is now the only filename extension that will be searched for.
  284. # </quote>
  285. if(WIN32 AND NOT CYGWIN)
  286. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  287. endif()
  288. elseif ("${swig_lowercase_language}" STREQUAL "r")
  289. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  290. elseif ("${swig_lowercase_language}" STREQUAL "ruby")
  291. # In ruby you want:
  292. # require 'LIBRARY'
  293. # then ruby will look for a library whose name is platform dependent, namely
  294. # MacOS : LIBRARY.bundle
  295. # Windows: LIBRARY.dll
  296. # Linux : LIBRARY.so
  297. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  298. if (APPLE)
  299. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".bundle")
  300. endif ()
  301. endif ()
  302. endmacro()
  303. #
  304. # Like TARGET_LINK_LIBRARIES but for swig modules
  305. #
  306. macro(SWIG_LINK_LIBRARIES name)
  307. if(SWIG_MODULE_${name}_REAL_NAME)
  308. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  309. else()
  310. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  311. endif()
  312. endmacro()