2 # SWIG module for CMake
4 # Defines the following macros:
6 # SWIG_ADD_MODULE(name language [ files ])
7 # - Define swig module with given name and specified language
9 # SWIG_LINK_LIBRARIES(name [ libraries ])
10 # - Link libraries to swig module
12 # All other macros are for internal use only.
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.
19 SET(SWIG_CXX_EXTENSION "cxx")
20 SET(SWIG_EXTRA_LIBRARIES "")
22 SET(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
25 # For given swig module initialize variables associated with it
27 MACRO(SWIG_MODULE_INITIALIZE name language)
28 STRING(TOUPPER "${language}" swig_uppercase_language)
29 STRING(TOLOWER "${language}" swig_lowercase_language)
30 SET(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
31 SET(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
33 IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
34 MESSAGE(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
35 ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xUNKNOWNx$")
37 SET(SWIG_MODULE_${name}_REAL_NAME "${name}")
38 IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
39 SET(SWIG_MODULE_${name}_REAL_NAME "_${name}")
40 ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPYTHONx$")
41 IF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
42 SET(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
43 ENDIF("x${SWIG_MODULE_${name}_LANGUAGE}x" MATCHES "^xPERLx$")
44 ENDMACRO(SWIG_MODULE_INITIALIZE)
47 # For a given language, input file, and output file, determine extra files that
48 # will be generated. This is internal swig macro.
51 MACRO(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
52 FOREACH(it ${SWIG_PYTHON_EXTRA_FILE_EXTENSION})
53 SET(outfiles ${outfiles}
54 "${generatedpath}/${infile}.${it}")
56 ENDMACRO(SWIG_GET_EXTRA_OUTPUT_FILES)
59 # Take swig (*.i) file and add proper custom commands for it
61 MACRO(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
62 SET(swig_full_infile ${infile})
63 GET_FILENAME_COMPONENT(swig_source_file_path "${infile}" PATH)
64 GET_FILENAME_COMPONENT(swig_source_file_name_we "${infile}" NAME_WE)
65 GET_SOURCE_FILE_PROPERTY(swig_source_file_generated ${infile} GENERATED)
66 GET_SOURCE_FILE_PROPERTY(swig_source_file_cplusplus ${infile} CPLUSPLUS)
67 GET_SOURCE_FILE_PROPERTY(swig_source_file_flags ${infile} SWIG_FLAGS)
68 SET(swig_source_file_fullname "${infile}")
69 IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
71 "^${CMAKE_CURRENT_SOURCE_DIR}" ""
72 swig_source_file_relative_path
73 "${swig_source_file_path}")
74 ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
75 IF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
77 "^${CMAKE_CURRENT_BINARY_DIR}" ""
78 swig_source_file_relative_path
79 "${swig_source_file_path}")
80 SET(swig_source_file_generated 1)
81 ELSE(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
82 SET(swig_source_file_relative_path "${swig_source_file_path}")
83 IF(swig_source_file_generated)
84 SET(swig_source_file_fullname "${CMAKE_CURRENT_BINARY_DIR}/${infile}")
85 ELSE(swig_source_file_generated)
86 SET(swig_source_file_fullname "${CMAKE_CURRENT_SOURCE_DIR}/${infile}")
87 ENDIF(swig_source_file_generated)
88 ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
89 ENDIF(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
91 SET(swig_generated_file_fullname
92 "${CMAKE_CURRENT_BINARY_DIR}")
93 IF(swig_source_file_relative_path)
94 SET(swig_generated_file_fullname
95 "${swig_generated_file_fullname}/${swig_source_file_relative_path}")
96 ENDIF(swig_source_file_relative_path)
97 SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
98 swig_extra_generated_files
99 "${swig_generated_file_fullname}"
100 "${swig_source_file_name_we}")
101 SET(swig_generated_file_fullname
102 "${swig_generated_file_fullname}/${swig_source_file_name_we}")
103 # add the language into the name of the file (i.e. TCL_wrap)
104 # this allows for the same .i file to be wrapped into different languages
105 SET(swig_generated_file_fullname
106 "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
108 IF(swig_source_file_cplusplus)
109 SET(swig_generated_file_fullname
110 "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
111 ELSE(swig_source_file_cplusplus)
112 SET(swig_generated_file_fullname
113 "${swig_generated_file_fullname}.c")
114 ENDIF(swig_source_file_cplusplus)
116 #MESSAGE("Full path to source file: ${swig_source_file_fullname}")
117 #MESSAGE("Full path to the output file: ${swig_generated_file_fullname}")
118 GET_DIRECTORY_PROPERTY(cmake_include_directories INCLUDE_DIRECTORIES)
119 SET(swig_include_dirs)
120 FOREACH(it ${cmake_include_directories})
121 SET(swig_include_dirs ${swig_include_dirs} "-I${it}")
124 SET(swig_special_flags)
125 IF(swig_source_file_cplusplus)
126 SET(swig_special_flags ${swig_special_flags} "-c++")
127 ELSE(swig_source_file_cplusplus)
128 SET(swig_special_flags ${swig_special_flags} "-c")
129 ENDIF(swig_source_file_cplusplus)
130 SET(swig_extra_flags)
131 IF(SWIG_MODULE_${name}_EXTRA_FLAGS)
132 SET(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
133 ENDIF(SWIG_MODULE_${name}_EXTRA_FLAGS)
135 OUTPUT "${swig_generated_file_fullname}"
136 COMMAND "${SWIG_EXECUTABLE}"
137 ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
138 ${swig_source_file_flags}
140 ${swig_special_flags}
143 -o "${swig_generated_file_fullname}"
144 "${swig_source_file_fullname}"
145 MAIN_DEPENDENCY "${swig_source_file_fullname}"
146 COMMENT "Swig source")
147 SET_SOURCE_FILES_PROPERTIES("${swig_generated_file_fullname}"
148 PROPERTIES GENERATED 1)
149 SET(${outfiles} "${swig_generated_file_fullname}")
150 ENDMACRO(SWIG_ADD_SOURCE_TO_MODULE)
155 MACRO(SWIG_ADD_MODULE name language)
156 SWIG_MODULE_INITIALIZE(${name} ${language})
157 SET(swig_dot_i_sources)
158 SET(swig_other_sources)
160 IF(${it} MATCHES ".*\\.i$")
161 SET(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
162 ELSE(${it} MATCHES ".*\\.i$")
163 SET(swig_other_sources ${swig_other_sources} "${it}")
164 ENDIF(${it} MATCHES ".*\\.i$")
167 SET(swig_generated_sources)
168 FOREACH(it ${swig_dot_i_sources})
169 SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
170 SET(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
172 SET_DIRECTORY_PROPERTIES(PROPERTIES
173 ADDITIONAL_MAKE_CLEAN_FILES "${ADDITIONAL_MAKE_CLEAN_FILES};${swig_generated_sources}")
174 ADD_LIBRARY(${SWIG_MODULE_${name}_REAL_NAME}
176 ${swig_generated_sources}
177 ${swig_other_sources})
178 SET_TARGET_PROPERTIES(${SWIG_MODULE_${name}_REAL_NAME}
179 PROPERTIES PREFIX "")
180 ENDMACRO(SWIG_ADD_MODULE)
183 # Like TARGET_LINK_LIBRARIES but for swig modules
185 MACRO(SWIG_LINK_LIBRARIES name)
186 IF(SWIG_MODULE_${name}_REAL_NAME)
187 TARGET_LINK_LIBRARIES(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
188 ELSE(SWIG_MODULE_${name}_REAL_NAME)
189 MESSAGE(SEND_ERROR "Cannot find Swig library \"${name}\".")
190 ENDIF(SWIG_MODULE_${name}_REAL_NAME)
191 ENDMACRO(SWIG_LINK_LIBRARIES name)