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