UseSWIG.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. SET(SWIG_CXX_EXTENSION "cxx")
  2. SET(SWIG_EXTRA_LIBRARIES "")
  3. SET(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
  4. #
  5. # For given swig module initialize variables associated with it
  6. #
  7. MACRO(SWIG_MODULE_INITIALIZE name language)
  8. STRING(TOUPPER "${language}" swig_uppercase_language)
  9. STRING(TOLOWER "${language}" swig_lowercase_language)
  10. SET(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
  11. SET(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
  12. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
  13. MESSAGE(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
  14. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
  15. SET(SWIG_MODULE_${name}_REAL_NAME "${name}")
  16. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
  17. SET(SWIG_MODULE_${name}_REAL_NAME "_${name}")
  18. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
  19. IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
  20. SET(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
  21. ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
  22. ENDMACRO(SWIG_MODULE_INITIALIZE)
  23. #
  24. # For a given language, input file, and output file, determine extra files that
  25. # will be generated
  26. #
  27. MACRO(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
  28. FOREACH(it ${SWIG_PYTHON_EXTRA_FILE_EXTENSION})
  29. SET(outfiles ${outfiles}
  30. "${generatedpath}/${infile}.${it}")
  31. ENDFOREACH(it)
  32. ENDMACRO(SWIG_GET_EXTRA_OUTPUT_FILES)
  33. #
  34. # Take swig (*.i) file and add proper custom commands for it
  35. #
  36. MACRO(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
  37. SET(swig_full_infile ${infile})
  38. GET_FILENAME_COMPONENT(swig_source_file_path "${infile}" PATH)
  39. GET_FILENAME_COMPONENT(swig_source_file_name_we "${infile}" NAME_WE)
  40. GET_SOURCE_FILE_PROPERTY(swig_source_file_generated ${infile} GENERATED)
  41. GET_SOURCE_FILE_PROPERTY(swig_source_file_cplusplus ${infile} CPLUSPLUS)
  42. SET(swig_source_file_fullname "${infile}")
  43. IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  44. STRING(REGEX REPLACE
  45. "^${CMAKE_CURRENT_SOURCE_DIR}" ""
  46. swig_source_file_relative_path
  47. "${swig_source_file_path}")
  48. ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  49. IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  50. STRING(REGEX REPLACE
  51. "^${CMAKE_CURRENT_BINARY_DIR}" ""
  52. swig_source_file_relative_path
  53. "${swig_source_file_path}")
  54. SET(swig_source_file_generated 1)
  55. ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  56. SET(swig_source_file_relative_path "${swig_source_file_path}")
  57. IF(swig_source_file_generated)
  58. SET(swig_source_file_fullname "${CMAKE_CURRENT_BINARY_DIR}/${infile}")
  59. ELSE(swig_source_file_generated)
  60. SET(swig_source_file_fullname "${CMAKE_CURRENT_SOURCE_DIR}/${infile}")
  61. ENDIF(swig_source_file_generated)
  62. ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
  63. ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
  64. SET(swig_generated_file_fullname
  65. "${CMAKE_CURRENT_BINARY_DIR}")
  66. IF(swig_source_file_relative_path)
  67. SET(swig_generated_file_fullname
  68. "${swig_generated_file_fullname}/${swig_source_file_relative_path}")
  69. ENDIF(swig_source_file_relative_path)
  70. SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
  71. swig_extra_generated_files
  72. "${swig_generated_file_fullname}"
  73. "${swig_source_file_name_we}")
  74. SET(swig_generated_file_fullname
  75. "${swig_generated_file_fullname}/${swig_source_file_name_we}")
  76. SET(swig_generated_file_fullname
  77. "${swig_generated_file_fullname}_wrap")
  78. IF(swig_source_file_cplusplus)
  79. SET(swig_generated_file_fullname
  80. "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
  81. ELSE(swig_source_file_cplusplus)
  82. SET(swig_generated_file_fullname
  83. "${swig_generated_file_fullname}.c")
  84. ENDIF(swig_source_file_cplusplus)
  85. #MESSAGE("Full path to source file: ${swig_source_file_fullname}")
  86. #MESSAGE("Full path to the output file: ${swig_generated_file_fullname}")
  87. GET_DIRECTORY_PROPERTY(cmake_include_directories INCLUDE_DIRECTORIES)
  88. SET(swig_include_dirs)
  89. FOREACH(it ${cmake_include_directories})
  90. SET(swig_include_dirs ${swig_include_dirs} "-I${it}")
  91. ENDFOREACH(it)
  92. SET(swig_special_flags)
  93. IF(swig_source_file_cplusplus)
  94. SET(swig_special_flags ${swig_special_flags} "-c++")
  95. ELSE(swig_source_file_cplusplus)
  96. SET(swig_special_flags ${swig_special_flags} "-c")
  97. ENDIF(swig_source_file_cplusplus)
  98. SET(swig_extra_flags)
  99. IF(SWIG_MODULE_${name}_EXTRA_FLAGS)
  100. SET(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
  101. ENDIF(SWIG_MODULE_${name}_EXTRA_FLAGS)
  102. ADD_CUSTOM_COMMAND(
  103. OUTPUT "${swig_generated_file_fullname}"
  104. COMMAND "${SWIG_EXECUTABLE}"
  105. ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
  106. ${swig_special_flags}
  107. ${swig_extra_flags}
  108. ${swig_include_dirs}
  109. -o "${swig_generated_file_fullname}"
  110. "${swig_source_file_fullname}"
  111. MAIN_DEPENDENCY "${swig_source_file_fullname}"
  112. COMMENT "Swig source")
  113. SET_SOURCE_FILES_PROPERTIES("${swig_generated_file_fullname}"
  114. PROPERTIES GENERATED 1)
  115. SET(${outfiles} "${swig_generated_file_fullname}")
  116. ENDMACRO(SWIG_ADD_SOURCE_TO_MODULE)
  117. #
  118. # Create Swig module
  119. #
  120. MACRO(SWIG_ADD_MODULE name language)
  121. SWIG_MODULE_INITIALIZE(${name} ${language})
  122. SET(swig_dot_i_sources)
  123. SET(swig_other_sources)
  124. FOREACH(it ${ARGN})
  125. IF(${it} MATCHES ".*\\.i$")
  126. SET(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
  127. ELSE(${it} MATCHES ".*\\.i$")
  128. SET(swig_other_sources ${swig_other_sources} "${it}")
  129. ENDIF(${it} MATCHES ".*\\.i$")
  130. ENDFOREACH(it)
  131. SET(swig_generated_sources)
  132. FOREACH(it ${swig_dot_i_sources})
  133. SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
  134. SET(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
  135. ENDFOREACH(it)
  136. ADD_LIBRARY(${SWIG_MODULE_${name}_REAL_NAME}
  137. MODULE
  138. ${swig_generated_sources}
  139. ${swig_other_sources})
  140. SET_TARGET_PROPERTIES(${SWIG_MODULE_${name}_REAL_NAME}
  141. PROPERTIES PREFIX "")
  142. ENDMACRO(SWIG_ADD_MODULE)
  143. #
  144. # Like TARGET_LINK_LIBRARIES but for swig modules
  145. #
  146. MACRO(SWIG_LINK_LIBRARIES name)
  147. IF(SWIG_MODULE_${name}_REAL_NAME)
  148. TARGET_LINK_LIBRARIES(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
  149. ELSE(SWIG_MODULE_${name}_REAL_NAME)
  150. MESSAGE(SEND_ERROR "Cannot find Swig library \"${name}\".")
  151. ENDIF(SWIG_MODULE_${name}_REAL_NAME)
  152. ENDMACRO(SWIG_LINK_LIBRARIES name)