blob: f2a5407b1106d6f6bd337b036ccda42974565485 [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
Alexander Dorokhine6c894602020-03-27 15:14:09 -070017# Build protoc with a host configuration. We need to run it on the host to
18# create our proto files.
Alexander Dorokhine49410d12020-03-02 16:19:49 -080019set(CMAKE_HOST_ARGS
20 -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
21 -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
22 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
23 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
24 -DCMAKE_GENERATOR:STRING=${CMAKE_GENERATOR}
25 -DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM})
Alexander Dorokhine49410d12020-03-02 16:19:49 -080026set(Protobuf_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../protobuf")
Alexander Dorokhine6c894602020-03-27 15:14:09 -070027set(Protobuf_HOST_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-host")
28set(Protobuf_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-target")
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070029add_custom_command(
30 OUTPUT "${Protobuf_HOST_BINARY_DIR}/protoc"
Alexander Dorokhine6c894602020-03-27 15:14:09 -070031
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070032 # Run another cmake invocation to configure the protobuf project
Alexander Dorokhine49410d12020-03-02 16:19:49 -080033 COMMAND "${CMAKE_COMMAND}"
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070034 ${CMAKE_HOST_ARGS}
35 -H${Protobuf_SOURCE_DIR}/cmake
36 -B${Protobuf_HOST_BINARY_DIR}
37 -Dprotobuf_BUILD_TESTS:BOOL=OFF
Alexander Dorokhine49410d12020-03-02 16:19:49 -080038
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070039 # Run the actual build tool (ninja) to compile protoc for the host
Alexander Dorokhine49410d12020-03-02 16:19:49 -080040 COMMAND "${CMAKE_MAKE_PROGRAM}" protoc
Alexander Dorokhine6c894602020-03-27 15:14:09 -070041 WORKING_DIRECTORY ${Protobuf_HOST_BINARY_DIR}
Alexander Dorokhine49410d12020-03-02 16:19:49 -080042)
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070043message(STATUS "Building protoc at: ${Protobuf_HOST_BINARY_DIR}/protoc")
Alexander Dorokhine49410d12020-03-02 16:19:49 -080044
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070045# Glob Icing proto sources. Results look like this: icing/proto/document.proto
Alexander Dorokhine49410d12020-03-02 16:19:49 -080046file(
47 GLOB_RECURSE
48 Icing_PROTO_FILES
49 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
50 "icing/*.proto")
51message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}")
52
53# Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files
54set(Icing_PROTO_GEN_DIR "${PROTO_GENERATED_FILES_BASE_DIR}/cpp")
55file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR})
56foreach(FILE ${Icing_PROTO_FILES})
57 # Find the name of the proto file without the .proto extension
58 string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE})
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070059 list(APPEND Icing_PROTO_SOURCES
60 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc"
61 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h")
62 add_custom_command(
63 OUTPUT "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.cc"
64 "${Icing_PROTO_GEN_DIR}/${FILE_NOEXT}.pb.h"
Alexander Dorokhine6c894602020-03-27 15:14:09 -070065 COMMAND "${Protobuf_HOST_BINARY_DIR}/protoc"
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070066 --proto_path ${CMAKE_CURRENT_SOURCE_DIR}
67 --cpp_out ${Icing_PROTO_GEN_DIR}
68 ${FILE}
Alexander Dorokhine49410d12020-03-02 16:19:49 -080069 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Alexander Dorokhine875dc9f2020-04-26 21:00:11 -070070 DEPENDS "${Protobuf_HOST_BINARY_DIR}/protoc"
Alexander Dorokhine49410d12020-03-02 16:19:49 -080071 )
Alexander Dorokhine49410d12020-03-02 16:19:49 -080072endforeach()
Alexander Dorokhine49410d12020-03-02 16:19:49 -080073message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}")
74
Alexander Dorokhine6c894602020-03-27 15:14:09 -070075# Compile libprotobuf
76set(protobuf_BUILD_TESTS OFF CACHE BOOL "")
77add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR})
78
Tim Barronb47184c2020-05-13 22:31:00 +000079# Compile libandroidicu
80set(ICU_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../icu/libandroidicu")
81set(ICU_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/icu-target")
82add_subdirectory(${ICU_SOURCE_DIR} ${ICU_TARGET_BINARY_DIR})
83
Alexander Dorokhine49410d12020-03-02 16:19:49 -080084# Glob Icing C++ sources
85# TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob
86# below so that cmake knows when to re-generate the makefiles.
87file(
88 # List files recursively
89 GLOB_RECURSE
90 # Store into a variable of this name
91 Icing_CC_SOURCES
92 # Return paths that are relative to the project root
93 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
94 # Glob expressions
95 icing/*.cc icing/*.h
96)
97# Exclude the same types of files as Android.bp. See the comments there.
98list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_test\.cc$")
99list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$")
100list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$")
101list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$")
102message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}")
103
104add_library(
105 # .so name
106 icing
107
108 # Shared or static
109 SHARED
110
111 # Provides a relative path to your source file(s).
112 ${Icing_CC_SOURCES}
113 ${Icing_PROTO_SOURCES}
114)
115target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
116target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR})
117target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src")
Tim Barronb47184c2020-05-13 22:31:00 +0000118target_include_directories(icing PRIVATE "${ICU_SOURCE_DIR}/include")
119target_link_libraries(icing protobuf::libprotobuf libandroidicu log)