UseSWIG.cmake 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #.rst:
  2. # UseSWIG
  3. # -------
  4. #
  5. # SWIG module for CMake
  6. #
  7. # Defines the following macros:
  8. #
  9. # ::
  10. #
  11. # SWIG_ADD_MODULE(name language [ files ])
  12. # - Define swig module with given name and specified language
  13. # SWIG_LINK_LIBRARIES(name [ libraries ])
  14. # - Link libraries to swig module
  15. #
  16. # All other macros are for internal use only. To get the actual name of
  17. # the swig module, use: ${SWIG_MODULE_${name}_REAL_NAME}. Set Source
  18. # files properties such as CPLUSPLUS and SWIG_FLAGS to specify special
  19. # behavior of SWIG. Also global CMAKE_SWIG_FLAGS can be used to add
  20. # special flags to all swig calls. Another special variable is
  21. # CMAKE_SWIG_OUTDIR, it allows one to specify where to write all the
  22. # swig generated module (swig -outdir option) The name-specific variable
  23. # SWIG_MODULE_<name>_EXTRA_DEPS may be used to specify extra
  24. # dependencies for the generated modules. If the source file generated
  25. # by swig need some special flag you can use
  26. # set_source_files_properties( ${swig_generated_file_fullname}
  27. #
  28. # ::
  29. #
  30. # PROPERTIES COMPILE_FLAGS "-bla")
  31. #=============================================================================
  32. # Copyright 2004-2009 Kitware, Inc.
  33. # Copyright 2009 Mathieu Malaterre <[email protected]>
  34. #
  35. # Distributed under the OSI-approved BSD License (the "License");
  36. # see accompanying file Copyright.txt for details.
  37. #
  38. # This software is distributed WITHOUT ANY WARRANTY; without even the
  39. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  40. # See the License for more information.
  41. #=============================================================================
  42. # (To distribute this file outside of CMake, substitute the full
  43. # License text for the above reference.)
  44. set(SWIG_CXX_EXTENSION "cxx")
  45. set(SWIG_EXTRA_LIBRARIES "")
  46. set(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
  47. #
  48. # For given swig module initialize variables associated with it
  49. #
  50. macro(SWIG_MODULE_INITIALIZE name language)
  51. string(TOUPPER "${language}" swig_uppercase_language)
  52. string(TOLOWER "${language}" swig_lowercase_language)
  53. set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  54. set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  55. set(SWIG_MODULE_${name}_REAL_NAME "${name}")
  56. if (CMAKE_SWIG_FLAGS MATCHES "-noproxy")
  57. set (SWIG_MODULE_${name}_NOPROXY TRUE)
  58. endif ()
  59. if("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "UNKNOWN")
  60. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  61. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PYTHON" AND NOT SWIG_MODULE_${name}_NOPROXY)
  62. # swig will produce a module.py containing an 'import _modulename' statement,
  63. # which implies having a corresponding _modulename.so (*NIX), _modulename.pyd (Win32),
  64. # unless the -noproxy flag is used
  65. set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  66. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PERL")
  67. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  68. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "CSHARP")
  69. # This makes sure that the name used in the generated DllImport
  70. # matches the library name created by CMake
  71. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-dllimport;${name}")
  72. endif()
  73. endmacro()
  74. #
  75. # For a given language, input file, and output file, determine extra files that
  76. # will be generated. This is internal swig macro.
  77. #
  78. macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  79. set(${outfiles} "")
  80. get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
  81. ${infile} SWIG_MODULE_NAME)
  82. if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  83. get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${infile}" NAME_WE)
  84. endif()
  85. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
  86. set(${outfiles} ${${outfiles}}
  87. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
  88. endforeach()
  89. endmacro()
  90. #
  91. # Take swig (*.i) file and add proper custom commands for it
  92. #
  93. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  94. set(swig_full_infile ${infile})
  95. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  96. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  97. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  98. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  99. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  100. set(swig_source_file_flags "")
  101. endif()
  102. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  103. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  104. if(CMAKE_SWIG_OUTDIR)
  105. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  106. else()
  107. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  108. endif()
  109. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  110. swig_extra_generated_files
  111. "${swig_outdir}"
  112. "${infile}")
  113. set(swig_generated_file_fullname
  114. "${swig_outdir}/${swig_source_file_name_we}")
  115. # add the language into the name of the file (i.e. TCL_wrap)
  116. # this allows for the same .i file to be wrapped into different languages
  117. set(swig_generated_file_fullname
  118. "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  119. if(swig_source_file_cplusplus)
  120. set(swig_generated_file_fullname
  121. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  122. else()
  123. set(swig_generated_file_fullname
  124. "${swig_generated_file_fullname}.c")
  125. endif()
  126. #message("Full path to source file: ${swig_source_file_fullname}")
  127. #message("Full path to the output file: ${swig_generated_file_fullname}")
  128. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  129. list(REMOVE_DUPLICATES cmake_include_directories)
  130. set(swig_include_dirs)
  131. foreach(it ${cmake_include_directories})
  132. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  133. endforeach()
  134. set(swig_special_flags)
  135. # default is c, so add c++ flag if it is c++
  136. if(swig_source_file_cplusplus)
  137. set(swig_special_flags ${swig_special_flags} "-c++")
  138. endif()
  139. set(swig_extra_flags)
  140. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  141. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  142. endif()
  143. add_custom_command(
  144. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  145. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  146. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  147. COMMAND "${SWIG_EXECUTABLE}"
  148. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  149. ${swig_source_file_flags}
  150. ${CMAKE_SWIG_FLAGS}
  151. -outdir ${swig_outdir}
  152. ${swig_special_flags}
  153. ${swig_extra_flags}
  154. ${swig_include_dirs}
  155. -o "${swig_generated_file_fullname}"
  156. "${swig_source_file_fullname}"
  157. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  158. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  159. COMMENT "Swig source")
  160. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  161. PROPERTIES GENERATED 1)
  162. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  163. endmacro()
  164. #
  165. # Create Swig module
  166. #
  167. macro(SWIG_ADD_MODULE name language)
  168. SWIG_MODULE_INITIALIZE(${name} ${language})
  169. set(swig_dot_i_sources)
  170. set(swig_other_sources)
  171. foreach(it ${ARGN})
  172. if(${it} MATCHES ".*\\.i$")
  173. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  174. else()
  175. set(swig_other_sources ${swig_other_sources} "${it}")
  176. endif()
  177. endforeach()
  178. set(swig_generated_sources)
  179. foreach(it ${swig_dot_i_sources})
  180. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  181. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  182. endforeach()
  183. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  184. set_directory_properties(PROPERTIES
  185. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  186. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  187. MODULE
  188. ${swig_generated_sources}
  189. ${swig_other_sources})
  190. string(TOLOWER "${language}" swig_lowercase_language)
  191. if ("${swig_lowercase_language}" STREQUAL "java")
  192. if (APPLE)
  193. # In java you want:
  194. # System.loadLibrary("LIBRARY");
  195. # then JNI will look for a library whose name is platform dependent, namely
  196. # MacOS : libLIBRARY.jnilib
  197. # Windows: LIBRARY.dll
  198. # Linux : libLIBRARY.so
  199. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  200. endif ()
  201. endif ()
  202. if ("${swig_lowercase_language}" STREQUAL "python")
  203. # this is only needed for the python case where a _modulename.so is generated
  204. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  205. # Python extension modules on Windows must have the extension ".pyd"
  206. # instead of ".dll" as of Python 2.5. Older python versions do support
  207. # this suffix.
  208. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  209. # <quote>
  210. # Windows: .dll is no longer supported as a filename extension for extension modules.
  211. # .pyd is now the only filename extension that will be searched for.
  212. # </quote>
  213. if(WIN32 AND NOT CYGWIN)
  214. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  215. endif()
  216. endif ()
  217. endmacro()
  218. #
  219. # Like TARGET_LINK_LIBRARIES but for swig modules
  220. #
  221. macro(SWIG_LINK_LIBRARIES name)
  222. if(SWIG_MODULE_${name}_REAL_NAME)
  223. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  224. else()
  225. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  226. endif()
  227. endmacro()