1include(LLVMParseArguments)
2
3# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using
4# 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)
11  if(NOT COMPILER_RT_STANDALONE_BUILD)
12    list(APPEND SOURCE_DEPS clang compiler-rt-headers)
13  endif()
14  if (TARGET CompilerRTUnitTestCheckCxx)
15    list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx)
16  endif()
17  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()
23  # 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()
30  # 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})
34  add_custom_command(
35    OUTPUT ${object_file}
36    COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c
37            -o "${object_file}"
38            ${source_rpath}
39    MAIN_DEPENDENCY ${source}
40    DEPENDS ${SOURCE_DEPS})
41endmacro()
42
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..."
76      VERBATIM)
77    if (TARGET clang)
78      ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang)
79    endif()
80  endif()
81endmacro()
82