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

Add cmake support. #457

Merged
merged 3 commits into from Jun 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 72 additions & 0 deletions cmake/CMakeLists.txt
@@ -0,0 +1,72 @@
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)
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)
if (BUILD_TESTING)
include(protoc.cmake)
endif (BUILD_TESTING)
127 changes: 127 additions & 0 deletions cmake/README.md
@@ -0,0 +1,127 @@
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
========================

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

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" ..

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.
7. Run extract_includes.bat to copy all the public headers into a separate
"include" directory (under the top-level package directory).
8. Copy the contents of the include directory to wherever you want to put
headers.
9. Copy protoc.exe wherever you put build tools (probably somewhere in your
PATH).
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
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.

4 changes: 4 additions & 0 deletions cmake/config.h.in
@@ -0,0 +1,4 @@
#define GOOGLE_PROTOBUF_CMAKE_BUILD

#define HAVE_PTHREAD ${HAVE_PTHREAD}
#define HAVE_ZLIB ${HAVE_ZLIB}
119 changes: 119 additions & 0 deletions 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. <unordered_map> ::std::unordered_map
# 2. <tr1/unordered_map> ::std::tr1::unordered_map
# 3. <hash_map> ::hash_map
# 4. <hash_map> ::stdext::hash_map
# 5. <ext/hash_map> ::std::hash_map
# 6. <ext/hash_map> ::__gnu_cxx::hash_map
check_cxx_source_compiles("
#include <unordered_map>
int main() { ::std::unordered_map<int, int> v; return v[0]; }
" HAS_STD_UNORDERED_MAP)
if (HAS_STD_UNORDERED_MAP)
set(HASH_NAMESPACE ::std PARENT_SCOPE)
set(HASH_MAP_H <unordered_map> PARENT_SCOPE)
set(HASH_MAP_CLASS unordered_map PARENT_SCOPE)
set(HASH_SET_H <unordered_set> PARENT_SCOPE)
set(HASH_SET_CLASS unordered_set PARENT_SCOPE)
return()
endif (HAS_STD_UNORDERED_MAP)

check_cxx_source_compiles("
#include <tr1/unordered_map>
int main() { ::std::tr1::unordered_map<int, int> 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 <tr1/unordered_map> PARENT_SCOPE)
set(HASH_MAP_CLASS unordered_map PARENT_SCOPE)
set(HASH_SET_H <tr1/unordered_set> PARENT_SCOPE)
set(HASH_SET_CLASS unordered_set PARENT_SCOPE)
return()
endif (HAS_STD_TR1_UNORDERED_MAP)

check_cxx_source_compiles("
#include <hash_map>
int main() { ::hash_map<int, int> v; return v[0]; }
" HAS_HASH_MAP)
if (HAS_HASH_MAP)
set(HASH_NAMESPACE :: PARENT_SCOPE)
set(HASH_MAP_H <hash_map> PARENT_SCOPE)
set(HASH_MAP_CLASS hash_map PARENT_SCOPE)
set(HASH_SET_H <hash_set> PARENT_SCOPE)
set(HASH_SET_CLASS hash_set PARENT_SCOPE)
return()
endif (HAS_HASH_MAP)

check_cxx_source_compiles("
#include <hash_map>
int main() { ::stdext::hash_map<int, int> v; return v[0]; }
" HAS_STDEXT_HASH_MAP)
if (HAS_STDEXT_HASH_MAP)
set(HASH_NAMESPACE ::stdext PARENT_SCOPE)
set(HASH_MAP_H <hash_map> PARENT_SCOPE)
set(HASH_MAP_CLASS hash_map PARENT_SCOPE)
set(HASH_SET_H <hash_set> PARENT_SCOPE)
set(HASH_SET_CLASS hash_set PARENT_SCOPE)
return()
endif (HAS_STDEXT_HASH_MAP)

check_cxx_source_compiles("
#include <ext/hash_map>
int main() { ::std::hash_map<int, int> v; return v[0]; }
" HAS_STD_HASH_MAP)
if (HAS_STD_HASH_MAP)
set(HASH_NAMESPACE ::std PARENT_SCOPE)
set(HASH_MAP_H <ext/hash_map> PARENT_SCOPE)
set(HASH_MAP_CLASS hash_map PARENT_SCOPE)
set(HASH_SET_H <ext/hash_set> PARENT_SCOPE)
set(HASH_SET_CLASS hash_set PARENT_SCOPE)
return()
endif (HAS_STD_HASH_MAP)

check_cxx_source_compiles("
#include <ext/hash_map>
int main() { ::__gnu_cxx::hash_map<int, int> 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 <ext/hash_map> PARENT_SCOPE)
set(HASH_MAP_CLASS hash_map PARENT_SCOPE)
set(HASH_SET_H <ext/hash_set> 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<int> 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<int> 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()
23 changes: 23 additions & 0 deletions 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)
42 changes: 42 additions & 0 deletions 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)