UseSWIG.cmake 7.6 KB

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