blob: e5206637719f85a83f0ed094c002eee61100ceb1 [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")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080018
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070019set(
20 Protobuf_PREBUILTS_DIR
21 "${CMAKE_CURRENT_SOURCE_DIR}/../../prebuilts/protobuf")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080022set(Protobuf_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../protobuf")
Alexander Dorokhine6c894602020-03-27 15:14:09 -070023set(Protobuf_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-target")
Alexander Dorokhine0647e782020-06-09 16:11:53 -070024set(Icing_PROTO_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/icing-protobuf-gen")
Alexander Dorokhine6c894602020-03-27 15:14:09 -070025
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070026## Configure libprotobuf ##
27# Find the right protoc to compile our proto files
28if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin")
29 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/darwin-x86_64/protoc")
30elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
31 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/linux-x86_64/protoc")
32elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
33 set(Protobuf_PROTOC_PATH "${Protobuf_PREBUILTS_DIR}/windows-x86/protoc.exe")
34else()
35 message(
36 FATAL_ERROR
37 "No protoc prebuilt found for host OS ${CMAKE_HOST_SYSTEM_NAME}")
38endif()
39message(STATUS "Using prebuilt protoc at: ${Protobuf_PROTOC_PATH}")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080040
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070041# Compile libprotobuf
42set(protobuf_BUILD_TESTS OFF CACHE BOOL "")
43add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR})
44
45# Compile libandroidicu
46set(ICU_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../icu/libandroidicu")
47set(ICU_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/icu-target")
48add_subdirectory(${ICU_SOURCE_DIR} ${ICU_TARGET_BINARY_DIR})
Alexander Dorokhine49410d12020-03-02 16:19:49 -080049
Terry Wangbe928632020-10-12 03:56:14 -070050# Creates a file deps_name.cmake to save dependencies in it. This file should be
51# treated as part of CMakeLists.txt. This file should stay in .gitignore
52# TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob
53# and remove this section.
54function(update_deps_file deps_name deps)
55 set(DEPS_FILE ${deps_name}.cmake.gen)
56 set(CONTENT "# generated by make process.\nset(Tmp_${deps_name} ${deps})\n")
57 set(EXISTING_CONTENT "")
58 if(EXISTS ${DEPS_FILE})
59 file(READ ${DEPS_FILE} EXISTING_CONTENT)
60 endif()
61 # Compare the new contents with the existing file, if it exists and is the same
62 # we don't want to trigger a make by changing its timestamp.
63 if(NOT EXISTING_CONTENT STREQUAL CONTENT)
64 file(WRITE ${DEPS_FILE} ${CONTENT})
65 endif()
66 # Include the file so it's tracked as a generation dependency we don't
67 # need the content.
68 include(${DEPS_FILE})
69endfunction(update_deps_file)
70
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070071# Glob Icing proto sources. Results look like this: icing/proto/document.proto
Alexander Dorokhine49410d12020-03-02 16:19:49 -080072file(
73 GLOB_RECURSE
74 Icing_PROTO_FILES
Alexander Dorokhine0647e782020-06-09 16:11:53 -070075 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/proto"
76 "*.proto")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080077message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}")
78
Terry Wangbe928632020-10-12 03:56:14 -070079update_deps_file("IcingProtoFiles" "${Icing_PROTO_FILES}")
80
Alexander Dorokhine49410d12020-03-02 16:19:49 -080081# Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files
Terry Wangbe928632020-10-12 03:56:14 -070082# The DEPENDS section of add_custom_command could trigger a remake if any proto
83# source file has been updated.
Alexander Dorokhine49410d12020-03-02 16:19:49 -080084file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR})
85foreach(FILE ${Icing_PROTO_FILES})
86 # Find the name of the proto file without the .proto extension
87 string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE})
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070088 list(APPEND Icing_PROTO_SOURCES
89 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc"
90 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h")
91 add_custom_command(
92 OUTPUT "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc"
93 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h"
Alexander Dorokhine695a85e2020-06-03 12:35:10 -070094 COMMAND ${Protobuf_PROTOC_PATH}
Alexander Dorokhine0647e782020-06-09 16:11:53 -070095 --proto_path "${CMAKE_CURRENT_SOURCE_DIR}/proto"
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070096 --cpp_out ${Icing_PROTO_GEN_DIR}
97 ${FILE}
Alexander Dorokhine49410d12020-03-02 16:19:49 -080098 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Terry Wangbe928632020-10-12 03:56:14 -070099 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/proto/${FILE}
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800100 )
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800101endforeach()
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800102message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}")
103
104# Glob Icing C++ sources
105# TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob
106# below so that cmake knows when to re-generate the makefiles.
107file(
108 # List files recursively
109 GLOB_RECURSE
110 # Store into a variable of this name
111 Icing_CC_SOURCES
112 # Return paths that are relative to the project root
113 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
114 # Glob expressions
115 icing/*.cc icing/*.h
116)
Terry Wangbe928632020-10-12 03:56:14 -0700117
118update_deps_file("IcingCCSources" "${Icing_CC_SOURCES}")
119
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800120# Exclude the same types of files as Android.bp. See the comments there.
Tim Barron770e5992020-06-05 16:19:07 -0700121list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*[^a-zA-Z0-9]test[^a-zA-Z0-9].*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800122list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$")
Tim Barron770e5992020-06-05 16:19:07 -0700123list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/helpers/icu/.*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800124list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$")
Tim Barron770e5992020-06-05 16:19:07 -0700125list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/icu/.*$")
126list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tokenization/simple/.*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800127list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$")
Tim Barron770e5992020-06-05 16:19:07 -0700128list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/icu/.*$")
129list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/transform/simple/.*$")
Alexander Dorokhine49410d12020-03-02 16:19:49 -0800130message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}")
131
132add_library(
133 # .so name
134 icing
135
136 # Shared or static
137 SHARED
138
139 # Provides a relative path to your source file(s).
140 ${Icing_CC_SOURCES}
141 ${Icing_PROTO_SOURCES}
142)
143target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
144target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR})
145target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src")
Tim Barronb47184c2020-05-13 22:31:00 +0000146target_include_directories(icing PRIVATE "${ICU_SOURCE_DIR}/include")
147target_link_libraries(icing protobuf::libprotobuf libandroidicu log)