From 40a5bdfc76493d6ed4384871f54f853472951ab0 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 16 Jul 2020 23:23:48 +1200 Subject: [PATCH 1/5] build: Update AX_CXX_COMPILE_STDCXX macro --- build-aux/m4/ax_cxx_compile_stdcxx.m4 | 455 ++++++++++++++++++++++++-- configure.ac | 2 +- 2 files changed, 420 insertions(+), 37 deletions(-) diff --git a/build-aux/m4/ax_cxx_compile_stdcxx.m4 b/build-aux/m4/ax_cxx_compile_stdcxx.m4 index f147cee3b..43087b2e6 100644 --- a/build-aux/m4/ax_cxx_compile_stdcxx.m4 +++ b/build-aux/m4/ax_cxx_compile_stdcxx.m4 @@ -1,5 +1,5 @@ # =========================================================================== -# http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html +# https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html # =========================================================================== # # SYNOPSIS @@ -33,21 +33,23 @@ # Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov # Copyright (c) 2015 Paul Norman # Copyright (c) 2015 Moritz Klammler +# Copyright (c) 2016, 2018 Krzesimir Nowak +# Copyright (c) 2019 Enji Cooper # # Copying and distribution of this file, with or without modification, are # permitted in any medium without royalty provided the copyright notice # and this notice are preserved. This file is offered as-is, without any # warranty. -#serial 4 +#serial 11 dnl This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro dnl (serial version number 13). AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl - m4_if([$1], [11], [], - [$1], [14], [], - [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])], + m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"], + [$1], [14], [ax_cxx_compile_alternatives="14 1y"], + [$1], [17], [ax_cxx_compile_alternatives="17 1z"], [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl m4_if([$2], [], [], [$2], [ext], [], @@ -57,26 +59,13 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true], [$3], [optional], [ax_cxx_compile_cxx$1_required=false], [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])]) - m4_if([$4], [], [ax_cxx_compile_cxx$1_try_default=true], - [$4], [default], [ax_cxx_compile_cxx$1_try_default=true], - [$4], [nodefault], [ax_cxx_compile_cxx$1_try_default=false], - [m4_fatal([invalid fourth argument `$4' to AX_CXX_COMPILE_STDCXX])]) AC_LANG_PUSH([C++])dnl ac_success=no - m4_if([$4], [nodefault], [], [dnl - AC_CACHE_CHECK(whether $CXX supports C++$1 features by default, - ax_cv_cxx_compile_cxx$1, - [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], - [ax_cv_cxx_compile_cxx$1=yes], - [ax_cv_cxx_compile_cxx$1=no])]) - if test x$ax_cv_cxx_compile_cxx$1 = xyes; then - ac_success=yes - fi]) - m4_if([$2], [noext], [], [dnl if test x$ac_success = xno; then - for switch in -std=gnu++$1 -std=gnu++0x; do + for alternative in ${ax_cxx_compile_alternatives}; do + switch="-std=gnu++${alternative}" cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, $cachevar, @@ -102,22 +91,27 @@ AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl dnl HP's aCC needs +std=c++11 according to: dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf dnl Cray's crayCC needs "-h std=c++11" - for switch in -std=c++$1 -std=c++0x +std=c++$1 "-h std=c++$1"; do - cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) - AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, - $cachevar, - [ac_save_CXX="$CXX" - CXX="$CXX $switch" - AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], - [eval $cachevar=yes], - [eval $cachevar=no]) - CXX="$ac_save_CXX"]) - if eval test x\$$cachevar = xyes; then - CXX="$CXX $switch" - if test -n "$CXXCPP" ; then - CXXCPP="$CXXCPP $switch" + for alternative in ${ax_cxx_compile_alternatives}; do + for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do + cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch]) + AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch, + $cachevar, + [ac_save_CXX="$CXX" + CXX="$CXX $switch" + AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])], + [eval $cachevar=yes], + [eval $cachevar=no]) + CXX="$ac_save_CXX"]) + if eval test x\$$cachevar = xyes; then + CXX="$CXX $switch" + if test -n "$CXXCPP" ; then + CXXCPP="$CXXCPP $switch" + fi + ac_success=yes + break fi - ac_success=yes + done + if test x$ac_success = xyes; then break fi done @@ -154,6 +148,11 @@ m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14], _AX_CXX_COMPILE_STDCXX_testbody_new_in_14 ) +m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17], + _AX_CXX_COMPILE_STDCXX_testbody_new_in_11 + _AX_CXX_COMPILE_STDCXX_testbody_new_in_14 + _AX_CXX_COMPILE_STDCXX_testbody_new_in_17 +) dnl Tests for new features in C++11 @@ -191,11 +190,13 @@ namespace cxx11 struct Base { + virtual ~Base() {} virtual void f() {} }; struct Derived : public Base { + virtual ~Derived() override {} virtual void f() override {} }; @@ -524,7 +525,7 @@ namespace cxx14 } - namespace test_digit_seperators + namespace test_digit_separators { constexpr auto ten_million = 100'000'000; @@ -566,3 +567,385 @@ namespace cxx14 #endif // __cplusplus >= 201402L ]]) + + +dnl Tests for new features in C++17 + +m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[ + +// If the compiler admits that it is not ready for C++17, why torture it? +// Hopefully, this will speed up the test. + +#ifndef __cplusplus + +#error "This is not a C++ compiler" + +#elif __cplusplus < 201703L + +#error "This is not a C++17 compiler" + +#else + +#include +#include +#include + +namespace cxx17 +{ + + namespace test_constexpr_lambdas + { + + constexpr int foo = [](){return 42;}(); + + } + + namespace test::nested_namespace::definitions + { + + } + + namespace test_fold_expression + { + + template + int multiply(Args... args) + { + return (args * ... * 1); + } + + template + bool all(Args... args) + { + return (args && ...); + } + + } + + namespace test_extended_static_assert + { + + static_assert (true); + + } + + namespace test_auto_brace_init_list + { + + auto foo = {5}; + auto bar {5}; + + static_assert(std::is_same, decltype(foo)>::value); + static_assert(std::is_same::value); + } + + namespace test_typename_in_template_template_parameter + { + + template typename X> struct D; + + } + + namespace test_fallthrough_nodiscard_maybe_unused_attributes + { + + int f1() + { + return 42; + } + + [[nodiscard]] int f2() + { + [[maybe_unused]] auto unused = f1(); + + switch (f1()) + { + case 17: + f1(); + [[fallthrough]]; + case 42: + f1(); + } + return f1(); + } + + } + + namespace test_extended_aggregate_initialization + { + + struct base1 + { + int b1, b2 = 42; + }; + + struct base2 + { + base2() { + b3 = 42; + } + int b3; + }; + + struct derived : base1, base2 + { + int d; + }; + + derived d1 {{1, 2}, {}, 4}; // full initialization + derived d2 {{}, {}, 4}; // value-initialized bases + + } + + namespace test_general_range_based_for_loop + { + + struct iter + { + int i; + + int& operator* () + { + return i; + } + + const int& operator* () const + { + return i; + } + + iter& operator++() + { + ++i; + return *this; + } + }; + + struct sentinel + { + int i; + }; + + bool operator== (const iter& i, const sentinel& s) + { + return i.i == s.i; + } + + bool operator!= (const iter& i, const sentinel& s) + { + return !(i == s); + } + + struct range + { + iter begin() const + { + return {0}; + } + + sentinel end() const + { + return {5}; + } + }; + + void f() + { + range r {}; + + for (auto i : r) + { + [[maybe_unused]] auto v = i; + } + } + + } + + namespace test_lambda_capture_asterisk_this_by_value + { + + struct t + { + int i; + int foo() + { + return [*this]() + { + return i; + }(); + } + }; + + } + + namespace test_enum_class_construction + { + + enum class byte : unsigned char + {}; + + byte foo {42}; + + } + + namespace test_constexpr_if + { + + template + int f () + { + if constexpr(cond) + { + return 13; + } + else + { + return 42; + } + } + + } + + namespace test_selection_statement_with_initializer + { + + int f() + { + return 13; + } + + int f2() + { + if (auto i = f(); i > 0) + { + return 3; + } + + switch (auto i = f(); i + 4) + { + case 17: + return 2; + + default: + return 1; + } + } + + } + + namespace test_template_argument_deduction_for_class_templates + { + + template + struct pair + { + pair (T1 p1, T2 p2) + : m1 {p1}, + m2 {p2} + {} + + T1 m1; + T2 m2; + }; + + void f() + { + [[maybe_unused]] auto p = pair{13, 42u}; + } + + } + + namespace test_non_type_auto_template_parameters + { + + template + struct B + {}; + + B<5> b1; + B<'a'> b2; + + } + + namespace test_structured_bindings + { + + int arr[2] = { 1, 2 }; + std::pair pr = { 1, 2 }; + + auto f1() -> int(&)[2] + { + return arr; + } + + auto f2() -> std::pair& + { + return pr; + } + + struct S + { + int x1 : 2; + volatile double y1; + }; + + S f3() + { + return {}; + } + + auto [ x1, y1 ] = f1(); + auto& [ xr1, yr1 ] = f1(); + auto [ x2, y2 ] = f2(); + auto& [ xr2, yr2 ] = f2(); + const auto [ x3, y3 ] = f3(); + + } + + namespace test_exception_spec_type_system + { + + struct Good {}; + struct Bad {}; + + void g1() noexcept; + void g2(); + + template + Bad + f(T*, T*); + + template + Good + f(T1*, T2*); + + static_assert (std::is_same_v); + + } + + namespace test_inline_variables + { + + template void f(T) + {} + + template inline T g(T) + { + return T{}; + } + + template<> inline void f<>(int) + {} + + template<> int g<>(int) + { + return 5; + } + + } + +} // namespace cxx17 + +#endif // __cplusplus < 201703L + +]]) diff --git a/configure.ac b/configure.ac index a3ce3229b..4bf8712f1 100644 --- a/configure.ac +++ b/configure.ac @@ -62,7 +62,7 @@ case $host in ;; esac dnl Require C++11 compiler (no GNU extensions) -AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory], [nodefault]) +AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory]) dnl Check if -latomic is required for CHECK_ATOMIC From 9d1a11a0e4459394145d41ea072930ea9660fb77 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 16 Jul 2020 23:39:02 +1200 Subject: [PATCH 2/5] build: Require and build with C++ 17 --- configure.ac | 4 ++-- src/leveldb/build_detect_platform | 15 +++------------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/configure.ac b/configure.ac index 4bf8712f1..2e870e762 100644 --- a/configure.ac +++ b/configure.ac @@ -61,8 +61,8 @@ case $host in lt_cv_deplibs_check_method="pass_all" ;; esac -dnl Require C++11 compiler (no GNU extensions) -AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory]) +dnl Require C++17 compiler (no GNU extensions) +AX_CXX_COMPILE_STDCXX([17], [noext], [mandatory]) dnl Check if -latomic is required for CHECK_ATOMIC diff --git a/src/leveldb/build_detect_platform b/src/leveldb/build_detect_platform index 4a9471590..ce8f3f9ce 100755 --- a/src/leveldb/build_detect_platform +++ b/src/leveldb/build_detect_platform @@ -56,7 +56,7 @@ fi COMMON_FLAGS= CROSS_COMPILE= PLATFORM_CCFLAGS= -PLATFORM_CXXFLAGS= +PLATFORM_CXXFLAGS="-std=c++17" PLATFORM_LDFLAGS= PLATFORM_LIBS= PLATFORM_SHARED_EXT="so" @@ -203,18 +203,9 @@ if [ "$CROSS_COMPILE" = "true" ]; then else CXXOUTPUT="${TMPDIR}/leveldb_build_detect_platform-cxx.$$" - # If -std=c++0x works, use as fallback for when memory barriers + # As -std=c++17 works, use as fallback for when memory barriers # are not available. - $CXX $CXXFLAGS -std=c++0x -x c++ - -o $CXXOUTPUT 2>/dev/null < - int main() {} -EOF - if [ "$?" = 0 ]; then - COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX -DLEVELDB_ATOMIC_PRESENT" - PLATFORM_CXXFLAGS="-std=c++0x" - else - COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX" - fi + COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX -DLEVELDB_ATOMIC_PRESENT" # Test whether tcmalloc is available $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT -ltcmalloc 2>/dev/null < Date: Fri, 17 Jul 2020 00:00:27 +1200 Subject: [PATCH 3/5] depends: Build C++ dependencies with C++ 17 --- depends/packages/bdb.mk | 2 +- depends/packages/boost.mk | 2 +- depends/packages/googletest.mk | 2 +- depends/packages/zeromq.mk | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/depends/packages/bdb.mk b/depends/packages/bdb.mk index c450707a0..5a5950059 100644 --- a/depends/packages/bdb.mk +++ b/depends/packages/bdb.mk @@ -19,7 +19,7 @@ ifneq ($(build_os),darwin) $(package)_config_opts_darwin=--disable-atomicsupport endif $(package)_config_opts_aarch64=--disable-atomicsupport -$(package)_cxxflags+=-std=c++11 +$(package)_cxxflags+=-std=c++17 $(package)_ldflags+=-static-libstdc++ -lc++abi endef diff --git a/depends/packages/boost.mk b/depends/packages/boost.mk index 1b4ec97e8..c99846d0c 100644 --- a/depends/packages/boost.mk +++ b/depends/packages/boost.mk @@ -25,7 +25,7 @@ $(package)_archiver_$(host_os)=$($(package)_ar) $(package)_toolset_darwin=darwin $(package)_archiver_darwin=$($(package)_libtool) $(package)_config_libraries=chrono,filesystem,program_options,system,thread,test -$(package)_cxxflags+=-std=c++11 -fvisibility=hidden +$(package)_cxxflags+=-std=c++17 -fvisibility=hidden $(package)_cxxflags_linux=-fPIC $(package)_cxxflags_freebsd=-fPIC $(package)_ldflags+=-static-libstdc++ -lc++abi diff --git a/depends/packages/googletest.mk b/depends/packages/googletest.mk index 32e1d286c..857f75789 100644 --- a/depends/packages/googletest.mk +++ b/depends/packages/googletest.mk @@ -10,7 +10,7 @@ $(package)_dependencies=libcxx endif define $(package)_set_vars -$(package)_cxxflags+=-std=c++11 +$(package)_cxxflags+=-std=c++17 $(package)_cxxflags_linux=-fPIC $(package)_cxxflags_freebsd=-fPIC $(package)_ldflags+=-static-libstdc++ -lc++abi diff --git a/depends/packages/zeromq.mk b/depends/packages/zeromq.mk index 1124dd574..afc24ee1e 100644 --- a/depends/packages/zeromq.mk +++ b/depends/packages/zeromq.mk @@ -16,7 +16,7 @@ define $(package)_set_vars $(package)_config_opts += --disable-drafts --enable-option-checking $(package)_config_opts_linux=--with-pic $(package)_config_opts_freebsd=--with-pic - $(package)_cxxflags+=-std=c++11 + $(package)_cxxflags+=-std=c++17 $(package)_ldflags+=-static-libstdc++ -lc++abi endef From c656ae13032c0c2ef844f8e1398b02766abfc4cd Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Fri, 17 Jul 2020 15:55:34 +0100 Subject: [PATCH 4/5] Add new copyright entries for build-aux/m4/ax_cxx_compile_stdcxx.m4 Signed-off-by: Daira Hopwood --- contrib/debian/copyright | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/debian/copyright b/contrib/debian/copyright index 7edd639f4..6a142c3a1 100644 --- a/contrib/debian/copyright +++ b/contrib/debian/copyright @@ -54,6 +54,8 @@ Copyright: 2008, Benjamin Kosnik 2014-2015, Google Inc.; contributed by Alexey Sokolov 2015, Paul Norman 2015, Moritz Klammler + 2016, 2018 Krzesimir Nowak + 2019 Enji Cooper License: GNU-All-permissive-License Files: build-aux/m4/ax_gcc_func_attribute.m4 From d0cb0b74aa05ce3e37b60bf89dfb71b0c358995a Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 21 Sep 2020 23:47:42 +0100 Subject: [PATCH 5/5] Switch from std::random_shuffle to std::shuffle std::random_shuffle is removed in C++17; it is replaced by std::shuffle, which was introduced in C++11. The new ZcashRandomEngine class provides the Uniform Random Number Generator interface, as inferred from std::random_device. --- src/random.h | 29 +++++++++++++++++++++++++++++ src/wallet/wallet.cpp | 4 +++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/random.h b/src/random.h index a0802d6fa..429007519 100644 --- a/src/random.h +++ b/src/random.h @@ -9,6 +9,7 @@ #include "uint256.h" #include +#include #include /** @@ -19,6 +20,34 @@ uint64_t GetRand(uint64_t nMax); int GetRandInt(int nMax); uint256 GetRandHash(); +/** + * Implementation of a C++ Uniform Random Number Generator, backed by GetRandBytes. + */ +class ZcashRandomEngine +{ +public: + typedef uint64_t result_type; + + explicit ZcashRandomEngine() {} + + static constexpr result_type min() { + return std::numeric_limits::min(); + } + static constexpr result_type max() { + return std::numeric_limits::max(); + } + + result_type operator()() { + result_type nRand = 0; + GetRandBytes((unsigned char*)&nRand, sizeof(nRand)); + return nRand; + } + + double entropy() const noexcept { + return 0; + } +}; + /** * Identity function for MappedShuffle, so that elements retain their original order. */ diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e244a17f1..aec7e0060 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -17,6 +17,7 @@ #include "main.h" #include "net.h" #include "policy/policy.h" +#include "random.h" #include "rpc/protocol.h" #include "rpc/server.h" #include "script/script.h" @@ -27,6 +28,7 @@ #include "crypter.h" #include "wallet/asyncrpcoperation_saplingmigration.h" +#include #include #include @@ -3306,7 +3308,7 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, int nConfMine, int vector > > vValue; CAmount nTotalLower = 0; - random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt); + std::shuffle(vCoins.begin(), vCoins.end(), ZcashRandomEngine()); BOOST_FOREACH(const COutput &output, vCoins) {