Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 1 | # Copyright 2020 The Android Open Source Project |
| 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 | cmake_minimum_required(VERSION 3.10.2) |
| 16 | |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 17 | # Build protoc with a host configuration. We need to run it on the host to |
| 18 | # create our proto files. |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 19 | set(CMAKE_HOST_ARGS |
| 20 | -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS} |
| 21 | -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} |
| 22 | -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} |
| 23 | -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} |
| 24 | -DCMAKE_GENERATOR:STRING=${CMAKE_GENERATOR} |
| 25 | -DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}) |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 26 | set(Protobuf_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../protobuf") |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 27 | set(Protobuf_HOST_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-host") |
| 28 | set(Protobuf_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-target") |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 29 | add_custom_command( |
| 30 | OUTPUT "${Protobuf_HOST_BINARY_DIR}/protoc" |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 31 | |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 32 | # Run another cmake invocation to configure the protobuf project |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 33 | COMMAND "${CMAKE_COMMAND}" |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 34 | ${CMAKE_HOST_ARGS} |
| 35 | -H${Protobuf_SOURCE_DIR}/cmake |
| 36 | -B${Protobuf_HOST_BINARY_DIR} |
| 37 | -Dprotobuf_BUILD_TESTS:BOOL=OFF |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 38 | |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 39 | # Run the actual build tool (ninja) to compile protoc for the host |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 40 | COMMAND "${CMAKE_MAKE_PROGRAM}" protoc |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 41 | WORKING_DIRECTORY ${Protobuf_HOST_BINARY_DIR} |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 42 | ) |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 43 | message(STATUS "Building protoc at: ${Protobuf_HOST_BINARY_DIR}/protoc") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 44 | |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 45 | # Glob Icing proto sources. Results look like this: icing/proto/document.proto |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 46 | file( |
| 47 | GLOB_RECURSE |
| 48 | Icing_PROTO_FILES |
| 49 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 50 | "icing/*.proto") |
| 51 | message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}") |
| 52 | |
| 53 | # Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files |
| 54 | set(Icing_PROTO_GEN_DIR "${PROTO_GENERATED_FILES_BASE_DIR}/cpp") |
| 55 | file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR}) |
| 56 | foreach(FILE ${Icing_PROTO_FILES}) |
| 57 | # Find the name of the proto file without the .proto extension |
| 58 | string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE}) |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 59 | list(APPEND Icing_PROTO_SOURCES |
| 60 | "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc" |
| 61 | "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h") |
| 62 | add_custom_command( |
| 63 | OUTPUT "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc" |
| 64 | "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h" |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 65 | COMMAND "${Protobuf_HOST_BINARY_DIR}/protoc" |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 66 | --proto_path ${CMAKE_CURRENT_SOURCE_DIR} |
| 67 | --cpp_out ${Icing_PROTO_GEN_DIR} |
| 68 | ${FILE} |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 69 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame^] | 70 | DEPENDS "${Protobuf_HOST_BINARY_DIR}/protoc" |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 71 | ) |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 72 | endforeach() |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 73 | message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}") |
| 74 | |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 75 | # Compile libprotobuf |
| 76 | set(protobuf_BUILD_TESTS OFF CACHE BOOL "") |
| 77 | add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR}) |
| 78 | |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 79 | # Glob Icing C++ sources |
| 80 | # TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob |
| 81 | # below so that cmake knows when to re-generate the makefiles. |
| 82 | file( |
| 83 | # List files recursively |
| 84 | GLOB_RECURSE |
| 85 | # Store into a variable of this name |
| 86 | Icing_CC_SOURCES |
| 87 | # Return paths that are relative to the project root |
| 88 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 89 | # Glob expressions |
| 90 | icing/*.cc icing/*.h |
| 91 | ) |
| 92 | # Exclude the same types of files as Android.bp. See the comments there. |
| 93 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_test\.cc$") |
| 94 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$") |
| 95 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$") |
| 96 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$") |
| 97 | message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}") |
| 98 | |
| 99 | add_library( |
| 100 | # .so name |
| 101 | icing |
| 102 | |
| 103 | # Shared or static |
| 104 | SHARED |
| 105 | |
| 106 | # Provides a relative path to your source file(s). |
| 107 | ${Icing_CC_SOURCES} |
| 108 | ${Icing_PROTO_SOURCES} |
| 109 | ) |
| 110 | target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) |
| 111 | target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR}) |
| 112 | target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src") |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 113 | target_link_libraries(icing protobuf::libprotobuf log) |