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 if(NOT CYGWIN AND NOT MINGW AND NOT MSVC)
81 if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
83 "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections"
87 if(NOT LLVM_NO_DEAD_STRIP)
88 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
89 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
90 LINK_FLAGS " -Wl,-dead_strip")
92 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
93 LINK_FLAGS " -Wl,--gc-sections")
96 endfunction(add_dead_strip)
98 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
99 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
100 # or a certain builder, for eaxample, msbuild.exe, would be confused.
101 function(set_output_directory target bindir libdir)
102 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
103 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
104 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
105 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
106 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
107 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
108 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
109 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
112 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
113 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
114 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
118 macro(add_llvm_library name)
119 llvm_process_sources( ALL_FILES ${ARGN} )
120 add_library( ${name} ${ALL_FILES} )
121 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
122 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
123 add_dead_strip( ${name} )
124 if( LLVM_COMMON_DEPENDS )
125 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
126 endif( LLVM_COMMON_DEPENDS )
128 if( BUILD_SHARED_LIBS )
129 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
131 set_target_properties(${name}
133 IMPORT_SUFFIX ".imp")
136 if (LLVM_EXPORTED_SYMBOL_FILE)
137 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
141 # Ensure that the system libraries always comes last on the
142 # list. Without this, linking the unit tests on MinGW fails.
143 link_system_libs( ${name} )
145 if( EXCLUDE_FROM_ALL )
146 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
148 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
149 install(TARGETS ${name}
150 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
151 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
154 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
156 # Add the explicit dependency information for this library.
158 # It would be nice to verify that we have the dependencies for this library
159 # name, but using get_property(... SET) doesn't suffice to determine if a
160 # property has been set to an empty value.
161 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
162 target_link_libraries(${name} ${lib_deps})
163 endmacro(add_llvm_library name)
165 macro(add_llvm_loadable_module name)
166 if( NOT LLVM_ON_UNIX OR CYGWIN )
167 message(STATUS "Loadable modules not supported on this platform.
169 # Add empty "phony" target
170 add_custom_target(${name})
172 llvm_process_sources( ALL_FILES ${ARGN} )
179 add_library( ${name} ${libkind} ${ALL_FILES} )
180 set_target_properties( ${name} PROPERTIES PREFIX "" )
181 add_dead_strip( ${name} )
183 if (LLVM_EXPORTED_SYMBOL_FILE)
184 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
185 endif(LLVM_EXPORTED_SYMBOL_FILE)
187 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
188 link_system_libs( ${name} )
191 # Darwin-specific linker flags for loadable modules.
192 set_property(TARGET ${name} APPEND_STRING PROPERTY
193 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
196 if( EXCLUDE_FROM_ALL )
197 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
199 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
200 install(TARGETS ${name}
201 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
202 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
207 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
208 endmacro(add_llvm_loadable_module name)
211 macro(add_llvm_executable name)
212 llvm_process_sources( ALL_FILES ${ARGN} )
213 if( EXCLUDE_FROM_ALL )
214 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
216 add_executable(${name} ${ALL_FILES})
218 add_dead_strip( ${name} )
220 if (LLVM_EXPORTED_SYMBOL_FILE)
221 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
222 endif(LLVM_EXPORTED_SYMBOL_FILE)
224 set(EXCLUDE_FROM_ALL OFF)
225 set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
226 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
227 if( LLVM_COMMON_DEPENDS )
228 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
229 endif( LLVM_COMMON_DEPENDS )
230 link_system_libs( ${name} )
231 endmacro(add_llvm_executable name)
234 set (LLVM_TOOLCHAIN_TOOLS
239 macro(add_llvm_tool name)
240 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
241 if( NOT LLVM_BUILD_TOOLS )
242 set(EXCLUDE_FROM_ALL ON)
244 add_llvm_executable(${name} ${ARGN})
246 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
247 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
248 if( LLVM_BUILD_TOOLS )
249 install(TARGETS ${name} RUNTIME DESTINATION bin)
252 set_target_properties(${name} PROPERTIES FOLDER "Tools")
253 endmacro(add_llvm_tool name)
256 macro(add_llvm_example name)
257 # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
258 if( NOT LLVM_BUILD_EXAMPLES )
259 set(EXCLUDE_FROM_ALL ON)
261 add_llvm_executable(${name} ${ARGN})
262 if( LLVM_BUILD_EXAMPLES )
263 install(TARGETS ${name} RUNTIME DESTINATION examples)
265 set_target_properties(${name} PROPERTIES FOLDER "Examples")
266 endmacro(add_llvm_example name)
269 macro(add_llvm_utility name)
270 add_llvm_executable(${name} ${ARGN})
271 set_target_properties(${name} PROPERTIES FOLDER "Utils")
272 endmacro(add_llvm_utility name)
275 macro(add_llvm_target target_name)
276 include_directories(BEFORE
277 ${CMAKE_CURRENT_BINARY_DIR}
278 ${CMAKE_CURRENT_SOURCE_DIR})
279 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
280 set( CURRENT_LLVM_TARGET LLVM${target_name} )
281 endmacro(add_llvm_target)
283 # Add external project that may want to be built as part of llvm such as Clang,
284 # lld, and Polly. This adds two options. One for the source directory of the
285 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
286 # enable or disable building it with everything else.
287 # Additional parameter can be specified as the name of directory.
288 macro(add_llvm_external_project name)
289 set(add_llvm_external_dir "${ARGN}")
290 if("${add_llvm_external_dir}" STREQUAL "")
291 set(add_llvm_external_dir ${name})
293 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
294 string(REPLACE "-" "_" nameUNDERSCORE ${name})
295 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
296 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
297 CACHE PATH "Path to ${name} source directory")
298 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
299 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
300 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
301 "Whether to build ${name} as part of LLVM" ON)
302 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
303 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
306 endmacro(add_llvm_external_project)
308 macro(add_llvm_tool_subdirectory name)
309 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
310 add_subdirectory(${name})
311 endmacro(add_llvm_tool_subdirectory)
313 macro(ignore_llvm_tool_subdirectory name)
314 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
315 endmacro(ignore_llvm_tool_subdirectory)
317 function(add_llvm_implicit_external_projects)
318 set(list_of_implicit_subdirs "")
319 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
320 foreach(dir ${sub-dirs})
321 if(IS_DIRECTORY "${dir}")
322 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
323 if( tool_subdir_ignore EQUAL -1
324 AND EXISTS "${dir}/CMakeLists.txt")
325 get_filename_component(fn "${dir}" NAME)
326 list(APPEND list_of_implicit_subdirs "${fn}")
331 foreach(external_proj ${list_of_implicit_subdirs})
332 add_llvm_external_project("${external_proj}")
334 endfunction(add_llvm_implicit_external_projects)
336 # Generic support for adding a unittest.
337 function(add_unittest test_suite test_name)
338 if( NOT LLVM_BUILD_TESTS )
339 set(EXCLUDE_FROM_ALL ON)
342 add_llvm_executable(${test_name} ${ARGN})
343 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
344 set_output_directory(${test_name} ${outdir} ${outdir})
345 target_link_libraries(${test_name}
348 LLVMSupport # gtest needs it for raw_ostream.
351 add_dependencies(${test_suite} ${test_name})
352 get_target_property(test_suite_folder ${test_suite} FOLDER)
353 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
354 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
357 # Visual Studio 2012 only supports up to 8 template parameters in
358 # std::tr1::tuple by default, but gtest requires 10
359 if (MSVC AND MSVC_VERSION EQUAL 1700)
360 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
363 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
364 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
365 if (NOT LLVM_ENABLE_THREADS)
366 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
369 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
370 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
371 set(target_compile_flags "${target_compile_flags} -fno-rtti")
373 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
376 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
377 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
379 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
382 # This function provides an automatic way to 'configure'-like generate a file
383 # based on a set of common and custom variables, specifically targeting the
384 # variables needed for the 'lit.site.cfg' files. This function bundles the
385 # common variables that any Lit instance is likely to need, and custom
386 # variables can be passed in.
387 function(configure_lit_site_cfg input output)
388 foreach(c ${LLVM_TARGETS_TO_BUILD})
389 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
391 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
393 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
394 set(SHLIBDIR "${LLVM_LIBRARY_OUTPUT_INTDIR}")
396 if(BUILD_SHARED_LIBS)
397 set(LLVM_SHARED_LIBS_ENABLED "1")
399 set(LLVM_SHARED_LIBS_ENABLED "0")
400 endif(BUILD_SHARED_LIBS)
402 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
403 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
404 else() # Default for all other unix like systems.
405 # CMake hardcodes the library locaction using rpath.
406 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
407 # build dir. We pass it anyways.
408 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
411 # Configuration-time: See Unit/lit.site.cfg.in
412 if (CMAKE_CFG_INTDIR STREQUAL ".")
413 set(LLVM_BUILD_MODE ".")
415 set(LLVM_BUILD_MODE "%(build_mode)s")
418 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
419 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
420 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
421 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
422 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
423 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
424 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
426 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
427 set(ENABLE_ASSERTIONS "1")
429 set(ENABLE_ASSERTIONS "0")
432 set(HOST_OS ${CMAKE_SYSTEM_NAME})
433 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
435 if (CLANG_ENABLE_ARCMT)
436 set(ENABLE_CLANG_ARCMT "1")
438 set(ENABLE_CLANG_ARCMT "0")
440 if (CLANG_ENABLE_REWRITER)
441 set(ENABLE_CLANG_REWRITER "1")
443 set(ENABLE_CLANG_REWRITER "0")
445 if (CLANG_ENABLE_STATIC_ANALYZER)
446 set(ENABLE_CLANG_STATIC_ANALYZER "1")
448 set(ENABLE_CLANG_STATIC_ANALYZER "0")
451 configure_file(${input} ${output} @ONLY)
454 # A raw function to create a lit target. This is used to implement the testuite
455 # management functions.
456 function(add_lit_target target comment)
457 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
458 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
459 separate_arguments(LIT_ARGS)
460 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
461 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
465 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
468 foreach(param ${ARG_PARAMS})
469 list(APPEND LIT_COMMAND --param ${param})
472 add_custom_target(${target}
473 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
476 add_dependencies(${target} ${ARG_DEPENDS})
478 add_custom_target(${target}
479 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
480 message(STATUS "${target} does nothing.")
483 # Tests should be excluded from "Build Solution".
484 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
487 # A function to add a set of lit test suites to be driven through 'check-*' targets.
488 function(add_lit_testsuite target comment)
489 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
491 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
492 if(NOT EXCLUDE_FROM_ALL)
493 # Register the testsuites, params and depends for the global check rule.
494 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
495 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
496 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
497 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
500 # Produce a specific suffixed check rule.
501 add_lit_target(${target} ${comment}
504 DEPENDS ${ARG_DEPENDS}