From 4333edb34048d29a410cc247ff7ffbad8145132f Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Sun, 31 May 2015 02:28:34 -0700 Subject: [PATCH 1/3] Add cmake support. --- cmake/CMakeLists.txt | 70 ++++++++++ cmake/README.md | 118 +++++++++++++++++ cmake/config.h.in | 4 + cmake/find_hash_map.cmake | 119 +++++++++++++++++ cmake/libprotobuf-lite.cmake | 23 ++++ cmake/libprotobuf.cmake | 42 ++++++ cmake/libprotoc.cmake | 95 ++++++++++++++ cmake/pbconfig.h.in | 9 ++ cmake/protoc.cmake | 6 + cmake/tests.cmake | 148 ++++++++++++++++++++++ cmake/update_file_lists.sh | 114 +++++++++++++++++ src/google/protobuf/testing/googletest.cc | 19 ++- 12 files changed, 756 insertions(+), 11 deletions(-) create mode 100644 cmake/CMakeLists.txt create mode 100644 cmake/README.md create mode 100644 cmake/config.h.in create mode 100644 cmake/find_hash_map.cmake create mode 100644 cmake/libprotobuf-lite.cmake create mode 100644 cmake/libprotobuf.cmake create mode 100644 cmake/libprotoc.cmake create mode 100644 cmake/pbconfig.h.in create mode 100644 cmake/protoc.cmake create mode 100644 cmake/tests.cmake create mode 100644 cmake/update_file_lists.sh diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt new file mode 100644 index 000000000000..146ea7804a1d --- /dev/null +++ b/cmake/CMakeLists.txt @@ -0,0 +1,70 @@ +cmake_minimum_required(VERSION 2.8) + +project(protobuf C CXX) + +option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) +if (MSVC) + option(ZLIB "Build with zlib support" OFF) +endif (MSVC) + +find_package(Threads REQUIRED) +if (CMAKE_USE_PTHREADS_INIT) + set(HAVE_PTHREAD 1) +else (CMAKE_USE_PTHREADS_INIT) + set(HAVE_PTHREAD 0) +endif (CMAKE_USE_PTHREADS_INIT) + +if (MSVC) + if (ZLIB) + set(HAVE_ZLIB 1) + find_path(ZLIB_INCLUDE_DIRECTORIES zlib.h ${protobuf_SOURCE_DIR}) + find_library(ZLIB_LIBRARIES zdll ${protobuf_SOURCE_DIR}) + else (ZLIB) + set(HAVE_ZLIB 0) + endif (ZLIB) +else (MSVC) + find_package(ZLIB) + if (ZLIB_FOUND) + set(HAVE_ZLIB 1) + else (ZLIB_FOUND) + set(HAVE_ZLIB 0) + # Explicitly set these to empty (override NOT_FOUND) so cmake doesn't + # complain when we use them later. + set(ZLIB_INCLUDE_DIRECTORIES) + set(ZLIB_LIBRARIES) + endif (ZLIB_FOUND) +endif (MSVC) + +if (MSVC) + if (BUILD_SHARED_LIBS) + add_definitions(-DPROTOBUF_USE_DLLS) + endif (BUILD_SHARED_LIBS) + 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) + +get_filename_component(protobuf_source_dir ${protobuf_SOURCE_DIR} PATH) + +include_directories( + ${ZLIB_INCLUDE_DIRECTORIES} + ${protobuf_BINARY_DIR} + ${protobuf_source_dir}/src) + +if (MSVC) + # Add the "lib" prefix for generated .lib outputs. + set(LIB_PREFIX lib) +else (MSVC) + # When building with "make", "lib" prefix will be added automatically by + # the build tool. + set(LIB_PREFIX) +endif (MSVC) + +include(libprotobuf-lite.cmake) +include(libprotobuf.cmake) +include(libprotoc.cmake) +include(protoc.cmake) +include(tests.cmake) diff --git a/cmake/README.md b/cmake/README.md new file mode 100644 index 000000000000..2f1fef3f5bc0 --- /dev/null +++ b/cmake/README.md @@ -0,0 +1,118 @@ +This directory contains cmake files that can be used to generate MSVC project +files in order to build protobuf on windows. You need to have cmake installed +on your computer before proceeding. + +Compiling and Installing +======================== + +0) Check whether a gtest directory exists in the upper level directory. If + you checkout the code from github via "git clone", this gtest directory + won't exist and you won't be able to build the tests described below. To + avoid this problem consider downloading one of the release tar balls which + contains gtest already and copying the gest directory from there to your + protobuf directory: + https://github.com/google/protobuf/releases +1) Use cmake to generate MSVC project files. Running the following commands + in a command shell will generate project files for Visual Studio 2008 in + a sub-directory named "build". + > cd path/to/protobuf/cmake + > mkdir build + > cd build + > cmake -G "Visual Studio 9 2008" .. +2) Open the generated protobuf.sln file in Microsoft Visual Studio. +3) Choose "Debug" or "Release" configuration as desired. +4) From the Build menu, choose "Build Solution". Wait for compiling to finish. +5) From a command shell, run tests.exe and lite-test.exe and check that all + tests pass. Make sure you have changed the working directory to the output + directory because tests.exe will try to find and run test_plugin.exe + in the working directory. +5) Run extract_includes.bat to copy all the public headers into a separate + "include" directory (under the top-level package directory). +6) Copy the contents of the include directory to wherever you want to put + headers. +7) Copy protoc.exe wherever you put build tools (probably somewhere in your + PATH). +8) Copy libprotobuf.lib, libprotobuf-lite.lib, and libprotoc.lib wherever you + put libraries. + +* To avoid conflicts between the MSVC debug and release runtime libraries, when + compiling a debug build of your application, you may need to link against a + debug build of libprotobuf.lib. Similarly, release builds should link against + release libs. + +DLLs vs. static linking +======================= + +Static linking is now the default for the Protocol Buffer libraries. Due to +issues with Win32's use of a separate heap for each DLL, as well as binary +compatibility issues between different versions of MSVC's STL library, it is +recommended that you use static linkage only. However, it is possible to +build libprotobuf and libprotoc as DLLs if you really want. To do this, +do the following: + + 1) Add an additional flag "-DBUILD_SHARED_LIBS=ON" when invoking cmake: + > cmake -G "Visual Studio 9 2008" -DBUILD_SHARED_LIBS=ON .. + 2) Follow the same steps as described in the above section. + 3) When compiling your project, make sure to #define PROTOBUF_USE_DLLS. + +When distributing your software to end users, we strongly recommend that you +do NOT install libprotobuf.dll or libprotoc.dll to any shared location. +Instead, keep these libraries next to your binaries, in your application's +own install directory. C++ makes it very difficult to maintain binary +compatibility between releases, so it is likely that future versions of these +libraries will *not* be usable as drop-in replacements. + +If your project is itself a DLL intended for use by third-party software, we +recommend that you do NOT expose protocol buffer objects in your library's +public interface, and that you statically link protocol buffers into your +library. + +ZLib support +============ + +If you want to include GzipInputStream and GzipOutputStream +(google/protobuf/io/gzip_stream.h) in libprotobuf, you will need to do a few +additional steps: + +1) Obtain a copy of the zlib library. The pre-compiled DLL at zlib.net works. +2) Make sure zlib's two headers are in your include path and that the .lib file + is in your library path. You could place all three files directly into this + cmake directory to compile libprotobuf, but they need to be visible to + your own project as well, so you should probably just put them into the + VC shared icnlude and library directories. +3) Add flag "-DZLIB=ON" when invoking cmake: + > cmake -G "Visual Studio 9 2008" -DZLIB=ON .. + If it reports NOTFOUND for zlib_include or zlib_lib, you might haven't put + the headers or the .lib file in the right directory. +4) Open the generated protobuf.sln file and build as usual. + +Notes on Compiler Warnings +========================== + +The following warnings have been disabled while building the protobuf libraries +and compiler. You may have to disable some of them in your own project as +well, or live with them. + +C4018 - 'expression' : signed/unsigned mismatch +C4146 - unary minus operator applied to unsigned type, result still unsigned +C4244 - Conversion from 'type1' to 'type2', possible loss of data. +C4251 - 'identifier' : class 'type' needs to have dll-interface to be used by + clients of class 'type2' +C4267 - Conversion from 'size_t' to 'type', possible loss of data. +C4305 - 'identifier' : truncation from 'type1' to 'type2' +C4355 - 'this' : used in base member initializer list +C4800 - 'type' : forcing value to bool 'true' or 'false' (performance warning) +C4996 - 'function': was declared deprecated + +C4251 is of particular note, if you are compiling the Protocol Buffer library +as a DLL (see previous section). The protocol buffer library uses templates in +its public interfaces. MSVC does not provide any reasonable way to export +template classes from a DLL. However, in practice, it appears that exporting +templates is not necessary anyway. Since the complete definition of any +template is available in the header files, anyone importing the DLL will just +end up compiling instances of the templates into their own binary. The +Protocol Buffer implementation does not rely on static template members being +unique, so there should be no problem with this, but MSVC prints warning +nevertheless. So, we disable it. Unfortunately, this warning will also be +produced when compiling code which merely uses protocol buffers, meaning you +may have to disable it in your code too. diff --git a/cmake/config.h.in b/cmake/config.h.in new file mode 100644 index 000000000000..bca1cbba6618 --- /dev/null +++ b/cmake/config.h.in @@ -0,0 +1,4 @@ +#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 new file mode 100644 index 000000000000..22796fb880d3 --- /dev/null +++ b/cmake/find_hash_map.cmake @@ -0,0 +1,119 @@ +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/libprotobuf-lite.cmake b/cmake/libprotobuf-lite.cmake new file mode 100644 index 000000000000..32c2d0260e42 --- /dev/null +++ b/cmake/libprotobuf-lite.cmake @@ -0,0 +1,23 @@ +set(libprotobuf_lite_files + ${protobuf_source_dir}/src/google/protobuf/arena.cc + ${protobuf_source_dir}/src/google/protobuf/arenastring.cc + ${protobuf_source_dir}/src/google/protobuf/extension_set.cc + ${protobuf_source_dir}/src/google/protobuf/generated_message_util.cc + ${protobuf_source_dir}/src/google/protobuf/io/coded_stream.cc + ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream.cc + ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl_lite.cc + ${protobuf_source_dir}/src/google/protobuf/message_lite.cc + ${protobuf_source_dir}/src/google/protobuf/repeated_field.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/atomicops_internals_x86_gcc.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/atomicops_internals_x86_msvc.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/common.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/once.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/stringprintf.cc + ${protobuf_source_dir}/src/google/protobuf/wire_format_lite.cc +) + +add_library(libprotobuf-lite ${libprotobuf_lite_files}) +target_link_libraries(libprotobuf-lite ${CMAKE_THREAD_LIBS_INIT}) +set_target_properties(libprotobuf-lite PROPERTIES + COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS + OUTPUT_NAME ${LIB_PREFIX}protobuf-lite) diff --git a/cmake/libprotobuf.cmake b/cmake/libprotobuf.cmake new file mode 100644 index 000000000000..b1f2dc3e60bd --- /dev/null +++ b/cmake/libprotobuf.cmake @@ -0,0 +1,42 @@ +set(libprotobuf_files + ${protobuf_source_dir}/src/google/protobuf/any.cc + ${protobuf_source_dir}/src/google/protobuf/any.pb.cc + ${protobuf_source_dir}/src/google/protobuf/api.pb.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/importer.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/parser.cc + ${protobuf_source_dir}/src/google/protobuf/descriptor.cc + ${protobuf_source_dir}/src/google/protobuf/descriptor.pb.cc + ${protobuf_source_dir}/src/google/protobuf/descriptor_database.cc + ${protobuf_source_dir}/src/google/protobuf/duration.pb.cc + ${protobuf_source_dir}/src/google/protobuf/dynamic_message.cc + ${protobuf_source_dir}/src/google/protobuf/empty.pb.cc + ${protobuf_source_dir}/src/google/protobuf/extension_set_heavy.cc + ${protobuf_source_dir}/src/google/protobuf/field_mask.pb.cc + ${protobuf_source_dir}/src/google/protobuf/generated_message_reflection.cc + ${protobuf_source_dir}/src/google/protobuf/io/gzip_stream.cc + ${protobuf_source_dir}/src/google/protobuf/io/printer.cc + ${protobuf_source_dir}/src/google/protobuf/io/strtod.cc + ${protobuf_source_dir}/src/google/protobuf/io/tokenizer.cc + ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_impl.cc + ${protobuf_source_dir}/src/google/protobuf/map_field.cc + ${protobuf_source_dir}/src/google/protobuf/message.cc + ${protobuf_source_dir}/src/google/protobuf/reflection_ops.cc + ${protobuf_source_dir}/src/google/protobuf/service.cc + ${protobuf_source_dir}/src/google/protobuf/source_context.pb.cc + ${protobuf_source_dir}/src/google/protobuf/struct.pb.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/structurally_valid.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/strutil.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/substitute.cc + ${protobuf_source_dir}/src/google/protobuf/text_format.cc + ${protobuf_source_dir}/src/google/protobuf/timestamp.pb.cc + ${protobuf_source_dir}/src/google/protobuf/type.pb.cc + ${protobuf_source_dir}/src/google/protobuf/unknown_field_set.cc + ${protobuf_source_dir}/src/google/protobuf/wire_format.cc + ${protobuf_source_dir}/src/google/protobuf/wrappers.pb.cc +) + +add_library(libprotobuf ${libprotobuf_lite_files} ${libprotobuf_files}) +target_link_libraries(libprotobuf ${CMAKE_THREAD_LIBS_INIT} ${ZLIB_LIBRARIES}) +set_target_properties(libprotobuf PROPERTIES + COMPILE_DEFINITIONS LIBPROTOBUF_EXPORTS + OUTPUT_NAME ${LIB_PREFIX}protobuf) diff --git a/cmake/libprotoc.cmake b/cmake/libprotoc.cmake new file mode 100644 index 000000000000..8caa9e9e83ba --- /dev/null +++ b/cmake/libprotoc.cmake @@ -0,0 +1,95 @@ +set(libprotoc_files + ${protobuf_source_dir}/src/google/protobuf/compiler/code_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/command_line_interface.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_enum.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_enum_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_extension.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_file.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_helpers.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_map_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_message.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_message_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_primitive_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_service.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_string_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_enum.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_enum_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_extension.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_field_base.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_helpers.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_message.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_message_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_primitive_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_writer.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_context.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_doc_comment.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_enum.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_enum_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_enum_field_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_extension.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_file.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_generator_factory.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_helpers.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_lazy_message_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_lazy_message_field_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_map_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_map_field_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_message.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_message_builder.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_message_builder_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_message_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_message_field_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_message_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_name_resolver.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_primitive_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_primitive_field_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_service.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_shared_code_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_string_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_string_field_lite.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_enum.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_enum_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_extension.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_file.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_helpers.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_map_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_message.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_message_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_enum.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_enum_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_extension.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_file.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_map_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_message.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_message_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_oneof.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_primitive_field.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/plugin.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/plugin.pb.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/python/python_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/ruby/ruby_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/subprocess.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/zip_writer.cc +) + +add_library(libprotoc ${libprotoc_files}) +target_link_libraries(libprotoc libprotobuf) +set_target_properties(libprotoc PROPERTIES + COMPILE_DEFINITIONS LIBPROTOC_EXPORTS + OUTPUT_NAME ${LIB_PREFIX}protoc) diff --git a/cmake/pbconfig.h.in b/cmake/pbconfig.h.in new file mode 100644 index 000000000000..fdc59686298f --- /dev/null +++ b/cmake/pbconfig.h.in @@ -0,0 +1,9 @@ +#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/cmake/protoc.cmake b/cmake/protoc.cmake new file mode 100644 index 000000000000..4f07c389c976 --- /dev/null +++ b/cmake/protoc.cmake @@ -0,0 +1,6 @@ +set(protoc_files + ${protobuf_source_dir}/src/google/protobuf/compiler/main.cc +) + +add_executable(protoc ${protoc_files}) +target_link_libraries(protoc libprotobuf libprotoc) diff --git a/cmake/tests.cmake b/cmake/tests.cmake new file mode 100644 index 000000000000..24891521b84c --- /dev/null +++ b/cmake/tests.cmake @@ -0,0 +1,148 @@ +include_directories( + ${protobuf_source_dir}/gtest/include + ${protobuf_source_dir}/gtest) + +add_library(gtest STATIC ${protobuf_source_dir}/gtest/src/gtest-all.cc) +add_library(gtest_main STATIC ${protobuf_source_dir}/gtest/src/gtest_main.cc) +target_link_libraries(gtest_main gtest) + +set(lite_test_protos + google/protobuf/map_lite_unittest.proto + google/protobuf/unittest_import_lite.proto + google/protobuf/unittest_import_public_lite.proto + google/protobuf/unittest_lite.proto +) + +set(tests_protos + google/protobuf/any_test.proto + google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto + google/protobuf/compiler/cpp/cpp_test_large_enum_value.proto + google/protobuf/map_proto2_unittest.proto + google/protobuf/map_unittest.proto + google/protobuf/unittest.proto + google/protobuf/unittest_arena.proto + google/protobuf/unittest_custom_options.proto + google/protobuf/unittest_drop_unknown_fields.proto + google/protobuf/unittest_embed_optimize_for.proto + google/protobuf/unittest_empty.proto + google/protobuf/unittest_import.proto + google/protobuf/unittest_import_public.proto + google/protobuf/unittest_lite_imports_nonlite.proto + google/protobuf/unittest_mset.proto + google/protobuf/unittest_no_arena.proto + google/protobuf/unittest_no_arena_import.proto + google/protobuf/unittest_no_field_presence.proto + google/protobuf/unittest_no_generic_services.proto + google/protobuf/unittest_optimize_for.proto + google/protobuf/unittest_preserve_unknown_enum.proto + google/protobuf/unittest_preserve_unknown_enum2.proto + google/protobuf/unittest_proto3_arena.proto + google/protobuf/unittest_well_known_types.proto +) + +macro(compile_proto_file filename) + get_filename_component(dirname ${filename} PATH) + get_filename_component(basename ${filename} NAME_WE) + add_custom_command( + OUTPUT ${protobuf_source_dir}/src/${dirname}/${basename}.pb.cc + COMMAND protoc ${protobuf_source_dir}/src/${dirname}/${basename}.proto + --proto_path=${protobuf_source_dir}/src + --cpp_out=${protobuf_source_dir}/src + DEPENDS protoc + ) +endmacro(compile_proto_file) + +set(lite_test_proto_files) +foreach(proto_file ${lite_test_protos}) + compile_proto_file(${proto_file}) + string(REPLACE .proto .pb.cc pb_file ${proto_file}) + set(lite_test_proto_files ${lite_test_proto_files} + ${protobuf_source_dir}/src/${pb_file}) +endforeach(proto_file) + +set(tests_proto_files) +foreach(proto_file ${tests_protos}) + compile_proto_file(${proto_file}) + string(REPLACE .proto .pb.cc pb_file ${proto_file}) + set(tests_proto_files ${tests_proto_files} + ${protobuf_source_dir}/src/${pb_file}) +endforeach(proto_file) + +set(common_test_files + ${protobuf_source_dir}/src/google/protobuf/arena_test_util.cc + ${protobuf_source_dir}/src/google/protobuf/map_test_util.cc + ${protobuf_source_dir}/src/google/protobuf/test_util.cc + ${protobuf_source_dir}/src/google/protobuf/testing/file.cc + ${protobuf_source_dir}/src/google/protobuf/testing/googletest.cc +) + +set(tests_files + ${protobuf_source_dir}/src/google/protobuf/any_test.cc + ${protobuf_source_dir}/src/google/protobuf/arena_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/arenastring_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/command_line_interface_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_bootstrap_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_plugin_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/cpp/cpp_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/csharp/csharp_generator_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/importer_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_doc_comment_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/java/java_plugin_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/objectivec/objectivec_helpers_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/parser_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/python/python_plugin_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/compiler/ruby/ruby_generator_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/descriptor_database_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/descriptor_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/drop_unknown_fields_test.cc + ${protobuf_source_dir}/src/google/protobuf/dynamic_message_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/extension_set_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/generated_message_reflection_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/io/coded_stream_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/io/printer_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/io/tokenizer_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/io/zero_copy_stream_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/map_field_test.cc + ${protobuf_source_dir}/src/google/protobuf/map_test.cc + ${protobuf_source_dir}/src/google/protobuf/message_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/no_field_presence_test.cc + ${protobuf_source_dir}/src/google/protobuf/preserve_unknown_enum_test.cc + ${protobuf_source_dir}/src/google/protobuf/proto3_arena_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/reflection_ops_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/repeated_field_reflection_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/repeated_field_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/common_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/once_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/stringprintf_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/structurally_valid_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/strutil_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/template_util_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/stubs/type_traits_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/text_format_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/unknown_field_set_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/well_known_types_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/wire_format_unittest.cc +) + +add_executable(tests ${tests_files} ${common_test_files} ${tests_proto_files} ${lite_test_proto_files}) +target_link_libraries(tests libprotoc libprotobuf gtest_main) + +set(test_plugin_files + ${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc + ${protobuf_source_dir}/src/google/protobuf/testing/file.cc + ${protobuf_source_dir}/src/google/protobuf/testing/file.h + ${protobuf_source_dir}/src/google/protobuf/compiler/test_plugin.cc +) + +add_executable(test_plugin ${test_plugin_files}) +target_link_libraries(test_plugin libprotoc libprotobuf gtest) + +set(lite_test_files + ${protobuf_source_dir}/src/google/protobuf/arena_test_util.cc + ${protobuf_source_dir}/src/google/protobuf/lite_unittest.cc + ${protobuf_source_dir}/src/google/protobuf/map_lite_test_util.cc + ${protobuf_source_dir}/src/google/protobuf/test_util_lite.cc +) +add_executable(lite-test ${lite_test_files} ${lite_test_proto_files}) +target_link_libraries(lite-test libprotobuf-lite) diff --git a/cmake/update_file_lists.sh b/cmake/update_file_lists.sh new file mode 100644 index 000000000000..d64eb4aeb4b8 --- /dev/null +++ b/cmake/update_file_lists.sh @@ -0,0 +1,114 @@ +#!/bin/sh + +# This script copies source file lists from src/Makefile.am to cmake files. + +get_variable_value() { + FILENAME=$1 + VARNAME=$2 + awk " + BEGIN { start = 0; } + /^$VARNAME =/ { start = 1; } + { if (start) { print \$0; } } + /\\\\\$/ { next; } + { start = 0; } + " $FILENAME \ + | sed "s/^$VARNAME =//" \ + | sed "s/[ \\]//g" \ + | grep -v "^\\$" \ + | grep -v "^$" \ + | LC_ALL=C sort | uniq +} + +get_source_files() { + get_variable_value $@ | grep "cc$" +} + +get_proto_files() { + get_variable_value $@ | grep "pb.cc$" | sed "s/pb.cc/proto/" +} + +set_variable_value() { + FILENAME=$1 + VARNAME=$2 + PREFIX=$3 + shift + shift + shift + awk -v values="$*" -v prefix="$PREFIX" " + BEGIN { start = 0; } + /^set\\($VARNAME/ { + start = 1; + print \$0; + split(values, vlist, \" \"); + for (i = 1; i <= length(vlist); ++i) { + printf(\" %s%s\\n\", prefix, vlist[i]); + } + next; + } + start && /^\\)/ { + start = 0; + } + !start { + print \$0; + } + " $FILENAME > /tmp/$$ + cp /tmp/$$ $FILENAME +} + +sort_files() { + for FILE in $@; do + echo $FILE + done | sort | uniq +} + +MAKEFILE=../src/Makefile.am +CMAKE_DIR=. +EXTRACT_INCLUDES_BAT=../vsprojects/extract_includes.bat + +[ -f "$MAKEFILE" ] || { + echo "Cannot find: $MAKEFILE" + exit 1 +} + +[ -d "$CMAKE_DIR" ] || { + echo "Cannot find: $CMAKE_DIR" + exit 1 +} + +[ -f "$EXTRACT_INCLUDES_BAT" ] || { + echo "Cannot find: $EXTRACT_INCLUDES_BAT" + exit 1 +} + +# Extract file lists from src/Makefile.am +GZHEADERS=$(get_variable_value $MAKEFILE GZHEADERS) +HEADERS=$(get_variable_value $MAKEFILE nobase_include_HEADERS) +PUBLIC_HEADERS=$(sort_files $GZHEADERS $HEADERS) +LIBPROTOBUF_LITE_SOURCES=$(get_source_files $MAKEFILE libprotobuf_lite_la_SOURCES) +LIBPROTOBUF_SOURCES=$(get_source_files $MAKEFILE libprotobuf_la_SOURCES) +LIBPROTOC_SOURCES=$(get_source_files $MAKEFILE libprotoc_la_SOURCES) +LITE_PROTOS=$(get_proto_files $MAKEFILE protoc_lite_outputs) +PROTOS=$(get_proto_files $MAKEFILE protoc_outputs) +COMMON_TEST_SOURCES=$(get_source_files $MAKEFILE COMMON_TEST_SOURCES) +TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_test_SOURCES) +LITE_TEST_SOURCES=$(get_source_files $MAKEFILE protobuf_lite_test_SOURCES) + +# Replace file lists in cmake files. +COMMON_PREFIX="\${protobuf_source_dir}/src/" +set_variable_value $CMAKE_DIR/libprotobuf-lite.cmake libprotobuf_lite_files $COMMON_PREFIX $LIBPROTOBUF_LITE_SOURCES +set_variable_value $CMAKE_DIR/libprotobuf.cmake libprotobuf_files $COMMON_PREFIX $LIBPROTOBUF_SOURCES +set_variable_value $CMAKE_DIR/libprotoc.cmake libprotoc_files $COMMON_PREFIX $LIBPROTOC_SOURCES +set_variable_value $CMAKE_DIR/tests.cmake lite_test_protos "" $LITE_PROTOS +set_variable_value $CMAKE_DIR/tests.cmake tests_protos "" $PROTOS +set_variable_value $CMAKE_DIR/tests.cmake common_test_files $COMMON_PREFIX $COMMON_TEST_SOURCES +set_variable_value $CMAKE_DIR/tests.cmake tests_files $COMMON_PREFIX $TEST_SOURCES +set_variable_value $CMAKE_DIR/tests.cmake lite_test_files $COMMON_PREFIX $LITE_TEST_SOURCES + +# Generate extract_includes.bat +for HEADER in $HEADERS; do + echo $(dirname $HEADER) | sed "s/\\//\\\\/g" +done | sort | uniq | sed "s/^/mkdir include\\\\/" > $EXTRACT_INCLUDES_BAT +for HEADER in $HEADERS; do + WINPATH=$(echo $HEADER | sed 's;/;\\\\;g') + echo "copy ..\\src\\$WINPATH include\\$WINPATH" >> $EXTRACT_INCLUDES_BAT +done diff --git a/src/google/protobuf/testing/googletest.cc b/src/google/protobuf/testing/googletest.cc index 643280633dc6..b8bd790b6683 100644 --- a/src/google/protobuf/testing/googletest.cc +++ b/src/google/protobuf/testing/googletest.cc @@ -66,7 +66,14 @@ namespace protobuf { string TestSourceDir() { #ifndef GOOGLE_THIRD_PARTY_PROTOBUF -#ifdef _MSC_VER +#ifndef _MSC_VER + // automake sets the "srcdir" environment variable. + char* result = getenv("srcdir"); + if (result != NULL) { + return result; + } +#endif // _MSC_VER + // Look for the "src" directory. string prefix = "."; @@ -79,16 +86,6 @@ string TestSourceDir() { prefix += "/.."; } return prefix + "/src"; -#else - // automake sets the "srcdir" environment variable. - char* result = getenv("srcdir"); - if (result == NULL) { - // Otherwise, the test must be run from the source directory. - return "."; - } else { - return result; - } -#endif #else return "third_party/protobuf/src"; #endif // GOOGLE_THIRD_PARTY_PROTOBUF From 34448fca45b64e084db54ad3cc623b8bfe8b3863 Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Thu, 4 Jun 2015 01:27:19 -0700 Subject: [PATCH 2/3] Add an cmake option to exclude tests. --- cmake/CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 146ea7804a1d..02bba4c9cfd1 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.8) project(protobuf C CXX) +option(BUILD_TESTING "Build tests" ON) option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF) if (MSVC) option(ZLIB "Build with zlib support" OFF) @@ -66,5 +67,6 @@ endif (MSVC) include(libprotobuf-lite.cmake) include(libprotobuf.cmake) include(libprotoc.cmake) -include(protoc.cmake) -include(tests.cmake) +if (BUILD_TESTING) + include(protoc.cmake) +endif (BUILD_TESTING) From 2286ab328c0ca04630193b1d3c1aa8eb5fb7aea3 Mon Sep 17 00:00:00 2001 From: Feng Xiao Date: Thu, 4 Jun 2015 11:12:32 -0700 Subject: [PATCH 3/3] Fix README.md formatting. Change-Id: I121cb70bfdc4894c297ab1a36f1db47736c0652b --- cmake/README.md | 77 +++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/cmake/README.md b/cmake/README.md index 2f1fef3f5bc0..02798b631a6e 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -5,37 +5,41 @@ on your computer before proceeding. Compiling and Installing ======================== -0) Check whether a gtest directory exists in the upper level directory. If +1. Check whether a gtest directory exists in the upper level directory. If you checkout the code from github via "git clone", this gtest directory won't exist and you won't be able to build the tests described below. To avoid this problem consider downloading one of the release tar balls which contains gtest already and copying the gest directory from there to your protobuf directory: - https://github.com/google/protobuf/releases -1) Use cmake to generate MSVC project files. Running the following commands + + https://github.com/google/protobuf/releases + +2. Use cmake to generate MSVC project files. Running the following commands in a command shell will generate project files for Visual Studio 2008 in a sub-directory named "build". - > cd path/to/protobuf/cmake - > mkdir build - > cd build - > cmake -G "Visual Studio 9 2008" .. -2) Open the generated protobuf.sln file in Microsoft Visual Studio. -3) Choose "Debug" or "Release" configuration as desired. -4) From the Build menu, choose "Build Solution". Wait for compiling to finish. -5) From a command shell, run tests.exe and lite-test.exe and check that all + + $ cd path/to/protobuf/cmake + $ mkdir build + $ cd build + $ cmake -G "Visual Studio 9 2008" .. + +3. Open the generated protobuf.sln file in Microsoft Visual Studio. +4. Choose "Debug" or "Release" configuration as desired. +5. From the Build menu, choose "Build Solution". Wait for compiling to finish. +6. From a command shell, run tests.exe and lite-test.exe and check that all tests pass. Make sure you have changed the working directory to the output directory because tests.exe will try to find and run test_plugin.exe in the working directory. -5) Run extract_includes.bat to copy all the public headers into a separate +7. Run extract_includes.bat to copy all the public headers into a separate "include" directory (under the top-level package directory). -6) Copy the contents of the include directory to wherever you want to put +8. Copy the contents of the include directory to wherever you want to put headers. -7) Copy protoc.exe wherever you put build tools (probably somewhere in your +9. Copy protoc.exe wherever you put build tools (probably somewhere in your PATH). -8) Copy libprotobuf.lib, libprotobuf-lite.lib, and libprotoc.lib wherever you +10. Copy libprotobuf.lib, libprotobuf-lite.lib, and libprotoc.lib wherever you put libraries. -* To avoid conflicts between the MSVC debug and release runtime libraries, when + To avoid conflicts between the MSVC debug and release runtime libraries, when compiling a debug build of your application, you may need to link against a debug build of libprotobuf.lib. Similarly, release builds should link against release libs. @@ -50,10 +54,12 @@ recommended that you use static linkage only. However, it is possible to build libprotobuf and libprotoc as DLLs if you really want. To do this, do the following: - 1) Add an additional flag "-DBUILD_SHARED_LIBS=ON" when invoking cmake: - > cmake -G "Visual Studio 9 2008" -DBUILD_SHARED_LIBS=ON .. - 2) Follow the same steps as described in the above section. - 3) When compiling your project, make sure to #define PROTOBUF_USE_DLLS. + 1. Add an additional flag "-DBUILD_SHARED_LIBS=ON" when invoking cmake: + + $ cmake -G "Visual Studio 9 2008" -DBUILD_SHARED_LIBS=ON .. + + 2. Follow the same steps as described in the above section. + 3. When compiling your project, make sure to #define PROTOBUF_USE_DLLS. When distributing your software to end users, we strongly recommend that you do NOT install libprotobuf.dll or libprotoc.dll to any shared location. @@ -74,14 +80,16 @@ If you want to include GzipInputStream and GzipOutputStream (google/protobuf/io/gzip_stream.h) in libprotobuf, you will need to do a few additional steps: -1) Obtain a copy of the zlib library. The pre-compiled DLL at zlib.net works. -2) Make sure zlib's two headers are in your include path and that the .lib file +1. Obtain a copy of the zlib library. The pre-compiled DLL at zlib.net works. +2. Make sure zlib's two headers are in your include path and that the .lib file is in your library path. You could place all three files directly into this cmake directory to compile libprotobuf, but they need to be visible to your own project as well, so you should probably just put them into the VC shared icnlude and library directories. -3) Add flag "-DZLIB=ON" when invoking cmake: - > cmake -G "Visual Studio 9 2008" -DZLIB=ON .. +3. Add flag "-DZLIB=ON" when invoking cmake: + + $ cmake -G "Visual Studio 9 2008" -DZLIB=ON .. + If it reports NOTFOUND for zlib_include or zlib_lib, you might haven't put the headers or the .lib file in the right directory. 4) Open the generated protobuf.sln file and build as usual. @@ -93,16 +101,16 @@ The following warnings have been disabled while building the protobuf libraries and compiler. You may have to disable some of them in your own project as well, or live with them. -C4018 - 'expression' : signed/unsigned mismatch -C4146 - unary minus operator applied to unsigned type, result still unsigned -C4244 - Conversion from 'type1' to 'type2', possible loss of data. -C4251 - 'identifier' : class 'type' needs to have dll-interface to be used by - clients of class 'type2' -C4267 - Conversion from 'size_t' to 'type', possible loss of data. -C4305 - 'identifier' : truncation from 'type1' to 'type2' -C4355 - 'this' : used in base member initializer list -C4800 - 'type' : forcing value to bool 'true' or 'false' (performance warning) -C4996 - 'function': was declared deprecated +* C4018 - 'expression' : signed/unsigned mismatch +* C4146 - unary minus operator applied to unsigned type, result still unsigned +* C4244 - Conversion from 'type1' to 'type2', possible loss of data. +* C4251 - 'identifier' : class 'type' needs to have dll-interface to be used by + clients of class 'type2' +* C4267 - Conversion from 'size_t' to 'type', possible loss of data. +* C4305 - 'identifier' : truncation from 'type1' to 'type2' +* C4355 - 'this' : used in base member initializer list +* C4800 - 'type' : forcing value to bool 'true' or 'false' (performance warning) +* C4996 - 'function': was declared deprecated C4251 is of particular note, if you are compiling the Protocol Buffer library as a DLL (see previous section). The protocol buffer library uses templates in @@ -116,3 +124,4 @@ unique, so there should be no problem with this, but MSVC prints warning nevertheless. So, we disable it. Unfortunately, this warning will also be produced when compiling code which merely uses protocol buffers, meaning you may have to disable it in your code too. +