Skip to content

Commit

Permalink
Adapt CMakeDeps and CMakeToolchain Conan-CMake integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
doosuu committed Mar 22, 2024
1 parent c5f1bf2 commit a4a3fa1
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 110 deletions.
6 changes: 6 additions & 0 deletions .conan/profiles/linux_aarch64_debug
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ compiler=gcc
compiler.version=11
compiler.libcxx=libstdc++11
build_type=Debug

[env]
CONAN_CMAKE_FIND_ROOT_PATH=/usr/bin/aarch64-linux-gnu
CONAN_CMAKE_SYSROOT=/usr/bin/aarch64-linux-gnu
CC=/usr/bin/aarch64-linux-gnu-gcc
CXX=/usr/bin/aarch64-linux-gnu-g++
6 changes: 6 additions & 0 deletions .conan/profiles/linux_aarch64_release
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ compiler=gcc
compiler.version=11
compiler.libcxx=libstdc++11
build_type=Release

[env]
CONAN_CMAKE_FIND_ROOT_PATH=/usr/bin/aarch64-linux-gnu
CONAN_CMAKE_SYSROOT=/usr/bin/aarch64-linux-gnu
CC=/usr/bin/aarch64-linux-gnu-gcc
CXX=/usr/bin/aarch64-linux-gnu-g++
6 changes: 6 additions & 0 deletions .conan/profiles/linux_x86_64_debug
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ compiler=gcc
compiler.version=11
compiler.libcxx=libstdc++11
build_type=Debug

[env]
CONAN_CMAKE_FIND_ROOT_PATH=/usr/bin/x86_64-linux-gnu
CONAN_CMAKE_SYSROOT=/usr/bin/x86_64-linux-gnu
CC=/usr/bin/x86_64-linux-gnu-gcc
CXX=/usr/bin/x86_64-linux-gnu-g++
6 changes: 6 additions & 0 deletions .conan/profiles/linux_x86_64_release
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ compiler=gcc
compiler.version=11
compiler.libcxx=libstdc++11
build_type=Release

[env]
CONAN_CMAKE_FIND_ROOT_PATH=/usr/bin/x86_64-linux-gnu
CONAN_CMAKE_SYSROOT=/usr/bin/x86_64-linux-gnu
CC=/usr/bin/x86_64-linux-gnu-gcc
CXX=/usr/bin/x86_64-linux-gnu-g++
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ jobs:
name: Check Conan package
strategy:
matrix:
host: ["linux_x86_64"]
host: ["linux_x86_64", "linux_aarch64"]
fail-fast: false


steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down
41 changes: 14 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

cmake_minimum_required(VERSION 3.16)

set(SDK_STANDALONE ON CACHE BOOL "Build the SDK in standalone mode.")
# set the project name
project(VehicleAppCppSdk CXX)

set(SDK_BUILD_TESTS ON CACHE BOOL "Build the SDK tests.")
set(SDK_BUILD_EXAMPLES ON CACHE BOOL "Build the SDK examples.")

Expand All @@ -26,36 +28,21 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/sdk/include
)

if(SDK_STANDALONE)
# set the project name
project(VehicleAppCppSdk CXX)

if(STATIC_BUILD)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static")
endif()

set(BUILD_EXAMPLES CACHE BOOL true)
set(BUILD_TESTS CACHE BOOL true)

find_program(CCACHE_FOUND ccache)
if(STATIC_BUILD)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXE_LINKER_FLAGS "-static")
endif()

if(CCACHE_FOUND)
message("Found ccache installation")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
endif()
set(BUILD_EXAMPLES CACHE BOOL true)
set(BUILD_TESTS CACHE BOOL true)

# use conan for dependency management
set(CONAN_BUILD_INFO_FILE ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
find_program(CCACHE_FOUND ccache)

if(NOT EXISTS ${CONAN_BUILD_INFO_FILE})
message(FATAL_ERROR "Conan build info file missing. Did you run install_dependencies.sh first?")
return()
else()
include(${CONAN_BUILD_INFO_FILE})
conan_basic_setup(TARGETS)
endif()
endif(SDK_STANDALONE)
if(CCACHE_FOUND)
message("Found ccache installation")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
endif()

if(SDK_BUILD_TESTS)
enable_testing()
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The Vehicle App SDK for C++ allows to create `Vehicle Apps` from the [Velocitas
* 📁 `include` - the headers which need to be included by users of the SDK
* 📁 `src` - contains the source code for the SDK from which the SDK library is built
* 📁 `test` - contains the unit test code for the SDK
* 📁 `test_package` - test package for Conan package tests/verification

## Prerequisites

Expand Down
45 changes: 18 additions & 27 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,28 @@ if [ "${GEN_COVERAGE}" == "ON" ]; then
CMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} --coverage"
fi

mkdir -p build && cd build

# Expose the PATH of the build-time requirements from Conan to CMake - this is NOT handled by
# any of Conan's CMake generators at the moment, hence we parse the conanbuildinfo.txt which
# is generated and holds these paths. This allows us to always use the protoc and grpc cpp plugin
# of the build system.
BUILD_TOOLS_PATH=""
CONAN_BUILD_TOOLS_PATHS=$(sed '/^PATH=/!d;s/PATH=//g;s/,/\n/g' ./conanbuildinfo.txt | tr -d '[]'\" )
while read -r p; do
if [[ ! -z "${p// }" ]]; then
BUILD_TOOLS_PATH="$BUILD_TOOLS_PATH;$p"
fi
done < <(echo "$CONAN_BUILD_TOOLS_PATHS")

XCOMPILE_TOOLCHAIN_FILE=""
if [[ "${BUILD_ARCH}" != "${HOST_ARCH}" ]]; then
echo "Setting up cross compilation toolchain..."
XCOMPILE_TOOLCHAIN_FILE="-DCMAKE_TOOLCHAIN_FILE=../cmake/${BUILD_ARCH}_to_${HOST_ARCH}.cmake"
fi

# Configure CMake and build the project.
cmake --no-warn-unused-cli \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
-DCMAKE_BUILD_TYPE:STRING=${BUILD_VARIANT} \
-DSTATIC_BUILD:BOOL=${STATIC_BUILD} \
-DSDK_BUILD_EXAMPLES=${SDK_BUILD_EXAMPLES} \
-S.. \
-B../build \
-G Ninja \
-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}" \
-DBUILD_TOOLS_PATH:STRING="${BUILD_TOOLS_PATH}" \
${XCOMPILE_TOOLCHAIN_FILE} ..
cmake --build . --config ${BUILD_VARIANT} --target ${BUILD_TARGET} --
cd ..
#cmake --no-warn-unused-cli \
# -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE \
# -DCMAKE_BUILD_TYPE:STRING=${BUILD_VARIANT} \
# -DSTATIC_BUILD:BOOL=${STATIC_BUILD} \
# -DSDK_BUILD_EXAMPLES=${SDK_BUILD_EXAMPLES} \
# -S.. \
# -B../build \
# -G Ninja \
# -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}" \
# -DBUILD_TOOLS_PATH:STRING="${BUILD_TOOLS_PATH}" \
# ${XCOMPILE_TOOLCHAIN_FILE} ..
#cmake --build . --config ${BUILD_VARIANT} --target ${BUILD_TARGET} --
#cd ..

conan build \
-if ./build \
-bf ./build \
.
25 changes: 8 additions & 17 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@
# SPDX-License-Identifier: Apache-2.0

from conans import ConanFile, tools
from conan.tools.cmake import cmake_layout, CMakeToolchain, CMake
import subprocess
from conan.tools.cmake import cmake_layout, CMake
import os
import re
import subprocess

from conan.tools.cmake import cmake_layout
from conans import ConanFile, tools



Expand All @@ -44,27 +39,23 @@ class VehicleAppCppSdkConan(ConanFile):
("libcurl/8.1.2"),
("nlohmann_json/3.11.2"),
("openssl/1.1.1u"),
("paho-mqtt-c/1.3.9"),
("paho-mqtt-c/1.3.13"),
("paho-mqtt-cpp/1.2.0"),
("zlib/1.3"),
]
generators = "cmake"
generators = "CMakeToolchain", "CMakeDeps"
author = "Robert Bosch GmbH"

# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "examples": [True, False]}
default_options = {"shared": False, "examples": False}
default_options = {"shared": False, "examples": True}

exports = "version.txt"

# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = ".scripts/common.sh", "build.sh", "install_dependencies.sh", "CMakeLists.txt", "sdk/*", "examples/*", "conanfile.py", ".conan/profiles/*", "version.txt"

options = {"shared": [True, False]}

default_options = {"shared": True}

def set_version(self):
try:
git = tools.Git(folder=".")
Expand All @@ -88,17 +79,17 @@ def set_version(self):


def config_options(self):
if self.settings.os == "Linux":
if self.settings.os == "Windows":
del self.options.fPIC

def layout(self):
cmake_layout(self, src_folder="sdk")
cmake_layout(self)

def build(self):
cmake = CMake(self)
cmake.configure(variables={
"STATIC_BUILD:BOOL":not self.options["shared"],
"SDK_BUILD_EXAMPLES:BOOL":self.options["examples"]
"STATIC_BUILD:BOOL": "ON" if not self.options.shared else "OFF",
"SDK_BUILD_EXAMPLES:BOOL": "ON" if self.options.examples else "OFF"
})
cmake.build()

Expand Down
28 changes: 3 additions & 25 deletions install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,10 @@ echo "Build arch ${BUILD_ARCH}"
echo "Host arch ${HOST_ARCH}"
echo "Building deps ${WHICH_DEPS_TO_BUILD}"

mkdir -p build

XCOMPILE_PROFILE=""

if [[ "${BUILD_ARCH}" != "${HOST_ARCH}" ]]; then
echo "Setting up cross compilation toolchain..."

toolchain=/usr/bin/${HOST_ARCH}-linux-gnu
target_host=${HOST_ARCH}-linux-gnu
cc_compiler=gcc
cxx_compiler=g++

export CONAN_CMAKE_FIND_ROOT_PATH=$toolchain
export CONAN_CMAKE_SYSROOT=$toolchain
export CC=$target_host-$cc_compiler
export CXX=$target_host-$cxx_compiler

XCOMPILE_PROFILE="-pr:b .conan/profiles/linux_${BUILD_ARCH}_${BUILD_VARIANT}"
fi

# Enable Conan revision handling to enable pinning googleapis recipe revision (see conanfile.py)
export CONAN_REVISIONS_ENABLED=1

conan install \
-pr:b .conan/profiles/linux_${BUILD_ARCH}_${BUILD_VARIANT} \
-pr:h .conan/profiles/linux_${HOST_ARCH}_${BUILD_VARIANT} \
${XCOMPILE_PROFILE} \
--build "${WHICH_DEPS_TO_BUILD}" \
-of ./build \
-if ./build .
-if ./build \
.
2 changes: 2 additions & 0 deletions sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.16)

add_subdirectory(proto)
add_subdirectory(src)

Expand Down
21 changes: 14 additions & 7 deletions sdk/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
#
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.16)

file(GLOB_RECURSE PROTO_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.proto)

find_program(_gRPC_PROTOBUF_PROTOC_EXECUTABLE protoc REQUIRED
PATHS ${BUILD_TOOLS_PATH}
NO_DEFAULT_PATH)
PATHS ${BUILD_TOOLS_PATH})
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin REQUIRED
PATHS ${BUILD_TOOLS_PATH}
NO_DEFAULT_PATH)
PATHS ${BUILD_TOOLS_PATH})

set(_gRPC_PROTO_GENS_DIR ${CMAKE_BINARY_DIR}/gens)
set(_gRPC_PROTO_SRCS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down Expand Up @@ -47,11 +47,11 @@ foreach(FILE ${PROTO_FILES})
${OUTPUT_FILE_PATH_GRPC_MOCK_HDR}
)

set(GENERATE_GRPC_SOURCES FALSE)
set(GENERATE_GRPC_SOURCES OFF)

foreach(OUTPUT_FILE ${OUTPUT_FILES})
if(NOT EXISTS ${OUTPUT_FILE})
set(GENERATE_GRPC_SOURCES TRUE)
set(GENERATE_GRPC_SOURCES ON)
message("File does not exist: " ${OUTPUT_FILE})
else()
# message("File already exists: " ${OUTPUT_FILE})
Expand Down Expand Up @@ -88,6 +88,13 @@ add_library(${TARGET_NAME} STATIC
${PROTO_SRCS}
)

find_package(grpc REQUIRED)
find_package(protobuf REQUIRED)

target_link_libraries(${TARGET_NAME}
CONAN_PKG::grpc
gRPC::grpc
gRPC::grpc++
protobuf::protobuf
)

set_property(TARGET ${TARGET_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
22 changes: 17 additions & 5 deletions sdk/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
#
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.16)

set(TARGET_NAME "vehicle-app-sdk")

if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

add_library(${TARGET_NAME}
sdk/VehicleApp.cpp
sdk/Model.cpp
Expand Down Expand Up @@ -50,11 +56,17 @@ add_dependencies(${TARGET_NAME}
vehicle-app-sdk-generated-grpc
)

find_package(gRPC REQUIRED)
find_package(cpr REQUIRED)
find_package(fmt REQUIRED)
find_package(PahoMqttCpp REQUIRED)
find_package(nlohmann_json REQUIRED)

target_link_libraries(${TARGET_NAME}
CONAN_PKG::grpc
CONAN_PKG::cpr
CONAN_PKG::fmt
CONAN_PKG::paho-mqtt-cpp
CONAN_PKG::nlohmann_json
gRPC::grpc
cpr::cpr
fmt::fmt
PahoMqttCpp::paho-mqttpp3-static
nlohmann_json::nlohmann_json
vehicle-app-sdk-generated-grpc
)

0 comments on commit a4a3fa1

Please sign in to comment.