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 "${target_name}.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 "${target_name}.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 "${target_name}.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 " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
56 add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
58 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
60 get_filename_component(extension ${src} EXT)
61 if(extension STREQUAL ".cpp")
62 set(first_source_file ${src})
67 # Force re-linking when the exports file changes. Actually, it
68 # forces recompilation of the source file. The LINK_DEPENDS target
69 # property only works for makefile-based generators.
70 set_property(SOURCE ${first_source_file} APPEND PROPERTY
71 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
73 set_property(DIRECTORY APPEND
74 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
76 add_dependencies(${target_name} ${target_name}_exports)
77 endfunction(add_llvm_symbol_exports)
79 function(add_dead_strip target_name)
80 # FIXME: With MSVS, consider compiling with /Gy and linking with /OPT:REF?
81 # But MinSizeRel seems to add that automatically, so maybe disable these
82 # flags instead if LLVM_NO_DEAD_STRIP is set.
83 if(NOT CYGWIN AND NOT MINGW AND NOT MSVC)
84 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
86 "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections"
90 if(NOT LLVM_NO_DEAD_STRIP)
91 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
92 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
93 LINK_FLAGS " -Wl,-dead_strip")
95 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
96 LINK_FLAGS " -Wl,--gc-sections")
99 endfunction(add_dead_strip)
101 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
102 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
103 # or a certain builder, for eaxample, msbuild.exe, would be confused.
104 function(set_output_directory target bindir libdir)
105 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
106 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
107 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
108 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
109 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
110 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
111 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
112 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
115 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
116 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
117 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
121 macro(add_llvm_library name)
122 llvm_process_sources( ALL_FILES ${ARGN} )
123 add_library( ${name} ${ALL_FILES} )
124 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
125 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
126 add_dead_strip( ${name} )
127 if( LLVM_COMMON_DEPENDS )
128 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
129 endif( LLVM_COMMON_DEPENDS )
131 if( BUILD_SHARED_LIBS )
132 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
134 set_target_properties(${name}
136 IMPORT_SUFFIX ".imp")
139 if (LLVM_EXPORTED_SYMBOL_FILE)
140 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
144 # Ensure that the system libraries always comes last on the
145 # list. Without this, linking the unit tests on MinGW fails.
146 link_system_libs( ${name} )
148 if( EXCLUDE_FROM_ALL )
149 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
151 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
152 install(TARGETS ${name}
153 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
154 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
157 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
159 # Add the explicit dependency information for this library.
161 # It would be nice to verify that we have the dependencies for this library
162 # name, but using get_property(... SET) doesn't suffice to determine if a
163 # property has been set to an empty value.
164 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
165 target_link_libraries(${name} ${lib_deps})
166 endmacro(add_llvm_library name)
168 macro(add_llvm_loadable_module name)
169 if( NOT LLVM_ON_UNIX OR CYGWIN )
170 message(STATUS "Loadable modules not supported on this platform.
172 # Add empty "phony" target
173 add_custom_target(${name})
175 llvm_process_sources( ALL_FILES ${ARGN} )
182 add_library( ${name} ${libkind} ${ALL_FILES} )
183 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
184 set_target_properties( ${name} PROPERTIES PREFIX "" )
185 add_dead_strip( ${name} )
187 if (LLVM_EXPORTED_SYMBOL_FILE)
188 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
189 endif(LLVM_EXPORTED_SYMBOL_FILE)
191 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
192 link_system_libs( ${name} )
195 # Darwin-specific linker flags for loadable modules.
196 set_property(TARGET ${name} APPEND_STRING PROPERTY
197 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
200 if( EXCLUDE_FROM_ALL )
201 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
203 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
204 install(TARGETS ${name}
205 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
206 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
211 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
212 endmacro(add_llvm_loadable_module name)
215 macro(add_llvm_executable name)
216 llvm_process_sources( ALL_FILES ${ARGN} )
217 if( EXCLUDE_FROM_ALL )
218 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
220 add_executable(${name} ${ALL_FILES})
222 add_dead_strip( ${name} )
224 if (LLVM_EXPORTED_SYMBOL_FILE)
225 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
226 endif(LLVM_EXPORTED_SYMBOL_FILE)
228 set(EXCLUDE_FROM_ALL OFF)
229 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
230 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
231 if( LLVM_COMMON_DEPENDS )
232 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
233 endif( LLVM_COMMON_DEPENDS )
234 link_system_libs( ${name} )
235 endmacro(add_llvm_executable name)
238 set (LLVM_TOOLCHAIN_TOOLS
243 macro(add_llvm_tool name)
244 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
245 if( NOT LLVM_BUILD_TOOLS )
246 set(EXCLUDE_FROM_ALL ON)
248 add_llvm_executable(${name} ${ARGN})
250 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
251 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
252 if( LLVM_BUILD_TOOLS )
253 install(TARGETS ${name} RUNTIME DESTINATION bin)
256 set_target_properties(${name} PROPERTIES FOLDER "Tools")
257 endmacro(add_llvm_tool name)
260 macro(add_llvm_example name)
261 # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
262 if( NOT LLVM_BUILD_EXAMPLES )
263 set(EXCLUDE_FROM_ALL ON)
265 add_llvm_executable(${name} ${ARGN})
266 if( LLVM_BUILD_EXAMPLES )
267 install(TARGETS ${name} RUNTIME DESTINATION examples)
269 set_target_properties(${name} PROPERTIES FOLDER "Examples")
270 endmacro(add_llvm_example name)
273 macro(add_llvm_utility name)
274 add_llvm_executable(${name} ${ARGN})
275 set_target_properties(${name} PROPERTIES FOLDER "Utils")
276 endmacro(add_llvm_utility name)
279 macro(add_llvm_target target_name)
280 include_directories(BEFORE
281 ${CMAKE_CURRENT_BINARY_DIR}
282 ${CMAKE_CURRENT_SOURCE_DIR})
283 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
284 set( CURRENT_LLVM_TARGET LLVM${target_name} )
285 endmacro(add_llvm_target)
287 # Add external project that may want to be built as part of llvm such as Clang,
288 # lld, and Polly. This adds two options. One for the source directory of the
289 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
290 # enable or disable building it with everything else.
291 # Additional parameter can be specified as the name of directory.
292 macro(add_llvm_external_project name)
293 set(add_llvm_external_dir "${ARGN}")
294 if("${add_llvm_external_dir}" STREQUAL "")
295 set(add_llvm_external_dir ${name})
297 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
298 string(REPLACE "-" "_" nameUNDERSCORE ${name})
299 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
300 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
301 CACHE PATH "Path to ${name} source directory")
302 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
303 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
304 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
305 "Whether to build ${name} as part of LLVM" ON)
306 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
307 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
310 endmacro(add_llvm_external_project)
312 macro(add_llvm_tool_subdirectory name)
313 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
314 add_subdirectory(${name})
315 endmacro(add_llvm_tool_subdirectory)
317 macro(ignore_llvm_tool_subdirectory name)
318 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
319 endmacro(ignore_llvm_tool_subdirectory)
321 function(add_llvm_implicit_external_projects)
322 set(list_of_implicit_subdirs "")
323 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
324 foreach(dir ${sub-dirs})
325 if(IS_DIRECTORY "${dir}")
326 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
327 if( tool_subdir_ignore EQUAL -1
328 AND EXISTS "${dir}/CMakeLists.txt")
329 get_filename_component(fn "${dir}" NAME)
330 list(APPEND list_of_implicit_subdirs "${fn}")
335 foreach(external_proj ${list_of_implicit_subdirs})
336 add_llvm_external_project("${external_proj}")
338 endfunction(add_llvm_implicit_external_projects)
340 # Generic support for adding a unittest.
341 function(add_unittest test_suite test_name)
342 if( NOT LLVM_BUILD_TESTS )
343 set(EXCLUDE_FROM_ALL ON)
346 add_llvm_executable(${test_name} ${ARGN})
347 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
348 set_output_directory(${test_name} ${outdir} ${outdir})
349 target_link_libraries(${test_name}
352 LLVMSupport # gtest needs it for raw_ostream.
355 add_dependencies(${test_suite} ${test_name})
356 get_target_property(test_suite_folder ${test_suite} FOLDER)
357 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
358 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
361 # Visual Studio 2012 only supports up to 8 template parameters in
362 # std::tr1::tuple by default, but gtest requires 10
363 if (MSVC AND MSVC_VERSION EQUAL 1700)
364 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
367 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
368 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
369 if (NOT LLVM_ENABLE_THREADS)
370 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
373 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
374 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
375 set(target_compile_flags "${target_compile_flags} -fno-rtti")
377 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
380 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
381 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
383 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
386 # This function provides an automatic way to 'configure'-like generate a file
387 # based on a set of common and custom variables, specifically targeting the
388 # variables needed for the 'lit.site.cfg' files. This function bundles the
389 # common variables that any Lit instance is likely to need, and custom
390 # variables can be passed in.
391 function(configure_lit_site_cfg input output)
392 foreach(c ${LLVM_TARGETS_TO_BUILD})
393 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
395 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
397 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
399 if(BUILD_SHARED_LIBS)
400 set(LLVM_SHARED_LIBS_ENABLED "1")
402 set(LLVM_SHARED_LIBS_ENABLED "0")
403 endif(BUILD_SHARED_LIBS)
405 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
406 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
407 else() # Default for all other unix like systems.
408 # CMake hardcodes the library locaction using rpath.
409 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
410 # build dir. We pass it anyways.
411 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
414 # Configuration-time: See Unit/lit.site.cfg.in
415 if (CMAKE_CFG_INTDIR STREQUAL ".")
416 set(LLVM_BUILD_MODE ".")
418 set(LLVM_BUILD_MODE "%(build_mode)s")
421 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
422 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
423 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
424 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
425 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
426 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
427 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
428 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
430 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
431 set(ENABLE_ASSERTIONS "1")
433 set(ENABLE_ASSERTIONS "0")
436 set(HOST_OS ${CMAKE_SYSTEM_NAME})
437 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
439 if (CLANG_ENABLE_ARCMT)
440 set(ENABLE_CLANG_ARCMT "1")
442 set(ENABLE_CLANG_ARCMT "0")
444 if (CLANG_ENABLE_REWRITER)
445 set(ENABLE_CLANG_REWRITER "1")
447 set(ENABLE_CLANG_REWRITER "0")
449 if (CLANG_ENABLE_STATIC_ANALYZER)
450 set(ENABLE_CLANG_STATIC_ANALYZER "1")
452 set(ENABLE_CLANG_STATIC_ANALYZER "0")
455 configure_file(${input} ${output} @ONLY)
458 # A raw function to create a lit target. This is used to implement the testuite
459 # management functions.
460 function(add_lit_target target comment)
461 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
462 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
463 separate_arguments(LIT_ARGS)
464 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
465 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
469 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
472 foreach(param ${ARG_PARAMS})
473 list(APPEND LIT_COMMAND --param ${param})
476 add_custom_target(${target}
477 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
480 add_dependencies(${target} ${ARG_DEPENDS})
482 add_custom_target(${target}
483 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
484 message(STATUS "${target} does nothing.")
487 # Tests should be excluded from "Build Solution".
488 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
491 # A function to add a set of lit test suites to be driven through 'check-*' targets.
492 function(add_lit_testsuite target comment)
493 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
495 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
496 if(NOT EXCLUDE_FROM_ALL)
497 # Register the testsuites, params and depends for the global check rule.
498 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
499 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
500 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
501 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
504 # Produce a specific suffixed check rule.
505 add_lit_target(${target} ${comment}
508 DEPENDS ${ARG_DEPENDS}