Skip to content

Commit c78c0e0

Browse files
committed
[libc++] Use a function to set warning flags per target
This is part of a larger shift to move to per-target settings and eradicate global variables from the CMake build. I'm starting small with warnings only because those are easy to transition over and I want to see how it pans out, but we can handle all flags like exceptions and RTTI in the future. llvm-svn: 373511
1 parent 3c10843 commit c78c0e0

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

libcxx/CMakeLists.txt

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -568,19 +568,19 @@ if (LIBCXX_HAS_COMMENT_LIB_PRAGMA)
568568
endif()
569569

570570
# Warning flags ===============================================================
571-
add_definitions(-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
572-
add_compile_flags_if_supported(
573-
-Wall -Wextra -W -Wwrite-strings
574-
-Wno-unused-parameter -Wno-long-long
575-
-Werror=return-type -Wextra-semi)
576-
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
577-
add_compile_flags_if_supported(
578-
-Wno-user-defined-literals
579-
-Wno-covered-switch-default
580-
-Wno-ignored-attributes # FIXME: Caused by _LIBCPP_NODEBUG_TYPE not being supported on older clangs
581-
)
571+
function(cxx_add_warning_flags target)
572+
target_compile_definitions(${target} PUBLIC -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
573+
target_add_compile_flags_if_supported(${target} PRIVATE -Wall -Wextra -W -Wwrite-strings
574+
-Wno-unused-parameter -Wno-long-long
575+
-Werror=return-type -Wextra-semi)
576+
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
577+
target_add_compile_flags_if_supported(${target} PRIVATE
578+
-Wno-user-defined-literals
579+
-Wno-covered-switch-default
580+
-Wno-ignored-attributes # FIXME: Caused by _LIBCPP_NODEBUG_TYPE not being supported on older clangs
581+
)
582582
if (LIBCXX_TARGETING_CLANG_CL)
583-
add_compile_flags_if_supported(
583+
target_add_compile_flags_if_supported(${target} PRIVATE
584584
-Wno-c++98-compat
585585
-Wno-c++98-compat-pedantic
586586
-Wno-c++11-compat
@@ -597,26 +597,27 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
597597
-Wno-double-promotion # FIXME: remove me
598598
)
599599
endif()
600-
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
601-
add_compile_flags_if_supported(
602-
-Wno-literal-suffix
603-
-Wno-c++14-compat
604-
-Wno-noexcept-type)
605-
endif()
606-
if (LIBCXX_ENABLE_WERROR)
607-
add_compile_flags_if_supported(-Werror)
608-
add_compile_flags_if_supported(-WX)
609-
else()
610-
# TODO(EricWF) Remove this. We shouldn't be suppressing errors when -Werror is
611-
# added elsewhere.
612-
add_compile_flags_if_supported(-Wno-error)
613-
endif()
614-
if (LIBCXX_ENABLE_PEDANTIC)
615-
add_compile_flags_if_supported(-pedantic)
616-
endif()
617-
if (LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS)
618-
add_definitions(-D_LIBCPP_DISABLE_MACRO_CONFLICT_WARNINGS)
619-
endif()
600+
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
601+
target_add_compile_flags_if_supported(${target} PRIVATE
602+
-Wno-literal-suffix
603+
-Wno-c++14-compat
604+
-Wno-noexcept-type)
605+
endif()
606+
if (LIBCXX_ENABLE_WERROR)
607+
target_add_compile_flags_if_supported(${target} PRIVATE -Werror)
608+
target_add_compile_flags_if_supported(${target} PRIVATE -WX)
609+
else()
610+
# TODO(EricWF) Remove this. We shouldn't be suppressing errors when -Werror is
611+
# added elsewhere.
612+
target_add_compile_flags_if_supported(${target} PRIVATE -Wno-error)
613+
endif()
614+
if (LIBCXX_ENABLE_PEDANTIC)
615+
target_add_compile_flags_if_supported(${target} PRIVATE -pedantic)
616+
endif()
617+
if (LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS)
618+
target_compile_definitions(${target} PRIVATE -D_LIBCPP_DISABLE_MACRO_CONFLICT_WARNINGS)
619+
endif()
620+
endfunction()
620621

621622
# Exception flags =============================================================
622623
if (LIBCXX_ENABLE_EXCEPTIONS)

libcxx/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ if (LIBCXX_ENABLE_SHARED)
260260
DEFINE_SYMBOL ""
261261
)
262262
cxx_set_common_defines(cxx_shared)
263+
cxx_add_warning_flags(cxx_shared)
263264

264265
# Link against LLVM libunwind
265266
if (LIBCXXABI_USE_LLVM_UNWINDER)
@@ -360,6 +361,7 @@ if (LIBCXX_ENABLE_STATIC)
360361
OUTPUT_NAME "c++"
361362
)
362363
cxx_set_common_defines(cxx_static)
364+
cxx_add_warning_flags(cxx_static)
363365

364366
if (LIBCXX_HERMETIC_STATIC_LIBRARY)
365367
# If the hermetic library doesn't define the operator new/delete functions

0 commit comments

Comments
 (0)