UseSWIG.cmake 13 KB

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