UseSWIG.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_EXTENSIONS ".py")
  58. set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
  59. #
  60. # For given swig module initialize variables associated with it
  61. #
  62. macro(SWIG_MODULE_INITIALIZE name language)
  63. string(TOUPPER "${language}" swig_uppercase_language)
  64. string(TOLOWER "${language}" swig_lowercase_language)
  65. set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  66. set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  67. set(SWIG_MODULE_${name}_REAL_NAME "${name}")
  68. if (";${CMAKE_SWIG_FLAGS};" MATCHES ";-noproxy;")
  69. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  70. endif ()
  71. if("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xUNKNOWN")
  72. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  73. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  74. # swig will produce a module.py containing an 'import _modulename' statement,
  75. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  76. # unless the -noproxy flag is used
  77. set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  78. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xPERL")
  79. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  80. elseif("x${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "xCSHARP")
  81. # This makes sure that the name used in the generated DllImport
  82. # matches the library name created by CMake
  83. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport;${name}")
  84. endif()
  85. endmacro()
  86. #
  87. # For a given language, input file, and output file, determine extra files that
  88. # will be generated. This is internal swig macro.
  89. #
  90. macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  91. set(${outfiles} "")
  92. get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
  93. ${infile} SWIG_MODULE_NAME)
  94. if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  95. # try to get module name from "%module foo" syntax
  96. if ( EXISTS ${infile} )
  97. file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
  98. endif ()
  99. if ( _MODULE_NAME )
  100. string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
  101. set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
  102. else ()
  103. # try to get module name from "%module (options=...) foo" syntax
  104. if ( EXISTS ${infile} )
  105. file ( STRINGS ${infile} _MODULE_NAME REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
  106. endif ()
  107. if ( _MODULE_NAME )
  108. string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" _MODULE_NAME "${_MODULE_NAME}" )
  109. set(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${_MODULE_NAME}")
  110. else ()
  111. # fallback to file basename
  112. get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename ${infile} NAME_WE)
  113. endif ()
  114. endif ()
  115. endif()
  116. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSIONS})
  117. set(extra_file "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}${it}")
  118. list(APPEND ${outfiles} ${extra_file})
  119. # Treat extra outputs as plain files regardless of language.
  120. set_property(SOURCE "${extra_file}" PROPERTY LANGUAGE "")
  121. endforeach()
  122. endmacro()
  123. #
  124. # Take swig (*.i) file and add proper custom commands for it
  125. #
  126. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  127. set(swig_full_infile ${infile})
  128. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  129. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  130. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  131. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  132. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  133. set(swig_source_file_flags "")
  134. endif()
  135. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  136. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  137. if(CMAKE_SWIG_OUTDIR)
  138. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  139. else()
  140. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  141. endif()
  142. if(SWIG_OUTFILE_DIR)
  143. set(swig_outfile_dir ${SWIG_OUTFILE_DIR})
  144. else()
  145. set(swig_outfile_dir ${swig_outdir})
  146. endif()
  147. if (NOT SWIG_MODULE_${name}_NOPROXY)
  148. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  149. swig_extra_generated_files
  150. "${swig_outdir}"
  151. "${swig_source_file_fullname}")
  152. endif()
  153. set(swig_generated_file_fullname
  154. "${swig_outfile_dir}/${swig_source_file_name_we}")
  155. # add the language into the name of the file (i.e. TCL_wrap)
  156. # this allows for the same .i file to be wrapped into different languages
  157. string(APPEND swig_generated_file_fullname
  158. "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  159. if(swig_source_file_cplusplus)
  160. string(APPEND swig_generated_file_fullname
  161. ".${SWIG_CXX_EXTENSION}")
  162. else()
  163. string(APPEND swig_generated_file_fullname
  164. ".c")
  165. endif()
  166. #message("Full path to source file: ${swig_source_file_fullname}")
  167. #message("Full path to the output file: ${swig_generated_file_fullname}")
  168. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  169. list(REMOVE_DUPLICATES cmake_include_directories)
  170. set(swig_include_dirs)
  171. foreach(it ${cmake_include_directories})
  172. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  173. endforeach()
  174. set(swig_special_flags)
  175. # default is c, so add c++ flag if it is c++
  176. if(swig_source_file_cplusplus)
  177. set(swig_special_flags ${swig_special_flags} "-c++")
  178. endif()
  179. set(swig_extra_flags)
  180. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  181. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  182. endif()
  183. add_custom_command(
  184. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  185. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  186. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  187. COMMAND "${SWIG_EXECUTABLE}"
  188. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  189. ${swig_source_file_flags}
  190. ${CMAKE_SWIG_FLAGS}
  191. -outdir ${swig_outdir}
  192. ${swig_special_flags}
  193. ${swig_extra_flags}
  194. ${swig_include_dirs}
  195. -o "${swig_generated_file_fullname}"
  196. "${swig_source_file_fullname}"
  197. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  198. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  199. IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}"
  200. COMMENT "Swig source")
  201. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  202. PROPERTIES GENERATED 1)
  203. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  204. endmacro()
  205. #
  206. # Create Swig module
  207. #
  208. macro(SWIG_ADD_MODULE name language)
  209. message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  210. swig_add_library(${name}
  211. LANGUAGE ${language}
  212. TYPE MODULE
  213. SOURCES ${ARGN})
  214. endmacro()
  215. macro(SWIG_ADD_LIBRARY name)
  216. set(options "")
  217. set(oneValueArgs LANGUAGE
  218. TYPE)
  219. set(multiValueArgs SOURCES)
  220. cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  221. if(NOT DEFINED _SAM_LANGUAGE)
  222. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  223. endif()
  224. if(NOT DEFINED _SAM_SOURCES)
  225. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  226. endif()
  227. if(NOT DEFINED _SAM_TYPE)
  228. set(_SAM_TYPE MODULE)
  229. elseif("${_SAM_TYPE}" STREQUAL "USE_BUILD_SHARED_LIBS")
  230. unset(_SAM_TYPE)
  231. endif()
  232. swig_module_initialize(${name} ${_SAM_LANGUAGE})
  233. set(swig_dot_i_sources)
  234. set(swig_other_sources)
  235. foreach(it ${_SAM_SOURCES})
  236. if(${it} MATCHES "\\.i$")
  237. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  238. else()
  239. set(swig_other_sources ${swig_other_sources} "${it}")
  240. endif()
  241. endforeach()
  242. set(swig_generated_sources)
  243. foreach(it ${swig_dot_i_sources})
  244. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  245. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  246. endforeach()
  247. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  248. set_directory_properties(PROPERTIES
  249. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  250. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  251. ${_SAM_TYPE}
  252. ${swig_generated_sources}
  253. ${swig_other_sources})
  254. if("${_SAM_TYPE}" STREQUAL "MODULE")
  255. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES NO_SONAME ON)
  256. endif()
  257. string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
  258. if ("${swig_lowercase_language}" STREQUAL "octave")
  259. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  260. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
  261. elseif ("${swig_lowercase_language}" STREQUAL "go")
  262. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  263. elseif ("${swig_lowercase_language}" STREQUAL "java")
  264. if (APPLE)
  265. # In java you want:
  266. # System.loadLibrary("LIBRARY");
  267. # then JNI will look for a library whose name is platform dependent, namely
  268. # MacOS : libLIBRARY.jnilib
  269. # Windows: LIBRARY.dll
  270. # Linux : libLIBRARY.so
  271. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  272. endif ()
  273. elseif ("${swig_lowercase_language}" STREQUAL "lua")
  274. if("${_SAM_TYPE}" STREQUAL "MODULE")
  275. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  276. endif()
  277. elseif ("${swig_lowercase_language}" STREQUAL "python")
  278. # this is only needed for the python case where a _modulename.so is generated
  279. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  280. # Python extension modules on Windows must have the extension ".pyd"
  281. # instead of ".dll" as of Python 2.5. Older python versions do support
  282. # this suffix.
  283. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  284. # <quote>
  285. # Windows: .dll is no longer supported as a filename extension for extension modules.
  286. # .pyd is now the only filename extension that will be searched for.
  287. # </quote>
  288. if(WIN32 AND NOT CYGWIN)
  289. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  290. endif()
  291. elseif ("${swig_lowercase_language}" STREQUAL "r")
  292. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  293. elseif ("${swig_lowercase_language}" STREQUAL "ruby")
  294. # In ruby you want:
  295. # require 'LIBRARY'
  296. # then ruby will look for a library whose name is platform dependent, namely
  297. # MacOS : LIBRARY.bundle
  298. # Windows: LIBRARY.dll
  299. # Linux : LIBRARY.so
  300. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  301. if (APPLE)
  302. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".bundle")
  303. endif ()
  304. else()
  305. # assume empty prefix because we expect the module to be dynamically loaded
  306. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  307. endif ()
  308. endmacro()
  309. #
  310. # Like TARGET_LINK_LIBRARIES but for swig modules
  311. #
  312. macro(SWIG_LINK_LIBRARIES name)
  313. if(SWIG_MODULE_${name}_REAL_NAME)
  314. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  315. else()
  316. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  317. endif()
  318. endmacro()