From 78d470c7a50d3788bf3f819eba99597aea43674f Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Fri, 12 Jun 2015 15:49:21 -0700 Subject: [PATCH] Make the code independent of config.h Now the Build tool needs to define -DHAVE_ZLIB and -DHAVE-PTHREAD rather than providing a config.h - Make pbconfig.h a manually written file to handle hash conditions according to platform related macros. - Remove #include "config.h" from source code. - Changed the configure.ac and Makefile.am to pass down the macros. - Change cmake to pass down the the macros. Change-Id: I537249d5df8fdeba189706aec436d1ab1104a4dc --- Makefile.am | 3 - cmake/CMakeLists.txt | 14 +-- cmake/config.h.in | 4 - cmake/find_hash_map.cmake | 119 ------------------ cmake/pbconfig.h.in | 9 -- configure.ac | 6 + src/Makefile.am | 12 +- src/google/protobuf/io/gzip_stream.cc | 2 - .../protobuf/io/zero_copy_stream_unittest.cc | 2 - src/google/protobuf/stubs/common.cc | 2 - src/google/protobuf/stubs/common_unittest.cc | 2 - src/google/protobuf/testing/zcgunzip.cc | 2 - src/google/protobuf/testing/zcgzip.cc | 2 - 13 files changed, 23 insertions(+), 156 deletions(-) delete mode 100644 cmake/config.h.in delete mode 100644 cmake/find_hash_map.cmake delete mode 100644 cmake/pbconfig.h.in diff --git a/Makefile.am b/Makefile.am index 0eb8020246e26..8d7bb5634b520 100644 --- a/Makefile.am +++ b/Makefile.am @@ -744,12 +744,9 @@ EXTRA_DIST = $(@DIST_LANG@_EXTRA_DIST) \ CONTRIBUTORS.txt \ CHANGES.txt \ cmake/CMakeLists.txt \ - cmake/config.h.in \ - cmake/find_hash_map.cmake \ cmake/libprotobuf.cmake \ cmake/libprotobuf-lite.cmake \ cmake/libprotoc.cmake \ - cmake/pbconfig.h.in \ cmake/protoc.cmake \ cmake/README.md \ cmake/tests.cmake \ diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 727864ba3a47e..d36292df689c9 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -8,11 +8,11 @@ if (MSVC) option(ZLIB "Build with zlib support" OFF) endif (MSVC) +add_definitions(-DGOOGLE_PROTOBUF_CMAKE_BUILD) + find_package(Threads REQUIRED) if (CMAKE_USE_PTHREADS_INIT) - set(HAVE_PTHREAD 1) -else (CMAKE_USE_PTHREADS_INIT) - set(HAVE_PTHREAD 0) + add_definitions(-DHAVE_PTHREAD) endif (CMAKE_USE_PTHREADS_INIT) if (MSVC) @@ -36,6 +36,10 @@ else (MSVC) endif (ZLIB_FOUND) endif (MSVC) +if (HAVE_ZLIB) + add_definitions(-DHAVE_ZLIB) +endif (HAVE_ZLIB) + if (MSVC) if (BUILD_SHARED_LIBS) add_definitions(-DPROTOBUF_USE_DLLS) @@ -43,10 +47,6 @@ if (MSVC) add_definitions(/wd4244 /wd4267 /wd4018 /wd4355 /wd4800 /wd4251 /wd4996 /wd4146 /wd4305) endif (MSVC) -include(find_hash_map.cmake) - -configure_file(config.h.in config.h) -configure_file(pbconfig.h.in google/protobuf/stubs/pbconfig.h) if (MSVC) string(REPLACE "/" "\\" PROTOBUF_SOURCE_WIN32_PATH ${protobuf_SOURCE_DIR}) string(REPLACE "/" "\\" PROTOBUF_BINARY_WIN32_PATH ${protobuf_BINARY_DIR}) diff --git a/cmake/config.h.in b/cmake/config.h.in deleted file mode 100644 index bca1cbba6618e..0000000000000 --- a/cmake/config.h.in +++ /dev/null @@ -1,4 +0,0 @@ -#define GOOGLE_PROTOBUF_CMAKE_BUILD - -#define HAVE_PTHREAD ${HAVE_PTHREAD} -#define HAVE_ZLIB ${HAVE_ZLIB} diff --git a/cmake/find_hash_map.cmake b/cmake/find_hash_map.cmake deleted file mode 100644 index 22796fb880d31..0000000000000 --- a/cmake/find_hash_map.cmake +++ /dev/null @@ -1,119 +0,0 @@ -include(CheckCXXSourceCompiles) - -function(find_hash_map) - set(HAVE_HASH_MAP 1 PARENT_SCOPE) - set(HAVE_HASH_SET 1 PARENT_SCOPE) - # Search for hash_map in the following order: - # 1. ::std::unordered_map - # 2. ::std::tr1::unordered_map - # 3. ::hash_map - # 4. ::stdext::hash_map - # 5. ::std::hash_map - # 6. ::__gnu_cxx::hash_map - check_cxx_source_compiles(" - #include - int main() { ::std::unordered_map v; return v[0]; } - " HAS_STD_UNORDERED_MAP) - if (HAS_STD_UNORDERED_MAP) - set(HASH_NAMESPACE ::std PARENT_SCOPE) - set(HASH_MAP_H PARENT_SCOPE) - set(HASH_MAP_CLASS unordered_map PARENT_SCOPE) - set(HASH_SET_H PARENT_SCOPE) - set(HASH_SET_CLASS unordered_set PARENT_SCOPE) - return() - endif (HAS_STD_UNORDERED_MAP) - - check_cxx_source_compiles(" - #include - int main() { ::std::tr1::unordered_map v; return v[0]; } - " HAS_STD_TR1_UNORDERED_MAP) - if (HAS_STD_TR1_UNORDERED_MAP) - set(HASH_NAMESPACE ::std::tr1 PARENT_SCOPE) - set(HASH_MAP_H PARENT_SCOPE) - set(HASH_MAP_CLASS unordered_map PARENT_SCOPE) - set(HASH_SET_H PARENT_SCOPE) - set(HASH_SET_CLASS unordered_set PARENT_SCOPE) - return() - endif (HAS_STD_TR1_UNORDERED_MAP) - - check_cxx_source_compiles(" - #include - int main() { ::hash_map v; return v[0]; } - " HAS_HASH_MAP) - if (HAS_HASH_MAP) - set(HASH_NAMESPACE :: PARENT_SCOPE) - set(HASH_MAP_H PARENT_SCOPE) - set(HASH_MAP_CLASS hash_map PARENT_SCOPE) - set(HASH_SET_H PARENT_SCOPE) - set(HASH_SET_CLASS hash_set PARENT_SCOPE) - return() - endif (HAS_HASH_MAP) - - check_cxx_source_compiles(" - #include - int main() { ::stdext::hash_map v; return v[0]; } - " HAS_STDEXT_HASH_MAP) - if (HAS_STDEXT_HASH_MAP) - set(HASH_NAMESPACE ::stdext PARENT_SCOPE) - set(HASH_MAP_H PARENT_SCOPE) - set(HASH_MAP_CLASS hash_map PARENT_SCOPE) - set(HASH_SET_H PARENT_SCOPE) - set(HASH_SET_CLASS hash_set PARENT_SCOPE) - return() - endif (HAS_STDEXT_HASH_MAP) - - check_cxx_source_compiles(" - #include - int main() { ::std::hash_map v; return v[0]; } - " HAS_STD_HASH_MAP) - if (HAS_STD_HASH_MAP) - set(HASH_NAMESPACE ::std PARENT_SCOPE) - set(HASH_MAP_H PARENT_SCOPE) - set(HASH_MAP_CLASS hash_map PARENT_SCOPE) - set(HASH_SET_H PARENT_SCOPE) - set(HASH_SET_CLASS hash_set PARENT_SCOPE) - return() - endif (HAS_STD_HASH_MAP) - - check_cxx_source_compiles(" - #include - int main() { ::__gnu_cxx::hash_map v; return v[0]; } - " HAS_GNU_CXX_HASH_MAP) - if (HAS_GNU_CXX_HASH_MAP) - set(HASH_NAMESPACE ::gnu_cxx PARENT_SCOPE) - set(HASH_MAP_H PARENT_SCOPE) - set(HASH_MAP_CLASS hash_map PARENT_SCOPE) - set(HASH_SET_H PARENT_SCOPE) - set(HASH_SET_CLASS hash_set PARENT_SCOPE) - return() - endif (HAS_GNU_CXX_HASH_MAP) - - set(HAVE_HASH_MAP 0 PARENT_SCOPE) - set(HAVE_HASH_SET 0 PARENT_SCOPE) -endfunction() - -function(find_hash_compare) - if (MSVC) - check_cxx_source_compiles(" - #include ${HASH_MAP_H} - int main() { ::std::hash_compare cp; return cp(0); } - " HAS_STD_HASH_COMPARE) - if (HAS_STD_HASH_COMPARE) - set(HASH_COMPARE ::std::hash_compare PARENT_SCOPE) - return() - endif (HAS_STD_HASH_COMPARE) - - check_cxx_source_compiles(" - #include ${HASH_MAP_H} - int main() { ::stdext::hash_compare cp; return cp(0); } - " HAS_STDEXT_HASH_COMPARE) - if (HAS_STDEXT_HASH_COMPARE) - set(HASH_COMPARE ::stdext::hash_compare PARENT_SCOPE) - return() - endif (HAS_STDEXT_HASH_COMPARE) - endif (MSVC) - set(HASH_COMPARE PARENT_SCOPE) -endfunction() - -find_hash_map() -find_hash_compare() diff --git a/cmake/pbconfig.h.in b/cmake/pbconfig.h.in deleted file mode 100644 index fdc59686298f8..0000000000000 --- a/cmake/pbconfig.h.in +++ /dev/null @@ -1,9 +0,0 @@ -#define GOOGLE_PROTOBUF_HAVE_HASH_MAP ${HAVE_HASH_MAP} -#define GOOGLE_PROTOBUF_HAVE_HASH_SET ${HAVE_HASH_MAP} - -#define GOOGLE_PROTOBUF_HASH_NAMESPACE ${HASH_NAMESPACE} -#define GOOGLE_PROTOBUF_HASH_MAP_H ${HASH_MAP_H} -#define GOOGLE_PROTOBUF_HASH_MAP_CLASS ${HASH_MAP_CLASS} -#define GOOGLE_PROTOBUF_HASH_SET_H ${HASH_SET_H} -#define GOOGLE_PROTOBUF_HASH_SET_CLASS ${HASH_SET_CLASS} -#define GOOGLE_PROTOBUF_HASH_COMPARE ${HASH_COMPARE} diff --git a/configure.ac b/configure.ac index 8018cc758ed70..b36fa0c0000ef 100644 --- a/configure.ac +++ b/configure.ac @@ -17,6 +17,9 @@ AC_INIT([Protocol Buffers],[3.0.0-alpha-4-pre],[protobuf@googlegroups.com],[prot AM_MAINTAINER_MODE([enable]) AC_CONFIG_SRCDIR(src/google/protobuf/message.cc) +# The config file is generated but not used by the source code, since we only +# need very few of them, e.g. HAVE_PTHREAD and HAVE_ZLIB. Those macros are +# passed down in CXXFLAGS manually in src/Makefile.am AC_CONFIG_HEADERS([config.h]) AC_CONFIG_MACRO_DIR([m4]) @@ -146,6 +149,9 @@ AS_IF([test "$with_protoc" != "no"], [ AM_CONDITIONAL([USE_EXTERNAL_PROTOC], [test "$with_protoc" != "no"]) ACX_PTHREAD +AM_CONDITIONAL([HAVE_PTHREAD], [test "x$acx_pthread_ok" = "xyes"]) + +# We still keep this for improving pbconfig.h for unsupported platforms. AC_CXX_STL_HASH case "$target_os" in diff --git a/src/Makefile.am b/src/Makefile.am index ac6ec6f4d24ad..67cd4c5700705 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,17 +4,25 @@ if HAVE_ZLIB GZCHECKPROGRAMS = zcgzip zcgunzip GZHEADERS = google/protobuf/io/gzip_stream.h GZTESTS = google/protobuf/io/gzip_stream_unittest.sh +ZLIB_DEF = -DHAVE_ZLIB=1 else GZCHECKPROGRAMS = GZHEADERS = GZTESTS = +ZLIB_DEF = +endif + +if HAVE_PTHREAD +PTHREAD_DEF = -DHAVE_PTHREAD=1 +else +PTHREAD_DEF = endif if GCC # These are good warnings to turn on by default -NO_OPT_CXXFLAGS = $(PTHREAD_CFLAGS) -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare +NO_OPT_CXXFLAGS = $(PTHREAD_CFLAGS) $(PTHREAD_DEF) $(ZLIB_DEF) -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare else -NO_OPT_CXXFLAGS = $(PTHREAD_CFLAGS) +NO_OPT_CXXFLAGS = $(PTHREAD_CFLAGS) $(PTHREAD_DEF) $(ZLIB_DEF) endif AM_CXXFLAGS = $(NO_OPT_CXXFLAGS) $(PROTOBUF_OPT_FLAG) diff --git a/src/google/protobuf/io/gzip_stream.cc b/src/google/protobuf/io/gzip_stream.cc index c9f4ca7fab686..dd7c036e30e2c 100644 --- a/src/google/protobuf/io/gzip_stream.cc +++ b/src/google/protobuf/io/gzip_stream.cc @@ -33,8 +33,6 @@ // This file contains the implementation of classes GzipInputStream and // GzipOutputStream. -#include "config.h" - #if HAVE_ZLIB #include diff --git a/src/google/protobuf/io/zero_copy_stream_unittest.cc b/src/google/protobuf/io/zero_copy_stream_unittest.cc index dd3d1285097ff..f2e5b629dfcf3 100644 --- a/src/google/protobuf/io/zero_copy_stream_unittest.cc +++ b/src/google/protobuf/io/zero_copy_stream_unittest.cc @@ -46,8 +46,6 @@ // "parametized tests" so that one set of tests can be used on all the // implementations. -#include "config.h" - #ifdef _MSC_VER #include #else diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc index 54e00ccb649f3..22cc5aa127c2a 100644 --- a/src/google/protobuf/stubs/common.cc +++ b/src/google/protobuf/stubs/common.cc @@ -36,8 +36,6 @@ #include #include -#include "config.h" - #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN // We only need minimal includes #include diff --git a/src/google/protobuf/stubs/common_unittest.cc b/src/google/protobuf/stubs/common_unittest.cc index 5f458e980012c..f9e2cfd4370ad 100644 --- a/src/google/protobuf/stubs/common_unittest.cc +++ b/src/google/protobuf/stubs/common_unittest.cc @@ -39,8 +39,6 @@ #include #include -#include "config.h" - namespace google { namespace protobuf { namespace { diff --git a/src/google/protobuf/testing/zcgunzip.cc b/src/google/protobuf/testing/zcgunzip.cc index daf74ff1f575e..c9d085c8fc2a0 100644 --- a/src/google/protobuf/testing/zcgunzip.cc +++ b/src/google/protobuf/testing/zcgunzip.cc @@ -38,8 +38,6 @@ // Reads gzip stream on standard input and writes decompressed data to standard // output. -#include "config.h" - #include #include #include diff --git a/src/google/protobuf/testing/zcgzip.cc b/src/google/protobuf/testing/zcgzip.cc index a41019996cdee..e910f321fe282 100644 --- a/src/google/protobuf/testing/zcgzip.cc +++ b/src/google/protobuf/testing/zcgzip.cc @@ -38,8 +38,6 @@ // Reads data on standard input and writes compressed gzip stream to standard // output. -#include "config.h" - #include #include #include