blob: f41890ac10fdf3ac40fc1729c8e9c9b410b65186 [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")
29
Alexander Dorokhine49410d12020-03-02 16:19:49 -080030# Run another cmake invocation to configure the protobuf project
31execute_process(
32 COMMAND "${CMAKE_COMMAND}"
33 ${CMAKE_HOST_ARGS}
34 -H${Protobuf_SOURCE_DIR}/cmake
Alexander Dorokhine6c894602020-03-27 15:14:09 -070035 -B${Protobuf_HOST_BINARY_DIR}
Alexander Dorokhine49410d12020-03-02 16:19:49 -080036 -Dprotobuf_BUILD_TESTS:BOOL=OFF
37 RESULT_VARIABLE exec_value
38 OUTPUT_VARIABLE exec_output
39 ERROR_VARIABLE exec_output
40)
41message("Result of proto configuration: ${exec_value}. Output: ${exec_output}")
42
43# Run the actual build tool (ninja) to compile protoc for the host
44execute_process(
45 COMMAND "${CMAKE_MAKE_PROGRAM}" protoc
Alexander Dorokhine6c894602020-03-27 15:14:09 -070046 WORKING_DIRECTORY ${Protobuf_HOST_BINARY_DIR}
Alexander Dorokhine49410d12020-03-02 16:19:49 -080047 RESULT_VARIABLE exec_value
48 OUTPUT_VARIABLE exec_output
49 ERROR_VARIABLE exec_output
50)
51message("Result of proto build: ${exec_value}. Output: ${exec_output}")
52
53# Glob Icing proto sources
54file(
55 GLOB_RECURSE
56 Icing_PROTO_FILES
57 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
58 "icing/*.proto")
59message(STATUS "Icing_PROTO_FILES=${Icing_PROTO_FILES}")
60
61# Run protoc on Icing_PROTO_FILES to generate pb.cc and pb.h files
62set(Icing_PROTO_GEN_DIR "${PROTO_GENERATED_FILES_BASE_DIR}/cpp")
63file(MAKE_DIRECTORY ${Icing_PROTO_GEN_DIR})
64foreach(FILE ${Icing_PROTO_FILES})
65 # Find the name of the proto file without the .proto extension
66 string(REGEX REPLACE "\.proto$" "" FILE_NOEXT ${FILE})
67 execute_process(
Alexander Dorokhine6c894602020-03-27 15:14:09 -070068 COMMAND "${Protobuf_HOST_BINARY_DIR}/protoc"
Alexander Dorokhine49410d12020-03-02 16:19:49 -080069 --proto_path ${CMAKE_CURRENT_SOURCE_DIR}
70 --cpp_out ${Icing_PROTO_GEN_DIR}
71 ${FILE}
72 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
73 RESULT_VARIABLE exec_value
74 OUTPUT_VARIABLE exec_output
75 ERROR_VARIABLE exec_output
76 )
77 message("Result of protoc ${FILE}: ${exec_value}. Output: ${exec_output}")
78endforeach()
79
80# Glob generated source files from running protoc
81file(
82 GLOB_RECURSE
83 Icing_PROTO_SOURCES
84 "${Icing_PROTO_GEN_DIR}/*.pb.cc"
85 "${Icing_PROTO_GEN_DIR}/*.pb.h"
86)
87message(STATUS "Icing_PROTO_SOURCES=${Icing_PROTO_SOURCES}")
88
Alexander Dorokhine6c894602020-03-27 15:14:09 -070089# Compile libprotobuf
90set(protobuf_BUILD_TESTS OFF CACHE BOOL "")
91add_subdirectory("${Protobuf_SOURCE_DIR}/cmake" ${Protobuf_TARGET_BINARY_DIR})
92
Alexander Dorokhine49410d12020-03-02 16:19:49 -080093# Glob Icing C++ sources
94# TODO: When supporting cmake v3.12 or higher, use CONFIGURE_DEPENDS in the glob
95# below so that cmake knows when to re-generate the makefiles.
96file(
97 # List files recursively
98 GLOB_RECURSE
99 # Store into a variable of this name
100 Icing_CC_SOURCES
101 # Return paths that are relative to the project root
102 RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
103 # Glob expressions
104 icing/*.cc icing/*.h
105)
106# Exclude the same types of files as Android.bp. See the comments there.
107list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_test\.cc$")
108list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/.*_benchmark\.cc$")
109list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/testing/.*$")
110list(FILTER Icing_CC_SOURCES EXCLUDE REGEX "^icing/tools/.*$")
111message(STATUS "Icing_CC_SOURCES=${Icing_CC_SOURCES}")
112
113add_library(
114 # .so name
115 icing
116
117 # Shared or static
118 SHARED
119
120 # Provides a relative path to your source file(s).
121 ${Icing_CC_SOURCES}
122 ${Icing_PROTO_SOURCES}
123)
124target_include_directories(icing PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
125target_include_directories(icing PRIVATE ${Icing_PROTO_GEN_DIR})
126target_include_directories(icing PRIVATE "${Protobuf_SOURCE_DIR}/src")
Alexander Dorokhine6c894602020-03-27 15:14:09 -0700127target_link_libraries(icing protobuf::libprotobuf log)