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") |
| 29 | |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 30 | # Run another cmake invocation to configure the protobuf project |
| 31 | execute_process( |
| 32 | COMMAND "${CMAKE_COMMAND}" |
| 33 | ${CMAKE_HOST_ARGS} |
| 34 | -H${Protobuf_SOURCE_DIR}/cmake |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame^] | 35 | -B${Protobuf_HOST_BINARY_DIR} |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 36 | -Dprotobuf_BUILD_TESTS:BOOL=OFF |
| 37 | RESULT_VARIABLE exec_value |
| 38 | OUTPUT_VARIABLE exec_output |
| 39 | ERROR_VARIABLE exec_output |
| 40 | ) |
| 41 | message("Result of proto configuration: ${exec_value}. Output: ${exec_output}") |
| 42 | |
| 43 | # Run the actual build tool (ninja) to compile protoc for the host |
| 44 | execute_process( |
| 45 | COMMAND "${CMAKE_MAKE_PROGRAM}" protoc |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame^] | 46 | WORKING_DIRECTORY ${Protobuf_HOST_BINARY_DIR} |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 47 | RESULT_VARIABLE exec_value |
| 48 | OUTPUT_VARIABLE exec_output |
| 49 | ERROR_VARIABLE exec_output |
| 50 | ) |
| 51 | message("Result of proto build: ${exec_value}. Output: ${exec_output}") |
| 52 | |
| 53 | # Glob Icing proto sources |
| 54 | file( |
| 55 | GLOB_RECURSE |
| 56 | Icing_PROTO_FILES |
| 57 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 58 | "icing/*.proto") |
| 59 | message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}") |
| 60 | |
| 61 | # Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files |
| 62 | set(Icing_PROTO_GEN_DIR "${PROTO_GENERATED_FILES_BASE_DIR}/cpp") |
| 63 | file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR}) |
| 64 | foreach(FILE ${Icing_PROTO_FILES}) |
| 65 | # Find the name of the proto file without the .proto extension |
| 66 | string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE}) |
| 67 | execute_process( |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame^] | 68 | COMMAND "${Protobuf_HOST_BINARY_DIR}/protoc" |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 69 | --proto_path ${CMAKE_CURRENT_SOURCE_DIR} |
| 70 | --cpp_out ${Icing_PROTO_GEN_DIR} |
| 71 | ${FILE} |
| 72 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 73 | RESULT_VARIABLE exec_value |
| 74 | OUTPUT_VARIABLE exec_output |
| 75 | ERROR_VARIABLE exec_output |
| 76 | ) |
| 77 | message("Result of protoc ${FILE}: ${exec_value}. Output: ${exec_output}") |
| 78 | endforeach() |
| 79 | |
| 80 | # Glob generated source files from running protoc |
| 81 | file( |
| 82 | GLOB_RECURSE |
| 83 | Icing_PROTO_SOURCES |
| 84 | "${Icing_PROTO_GEN_DIR}/*.pb.cc" |
| 85 | "${Icing_PROTO_GEN_DIR}/*.pb.h" |
| 86 | ) |
| 87 | message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}") |
| 88 | |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame^] | 89 | # Compile libprotobuf |
| 90 | set(protobuf_BUILD_TESTS OFF CACHE BOOL "") |
| 91 | add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR}) |
| 92 | |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 93 | # Glob Icing C++ sources |
| 94 | # TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob |
| 95 | # below so that cmake knows when to re-generate the makefiles. |
| 96 | file( |
| 97 | # List files recursively |
| 98 | GLOB_RECURSE |
| 99 | # Store into a variable of this name |
| 100 | Icing_CC_SOURCES |
| 101 | # Return paths that are relative to the project root |
| 102 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 103 | # Glob expressions |
| 104 | icing/*.cc icing/*.h |
| 105 | ) |
| 106 | # Exclude the same types of files as Android.bp. See the comments there. |
| 107 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_test\.cc$") |
| 108 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$") |
| 109 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$") |
| 110 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$") |
| 111 | message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}") |
| 112 | |
| 113 | add_library( |
| 114 | # .so name |
| 115 | icing |
| 116 | |
| 117 | # Shared or static |
| 118 | SHARED |
| 119 | |
| 120 | # Provides a relative path to your source file(s). |
| 121 | ${Icing_CC_SOURCES} |
| 122 | ${Icing_PROTO_SOURCES} |
| 123 | ) |
| 124 | target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) |
| 125 | target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR}) |
| 126 | target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src") |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame^] | 127 | target_link_libraries(icing protobuf::libprotobuf log) |