UseSWIG.cmake 11 KB

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