1# Copyright (c) 2015, 2021, Oracle and/or its affiliates.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License, version 2.0,
5# as published by the Free Software Foundation.
6#
7# This program is also distributed with certain software (including
8# but not limited to OpenSSL) that is licensed under separate terms,
9# as designated in a particular file or component or in included license
10# documentation.  The authors of MySQL hereby grant you an additional
11# permission to link the program and your derivative works with the
12# separately licensed software that they have included with MySQL.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License, version 2.0, for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
23#
24# Usage:
25#
26#  cmake -DWITH_PROTOBUF="bundled"|"system"
27#
28#  Default is "bundled"
29#  Other values will be ignored, and we fall back to "bundled"
30#
31
32MACRO(RESET_PROTOBUF_VARIABLES)
33  UNSET(PROTOBUF_INCLUDE_DIR CACHE)
34  UNSET(PROTOBUF_INCLUDE_DIR)
35  UNSET(PROTOBUF_INCLUDE_DIRS CACHE)
36  UNSET(PROTOBUF_INCLUDE_DIRS)
37  UNSET(PROTOBUF_LIBRARIES CACHE)
38  UNSET(PROTOBUF_LIBRARIES)
39  UNSET(PROTOBUF_LIBRARY CACHE)
40  UNSET(PROTOBUF_LIBRARY)
41  UNSET(PROTOBUF_LIBRARY_DEBUG CACHE)
42  UNSET(PROTOBUF_LIBRARY_DEBUG)
43  UNSET(PROTOBUF_LITE_LIBRARY CACHE)
44  UNSET(PROTOBUF_LITE_LIBRARY)
45  UNSET(PROTOBUF_LITE_LIBRARY_DEBUG CACHE)
46  UNSET(PROTOBUF_LITE_LIBRARY_DEBUG)
47  UNSET(PROTOBUF_PROTOC_EXECUTABLE CACHE)
48  UNSET(PROTOBUF_PROTOC_EXECUTABLE)
49  UNSET(PROTOBUF_PROTOC_LIBRARY_DEBUG CACHE)
50  UNSET(PROTOBUF_PROTOC_LIBRARY_DEBUG)
51ENDMACRO()
52
53MACRO(ECHO_PROTOBUF_VARIABLES)
54  MESSAGE(STATUS "PROTOBUF_INCLUDE_DIR ${PROTOBUF_INCLUDE_DIR}")
55  MESSAGE(STATUS "PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY}")
56  MESSAGE(STATUS "PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE}")
57ENDMACRO()
58
59MACRO(COULD_NOT_FIND_PROTOBUF)
60  ECHO_PROTOBUF_VARIABLES()
61  MESSAGE(STATUS "Could not find (the correct version of) protobuf.")
62  MESSAGE(STATUS "MySQL currently requires at least protobuf version 2.5")
63  MESSAGE(FATAL_ERROR
64    "You can build with the bundled sources"
65    )
66ENDMACRO()
67
68SET(BUNDLED_PROTO_SRCDIR ${CMAKE_SOURCE_DIR}/extra/protobuf/protobuf-2.6.1/src)
69
70MACRO(MYSQL_USE_BUNDLED_PROTOBUF)
71  SET(WITH_PROTOBUF "bundled" CACHE INTERNAL
72    "Bundled protoc and protobuf library")
73  # Set the same variables as FindProtobuf.cmake
74  SET(PROTOBUF_FOUND 1 CACHE INTERNAL "")
75  SET(PROTOBUF_INCLUDE_DIR ${BUNDLED_PROTO_SRCDIR} CACHE INTERNAL "")
76  SET(PROTOBUF_INCLUDE_DIRS ${BUNDLED_PROTO_SRCDIR} CACHE INTERNAL "")
77  SET(PROTOBUF_LIBRARY protobuf CACHE INTERNAL "")
78  SET(PROTOBUF_LIBRARY_DEBUG protobuf CACHE INTERNAL "")
79  SET(PROTOBUF_LIBRARIES protobuf CACHE INTERNAL "")
80  SET(PROTOBUF_PROTOC_EXECUTABLE protoc CACHE INTERNAL "")
81  SET(PROTOBUF_PROTOC_LIBRARY protoclib CACHE INTERNAL "")
82  SET(PROTOBUF_PROTOC_LIBRARY_DEBUG protoclib CACHE INTERNAL "")
83  SET(PROTOBUF_LITE_LIBRARY protobuf-lite CACHE INTERNAL "")
84  SET(PROTOBUF_LITE_LIBRARY_DEBUG protobuf-lite CACHE INTERNAL "")
85  ADD_SUBDIRECTORY(extra/protobuf)
86ENDMACRO()
87
88MACRO(MYSQL_CHECK_PROTOBUF)
89  IF (NOT WITH_PROTOBUF OR
90      NOT WITH_PROTOBUF STREQUAL "system")
91    SET(WITH_PROTOBUF "bundled")
92  ENDIF()
93  MESSAGE(STATUS "WITH_PROTOBUF=${WITH_PROTOBUF}")
94  IF(WITH_PROTOBUF STREQUAL "bundled")
95    MYSQL_USE_BUNDLED_PROTOBUF()
96  ELSE()
97    FIND_PACKAGE(Protobuf)
98  ENDIF()
99
100  IF(NOT PROTOBUF_FOUND)
101    MESSAGE(WARNING "Protobuf could not be found")
102  ENDIF()
103
104  IF(PROTOBUF_FOUND)
105    # Verify protobuf version number. Version information looks like:
106    # // The current version, represented as a single integer to make comparison
107    # // easier:  major * 10^6 + minor * 10^3 + micro
108    # #define GOOGLE_PROTOBUF_VERSION 2006000
109    FILE(STRINGS "${PROTOBUF_INCLUDE_DIR}/google/protobuf/stubs/common.h"
110      PROTOBUF_VERSION_NUMBER
111      REGEX "^#define[\t ]+GOOGLE_PROTOBUF_VERSION[\t ][0-9]+.*"
112      )
113    STRING(REGEX REPLACE
114      "^.*GOOGLE_PROTOBUF_VERSION[\t ]([0-9])[0-9][0-9]([0-9])[0-9][0-9].*$"
115      "\\1"
116      PROTOBUF_MAJOR_VERSION "${PROTOBUF_VERSION_NUMBER}")
117    STRING(REGEX REPLACE
118      "^.*GOOGLE_PROTOBUF_VERSION[\t ]([0-9])[0-9][0-9]([0-9])[0-9][0-9].*$"
119      "\\2"
120      PROTOBUF_MINOR_VERSION "${PROTOBUF_VERSION_NUMBER}")
121
122    MESSAGE(STATUS
123      "protobuf version is ${PROTOBUF_MAJOR_VERSION}.${PROTOBUF_MINOR_VERSION}")
124
125    IF("${PROTOBUF_MAJOR_VERSION}.${PROTOBUF_MINOR_VERSION}" VERSION_LESS "2.5")
126      COULD_NOT_FIND_PROTOBUF()
127    ENDIF()
128  ENDIF()
129ENDMACRO()
130