-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
110 lines (86 loc) · 3.61 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
cmake_minimum_required(VERSION 3.8)
project(howto-protobuf-zlib)
# Use C11 and C++11 standards
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
macro(PROCESS_DEPENDENCY NAME REPOSITORY TAG SOURCE_SUBDIR)
set(DEPENDENCY_NAME ${NAME})
set(DEPENDENCY_REPOSITORY ${REPOSITORY})
set(DEPENDENCY_TAG ${TAG})
set(DEPENDENCY_CMAKE_ARGS ${ARGN})
set(DEPENDENCY_DOWNLOAD_PROJECT "${DEPENDENCY_NAME}-download")
set(DEPENDENCY_SOURCE_PROJECT "${DEPENDENCY_NAME}")
find_path(DEPENDENCY_DOWNLOAD_DIR cmake_install.cmake
HINTS ${CMAKE_BINARY_DIR}/${DEPENDENCY_DOWNLOAD_PROJECT}
NO_DEFAULT_PATH
NO_CMAKE_PATH
NO_CMAKE_ENVIRONMENT_PATH
NO_SYSTEM_ENVIRONMENT_PATH
)
set(DEPENDENCY_SOURCE_DIR "${CMAKE_BINARY_DIR}/${DEPENDENCY_SOURCE_PROJECT}")
if(NOT DEPENDENCY_DOWNLOAD_DIR OR NOT EXISTS ${DEPENDENCY_SOURCE_DIR})
# Configure a project to download the dependency
set(DEPENDENCY_DOWNLOAD_DIR "${CMAKE_BINARY_DIR}/${DEPENDENCY_DOWNLOAD_PROJECT}")
configure_file(CMakeLists.txt.dep.in ${DEPENDENCY_DOWNLOAD_DIR}/CMakeLists.txt)
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE ${DEPENDENCY_NAME}_CMAKE_ERROR
WORKING_DIRECTORY ${DEPENDENCY_DOWNLOAD_DIR}
)
if(${DEPENDENCY_NAME}_CMAKE_ERROR)
message(FATAL_ERROR "Could not configure download for ${DEPENDENCY_NAME} dependency: ${${DEPENDENCY_NAME}_CMAKE_ERROR}")
endif()
# Build the dependency download project to retrieve the dependency
execute_process(
COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE ${DEPENDENCY_NAME}_CMAKE_ERROR
WORKING_DIRECTORY ${DEPENDENCY_DOWNLOAD_DIR}
)
if(${DEPENDENCY_NAME}_CMAKE_ERROR)
message(FATAL_ERROR "Could not complete download for ${DEPENDENCY_NAME} dependency: ${${DEPENDENCY_NAME}_CMAKE_ERROR}")
endif()
endif()
# Configure the dependency source project
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${DEPENDENCY_CMAKE_ARGS} ${DEPENDENCY_SOURCE_DIR}/${SOURCE_SUBDIR}
RESULT_VARIABLE ${DEPENDENCY_NAME}_CMAKE_ERROR
WORKING_DIRECTORY ${DEPENDENCY_SOURCE_DIR}
)
if(${DEPENDENCY_NAME}_CMAKE_ERROR)
message(FATAL_ERROR "Could not configure project for ${DEPENDENCY_NAME} dependency: ${${DEPENDENCY_NAME}_CMAKE_ERROR}")
endif()
# Build and install the dependency source project
execute_process(
COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE}
RESULT_VARIABLE ${DEPENDENCY_NAME}_CMAKE_ERROR
WORKING_DIRECTORY ${DEPENDENCY_SOURCE_DIR}
)
if(${DEPENDENCY_NAME}_CMAKE_ERROR)
message(FATAL_ERROR "Could not build and install project for ${DEPENDENCY_NAME} dependency: ${${DEPENDENCY_NAME}_CMAKE_ERROR}")
endif()
find_package(${NAME} REQUIRED)
endmacro()
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
PROCESS_DEPENDENCY(ZLIB https://github.com/madler/zlib.git v1.2.8 "."
"-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}"
"-DCMAKE_DEBUG_POSTFIX=d"
)
PROCESS_DEPENDENCY(Protobuf https://github.com/protocolbuffers/protobuf.git v3.5.1 "cmake"
"-Dprotobuf_BUILD_SHARED_LIBS=ON"
"-Dprotobuf_BUILD_TESTS=OFF"
"-Dprotobuf_WITH_ZLIB=ON"
"-Dprotobuf_DEBUG_POSTFIX=d"
"-Dprotobuf_VERBOSE=ON"
"-Dprotobuf_MSVC_STATIC_RUNTIME=OFF"
"-DCMAKE_DEBUG_POSTFIX=d"
"-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}"
"-DZLIB_ROOT=${CMAKE_BINARY_DIR}"
)
# Copy missing protobuf header: google/protobuf/stubs/io_win32.h
file(COPY "${CMAKE_BINARY_DIR}/Protobuf/src/google/protobuf/stubs/io_win32.h"
DESTINATION "${CMAKE_BINARY_DIR}/include/google/protobuf/stubs"
)
add_subdirectory(examples/cpp)
install(DIRECTORY data/ DESTINATION ${CMAKE_BINARY_DIR}/bin)