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 | 695a85e | 2020-06-03 12:35:10 -0700 | [diff] [blame] | 17 | set( |
| 18 | Protobuf_PREBUILTS_DIR |
| 19 | "${CMAKE_CURRENT_SOURCE_DIR}/../../prebuilts/protobuf") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 20 | set(Protobuf_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../protobuf") |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 21 | set(Protobuf_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-target") |
Alexander Dorokhine | 0647e78 | 2020-06-09 16:11:53 -0700 | [diff] [blame^] | 22 | set(Icing_PROTO_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/icing-protobuf-gen") |
Alexander Dorokhine | 6c89460 | 2020-03-27 15:14:09 -0700 | [diff] [blame] | 23 | |
Alexander Dorokhine | 695a85e | 2020-06-03 12:35:10 -0700 | [diff] [blame] | 24 | ## Configure libprotobuf ## |
| 25 | # Find the right protoc to compile our proto files |
| 26 | if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin") |
| 27 | set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/darwin-x86_64/protoc") |
| 28 | elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux") |
| 29 | set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/linux-x86_64/protoc") |
| 30 | elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") |
| 31 | set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/windows-x86/protoc.exe") |
| 32 | else() |
| 33 | message( |
| 34 | FATAL_ERROR |
| 35 | "No protoc prebuilt found for host OS ${CMAKE_HOST_SYSTEM_NAME}") |
| 36 | endif() |
| 37 | message(STATUS "Using prebuilt protoc at: ${Protobuf_PROTOC_PATH}") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 38 | |
Alexander Dorokhine | 695a85e | 2020-06-03 12:35:10 -0700 | [diff] [blame] | 39 | # Compile libprotobuf |
| 40 | set(protobuf_BUILD_TESTS OFF CACHE BOOL "") |
| 41 | add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR}) |
| 42 | |
| 43 | # Compile libandroidicu |
| 44 | set(ICU_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../icu/libandroidicu") |
| 45 | set(ICU_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/icu-target") |
| 46 | add_subdirectory(${ICU_SOURCE_DIR} ${ICU_TARGET_BINARY_DIR}) |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 47 | |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame] | 48 | # Glob Icing proto sources. Results look like this: icing/proto/document.proto |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 49 | file( |
| 50 | GLOB_RECURSE |
| 51 | Icing_PROTO_FILES |
Alexander Dorokhine | 0647e78 | 2020-06-09 16:11:53 -0700 | [diff] [blame^] | 52 | RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/proto" |
| 53 | "*.proto") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 54 | message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}") |
| 55 | |
| 56 | # Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 57 | file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR}) |
| 58 | foreach(FILE ${Icing_PROTO_FILES}) |
| 59 | # Find the name of the proto file without the .proto extension |
| 60 | string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE}) |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame] | 61 | list(APPEND Icing_PROTO_SOURCES |
| 62 | "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc" |
| 63 | "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h") |
| 64 | add_custom_command( |
| 65 | OUTPUT "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc" |
| 66 | "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h" |
Alexander Dorokhine | 695a85e | 2020-06-03 12:35:10 -0700 | [diff] [blame] | 67 | COMMAND ${Protobuf_PROTOC_PATH} |
Alexander Dorokhine | 0647e78 | 2020-06-09 16:11:53 -0700 | [diff] [blame^] | 68 | --proto_path "${CMAKE_CURRENT_SOURCE_DIR}/proto" |
Alexander Dorokhine | 875dc9f | 2020-04-26 21:00:11 -0700 | [diff] [blame] | 69 | --cpp_out ${Icing_PROTO_GEN_DIR} |
| 70 | ${FILE} |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 71 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
Alexander Dorokhine | 695a85e | 2020-06-03 12:35:10 -0700 | [diff] [blame] | 72 | DEPENDS ${Protobuf_PROTOC_PATH} |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 73 | ) |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 74 | endforeach() |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 75 | message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}") |
| 76 | |
| 77 | # Glob Icing C++ sources |
| 78 | # TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob |
| 79 | # below so that cmake knows when to re-generate the makefiles. |
| 80 | file( |
| 81 | # List files recursively |
| 82 | GLOB_RECURSE |
| 83 | # Store into a variable of this name |
| 84 | Icing_CC_SOURCES |
| 85 | # Return paths that are relative to the project root |
| 86 | RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} |
| 87 | # Glob expressions |
| 88 | icing/*.cc icing/*.h |
| 89 | ) |
| 90 | # Exclude the same types of files as Android.bp. See the comments there. |
Tim Barron | 770e599 | 2020-06-05 16:19:07 -0700 | [diff] [blame] | 91 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*[^a-zA-Z0-9]test[^a-zA-Z0-9].*$") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 92 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$") |
Tim Barron | 770e599 | 2020-06-05 16:19:07 -0700 | [diff] [blame] | 93 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/helpers/icu/.*$") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 94 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$") |
Tim Barron | 770e599 | 2020-06-05 16:19:07 -0700 | [diff] [blame] | 95 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/icu/.*$") |
| 96 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/simple/.*$") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 97 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$") |
Tim Barron | 770e599 | 2020-06-05 16:19:07 -0700 | [diff] [blame] | 98 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/icu/.*$") |
| 99 | list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/simple/.*$") |
Alexander Dorokhine | 49410d1 | 2020-03-02 16:19:49 -0800 | [diff] [blame] | 100 | message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}") |
| 101 | |
| 102 | add_library( |
| 103 | # .so name |
| 104 | icing |
| 105 | |
| 106 | # Shared or static |
| 107 | SHARED |
| 108 | |
| 109 | # Provides a relative path to your source file(s). |
| 110 | ${Icing_CC_SOURCES} |
| 111 | ${Icing_PROTO_SOURCES} |
| 112 | ) |
| 113 | target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) |
| 114 | target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR}) |
| 115 | target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src") |
Tim Barron | b47184c | 2020-05-13 22:31:00 +0000 | [diff] [blame] | 116 | target_include_directories(icing PRIVATE "${ICU_SOURCE_DIR}/include") |
| 117 | target_link_libraries(icing protobuf::libprotobuf libandroidicu log) |