Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
abdes committed Aug 20, 2022
2 parents fcfbe63 + e9b00bd commit 05632a5
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 337 deletions.
5 changes: 3 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ Checks: >
portability-*,
readability-*,
-cppcoreguidelines-macro-usage,
-hicpp-signed-bitwise,
-google-runtime-references
-hicpp-exception-baseclass,
-hicpp-signed-bitwise,
-readability-function-cognitive-complexity,
-google-runtime-references
-used-but-marked-unused,
HeaderFilterRegex: ".*/[common|logging]/.*"
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ set(${PROJECT_NAME}_BUILD_EXAMPLES ${ASAP_BUILD_EXAMPLES})
include(AsapTargets)
include(BuildHelpers)
include(GenerateExportHeader)
include(CompileOptions)

# Override the ${META_PROJECT_ID}_INSTALL option to ON/OFF to respectively force
# install/no-install behavior for this project. This is particularly useful when
Expand Down
24 changes: 13 additions & 11 deletions cmake/AsapTargets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
# ===-----------------------------------------------------------------------===#

include(CMakePackageConfigHelpers)
include(common/CompileOptions)
include(common/SwiftTargets)
include(CompileDefinitions)
include(CompileOptions)

# ------------------------------------------------------------------------------
# Meta information about the this module
Expand Down Expand Up @@ -113,6 +115,8 @@ function(asap_add_library target)
if(NOT ${type} STREQUAL "INTERFACE_LIBRARY")
# Set some common private compiler defines
asap_set_compile_definitions(${target})
# Set some common compiler options
asap_set_compile_options(${target})
# Generate export headers for the library
asap_generate_export_headers(${target} ${META_MODULE_NAME})

Expand All @@ -129,34 +133,32 @@ endfunction()

function(asap_add_executable target)
swift_add_executable("${target}" ${ARGN})
# Set some common private compiler defines
asap_set_compile_definitions(${target})
# Set some common compiler options
asap_set_compile_options(${target})
set_target_properties(${target} PROPERTIES FOLDER "Executables")
endfunction()

function(asap_add_tool target)
swift_add_tool("${target}" ${ARGN})
# Set some common private compiler defines
asap_set_compile_definitions(${target})
# Set some common compiler options
asap_set_compile_options(${target})
set_target_properties(${target} PROPERTIES FOLDER "Tools")
endfunction()

function(asap_add_tool_library target)
swift_add_tool_library("${target}" ${ARGN})
# Set some common private compiler defines
asap_set_compile_definitions(${target})
# Set some common compiler options
asap_set_compile_options(${target})
set_target_properties(
${target}
PROPERTIES FOLDER "Tool Libraries"
VERSION ${META_MODULE_VERSION}
SOVERSION ${META_MODULE_VERSION_MAJOR}
DEBUG_POSTFIX "d")
endfunction()

function(asap_add_test_library target)
swift_add_test_library("${target}" ${ARGN})
asap_set_compile_definitions(${target})
set_target_properties(
${target}
PROPERTIES FOLDER "Test Libraries"
VERSION ${META_MODULE_VERSION}
SOVERSION ${META_MODULE_VERSION_MAJOR}
DEBUG_POSTFIX "d")
endfunction()
26 changes: 0 additions & 26 deletions cmake/BuildHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@
# Build Helpers to simplify target creation.
# ------------------------------------------------------------------------------

function(asap_compile_definitions target)
#
# Compile definitions
#
# ones we use for every single target
target_compile_definitions(
${target}
PRIVATE $<$<STREQUAL:${OPTION_CONTRACT_MODE},DEFAULT>:
ASAP_CONTRACT_DEFAULT
>
$<$<STREQUAL:${OPTION_CONTRACT_MODE},XXX>:
ASAP_CONTRACT_OFF
>
$<$<STREQUAL:${OPTION_CONTRACT_MODE},AUDIT>:
ASAP_CONTRACT_AUDIT
>
$<$<CXX_COMPILER_ID:MSVC>:
NOMINMAX
WIN32_LEAN_AND_MEAN=1
_WIN32_WINNT=0x0600
>)
endfunction()

include(GenerateTemplateExportHeader)
function(asap_generate_export_headers target include_dir)
# Set API export file and macro
string(MAKE_C_IDENTIFIER ${target} TEMPLATE_TARGET_ID)
Expand All @@ -44,6 +20,4 @@ function(asap_generate_export_headers target include_dir)
# Create API export headers
generate_export_header(${target} EXPORT_FILE_NAME ${export_file}
EXPORT_MACRO_NAME ${TEMPLATE_TARGET_ID}_API)
generate_template_export_header(${target} ${TEMPLATE_TARGET_ID}
${template_export_file})
endfunction()
221 changes: 23 additions & 198 deletions cmake/CompileOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,232 +5,57 @@
# ===-----------------------------------------------------------------------===#

# ------------------------------------------------------------------------------
# Set a common set of compiler options and warning flags
# Set additional common compiler options and warning flags
# ------------------------------------------------------------------------------

#
# Call swift_set_compile_options() for any target to set the Swift default set
# of compiler options. This includes
# ~~~
# - exceptions disabled
# - rtti disabled
# - strict aliasing disabled
# - Warnings as errors (-Werror)
# - Extensive set of enabled warnings
# ~~~
#
# Exceptions and/or RTTI can be selectively enabled for a target be passing
# EXCEPTIONS and/or RTTI as a parameter, eg
#
# ~~~
# swift_set_compile_options(sample-target EXCEPTIONS RTTI)
# ~~~
#
# will enable exceptions and rtti for sample-target only
#
# Warning flags can be removed from the default set by passing REMOVE followed
# by a list of warning flags, eg
#
# ~~~
# swift_set_compile_options(sample-target REMOVE -Wconversion)
# ~~~
#
# will prevent -Wconversion from being passed to the compiler for sample-target
# only
#
# Similarly extra options can be given by passing ADD followed by a list of
# warning flags (or other compiler options), eg
#
# ~~~
# swift_set_compile_options(sample-target ADD -Wformat=2)
# ~~~
#
# will pass -Wformat=2 to the compiler for sample-target only
#
# By default -Werror is set, but this can be prevented by passing WARNING as a
# parameter, eg
#
# ~~~
# swift_set_compile_options(sample-target WARNING)
# ~~~
# Refer to swift_set_compile_options() for additional detailed information on
# accepted options for this function, which forwards all options.
#
# will disable warnings-as-errors for sample-target only
#
# All flags will be checked for suitability with the in-use compilers before
# being selected. This is important since Swift code tends to be compiled with a
# wide variety of compilers which may not support the same set of flags and
# options. Therefore, it should be preferred to use this function to set
# compiler flags and options rather than target_compile_options()
#
# NOTE: user's can call on EXTRA_FLAGS to augment the default list of flags
# before flags are removed with REMOVE and subsequently added with ADD.
#

include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)

function(swift_set_compile_options)
set(argOption "WARNING" "NO_EXCEPTIONS" "EXCEPTIONS" "NO_RTTI" "RTTI")
set(argSingle "")
set(argMulti "ADD" "REMOVE" "EXTRA_FLAGS")

unset(x_WARNING)
unset(x_ADD)
unset(x_REMOVE)
unset(x_EXTRA_FLAGS)

cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
set(targets ${x_UNPARSED_ARGUMENTS})

if(x_RTTI AND x_NO_RTTI)
message(FATAL_ERROR "RTTI and NO_RTTI can't be used together")
endif()

if(x_EXCEPTIONS AND x_NO_EXCEPTIONS)
message(FATAL_ERROR "EXCEPTIONS and NO_EXCEPTIONS can't be used together")
endif()
# In addition to relaying the call, this function adds common compiler options
# and overrides the default behavior of swift with regard to exceptions and
# RTTI. By default, both are enabled.

foreach(flag ${x_ADD} ${x_REMOVE})
if(${flag} STREQUAL "-Werror")
message(
FATAL_ERROR
"Do not specify -Werror directly, use WARNING to disable -Werror")
endif()
if(${flag} STREQUAL "-Wno-error")
message(
FATAL_ERROR
"Do not specify -Wno-error directly, use WARNING to disable -Werror")
endif()
endforeach()

if(DEFINED SWIFT_COMPILER_WARNING_ARE_ERROR)
if(SWIFT_COMPILER_WARNING_ARE_ERROR)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(all_flags /WX)
else()
set(all_flags -Werror -Wno-error=deprecated-declarations)
endif()
else()
set(all_flags -Wno-error)
endif()
else()
if(NOT x_WARNING)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(all_flags /WX)
else()
set(all_flags -Werror -Wno-error=deprecated-declarations)
endif()
endif()
endif()
function(asap_set_compile_options)
swift_set_compile_options(EXCEPTIONS RTTI ${ARGV})

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# using Clang
list(
APPEND
all_flags
swift_set_compile_options(
ADD
-Weverything
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-c++98-c++11-compat-pedantic
-Wno-padded
-Wno-documentation-unknown-command
-Wno-switch-enum
-Wno-unused-macros
-Wno-disabled-macro-expansion)
-Wno-documentation-unknown-command)
# -Wno-switch-enum -Wno-unused-macros -Wno-disabled-macro-expansion)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# using GCC
list(
APPEND
all_flags
-Wall
-Wextra
-Wcast-align
-Wcast-qual
swift_set_compile_options(
ADD
-Wctor-dtor-privacy
-Wdisabled-optimization
-Wformat=2
-Winit-self
-Wmissing-declarations
-Wmissing-include-dirs
-Wold-style-cast
-Woverloaded-virtual
-Wredundant-decls
-Wshadow
-Wsign-conversion
-Wsign-promo
-Wundef
-Werror
-Wno-unused)
-Wundef)
if(NOT DEFINED CMAKE_CXX_CLANG_TIDY)
list(APPEND all_flags -Wlogical-op -Wstrict-null-sentinel)
swift_set_compile_options(ADD -Wlogical-op -Wstrict-null-sentinel)
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# using Visual Studio C++
list(APPEND all_flags /EHsc /MP /W4)
endif()

if(x_REMOVE)
foreach(flag ${x_REMOVE})
list(FIND all_flags ${flag} found)
if(found EQUAL -1)
message(
FATAL_ERROR
"Compiler flag '${flag}' specified for removal is not part of the set of common compiler flags"
)
endif()
set(argOption "WARNING" "NO_EXCEPTIONS" "EXCEPTIONS" "NO_RTTI" "RTTI")
set(argSingle "")
set(argMulti "ADD" "REMOVE" "EXTRA_FLAGS")
cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
set(targets ${x_UNPARSED_ARGUMENTS})
foreach(target ${targets})
target_compile_options(${target} PRIVATE /EHsc /MP /W4)
endforeach()
list(REMOVE_ITEM all_flags ${x_REMOVE})
endif()

list(APPEND all_flags ${x_ADD})

unset(final_flags)

get_property(enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
list(FIND enabled_languages "C" c_enabled)
list(FIND enabled_languages "CXX" cxx_enabled)

foreach(flag ${all_flags})
string(TOUPPER ${flag} sanitised_flag)
string(REPLACE "+" "X" sanitised_flag ${sanitised_flag})
string(REGEX REPLACE "[^A-Za-z_0-9]" "_" sanitised_flag ${sanitised_flag})

set(c_supported HAVE_C_FLAG_${sanitised_flag})
string(REGEX REPLACE "_+" "_" c_supported ${c_supported})
set(cxx_supported HAVE_CXX_FLAG_${sanitised_flag})
string(REGEX REPLACE "_+" "_" cxx_supported ${cxx_supported})

if(${c_enabled} GREATER -1)
if(MSVC)
check_c_compiler_flag("/WX ${flag}" ${c_supported})
else()
check_c_compiler_flag("-Werror ${flag}" ${c_supported})
endif()
if(${${c_supported}})
list(APPEND final_flags $<$<COMPILE_LANGUAGE:C>:${flag}>)
endif()
endif()

if(${cxx_enabled} GREATER -1)
if(MSVC)
check_cxx_compiler_flag("/WX ${flag}" ${cxx_supported})
else()
check_cxx_compiler_flag("-Werror ${flag}" ${cxx_supported})
endif()
if(${${cxx_supported}})
list(APPEND final_flags $<$<COMPILE_LANGUAGE:CXX>:${flag}>)
endif()
endif()
endforeach()

foreach(target ${targets})
target_compile_options(${target} PRIVATE ${final_flags})
endforeach()

endfunction()

function(asap_set_compile_options)
swift_set_compile_options(${ARGV})
endfunction()

# ------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 05632a5

Please sign in to comment.