Reid Kleckner | a141032 | 2015-03-13 21:39:29 +0000 | [diff] [blame] | 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. |
| 4 | function(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) |
| 25 | endfunction() |
| 26 | |
Chris Bieneman | 73107c6 | 2015-08-20 17:35:56 +0000 | [diff] [blame^] | 27 | if (APPLE) |
| 28 | # On Darwin if /usr/include doesn't exist, the user probably has Xcode but not |
| 29 | # the command line tools. If this is the case, we need to find the OS X |
| 30 | # sysroot to pass to clang. |
| 31 | if(NOT EXISTS /usr/include) |
| 32 | execute_process(COMMAND xcodebuild -version -sdk macosx Path |
| 33 | OUTPUT_VARIABLE OSX_SYSROOT |
| 34 | ERROR_QUIET |
| 35 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 36 | set(OSX_SYSROOT_FLAG "-isysroot ${OSX_SYSROOT}") |
| 37 | endif() |
| 38 | endif() |
| 39 | |
Alexey Samsonov | 150d62a | 2014-02-19 13:01:03 +0000 | [diff] [blame] | 40 | # Compile a source into an object file with COMPILER_RT_TEST_COMPILER using |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 41 | # a provided compile flags and dependenices. |
| 42 | # clang_compile(<object> <source> |
| 43 | # CFLAGS <list of compile flags> |
| 44 | # DEPS <list of dependencies>) |
| 45 | macro(clang_compile object_file source) |
Filipe Cabecinhas | 7af0a1c | 2015-06-19 03:39:24 +0000 | [diff] [blame] | 46 | cmake_parse_arguments(SOURCE "" "" "CFLAGS;DEPS" ${ARGN}) |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 47 | get_filename_component(source_rpath ${source} REALPATH) |
Alexey Samsonov | 150d62a | 2014-02-19 13:01:03 +0000 | [diff] [blame] | 48 | if(NOT COMPILER_RT_STANDALONE_BUILD) |
Alexey Samsonov | 6239ebc | 2015-01-06 20:58:40 +0000 | [diff] [blame] | 49 | list(APPEND SOURCE_DEPS clang compiler-rt-headers) |
Alexey Samsonov | 150d62a | 2014-02-19 13:01:03 +0000 | [diff] [blame] | 50 | endif() |
Kuba Brecka | fa2de77 | 2014-09-10 17:23:58 +0000 | [diff] [blame] | 51 | if (TARGET CompilerRTUnitTestCheckCxx) |
| 52 | list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx) |
| 53 | endif() |
Alexey Samsonov | 1847401 | 2014-03-24 09:42:12 +0000 | [diff] [blame] | 54 | string(REGEX MATCH "[.](cc|cpp)$" is_cxx ${source_rpath}) |
| 55 | if(is_cxx) |
| 56 | string(REPLACE " " ";" global_flags "${CMAKE_CXX_FLAGS}") |
| 57 | else() |
| 58 | string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}") |
| 59 | endif() |
Reid Kleckner | a141032 | 2015-03-13 21:39:29 +0000 | [diff] [blame] | 60 | |
| 61 | if (MSVC) |
| 62 | translate_msvc_cflags(global_flags "${global_flags}") |
Timur Iskhodzhanov | 82ee043 | 2014-05-28 08:38:13 +0000 | [diff] [blame] | 63 | endif() |
Reid Kleckner | a141032 | 2015-03-13 21:39:29 +0000 | [diff] [blame] | 64 | |
Chris Bieneman | 73107c6 | 2015-08-20 17:35:56 +0000 | [diff] [blame^] | 65 | if (APPLE) |
| 66 | set(global_flags ${OSX_SYSROOT_FLAG} ${global_flags}) |
| 67 | endif() |
| 68 | |
Alexey Samsonov | 1847401 | 2014-03-24 09:42:12 +0000 | [diff] [blame] | 69 | # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options |
| 70 | # which are not supported by Clang. |
| 71 | list(APPEND global_flags -Wno-unknown-warning-option) |
| 72 | set(compile_flags ${global_flags} ${SOURCE_CFLAGS}) |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 73 | add_custom_command( |
| 74 | OUTPUT ${object_file} |
Timur Iskhodzhanov | b0279d7 | 2014-05-12 08:55:20 +0000 | [diff] [blame] | 75 | COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c |
Timur Iskhodzhanov | 82ee043 | 2014-05-28 08:38:13 +0000 | [diff] [blame] | 76 | -o "${object_file}" |
Alexey Samsonov | 150d62a | 2014-02-19 13:01:03 +0000 | [diff] [blame] | 77 | ${source_rpath} |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 78 | MAIN_DEPENDENCY ${source} |
Alexey Samsonov | 150d62a | 2014-02-19 13:01:03 +0000 | [diff] [blame] | 79 | DEPENDS ${SOURCE_DEPS}) |
Alexey Samsonov | ca7fcf2 | 2012-12-19 12:33:39 +0000 | [diff] [blame] | 80 | endmacro() |
Kuba Brecka | fa2de77 | 2014-09-10 17:23:58 +0000 | [diff] [blame] | 81 | |
| 82 | # On Darwin, there are no system-wide C++ headers and the just-built clang is |
| 83 | # therefore not able to compile C++ files unless they are copied/symlinked into |
| 84 | # ${LLVM_BINARY_DIR}/include/c++ |
| 85 | # The just-built clang is used to build compiler-rt unit tests. Let's detect |
| 86 | # this before we try to build the tests and print out a suggestion how to fix |
| 87 | # it. |
| 88 | # On other platforms, this is currently not an issue. |
| 89 | macro(clang_compiler_add_cxx_check) |
| 90 | if (APPLE) |
| 91 | set(CMD |
Chris Bieneman | 73107c6 | 2015-08-20 17:35:56 +0000 | [diff] [blame^] | 92 | "echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} ${OSX_SYSROOT_FLAG} -E -x c++ - > /dev/null" |
Kuba Brecka | fa2de77 | 2014-09-10 17:23:58 +0000 | [diff] [blame] | 93 | "if [ $? != 0 ] " |
| 94 | " then echo" |
| 95 | " echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'" |
| 96 | " echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'" |
| 97 | " if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]" |
| 98 | " then echo 'e.g. with:'" |
| 99 | " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'" |
| 100 | " elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]" |
| 101 | " then echo 'e.g. with:'" |
| 102 | " echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'" |
| 103 | " fi" |
| 104 | " echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'" |
| 105 | " echo 'into your build directory:'" |
| 106 | " echo ' cd ${LLVM_SOURCE_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'" |
| 107 | " echo ' cd ${LLVM_BINARY_DIR} && make -C ${LLVM_SOURCE_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'" |
| 108 | " echo" |
| 109 | " false" |
| 110 | "fi" |
| 111 | ) |
| 112 | add_custom_target(CompilerRTUnitTestCheckCxx |
| 113 | COMMAND bash -c "${CMD}" |
| 114 | COMMENT "Checking that just-built clang can find C++ headers..." |
Kuba Brecka | fa2de77 | 2014-09-10 17:23:58 +0000 | [diff] [blame] | 115 | VERBATIM) |
Alexander Potapenko | bc4ce3a | 2014-09-25 09:30:05 +0000 | [diff] [blame] | 116 | if (TARGET clang) |
| 117 | ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang) |
| 118 | endif() |
Kuba Brecka | fa2de77 | 2014-09-10 17:23:58 +0000 | [diff] [blame] | 119 | endif() |
| 120 | endmacro() |