1 include(LLVMParseArguments)
2 include(LLVMProcessSources)
5 function(add_llvm_symbol_exports target_name export_file)
6 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
7 set(native_export_file "symbol.exports")
8 add_custom_command(OUTPUT ${native_export_file}
9 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
10 DEPENDS ${export_file}
12 COMMENT "Creating export file for ${target_name}")
13 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
14 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
15 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
16 # Gold and BFD ld require a version script rather than a plain list.
17 set(native_export_file "symbol.exports")
18 # FIXME: Don't write the "local:" line on OpenBSD.
19 add_custom_command(OUTPUT ${native_export_file}
20 COMMAND echo "{" > ${native_export_file}
21 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || :
22 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file}
23 COMMAND echo " local: *;" >> ${native_export_file}
24 COMMAND echo "};" >> ${native_export_file}
25 DEPENDS ${export_file}
27 COMMENT "Creating export file for ${target_name}")
28 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
29 LINK_FLAGS " -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
31 set(native_export_file "symbol.def")
38 # Using ${export_file} in add_custom_command directly confuses cmd.exe.
39 file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
41 add_custom_command(OUTPUT ${native_export_file}
42 COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
43 COMMAND ${CAT} ${export_file_backslashes} >> ${native_export_file}
44 DEPENDS ${export_file}
46 COMMENT "Creating export file for ${target_name}")
48 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
49 LINK_FLAGS "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
51 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
52 LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
55 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
57 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
59 get_filename_component(extension ${src} EXT)
60 if(extension STREQUAL ".cpp")
61 set(first_source_file ${src})
66 # Force re-linking when the exports file changes. Actually, it
67 # forces recompilation of the source file. The LINK_DEPENDS target
68 # property only works for makefile-based generators.
69 set_property(SOURCE ${first_source_file} APPEND PROPERTY
70 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
72 set_property(DIRECTORY APPEND
73 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
75 add_dependencies(${target_name} ${target_name}_exports)
76 endfunction(add_llvm_symbol_exports)
78 macro(add_llvm_library name)
79 llvm_process_sources( ALL_FILES ${ARGN} )
80 add_library( ${name} ${ALL_FILES} )
81 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
82 if( LLVM_COMMON_DEPENDS )
83 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
84 endif( LLVM_COMMON_DEPENDS )
86 if( BUILD_SHARED_LIBS )
87 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
89 set_target_properties(${name}
94 if (LLVM_EXPORTED_SYMBOL_FILE)
95 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
99 # Ensure that the system libraries always comes last on the
100 # list. Without this, linking the unit tests on MinGW fails.
101 link_system_libs( ${name} )
103 if( EXCLUDE_FROM_ALL )
104 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
106 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
107 install(TARGETS ${name}
108 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
109 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
112 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
114 # Add the explicit dependency information for this library.
116 # It would be nice to verify that we have the dependencies for this library
117 # name, but using get_property(... SET) doesn't suffice to determine if a
118 # property has been set to an empty value.
119 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
120 target_link_libraries(${name} ${lib_deps})
121 endmacro(add_llvm_library name)
123 macro(add_llvm_loadable_module name)
124 if( NOT LLVM_ON_UNIX OR CYGWIN )
125 message(STATUS "Loadable modules not supported on this platform.
127 # Add empty "phony" target
128 add_custom_target(${name})
130 llvm_process_sources( ALL_FILES ${ARGN} )
137 if (LLVM_EXPORTED_SYMBOL_FILE)
138 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
139 endif(LLVM_EXPORTED_SYMBOL_FILE)
141 add_library( ${name} ${libkind} ${ALL_FILES} )
142 set_target_properties( ${name} PROPERTIES PREFIX "" )
144 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
145 link_system_libs( ${name} )
148 # Darwin-specific linker flags for loadable modules.
149 set_target_properties(${name} PROPERTIES
150 LINK_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
153 if( EXCLUDE_FROM_ALL )
154 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
156 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
157 install(TARGETS ${name}
158 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
159 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
164 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
165 endmacro(add_llvm_loadable_module name)
168 macro(add_llvm_executable name)
169 llvm_process_sources( ALL_FILES ${ARGN} )
170 if( EXCLUDE_FROM_ALL )
171 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
173 add_executable(${name} ${ALL_FILES})
176 if (LLVM_EXPORTED_SYMBOL_FILE)
177 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
178 endif(LLVM_EXPORTED_SYMBOL_FILE)
180 set(EXCLUDE_FROM_ALL OFF)
181 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
182 if( LLVM_COMMON_DEPENDS )
183 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
184 endif( LLVM_COMMON_DEPENDS )
185 link_system_libs( ${name} )
186 endmacro(add_llvm_executable name)
189 set (LLVM_TOOLCHAIN_TOOLS
194 macro(add_llvm_tool name)
195 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
196 if( NOT LLVM_BUILD_TOOLS )
197 set(EXCLUDE_FROM_ALL ON)
199 add_llvm_executable(${name} ${ARGN})
201 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
202 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
203 if( LLVM_BUILD_TOOLS )
204 install(TARGETS ${name} RUNTIME DESTINATION bin)
207 set_target_properties(${name} PROPERTIES FOLDER "Tools")
208 endmacro(add_llvm_tool name)
211 macro(add_llvm_example name)
212 # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
213 if( NOT LLVM_BUILD_EXAMPLES )
214 set(EXCLUDE_FROM_ALL ON)
216 add_llvm_executable(${name} ${ARGN})
217 if( LLVM_BUILD_EXAMPLES )
218 install(TARGETS ${name} RUNTIME DESTINATION examples)
220 set_target_properties(${name} PROPERTIES FOLDER "Examples")
221 endmacro(add_llvm_example name)
224 macro(add_llvm_utility name)
225 add_llvm_executable(${name} ${ARGN})
226 set_target_properties(${name} PROPERTIES FOLDER "Utils")
227 endmacro(add_llvm_utility name)
230 macro(add_llvm_target target_name)
231 include_directories(BEFORE
232 ${CMAKE_CURRENT_BINARY_DIR}
233 ${CMAKE_CURRENT_SOURCE_DIR})
234 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
235 set( CURRENT_LLVM_TARGET LLVM${target_name} )
236 endmacro(add_llvm_target)
238 # Add external project that may want to be built as part of llvm such as Clang,
239 # lld, and Polly. This adds two options. One for the source directory of the
240 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
241 # enable or disable building it with everything else.
242 # Additional parameter can be specified as the name of directory.
243 macro(add_llvm_external_project name)
244 set(add_llvm_external_dir "${ARGN}")
245 if("${add_llvm_external_dir}" STREQUAL "")
246 set(add_llvm_external_dir ${name})
248 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
249 string(REPLACE "-" "_" nameUNDERSCORE ${name})
250 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
251 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
252 CACHE PATH "Path to ${name} source directory")
253 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
254 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
255 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
256 "Whether to build ${name} as part of LLVM" ON)
257 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
258 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
261 endmacro(add_llvm_external_project)
263 macro(add_llvm_tool_subdirectory name)
264 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
265 add_subdirectory(${name})
266 endmacro(add_llvm_tool_subdirectory)
268 macro(ignore_llvm_tool_subdirectory name)
269 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
270 endmacro(ignore_llvm_tool_subdirectory)
272 function(add_llvm_implicit_external_projects)
273 set(list_of_implicit_subdirs "")
274 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
275 foreach(dir ${sub-dirs})
276 if(IS_DIRECTORY "${dir}")
277 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
278 if( tool_subdir_ignore EQUAL -1
279 AND EXISTS "${dir}/CMakeLists.txt")
280 get_filename_component(fn "${dir}" NAME)
281 list(APPEND list_of_implicit_subdirs "${fn}")
286 foreach(external_proj ${list_of_implicit_subdirs})
287 add_llvm_external_project("${external_proj}")
289 endfunction(add_llvm_implicit_external_projects)
291 # Generic support for adding a unittest.
292 function(add_unittest test_suite test_name)
293 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
294 if( NOT LLVM_BUILD_TESTS )
295 set(EXCLUDE_FROM_ALL ON)
298 add_llvm_executable(${test_name} ${ARGN})
299 target_link_libraries(${test_name}
302 LLVMSupport # gtest needs it for raw_ostream.
305 add_dependencies(${test_suite} ${test_name})
306 get_target_property(test_suite_folder ${test_suite} FOLDER)
307 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
308 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
311 # Visual Studio 2012 only supports up to 8 template parameters in
312 # std::tr1::tuple by default, but gtest requires 10
313 if (MSVC AND MSVC_VERSION EQUAL 1700)
314 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
317 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
318 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
319 if (NOT LLVM_ENABLE_THREADS)
320 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
323 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
324 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
325 set(target_compile_flags "${target_compile_flags} -fno-rtti")
327 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
330 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
331 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
333 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
336 # This function provides an automatic way to 'configure'-like generate a file
337 # based on a set of common and custom variables, specifically targeting the
338 # variables needed for the 'lit.site.cfg' files. This function bundles the
339 # common variables that any Lit instance is likely to need, and custom
340 # variables can be passed in.
341 function(configure_lit_site_cfg input output)
342 foreach(c ${LLVM_TARGETS_TO_BUILD})
343 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
345 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
347 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
348 set(SHLIBDIR "${LLVM_LIBRARY_OUTPUT_INTDIR}")
350 if(BUILD_SHARED_LIBS)
351 set(LLVM_SHARED_LIBS_ENABLED "1")
353 set(LLVM_SHARED_LIBS_ENABLED "0")
354 endif(BUILD_SHARED_LIBS)
356 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
357 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
358 else() # Default for all other unix like systems.
359 # CMake hardcodes the library locaction using rpath.
360 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
361 # build dir. We pass it anyways.
362 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
365 # Configuration-time: See Unit/lit.site.cfg.in
366 if (CMAKE_CFG_INTDIR STREQUAL ".")
367 set(LLVM_BUILD_MODE ".")
369 set(LLVM_BUILD_MODE "%(build_mode)s")
372 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
373 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
374 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
375 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
376 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
377 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
378 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
380 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
381 set(ENABLE_ASSERTIONS "1")
383 set(ENABLE_ASSERTIONS "0")
386 set(HOST_OS ${CMAKE_SYSTEM_NAME})
387 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
389 if (CLANG_ENABLE_ARCMT)
390 set(ENABLE_CLANG_ARCMT "1")
392 set(ENABLE_CLANG_ARCMT "0")
394 if (CLANG_ENABLE_REWRITER)
395 set(ENABLE_CLANG_REWRITER "1")
397 set(ENABLE_CLANG_REWRITER "0")
399 if (CLANG_ENABLE_STATIC_ANALYZER)
400 set(ENABLE_CLANG_STATIC_ANALYZER "1")
402 set(ENABLE_CLANG_STATIC_ANALYZER "0")
405 configure_file(${input} ${output} @ONLY)
408 # A raw function to create a lit target. This is used to implement the testuite
409 # management functions.
410 function(add_lit_target target comment)
411 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
412 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
413 separate_arguments(LIT_ARGS)
414 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
415 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
419 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
422 foreach(param ${ARG_PARAMS})
423 list(APPEND LIT_COMMAND --param ${param})
426 add_custom_target(${target}
427 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
430 add_dependencies(${target} ${ARG_DEPENDS})
432 add_custom_target(${target}
433 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
434 message(STATUS "${target} does nothing.")
437 # Tests should be excluded from "Build Solution".
438 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
441 # A function to add a set of lit test suites to be driven through 'check-*' targets.
442 function(add_lit_testsuite target comment)
443 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
445 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
446 if(NOT EXCLUDE_FROM_ALL)
447 # Register the testsuites, params and depends for the global check rule.
448 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
449 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
450 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
451 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
454 # Produce a specific suffixed check rule.
455 add_lit_target(${target} ${comment}
458 DEPENDS ${ARG_DEPENDS}