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 15cmake_minimum_required(VERSION 3.10.2) 16 17project(icing) 18 19add_definitions("-DICING_REVERSE_JNI_SEGMENTATION=1") 20set(VERSION_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/icing/jni.lds") 21set(CMAKE_SHARED_LINKER_FLAGS 22 "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections -Wl,--version-script=${VERSION_SCRIPT}") 23 24set( 25 Protobuf_PREBUILTS_DIR 26 "${CMAKE_CURRENT_SOURCE_DIR}/../../prebuilts/protobuf") 27set(Protobuf_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../protobuf") 28set(Protobuf_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-target") 29set(Icing_PROTO_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/icing-protobuf-gen") 30 31## Configure libprotobuf ## 32# Find the right protoc to compile our proto files 33if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin") 34 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/darwin-x86_64/protoc") 35elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux") 36 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/linux-x86_64/protoc") 37elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") 38 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/windows-x86/protoc.exe") 39else() 40 message( 41 FATAL_ERROR 42 "No protoc prebuilt found for host OS ${CMAKE_HOST_SYSTEM_NAME}") 43endif() 44message(STATUS "Using prebuilt protoc at: ${Protobuf_PROTOC_PATH}") 45 46# Compile libprotobuf 47set(protobuf_BUILD_TESTS OFF CACHE BOOL "") 48add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR}) 49 50# Compile libandroidicu 51set(ICU_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../icu/libandroidicu") 52set(ICU_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/icu-target") 53add_subdirectory("${ICU_SOURCE_DIR}/static_shim" ${ICU_TARGET_BINARY_DIR}) 54 55# Glob Icing proto sources. Results look like this: icing/proto/document.proto 56file( 57 GLOB_RECURSE 58 Icing_PROTO_FILES 59 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/proto" 60 "*.proto") 61message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}") 62 63 64# Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files 65# The DEPENDS section of add_custom_command could trigger a remake if any proto 66# source file has been updated. 67file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR}) 68foreach(FILE ${Icing_PROTO_FILES}) 69 # Find the name of the proto file without the .proto extension 70 string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE}) 71 list(APPEND Icing_PROTO_SOURCES 72 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc" 73 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h") 74 add_custom_command( 75 OUTPUT "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc" 76 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h" 77 COMMAND ${Protobuf_PROTOC_PATH} 78 --proto_path "${CMAKE_CURRENT_SOURCE_DIR}/proto" 79 --cpp_out "lite:${Icing_PROTO_GEN_DIR}" 80 ${FILE} 81 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 82 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/proto/${FILE} 83 ) 84endforeach() 85message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}") 86 87# Glob Icing C++ sources 88# TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob 89# below so that cmake knows when to re-generate the makefiles. 90file( 91 # List files recursively 92 GLOB_RECURSE 93 # Store into a variable of this name 94 Icing_CC_SOURCES 95 # Return paths that are relative to the project root 96 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} 97 # Glob expressions 98 icing/*.cc icing/*.h 99) 100 101# TODO(b/170611579): When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS 102# in the glob and remove this section. 103include(synced_AOSP_CL_number.txt) 104 105# Exclude the same types of files as Android.bp. See the comments there. 106list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*[^a-zA-Z0-9]test[^a-zA-Z0-9].*$") 107list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$") 108list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/helpers/icu/.*$") 109list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$") 110list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/icu/.*$") 111list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/simple/.*$") 112list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$") 113list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/icu/.*$") 114list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/simple/.*$") 115message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}") 116 117add_library( 118 # .so name 119 icing 120 121 # Shared or static 122 SHARED 123 124 # Provides a relative path to your source file(s). 125 ${Icing_CC_SOURCES} 126 ${Icing_PROTO_SOURCES} 127) 128target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) 129target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR}) 130target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src") 131target_include_directories(icing PRIVATE "${ICU_SOURCE_DIR}/include") 132target_link_libraries(icing protobuf::libprotobuf-lite libandroidicu log z) 133