Skip to content

Commit

Permalink
Merge pull request #770 from wasmx/cable_add_compile_options
Browse files Browse the repository at this point in the history
cmake: Simplify compiler options with Cable
  • Loading branch information
chfast authored May 20, 2021
2 parents eeca732 + 1260d7b commit 2fe6f02
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 11 deletions.
24 changes: 13 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)

include(cmake/cable.cmake)
include(CableBuildType)
include(CableCompileOptions)
include(CableCompilerSettings)
include(CMakeDependentOption)

Expand Down Expand Up @@ -37,21 +38,22 @@ if(is_big_endian)
endif()

cable_configure_compiler()
add_compile_options(
cable_add_compile_options(
-Wcast-qual
-Wcast-align
-Wmissing-declarations
$<$<COMPILE_LANGUAGE:CXX>:-Wextra-semi>
$<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast>
IF_SUPPORTED
-Wextra-semi
-Wold-style-cast
-Wfinal-dtor-non-final-class
-Wnewline-eof
-Wsuggest-destructor-override
-Wunreachable-code-break
-Wduplicated-cond
-Wduplicate-enum
-Wlogical-op
-Wno-unknown-attributes
)
cable_add_cxx_compiler_flag_if_supported(-Wfinal-dtor-non-final-class)
cable_add_cxx_compiler_flag_if_supported(-Wnewline-eof)
cable_add_cxx_compiler_flag_if_supported(-Wsuggest-destructor-override)
cable_add_cxx_compiler_flag_if_supported(-Wunreachable-code-break)
cable_add_cxx_compiler_flag_if_supported(-Wduplicated-cond)
cable_add_cxx_compiler_flag_if_supported(-Wduplicate-enum)
cable_add_cxx_compiler_flag_if_supported(-Wlogical-op)
cable_add_cxx_compiler_flag_if_supported(-Wno-unknown-attributes)

if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
option(WEVERYTHING "Enable almost all compiler warnings" OFF)
Expand Down
73 changes: 73 additions & 0 deletions cmake/CableCompileOptions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Cable: CMake Bootstrap Library <https://github.com/ethereum/cable>
# Copyright 2021 Pawel Bylica.
# Licensed under the Apache License, Version 2.0.

# Cable Compile Options, version 1.0.0
#
# This CMake module provides utilities to conditionally add compile options
# depending on language and compiler support.
#
# CHANGELOG
#
# 1.0.0 - 2021-03-24

include_guard(GLOBAL)

include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)

function(cable_add_compile_options)
cmake_parse_arguments(ARG "" "" "IF_SUPPORTED" ${ARGN})

# Init options list with all arguments before IF_SUPPORTED keyword.
set(options ${ARG_UNPARSED_ARGUMENTS})

# Get list of languages to check.
# Currently only C and CXX is supported here, but with check_compiler_flag()
# from CMake 3.19 all languages can be checked.
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
list(FILTER languages INCLUDE REGEX "C|CXX")

# Some flags causes compiler warning instead of error. Still such flags must
# be considered unsupported. To make check_X_compiler_flag() fail before
# CMake 3.19 additional -Werror flag must be added.
list(GET languages 0 example_lang)
set(compiler ${CMAKE_${example_lang}_COMPILER_ID})
if(compiler MATCHES GNU OR compiler MATCHES Clang)
set(CMAKE_REQUIRED_FLAGS -Werror)
endif()

foreach(flag ${ARG_IF_SUPPORTED})
string(MAKE_C_IDENTIFIER ${flag} flag_id)
set(supported_in_all_languages TRUE)

foreach(lang ${languages})
set(result_var "${lang}${flag_id}")

# Check if the flag works in the lang's compiler.
# In CMake 3.18+ cmake_language(CALL ...) can be used.
if(lang STREQUAL CXX)
check_cxx_compiler_flag(${flag} ${result_var})
else()
check_c_compiler_flag(${flag} ${result_var})
endif()

if(NOT "${${result_var}}")
set(supported_in_all_languages FALSE)
endif()
endforeach()

if(supported_in_all_languages)
list(APPEND options ${flag})
else()
foreach(lang ${languages})
set(result_var "${lang}${flag_id}")
if(${${result_var}})
list(APPEND options "$<$<COMPILE_LANGUAGE:${lang}>:${flag}>")
endif()
endforeach()
endif()
endforeach()

add_compile_options(${options})
endfunction()

0 comments on commit 2fe6f02

Please sign in to comment.