blob: 07b589beb2d10824303a9c5a6264f0a041e2dda0 [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
George Karpenkov831875b2017-07-28 17:32:37 +000027# Compile a sanitizer test with a freshly built clang
28# for a given architecture, adding the result to the object list.
29# - obj_list: output list of objects, populated by path
George Karpenkovcb6be4d2017-08-15 18:35:02 +000030# of a generated object file.
George Karpenkov831875b2017-07-28 17:32:37 +000031# - source: source file of a test.
32# - arch: architecture to compile for.
33# sanitizer_test_compile(<obj_list> <source> <arch>
34# KIND <custom namespace>
35# COMPILE_DEPS <list of compile-time dependencies>
36# DEPS <list of dependencies>
37# CFLAGS <list of flags>
38# )
George Karpenkovcb6be4d2017-08-15 18:35:02 +000039function(sanitizer_test_compile obj_list source arch)
George Karpenkov831875b2017-07-28 17:32:37 +000040 cmake_parse_arguments(TEST
41 "" "" "KIND;COMPILE_DEPS;DEPS;CFLAGS" ${ARGN})
42 get_filename_component(basename ${source} NAME)
George Karpenkovcb6be4d2017-08-15 18:35:02 +000043 set(output_obj
44 "${CMAKE_CFG_RESOLVED_INTDIR}${obj_list}.${basename}.${arch}${TEST_KIND}.o")
George Karpenkov831875b2017-07-28 17:32:37 +000045
46 # Write out architecture-specific flags into TARGET_CFLAGS variable.
47 get_target_flags_for_arch(${arch} TARGET_CFLAGS)
48 set(COMPILE_DEPS ${TEST_COMPILE_DEPS})
49 if(NOT COMPILER_RT_STANDALONE_BUILD)
50 list(APPEND COMPILE_DEPS ${TEST_DEPS})
51 endif()
52 clang_compile(${output_obj} ${source}
53 CFLAGS ${TEST_CFLAGS} ${TARGET_CFLAGS}
Michal Gorny858509c2017-10-12 18:51:37 +000054 DEPS ${COMPILE_DEPS})
George Karpenkov831875b2017-07-28 17:32:37 +000055 list(APPEND ${obj_list} ${output_obj})
George Karpenkovcb6be4d2017-08-15 18:35:02 +000056 set("${obj_list}" "${${obj_list}}" PARENT_SCOPE)
57endfunction()
George Karpenkov831875b2017-07-28 17:32:37 +000058
Alexey Samsonov150d62a2014-02-19 13:01:03 +000059# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000060# a provided compile flags and dependenices.
61# clang_compile(<object> <source>
62# CFLAGS <list of compile flags>
63# DEPS <list of dependencies>)
George Karpenkov83ea2812017-08-15 18:38:14 +000064function(clang_compile object_file source)
Filipe Cabecinhas7af0a1c2015-06-19 03:39:24 +000065 cmake_parse_arguments(SOURCE "" "" "CFLAGS;DEPS" ${ARGN})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000066 get_filename_component(source_rpath ${source} REALPATH)
Alexey Samsonov150d62a2014-02-19 13:01:03 +000067 if(NOT COMPILER_RT_STANDALONE_BUILD)
Alexey Samsonov6239ebc2015-01-06 20:58:40 +000068 list(APPEND SOURCE_DEPS clang compiler-rt-headers)
Alexey Samsonov150d62a2014-02-19 13:01:03 +000069 endif()
Adrian Prantl1d6ae012017-08-25 02:36:36 +000070 if (TARGET CompilerRTUnitTestCheckCxx)
71 list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx)
Kuba Breckafa2de772014-09-10 17:23:58 +000072 endif()
Alexey Samsonov18474012014-03-24 09:42:12 +000073 string(REGEX MATCH "[.](cc|cpp)$" is_cxx ${source_rpath})
George Karpenkov83ea2812017-08-15 18:38:14 +000074 string(REGEX MATCH "[.](m|mm)$" is_objc ${source_rpath})
Alexey Samsonov18474012014-03-24 09:42:12 +000075 if(is_cxx)
76 string(REPLACE " " ";" global_flags "${CMAKE_CXX_FLAGS}")
77 else()
78 string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}")
79 endif()
Reid Klecknera1410322015-03-13 21:39:29 +000080
81 if (MSVC)
82 translate_msvc_cflags(global_flags "${global_flags}")
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +000083 endif()
Reid Klecknera1410322015-03-13 21:39:29 +000084
Chris Bieneman73107c62015-08-20 17:35:56 +000085 if (APPLE)
86 set(global_flags ${OSX_SYSROOT_FLAG} ${global_flags})
87 endif()
George Karpenkov83ea2812017-08-15 18:38:14 +000088 if (is_objc)
89 list(APPEND global_flags -ObjC)
90 endif()
Chris Bieneman73107c62015-08-20 17:35:56 +000091
Alexey Samsonov18474012014-03-24 09:42:12 +000092 # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options
93 # which are not supported by Clang.
94 list(APPEND global_flags -Wno-unknown-warning-option)
95 set(compile_flags ${global_flags} ${SOURCE_CFLAGS})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000096 add_custom_command(
97 OUTPUT ${object_file}
Timur Iskhodzhanovb0279d72014-05-12 08:55:20 +000098 COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c
Timur Iskhodzhanov82ee0432014-05-28 08:38:13 +000099 -o "${object_file}"
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000100 ${source_rpath}
Alexey Samsonovca7fcf22012-12-19 12:33:39 +0000101 MAIN_DEPENDENCY ${source}
Alexey Samsonov150d62a2014-02-19 13:01:03 +0000102 DEPENDS ${SOURCE_DEPS})
George Karpenkov83ea2812017-08-15 18:38:14 +0000103endfunction()
Kuba Breckafa2de772014-09-10 17:23:58 +0000104
105# On Darwin, there are no system-wide C++ headers and the just-built clang is
106# therefore not able to compile C++ files unless they are copied/symlinked into
107# ${LLVM_BINARY_DIR}/include/c++
108# The just-built clang is used to build compiler-rt unit tests. Let's detect
109# this before we try to build the tests and print out a suggestion how to fix
110# it.
111# On other platforms, this is currently not an issue.
112macro(clang_compiler_add_cxx_check)
113 if (APPLE)
114 set(CMD
Chris Bieneman73107c62015-08-20 17:35:56 +0000115 "echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} ${OSX_SYSROOT_FLAG} -E -x c++ - > /dev/null"
Kuba Breckafa2de772014-09-10 17:23:58 +0000116 "if [ $? != 0 ] "
117 " then echo"
118 " echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'"
119 " echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'"
120 " if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]"
121 " then echo 'e.g. with:'"
122 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'"
123 " elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]"
124 " then echo 'e.g. with:'"
125 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'"
126 " fi"
127 " echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'"
128 " echo 'into your build directory:'"
Alexey Samsonova7be03d2016-02-01 21:08:16 +0000129 " echo ' cd ${LLVM_MAIN_SRC_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'"
130 " echo ' cd ${LLVM_BINARY_DIR} && make -C ${LLVM_MAIN_SRC_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'"
Kuba Breckafa2de772014-09-10 17:23:58 +0000131 " echo"
132 " false"
133 "fi"
134 )
Adrian Prantl1d6ae012017-08-25 02:36:36 +0000135 add_custom_target(CompilerRTUnitTestCheckCxx
Kuba Breckafa2de772014-09-10 17:23:58 +0000136 COMMAND bash -c "${CMD}"
137 COMMENT "Checking that just-built clang can find C++ headers..."
Kuba Breckafa2de772014-09-10 17:23:58 +0000138 VERBATIM)
Petr Hosek38c723b2019-02-13 06:49:47 +0000139 if (NOT COMPILER_RT_STANDALONE_BUILD AND NOT RUNTIMES_BUILD)
Adrian Prantl1d6ae012017-08-25 02:36:36 +0000140 ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang)
141 endif()
Kuba Breckafa2de772014-09-10 17:23:58 +0000142 endif()
143endmacro()