Skip to content

Commit

Permalink
Merge pull request #230 from chfast/cmake
Browse files Browse the repository at this point in the history
CMake and build updates
  • Loading branch information
chfast committed Apr 3, 2023
2 parents b6ca91b + 3d17ecf commit 02b3417
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 95 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Checks: >
hicpp-multiway-paths-covered,
hicpp-no-assembler,
misc-*,
-misc-const-correctness,
-misc-non-private-member-variables-in-classes,
modernize-*,
-modernize-avoid-c-arrays,
Expand Down
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ set(PROJECT_VERSION 1.0.0)

cable_configure_compiler(NO_STACK_PROTECTION)
if(CABLE_COMPILER_GNULIKE)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Og")

add_compile_options(
# -Wcast-align #TODO: Build fails on mips64
-Wcast-qual
Expand All @@ -37,6 +34,7 @@ if(CABLE_COMPILER_GNULIKE)
$<$<CXX_COMPILER_ID:Clang>:-Wduplicate-enum>
$<$<CXX_COMPILER_ID:Clang>:-Wnewline-eof>
$<$<CXX_COMPILER_ID:Clang>:-Wunreachable-code-aggressive>
$<$<CXX_COMPILER_ID:GNU>:-Wno-attributes>
)

if(MSVC) # clang-cl
Expand All @@ -52,6 +50,18 @@ elseif(MSVC AND CMAKE_SIZEOF_VOID_P EQUAL 4)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LARGEADDRESSAWARE")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Og")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og")
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} -fprofile-instr-generate -fcoverage-mapping")
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-instr-generate -fcoverage-mapping")
elseif(CMAKE_CXX_COMPILER_ID MATCHES GNU)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Og")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og")
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} --coverage")
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage")
endif()

option(ETHASH_INSTALL_CMAKE_CONFIG "Install CMake configuration scripts for find_package(CONFIG)" ON)

set(include_dir ${PROJECT_SOURCE_DIR}/include)
Expand Down
21 changes: 10 additions & 11 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ commands:
# (ninja is using relative paths otherwise).
working_directory: ~/build
command: |
cmake ../project -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_INSTALL_PREFIX=/tmp/local -DETHASH_TESTING=ON $CMAKE_OPTIONS
cmake ../project -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_INSTALL_PREFIX=/tmp/local -DETHASH_TESTING=ON -DCMAKE_COMPILE_WARNING_AS_ERROR=TRUE $CMAKE_OPTIONS
build:
steps:
Expand Down Expand Up @@ -95,7 +95,7 @@ commands:
steps:
- run:
name: "Upgrade codecov"
command: sudo pip3 install --upgrade --quiet --no-cache-dir codecov
command: sudo pip3 install --break-system-packages --upgrade --quiet --no-cache-dir codecov
- run:
name: "Upload to Codecov"
command: |
Expand Down Expand Up @@ -125,10 +125,9 @@ jobs:

linux-gcc-coverage:
docker:
- image: ethereum/cpp-build-env:17-gcc-11
- image: ethereum/cpp-build-env:19-gcc-12
environment:
- BUILD_TYPE: Debug
- CMAKE_OPTIONS: -DCOVERAGE=ON
- BUILD_TYPE: Coverage
steps:
- checkout
- configure
Expand All @@ -154,7 +153,7 @@ jobs:

linux-clang-sanitizers:
docker:
- image: ethereum/cpp-build-env:17-clang-13
- image: ethereum/cpp-build-env:19-clang-15
environment:
- CMAKE_OPTIONS: -DSANITIZE=address,undefined,unsigned-integer-overflow,shift-exponent,implicit-conversion,nullability -DCMAKE_CXX_CLANG_TIDY=clang-tidy
- ASAN_OPTIONS: allocator_may_return_null=1
Expand All @@ -168,7 +167,7 @@ jobs:

linux-32bit-asan:
docker:
- image: ethereum/cpp-build-env:17-gcc-11-multilib
- image: ethereum/cpp-build-env:19-gcc-12-multilib
environment:
- BUILD_TYPE: RelWithDebInfo
- CMAKE_OPTIONS: -DTOOLCHAIN=cxx11-32bit -DSANITIZE=address
Expand All @@ -182,7 +181,7 @@ jobs:

mips64:
docker:
- image: ethereum/cpp-build-env:17-gcc-10
- image: ethereum/cpp-build-env:19-gcc-12
environment:
- TESTS_EXCLUDE: _oom|_multithreaded
- CMAKE_OPTIONS: -DTOOLCHAIN=mips64 -DCMAKE_EXE_LINKER_FLAGS=-static
Expand All @@ -195,11 +194,11 @@ jobs:

powerpc64:
docker:
- image: ethereum/cpp-build-env:17-gcc-11
- image: ethereum/cpp-build-env:19-gcc-12
environment:
- TESTS_EXCLUDE: _oom|_multithreaded
- BUILD_TYPE: Debug
- CMAKE_OPTIONS: -DTOOLCHAIN=powerpc64 -DCMAKE_EXE_LINKER_FLAGS=-static -DCOVERAGE=ON
- BUILD_TYPE: Coverage
- CMAKE_OPTIONS: -DTOOLCHAIN=powerpc64 -DCMAKE_EXE_LINKER_FLAGS=-static
steps:
- checkout
- install-powerpc64
Expand Down
98 changes: 62 additions & 36 deletions cmake/cable/CableCompilerSettings.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
# Cable: CMake Bootstrap Library
# Cable: CMake Bootstrap Library <https://github.com/ethereum/cable>
# Copyright 2018 Pawel Bylica.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.
# Licensed under the Apache License, Version 2.0.

# Cable Compiler Settings, version 1.2.0
#
# This CMake module provides default configuration (with some options)
# for C/C++ compilers. Use cable_configure_compiler().
#
# CHANGELOG
#
# 1.2.0 - 2023-02-25
# - Do not set -Werror nor /WX. This has been standardized in CMake 3.24 as CMAKE_COMPILE_WARNING_AS_ERROR.
# - Keep compiler warnings about unknown pragmas.
# - Keep MSVC warning C5030: attribute is not recognized. It should be disabled in source code.
# - Do not try to erase MSVC default warning level /W3. This is not set since CMake 3.15 (CMP0092).
# - Drop explicit -Wimplicit-fallthrough. It is a part of -Wextra.
# - Use PROJECT_IS_TOP_LEVEL if available (or define it).
# - Use include_guard().
#
# 1.1.0 - 2020-06-20
# - Allow unknown C++ attributes in MSVC compiler.
# - Option -DEXCEPTIONS=OFF to disable C++ exceptions support (GCC, clang).
# - Option -DRTTI=OFF to disable RTTI support (GCC, clang).
#
# 1.0.1 - 2020-01-30
# - Do not explicitly set -mtune=generic, this is default anyway.
#
# 1.0.0 - 2019-12-20

include_guard()
include(CheckCXXCompilerFlag)

# Adds CXX compiler flag if the flag is supported by the compiler.
Expand Down Expand Up @@ -29,8 +56,17 @@ endfunction()

# Configures the compiler with default flags.
macro(cable_configure_compiler)
if(NOT PROJECT_IS_NESTED)
# Do this configuration only in the top project.
if(NOT PROJECT_SOURCE_DIR)
message(FATAL_ERROR "cable_configure_compiler() must be used after project()")
endif()

if(NOT DEFINED PROJECT_IS_TOP_LEVEL)
# Define PROJECT_IS_TOP_LEVEL (since CMake 3.21) if not available.
string(COMPARE EQUAL ${CMAKE_SOURCE_DIR} ${PROJECT_SOURCE_DIR} PROJECT_IS_TOP_LEVEL)
endif()

if(PROJECT_IS_TOP_LEVEL)
# Do this configuration only in the top level project.

cmake_parse_arguments(cable "NO_CONVERSION_WARNINGS;NO_STACK_PROTECTION;NO_PEDANTIC" "" "" ${ARGN})

Expand All @@ -56,17 +92,14 @@ macro(cable_configure_compiler)
add_compile_options(-Wpedantic)
endif()

# Enable base warnings.
# Enable basic warnings.
add_compile_options(-Wall -Wextra -Wshadow)

if(NOT cable_NO_CONVERSION_WARNINGS)
# Enable conversion warnings if not explicitly disabled.
add_compile_options(-Wconversion -Wsign-conversion)
endif()

# Allow unknown pragmas, we don't want to wrap them with #ifdefs.
add_compile_options(-Wno-unknown-pragmas)

# Stack protection.
check_cxx_compiler_flag(-fstack-protector fstack-protector)
if(fstack-protector)
Expand All @@ -79,22 +112,27 @@ macro(cable_configure_compiler)
# Try enabling the "strong" variant.
cable_add_cxx_compiler_flag_if_supported(-fstack-protector-strong have_stack_protector_strong_support)
if(NOT have_stack_protector_strong_support)
# Fallback to standard variant of "strong" not available.
# Fallback to standard variant if "strong" not available.
add_compile_options(-fstack-protector)
endif()
endif()
endif()

cable_add_cxx_compiler_flag_if_supported(-Wimplicit-fallthrough)

elseif(MSVC)

# Enable base warnings.
# Enable basic warnings.
add_compile_options(/W4)

# Allow unknown pragmas, we don't want to wrap them with #ifdefs.
add_compile_options(/wd4068)
endif()

option(EXCEPTIONS "Build with exceptions support" ON)
if(NOT EXCEPTIONS)
add_compile_options(-fno-exceptions)
endif()

option(RTTI "Build with RTTI support" ON)
if(NOT RTTI)
add_compile_options(-fno-rtti)
endif()

# Option for arch=native.
Expand All @@ -103,7 +141,7 @@ macro(cable_configure_compiler)
if(MSVC)
add_compile_options(-arch:AVX)
else()
add_compile_options(-march=native)
add_compile_options(-mtune=native -march=native)
endif()
endif()

Expand All @@ -121,30 +159,18 @@ macro(cable_configure_compiler)
endif()
add_compile_options(-fno-omit-frame-pointer -fsanitize=${SANITIZE})

set(backlist_file ${PROJECT_SOURCE_DIR}/sanitizer-blacklist.txt)
if(EXISTS ${backlist_file})
check_cxx_compiler_flag(-fsanitize-blacklist=${backlist_file} have_fsanitize-blacklist)
if(have_fsanitize-blacklist)
add_compile_options(-fsanitize-blacklist=${backlist_file})
endif()
set(blacklist_file ${PROJECT_SOURCE_DIR}/sanitizer-blacklist.txt)
if(EXISTS ${blacklist_file})
cable_add_cxx_compiler_flag_if_supported(-fsanitize-blacklist=${blacklist_file})
endif()
unset(blacklist_file)
endif()

# Code coverage support.
option(COVERAGE "Build with code coverage support" OFF)
if(COVERAGE)
# Set the linker flags first, they are required to properly test the compiler flag.
set(CMAKE_SHARED_LINKER_FLAGS "--coverage ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "--coverage ${CMAKE_EXE_LINKER_FLAGS}")

set(CMAKE_REQUIRED_LIBRARIES "--coverage ${CMAKE_REQUIRED_LIBRARIES}")
check_cxx_compiler_flag(--coverage have_coverage)
string(REPLACE "--coverage " "" CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
if(NOT have_coverage)
message(FATAL_ERROR "Coverage not supported")
endif()
add_compile_options(-g --coverage)
# The "Coverage" build type.
if(CABLE_COMPILER_CLANG)
set(CMAKE_CXX_FLAGS_COVERAGE "-fprofile-instr-generate -fcoverage-mapping")
elseif(CABLE_COMPILER_GNU)
set(CMAKE_CXX_FLAGS_COVERAGE "--coverage")
endif()

endif()
endmacro()
17 changes: 14 additions & 3 deletions cmake/cable/CablePackage.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Cable: CMake Bootstrap Library.
# Copyright 2019 Pawel Bylica.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.
# Cable: CMake Bootstrap Library <https://github.com/ethereum/cable>
# Copyright 2019-2020 Pawel Bylica.
# Licensed under the Apache License, Version 2.0.

# Cable Package, version 1.0.0
#
# This CMake module provides default configuration for CPack
#
# CHANGELOG
#
# 1.0.0 - 2020-05-06

if(cable_package_included)
return()
Expand All @@ -11,12 +19,15 @@ set(cable_package_included TRUE)
macro(cable_add_archive_package)
if(WIN32)
set(CPACK_GENERATOR ZIP)
set(CPACK_SOURCE_GENERATOR ZIP)
else()
set(CPACK_GENERATOR TGZ)
set(CPACK_SOURCE_GENERATOR TGZ)
endif()
string(TOLOWER ${CMAKE_SYSTEM_NAME} system_name)
string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} system_processor)
set(CPACK_PACKAGE_FILE_NAME ${PROJECT_NAME}-${PROJECT_VERSION}-${system_name}-${system_processor})
set(CPACK_SOURCE_PACKAGE_FILE_NAME ${PROJECT_NAME}-${PROJECT_VERSION}-source)
set(CPACK_PACKAGE_CHECKSUM SHA256)
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY FALSE)
unset(system_name)
Expand Down
5 changes: 0 additions & 5 deletions include/ethash/ethash.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include <stdbool.h>
#include <stdint.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-id-macro"

#ifndef __has_cpp_attribute
#define __has_cpp_attribute(X) 0
#endif
Expand All @@ -20,8 +17,6 @@
#define __has_attribute(X) 0
#endif

#pragma clang diagnostic pop

#ifndef __cplusplus
#define noexcept // Ignore noexcept in C code.
#endif
Expand Down
15 changes: 6 additions & 9 deletions lib/ethash/ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace
{
/// The core transformation of the FNV-1 hash function.
/// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash.
NO_SANITIZE("unsigned-integer-overflow")
inline uint32_t fnv1(uint32_t u, uint32_t v) noexcept
[[clang::no_sanitize("unsigned-integer-overflow")]] inline uint32_t fnv1(
uint32_t u, uint32_t v) noexcept
{
static const uint32_t fnv_prime = 0x01000193;
return (u * fnv_prime) ^ v;
Expand Down Expand Up @@ -292,18 +292,15 @@ search_result search(const epoch_context_full& context, const hash256& header_ha
}


#pragma clang diagnostic ignored "-Wunknown-sanitizers"

struct uint128
{
uint64_t lo;
uint64_t hi;
};

/// Full unsigned multiplication 64 x 64 -> 128.
NO_SANITIZE("unsigned-integer-overflow")
NO_SANITIZE("unsigned-shift-base")
inline uint128 umul(uint64_t x, uint64_t y) noexcept
[[clang::no_sanitize("unsigned-integer-overflow", "unsigned-shift-base")]] inline uint128 umul(
uint64_t x, uint64_t y) noexcept
{
#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic push
Expand Down Expand Up @@ -334,8 +331,8 @@ inline uint128 umul(uint64_t x, uint64_t y) noexcept
return {lo, hi};
}

NO_SANITIZE("unsigned-integer-overflow")
bool check_against_difficulty(const hash256& final_hash, const hash256& difficulty) noexcept
[[clang::no_sanitize("unsigned-integer-overflow")]] bool check_against_difficulty(
const hash256& final_hash, const hash256& difficulty) noexcept
{
constexpr size_t num_words = sizeof(hash256) / sizeof(uint64_t);

Expand Down
7 changes: 0 additions & 7 deletions lib/support/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,3 @@
#else
#define ALWAYS_INLINE
#endif

// [[no_sanitize()]]
#if defined(__clang__)
#define NO_SANITIZE(sanitizer) __attribute__((no_sanitize(sanitizer)))
#else
#define NO_SANITIZE(sanitizer)
#endif
5 changes: 0 additions & 5 deletions test/benchmarks/keccak_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
// SPDX-License-Identifier: Apache-2.0

#include "keccak_utils.hpp"
#include <cstdint>
#include <cstring>

#pragma GCC diagnostic ignored "-Wcast-qual"
#pragma clang diagnostic ignored "-Wcast-qual"
#pragma clang diagnostic ignored "-Wcast-align"

#define fix_endianness(X) X

namespace
Expand Down

0 comments on commit 02b3417

Please sign in to comment.