Mercurial > hg > CbC > CbC_llvm
comparison cmake/modules/AddLLVM.cmake @ 0:95c75e76d11b LLVM3.4
LLVM 3.4
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 12 Dec 2013 13:56:28 +0900 |
parents | |
children | e4204d083e25 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:95c75e76d11b |
---|---|
1 include(LLVMParseArguments) | |
2 include(LLVMProcessSources) | |
3 include(LLVM-Config) | |
4 | |
5 macro(add_llvm_library name) | |
6 llvm_process_sources( ALL_FILES ${ARGN} ) | |
7 add_library( ${name} ${ALL_FILES} ) | |
8 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} ) | |
9 if( LLVM_COMMON_DEPENDS ) | |
10 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) | |
11 endif( LLVM_COMMON_DEPENDS ) | |
12 | |
13 if( BUILD_SHARED_LIBS ) | |
14 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) | |
15 if (MSVC) | |
16 set_target_properties(${name} | |
17 PROPERTIES | |
18 IMPORT_SUFFIX ".imp") | |
19 endif () | |
20 endif() | |
21 | |
22 # Ensure that the system libraries always comes last on the | |
23 # list. Without this, linking the unit tests on MinGW fails. | |
24 link_system_libs( ${name} ) | |
25 | |
26 if( EXCLUDE_FROM_ALL ) | |
27 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) | |
28 else() | |
29 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO") | |
30 install(TARGETS ${name} | |
31 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} | |
32 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) | |
33 endif() | |
34 endif() | |
35 set_target_properties(${name} PROPERTIES FOLDER "Libraries") | |
36 | |
37 # Add the explicit dependency information for this library. | |
38 # | |
39 # It would be nice to verify that we have the dependencies for this library | |
40 # name, but using get_property(... SET) doesn't suffice to determine if a | |
41 # property has been set to an empty value. | |
42 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name}) | |
43 target_link_libraries(${name} ${lib_deps}) | |
44 endmacro(add_llvm_library name) | |
45 | |
46 macro(add_llvm_loadable_module name) | |
47 if( NOT LLVM_ON_UNIX OR CYGWIN ) | |
48 message(STATUS "Loadable modules not supported on this platform. | |
49 ${name} ignored.") | |
50 # Add empty "phony" target | |
51 add_custom_target(${name}) | |
52 else() | |
53 llvm_process_sources( ALL_FILES ${ARGN} ) | |
54 if (MODULE) | |
55 set(libkind MODULE) | |
56 else() | |
57 set(libkind SHARED) | |
58 endif() | |
59 | |
60 add_library( ${name} ${libkind} ${ALL_FILES} ) | |
61 set_target_properties( ${name} PROPERTIES PREFIX "" ) | |
62 | |
63 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) | |
64 link_system_libs( ${name} ) | |
65 | |
66 if (APPLE) | |
67 # Darwin-specific linker flags for loadable modules. | |
68 set_target_properties(${name} PROPERTIES | |
69 LINK_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress") | |
70 endif() | |
71 | |
72 if( EXCLUDE_FROM_ALL ) | |
73 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) | |
74 else() | |
75 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) | |
76 install(TARGETS ${name} | |
77 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} | |
78 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) | |
79 endif() | |
80 endif() | |
81 endif() | |
82 | |
83 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules") | |
84 endmacro(add_llvm_loadable_module name) | |
85 | |
86 | |
87 macro(add_llvm_executable name) | |
88 llvm_process_sources( ALL_FILES ${ARGN} ) | |
89 if( EXCLUDE_FROM_ALL ) | |
90 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES}) | |
91 else() | |
92 add_executable(${name} ${ALL_FILES}) | |
93 endif() | |
94 set(EXCLUDE_FROM_ALL OFF) | |
95 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) | |
96 if( LLVM_COMMON_DEPENDS ) | |
97 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) | |
98 endif( LLVM_COMMON_DEPENDS ) | |
99 link_system_libs( ${name} ) | |
100 endmacro(add_llvm_executable name) | |
101 | |
102 | |
103 set (LLVM_TOOLCHAIN_TOOLS | |
104 llvm-ar | |
105 llvm-objdump | |
106 ) | |
107 | |
108 macro(add_llvm_tool name) | |
109 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR}) | |
110 if( NOT LLVM_BUILD_TOOLS ) | |
111 set(EXCLUDE_FROM_ALL ON) | |
112 endif() | |
113 add_llvm_executable(${name} ${ARGN}) | |
114 | |
115 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL) | |
116 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) | |
117 if( LLVM_BUILD_TOOLS ) | |
118 install(TARGETS ${name} RUNTIME DESTINATION bin) | |
119 endif() | |
120 endif() | |
121 set_target_properties(${name} PROPERTIES FOLDER "Tools") | |
122 endmacro(add_llvm_tool name) | |
123 | |
124 | |
125 macro(add_llvm_example name) | |
126 # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR}) | |
127 if( NOT LLVM_BUILD_EXAMPLES ) | |
128 set(EXCLUDE_FROM_ALL ON) | |
129 endif() | |
130 add_llvm_executable(${name} ${ARGN}) | |
131 if( LLVM_BUILD_EXAMPLES ) | |
132 install(TARGETS ${name} RUNTIME DESTINATION examples) | |
133 endif() | |
134 set_target_properties(${name} PROPERTIES FOLDER "Examples") | |
135 endmacro(add_llvm_example name) | |
136 | |
137 | |
138 macro(add_llvm_utility name) | |
139 add_llvm_executable(${name} ${ARGN}) | |
140 set_target_properties(${name} PROPERTIES FOLDER "Utils") | |
141 endmacro(add_llvm_utility name) | |
142 | |
143 | |
144 macro(add_llvm_target target_name) | |
145 include_directories(BEFORE | |
146 ${CMAKE_CURRENT_BINARY_DIR} | |
147 ${CMAKE_CURRENT_SOURCE_DIR}) | |
148 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT}) | |
149 set( CURRENT_LLVM_TARGET LLVM${target_name} ) | |
150 endmacro(add_llvm_target) | |
151 | |
152 # Add external project that may want to be built as part of llvm such as Clang, | |
153 # lld, and Polly. This adds two options. One for the source directory of the | |
154 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to | |
155 # enable or disable building it with everthing else. | |
156 # Additional parameter can be specified as the name of directory. | |
157 macro(add_llvm_external_project name) | |
158 set(add_llvm_external_dir "${ARGN}") | |
159 if("${add_llvm_external_dir}" STREQUAL "") | |
160 set(add_llvm_external_dir ${name}) | |
161 endif() | |
162 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}") | |
163 string(REPLACE "-" "_" nameUNDERSCORE ${name}) | |
164 string(TOUPPER ${nameUNDERSCORE} nameUPPER) | |
165 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}" | |
166 CACHE PATH "Path to ${name} source directory") | |
167 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL "" | |
168 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt) | |
169 option(LLVM_EXTERNAL_${nameUPPER}_BUILD | |
170 "Whether to build ${name} as part of LLVM" ON) | |
171 if (LLVM_EXTERNAL_${nameUPPER}_BUILD) | |
172 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir}) | |
173 endif() | |
174 endif() | |
175 endmacro(add_llvm_external_project) | |
176 | |
177 macro(add_llvm_tool_subdirectory name) | |
178 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}") | |
179 add_subdirectory(${name}) | |
180 endmacro(add_llvm_tool_subdirectory) | |
181 | |
182 macro(ignore_llvm_tool_subdirectory name) | |
183 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}") | |
184 endmacro(ignore_llvm_tool_subdirectory) | |
185 | |
186 function(add_llvm_implicit_external_projects) | |
187 set(list_of_implicit_subdirs "") | |
188 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*") | |
189 foreach(dir ${sub-dirs}) | |
190 if(IS_DIRECTORY "${dir}") | |
191 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore) | |
192 if( tool_subdir_ignore EQUAL -1 | |
193 AND EXISTS "${dir}/CMakeLists.txt") | |
194 get_filename_component(fn "${dir}" NAME) | |
195 list(APPEND list_of_implicit_subdirs "${fn}") | |
196 endif() | |
197 endif() | |
198 endforeach() | |
199 | |
200 foreach(external_proj ${list_of_implicit_subdirs}) | |
201 add_llvm_external_project("${external_proj}") | |
202 endforeach() | |
203 endfunction(add_llvm_implicit_external_projects) | |
204 | |
205 # Generic support for adding a unittest. | |
206 function(add_unittest test_suite test_name) | |
207 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) | |
208 if( NOT LLVM_BUILD_TESTS ) | |
209 set(EXCLUDE_FROM_ALL ON) | |
210 endif() | |
211 | |
212 add_llvm_executable(${test_name} ${ARGN}) | |
213 target_link_libraries(${test_name} | |
214 gtest | |
215 gtest_main | |
216 LLVMSupport # gtest needs it for raw_ostream. | |
217 ) | |
218 | |
219 add_dependencies(${test_suite} ${test_name}) | |
220 get_target_property(test_suite_folder ${test_suite} FOLDER) | |
221 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND") | |
222 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}") | |
223 endif () | |
224 | |
225 # Visual Studio 2012 only supports up to 8 template parameters in | |
226 # std::tr1::tuple by default, but gtest requires 10 | |
227 if (MSVC AND MSVC_VERSION EQUAL 1700) | |
228 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10) | |
229 endif () | |
230 | |
231 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include) | |
232 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0) | |
233 if (NOT LLVM_ENABLE_THREADS) | |
234 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0) | |
235 endif () | |
236 | |
237 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS) | |
238 if (LLVM_COMPILER_IS_GCC_COMPATIBLE) | |
239 set(target_compile_flags "${target_compile_flags} -fno-rtti") | |
240 elseif (MSVC) | |
241 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-") | |
242 endif () | |
243 | |
244 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG) | |
245 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros") | |
246 endif () | |
247 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}") | |
248 endfunction() | |
249 | |
250 # This function provides an automatic way to 'configure'-like generate a file | |
251 # based on a set of common and custom variables, specifically targetting the | |
252 # variables needed for the 'lit.site.cfg' files. This function bundles the | |
253 # common variables that any Lit instance is likely to need, and custom | |
254 # variables can be passed in. | |
255 function(configure_lit_site_cfg input output) | |
256 foreach(c ${LLVM_TARGETS_TO_BUILD}) | |
257 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}") | |
258 endforeach(c) | |
259 set(TARGETS_TO_BUILD ${TARGETS_BUILT}) | |
260 | |
261 set(SHLIBEXT "${LTDL_SHLIB_EXT}") | |
262 set(SHLIBDIR "${LLVM_BINARY_DIR}/lib/${CMAKE_CFG_INTDIR}") | |
263 | |
264 if(BUILD_SHARED_LIBS) | |
265 set(LLVM_SHARED_LIBS_ENABLED "1") | |
266 else() | |
267 set(LLVM_SHARED_LIBS_ENABLED "0") | |
268 endif(BUILD_SHARED_LIBS) | |
269 | |
270 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") | |
271 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH") | |
272 else() # Default for all other unix like systems. | |
273 # CMake hardcodes the library locaction using rpath. | |
274 # Therefore LD_LIBRARY_PATH is not required to run binaries in the | |
275 # build dir. We pass it anyways. | |
276 set(SHLIBPATH_VAR "LD_LIBRARY_PATH") | |
277 endif() | |
278 | |
279 # Configuration-time: See Unit/lit.site.cfg.in | |
280 set(LLVM_BUILD_MODE "%(build_mode)s") | |
281 | |
282 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR}) | |
283 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR}) | |
284 set(LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}/%(build_mode)s") | |
285 set(LLVM_LIBS_DIR "${LLVM_BINARY_DIR}/lib/%(build_mode)s") | |
286 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE}) | |
287 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED}) | |
288 set(SHLIBPATH_VAR ${SHLIBPATH_VAR}) | |
289 | |
290 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE) | |
291 set(ENABLE_ASSERTIONS "1") | |
292 else() | |
293 set(ENABLE_ASSERTIONS "0") | |
294 endif() | |
295 | |
296 set(HOST_OS ${CMAKE_SYSTEM_NAME}) | |
297 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR}) | |
298 | |
299 if (CLANG_ENABLE_ARCMT) | |
300 set(ENABLE_CLANG_ARCMT "1") | |
301 else() | |
302 set(ENABLE_CLANG_ARCMT "0") | |
303 endif() | |
304 if (CLANG_ENABLE_REWRITER) | |
305 set(ENABLE_CLANG_REWRITER "1") | |
306 else() | |
307 set(ENABLE_CLANG_REWRITER "0") | |
308 endif() | |
309 if (CLANG_ENABLE_STATIC_ANALYZER) | |
310 set(ENABLE_CLANG_STATIC_ANALYZER "1") | |
311 else() | |
312 set(ENABLE_CLANG_STATIC_ANALYZER "0") | |
313 endif() | |
314 | |
315 configure_file(${input} ${output} @ONLY) | |
316 endfunction() | |
317 | |
318 # A raw function to create a lit target. This is used to implement the testuite | |
319 # management functions. | |
320 function(add_lit_target target comment) | |
321 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN}) | |
322 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}") | |
323 separate_arguments(LIT_ARGS) | |
324 set(LIT_COMMAND | |
325 ${PYTHON_EXECUTABLE} | |
326 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py | |
327 --param build_mode=${CMAKE_CFG_INTDIR} | |
328 ${LIT_ARGS} | |
329 ) | |
330 foreach(param ${ARG_PARAMS}) | |
331 list(APPEND LIT_COMMAND --param ${param}) | |
332 endforeach() | |
333 if( ARG_DEPENDS ) | |
334 add_custom_target(${target} | |
335 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS} | |
336 COMMENT "${comment}" | |
337 ) | |
338 add_dependencies(${target} ${ARG_DEPENDS}) | |
339 else() | |
340 add_custom_target(${target} | |
341 COMMAND cmake -E echo "${target} does nothing, no tools built.") | |
342 message(STATUS "${target} does nothing.") | |
343 endif() | |
344 endfunction() | |
345 | |
346 # A function to add a set of lit test suites to be driven through 'check-*' targets. | |
347 function(add_lit_testsuite target comment) | |
348 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN}) | |
349 | |
350 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all. | |
351 if(NOT EXCLUDE_FROM_ALL) | |
352 # Register the testsuites, params and depends for the global check rule. | |
353 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS}) | |
354 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS}) | |
355 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS}) | |
356 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS}) | |
357 endif() | |
358 | |
359 # Produce a specific suffixed check rule. | |
360 add_lit_target(${target} ${comment} | |
361 ${ARG_DEFAULT_ARGS} | |
362 PARAMS ${ARG_PARAMS} | |
363 DEPENDS ${ARG_DEPENDS} | |
364 ARGS ${ARG_ARGS} | |
365 ) | |
366 endfunction() |