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