UseSWIG.cmake 8.4 KB

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