blob: 01ee8eb07cac8e3c485d2a4ab1850c55dbf93b94 [file] [log] [blame]
Alexander Dorokhine49410d12020-03-02 16:19:49 -08001# 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
Cassie Wang1db05b52020-06-26 13:14:18 -070017add_definitions("-DICING_REVERSE_JNI_SEGMENTATION=1")
Tim Barrond86119a2021-06-04 11:29:24 -070018set(VERSION_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/icing/jni.lds")
19set(CMAKE_SHARED_LINKER_FLAGS
20 "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections -Wl,--version-script=${VERSION_SCRIPT}")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080021
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070022set(
23 Protobuf_PREBUILTS_DIR
24 "${CMAKE_CURRENT_SOURCE_DIR}/../../prebuilts/protobuf")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080025set(Protobuf_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../protobuf")
Alexander Dorokhine6c894602020-03-27 15:14:09 -070026set(Protobuf_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-target")
Alexander Dorokhine0647e782020-06-09 16:11:53 -070027set(Icing_PROTO_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/icing-protobuf-gen")
Alexander Dorokhine6c894602020-03-27 15:14:09 -070028
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070029## Configure libprotobuf ##
30# Find the right protoc to compile our proto files
31if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin")
32 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/darwin-x86_64/protoc")
33elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
34 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/linux-x86_64/protoc")
35elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
36 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/windows-x86/protoc.exe")
37else()
38 message(
39 FATAL_ERROR
40 "No protoc prebuilt found for host OS ${CMAKE_HOST_SYSTEM_NAME}")
41endif()
42message(STATUS "Using prebuilt protoc at: ${Protobuf_PROTOC_PATH}")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080043
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070044# Compile libprotobuf
45set(protobuf_BUILD_TESTS OFF CACHE BOOL "")
46add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR})
47
48# Compile libandroidicu
49set(ICU_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../icu/libandroidicu")
50set(ICU_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/icu-target")
Tim Barron850ab322021-04-21 22:07:28 -070051add_subdirectory("${ICU_SOURCE_DIR}/static_shim" ${ICU_TARGET_BINARY_DIR})
Alexander Dorokhine49410d12020-03-02 16:19:49 -080052
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070053# Glob Icing proto sources. Results look like this: icing/proto/document.proto
Alexander Dorokhine49410d12020-03-02 16:19:49 -080054file(
55 GLOB_RECURSE
56 Icing_PROTO_FILES
Alexander Dorokhine0647e782020-06-09 16:11:53 -070057 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/proto"
58 "*.proto")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080059message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}")
60
Terry Wangbe928632020-10-12 03:56:14 -070061
Alexander Dorokhine49410d12020-03-02 16:19:49 -080062# Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files
Terry Wangbe928632020-10-12 03:56:14 -070063# The DEPENDS section of add_custom_command could trigger a remake if any proto
64# source file has been updated.
Alexander Dorokhine49410d12020-03-02 16:19:49 -080065file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR})
66foreach(FILE ${Icing_PROTO_FILES})
67 # Find the name of the proto file without the .proto extension
68 string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE})
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070069 list(APPEND Icing_PROTO_SOURCES
70 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc"
71 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h")
72 add_custom_command(
73 OUTPUT "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc"
74 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h"
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070075 COMMAND ${Protobuf_PROTOC_PATH}
Alexander Dorokhine0647e782020-06-09 16:11:53 -070076 --proto_path "${CMAKE_CURRENT_SOURCE_DIR}/proto"
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070077 --cpp_out ${Icing_PROTO_GEN_DIR}
78 ${FILE}
Alexander Dorokhine49410d12020-03-02 16:19:49 -080079 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Terry Wangbe928632020-10-12 03:56:14 -070080 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/proto/${FILE}
Alexander Dorokhine49410d12020-03-02 16:19:49 -080081 )
Alexander Dorokhine49410d12020-03-02 16:19:49 -080082endforeach()
Alexander Dorokhine49410d12020-03-02 16:19:49 -080083message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}")
84
85# Glob Icing C++ sources
86# TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob
87# below so that cmake knows when to re-generate the makefiles.
88file(
89 # List files recursively
90 GLOB_RECURSE
91 # Store into a variable of this name
92 Icing_CC_SOURCES
93 # Return paths that are relative to the project root
94 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
95 # Glob expressions
96 icing/*.cc icing/*.h
97)
Terry Wangbe928632020-10-12 03:56:14 -070098
Terry Wangffe04ca2020-12-23 00:00:05 -080099# TODO(b/170611579): When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS
100# in the glob and remove this section.
101include(synced_AOSP_CL_number.txt)
Terry Wangbe928632020-10-12 03:56:14 -0700102
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800103# Exclude the same types of files as Android.bp. See the comments there.
Tim Barron770e5992020-06-05 16:19:07 -0700104list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*[^a-zA-Z0-9]test[^a-zA-Z0-9].*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800105list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$")
Tim Barron770e5992020-06-05 16:19:07 -0700106list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/helpers/icu/.*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800107list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$")
Tim Barron770e5992020-06-05 16:19:07 -0700108list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/icu/.*$")
109list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/simple/.*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800110list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$")
Tim Barron770e5992020-06-05 16:19:07 -0700111list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/icu/.*$")
112list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/simple/.*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800113message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}")
114
115add_library(
116 # .so name
117 icing
118
119 # Shared or static
120 SHARED
121
122 # Provides a relative path to your source file(s).
123 ${Icing_CC_SOURCES}
124 ${Icing_PROTO_SOURCES}
125)
126target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
127target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR})
128target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src")
Tim Barronb47184c2020-05-13 22:31:00 +0000129target_include_directories(icing PRIVATE "${ICU_SOURCE_DIR}/include")
130target_link_libraries(icing protobuf::libprotobuf libandroidicu log)