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 macro(add_llvm_library name)
99 llvm_process_sources( ALL_FILES ${ARGN} )
100 add_library( ${name} ${ALL_FILES} )
101 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
102 add_dead_strip( ${name} )
103 if( LLVM_COMMON_DEPENDS )
104 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
105 endif( LLVM_COMMON_DEPENDS )
107 if( BUILD_SHARED_LIBS )
108 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
110 set_target_properties(${name}
112 IMPORT_SUFFIX ".imp")
115 if (LLVM_EXPORTED_SYMBOL_FILE)
116 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
120 # Ensure that the system libraries always comes last on the
121 # list. Without this, linking the unit tests on MinGW fails.
122 link_system_libs( ${name} )
124 if( EXCLUDE_FROM_ALL )
125 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
127 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
128 install(TARGETS ${name}
129 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
130 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
133 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
135 # Add the explicit dependency information for this library.
137 # It would be nice to verify that we have the dependencies for this library
138 # name, but using get_property(... SET) doesn't suffice to determine if a
139 # property has been set to an empty value.
140 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
141 target_link_libraries(${name} ${lib_deps})
142 endmacro(add_llvm_library name)
144 macro(add_llvm_loadable_module name)
145 if( NOT LLVM_ON_UNIX OR CYGWIN )
146 message(STATUS "Loadable modules not supported on this platform.
148 # Add empty "phony" target
149 add_custom_target(${name})
151 llvm_process_sources( ALL_FILES ${ARGN} )
158 add_library( ${name} ${libkind} ${ALL_FILES} )
159 set_target_properties( ${name} PROPERTIES PREFIX "" )
160 add_dead_strip( ${name} )
162 if (LLVM_EXPORTED_SYMBOL_FILE)
163 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
164 endif(LLVM_EXPORTED_SYMBOL_FILE)
166 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
167 link_system_libs( ${name} )
170 # Darwin-specific linker flags for loadable modules.
171 set_property(TARGET ${name} APPEND_STRING PROPERTY
172 LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
175 if( EXCLUDE_FROM_ALL )
176 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
178 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
179 install(TARGETS ${name}
180 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
181 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
186 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
187 endmacro(add_llvm_loadable_module name)
190 macro(add_llvm_executable name)
191 llvm_process_sources( ALL_FILES ${ARGN} )
192 if( EXCLUDE_FROM_ALL )
193 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
195 add_executable(${name} ${ALL_FILES})
197 add_dead_strip( ${name} )
199 if (LLVM_EXPORTED_SYMBOL_FILE)
200 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
201 endif(LLVM_EXPORTED_SYMBOL_FILE)
203 set(EXCLUDE_FROM_ALL OFF)
204 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
205 if( LLVM_COMMON_DEPENDS )
206 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
207 endif( LLVM_COMMON_DEPENDS )
208 link_system_libs( ${name} )
209 endmacro(add_llvm_executable name)
212 set (LLVM_TOOLCHAIN_TOOLS
217 macro(add_llvm_tool name)
218 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
219 if( NOT LLVM_BUILD_TOOLS )
220 set(EXCLUDE_FROM_ALL ON)
222 add_llvm_executable(${name} ${ARGN})
224 list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
225 if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
226 if( LLVM_BUILD_TOOLS )
227 install(TARGETS ${name} RUNTIME DESTINATION bin)
230 set_target_properties(${name} PROPERTIES FOLDER "Tools")
231 endmacro(add_llvm_tool name)
234 macro(add_llvm_example name)
235 # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
236 if( NOT LLVM_BUILD_EXAMPLES )
237 set(EXCLUDE_FROM_ALL ON)
239 add_llvm_executable(${name} ${ARGN})
240 if( LLVM_BUILD_EXAMPLES )
241 install(TARGETS ${name} RUNTIME DESTINATION examples)
243 set_target_properties(${name} PROPERTIES FOLDER "Examples")
244 endmacro(add_llvm_example name)
247 macro(add_llvm_utility name)
248 add_llvm_executable(${name} ${ARGN})
249 set_target_properties(${name} PROPERTIES FOLDER "Utils")
250 endmacro(add_llvm_utility name)
253 macro(add_llvm_target target_name)
254 include_directories(BEFORE
255 ${CMAKE_CURRENT_BINARY_DIR}
256 ${CMAKE_CURRENT_SOURCE_DIR})
257 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
258 set( CURRENT_LLVM_TARGET LLVM${target_name} )
259 endmacro(add_llvm_target)
261 # Add external project that may want to be built as part of llvm such as Clang,
262 # lld, and Polly. This adds two options. One for the source directory of the
263 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
264 # enable or disable building it with everything else.
265 # Additional parameter can be specified as the name of directory.
266 macro(add_llvm_external_project name)
267 set(add_llvm_external_dir "${ARGN}")
268 if("${add_llvm_external_dir}" STREQUAL "")
269 set(add_llvm_external_dir ${name})
271 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
272 string(REPLACE "-" "_" nameUNDERSCORE ${name})
273 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
274 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
275 CACHE PATH "Path to ${name} source directory")
276 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
277 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
278 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
279 "Whether to build ${name} as part of LLVM" ON)
280 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
281 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
284 endmacro(add_llvm_external_project)
286 macro(add_llvm_tool_subdirectory name)
287 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
288 add_subdirectory(${name})
289 endmacro(add_llvm_tool_subdirectory)
291 macro(ignore_llvm_tool_subdirectory name)
292 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
293 endmacro(ignore_llvm_tool_subdirectory)
295 function(add_llvm_implicit_external_projects)
296 set(list_of_implicit_subdirs "")
297 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
298 foreach(dir ${sub-dirs})
299 if(IS_DIRECTORY "${dir}")
300 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
301 if( tool_subdir_ignore EQUAL -1
302 AND EXISTS "${dir}/CMakeLists.txt")
303 get_filename_component(fn "${dir}" NAME)
304 list(APPEND list_of_implicit_subdirs "${fn}")
309 foreach(external_proj ${list_of_implicit_subdirs})
310 add_llvm_external_project("${external_proj}")
312 endfunction(add_llvm_implicit_external_projects)
314 # Generic support for adding a unittest.
315 function(add_unittest test_suite test_name)
316 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
317 if( NOT LLVM_BUILD_TESTS )
318 set(EXCLUDE_FROM_ALL ON)
321 add_llvm_executable(${test_name} ${ARGN})
322 target_link_libraries(${test_name}
325 LLVMSupport # gtest needs it for raw_ostream.
328 add_dependencies(${test_suite} ${test_name})
329 get_target_property(test_suite_folder ${test_suite} FOLDER)
330 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
331 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
334 # Visual Studio 2012 only supports up to 8 template parameters in
335 # std::tr1::tuple by default, but gtest requires 10
336 if (MSVC AND MSVC_VERSION EQUAL 1700)
337 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
340 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
341 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
342 if (NOT LLVM_ENABLE_THREADS)
343 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
346 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
347 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
348 set(target_compile_flags "${target_compile_flags} -fno-rtti")
350 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
353 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
354 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
356 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
359 # This function provides an automatic way to 'configure'-like generate a file
360 # based on a set of common and custom variables, specifically targeting the
361 # variables needed for the 'lit.site.cfg' files. This function bundles the
362 # common variables that any Lit instance is likely to need, and custom
363 # variables can be passed in.
364 function(configure_lit_site_cfg input output)
365 foreach(c ${LLVM_TARGETS_TO_BUILD})
366 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
368 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
370 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
371 set(SHLIBDIR "${LLVM_LIBRARY_OUTPUT_INTDIR}")
373 if(BUILD_SHARED_LIBS)
374 set(LLVM_SHARED_LIBS_ENABLED "1")
376 set(LLVM_SHARED_LIBS_ENABLED "0")
377 endif(BUILD_SHARED_LIBS)
379 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
380 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
381 else() # Default for all other unix like systems.
382 # CMake hardcodes the library locaction using rpath.
383 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
384 # build dir. We pass it anyways.
385 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
388 # Configuration-time: See Unit/lit.site.cfg.in
389 if (CMAKE_CFG_INTDIR STREQUAL ".")
390 set(LLVM_BUILD_MODE ".")
392 set(LLVM_BUILD_MODE "%(build_mode)s")
395 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
396 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
397 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
398 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
399 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
400 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
401 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
403 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
404 set(ENABLE_ASSERTIONS "1")
406 set(ENABLE_ASSERTIONS "0")
409 set(HOST_OS ${CMAKE_SYSTEM_NAME})
410 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
412 if (CLANG_ENABLE_ARCMT)
413 set(ENABLE_CLANG_ARCMT "1")
415 set(ENABLE_CLANG_ARCMT "0")
417 if (CLANG_ENABLE_REWRITER)
418 set(ENABLE_CLANG_REWRITER "1")
420 set(ENABLE_CLANG_REWRITER "0")
422 if (CLANG_ENABLE_STATIC_ANALYZER)
423 set(ENABLE_CLANG_STATIC_ANALYZER "1")
425 set(ENABLE_CLANG_STATIC_ANALYZER "0")
428 configure_file(${input} ${output} @ONLY)
431 # A raw function to create a lit target. This is used to implement the testuite
432 # management functions.
433 function(add_lit_target target comment)
434 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
435 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
436 separate_arguments(LIT_ARGS)
437 if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
438 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
442 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
445 foreach(param ${ARG_PARAMS})
446 list(APPEND LIT_COMMAND --param ${param})
449 add_custom_target(${target}
450 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
453 add_dependencies(${target} ${ARG_DEPENDS})
455 add_custom_target(${target}
456 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
457 message(STATUS "${target} does nothing.")
460 # Tests should be excluded from "Build Solution".
461 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
464 # A function to add a set of lit test suites to be driven through 'check-*' targets.
465 function(add_lit_testsuite target comment)
466 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
468 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
469 if(NOT EXCLUDE_FROM_ALL)
470 # Register the testsuites, params and depends for the global check rule.
471 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
472 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
473 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
474 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
477 # Produce a specific suffixed check rule.
478 add_lit_target(${target} ${comment}
481 DEPENDS ${ARG_DEPENDS}