blob: de73ccfc5cb8983236f53f1fc5f4883490c5ba67 [file] [log] [blame]
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00001include(LLVMParseArguments)
2
Alexey Samsonov150d62a2014-02-19 13:01:03 +00003# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using
Alexey Samsonovca7fcf22012-12-19 12:33:39 +00004# a provided compile flags and dependenices.
5# clang_compile(<object> <source>
6# CFLAGS <list of compile flags>
7# DEPS <list of dependencies>)
8macro(clang_compile object_file source)
9 parse_arguments(SOURCE "CFLAGS;DEPS" "" ${ARGN})
10 get_filename_component(source_rpath ${source} REALPATH)
Alexey Samsonov150d62a2014-02-19 13:01:03 +000011 if(NOT COMPILER_RT_STANDALONE_BUILD)
Alexey Samsonov6239ebc2015-01-06 20:58:40 +000012 list(APPEND SOURCE_DEPS clang compiler-rt-headers)
Alexey Samsonov150d62a2014-02-19 13:01:03 +000013 endif()
Kuba Breckafa2de772014-09-10 17:23:58 +000014 if (TARGET CompilerRTUnitTestCheckCxx)
15 list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx)
16 endif()
Alexey Samsonov18474012014-03-24 09:42:12 +000017 string(REGEX MATCH "[.](cc|cpp)$" is_cxx ${source_rpath})
18 if(is_cxx)
19 string(REPLACE " " ";" global_flags "${CMAKE_CXX_FLAGS}")
20 else()
21 string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}")
22 endif()
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +000023 # On Windows, CMAKE_*_FLAGS are built for MSVC but we use the GCC clang.exe
24 # which doesn't support flags starting with "/smth". Replace those with
25 # "-smth" equivalents.
26 if(MSVC)
27 string(REGEX REPLACE "^/" "-" global_flags "${global_flags}")
28 string(REPLACE ";/" ";-" global_flags "${global_flags}")
29 endif()
Alexey Samsonov18474012014-03-24 09:42:12 +000030 # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options
31 # which are not supported by Clang.
32 list(APPEND global_flags -Wno-unknown-warning-option)
33 set(compile_flags ${global_flags} ${SOURCE_CFLAGS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000034 add_custom_command(
35 OUTPUT ${object_file}
Timur Iskhodzhanovb0279d72014-05-12 08:55:20 +000036 COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +000037 -o "${object_file}"
Alexey Samsonov150d62a2014-02-19 13:01:03 +000038 ${source_rpath}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000039 MAIN_DEPENDENCY ${source}
Alexey Samsonov150d62a2014-02-19 13:01:03 +000040 DEPENDS ${SOURCE_DEPS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000041endmacro()
Kuba Breckafa2de772014-09-10 17:23:58 +000042
43# On Darwin, there are no system-wide C++ headers and the just-built clang is
44# therefore not able to compile C++ files unless they are copied/symlinked into
45# ${LLVM_BINARY_DIR}/include/c++
46# The just-built clang is used to build compiler-rt unit tests. Let's detect
47# this before we try to build the tests and print out a suggestion how to fix
48# it.
49# On other platforms, this is currently not an issue.
50macro(clang_compiler_add_cxx_check)
51 if (APPLE)
52 set(CMD
53 "echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} -E -x c++ - > /dev/null"
54 "if [ $? != 0 ] "
55 " then echo"
56 " echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'"
57 " echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'"
58 " if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]"
59 " then echo 'e.g. with:'"
60 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'"
61 " elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]"
62 " then echo 'e.g. with:'"
63 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'"
64 " fi"
65 " echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'"
66 " echo 'into your build directory:'"
67 " echo ' cd ${LLVM_SOURCE_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'"
68 " echo ' cd ${LLVM_BINARY_DIR} && make -C ${LLVM_SOURCE_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'"
69 " echo"
70 " false"
71 "fi"
72 )
73 add_custom_target(CompilerRTUnitTestCheckCxx
74 COMMAND bash -c "${CMD}"
75 COMMENT "Checking that just-built clang can find C++ headers..."
Kuba Breckafa2de772014-09-10 17:23:58 +000076 VERBATIM)
Alexander Potapenkobc4ce3a2014-09-25 09:30:05 +000077 if (TARGET clang)
78 ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang)
79 endif()
Kuba Breckafa2de772014-09-10 17:23:58 +000080 endif()
81endmacro()