blob: 48f40bf4f7530a66c0c837d4840e075430e1df56 [file] [log] [blame]
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -07001# 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
Stephen Hines2d1fdb22014-05-28 23:58:16 -070027# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using
Alexey Samsonov02dcc632012-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)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080033 cmake_parse_arguments(SOURCE "" "" "CFLAGS;DEPS" ${ARGN})
Alexey Samsonov02dcc632012-12-19 12:33:39 +000034 get_filename_component(source_rpath ${source} REALPATH)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070035 if(NOT COMPILER_RT_STANDALONE_BUILD)
Stephen Hines86277eb2015-03-23 12:06:32 -070036 list(APPEND SOURCE_DEPS clang compiler-rt-headers)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070037 endif()
Stephen Hines6d186232014-11-26 17:56:19 -080038 if (TARGET CompilerRTUnitTestCheckCxx)
39 list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx)
40 endif()
Stephen Hines2d1fdb22014-05-28 23:58:16 -070041 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()
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070047
48 if (MSVC)
49 translate_msvc_cflags(global_flags "${global_flags}")
Stephen Hines6a211c52014-07-21 00:49:56 -070050 endif()
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070051
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080052 if (APPLE)
53 set(global_flags ${OSX_SYSROOT_FLAG} ${global_flags})
54 endif()
55
Stephen Hines2d1fdb22014-05-28 23:58:16 -070056 # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options
57 # which are not supported by Clang.
58 list(APPEND global_flags -Wno-unknown-warning-option)
59 set(compile_flags ${global_flags} ${SOURCE_CFLAGS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +000060 add_custom_command(
61 OUTPUT ${object_file}
Stephen Hines2d1fdb22014-05-28 23:58:16 -070062 COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c
Stephen Hines6a211c52014-07-21 00:49:56 -070063 -o "${object_file}"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070064 ${source_rpath}
Alexey Samsonov02dcc632012-12-19 12:33:39 +000065 MAIN_DEPENDENCY ${source}
Stephen Hines2d1fdb22014-05-28 23:58:16 -070066 DEPENDS ${SOURCE_DEPS})
Alexey Samsonov02dcc632012-12-19 12:33:39 +000067endmacro()
Stephen Hines6d186232014-11-26 17:56:19 -080068
69# On Darwin, there are no system-wide C++ headers and the just-built clang is
70# therefore not able to compile C++ files unless they are copied/symlinked into
71# ${LLVM_BINARY_DIR}/include/c++
72# The just-built clang is used to build compiler-rt unit tests. Let's detect
73# this before we try to build the tests and print out a suggestion how to fix
74# it.
75# On other platforms, this is currently not an issue.
76macro(clang_compiler_add_cxx_check)
77 if (APPLE)
78 set(CMD
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080079 "echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} ${OSX_SYSROOT_FLAG} -E -x c++ - > /dev/null"
Stephen Hines6d186232014-11-26 17:56:19 -080080 "if [ $? != 0 ] "
81 " then echo"
82 " echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'"
83 " echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'"
84 " if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]"
85 " then echo 'e.g. with:'"
86 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'"
87 " elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]"
88 " then echo 'e.g. with:'"
89 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'"
90 " fi"
91 " echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'"
92 " echo 'into your build directory:'"
93 " echo ' cd ${LLVM_SOURCE_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'"
94 " echo ' cd ${LLVM_BINARY_DIR} && make -C ${LLVM_SOURCE_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'"
95 " echo"
96 " false"
97 "fi"
98 )
99 add_custom_target(CompilerRTUnitTestCheckCxx
100 COMMAND bash -c "${CMD}"
101 COMMENT "Checking that just-built clang can find C++ headers..."
102 VERBATIM)
103 if (TARGET clang)
104 ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang)
105 endif()
106 endif()
107endmacro()