UseSWIG.cmake 10.0 KB

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