From a1410321f6ddfe0ce47eccfbadf2e1955588ed32 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Fri, 13 Mar 2015 21:39:29 +0000 Subject: [PATCH] Translate some MSVC CMAKE_*_FLAGS to clang flags in clang_compile Passing MSVC-style cflags to the gcc-style clang driver will almost always end badly. Just translate a couple of simple flags used by the base CMake cflags like /D, /U, and /O. llvm-svn: 232219 --- compiler-rt/cmake/Modules/CompilerRTCompile.cmake | 36 +++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/compiler-rt/cmake/Modules/CompilerRTCompile.cmake b/compiler-rt/cmake/Modules/CompilerRTCompile.cmake index de73ccf..c883e43 100644 --- a/compiler-rt/cmake/Modules/CompilerRTCompile.cmake +++ b/compiler-rt/cmake/Modules/CompilerRTCompile.cmake @@ -1,5 +1,31 @@ include(LLVMParseArguments) +# On Windows, CMAKE_*_FLAGS are built for MSVC but we use the GCC clang.exe, +# which uses completely different flags. Translate some common flag types, and +# drop the rest. +function(translate_msvc_cflags out_flags msvc_flags) + # Insert an empty string in the list to simplify processing. + set(msvc_flags ";${msvc_flags}") + + # Canonicalize /flag to -flag. + string(REPLACE ";/" ";-" msvc_flags "${msvc_flags}") + + # Make space separated -D and -U flags into joined flags. + string(REGEX REPLACE ";-\([DU]\);" ";-\\1" msvc_flags "${msvc_flags}") + + set(clang_flags "") + foreach(flag ${msvc_flags}) + if ("${flag}" MATCHES "^-[DU]") + # Pass through basic command line macro definitions (-DNDEBUG). + list(APPEND clang_flags "${flag}") + elseif ("${flag}" MATCHES "^-O[2x]") + # Canonicalize normal optimization flags to -O2. + list(APPEND clang_flags "-O2") + endif() + endforeach() + set(${out_flags} "${clang_flags}" PARENT_SCOPE) +endfunction() + # Compile a source into an object file with COMPILER_RT_TEST_COMPILER using # a provided compile flags and dependenices. # clang_compile( @@ -20,13 +46,11 @@ macro(clang_compile object_file source) else() string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}") endif() - # On Windows, CMAKE_*_FLAGS are built for MSVC but we use the GCC clang.exe - # which doesn't support flags starting with "/smth". Replace those with - # "-smth" equivalents. - if(MSVC) - string(REGEX REPLACE "^/" "-" global_flags "${global_flags}") - string(REPLACE ";/" ";-" global_flags "${global_flags}") + + if (MSVC) + translate_msvc_cflags(global_flags "${global_flags}") endif() + # Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options # which are not supported by Clang. list(APPEND global_flags -Wno-unknown-warning-option) -- 2.7.4