Export lib and exe build target names from build tree
[oota-llvm.git] / cmake / modules / AddLLVM.cmake
1 include(LLVMParseArguments)
2 include(LLVMProcessSources)
3 include(LLVM-Config)
4
5 function(llvm_update_compile_flags name)
6   get_property(sources TARGET ${name} PROPERTY SOURCES)
7   if("${sources}" MATCHES "\\.c(;|$)")
8     set(update_src_props ON)
9   endif()
10
11   if(LLVM_REQUIRES_EH)
12     set(LLVM_REQUIRES_RTTI ON)
13   else()
14     if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
15       list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions")
16     elseif(MSVC)
17       list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0)
18       list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-")
19     endif()
20   endif()
21
22   if(NOT LLVM_REQUIRES_RTTI)
23     list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
24     if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
25       list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti")
26     elseif (MSVC)
27       list(APPEND LLVM_COMPILE_FLAGS "/GR-")
28     endif ()
29   endif()
30
31   # Assume that;
32   #   - LLVM_COMPILE_FLAGS is list.
33   #   - PROPERTY COMPILE_FLAGS is string.
34   string(REPLACE ";" " " target_compile_flags "${LLVM_COMPILE_FLAGS}")
35
36   if(update_src_props)
37     foreach(fn ${sources})
38       get_filename_component(suf ${fn} EXT)
39       if("${suf}" STREQUAL ".cpp")
40         set_property(SOURCE ${fn} APPEND_STRING PROPERTY
41           COMPILE_FLAGS "${target_compile_flags}")
42       endif()
43     endforeach()
44   else()
45     # Update target props, since all sources are C++.
46     set_property(TARGET ${name} APPEND_STRING PROPERTY
47       COMPILE_FLAGS "${target_compile_flags}")
48   endif()
49
50   set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS})
51 endfunction()
52
53 function(add_llvm_symbol_exports target_name export_file)
54   if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
55     set(native_export_file "${target_name}.exports")
56     add_custom_command(OUTPUT ${native_export_file}
57       COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file}
58       DEPENDS ${export_file}
59       VERBATIM
60       COMMENT "Creating export file for ${target_name}")
61     set_property(TARGET ${target_name} APPEND_STRING PROPERTY
62                  LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
63   elseif(LLVM_HAVE_LINK_VERSION_SCRIPT)
64     # Gold and BFD ld require a version script rather than a plain list.
65     set(native_export_file "${target_name}.exports")
66     # FIXME: Don't write the "local:" line on OpenBSD.
67     add_custom_command(OUTPUT ${native_export_file}
68       COMMAND echo "{" > ${native_export_file}
69       COMMAND grep -q "[[:alnum:]]" ${export_file} && echo "  global:" >> ${native_export_file} || :
70       COMMAND sed -e "s/$/;/" -e "s/^/    /" < ${export_file} >> ${native_export_file}
71       COMMAND echo "  local: *;" >> ${native_export_file}
72       COMMAND echo "};" >> ${native_export_file}
73       DEPENDS ${export_file}
74       VERBATIM
75       COMMENT "Creating export file for ${target_name}")
76     set_property(TARGET ${target_name} APPEND_STRING PROPERTY
77                  LINK_FLAGS "  -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
78   else()
79     set(native_export_file "${target_name}.def")
80
81     set(CAT "type")
82     if(CYGWIN)
83       set(CAT "cat")
84     endif()
85
86     # Using ${export_file} in add_custom_command directly confuses cmd.exe.
87     file(TO_NATIVE_PATH ${export_file} export_file_backslashes)
88
89     add_custom_command(OUTPUT ${native_export_file}
90       COMMAND ${CMAKE_COMMAND} -E echo "EXPORTS" > ${native_export_file}
91       COMMAND ${CAT} ${export_file_backslashes} >> ${native_export_file}
92       DEPENDS ${export_file}
93       VERBATIM
94       COMMENT "Creating export file for ${target_name}")
95     if(CYGWIN OR MINGW)
96       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
97                    LINK_FLAGS " ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
98     else()
99       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
100                    LINK_FLAGS " /DEF:${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}")
101     endif()
102   endif()
103
104   add_custom_target(${target_name}_exports DEPENDS ${native_export_file})
105   set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc")
106
107   get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
108   foreach(src ${srcs})
109     get_filename_component(extension ${src} EXT)
110     if(extension STREQUAL ".cpp")
111       set(first_source_file ${src})
112       break()
113     endif()
114   endforeach()
115
116   # Force re-linking when the exports file changes. Actually, it
117   # forces recompilation of the source file. The LINK_DEPENDS target
118   # property only works for makefile-based generators.
119   # FIXME: This is not safe because this will create the same target
120   # ${native_export_file} in several different file:
121   # - One where we emitted ${target_name}_exports
122   # - One where we emitted the build command for the following object.
123   # set_property(SOURCE ${first_source_file} APPEND PROPERTY
124   #   OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file})
125
126   set_property(DIRECTORY APPEND
127     PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file})
128
129   add_dependencies(${target_name} ${target_name}_exports)
130 endfunction(add_llvm_symbol_exports)
131
132 function(add_dead_strip target_name)
133   if(NOT LLVM_NO_DEAD_STRIP)
134     if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
135       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
136                    LINK_FLAGS " -Wl,-dead_strip")
137     elseif(NOT WIN32)
138       # Object files are compiled with -ffunction-data-sections.
139       set_property(TARGET ${target_name} APPEND_STRING PROPERTY
140                    LINK_FLAGS " -Wl,--gc-sections")
141     endif()
142   endif()
143 endfunction(add_dead_strip)
144
145 # Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}.
146 # Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more,
147 # or a certain builder, for eaxample, msbuild.exe, would be confused.
148 function(set_output_directory target bindir libdir)
149   if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
150     foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
151       string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
152       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${bindir})
153       string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${libdir})
154       set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
155       set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
156       set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
157     endforeach()
158   else()
159     set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${bindir})
160     set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libdir})
161     set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libdir})
162   endif()
163 endfunction()
164
165 macro(add_llvm_library name)
166   llvm_process_sources( ALL_FILES ${ARGN} )
167   add_library( ${name} ${ALL_FILES} )
168   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
169   set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
170   llvm_update_compile_flags(${name})
171   add_dead_strip( ${name} )
172   if( LLVM_COMMON_DEPENDS )
173     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
174   endif( LLVM_COMMON_DEPENDS )
175
176   if( BUILD_SHARED_LIBS )
177     llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
178     if (MSVC)
179       set_target_properties(${name}
180         PROPERTIES
181         IMPORT_SUFFIX ".imp")
182     endif ()
183
184     if (LLVM_EXPORTED_SYMBOL_FILE)
185       add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
186     endif()
187   endif()
188
189   # Ensure that the system libraries always comes last on the
190   # list. Without this, linking the unit tests on MinGW fails.
191   link_system_libs( ${name} )
192
193   if( EXCLUDE_FROM_ALL )
194     set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
195   else()
196     if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "LTO")
197       install(TARGETS ${name}
198         EXPORT LLVMExports
199         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
200         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
201     endif()
202     set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
203   endif()
204   set_target_properties(${name} PROPERTIES FOLDER "Libraries")
205
206   # Add the explicit dependency information for this library.
207   #
208   # It would be nice to verify that we have the dependencies for this library
209   # name, but using get_property(... SET) doesn't suffice to determine if a
210   # property has been set to an empty value.
211   get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
212   target_link_libraries(${name} ${lib_deps})
213 endmacro(add_llvm_library name)
214
215 macro(add_llvm_loadable_module name)
216   if( NOT LLVM_ON_UNIX OR CYGWIN )
217     message(STATUS "Loadable modules not supported on this platform.
218 ${name} ignored.")
219     # Add empty "phony" target
220     add_custom_target(${name})
221   else()
222     llvm_process_sources( ALL_FILES ${ARGN} )
223     add_library(${name} MODULE ${ALL_FILES})
224     set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
225     set_target_properties( ${name} PROPERTIES PREFIX "" )
226     llvm_update_compile_flags(${name})
227     add_dead_strip( ${name} )
228
229     if (LLVM_EXPORTED_SYMBOL_FILE)
230       add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
231     endif(LLVM_EXPORTED_SYMBOL_FILE)
232
233     llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
234     link_system_libs( ${name} )
235
236     if (APPLE)
237       # Darwin-specific linker flags for loadable modules.
238       set_property(TARGET ${name} APPEND_STRING PROPERTY
239         LINK_FLAGS " -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
240     endif()
241
242     if (MODULE)
243       set_property(TARGET ${name} PROPERTY SUFFIX ${LLVM_PLUGIN_EXT})
244     endif ()
245
246     if( EXCLUDE_FROM_ALL )
247       set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
248     else()
249       if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
250         install(TARGETS ${name}
251           EXPORT LLVMExports
252           LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
253           ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
254       endif()
255       set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
256     endif()
257   endif()
258
259   set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
260 endmacro(add_llvm_loadable_module name)
261
262
263 macro(add_llvm_executable name)
264   llvm_process_sources( ALL_FILES ${ARGN} )
265   if( EXCLUDE_FROM_ALL )
266     add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
267   else()
268     add_executable(${name} ${ALL_FILES})
269   endif()
270   llvm_update_compile_flags(${name})
271   add_dead_strip( ${name} )
272
273   if (LLVM_EXPORTED_SYMBOL_FILE)
274     add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
275   endif(LLVM_EXPORTED_SYMBOL_FILE)
276
277   set(EXCLUDE_FROM_ALL OFF)
278   set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR})
279   llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
280   if( LLVM_COMMON_DEPENDS )
281     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
282   endif( LLVM_COMMON_DEPENDS )
283   link_system_libs( ${name} )
284 endmacro(add_llvm_executable name)
285
286
287 set (LLVM_TOOLCHAIN_TOOLS
288   llvm-ar
289   llvm-objdump
290   )
291
292 macro(add_llvm_tool name)
293   if( NOT LLVM_BUILD_TOOLS )
294     set(EXCLUDE_FROM_ALL ON)
295   endif()
296   add_llvm_executable(${name} ${ARGN})
297
298   list(FIND LLVM_TOOLCHAIN_TOOLS ${name} LLVM_IS_${name}_TOOLCHAIN_TOOL)
299   if (LLVM_IS_${name}_TOOLCHAIN_TOOL GREATER -1 OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
300     if( LLVM_BUILD_TOOLS )
301       install(TARGETS ${name}
302               EXPORT LLVMExports
303               RUNTIME DESTINATION bin)
304     endif()
305   endif()
306   if( LLVM_BUILD_TOOLS )
307     set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name})
308   endif()
309   set_target_properties(${name} PROPERTIES FOLDER "Tools")
310 endmacro(add_llvm_tool name)
311
312
313 macro(add_llvm_example name)
314   if( NOT LLVM_BUILD_EXAMPLES )
315     set(EXCLUDE_FROM_ALL ON)
316   endif()
317   add_llvm_executable(${name} ${ARGN})
318   if( LLVM_BUILD_EXAMPLES )
319     install(TARGETS ${name} RUNTIME DESTINATION examples)
320   endif()
321   set_target_properties(${name} PROPERTIES FOLDER "Examples")
322 endmacro(add_llvm_example name)
323
324
325 macro(add_llvm_utility name)
326   add_llvm_executable(${name} ${ARGN})
327   set_target_properties(${name} PROPERTIES FOLDER "Utils")
328 endmacro(add_llvm_utility name)
329
330
331 macro(add_llvm_target target_name)
332   include_directories(BEFORE
333     ${CMAKE_CURRENT_BINARY_DIR}
334     ${CMAKE_CURRENT_SOURCE_DIR})
335   add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
336   set( CURRENT_LLVM_TARGET LLVM${target_name} )
337 endmacro(add_llvm_target)
338
339 # Add external project that may want to be built as part of llvm such as Clang,
340 # lld, and Polly. This adds two options. One for the source directory of the
341 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
342 # enable or disable building it with everything else.
343 # Additional parameter can be specified as the name of directory.
344 macro(add_llvm_external_project name)
345   set(add_llvm_external_dir "${ARGN}")
346   if("${add_llvm_external_dir}" STREQUAL "")
347     set(add_llvm_external_dir ${name})
348   endif()
349   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
350   string(REPLACE "-" "_" nameUNDERSCORE ${name})
351   string(TOUPPER ${nameUNDERSCORE} nameUPPER)
352   set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
353       CACHE PATH "Path to ${name} source directory")
354   if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
355       AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
356     option(LLVM_EXTERNAL_${nameUPPER}_BUILD
357            "Whether to build ${name} as part of LLVM" ON)
358     if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
359       add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
360     endif()
361   endif()
362 endmacro(add_llvm_external_project)
363
364 macro(add_llvm_tool_subdirectory name)
365   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
366   add_subdirectory(${name})
367 endmacro(add_llvm_tool_subdirectory)
368
369 macro(ignore_llvm_tool_subdirectory name)
370   list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
371 endmacro(ignore_llvm_tool_subdirectory)
372
373 function(add_llvm_implicit_external_projects)
374   set(list_of_implicit_subdirs "")
375   file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
376   foreach(dir ${sub-dirs})
377     if(IS_DIRECTORY "${dir}")
378       list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
379       if( tool_subdir_ignore EQUAL -1
380           AND EXISTS "${dir}/CMakeLists.txt")
381         get_filename_component(fn "${dir}" NAME)
382         list(APPEND list_of_implicit_subdirs "${fn}")
383       endif()
384     endif()
385   endforeach()
386
387   foreach(external_proj ${list_of_implicit_subdirs})
388     add_llvm_external_project("${external_proj}")
389   endforeach()
390 endfunction(add_llvm_implicit_external_projects)
391
392 # Generic support for adding a unittest.
393 function(add_unittest test_suite test_name)
394   if( NOT LLVM_BUILD_TESTS )
395     set(EXCLUDE_FROM_ALL ON)
396   endif()
397
398   # Visual Studio 2012 only supports up to 8 template parameters in
399   # std::tr1::tuple by default, but gtest requires 10
400   if (MSVC AND MSVC_VERSION EQUAL 1700)
401     list(APPEND LLVM_COMPILE_DEFINITIONS _VARIADIC_MAX=10)
402   endif ()
403
404   include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
405   if (NOT LLVM_ENABLE_THREADS)
406     list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
407   endif ()
408
409   if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
410     list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros")
411   endif ()
412
413   set(LLVM_REQUIRES_RTTI OFF)
414
415   add_llvm_executable(${test_name} ${ARGN})
416   set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
417   set_output_directory(${test_name} ${outdir} ${outdir})
418   target_link_libraries(${test_name}
419     gtest
420     gtest_main
421     LLVMSupport # gtest needs it for raw_ostream.
422     )
423
424   add_dependencies(${test_suite} ${test_name})
425   get_target_property(test_suite_folder ${test_suite} FOLDER)
426   if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
427     set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
428   endif ()
429 endfunction()
430
431 # This function provides an automatic way to 'configure'-like generate a file
432 # based on a set of common and custom variables, specifically targeting the
433 # variables needed for the 'lit.site.cfg' files. This function bundles the
434 # common variables that any Lit instance is likely to need, and custom
435 # variables can be passed in.
436 function(configure_lit_site_cfg input output)
437   foreach(c ${LLVM_TARGETS_TO_BUILD})
438     set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
439   endforeach(c)
440   set(TARGETS_TO_BUILD ${TARGETS_BUILT})
441
442   set(SHLIBEXT "${LTDL_SHLIB_EXT}")
443
444   if(BUILD_SHARED_LIBS)
445     set(LLVM_SHARED_LIBS_ENABLED "1")
446   else()
447     set(LLVM_SHARED_LIBS_ENABLED "0")
448   endif(BUILD_SHARED_LIBS)
449
450   if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
451     set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
452   else() # Default for all other unix like systems.
453     # CMake hardcodes the library locaction using rpath.
454     # Therefore LD_LIBRARY_PATH is not required to run binaries in the
455     # build dir. We pass it anyways.
456     set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
457   endif()
458
459   # Configuration-time: See Unit/lit.site.cfg.in
460   if (CMAKE_CFG_INTDIR STREQUAL ".")
461     set(LLVM_BUILD_MODE ".")
462   else ()
463     set(LLVM_BUILD_MODE "%(build_mode)s")
464   endif ()
465
466   # They below might not be the build tree but provided binary tree.
467   set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
468   set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
469   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_TOOLS_DIR ${LLVM_TOOLS_BINARY_DIR})
470   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} LLVM_LIBS_DIR  ${LLVM_LIBRARY_DIR})
471
472   # SHLIBDIR points the build tree.
473   string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} SHLIBDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
474
475   set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
476   set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
477   set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
478
479   if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
480     set(ENABLE_ASSERTIONS "1")
481   else()
482     set(ENABLE_ASSERTIONS "0")
483   endif()
484
485   set(HOST_OS ${CMAKE_SYSTEM_NAME})
486   set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
487
488   if (CLANG_ENABLE_ARCMT)
489     set(ENABLE_CLANG_ARCMT "1")
490   else()
491     set(ENABLE_CLANG_ARCMT "0")
492   endif()
493   if (CLANG_ENABLE_REWRITER)
494     set(ENABLE_CLANG_REWRITER "1")
495   else()
496     set(ENABLE_CLANG_REWRITER "0")
497   endif()
498   if (CLANG_ENABLE_STATIC_ANALYZER)
499     set(ENABLE_CLANG_STATIC_ANALYZER "1")
500   else()
501     set(ENABLE_CLANG_STATIC_ANALYZER "0")
502   endif()
503
504   configure_file(${input} ${output} @ONLY)
505 endfunction()
506
507 # A raw function to create a lit target. This is used to implement the testuite
508 # management functions.
509 function(add_lit_target target comment)
510   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
511   set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
512   separate_arguments(LIT_ARGS)
513   if (NOT CMAKE_CFG_INTDIR STREQUAL ".")
514     list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR})
515   endif ()
516   set(LIT_COMMAND
517     ${PYTHON_EXECUTABLE}
518     ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
519     ${LIT_ARGS}
520     )
521   foreach(param ${ARG_PARAMS})
522     list(APPEND LIT_COMMAND --param ${param})
523   endforeach()
524   if( ARG_DEPENDS )
525     add_custom_target(${target}
526       COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
527       COMMENT "${comment}"
528       )
529     add_dependencies(${target} ${ARG_DEPENDS})
530   else()
531     add_custom_target(${target}
532       COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.")
533     message(STATUS "${target} does nothing.")
534   endif()
535
536   # Tests should be excluded from "Build Solution".
537   set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
538 endfunction()
539
540 # A function to add a set of lit test suites to be driven through 'check-*' targets.
541 function(add_lit_testsuite target comment)
542   parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
543
544   # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
545   if(NOT EXCLUDE_FROM_ALL)
546     # Register the testsuites, params and depends for the global check rule.
547     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
548     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
549     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
550     set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
551   endif()
552
553   # Produce a specific suffixed check rule.
554   add_lit_target(${target} ${comment}
555     ${ARG_DEFAULT_ARGS}
556     PARAMS ${ARG_PARAMS}
557     DEPENDS ${ARG_DEPENDS}
558     ARGS ${ARG_ARGS}
559     )
560 endfunction()