1# Copyright 2016 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# We only need protobuf_generate_cpp from FindProtobuf, and we are going to
16# override the rest with ExternalProject version.
17include (FindProtobuf)
18
19set(PROTOBUF_TARGET external.protobuf)
20set(PROTOBUF_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/${PROTOBUF_TARGET})
21
22set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INSTALL_DIR}/include)
23include_directories(${PROTOBUF_INCLUDE_DIRS})
24include_directories(${CMAKE_CURRENT_BINARY_DIR})
25
26IF(CMAKE_BUILD_TYPE MATCHES Debug)
27  set(PROTOBUF_LIBRARIES protobufd)
28ELSE()
29  set(PROTOBUF_LIBRARIES protobuf)
30ENDIF()
31
32foreach(lib ${PROTOBUF_LIBRARIES})
33  if (MSVC)
34    set(LIB_PATH ${PROTOBUF_INSTALL_DIR}/lib/lib${lib}.lib)
35  else()
36    set(LIB_PATH ${PROTOBUF_INSTALL_DIR}/lib/lib${lib}.a)
37  endif()
38  list(APPEND PROTOBUF_BUILD_BYPRODUCTS ${LIB_PATH})
39
40  add_library(${lib} STATIC IMPORTED)
41  set_property(TARGET ${lib} PROPERTY IMPORTED_LOCATION
42               ${LIB_PATH})
43  add_dependencies(${lib} ${PROTOBUF_TARGET})
44endforeach(lib)
45
46set(PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_INSTALL_DIR}/bin/protoc)
47list(APPEND PROTOBUF_BUILD_BYPRODUCTS ${PROTOBUF_PROTOC_EXECUTABLE})
48
49if(${CMAKE_VERSION} VERSION_LESS "3.10.0")
50  set(PROTOBUF_PROTOC_TARGET protoc)
51else()
52  set(PROTOBUF_PROTOC_TARGET protobuf::protoc)
53endif()
54
55if(NOT TARGET ${PROTOBUF_PROTOC_TARGET})
56  add_executable(${PROTOBUF_PROTOC_TARGET} IMPORTED)
57endif()
58set_property(TARGET ${PROTOBUF_PROTOC_TARGET} PROPERTY IMPORTED_LOCATION
59             ${PROTOBUF_PROTOC_EXECUTABLE})
60add_dependencies(${PROTOBUF_PROTOC_TARGET} ${PROTOBUF_TARGET})
61
62include (ExternalProject)
63ExternalProject_Add(${PROTOBUF_TARGET}
64    PREFIX ${PROTOBUF_TARGET}
65    GIT_REPOSITORY https://github.com/google/protobuf.git
66    GIT_TAG 214c77e1b76e63e512bd675d1c300c80438642b6
67    UPDATE_COMMAND ""
68    CONFIGURE_COMMAND ${CMAKE_COMMAND} ${PROTOBUF_INSTALL_DIR}/src/${PROTOBUF_TARGET}/cmake
69        -G${CMAKE_GENERATOR}
70        -DCMAKE_INSTALL_PREFIX=${PROTOBUF_INSTALL_DIR}
71        -DCMAKE_INSTALL_LIBDIR=lib
72        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
73        -DCMAKE_POSITION_INDEPENDENT_CODE=ON
74        -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
75        -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
76        -DCMAKE_C_FLAGS=${PROTOBUF_CFLAGS}
77        -DCMAKE_CXX_FLAGS=${PROTOBUF_CXXFLAGS}
78        -Dprotobuf_BUILD_TESTS=OFF
79    BUILD_BYPRODUCTS ${PROTOBUF_BUILD_BYPRODUCTS}
80)
81
82# cmake 3.7 uses Protobuf_ when 3.5 PROTOBUF_ prefixes.
83set(Protobuf_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIRS})
84set(Protobuf_LIBRARIES ${PROTOBUF_LIBRARIES})
85set(Protobuf_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
86