blob: b2e62dd0bac643d8ad577ff98ca427c297c69282 [file] [log] [blame]
Reid Klecknera1410322015-03-13 21:39:29 +00001# On Windows, CMAKE_*_FLAGS are built for MSVC but we use the GCC clang.exe,
2# which uses completely different flags. Translate some common flag types, and
3# drop the rest.
4function(translate_msvc_cflags out_flags msvc_flags)
5 # Insert an empty string in the list to simplify processing.
6 set(msvc_flags ";${msvc_flags}")
7
8 # Canonicalize /flag to -flag.
9 string(REPLACE ";/" ";-" msvc_flags "${msvc_flags}")
10
11 # Make space separated -D and -U flags into joined flags.
12 string(REGEX REPLACE ";-\([DU]\);" ";-\\1" msvc_flags "${msvc_flags}")
13
14 set(clang_flags "")
15 foreach(flag ${msvc_flags})
16 if ("${flag}" MATCHES "^-[DU]")
17 # Pass through basic command line macro definitions (-DNDEBUG).
18 list(APPEND clang_flags "${flag}")
19 elseif ("${flag}" MATCHES "^-O[2x]")
20 # Canonicalize normal optimization flags to -O2.
21 list(APPEND clang_flags "-O2")
22 endif()
23 endforeach()
24 set(${out_flags} "${clang_flags}" PARENT_SCOPE)
25endfunction()
26
Alexey Samsonov150d62a2014-02-19 13:01:03 +000027# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000028# a provided compile flags and dependenices.
29# clang_compile(<object> <source>
30# CFLAGS <list of compile flags>
31# DEPS <list of dependencies>)
32macro(clang_compile object_file source)
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +000033 cmake_parse_arguments(SOURCE "" "" "CFLAGS;DEPS" ${ARGN})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000034 get_filename_component(source_rpath ${source} REALPATH)
Alexey Samsonov150d62a2014-02-19 13:01:03 +000035 if(NOT COMPILER_RT_STANDALONE_BUILD)
Alexey Samsonov6239ebc2015-01-06 20:58:40 +000036 list(APPEND SOURCE_DEPS clang compiler-rt-headers)
Alexey Samsonov150d62a2014-02-19 13:01:03 +000037 endif()
Kuba Breckafa2de772014-09-10 17:23:58 +000038 if (TARGET CompilerRTUnitTestCheckCxx)
39 list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx)
40 endif()
Alexey Samsonov18474012014-03-24 09:42:12 +000041 string(REGEX MATCH "[.](cc|cpp)$" is_cxx ${source_rpath})
42 if(is_cxx)
43 string(REPLACE " " ";" global_flags "${CMAKE_CXX_FLAGS}")
44 else()
45 string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}")
46 endif()
Reid Klecknera1410322015-03-13 21:39:29 +000047
48 if (MSVC)
49 translate_msvc_cflags(global_flags "${global_flags}")
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +000050 endif()
Reid Klecknera1410322015-03-13 21:39:29 +000051
Alexey Samsonov18474012014-03-24 09:42:12 +000052 # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options
53 # which are not supported by Clang.
54 list(APPEND global_flags -Wno-unknown-warning-option)
55 set(compile_flags ${global_flags} ${SOURCE_CFLAGS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000056 add_custom_command(
57 OUTPUT ${object_file}
Timur Iskhodzhanovb0279d72014-05-12 08:55:20 +000058 COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +000059 -o "${object_file}"
Alexey Samsonov150d62a2014-02-19 13:01:03 +000060 ${source_rpath}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000061 MAIN_DEPENDENCY ${source}
Alexey Samsonov150d62a2014-02-19 13:01:03 +000062 DEPENDS ${SOURCE_DEPS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000063endmacro()
Kuba Breckafa2de772014-09-10 17:23:58 +000064
65# On Darwin, there are no system-wide C++ headers and the just-built clang is
66# therefore not able to compile C++ files unless they are copied/symlinked into
67# ${LLVM_BINARY_DIR}/include/c++
68# The just-built clang is used to build compiler-rt unit tests. Let's detect
69# this before we try to build the tests and print out a suggestion how to fix
70# it.
71# On other platforms, this is currently not an issue.
72macro(clang_compiler_add_cxx_check)
73 if (APPLE)
74 set(CMD
75 "echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} -E -x c++ - > /dev/null"
76 "if [ $? != 0 ] "
77 " then echo"
78 " echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'"
79 " echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'"
80 " if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]"
81 " then echo 'e.g. with:'"
82 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'"
83 " elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]"
84 " then echo 'e.g. with:'"
85 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'"
86 " fi"
87 " echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'"
88 " echo 'into your build directory:'"
89 " echo ' cd ${LLVM_SOURCE_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'"
90 " echo ' cd ${LLVM_BINARY_DIR} && make -C ${LLVM_SOURCE_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'"
91 " echo"
92 " false"
93 "fi"
94 )
95 add_custom_target(CompilerRTUnitTestCheckCxx
96 COMMAND bash -c "${CMD}"
97 COMMENT "Checking that just-built clang can find C++ headers..."
Kuba Breckafa2de772014-09-10 17:23:58 +000098 VERBATIM)
Alexander Potapenkobc4ce3a2014-09-25 09:30:05 +000099 if (TARGET clang)
100 ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang)
101 endif()
Kuba Breckafa2de772014-09-10 17:23:58 +0000102 endif()
103endmacro()