1# 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 27# 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 30# of a generated object file. 31# - 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# ) 39function(sanitizer_test_compile obj_list source arch) 40 cmake_parse_arguments(TEST 41 "" "" "KIND;COMPILE_DEPS;DEPS;CFLAGS" ${ARGN}) 42 get_filename_component(basename ${source} NAME) 43 set(output_obj 44 "${CMAKE_CFG_RESOLVED_INTDIR}${obj_list}.${basename}.${arch}${TEST_KIND}.o") 45 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 OR COMPILER_RT_TEST_STANDALONE_BUILD_LIBS) 50 list(APPEND COMPILE_DEPS ${TEST_DEPS}) 51 endif() 52 clang_compile(${output_obj} ${source} 53 CFLAGS ${TEST_CFLAGS} ${TARGET_CFLAGS} 54 DEPS ${COMPILE_DEPS}) 55 list(APPEND ${obj_list} ${output_obj}) 56 set("${obj_list}" "${${obj_list}}" PARENT_SCOPE) 57endfunction() 58 59# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using 60# a provided compile flags and dependenices. 61# clang_compile(<object> <source> 62# CFLAGS <list of compile flags> 63# DEPS <list of dependencies>) 64function(clang_compile object_file source) 65 cmake_parse_arguments(SOURCE "" "" "CFLAGS;DEPS" ${ARGN}) 66 get_filename_component(source_rpath ${source} REALPATH) 67 if(NOT COMPILER_RT_STANDALONE_BUILD) 68 list(APPEND SOURCE_DEPS clang) 69 endif() 70 list(APPEND SOURCE_DEPS compiler-rt-headers) 71 if (TARGET CompilerRTUnitTestCheckCxx) 72 list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx) 73 endif() 74 string(REGEX MATCH "[.](cc|cpp)$" is_cxx ${source_rpath}) 75 if (is_cxx) 76 set(compiler ${COMPILER_RT_TEST_CXX_COMPILER}) 77 else() 78 set(compiler ${COMPILER_RT_TEST_COMPILER}) 79 endif() 80 if(COMPILER_RT_STANDALONE_BUILD) 81 # Only add global flags in standalone build. 82 if(is_cxx) 83 string(REPLACE " " ";" global_flags "${CMAKE_CXX_FLAGS}") 84 else() 85 string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}") 86 endif() 87 88 if (MSVC) 89 translate_msvc_cflags(global_flags "${global_flags}") 90 endif() 91 92 if (APPLE) 93 set(global_flags ${OSX_SYSROOT_FLAG} ${global_flags}) 94 endif() 95 96 # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options 97 # which are not supported by Clang. 98 list(APPEND global_flags -Wno-unknown-warning-option) 99 set(compile_flags ${global_flags} ${SOURCE_CFLAGS}) 100 else() 101 set(compile_flags ${SOURCE_CFLAGS}) 102 endif() 103 104 string(REGEX MATCH "[.](m|mm)$" is_objc ${source_rpath}) 105 if (is_objc) 106 list(APPEND compile_flags "-ObjC") 107 endif() 108 109 add_custom_command( 110 OUTPUT ${object_file} 111 COMMAND ${compiler} ${compile_flags} -c 112 -o "${object_file}" 113 ${source_rpath} 114 MAIN_DEPENDENCY ${source} 115 DEPENDS ${SOURCE_DEPS} 116 COMMAND_EXPAND_LISTS) 117endfunction() 118 119# On Darwin, there are no system-wide C++ headers and the just-built clang is 120# therefore not able to compile C++ files unless they are copied/symlinked into 121# ${LLVM_BINARY_DIR}/include/c++ 122# The just-built clang is used to build compiler-rt unit tests. Let's detect 123# this before we try to build the tests and print out a suggestion how to fix 124# it. 125# On other platforms, this is currently not an issue. 126macro(clang_compiler_add_cxx_check) 127 if (APPLE) 128 set(CMD 129 "echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} ${OSX_SYSROOT_FLAG} -E -x c++ - > /dev/null" 130 "if [ $? != 0 ] " 131 " then echo" 132 " echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'" 133 " echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'" 134 " if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]" 135 " then echo 'e.g. with:'" 136 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'" 137 " elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]" 138 " then echo 'e.g. with:'" 139 " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'" 140 " fi" 141 " echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'" 142 " echo 'into your build directory:'" 143 " echo ' cd ${LLVM_MAIN_SRC_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'" 144 " echo ' cd ${LLVM_BINARY_DIR} && make -C ${LLVM_MAIN_SRC_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'" 145 " echo" 146 " false" 147 "fi" 148 ) 149 add_custom_target(CompilerRTUnitTestCheckCxx 150 COMMAND bash -c "${CMD}" 151 COMMENT "Checking that just-built clang can find C++ headers..." 152 VERBATIM) 153 if (NOT COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD) 154 ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang) 155 endif() 156 endif() 157endmacro() 158