UseSWIG.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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(${outfiles} ${${outfiles}}
  118. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}${it}")
  119. endforeach()
  120. endmacro()
  121. #
  122. # Take swig (*.i) file and add proper custom commands for it
  123. #
  124. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  125. set(swig_full_infile ${infile})
  126. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  127. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  128. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  129. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  130. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  131. set(swig_source_file_flags "")
  132. endif()
  133. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  134. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  135. if(CMAKE_SWIG_OUTDIR)
  136. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  137. else()
  138. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  139. endif()
  140. if(SWIG_OUTFILE_DIR)
  141. set(swig_outfile_dir ${SWIG_OUTFILE_DIR})
  142. else()
  143. set(swig_outfile_dir ${swig_outdir})
  144. endif()
  145. if (NOT SWIG_MODULE_${name}_NOPROXY)
  146. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  147. swig_extra_generated_files
  148. "${swig_outdir}"
  149. "${swig_source_file_fullname}")
  150. endif()
  151. set(swig_generated_file_fullname
  152. "${swig_outfile_dir}/${swig_source_file_name_we}")
  153. # add the language into the name of the file (i.e. TCL_wrap)
  154. # this allows for the same .i file to be wrapped into different languages
  155. string(APPEND swig_generated_file_fullname
  156. "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  157. if(swig_source_file_cplusplus)
  158. string(APPEND swig_generated_file_fullname
  159. ".${SWIG_CXX_EXTENSION}")
  160. else()
  161. string(APPEND swig_generated_file_fullname
  162. ".c")
  163. endif()
  164. #message("Full path to source file: ${swig_source_file_fullname}")
  165. #message("Full path to the output file: ${swig_generated_file_fullname}")
  166. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  167. list(REMOVE_DUPLICATES cmake_include_directories)
  168. set(swig_include_dirs)
  169. foreach(it ${cmake_include_directories})
  170. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  171. endforeach()
  172. set(swig_special_flags)
  173. # default is c, so add c++ flag if it is c++
  174. if(swig_source_file_cplusplus)
  175. set(swig_special_flags ${swig_special_flags} "-c++")
  176. endif()
  177. set(swig_extra_flags)
  178. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  179. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  180. endif()
  181. add_custom_command(
  182. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  183. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  184. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  185. COMMAND "${SWIG_EXECUTABLE}"
  186. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  187. ${swig_source_file_flags}
  188. ${CMAKE_SWIG_FLAGS}
  189. -outdir ${swig_outdir}
  190. ${swig_special_flags}
  191. ${swig_extra_flags}
  192. ${swig_include_dirs}
  193. -o "${swig_generated_file_fullname}"
  194. "${swig_source_file_fullname}"
  195. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  196. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  197. IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}"
  198. COMMENT "Swig source")
  199. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  200. PROPERTIES GENERATED 1)
  201. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  202. endmacro()
  203. #
  204. # Create Swig module
  205. #
  206. macro(SWIG_ADD_MODULE name language)
  207. message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  208. swig_add_library(${name}
  209. LANGUAGE ${language}
  210. TYPE MODULE
  211. SOURCES ${ARGN})
  212. endmacro()
  213. macro(SWIG_ADD_LIBRARY name)
  214. set(options "")
  215. set(oneValueArgs LANGUAGE
  216. TYPE)
  217. set(multiValueArgs SOURCES)
  218. cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  219. if(NOT DEFINED _SAM_LANGUAGE)
  220. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  221. endif()
  222. if(NOT DEFINED _SAM_SOURCES)
  223. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  224. endif()
  225. if(NOT DEFINED _SAM_TYPE)
  226. set(_SAM_TYPE MODULE)
  227. elseif("${_SAM_TYPE}" STREQUAL "USE_BUILD_SHARED_LIBS")
  228. unset(_SAM_TYPE)
  229. endif()
  230. swig_module_initialize(${name} ${_SAM_LANGUAGE})
  231. set(swig_dot_i_sources)
  232. set(swig_other_sources)
  233. foreach(it ${_SAM_SOURCES})
  234. if(${it} MATCHES "\\.i$")
  235. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  236. else()
  237. set(swig_other_sources ${swig_other_sources} "${it}")
  238. endif()
  239. endforeach()
  240. set(swig_generated_sources)
  241. foreach(it ${swig_dot_i_sources})
  242. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  243. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  244. endforeach()
  245. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  246. set_directory_properties(PROPERTIES
  247. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  248. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  249. ${_SAM_TYPE}
  250. ${swig_generated_sources}
  251. ${swig_other_sources})
  252. if("${_SAM_TYPE}" STREQUAL "MODULE")
  253. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES NO_SONAME ON)
  254. endif()
  255. string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
  256. if ("${swig_lowercase_language}" STREQUAL "octave")
  257. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  258. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".oct")
  259. elseif ("${swig_lowercase_language}" STREQUAL "go")
  260. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  261. elseif ("${swig_lowercase_language}" STREQUAL "java")
  262. if (APPLE)
  263. # In java you want:
  264. # System.loadLibrary("LIBRARY");
  265. # then JNI will look for a library whose name is platform dependent, namely
  266. # MacOS : libLIBRARY.jnilib
  267. # Windows: LIBRARY.dll
  268. # Linux : libLIBRARY.so
  269. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  270. endif ()
  271. elseif ("${swig_lowercase_language}" STREQUAL "lua")
  272. if("${_SAM_TYPE}" STREQUAL "MODULE")
  273. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  274. endif()
  275. elseif ("${swig_lowercase_language}" STREQUAL "python")
  276. # this is only needed for the python case where a _modulename.so is generated
  277. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  278. # Python extension modules on Windows must have the extension ".pyd"
  279. # instead of ".dll" as of Python 2.5. Older python versions do support
  280. # this suffix.
  281. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  282. # <quote>
  283. # Windows: .dll is no longer supported as a filename extension for extension modules.
  284. # .pyd is now the only filename extension that will be searched for.
  285. # </quote>
  286. if(WIN32 AND NOT CYGWIN)
  287. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  288. endif()
  289. elseif ("${swig_lowercase_language}" STREQUAL "r")
  290. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  291. elseif ("${swig_lowercase_language}" STREQUAL "ruby")
  292. # In ruby you want:
  293. # require 'LIBRARY'
  294. # then ruby will look for a library whose name is platform dependent, namely
  295. # MacOS : LIBRARY.bundle
  296. # Windows: LIBRARY.dll
  297. # Linux : LIBRARY.so
  298. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  299. if (APPLE)
  300. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".bundle")
  301. endif ()
  302. else()
  303. # assume empty prefix because we expect the module to be dynamically loaded
  304. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  305. endif ()
  306. endmacro()
  307. #
  308. # Like TARGET_LINK_LIBRARIES but for swig modules
  309. #
  310. macro(SWIG_LINK_LIBRARIES name)
  311. if(SWIG_MODULE_${name}_REAL_NAME)
  312. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  313. else()
  314. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  315. endif()
  316. endmacro()