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 # The name-specific variable SWIG_MODULE_<name>_EXTRA_DEPS may be used
16 # to specify extra dependencies for the generated modules.
17 # If the source file generated by swig need some special flag you can use
18 # set_source_files_properties( ${swig_generated_file_fullname}
19 # PROPERTIES COMPILE_FLAGS "-bla")
22 #=============================================================================
23 # Copyright 2004-2009 Kitware, Inc.
24 # Copyright 2009 Mathieu Malaterre <mathieu.malaterre@gmail.com>
26 # Distributed under the OSI-approved BSD License (the "License");
27 # see accompanying file Copyright.txt for details.
29 # This software is distributed WITHOUT ANY WARRANTY; without even the
30 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31 # See the License for more information.
32 #=============================================================================
33 # (To distribute this file outside of CMake, substitute the full
34 # License text for the above reference.)
36 set(SWIG_CXX_EXTENSION "cxx")
37 set(SWIG_EXTRA_LIBRARIES "")
39 set(SWIG_PYTHON_EXTRA_FILE_EXTENSION "py")
42 # For given swig module initialize variables associated with it
44 macro(SWIG_MODULE_INITIALIZE name language)
45 string(TOUPPER "${language}" swig_uppercase_language)
46 string(TOLOWER "${language}" swig_lowercase_language)
47 set(SWIG_MODULE_${name}_LANGUAGE "${swig_uppercase_language}")
48 set(SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG "${swig_lowercase_language}")
50 set(SWIG_MODULE_${name}_REAL_NAME "${name}")
51 if("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "UNKNOWN")
52 message(FATAL_ERROR "SWIG Error: Language \"${language}\" not found")
53 elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PYTHON")
54 # when swig is used without the -interface it will produce in the module.py
55 # a 'import _modulename' statement, which implies having a corresponding
56 # _modulename.so (*NIX), _modulename.pyd (Win32).
57 set(SWIG_MODULE_${name}_REAL_NAME "_${name}")
58 elseif("${SWIG_MODULE_${name}_LANGUAGE}" STREQUAL "PERL")
59 set(SWIG_MODULE_${name}_EXTRA_FLAGS "-shadow")
64 # For a given language, input file, and output file, determine extra files that
65 # will be generated. This is internal swig macro.
68 macro(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
70 get_source_file_property(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename
71 ${infile} SWIG_MODULE_NAME)
72 if(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename STREQUAL "NOTFOUND")
73 get_filename_component(SWIG_GET_EXTRA_OUTPUT_FILES_module_basename "${infile}" NAME_WE)
75 foreach(it ${SWIG_${language}_EXTRA_FILE_EXTENSION})
76 set(${outfiles} ${${outfiles}}
77 "${generatedpath}/${SWIG_GET_EXTRA_OUTPUT_FILES_module_basename}.${it}")
82 # Take swig (*.i) file and add proper custom commands for it
84 macro(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
85 set(swig_full_infile ${infile})
86 get_filename_component(swig_source_file_path "${infile}" PATH)
87 get_filename_component(swig_source_file_name_we "${infile}" NAME_WE)
88 get_source_file_property(swig_source_file_generated ${infile} GENERATED)
89 get_source_file_property(swig_source_file_cplusplus ${infile} CPLUSPLUS)
90 get_source_file_property(swig_source_file_flags ${infile} SWIG_FLAGS)
91 if("${swig_source_file_flags}" STREQUAL "NOTFOUND")
92 set(swig_source_file_flags "")
94 set(swig_source_file_fullname "${infile}")
95 if(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_SOURCE_DIR}")
97 "^${CMAKE_CURRENT_SOURCE_DIR}" ""
98 swig_source_file_relative_path
99 "${swig_source_file_path}")
101 if(${swig_source_file_path} MATCHES "^${CMAKE_CURRENT_BINARY_DIR}")
103 "^${CMAKE_CURRENT_BINARY_DIR}" ""
104 swig_source_file_relative_path
105 "${swig_source_file_path}")
106 set(swig_source_file_generated 1)
108 set(swig_source_file_relative_path "${swig_source_file_path}")
109 if(swig_source_file_generated)
110 set(swig_source_file_fullname "${CMAKE_CURRENT_BINARY_DIR}/${infile}")
112 set(swig_source_file_fullname "${CMAKE_CURRENT_SOURCE_DIR}/${infile}")
117 set(swig_generated_file_fullname
118 "${CMAKE_CURRENT_BINARY_DIR}")
119 if(swig_source_file_relative_path)
120 set(swig_generated_file_fullname
121 "${swig_generated_file_fullname}/${swig_source_file_relative_path}")
123 # If CMAKE_SWIG_OUTDIR was specified then pass it to -outdir
124 if(CMAKE_SWIG_OUTDIR)
125 set(swig_outdir ${CMAKE_SWIG_OUTDIR})
127 set(swig_outdir ${CMAKE_CURRENT_BINARY_DIR})
129 SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
130 swig_extra_generated_files
133 set(swig_generated_file_fullname
134 "${swig_generated_file_fullname}/${swig_source_file_name_we}")
135 # add the language into the name of the file (i.e. TCL_wrap)
136 # this allows for the same .i file to be wrapped into different languages
137 set(swig_generated_file_fullname
138 "${swig_generated_file_fullname}${SWIG_MODULE_${name}_LANGUAGE}_wrap")
140 if(swig_source_file_cplusplus)
141 set(swig_generated_file_fullname
142 "${swig_generated_file_fullname}.${SWIG_CXX_EXTENSION}")
144 set(swig_generated_file_fullname
145 "${swig_generated_file_fullname}.c")
148 #message("Full path to source file: ${swig_source_file_fullname}")
149 #message("Full path to the output file: ${swig_generated_file_fullname}")
150 get_directory_property(cmake_include_directories INCLUDE_DIRECTORIES)
151 set(swig_include_dirs)
152 foreach(it ${cmake_include_directories})
153 set(swig_include_dirs ${swig_include_dirs} "-I${it}")
156 set(swig_special_flags)
157 # default is c, so add c++ flag if it is c++
158 if(swig_source_file_cplusplus)
159 set(swig_special_flags ${swig_special_flags} "-c++")
161 set(swig_extra_flags)
162 if(SWIG_MODULE_${name}_EXTRA_FLAGS)
163 set(swig_extra_flags ${swig_extra_flags} ${SWIG_MODULE_${name}_EXTRA_FLAGS})
166 OUTPUT "${swig_generated_file_fullname}" ${swig_extra_generated_files}
167 # Let's create the ${swig_outdir} at execution time, in case dir contains $(OutDir)
168 COMMAND ${CMAKE_COMMAND} -E make_directory ${swig_outdir}
169 COMMAND "${SWIG_EXECUTABLE}"
170 ARGS "-${SWIG_MODULE_${name}_SWIG_LANGUAGE_FLAG}"
171 ${swig_source_file_flags}
173 -outdir ${swig_outdir}
174 ${swig_special_flags}
177 -o "${swig_generated_file_fullname}"
178 "${swig_source_file_fullname}"
179 MAIN_DEPENDENCY "${swig_source_file_fullname}"
180 DEPENDS ${SWIG_MODULE_${name}_EXTRA_DEPS}
181 COMMENT "Swig source")
182 set_source_files_properties("${swig_generated_file_fullname}" ${swig_extra_generated_files}
183 PROPERTIES GENERATED 1)
184 set(${outfiles} "${swig_generated_file_fullname}" ${swig_extra_generated_files})
190 macro(SWIG_ADD_MODULE name language)
191 SWIG_MODULE_INITIALIZE(${name} ${language})
192 set(swig_dot_i_sources)
193 set(swig_other_sources)
195 if(${it} MATCHES ".*\\.i$")
196 set(swig_dot_i_sources ${swig_dot_i_sources} "${it}")
198 set(swig_other_sources ${swig_other_sources} "${it}")
202 set(swig_generated_sources)
203 foreach(it ${swig_dot_i_sources})
204 SWIG_ADD_SOURCE_TO_MODULE(${name} swig_generated_source ${it})
205 set(swig_generated_sources ${swig_generated_sources} "${swig_generated_source}")
207 get_directory_property(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES)
208 set_directory_properties(PROPERTIES
209 ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources}")
210 add_library(${SWIG_MODULE_${name}_REAL_NAME}
212 ${swig_generated_sources}
213 ${swig_other_sources})
214 string(TOLOWER "${language}" swig_lowercase_language)
215 if ("${swig_lowercase_language}" STREQUAL "java")
218 # System.loadLibrary("LIBRARY");
219 # then JNI will look for a library whose name is platform dependent, namely
220 # MacOS : libLIBRARY.jnilib
221 # Windows: LIBRARY.dll
222 # Linux : libLIBRARY.so
223 set_target_properties (${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".jnilib")
226 if ("${swig_lowercase_language}" STREQUAL "python")
227 # this is only needed for the python case where a _modulename.so is generated
228 set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES PREFIX "")
229 # Python extension modules on Windows must have the extension ".pyd"
230 # instead of ".dll" as of Python 2.5. Older python versions do support
232 # http://docs.python.org/whatsnew/ports.html#SECTION0001510000000000000000
234 # Windows: .dll is no longer supported as a filename extension for extension modules.
235 # .pyd is now the only filename extension that will be searched for.
237 if(WIN32 AND NOT CYGWIN)
238 set_target_properties(${SWIG_MODULE_${name}_REAL_NAME} PROPERTIES SUFFIX ".pyd")
244 # Like TARGET_LINK_LIBRARIES but for swig modules
246 macro(SWIG_LINK_LIBRARIES name)
247 if(SWIG_MODULE_${name}_REAL_NAME)
248 target_link_libraries(${SWIG_MODULE_${name}_REAL_NAME} ${ARGN})
250 message(SEND_ERROR "Cannot find Swig library \"${name}\".")