Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMakeLists.txt file to ACS project to enable build in external toolchain. #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
cmake_minimum_required(VERSION 3.15)

project(acs VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STARDARD_REQUIRED True)

# Set the build type to Release
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -O3")

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

find_package(gRPC CONFIG REQUIRED)
find_package(absl CONFIG REQUIRED)
option(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
find_package(CURL REQUIRED)
find_package(GTest CONFIG REQUIRED)

add_subdirectory(proto)

add_library(acs_agent_helper
"cpp/acs_agent_helper.cc"
"cpp/acs_agent_helper.h"
)
target_link_libraries(acs_agent_helper PUBLIC
acs_proto
${CURL_LIBRARIES}
)
target_include_directories(
acs_agent_helper PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
)

add_library(acs_agent_client_reactor
"cpp/acs_agent_client_reactor.cc"
"cpp/acs_agent_client_reactor.h"
)
target_link_libraries(acs_agent_client_reactor PUBLIC
acs_agent_helper
)

add_library(fake_acs_agent_server_reactor
"cpp/fake_acs_agent_server_reactor.cc"
"cpp/fake_acs_agent_server_reactor.h"
)
target_link_libraries(fake_acs_agent_server_reactor PUBLIC
acs_agent_helper
)

add_library(acs_client SHARED
"cpp/acs_agent_client.cc"
"cpp/acs_agent_client.h"
)
target_link_libraries(acs_client PUBLIC
acs_agent_client_reactor
acs_agent_helper
)

add_executable(acs_agent_helper_test
"cpp/acs_agent_helper_test.cc"
)
target_link_libraries(acs_agent_helper_test PUBLIC
acs_agent_helper
GTest::gtest_main
GTest::gmock_main
)

add_executable(acs_agent_client_reactor_test
"cpp/acs_agent_client_reactor_test.cc"
)
target_link_libraries(acs_agent_client_reactor_test PUBLIC
acs_agent_client_reactor
fake_acs_agent_server_reactor
GTest::gtest_main
GTest::gmock_main
)

add_executable(acs_agent_client_test
"cpp/acs_agent_client_test.cc"
)
target_link_libraries(acs_agent_client_test PUBLIC
acs_client
fake_acs_agent_server_reactor
GTest::gtest_main
GTest::gmock_main
)
186 changes: 186 additions & 0 deletions cpp/CMakeExternalProject/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# This cmake file serves as an example of how to build ACS library
# with dependency of gRPC and other related libraries.
# The cmake file uses the ExternalProject_Add to manage dependency.
# Use the add_subdirectory(grpc) is a good alternative, however,
# the approach in this file would give the user complete control
# of dependency of gRPC (like protobuf, abseil).
# This file is largely borrowed from the gRPC GitHub's example:
# https://github.com/grpc/grpc/blob/master/examples/cpp/helloworld/cmake_externalproject/CMakeLists.txt

cmake_minimum_required(VERSION 3.15)

# Project
project(ACS-SuperBuild C CXX)

include(ExternalProject)

# Note: For all external projects, instead of using checked-out code, one could
# specify GIT_REPOSITORY and GIT_TAG to have cmake download the dependency directly,
# without needing to add a submodule to your project.
# The below code assumes that the grpc repo is pulled with its submodules and
# put in parallel with acs proto. Structure like this:
# root
# |-- acs
# |-- cpp
# |-- acs_agent_client.h etc.
# |-- proto
# |-- agent_communication.proto etc.
# `-- grpc
# |-- third_party
# |-- googletest
# |--CMakeLists.txt
# |-- abseil-cpp
# |--CMakeLists.txt
# |-- protobuf
# |--CMakeLists.txt

# Builds googletest project.
ExternalProject_Add(googletest
PREFIX googletest
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/third_party/googletest"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/googletest
)

# Builds absl project.
ExternalProject_Add(absl
PREFIX absl
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/third_party/abseil-cpp"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/absl
)

# Builds utf8_range project.
ExternalProject_Add(utf8_range
PREFIX utf8_range
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/third_party/utf8_range"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-Dutf8_range_ENABLE_TESTS:BOOL=OFF
-Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/utf8_range
DEPENDS absl
)

# Builds c-ares project.
ExternalProject_Add(c-ares
PREFIX c-ares
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/third_party/cares/cares"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-DCARES_SHARED:BOOL=OFF
-DCARES_STATIC:BOOL=ON
-DCARES_STATIC_PIC:BOOL=ON
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares
)

# Builds protobuf project.
ExternalProject_Add(protobuf
PREFIX protobuf
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/third_party/protobuf"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-Dprotobuf_BUILD_TESTS:BOOL=OFF
-Dprotobuf_WITH_ZLIB:BOOL=OFF
-Dprotobuf_ABSL_PROVIDER:STRING=package
-Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
-Dutf8_range_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/utf8_range/lib/cmake/utf8_range
-Dprotobuf_MSVC_STATIC_RUNTIME:BOOL=OFF
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/protobuf
DEPENDS absl utf8_range
)

# Builds re2 project.
ExternalProject_Add(re2
PREFIX re2
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/third_party/re2"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/re2
)

# Builds zlib project.
ExternalProject_Add(zlib
PREFIX zlib
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/third_party/zlib"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/zlib
)

# the location where protobuf-config.cmake will be installed varies by platform
if (WIN32)
set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/cmake")
else()
set(_FINDPACKAGE_PROTOBUF_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf/lib/cmake/protobuf")
endif()

# if OPENSSL_ROOT_DIR is set, propagate that hint path to the external projects with OpenSSL dependency.
set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "")
if (OPENSSL_ROOT_DIR)
set(_CMAKE_ARGS_OPENSSL_ROOT_DIR "-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}")
endif()

# Builds gRPC based on locally checked-out sources and set arguments so that all the dependencies
# are correctly located.
ExternalProject_Add(grpc
PREFIX grpc
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../grpc/"
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-DgRPC_INSTALL:BOOL=ON
-DgRPC_BUILD_TESTS:BOOL=OFF
-DgRPC_BUILD_MSVC_MP_COUNT:STRING=-1
-Dutf8_range_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/utf8_range/lib/cmake/utf8_range
-DgRPC_PROTOBUF_PROVIDER:STRING=package
-DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
-DgRPC_RE2_PROVIDER:STRING=package
-Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
-DgRPC_ZLIB_PROVIDER:STRING=package
-DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
-DgRPC_ABSL_PROVIDER:STRING=package
-Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
-DgRPC_CARES_PROVIDER:STRING=package
-Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
-DgRPC_SSL_PROVIDER:STRING=package
${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc
DEPENDS c-ares protobuf re2 zlib absl
)

# Build the acs projects itself using a CMakeLists.txt that assumes all the dependencies
# have already been installed.
# Even though acs is not really an "external project" from perspective of this build,
# we are still importing it using ExternalProject_Add because that allows us to use find_package()
# to locate all the dependencies (if we were building acs directly in this build we,
# we would have needed to manually import the libraries as opposed to reusing targets exported by
# gRPC and protobuf).
ExternalProject_Add(acs
PREFIX acs
SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../../"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/acs"
INSTALL_COMMAND ""
CMAKE_CACHE_ARGS
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE
-DProtobuf_DIR:PATH=${_FINDPACKAGE_PROTOBUF_CONFIG_DIR}
-Dc-ares_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/c-ares/lib/cmake/c-ares
-Dre2_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/re2/lib/cmake/re2
-Dutf8_range_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/utf8_range/lib/cmake/utf8_range
-DZLIB_ROOT:STRING=${CMAKE_CURRENT_BINARY_DIR}/zlib
-Dabsl_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/absl/lib/cmake/absl
${_CMAKE_ARGS_OPENSSL_ROOT_DIR}
-DgRPC_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/grpc/lib/cmake/grpc
-DGTest_DIR:PATH=${CMAKE_CURRENT_BINARY_DIR}/googletest/lib/cmake/GTest
DEPENDS protobuf grpc
)
2 changes: 1 addition & 1 deletion cpp/acs_agent_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <utility>
#include <vector>

#include "google/rpc/status.proto.h"
#include "status.proto.h"
#include "absl/log/absl_log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
Expand Down
36 changes: 36 additions & 0 deletions proto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
include(FindProtobuf)

set(PROTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(PROTO_IMPORT_DIRS
"${CMAKE_CURRENT_SOURCE_DIR}"
"${Protobuf_INCLUDE_DIRS}"
"${Protobuf_INCLUDE_DIR}"
)

add_library(acs_proto OBJECT
"agent_communication.proto"
"client.proto"
"field_behavior.proto"
"launch_stage.proto"
"status.proto"
)
target_link_libraries(acs_proto PUBLIC protobuf::libprotobuf gRPC::grpc++)

protobuf_generate(
TARGET acs_proto
IMPORT_DIRS ${PROTO_IMPORT_DIRS}
PROTOC_OUT_DIR "${PROTO_BINARY_DIR}")

protobuf_generate(
TARGET acs_proto
LANGUAGE grpc
GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc
PLUGIN "protoc-gen-grpc=\$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
IMPORT_DIRS ${PROTO_IMPORT_DIRS}
PROTOC_OUT_DIR "${PROTO_BINARY_DIR}")

target_include_directories(acs_proto
PUBLIC
${PROTO_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
)
6 changes: 3 additions & 3 deletions proto/agent_communication.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ syntax = "proto3";

package google.cloud.agentcommunication.v1;

import "google/api/client.proto";
import "google/api/field_behavior.proto";
import "client.proto";
import "field_behavior.proto";
import "google/protobuf/any.proto";
import "google/rpc/status.proto";
import "status.proto";

option csharp_namespace = "Google.Cloud.AgentCommunication.V1";
option go_package = "cloud.google.com/go/agentcommunication/apiv1/agentcommunicationpb;agentcommunicationpb";
Expand Down
Loading