UseSWIG.cmake 11 KB

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