Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation with GCC9 and MSVC 2019 with CMAKE_CXX_STANDARD=20 #6648

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,18 @@ env:
- JOB_NAME=examples # 5-7 minutes
- JOB_NAME=cmake # 3-5 minutes
- JOB_NAME=cmake-gcc8 # 3-5 minutes
- JOB_NAME=cmake-gcc9 # 3-5 minutes
- JOB_NAME=cmake-gcc9-c++20 # 3-5 minutes
- JOB_NAME=cmake-mingw # 3 minutes

matrix:
exclude:
- os: osx
env: JOB_NAME=cmake-gcc8
- os: osx
env: JOB_NAME=cmake-gcc9
- os: osx
env: JOB_NAME=cmake-gcc9-c++20
- os: osx
env: JOB_NAME=cmake-mingw
- os: osx
Expand Down Expand Up @@ -157,7 +163,22 @@ matrix:
os: linux
arch: ppc64le
env: JOB_NAME=cmake-gcc8

- if: type = pull_request
os : linux
arch: arm64
env: JOB_NAME=cmake-gcc9
- if: type = pull_request
os: linux
arch: ppc64le
env: JOB_NAME=cmake-gcc9
- if: type = pull_request
os : linux
arch: arm64
env: JOB_NAME=cmake-gcc9-c++20
- if: type = pull_request
os: linux
arch: ppc64le
env: JOB_NAME=cmake-gcc9-c++20
install:
- if [ "${TRAVIS_OS_NAME}" == osx ]; then
PATH=$PATH:/usr/local/opt/ccache/libexec;
Expand All @@ -166,6 +187,10 @@ install:
sudo apt-get install -y g++-8;
CC=gcc-8 && CXX=g++-8;
fi
- if [ "${JOB_NAME}" == cmake-gcc9 ] || [ "${JOB_NAME}" == cmake-gcc9-c++20 ]; then
sudo apt-get install -y g++-9;
CC=gcc-9 && CXX=g++-9;
fi
- if [ "${JOB_NAME}" == cmake-mingw ]; then
sudo apt-get install -y mingw-w64 ;
fi
Expand Down Expand Up @@ -235,7 +260,13 @@ script:
mkdir build && cd build && cmake -DJNI=1 -DWITH_GFLAGS=OFF .. -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DCMAKE_SYSTEM_NAME=Windows && make -j4 rocksdb rocksdbjni
;;
cmake*)
mkdir build && cd build && cmake -DJNI=1 .. -DCMAKE_BUILD_TYPE=Release && make -j4 rocksdb rocksdbjni
case $JOB_NAME in
*-c++20)
OPT=-DCMAKE_CXX_STANDARD=20
;;
esac

mkdir build && cd build && cmake -DJNI=1 .. -DCMAKE_BUILD_TYPE=Release $OPT && make -j4 rocksdb rocksdbjni
;;
esac
notifications:
Expand Down
64 changes: 57 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ else()
option(WITH_FOLLY_DISTRIBUTED_MUTEX "build with folly::DistributedMutex" OFF)
endif()

if( NOT DEFINED CMAKE_CXX_STANDARD )
set(CMAKE_CXX_STANDARD 11)
endif()

include(CMakeDependentOption)
CMAKE_DEPENDENT_OPTION(WITH_GFLAGS "build with GFlags" ON
"NOT MSVC;NOT MINGW" OFF)
Expand Down Expand Up @@ -191,7 +195,6 @@ else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format -fno-asynchronous-unwind-tables")
add_definitions(-D_POSIX_C_SOURCE=1)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
include(CheckCXXCompilerFlag)
Expand Down Expand Up @@ -254,6 +257,7 @@ include(CheckCXXSourceCompiles)
if(NOT MSVC)
set(CMAKE_REQUIRED_FLAGS "-msse4.2 -mpclmul")
endif()

CHECK_CXX_SOURCE_COMPILES("
#include <cstdint>
#include <nmmintrin.h>
Expand Down Expand Up @@ -386,7 +390,15 @@ if(MSVC)
message(STATUS "Debug optimization is enabled")
set(CMAKE_CXX_FLAGS_DEBUG "/Oxt")
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /RTC1 /Gm")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /RTC1")

# Minimal Build is deprecated after MSVC 2015
if( MSVC_VERSION GREATER 1900 )
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Gm-")
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Gm")
endif()

endif()
if(WITH_RUNTIME_DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /${RUNTIME_LIBRARY}d")
Expand Down Expand Up @@ -495,6 +507,45 @@ if(HAVE_AUXV_GETAUXVAL)
add_definitions(-DROCKSDB_AUXV_GETAUXVAL_PRESENT)
endif()

# Workaround to get c++ std version option coming from CMAKE_CXX_STANDARD for try_compile
include(CxxFlags)
get_cxx_std_flags(CMAKE_REQUIRED_FLAGS)

CHECK_CXX_SOURCE_COMPILES("
template< typename F >
int apply(F f) {
return f();
}

struct test {
test() : value_(0) {}

int lambda() {
return apply([=]() {
return value_;
});
}

int value_;
};

int main() {
test t;
return t.lambda();
}
" HAVE_IMPLICIT_THIS_LAMBDA_CAPTURE
)

unset(CMAKE_REQUIRED_FLAGS)

if(HAVE_IMPLICIT_THIS_LAMBDA_CAPTURE)
set(ROCKSDB_THIS_LAMBDA_CAPTURE "=")
else()
set(ROCKSDB_THIS_LAMBDA_CAPTURE "=, this")
endif()
add_definitions(-DROCKSDB_THIS_LAMBDA_CAPTURE=${ROCKSDB_THIS_LAMBDA_CAPTURE})


include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(SYSTEM ${PROJECT_SOURCE_DIR}/third-party/gtest-1.8.1/fused-src)
Expand Down Expand Up @@ -848,7 +899,6 @@ if(ROCKSDB_BUILD_SHARED)
LINKER_LANGUAGE CXX
VERSION ${rocksdb_VERSION}
SOVERSION ${rocksdb_VERSION_MAJOR}
CXX_STANDARD 11
OUTPUT_NAME "rocksdb")
endif()
endif()
Expand Down Expand Up @@ -941,7 +991,7 @@ if(WITH_TESTS)
add_library(testharness STATIC
test_util/testharness.cc)
target_link_libraries(testharness gtest)

set(TESTS
cache/cache_test.cc
cache/lru_cache_test.cc
Expand Down Expand Up @@ -1125,7 +1175,7 @@ if(WITH_TESTS)
PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD_RELEASE 1
EXCLUDE_FROM_DEFAULT_BUILD_MINRELEASE 1
EXCLUDE_FROM_DEFAULT_BUILD_RELWITHDEBINFO 1
)
)

foreach(sourcefile ${TESTS})
get_filename_component(exename ${sourcefile} NAME_WE)
Expand All @@ -1135,7 +1185,7 @@ if(WITH_TESTS)
EXCLUDE_FROM_DEFAULT_BUILD_MINRELEASE 1
EXCLUDE_FROM_DEFAULT_BUILD_RELWITHDEBINFO 1
OUTPUT_NAME ${exename}${ARTIFACT_SUFFIX}
)
)
target_link_libraries(${CMAKE_PROJECT_NAME}_${exename}${ARTIFACT_SUFFIX} testutillib${ARTIFACT_SUFFIX} testharness gtest ${ROCKSDB_LIB})
if(NOT "${exename}" MATCHES "db_sanity_test")
add_test(NAME ${exename} COMMAND ${exename}${ARTIFACT_SUFFIX})
Expand Down Expand Up @@ -1215,4 +1265,4 @@ if(WITH_TOOLS)
add_subdirectory(db_stress_tool)
add_custom_target(tools
DEPENDS ${tool_deps})
endif()
endif()
17 changes: 14 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 1.0.{build}

image: Visual Studio 2017
image: Visual Studio 2019

environment:
JAVA_HOME: C:\Program Files\Java\jdk1.8.0
Expand All @@ -24,6 +24,15 @@ environment:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
CMAKE_GENERATOR: Visual Studio 15 Win64
DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\devenv.com
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
CMAKE_GENERATOR: Visual Studio 16
CMAKE_PLATEFORM_NAME: x64
DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.com
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
CMAKE_GENERATOR: Visual Studio 16
CMAKE_PLATEFORM_NAME: x64
CMAKE_OPT: -DCMAKE_CXX_STANDARD=20
DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.com

install:
- md %THIRDPARTY_HOME%
Expand All @@ -34,7 +43,8 @@ install:
- cd snappy-1.1.7
- mkdir build
- cd build
- cmake -G "%CMAKE_GENERATOR%" ..
- if DEFINED CMAKE_PLATEFORM_NAME (set "PLATEFORM_OPT=-A %CMAKE_PLATEFORM_NAME%")
- cmake .. -G "%CMAKE_GENERATOR%" %PLATEFORM_OPT%
- msbuild Snappy.sln /p:Configuration=Debug /p:Platform=x64
- msbuild Snappy.sln /p:Configuration=Release /p:Platform=x64
- echo "Building LZ4 dependency..."
Expand All @@ -57,7 +67,8 @@ install:
before_build:
- md %APPVEYOR_BUILD_FOLDER%\build
- cd %APPVEYOR_BUILD_FOLDER%\build
- cmake -G "%CMAKE_GENERATOR%" -DCMAKE_BUILD_TYPE=Debug -DOPTDBG=1 -DPORTABLE=1 -DSNAPPY=1 -DLZ4=1 -DZSTD=1 -DXPRESS=1 -DJNI=1 ..
- if DEFINED CMAKE_PLATEFORM_NAME (set "PLATEFORM_OPT=-A %CMAKE_PLATEFORM_NAME%")
- cmake .. -G "%CMAKE_GENERATOR%" %PLATEFORM_OPT% %CMAKE_OPT% -DCMAKE_BUILD_TYPE=Debug -DOPTDBG=1 -DPORTABLE=1 -DSNAPPY=1 -DLZ4=1 -DZSTD=1 -DXPRESS=1 -DJNI=1
- cd ..

build:
Expand Down
23 changes: 23 additions & 0 deletions build_tools/build_detect_platform
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,29 @@ EOF
fi
fi


$CXX $PLATFORM_CXXFLAGS $COMMON_FLAGS -x c++ - -o /dev/null 2>/dev/null <<EOF
struct test {
test() : value_(0) {}

int lambda() {
return [=]() { return value_; }();
}

int value_;
};

int main() {
test t;
return t.lambda();
}
EOF
if [ "$?" = 0 ]; then
COMMON_FLAGS="$COMMON_FLAGS -DROCKSDB_THIS_LAMBDA_CAPTURE=\"=\""
else
COMMON_FLAGS="$COMMON_FLAGS -DROCKSDB_THIS_LAMBDA_CAPTURE=\"=, this\""
fi

if [ "$FBCODE_BUILD" != "true" -a "$PLATFORM" = OS_LINUX ]; then
$CXX $COMMON_FLAGS $PLATFORM_SHARED_CFLAGS -x c++ -c - -o test_dl.o 2>/dev/null <<EOF
void dummy_func() {}
Expand Down
7 changes: 7 additions & 0 deletions cmake/modules/CxxFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro(get_cxx_std_flags FLAGS_VARIABLE)
if( CMAKE_CXX_STANDARD_REQUIRED )
set(${FLAGS_VARIABLE} ${CMAKE_CXX${CMAKE_CXX_STANDARD}_STANDARD_COMPILE_OPTION})
else()
set(${FLAGS_VARIABLE} ${CMAKE_CXX${CMAKE_CXX_STANDARD}_EXTENSION_COMPILE_OPTION})
endif()
endmacro()
6 changes: 4 additions & 2 deletions db/db_bloom_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "rocksdb/perf_context.h"
#include "table/block_based/filter_policy_internal.h"


namespace ROCKSDB_NAMESPACE {

namespace {
Expand Down Expand Up @@ -1343,8 +1344,9 @@ TEST_F(DBBloomFilterTest, OptimizeFiltersForHits) {
for (int i = 0; i < numkeys; i += 2) {
keys.push_back(i);
}
std::random_shuffle(std::begin(keys), std::end(keys));

std::random_device rng;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should unify all of these, including

test_util/transaction_test_util.cc: std::shuffle(set_vec.begin(), set_vec.end(), std::random_device{});

with a single implementation in util/random.h,cc

Though we can do this after merging this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pdillinger Yes you are right, I would be happy to propose something after this PR if it's good for you. Else I can try here, but I would prefer making it in another step.

std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);
int num_inserted = 0;
for (int key : keys) {
ASSERT_OK(Put(1, Key(key), "val"));
Expand Down
4 changes: 3 additions & 1 deletion db/db_compaction_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4762,7 +4762,9 @@ TEST_P(CompactionPriTest, Test) {
for (int i = 0; i < kNKeys; i++) {
keys[i] = i;
}
std::random_shuffle(std::begin(keys), std::end(keys));
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);

for (int i = 0; i < kNKeys; i++) {
ASSERT_OK(Put(Key(keys[i]), RandomString(&rnd, 102)));
Expand Down
4 changes: 3 additions & 1 deletion db/db_dynamic_level_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ TEST_F(DBTestDynamicLevel, DynamicLevelMaxBytesBase) {
keys[i] = i;
}
if (ordered_insert == 0) {
std::random_shuffle(std::begin(keys), std::end(keys));
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);
}
for (int max_background_compactions = 1; max_background_compactions < 4;
max_background_compactions += 2) {
Expand Down
12 changes: 9 additions & 3 deletions db/db_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,9 @@ TEST_F(DBTest, ApproximateSizesMemTable) {
keys[i * 3 + 1] = i * 5 + 1;
keys[i * 3 + 2] = i * 5 + 2;
}
std::random_shuffle(std::begin(keys), std::end(keys));
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);

for (int i = 0; i < N * 3; i++) {
ASSERT_OK(Put(Key(keys[i] + 1000), RandomString(&rnd, 1024)));
Expand Down Expand Up @@ -4530,7 +4532,9 @@ TEST_F(DBTest, DynamicLevelCompressionPerLevel) {
for (int i = 0; i < kNKeys; i++) {
keys[i] = i;
}
std::random_shuffle(std::begin(keys), std::end(keys));
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);

Random rnd(301);
Options options;
Expand Down Expand Up @@ -4613,7 +4617,9 @@ TEST_F(DBTest, DynamicLevelCompressionPerLevel2) {
for (int i = 0; i < kNKeys; i++) {
keys[i] = i;
}
std::random_shuffle(std::begin(keys), std::end(keys));
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);

Random rnd(301);
Options options;
Expand Down
8 changes: 6 additions & 2 deletions db/perf_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ void ProfileQueries(bool enabled_time = false) {
}

if (FLAGS_random_key) {
std::random_shuffle(keys.begin(), keys.end());
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);
}
#ifndef NDEBUG
ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 1U);
Expand Down Expand Up @@ -524,7 +526,9 @@ TEST_F(PerfContextTest, SeekKeyComparison) {
}

if (FLAGS_random_key) {
std::random_shuffle(keys.begin(), keys.end());
std::random_device rng;
std::mt19937 urng(rng());
std::shuffle(std::begin(keys), std::end(keys), urng);
}

HistogramImpl hist_put_time;
Expand Down
2 changes: 2 additions & 0 deletions include/rocksdb/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ struct FileOptions : EnvOptions {

FileOptions(const FileOptions& opts)
: EnvOptions(opts), io_options(opts.io_options) {}

FileOptions& operator=(const FileOptions& opts) = default;
};

// A structure to pass back some debugging information from the FileSystem
Expand Down
Loading