UseSWIG.cmake 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #=============================================================================
  18. # Copyright 2004-2009 Kitware, Inc.
  19. #
  20. # Distributed under the OSI-approved BSD License (the "License");
  21. # see accompanying file Copyright.txt for details.
  22. #
  23. # This software is distributed WITHOUT ANY WARRANTY; without even the
  24. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  25. # See the License for more information.
  26. #=============================================================================
  27. # (To distributed this file outside of CMake, substitute the full
  28. # License text for the above reference.)
  29. SET(SWIG_CXX_EXTENSION "cxx")
  30. SET(SWIG_EXTRA_LIBRARIES "")
  31. SET(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
  32. #
  33. # For given swig module initialize variables associated with it
  34. #
  35. MACRO(SWIG_MODULE_INITIALIZE name language)
  36. STRING(TOUPPER "${language}" swig_uppercase_language)
  37. STRING(TOLOWER "${language}" swig_lowercase_language)
  38. SET(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  39. SET(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  40. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
  41. MESSAGE(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  42. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
  43. SET(SWIG_MODULE_${name}_REAL_NAME "${name}")
  44. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
  45. SET(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  46. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
  47. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
  48. SET(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  49. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
  50. ENDMACRO(SWIG_MODULE_INITIALIZE)
  51. #
  52. # For a given language, input file, and output file, determine extra files that
  53. # will be generated. This is internal swig macro.
  54. #
  55. MACRO(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  56. GET_SOURCE_FILE_PROPERTY(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
  57. ${infile} SWIG_MODULE_NAME)
  58. IF(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  59. GET_FILENAME_COMPONENT(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${infile}" NAME_WE)
  60. ENDIF(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
  61. FOREACH(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
  62. SET(${outfiles} ${${outfiles}}
  63. "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
  64. ENDFOREACH(it)
  65. ENDMACRO(SWIG_GET_EXTRA_OUTPUT_FILES)
  66. #
  67. # Take swig (*.i) file and add proper custom commands for it
  68. #
  69. MACRO(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  70. SET(swig_full_infile ${infile})
  71. GET_FILENAME_COMPONENT(swig_source_file_path "${infile}" PATH)
  72. GET_FILENAME_COMPONENT(swig_source_file_name_we "${infile}" NAME_WE)
  73. GET_SOURCE_FILE_PROPERTY(swig_source_file_generated ${infile} GENERATED)
  74. GET_SOURCE_FILE_PROPERTY(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  75. GET_SOURCE_FILE_PROPERTY(swig_source_file_flags ${infile} SWIG_FLAGS)
  76. IF("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  77. SET(swig_source_file_flags "")
  78. ENDIF("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  79. SET(swig_source_file_fullname "${infile}")
  80. IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  81. STRING(REGEX REPLACE
  82. "^${CMAKE_CURRENT_SOURCE_DIR}" ""
  83. swig_source_file_relative_path
  84. "${swig_source_file_path}")
  85. ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  86. IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  87. STRING(REGEX REPLACE
  88. "^${CMAKE_CURRENT_BINARY_DIR}" ""
  89. swig_source_file_relative_path
  90. "${swig_source_file_path}")
  91. SET(swig_source_file_generated 1)
  92. ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  93. SET(swig_source_file_relative_path "${swig_source_file_path}")
  94. IF(swig_source_file_generated)
  95. SET(swig_source_file_fullname "${CMAKE_CURRENT_BINARY_DIR}/${infile}")
  96. ELSE(swig_source_file_generated)
  97. SET(swig_source_file_fullname "${CMAKE_CURRENT_SOURCE_DIR}/${infile}")
  98. ENDIF(swig_source_file_generated)
  99. ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  100. ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  101. SET(swig_generated_file_fullname
  102. "${CMAKE_CURRENT_BINARY_DIR}")
  103. IF(swig_source_file_relative_path)
  104. SET(swig_generated_file_fullname
  105. "${swig_generated_file_fullname}/${swig_source_file_relative_path}")
  106. ENDIF(swig_source_file_relative_path)
  107. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  108. IF(CMAKE_SWIG_OUTDIR)
  109. SET(swig_outdir ${CMAKE_SWIG_OUTDIR})
  110. ELSE(CMAKE_SWIG_OUTDIR)
  111. SET(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
  112. ENDIF(CMAKE_SWIG_OUTDIR)
  113. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  114. swig_extra_generated_files
  115. "${swig_outdir}"
  116. "${infile}")
  117. SET(swig_generated_file_fullname
  118. "${swig_generated_file_fullname}/${swig_source_file_name_we}")
  119. # add the language into the name of the file (i.e. TCL_wrap)
  120. # this allows for the same .i file to be wrapped into different languages
  121. SET(swig_generated_file_fullname
  122. "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  123. IF(swig_source_file_cplusplus)
  124. SET(swig_generated_file_fullname
  125. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  126. ELSE(swig_source_file_cplusplus)
  127. SET(swig_generated_file_fullname
  128. "${swig_generated_file_fullname}.c")
  129. ENDIF(swig_source_file_cplusplus)
  130. #MESSAGE("Full path to source file: ${swig_source_file_fullname}")
  131. #MESSAGE("Full path to the output file: ${swig_generated_file_fullname}")
  132. GET_DIRECTORY_PROPERTY(cmake_include_directories INCLUDE_DIRECTORIES)
  133. SET(swig_include_dirs)
  134. FOREACH(it ${cmake_include_directories})
  135. SET(swig_include_dirs ${swig_include_dirs} "-I${it}")
  136. ENDFOREACH(it)
  137. SET(swig_special_flags)
  138. # default is c, so add c++ flag if it is c++
  139. IF(swig_source_file_cplusplus)
  140. SET(swig_special_flags ${swig_special_flags} "-c++")
  141. ENDIF(swig_source_file_cplusplus)
  142. SET(swig_extra_flags)
  143. IF(SWIG_MODULE_${name}_EXTRA_FLAGS)
  144. SET(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  145. ENDIF(SWIG_MODULE_${name}_EXTRA_FLAGS)
  146. ADD_CUSTOM_COMMAND(
  147. OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
  148. COMMAND "${SWIG_EXECUTABLE}"
  149. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  150. ${swig_source_file_flags}
  151. ${CMAKE_SWIG_FLAGS}
  152. -outdir ${swig_outdir}
  153. ${swig_special_flags}
  154. ${swig_extra_flags}
  155. ${swig_include_dirs}
  156. -o "${swig_generated_file_fullname}"
  157. "${swig_source_file_fullname}"
  158. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  159. DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
  160. COMMENT "Swig source")
  161. SET_SOURCE_FILES_PROPERTIES("${swig_generated_file_fullname}" ${swig_extra_generated_files}
  162. PROPERTIES GENERATED 1)
  163. SET(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
  164. ENDMACRO(SWIG_ADD_SOURCE_TO_MODULE)
  165. #
  166. # Create Swig module
  167. #
  168. MACRO(SWIG_ADD_MODULE name language)
  169. SWIG_MODULE_INITIALIZE(${name} ${language})
  170. SET(swig_dot_i_sources)
  171. SET(swig_other_sources)
  172. FOREACH(it ${ARGN})
  173. IF(${it} MATCHES ".*\\.i$")
  174. SET(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  175. ELSE(${it} MATCHES ".*\\.i$")
  176. SET(swig_other_sources ${swig_other_sources} "${it}")
  177. ENDIF(${it} MATCHES ".*\\.i$")
  178. ENDFOREACH(it)
  179. SET(swig_generated_sources)
  180. FOREACH(it ${swig_dot_i_sources})
  181. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  182. SET(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  183. ENDFOREACH(it)
  184. GET_DIRECTORY_PROPERTY(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  185. SET_DIRECTORY_PROPERTIES(PROPERTIES
  186. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  187. ADD_LIBRARY(${SWIG_MODULE_${name}_REAL_NAME}
  188. MODULE
  189. ${swig_generated_sources}
  190. ${swig_other_sources})
  191. SET_TARGET_PROPERTIES(${SWIG_MODULE_${name}_REAL_NAME}
  192. PROPERTIES PREFIX "")
  193. ENDMACRO(SWIG_ADD_MODULE)
  194. #
  195. # Like TARGET_LINK_LIBRARIES but for swig modules
  196. #
  197. MACRO(SWIG_LINK_LIBRARIES name)
  198. IF(SWIG_MODULE_${name}_REAL_NAME)
  199. TARGET_LINK_LIBRARIES(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  200. ELSE(SWIG_MODULE_${name}_REAL_NAME)
  201. MESSAGE(SEND_ERROR "Cannot find Swig library \"${name}\".")
  202. ENDIF(SWIG_MODULE_${name}_REAL_NAME)
  203. ENDMACRO(SWIG_LINK_LIBRARIES name)