UseSWIG.cmake 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "UNKNOWN")
  57. message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  58. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PYTHON")
  59. # when swig is used without the -interface it will produce in the module.py
  60. # a 'import _modulename' statement, which implies having a corresponding
  61. # _modulename.so (*NIX), _modulename.pyd (Win32).
  62. set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  63. elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PERL")
  64. set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  65. endif()
  66. endmacro()
  67. #
  68. # For a given language, input file, and output file, determine extra files that
  69. # will be generated. This is internal swig macro.
  70. #
  71. macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  72. set(${outfiles} "")
  73. get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
  74. ${infile} SWIG_MODULE_NAME)
  75. if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  76. get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${infile}" NAME_WE)
  77. endif()
  78. foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
  79. set(${outfiles} ${${outfiles}}
  80. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
  81. endforeach()
  82. endmacro()
  83. #
  84. # Take swig (*.i) file and add proper custom commands for it
  85. #
  86. macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  87. set(swig_full_infile ${infile})
  88. get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
  89. get_source_file_property(swig_source_file_generated ${infile} GENERATED)
  90. get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  91. get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
  92. if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  93. set(swig_source_file_flags "")
  94. endif()
  95. get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
  96. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  97. if(CMAKE_SWIG_OUTDIR)
  98. set(swig_outdir ${CMAKE_SWIG_OUTDIR})
  99. else()
  100. set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  101. endif()
  102. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  103. swig_extra_generated_files
  104. "${swig_outdir}"
  105. "${infile}")
  106. set(swig_generated_file_fullname
  107. "${swig_outdir}/${swig_source_file_name_we}")
  108. # add the language into the name of the file (i.e. TCL_wrap)
  109. # this allows for the same .i file to be wrapped into different languages
  110. set(swig_generated_file_fullname
  111. "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  112. if(swig_source_file_cplusplus)
  113. set(swig_generated_file_fullname
  114. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  115. else()
  116. set(swig_generated_file_fullname
  117. "${swig_generated_file_fullname}.c")
  118. endif()
  119. #message("Full path to source file: ${swig_source_file_fullname}")
  120. #message("Full path to the output file: ${swig_generated_file_fullname}")
  121. get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
  122. set(swig_include_dirs)
  123. foreach(it ${cmake_include_directories})
  124. set(swig_include_dirs ${swig_include_dirs} "-I${it}")
  125. endforeach()
  126. set(swig_special_flags)
  127. # default is c, so add c++ flag if it is c++
  128. if(swig_source_file_cplusplus)
  129. set(swig_special_flags ${swig_special_flags} "-c++")
  130. endif()
  131. set(swig_extra_flags)
  132. if(SWIG_MODULE_${name}_EXTRA_FLAGS)
  133. set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  134. endif()
  135. add_custom_command(
  136. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  137. # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
  138. COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
  139. COMMAND "${SWIG_EXECUTABLE}"
  140. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  141. ${swig_source_file_flags}
  142. ${CMAKE_SWIG_FLAGS}
  143. -outdir ${swig_outdir}
  144. ${swig_special_flags}
  145. ${swig_extra_flags}
  146. ${swig_include_dirs}
  147. -o "${swig_generated_file_fullname}"
  148. "${swig_source_file_fullname}"
  149. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  150. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  151. COMMENT "Swig source")
  152. set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  153. PROPERTIES GENERATED 1)
  154. set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  155. endmacro()
  156. #
  157. # Create Swig module
  158. #
  159. macro(SWIG_ADD_MODULE name language)
  160. SWIG_MODULE_INITIALIZE(${name} ${language})
  161. set(swig_dot_i_sources)
  162. set(swig_other_sources)
  163. foreach(it ${ARGN})
  164. if(${it} MATCHES ".*\\.i$")
  165. set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  166. else()
  167. set(swig_other_sources ${swig_other_sources} "${it}")
  168. endif()
  169. endforeach()
  170. set(swig_generated_sources)
  171. foreach(it ${swig_dot_i_sources})
  172. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  173. set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  174. endforeach()
  175. get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  176. set_directory_properties(PROPERTIES
  177. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  178. add_library(${SWIG_MODULE_${name}_REAL_NAME}
  179. MODULE
  180. ${swig_generated_sources}
  181. ${swig_other_sources})
  182. string(TOLOWER "${language}" swig_lowercase_language)
  183. if ("${swig_lowercase_language}" STREQUAL "java")
  184. if (APPLE)
  185. # In java you want:
  186. # System.loadLibrary("LIBRARY");
  187. # then JNI will look for a library whose name is platform dependent, namely
  188. # MacOS : libLIBRARY.jnilib
  189. # Windows: LIBRARY.dll
  190. # Linux : libLIBRARY.so
  191. set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
  192. endif ()
  193. endif ()
  194. if ("${swig_lowercase_language}" STREQUAL "python")
  195. # this is only needed for the python case where a _modulename.so is generated
  196. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
  197. # Python extension modules on Windows must have the extension ".pyd"
  198. # instead of ".dll" as of Python 2.5. Older python versions do support
  199. # this suffix.
  200. # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
  201. # <quote>
  202. # Windows: .dll is no longer supported as a filename extension for extension modules.
  203. # .pyd is now the only filename extension that will be searched for.
  204. # </quote>
  205. if(WIN32 AND NOT CYGWIN)
  206. set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
  207. endif()
  208. endif ()
  209. endmacro()
  210. #
  211. # Like TARGET_LINK_LIBRARIES but for swig modules
  212. #
  213. macro(SWIG_LINK_LIBRARIES name)
  214. if(SWIG_MODULE_${name}_REAL_NAME)
  215. target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  216. else()
  217. message(SEND_ERROR "Cannot find Swig library \"${name}\".")
  218. endif()
  219. endmacro()