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