UseSWIG.cmake 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. Defines the following command for use with SWIG:
  7. .. command:: swig_add_library
  8. Define swig module with given name and specified language::
  9. swig_add_library(<name>
  10. [TYPE <SHARED|MODULE|STATIC|USE_BUILD_SHARED_LIBS>]
  11. LANGUAGE <language>
  12. [NO_PROXY]
  13. [OUTPUT_DIR <directory>]
  14. [OUTFILE_DIR <directory>]
  15. SOURCES <file>...
  16. )
  17. Targets created with command ``swig_add_library`` have the same capabilities as targets
  18. created with command :command:`add_library`, so can be used with any command accepting a target
  19. especially command :command:`target_link_libraries`.
  20. The arguments are:
  21. ``TYPE``
  22. ``SHARED``, ``MODULE`` and ``STATIC`` have same semantic as command :command:`add_library`.
  23. if ``USE_BUILD_SHARED_LIBS`` is specified, library type will be ``STATIC`` or ``SHARED``
  24. based on whether the current value of the variable :variable:`BUILD_SHARED_LIBS` is ``ON``.
  25. If none is specified, ``MODULE`` will be used.
  26. ``LANGUAGE``
  27. Specify the target language.
  28. ``NO_PROXY``
  29. Prevent the generation of the wrapper layer (swig ``-noproxy`` option).
  30. ``OUTPUT_DIR``
  31. Specify where to write the language specific files (swig ``-outdir`` option).
  32. If not specified, variable ``CMAKE_SWIG_OUTDIR`` will be used. If none is specified,
  33. :variable:`CMAKE_CURRENT_BINARY_DIR` is used.
  34. ``OUTFILE_DIR``
  35. Specify an output directory name where the generated source file will be placed
  36. (swig -o option). If not specified, variable ``SWIG_OUTFILE_DIR`` will be used.
  37. If none is specified, option ``OUTPUT_DIR`` or variable ``CMAKE_SWIG_OUTDIR`` is used.
  38. ``SOURCES``
  39. List of sources for the library. Files with extension ``.i`` will be identified as sources
  40. for ``SWIG`` tool. Other files will be handled in the standard way.
  41. Source files properties on module files **must** be set before the invocation
  42. of the ``swig_add_library`` command to specify special behavior of SWIG and ensure
  43. generated files will receive required settings.
  44. ``CPLUSPLUS``
  45. Call SWIG in c++ mode. For example:
  46. .. code-block:: cmake
  47. set_property(SOURCE mymod.i PROPERTY CPLUSPLUS ON)
  48. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  49. ``INCLUDE_DIRECTORIES``, ``COMPILE_DEFINITIONS`` and ``COMPILE_OPTIONS``
  50. Add custom flags to SWIG compiler and have same semantic as properties
  51. :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and :prop_sf:`COMPILE_OPTIONS`.
  52. ``GENERATED_INCLUDE_DIRECTORIES``, ``GENERATED_COMPILE_DEFINITIONS`` and ``GENERATED_COMPILE_OPTIONS``
  53. Add custom flags to the C/C++ generated source. They will fill, respectively,
  54. properties :prop_sf:`INCLUDE_DIRECTORIES`, :prop_sf:`COMPILE_DEFINITIONS` and
  55. :prop_sf:`COMPILE_OPTIONS` of generated C/C++ file.
  56. ``DEPENDS``
  57. Specify additional dependencies to the source file.
  58. ``SWIG_MODULE_NAME``
  59. Specify the actual import name of the module in the target language.
  60. This is required if it cannot be scanned automatically from source
  61. or different from the module file basename. For example:
  62. .. code-block:: cmake
  63. set_property(SOURCE mymod.i PROPERTY SWIG_MODULE_NAME mymod_realname)
  64. Target library properties can be set to apply same configuration to all SWIG input files.
  65. ``SWIG_INCLUDE_DIRECTORIES``, ``SWIG_COMPILE_DEFINITIONS`` and ``SWIG_COMPILE_OPTIONS``
  66. These properties will be applied to all SWIG input files and have same semantic as
  67. target properties :prop_tgt:`INCLUDE_DIRECTORIES`, :prop_tgt:`COMPILE_DEFINITIONS` and
  68. :prop_tgt:`COMPILE_OPTIONS`.
  69. .. code-block:: cmake
  70. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  71. set_property(TARGET mymod PROPERTY SWIG_COMPILE_DEFINITIONS MY_DEF1 MY_DEF2)
  72. set_property(TARGET mymod PROPERTY SWIG_COMPILE_OPTIONS -bla -blb)
  73. ``SWIG_GENERATED_INCLUDE_DIRECTORIES``, ``SWIG_GENERATED_COMPILE_DEFINITIONS`` and ``SWIG_GENERATED_COMPILE_OPTIONS``
  74. These properties will populate, respectively, properties :prop_sf:`INCLUDE_DIRECTORIES`,
  75. :prop_sf:`COMPILE_DEFINITIONS` and :prop_sf:`COMPILE_FLAGS` of all generated C/C++ files.
  76. ``SWIG_DEPENDS``
  77. Add dependencies to all SWIG input files.
  78. ``SWIG_SUPPORT_FILES``
  79. This output property list of wrapper files generated during SWIG compilation.
  80. .. code-block:: cmake
  81. swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
  82. get_property(support_files TARGET mymod PROPERTY SWIG_SUPPORT_FILES)
  83. Some variables can be set to specify special behavior of SWIG:
  84. ``CMAKE_SWIG_FLAGS``
  85. Add flags to all swig calls.
  86. ``CMAKE_SWIG_OUTDIR``
  87. Specify where to write the language specific files (swig ``-outdir`` option).
  88. ``SWIG_OUTFILE_DIR``
  89. Specify an output directory name where the generated source file will be
  90. placed. If not specified, ``CMAKE_SWIG_OUTDIR`` is used.
  91. ``SWIG_MODULE_<name>_EXTRA_DEPS``
  92. Specify extra dependencies for the generated module for ``<name>``.
  93. #]=======================================================================]
  94. cmake_policy (VERSION 3.11)
  95. set(SWIG_CXX_EXTENSION "cxx")
  96. set(SWIG_EXTRA_LIBRARIES "")
  97. set(SWIG_PYTHON_EXTRA_FILE_EXTENSIONS ".py")
  98. set(SWIG_JAVA_EXTRA_FILE_EXTENSIONS ".java" "JNI.java")
  99. ##
  100. ## PRIVATE functions
  101. ##
  102. function (__SWIG_COMPUTE_TIMESTAMP name language infile workingdir __timestamp)
  103. get_filename_component(filename "${infile}" NAME_WE)
  104. set(${__timestamp}
  105. "${workingdir}/${filename}${language}.stamp" PARENT_SCOPE)
  106. # get_filename_component(filename "${infile}" ABSOLUTE)
  107. # string(UUID uuid NAMESPACE 9735D882-D2F8-4E1D-88C9-A0A4F1F6ECA4
  108. # NAME ${name}-${language}-${filename} TYPE SHA1)
  109. # set(${__timestamp} "${workingdir}/${uuid}.stamp" PARENT_SCOPE)
  110. endfunction()
  111. #
  112. # For given swig module initialize variables associated with it
  113. #
  114. macro(SWIG_MODULE_INITIALIZE name language)
  115. string(TOUPPER "${language}" SWIG_MODULE_${name}_LANGUAGE)
  116. string(TOLOWER "${language}" SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG)
  117. set(SWIG_MODULE_${name}_NAME "${name}")
  118. set(SWIG_MODULE_${name}_EXTRA_FLAGS)
  119. if (NOT DEFINED SWIG_MODULE_${name}_NOPROXY)
  120. set (SWIG_MODULE_${name}_NOPROXY FALSE)
  121. endif()
  122. if ("-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
  123. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  124. endif ()
  125. if (SWIG_MODULE_${name}_NOPROXY AND NOT "-noproxy" IN_LIST CMAKE_SWIG_FLAGS)
  126. list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-noproxy")
  127. endif()
  128. if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "UNKNOWN")
  129. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  130. elseif(SWIG_MODULE_${name}_LANGUAGE STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  131. # swig will produce a module.py containing an 'import _modulename' statement,
  132. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  133. # unless the -noproxy flag is used
  134. set(SWIG_MODULE_${name}_NAME "_${name}")
  135. elseif(SWIG_MODULE_${name}_LANGUAGE STREQUAL "PERL")
  136. list(APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  137. endif()
  138. endmacro()
  139. #
  140. # For a given language, input file, and output file, determine extra files that
  141. # will be generated. This is internal swig macro.
  142. #
  143. function(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  144. set(files)
  145. get_source_file_property(module_basename
  146. "${infile}" SWIG_MODULE_NAME)
  147. if(NOT swig_module_basename)
  148. # try to get module name from "%module foo" syntax
  149. if ( EXISTS "${infile}" )
  150. file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*[a-zA-Z0-9_]+.*" )
  151. endif ()
  152. if ( module_basename )
  153. string ( REGEX REPLACE "[ ]*%module[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
  154. else ()
  155. # try to get module name from "%module (options=...) foo" syntax
  156. if ( EXISTS "${infile}" )
  157. file ( STRINGS "${infile}" module_basename REGEX "[ ]*%module[ ]*\\(.*\\)[ ]*[a-zA-Z0-9_]+.*" )
  158. endif ()
  159. if ( module_basename )
  160. string ( REGEX REPLACE "[ ]*%module[ ]*\\(.*\\)[ ]*([a-zA-Z0-9_]+).*" "\\1" module_basename "${module_basename}" )
  161. else ()
  162. # fallback to file basename
  163. get_filename_component(module_basename "${infile}" NAME_WE)
  164. endif ()
  165. endif ()
  166. endif()
  167. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSIONS})
  168. set(extra_file "${generatedpath}/${module_basename}${it}")
  169. list(APPEND files "${extra_file}")
  170. endforeach()
  171. # Treat extra outputs as plain files regardless of language.
  172. set_source_files_properties(${files} PROPERTIES LANGUAGE "")
  173. set (${outfiles} ${files} PARENT_SCOPE)
  174. endfunction()
  175. #
  176. # Take swig (*.i) file and add proper custom commands for it
  177. #
  178. function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  179. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  180. get_source_file_property(swig_source_file_cplusplus "${infile}" CPLUSPLUS)
  181. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  182. if(CMAKE_SWIG_OUTDIR)
  183. set(outdir ${CMAKE_SWIG_OUTDIR})
  184. else()
  185. set(outdir ${CMAKE_CURRENT_BINARY_DIR})
  186. endif()
  187. if(SWIG_OUTFILE_DIR)
  188. set(outfiledir ${SWIG_OUTFILE_DIR})
  189. else()
  190. set(outfiledir ${outdir})
  191. endif()
  192. if(SWIG_WORKING_DIR)
  193. set (workingdir "${SWIG_WORKING_DIR}")
  194. else()
  195. set(workingdir "${outdir}")
  196. endif()
  197. set (swig_source_file_flags ${CMAKE_SWIG_FLAGS})
  198. # handle various swig compile flags properties
  199. get_source_file_property (include_directories "${infile}" INCLUDE_DIRECTORIES)
  200. if (include_directories)
  201. list (APPEND swig_source_file_flags "-I$<JOIN:${include_directories},$<SEMICOLON>-I>")
  202. endif()
  203. set (property "$<TARGET_PROPERTY:${name},SWIG_INCLUDE_DIRECTORIES>")
  204. list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-I$<JOIN:${property},$<SEMICOLON>-I>>")
  205. set (property "$<TARGET_PROPERTY:${name},SWIG_COMPILE_DEFINITIONS>")
  206. list (APPEND swig_source_file_flags "$<$<BOOL:${property}>:-D$<JOIN:${property},$<SEMICOLON>-D>>")
  207. get_source_file_property (compile_definitions "${infile}" COMPILE_DEFINITIONS)
  208. if (compile_definitions)
  209. list (APPEND swig_source_file_flags "-D$<JOIN:${compile_definitions},$<SEMICOLON>-D>")
  210. endif()
  211. list (APPEND swig_source_file_flags "$<TARGET_PROPERTY:${name},SWIG_COMPILE_OPTIONS>")
  212. get_source_file_property (compile_options "${infile}" COMPILE_OPTIONS)
  213. if (compile_options)
  214. list (APPEND swig_source_file_flags ${compile_options})
  215. endif()
  216. # legacy support
  217. get_source_file_property (swig_flags "${infile}" SWIG_FLAGS)
  218. if (swig_flags)
  219. list (APPEND swig_source_file_flags ${swig_flags})
  220. endif()
  221. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  222. if (NOT SWIG_MODULE_${name}_NOPROXY)
  223. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  224. swig_extra_generated_files
  225. "${outdir}"
  226. "${swig_source_file_fullname}")
  227. endif()
  228. set(swig_generated_file_fullname
  229. "${outfiledir}/${swig_source_file_name_we}")
  230. # add the language into the name of the file (i.e. TCL_wrap)
  231. # this allows for the same .i file to be wrapped into different languages
  232. string(APPEND swig_generated_file_fullname
  233. "${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  234. if(swig_source_file_cplusplus)
  235. string(APPEND swig_generated_file_fullname
  236. ".${SWIG_CXX_EXTENSION}")
  237. else()
  238. string(APPEND swig_generated_file_fullname
  239. ".c")
  240. endif()
  241. get_directory_property (cmake_include_directories INCLUDE_DIRECTORIES)
  242. list (REMOVE_DUPLICATES cmake_include_directories)
  243. set (swig_include_dirs)
  244. if (cmake_include_directories)
  245. set (swig_include_dirs "-I$<JOIN:${cmake_include_directories},$<SEMICOLON>-I>")
  246. endif()
  247. set(swig_special_flags)
  248. # default is c, so add c++ flag if it is c++
  249. if(swig_source_file_cplusplus)
  250. list (APPEND swig_special_flags "-c++")
  251. endif()
  252. set (swig_extra_flags)
  253. if(SWIG_MODULE_${name}_LANGUAGE STREQUAL "CSHARP")
  254. if(NOT ("-dllimport" IN_LIST swig_source_file_flags OR "-dllimport" IN_LIST SWIG_MODULE_${name}_EXTRA_FLAGS))
  255. # This makes sure that the name used in the generated DllImport
  256. # matches the library name created by CMake
  257. list (APPEND SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport" "${name}")
  258. endif()
  259. endif()
  260. list (APPEND swig_extra_flags ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  261. # dependencies
  262. set (swig_dependencies ${SWIG_MODULE_${name}_EXTRA_DEPS} $<TARGET_PROPERTY:${name},SWIG_DEPENDS>)
  263. get_source_file_property(file_depends "${infile}" DEPENDS)
  264. if (file_depends)
  265. list (APPEND swig_dependencies ${file_depends})
  266. endif()
  267. # IMPLICIT_DEPENDS below can not handle situations where a dependent file is
  268. # removed. We need an extra step with timestamp and custom target, see #16830
  269. # As this is needed only for Makefile generator do it conditionally
  270. if(CMAKE_GENERATOR MATCHES "Make")
  271. __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE}
  272. "${infile}" "${workingdir}" swig_generated_timestamp)
  273. set(swig_custom_output "${swig_generated_timestamp}")
  274. set(swig_custom_products
  275. BYPRODUCTS "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  276. set(swig_timestamp_command
  277. COMMAND ${CMAKE_COMMAND} -E touch "${swig_generated_timestamp}")
  278. else()
  279. set(swig_custom_output
  280. "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  281. set(swig_custom_products)
  282. set(swig_timestamp_command)
  283. endif()
  284. add_custom_command(
  285. OUTPUT ${swig_custom_output}
  286. ${swig_custom_products}
  287. # Let's create the ${outdir} at execution time, in case dir contains $(OutDir)
  288. COMMAND "${CMAKE_COMMAND}" -E make_directory ${outdir} ${outfiledir}
  289. ${swig_timestamp_command}
  290. COMMAND "${SWIG_EXECUTABLE}"
  291. "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  292. "${swig_source_file_flags}"
  293. -outdir "${outdir}"
  294. ${swig_special_flags}
  295. ${swig_extra_flags}
  296. "${swig_include_dirs}"
  297. -o "${swig_generated_file_fullname}"
  298. "${swig_source_file_fullname}"
  299. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  300. DEPENDS ${swig_dependencies}
  301. IMPLICIT_DEPENDS CXX "${swig_source_file_fullname}"
  302. COMMENT "Swig source"
  303. COMMAND_EXPAND_LISTS)
  304. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  305. PROPERTIES GENERATED 1)
  306. ## add all properties for generated file to various properties
  307. get_property (include_directories SOURCE "${infile}" PROPERTY GENERATED_INCLUDE_DIRECTORIES)
  308. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY INCLUDE_DIRECTORIES ${include_directories} $<TARGET_PROPERTY:${name},SWIG_GENERATED_INCLUDE_DIRECTORIES>)
  309. get_property (compile_definitions SOURCE "${infile}" PROPERTY GENERATED_COMPILE_DEFINITIONS)
  310. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_DEFINITIONS $<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_DEFINITIONS> ${compile_definitions})
  311. get_property (compile_options SOURCE "${infile}" PROPERTY GENERATED_COMPILE_OPTIONS)
  312. set_property (SOURCE "${swig_generated_file_fullname}" PROPERTY COMPILE_OPTIONS $<TARGET_PROPERTY:${name},SWIG_GENERATED_COMPILE_OPTIONS> ${compile_options})
  313. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files} PARENT_SCOPE)
  314. # legacy support
  315. set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
  316. endfunction()
  317. #
  318. # Create Swig module
  319. #
  320. macro(SWIG_ADD_MODULE name language)
  321. message(DEPRECATION "SWIG_ADD_MODULE is deprecated. Use SWIG_ADD_LIBRARY instead.")
  322. swig_add_library(${name}
  323. LANGUAGE ${language}
  324. TYPE MODULE
  325. SOURCES ${ARGN})
  326. endmacro()
  327. function(SWIG_ADD_LIBRARY name)
  328. set(options NO_PROXY)
  329. set(oneValueArgs LANGUAGE
  330. TYPE
  331. OUTPUT_DIR
  332. OUTFILE_DIR)
  333. set(multiValueArgs SOURCES)
  334. cmake_parse_arguments(_SAM "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  335. if (TARGET ${name})
  336. # a target with same name is already defined.
  337. # call NOW add_library command to raise the most useful error message
  338. add_library(${name})
  339. return()
  340. endif()
  341. if (_SAM_UNPARSED_ARGUMENTS)
  342. message(FATAL_ERROR "SWIG_ADD_LIBRARY: ${_SAM_UNPARSED_ARGUMENTS}: unexpected arguments")
  343. endif()
  344. if(NOT DEFINED _SAM_LANGUAGE)
  345. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing LANGUAGE argument")
  346. endif()
  347. if(NOT DEFINED _SAM_SOURCES)
  348. message(FATAL_ERROR "SWIG_ADD_LIBRARY: Missing SOURCES argument")
  349. endif()
  350. if(NOT DEFINED _SAM_TYPE)
  351. set(_SAM_TYPE MODULE)
  352. elseif(_SAM_TYPE STREQUAL "USE_BUILD_SHARED_LIBS")
  353. unset(_SAM_TYPE)
  354. endif()
  355. set (workingdir "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${name}.dir")
  356. # set special variable to pass extra information to command SWIG_ADD_SOURCE_TO_MODULE
  357. # which cannot be changed due to legacy compatibility
  358. set (SWIG_WORKING_DIR "${workingdir}")
  359. set (outputdir "${_SAM_OUTPUT_DIR}")
  360. if (NOT _SAM_OUTPUT_DIR)
  361. if (CMAKE_SWIG_OUTDIR)
  362. set (outputdir "${CMAKE_SWIG_OUTDIR}")
  363. else()
  364. set (outputdir "${CMAKE_CURRENT_BINARY_DIR}")
  365. endif()
  366. endif()
  367. set (outfiledir "${_SAM_OUTFILE_DIR}")
  368. if(NOT _SAM_OUTFILE_DIR)
  369. if (SWIG_OUTFILE_DIR)
  370. set (outfiledir "${SWIG_OUTFILE_DIR}")
  371. else()
  372. if (_SAM_OUTPUT_DIR OR CMAKE_SWIG_OUTDIR)
  373. set (outfiledir "${outputdir}")
  374. else()
  375. set (outfiledir "${workingdir}")
  376. endif()
  377. endif()
  378. endif()
  379. # set again, locally, predefined variables to ensure compatibility
  380. # with command SWIG_ADD_SOURCE_TO_MODULE
  381. set(CMAKE_SWIG_OUTDIR "${outputdir}")
  382. set(SWIG_OUTFILE_DIR "${outfiledir}")
  383. set (SWIG_MODULE_${name}_NOPROXY ${_SAM_NO_PROXY})
  384. swig_module_initialize(${name} ${_SAM_LANGUAGE})
  385. set(swig_dot_i_sources ${_SAM_SOURCES})
  386. list(FILTER swig_dot_i_sources INCLUDE REGEX "\\.i$")
  387. set(swig_other_sources ${_SAM_SOURCES})
  388. list(REMOVE_ITEM swig_other_sources ${swig_dot_i_sources})
  389. set(swig_generated_sources)
  390. set(swig_generated_timestamps)
  391. foreach(swig_it IN LISTS swig_dot_i_sources)
  392. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source "${swig_it}")
  393. list (APPEND swig_generated_sources "${swig_generated_source}")
  394. if(CMAKE_GENERATOR MATCHES "Make")
  395. __swig_compute_timestamp(${name} ${SWIG_MODULE_${name}_LANGUAGE} "${swig_it}"
  396. "${workingdir}" swig_timestamp)
  397. list (APPEND swig_generated_timestamps "${swig_timestamp}")
  398. endif()
  399. endforeach()
  400. set_property (DIRECTORY APPEND PROPERTY
  401. ADDITIONAL_MAKE_CLEAN_FILES ${swig_generated_sources} ${swig_generated_timestamps})
  402. add_library(${name}
  403. ${_SAM_TYPE}
  404. ${swig_generated_sources}
  405. ${swig_other_sources})
  406. set_target_properties(${name} PROPERTIES OUTPUT_NAME "${SWIG_MODULE_${name}_NAME}")
  407. if(CMAKE_GENERATOR MATCHES "Make")
  408. # see IMPLICIT_DEPENDS above
  409. add_custom_target(${name}_swig_compilation DEPENDS ${swig_generated_timestamps})
  410. add_dependencies(${name} ${name}_swig_compilation)
  411. endif()
  412. if(_SAM_TYPE STREQUAL "MODULE")
  413. set_target_properties(${name} PROPERTIES NO_SONAME ON)
  414. endif()
  415. string(TOLOWER "${_SAM_LANGUAGE}" swig_lowercase_language)
  416. if (swig_lowercase_language STREQUAL "octave")
  417. set_target_properties(${name} PROPERTIES PREFIX "")
  418. set_target_properties(${name} PROPERTIES SUFFIX ".oct")
  419. elseif (swig_lowercase_language STREQUAL "go")
  420. set_target_properties(${name} PROPERTIES PREFIX "")
  421. elseif (swig_lowercase_language STREQUAL "java")
  422. if (APPLE)
  423. # In java you want:
  424. # System.loadLibrary("LIBRARY");
  425. # then JNI will look for a library whose name is platform dependent, namely
  426. # MacOS : libLIBRARY.jnilib
  427. # Windows: LIBRARY.dll
  428. # Linux : libLIBRARY.so
  429. set_target_properties (${name} PROPERTIES SUFFIX ".jnilib")
  430. endif ()
  431. elseif (swig_lowercase_language STREQUAL "lua")
  432. if(_SAM_TYPE STREQUAL "MODULE")
  433. set_target_properties(${name} PROPERTIES PREFIX "")
  434. endif()
  435. elseif (swig_lowercase_language STREQUAL "python")
  436. # this is only needed for the python case where a _modulename.so is generated
  437. set_target_properties(${name} PROPERTIES PREFIX "")
  438. # Python extension modules on Windows must have the extension ".pyd"
  439. # instead of ".dll" as of Python 2.5. Older python versions do support
  440. # this suffix.
  441. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  442. # <quote>
  443. # Windows: .dll is no longer supported as a filename extension for extension modules.
  444. # .pyd is now the only filename extension that will be searched for.
  445. # </quote>
  446. if(WIN32 AND NOT CYGWIN)
  447. set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
  448. endif()
  449. elseif (swig_lowercase_language STREQUAL "r")
  450. set_target_properties(${name} PROPERTIES PREFIX "")
  451. elseif (swig_lowercase_language STREQUAL "ruby")
  452. # In ruby you want:
  453. # require 'LIBRARY'
  454. # then ruby will look for a library whose name is platform dependent, namely
  455. # MacOS : LIBRARY.bundle
  456. # Windows: LIBRARY.dll
  457. # Linux : LIBRARY.so
  458. set_target_properties (${name} PROPERTIES PREFIX "")
  459. if (APPLE)
  460. set_target_properties (${name} PROPERTIES SUFFIX ".bundle")
  461. endif ()
  462. elseif (swig_lowercase_language STREQUAL "perl")
  463. # assume empty prefix because we expect the module to be dynamically loaded
  464. set_target_properties (${name} PROPERTIES PREFIX "")
  465. if (APPLE)
  466. set_target_properties (${name} PROPERTIES SUFFIX ".dylib")
  467. endif ()
  468. else()
  469. # assume empty prefix because we expect the module to be dynamically loaded
  470. set_target_properties (${name} PROPERTIES PREFIX "")
  471. endif ()
  472. # target property SWIG_SUPPORT_FILES lists proxy support files
  473. if (NOT SWIG_MODULE_${name}_NOPROXY)
  474. string(TOUPPER "${_SAM_LANGUAGE}" swig_uppercase_language)
  475. foreach (swig_it IN LISTS SWIG_${swig_uppercase_language}_EXTRA_FILE_EXTENSIONS)
  476. set (swig_support_files ${swig_generated_sources})
  477. list (FILTER swig_support_files INCLUDE REGEX ".*${swig_it}$")
  478. set_property (TARGET ${name} APPEND PROPERTY SWIG_SUPPORT_FILES ${swig_support_files})
  479. endforeach()
  480. endif()
  481. # to ensure legacy behavior, export some variables
  482. set (SWIG_MODULE_${name}_LANGUAGE "${SWIG_MODULE_${name}_LANGUAGE}" PARENT_SCOPE)
  483. set (SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}" PARENT_SCOPE)
  484. set (SWIG_MODULE_${name}_REAL_NAME "${name}" PARENT_SCOPE)
  485. set (SWIG_MODULE_${name}_NOPROXY "${SWIG_MODULE_${name}_NOPROXY}" PARENT_SCOPE)
  486. set (SWIG_MODULE_${name}_EXTRA_FLAGS "${SWIG_MODULE_${name}_EXTRA_FLAGS}" PARENT_SCOPE)
  487. # the last one is a bit crazy but it is documented, so...
  488. # NOTA: works as expected if only ONE input file is specified
  489. set (swig_generated_file_fullname "${swig_generated_file_fullname}" PARENT_SCOPE)
  490. endfunction()
  491. #
  492. # Like TARGET_LINK_LIBRARIES but for swig modules
  493. #
  494. function(SWIG_LINK_LIBRARIES name)
  495. message(DEPRECATION "SWIG_LINK_LIBRARIES is deprecated. Use TARGET_LINK_LIBRARIES instead.")
  496. if(SWIG_MODULE_${name}_REAL_NAME)
  497. target_link_libraries(${name} ${ARGN})
  498. else()
  499. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  500. endif()
  501. endfunction()