From 63f1c0a57d7afd8a4730985cf720118794045b99 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Fri, 1 Dec 2017 10:40:45 -0800 Subject: [PATCH] fix gflags namespace Summary: I started adding gflags support for cmake on linux and got frustrated that I'd need to duplicate the build_detect_platform logic, which determines namespace based on attempting compilation. We can do it differently -- use the GFLAGS_NAMESPACE macro if available, and if not, that indicates it's an old gflags version without configurable namespace so we can simply hardcode "google". Closes https://github.com/facebook/rocksdb/pull/3212 Differential Revision: D6456973 Pulled By: ajkr fbshipit-source-id: 3e6d5bde3ca00d4496a120a7caf4687399f5d656 --- build_tools/build_detect_platform | 14 +------------- cache/cache_bench.cc | 8 ++++---- db/forward_iterator_bench.cc | 6 +++--- db/prefix_test.cc | 6 +++--- memtable/memtablerep_bench.cc | 9 ++++----- options/options_settable_test.cc | 4 ++-- options/options_test.cc | 4 ++-- table/cuckoo_table_reader_test.cc | 10 +++++----- table/table_reader_bench.cc | 7 +++---- tools/db_bench_tool.cc | 8 ++++---- tools/db_bench_tool_test.cc | 2 +- tools/db_repl_stress.cc | 7 +++---- tools/db_stress.cc | 8 ++++---- tools/dump/rocksdb_dump.cc | 4 ++-- tools/dump/rocksdb_undump.cc | 4 ++-- tools/write_stress.cc | 9 ++++----- util/bloom_test.cc | 4 ++-- util/dynamic_bloom_test.cc | 6 +++--- util/gflags_compat.h | 12 ++++++++++++ util/heap_test.cc | 4 ++-- util/log_write_bench.cc | 7 +++---- utilities/column_aware_encoding_exp.cc | 4 ++-- utilities/options/options_util_test.cc | 4 ++-- utilities/persistent_cache/hash_table_bench.cc | 8 ++++---- .../persistent_cache/persistent_cache_bench.cc | 8 ++++---- 25 files changed, 81 insertions(+), 86 deletions(-) create mode 100644 util/gflags_compat.h diff --git a/build_tools/build_detect_platform b/build_tools/build_detect_platform index 82eaee075..80937ffac 100755 --- a/build_tools/build_detect_platform +++ b/build_tools/build_detect_platform @@ -227,23 +227,11 @@ EOF # check if the namespace is gflags $CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null << EOF #include - using namespace gflags; int main() {} EOF if [ "$?" = 0 ]; then - COMMON_FLAGS="$COMMON_FLAGS -DGFLAGS=gflags" + COMMON_FLAGS="$COMMON_FLAGS -DGFLAGS=1" PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lgflags" - else - # check if namespace is google - $CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null << EOF - #include - using namespace google; - int main() {} -EOF - if [ "$?" = 0 ]; then - COMMON_FLAGS="$COMMON_FLAGS -DGFLAGS=google" - PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -lgflags" - fi fi # Test whether zlib library is installed diff --git a/cache/cache_bench.cc b/cache/cache_bench.cc index 16c2ced1d..4bf971d25 100644 --- a/cache/cache_bench.cc +++ b/cache/cache_bench.cc @@ -17,16 +17,16 @@ int main() { #include #include #include -#include -#include "rocksdb/db.h" +#include "port/port.h" #include "rocksdb/cache.h" +#include "rocksdb/db.h" #include "rocksdb/env.h" -#include "port/port.h" +#include "util/gflags_compat.h" #include "util/mutexlock.h" #include "util/random.h" -using GFLAGS::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; static const uint32_t KB = 1024; diff --git a/db/forward_iterator_bench.cc b/db/forward_iterator_bench.cc index e9ae770cf..e5af43628 100644 --- a/db/forward_iterator_bench.cc +++ b/db/forward_iterator_bench.cc @@ -17,7 +17,6 @@ int main() { // Block forward_iterator_bench under MAC and Windows int main() { return 0; } #else -#include #include #include #include @@ -30,11 +29,12 @@ int main() { return 0; } #include #include +#include "port/port.h" #include "rocksdb/cache.h" #include "rocksdb/db.h" #include "rocksdb/status.h" #include "rocksdb/table.h" -#include "port/port.h" +#include "util/gflags_compat.h" #include "util/testharness.h" const int MAX_SHARDS = 100000; @@ -319,7 +319,7 @@ struct StatsThread { }; int main(int argc, char** argv) { - GFLAGS::ParseCommandLineFlags(&argc, &argv, true); + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); std::mt19937 rng{std::random_device()()}; rocksdb::Status status; diff --git a/db/prefix_test.cc b/db/prefix_test.cc index 70bf525de..c3dfdea2e 100644 --- a/db/prefix_test.cc +++ b/db/prefix_test.cc @@ -17,7 +17,6 @@ int main() { #include #include -#include #include "db/db_impl.h" #include "monitoring/histogram.h" #include "rocksdb/comparator.h" @@ -27,14 +26,15 @@ int main() { #include "rocksdb/perf_context.h" #include "rocksdb/slice_transform.h" #include "rocksdb/table.h" +#include "util/coding.h" +#include "util/gflags_compat.h" #include "util/random.h" #include "util/stop_watch.h" #include "util/string_util.h" #include "util/testharness.h" #include "utilities/merge_operators.h" -#include "util/coding.h" -using GFLAGS::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; DEFINE_bool(trigger_deadlock, false, "issue delete in range scan to trigger PrefixHashMap deadlock"); diff --git a/memtable/memtablerep_bench.cc b/memtable/memtablerep_bench.cc index 63a0201ce..a357de29b 100644 --- a/memtable/memtablerep_bench.cc +++ b/memtable/memtablerep_bench.cc @@ -19,8 +19,6 @@ int main() { } #else -#include - #include #include #include @@ -38,13 +36,14 @@ int main() { #include "rocksdb/slice_transform.h" #include "rocksdb/write_buffer_manager.h" #include "util/arena.h" +#include "util/gflags_compat.h" #include "util/mutexlock.h" #include "util/stop_watch.h" #include "util/testutil.h" -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::RegisterFlagValidator; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::RegisterFlagValidator; +using GFLAGS_NAMESPACE::SetUsageMessage; DEFINE_string(benchmarks, "fillrandom", "Comma-separated list of benchmarks to run. Options:\n" diff --git a/options/options_settable_test.cc b/options/options_settable_test.cc index cc791027f..34157a0ec 100644 --- a/options/options_settable_test.cc +++ b/options/options_settable_test.cc @@ -20,8 +20,8 @@ #ifndef GFLAGS bool FLAGS_enable_print = false; #else -#include -using GFLAGS::ParseCommandLineFlags; +#include "util/gflags_compat.h" +using GFLAGS_NAMESPACE::ParseCommandLineFlags; DEFINE_bool(enable_print, false, "Print options generated to console."); #endif // GFLAGS diff --git a/options/options_test.cc b/options/options_test.cc index 3d9d37de9..edbfd3cfb 100644 --- a/options/options_test.cc +++ b/options/options_test.cc @@ -34,8 +34,8 @@ #ifndef GFLAGS bool FLAGS_enable_print = false; #else -#include -using GFLAGS::ParseCommandLineFlags; +#include "util/gflags_compat.h" +using GFLAGS_NAMESPACE::ParseCommandLineFlags; DEFINE_bool(enable_print, false, "Print options generated to console."); #endif // GFLAGS diff --git a/table/cuckoo_table_reader_test.cc b/table/cuckoo_table_reader_test.cc index 7e131e56e..981b9804c 100644 --- a/table/cuckoo_table_reader_test.cc +++ b/table/cuckoo_table_reader_test.cc @@ -18,24 +18,24 @@ int main() { #endif #include -#include #include #include #include -#include "table/meta_blocks.h" #include "table/cuckoo_table_builder.h" -#include "table/cuckoo_table_reader.h" #include "table/cuckoo_table_factory.h" +#include "table/cuckoo_table_reader.h" #include "table/get_context.h" +#include "table/meta_blocks.h" #include "util/arena.h" +#include "util/gflags_compat.h" #include "util/random.h" #include "util/string_util.h" #include "util/testharness.h" #include "util/testutil.h" -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::SetUsageMessage; DEFINE_string(file_dir, "", "Directory where the files will be created" " for benchmark. Added for using tmpfs."); diff --git a/table/table_reader_bench.cc b/table/table_reader_bench.cc index 85e48c1fe..0c60f55ff 100644 --- a/table/table_reader_bench.cc +++ b/table/table_reader_bench.cc @@ -11,8 +11,6 @@ int main() { } #else -#include - #include "db/db_impl.h" #include "db/dbformat.h" #include "monitoring/histogram.h" @@ -25,11 +23,12 @@ int main() { #include "table/plain_table_factory.h" #include "table/table_builder.h" #include "util/file_reader_writer.h" +#include "util/gflags_compat.h" #include "util/testharness.h" #include "util/testutil.h" -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::SetUsageMessage; namespace rocksdb { diff --git a/tools/db_bench_tool.cc b/tools/db_bench_tool.cc index fe29b100d..4d9f0b010 100644 --- a/tools/db_bench_tool.cc +++ b/tools/db_bench_tool.cc @@ -20,7 +20,6 @@ #include #endif #include -#include #include #include #include @@ -61,6 +60,7 @@ #include "util/cast_util.h" #include "util/compression.h" #include "util/crc32c.h" +#include "util/gflags_compat.h" #include "util/mutexlock.h" #include "util/random.h" #include "util/stderr_logger.h" @@ -76,9 +76,9 @@ #include // open/close #endif -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::RegisterFlagValidator; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::RegisterFlagValidator; +using GFLAGS_NAMESPACE::SetUsageMessage; DEFINE_string( benchmarks, diff --git a/tools/db_bench_tool_test.cc b/tools/db_bench_tool_test.cc index 145f329de..687f6574f 100644 --- a/tools/db_bench_tool_test.cc +++ b/tools/db_bench_tool_test.cc @@ -15,7 +15,7 @@ #include "util/testutil.h" #ifdef GFLAGS -#include +#include "util/gflags_compat.h" namespace rocksdb { namespace { diff --git a/tools/db_repl_stress.cc b/tools/db_repl_stress.cc index fac73c066..408e6484f 100644 --- a/tools/db_repl_stress.cc +++ b/tools/db_repl_stress.cc @@ -15,11 +15,10 @@ int main() { #include #include -#include - #include "db/write_batch_internal.h" #include "rocksdb/db.h" #include "rocksdb/types.h" +#include "util/gflags_compat.h" #include "util/testutil.h" // Run a thread to perform Put's. @@ -30,8 +29,8 @@ int main() { using namespace rocksdb; -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::SetUsageMessage; struct DataPumpThread { size_t no_records; diff --git a/tools/db_stress.cc b/tools/db_stress.cc index e64218d20..592f30027 100644 --- a/tools/db_stress.cc +++ b/tools/db_stress.cc @@ -39,7 +39,6 @@ int main() { #include #include -#include #include "db/db_impl.h" #include "db/version_set.h" #include "hdfs/env_hdfs.h" @@ -56,6 +55,7 @@ int main() { #include "util/coding.h" #include "util/compression.h" #include "util/crc32c.h" +#include "util/gflags_compat.h" #include "util/logging.h" #include "util/mutexlock.h" #include "util/random.h" @@ -68,9 +68,9 @@ int main() { #include "utilities/merge_operators.h" -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::RegisterFlagValidator; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::RegisterFlagValidator; +using GFLAGS_NAMESPACE::SetUsageMessage; static const long KB = 1024; static const int kRandomValueMaxFactor = 3; diff --git a/tools/dump/rocksdb_dump.cc b/tools/dump/rocksdb_dump.cc index ddbfc2fb6..249371a27 100644 --- a/tools/dump/rocksdb_dump.cc +++ b/tools/dump/rocksdb_dump.cc @@ -18,9 +18,9 @@ int main() { #else -#include #include "rocksdb/convenience.h" #include "rocksdb/db_dump_tool.h" +#include "util/gflags_compat.h" DEFINE_string(db_path, "", "Path to the db that will be dumped"); DEFINE_string(dump_location, "", "Path to where the dump file location"); @@ -30,7 +30,7 @@ DEFINE_string(db_options, "", "Options string used to open the database that will be dumped"); int main(int argc, char** argv) { - GFLAGS::ParseCommandLineFlags(&argc, &argv, true); + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); if (FLAGS_db_path == "" || FLAGS_dump_location == "") { fprintf(stderr, "Please set --db_path and --dump_location\n"); diff --git a/tools/dump/rocksdb_undump.cc b/tools/dump/rocksdb_undump.cc index 0d04ccaa6..450c7e475 100644 --- a/tools/dump/rocksdb_undump.cc +++ b/tools/dump/rocksdb_undump.cc @@ -18,9 +18,9 @@ int main() { #else -#include #include "rocksdb/convenience.h" #include "rocksdb/db_dump_tool.h" +#include "util/gflags_compat.h" DEFINE_string(dump_location, "", "Path to the dump file that will be loaded"); DEFINE_string(db_path, "", "Path to the db that we will undump the file into"); @@ -29,7 +29,7 @@ DEFINE_string(db_options, "", "Options string used to open the database that will be loaded"); int main(int argc, char **argv) { - GFLAGS::ParseCommandLineFlags(&argc, &argv, true); + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); if (FLAGS_db_path == "" || FLAGS_dump_location == "") { fprintf(stderr, "Please set --db_path and --dump_location\n"); diff --git a/tools/write_stress.cc b/tools/write_stress.cc index e8427be47..c010f5edf 100644 --- a/tools/write_stress.cc +++ b/tools/write_stress.cc @@ -56,8 +56,6 @@ int main() { } #else -#include - #define __STDC_FORMAT_MACROS #include #include @@ -72,10 +70,11 @@ int main() { #include "rocksdb/options.h" #include "rocksdb/slice.h" #include "util/filename.h" +#include "util/gflags_compat.h" -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::RegisterFlagValidator; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::RegisterFlagValidator; +using GFLAGS_NAMESPACE::SetUsageMessage; DEFINE_int32(key_size, 10, "Key size"); DEFINE_int32(value_size, 100, "Value size"); diff --git a/util/bloom_test.cc b/util/bloom_test.cc index 9c323414e..bbf1d3ae9 100644 --- a/util/bloom_test.cc +++ b/util/bloom_test.cc @@ -15,17 +15,17 @@ int main() { } #else -#include #include #include "rocksdb/filter_policy.h" #include "table/full_filter_bits_builder.h" #include "util/arena.h" +#include "util/gflags_compat.h" #include "util/logging.h" #include "util/testharness.h" #include "util/testutil.h" -using GFLAGS::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; DEFINE_int32(bits_per_key, 10, ""); diff --git a/util/dynamic_bloom_test.cc b/util/dynamic_bloom_test.cc index f50036b76..1bd9e0b1b 100644 --- a/util/dynamic_bloom_test.cc +++ b/util/dynamic_bloom_test.cc @@ -15,7 +15,6 @@ int main() { #define __STDC_FORMAT_MACROS #endif -#include #include #include #include @@ -27,12 +26,13 @@ int main() { #include "dynamic_bloom.h" #include "port/port.h" #include "util/arena.h" +#include "util/gflags_compat.h" #include "util/logging.h" +#include "util/stop_watch.h" #include "util/testharness.h" #include "util/testutil.h" -#include "util/stop_watch.h" -using GFLAGS::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; DEFINE_int32(bits_per_key, 10, ""); DEFINE_int32(num_probes, 6, ""); diff --git a/util/gflags_compat.h b/util/gflags_compat.h new file mode 100644 index 000000000..0ea3aef5e --- /dev/null +++ b/util/gflags_compat.h @@ -0,0 +1,12 @@ +// Copyright (c) 2017-present, Facebook, Inc. All rights reserved. +// This source code is licensed under both the GPLv2 (found in the +// COPYING file in the root directory) and Apache 2.0 License +// (found in the LICENSE.Apache file in the root directory). + +#include + +#ifndef GFLAGS_NAMESPACE +// in case it's not defined in old versions, that's probably because it was +// still google by default. +#define GFLAGS_NAMESPACE google +#endif diff --git a/util/heap_test.cc b/util/heap_test.cc index b415615f6..d036a62eb 100644 --- a/util/heap_test.cc +++ b/util/heap_test.cc @@ -16,7 +16,7 @@ #ifndef GFLAGS const int64_t FLAGS_iters = 100000; #else -#include +#include "util/gflags_compat.h" DEFINE_int64(iters, 100000, "number of pseudo-random operations in each test"); #endif // GFLAGS @@ -133,7 +133,7 @@ INSTANTIATE_TEST_CASE_P( int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); #ifdef GFLAGS - GFLAGS::ParseCommandLineFlags(&argc, &argv, true); + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); #endif // GFLAGS return RUN_ALL_TESTS(); } diff --git a/util/log_write_bench.cc b/util/log_write_bench.cc index 4008e4337..f42b12fc4 100644 --- a/util/log_write_bench.cc +++ b/util/log_write_bench.cc @@ -11,16 +11,15 @@ int main() { } #else -#include - #include "monitoring/histogram.h" #include "rocksdb/env.h" #include "util/file_reader_writer.h" +#include "util/gflags_compat.h" #include "util/testharness.h" #include "util/testutil.h" -using GFLAGS::ParseCommandLineFlags; -using GFLAGS::SetUsageMessage; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::SetUsageMessage; // A simple benchmark to simulate transactional logs diff --git a/utilities/column_aware_encoding_exp.cc b/utilities/column_aware_encoding_exp.cc index 9dcd23eed..38eaf9d2a 100644 --- a/utilities/column_aware_encoding_exp.cc +++ b/utilities/column_aware_encoding_exp.cc @@ -13,7 +13,6 @@ #ifndef ROCKSDB_LITE #ifdef GFLAGS -#include #include #include #include "rocksdb/env.h" @@ -23,11 +22,12 @@ #include "table/format.h" #include "tools/sst_dump_tool_imp.h" #include "util/compression.h" +#include "util/gflags_compat.h" #include "util/stop_watch.h" #include "utilities/col_buf_encoder.h" #include "utilities/column_aware_encoding_util.h" -using GFLAGS::ParseCommandLineFlags; +using GFLAGS_NAMESPACE::ParseCommandLineFlags; DEFINE_string(encoded_file, "", "file to store encoded data blocks"); DEFINE_string(decoded_file, "", "file to store decoded data blocks after encoding"); diff --git a/utilities/options/options_util_test.cc b/utilities/options/options_util_test.cc index 2ca8d4767..6a4efd778 100644 --- a/utilities/options/options_util_test.cc +++ b/utilities/options/options_util_test.cc @@ -24,8 +24,8 @@ #ifndef GFLAGS bool FLAGS_enable_print = false; #else -#include -using GFLAGS::ParseCommandLineFlags; +#include "util/gflags_compat.h" +using GFLAGS_NAMESPACE::ParseCommandLineFlags; DEFINE_bool(enable_print, false, "Print options generated to console."); #endif // GFLAGS diff --git a/utilities/persistent_cache/hash_table_bench.cc b/utilities/persistent_cache/hash_table_bench.cc index 65bcd7723..7505c43c0 100644 --- a/utilities/persistent_cache/hash_table_bench.cc +++ b/utilities/persistent_cache/hash_table_bench.cc @@ -10,7 +10,6 @@ #include int main() { fprintf(stderr, "Please install gflags to run tools\n"); } #else -#include #include #include @@ -19,6 +18,7 @@ int main() { fprintf(stderr, "Please install gflags to run tools\n"); } #include "port/port_posix.h" #include "rocksdb/env.h" +#include "util/gflags_compat.h" #include "util/mutexlock.h" #include "utilities/persistent_cache/hash_table.h" @@ -272,9 +272,9 @@ class GranularLockImpl : public HashTableImpl { // main // int main(int argc, char** argv) { - GFLAGS::SetUsageMessage(std::string("\nUSAGE:\n") + std::string(argv[0]) + - " [OPTIONS]..."); - GFLAGS::ParseCommandLineFlags(&argc, &argv, false); + GFLAGS_NAMESPACE::SetUsageMessage(std::string("\nUSAGE:\n") + + std::string(argv[0]) + " [OPTIONS]..."); + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, false); // // Micro benchmark unordered_map diff --git a/utilities/persistent_cache/persistent_cache_bench.cc b/utilities/persistent_cache/persistent_cache_bench.cc index 4aeb0549c..7d26c3a7d 100644 --- a/utilities/persistent_cache/persistent_cache_bench.cc +++ b/utilities/persistent_cache/persistent_cache_bench.cc @@ -9,7 +9,6 @@ #include int main() { fprintf(stderr, "Please install gflags to run tools\n"); } #else -#include #include #include #include @@ -25,6 +24,7 @@ int main() { fprintf(stderr, "Please install gflags to run tools\n"); } #include "monitoring/histogram.h" #include "port/port.h" #include "table/block_builder.h" +#include "util/gflags_compat.h" #include "util/mutexlock.h" #include "util/stop_watch.h" @@ -307,9 +307,9 @@ class CacheTierBenchmark { // main // int main(int argc, char** argv) { - GFLAGS::SetUsageMessage(std::string("\nUSAGE:\n") + std::string(argv[0]) + - " [OPTIONS]..."); - GFLAGS::ParseCommandLineFlags(&argc, &argv, false); + GFLAGS_NAMESPACE::SetUsageMessage(std::string("\nUSAGE:\n") + + std::string(argv[0]) + " [OPTIONS]..."); + GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, false); std::ostringstream msg; msg << "Config" << std::endl