UseSWIG.cmake 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #
  2. # SWIG module for CMake
  3. #
  4. # Defines the following macros:
  5. #
  6. # SWIG_ADD_MODULE(name language [ files ])
  7. # - Define swig module with given name and specified language
  8. #
  9. # SWIG_LINK_LIBRARIES(name [ libraries ])
  10. # - Link libraries to swig module
  11. #
  12. # All other macros are for internal use only.
  13. #
  14. # To get the actual name of the swig module, use: ${SWIG_MODULE_name_REAL_NAME}.
  15. # Set Source files propertis such as CPLUSPLUS and SWIG_FLAGS to specify
  16. # special behavior of SWIG. Also global CMAKE_SWIG_FLAGS can be used to add
  17. # special flags to all swig calls.
  18. # Another special variable is CMAKE_SWIG_OUTDIR, it allows one to specify
  19. # where to write all the swig generated module (swig -outdir option)
  20. SET(SWIG_CXX_EXTENSION "cxx")
  21. SET(SWIG_EXTRA_LIBRARIES "")
  22. SET(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
  23. #
  24. # For given swig module initialize variables associated with it
  25. #
  26. MACRO(SWIG_MODULE_INITIALIZE name language)
  27. STRING(TOUPPER "${language}" swig_uppercase_language)
  28. STRING(TOLOWER "${language}" swig_lowercase_language)
  29. SET(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  30. SET(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  31. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
  32. MESSAGE(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  33. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
  34. SET(SWIG_MODULE_${name}_REAL_NAME "${name}")
  35. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
  36. SET(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  37. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
  38. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
  39. SET(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  40. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
  41. ENDMACRO(SWIG_MODULE_INITIALIZE)
  42. #
  43. # For a given language, input file, and output file, determine extra files that
  44. # will be generated. This is internal swig macro.
  45. #
  46. MACRO(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  47. FOREACH(it ${SWIG_PYTHON_EXTRA_FILE_EXTENSION})
  48. SET(outfiles ${outfiles}
  49. "${generatedpath}/${infile}.${it}")
  50. ENDFOREACH(it)
  51. ENDMACRO(SWIG_GET_EXTRA_OUTPUT_FILES)
  52. #
  53. # Take swig (*.i) file and add proper custom commands for it
  54. #
  55. MACRO(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  56. SET(swig_full_infile ${infile})
  57. GET_FILENAME_COMPONENT(swig_source_file_path "${infile}" PATH)
  58. GET_FILENAME_COMPONENT(swig_source_file_name_we "${infile}" NAME_WE)
  59. GET_SOURCE_FILE_PROPERTY(swig_source_file_generated ${infile} GENERATED)
  60. GET_SOURCE_FILE_PROPERTY(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  61. GET_SOURCE_FILE_PROPERTY(swig_source_file_flags ${infile} SWIG_FLAGS)
  62. IF("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  63. SET(swig_source_file_flags "")
  64. ENDIF("${swig_source_file_flags}" STREQUAL "NOTFOUND")
  65. SET(swig_source_file_fullname "${infile}")
  66. IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  67. STRING(REGEX REPLACE
  68. "^${CMAKE_CURRENT_SOURCE_DIR}" ""
  69. swig_source_file_relative_path
  70. "${swig_source_file_path}")
  71. ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  72. IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  73. STRING(REGEX REPLACE
  74. "^${CMAKE_CURRENT_BINARY_DIR}" ""
  75. swig_source_file_relative_path
  76. "${swig_source_file_path}")
  77. SET(swig_source_file_generated 1)
  78. ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  79. SET(swig_source_file_relative_path "${swig_source_file_path}")
  80. IF(swig_source_file_generated)
  81. SET(swig_source_file_fullname "${CMAKE_CURRENT_BINARY_DIR}/${infile}")
  82. ELSE(swig_source_file_generated)
  83. SET(swig_source_file_fullname "${CMAKE_CURRENT_SOURCE_DIR}/${infile}")
  84. ENDIF(swig_source_file_generated)
  85. ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  86. ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  87. SET(swig_generated_file_fullname
  88. "${CMAKE_CURRENT_BINARY_DIR}")
  89. IF(swig_source_file_relative_path)
  90. SET(swig_generated_file_fullname
  91. "${swig_generated_file_fullname}/${swig_source_file_relative_path}")
  92. ENDIF(swig_source_file_relative_path)
  93. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  94. swig_extra_generated_files
  95. "${swig_generated_file_fullname}"
  96. "${swig_source_file_name_we}")
  97. SET(swig_generated_file_fullname
  98. "${swig_generated_file_fullname}/${swig_source_file_name_we}")
  99. # add the language into the name of the file (i.e. TCL_wrap)
  100. # this allows for the same .i file to be wrapped into different languages
  101. SET(swig_generated_file_fullname
  102. "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
  103. IF(swig_source_file_cplusplus)
  104. SET(swig_generated_file_fullname
  105. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  106. ELSE(swig_source_file_cplusplus)
  107. SET(swig_generated_file_fullname
  108. "${swig_generated_file_fullname}.c")
  109. ENDIF(swig_source_file_cplusplus)
  110. #MESSAGE("Full path to source file: ${swig_source_file_fullname}")
  111. #MESSAGE("Full path to the output file: ${swig_generated_file_fullname}")
  112. GET_DIRECTORY_PROPERTY(cmake_include_directories INCLUDE_DIRECTORIES)
  113. SET(swig_include_dirs)
  114. FOREACH(it ${cmake_include_directories})
  115. SET(swig_include_dirs ${swig_include_dirs} "-I${it}")
  116. ENDFOREACH(it)
  117. SET(swig_special_flags)
  118. # default is c, so add c++ flag if it is c++
  119. IF(swig_source_file_cplusplus)
  120. SET(swig_special_flags ${swig_special_flags} "-c++")
  121. ENDIF(swig_source_file_cplusplus)
  122. SET(swig_extra_flags)
  123. IF(SWIG_MODULE_${name}_EXTRA_FLAGS)
  124. SET(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  125. ENDIF(SWIG_MODULE_${name}_EXTRA_FLAGS)
  126. # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
  127. IF(CMAKE_SWIG_OUTDIR)
  128. ADD_CUSTOM_COMMAND(
  129. OUTPUT "${swig_generated_file_fullname}"
  130. COMMAND "${SWIG_EXECUTABLE}"
  131. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  132. ${swig_source_file_flags}
  133. ${CMAKE_SWIG_FLAGS}
  134. -outdir ${CMAKE_SWIG_OUTDIR}
  135. ${swig_special_flags}
  136. ${swig_extra_flags}
  137. ${swig_include_dirs}
  138. -o "${swig_generated_file_fullname}"
  139. "${swig_source_file_fullname}"
  140. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  141. COMMENT "Swig source")
  142. ELSE(CMAKE_SWIG_OUTDIR)
  143. ADD_CUSTOM_COMMAND(
  144. OUTPUT "${swig_generated_file_fullname}"
  145. COMMAND "${SWIG_EXECUTABLE}"
  146. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  147. ${swig_source_file_flags}
  148. ${CMAKE_SWIG_FLAGS}
  149. ${swig_outdir_flags}
  150. ${swig_special_flags}
  151. ${swig_extra_flags}
  152. ${swig_include_dirs}
  153. -o "${swig_generated_file_fullname}"
  154. "${swig_source_file_fullname}"
  155. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  156. COMMENT "Swig source")
  157. ENDIF(CMAKE_SWIG_OUTDIR)
  158. SET_SOURCE_FILES_PROPERTIES("${swig_generated_file_fullname}"
  159. PROPERTIES GENERATED 1)
  160. SET(${outfiles} "${swig_generated_file_fullname}")
  161. ENDMACRO(SWIG_ADD_SOURCE_TO_MODULE)
  162. #
  163. # Create Swig module
  164. #
  165. MACRO(SWIG_ADD_MODULE name language)
  166. SWIG_MODULE_INITIALIZE(${name} ${language})
  167. SET(swig_dot_i_sources)
  168. SET(swig_other_sources)
  169. FOREACH(it ${ARGN})
  170. IF(${it} MATCHES ".*\\.i$")
  171. SET(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  172. ELSE(${it} MATCHES ".*\\.i$")
  173. SET(swig_other_sources ${swig_other_sources} "${it}")
  174. ENDIF(${it} MATCHES ".*\\.i$")
  175. ENDFOREACH(it)
  176. SET(swig_generated_sources)
  177. FOREACH(it ${swig_dot_i_sources})
  178. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  179. SET(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  180. ENDFOREACH(it)
  181. GET_DIRECTORY_PROPERTY(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
  182. SET_DIRECTORY_PROPERTIES(PROPERTIES
  183. ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
  184. ADD_LIBRARY(${SWIG_MODULE_${name}_REAL_NAME}
  185. MODULE
  186. ${swig_generated_sources}
  187. ${swig_other_sources})
  188. SET_TARGET_PROPERTIES(${SWIG_MODULE_${name}_REAL_NAME}
  189. PROPERTIES PREFIX "")
  190. ENDMACRO(SWIG_ADD_MODULE)
  191. #
  192. # Like TARGET_LINK_LIBRARIES but for swig modules
  193. #
  194. MACRO(SWIG_LINK_LIBRARIES name)
  195. IF(SWIG_MODULE_${name}_REAL_NAME)
  196. TARGET_LINK_LIBRARIES(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  197. ELSE(SWIG_MODULE_${name}_REAL_NAME)
  198. MESSAGE(SEND_ERROR "Cannot find Swig library \"${name}\".")
  199. ENDIF(SWIG_MODULE_${name}_REAL_NAME)
  200. ENDMACRO(SWIG_LINK_LIBRARIES name)