Skip to content

Commit

Permalink
chore: Make it possible to import common modules from other projects.
Browse files Browse the repository at this point in the history
We do this by splitting `common`, `artifact` and `api` into their own
cmake projects.

Then we move the platform detection into `src/common`, since it is the
main responsible project, and it needs to be available to other
projects that import it. We also move the `config.h` here, and start
referring to it by its module path, so that it's less likely to clash.

We also adjust various path variables to refer mainly to
`PROJECT_SOURCE_DIR` instead of `CMAKE_SOURCE_DIR`, since the latter
is relative to the topmost project.

The common paths to the `src` directory, where the modules reside, as
well as the C++ `expected` and `optional` replacement libraries, are
now added to specific libraries instead of being global. This works
much better when other projects import the libraries than using global
settings, which are subject to project and directory scoping.

Signed-off-by: Kristian Amlie <[email protected]>
  • Loading branch information
Kristian Amlie committed Feb 1, 2024
1 parent 3ffcfeb commit d994692
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 93 deletions.
10 changes: 5 additions & 5 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[submodule "vendor/json"]
path = vendor/json
path = src/common/vendor/json
url = https://github.com/nlohmann/json.git
[submodule "vendor/lmdbxx"]
path = vendor/lmdbxx
path = src/common/vendor/lmdbxx
url = https://github.com/hoytech/lmdbxx.git
[submodule "vendor/tiny-process-library"]
path = vendor/tiny-process-library
path = src/common/vendor/tiny-process-library
url = https://gitlab.com/eidheim/tiny-process-library.git
[submodule "vendor/expected"]
path = vendor/expected
path = src/common/vendor/expected
url = https://github.com/TartanLlama/expected.git
[submodule "vendor/optional-lite"]
path = vendor/optional-lite
path = src/common/vendor/optional-lite
url = https://github.com/martinmoene/optional-lite.git
12 changes: 6 additions & 6 deletions LIC_FILES_CHKSUM.sha256
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
d0f406b04e7901e6b4076bdf5fd20f9d7f04fc41681069fd8954413ac6295688 LICENSE
#
# MIT clause license
86b998c792894ccb911a1cb7994f7a9652894e7a094c0b5e45be2f553f45cf14 vendor/json/LICENSE.MIT
08c345dca1019879171c2c465410f7bee984bbfbdb177afd2d95bf9ff6267602 vendor/json/docs/mkdocs/docs/home/license.md
2aefa40fa7e1c0fab18fbad5b15b02406eea1399d3cca42cc1012b65b4a56f74 vendor/tiny-process-library/LICENSE
86b998c792894ccb911a1cb7994f7a9652894e7a094c0b5e45be2f553f45cf14 src/common/vendor/json/LICENSE.MIT
08c345dca1019879171c2c465410f7bee984bbfbdb177afd2d95bf9ff6267602 src/common/vendor/json/docs/mkdocs/docs/home/license.md
2aefa40fa7e1c0fab18fbad5b15b02406eea1399d3cca42cc1012b65b4a56f74 src/common/vendor/tiny-process-library/LICENSE
#
# UNLICENSE
7e12e5df4bae12cb21581ba157ced20e1986a0508dd10d0e8a4ab9a4cf94e85c vendor/lmdbxx/UNLICENSE
7e12e5df4bae12cb21581ba157ced20e1986a0508dd10d0e8a4ab9a4cf94e85c src/common/vendor/lmdbxx/UNLICENSE
#
# CC0 1.0 Universal
a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499 vendor/expected/COPYING
a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499 src/common/vendor/expected/COPYING
#
# Boost Software License
c9bff75738922193e67fa726fa225535870d2aa1059f91452c411736284ad566 vendor/optional-lite/LICENSE.txt
c9bff75738922193e67fa726fa225535870d2aa1059f91452c411736284ad566 src/common/vendor/optional-lite/LICENSE.txt
40 changes: 0 additions & 40 deletions main.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,8 @@ if($CACHE{COVERAGE})
set(CMAKE_CXX_FLAGS "--coverage $CACHE{CMAKE_CXX_FLAGS}")
endif()

include(cmake/boost.cmake)

# TODO: proper platform detection
set(PLATFORM linux_x86)

set(MENDER_BUFSIZE 16384 CACHE STRING "Size of most internal block buffers. Can be reduced to conserve memory, but increases CPU usage.")

option(BUILD_TESTS "Build the unit tests (Default: ON)" ON)
option(ENABLE_CCACHE "Enable ccache support" OFF)
option(MENDER_LOG_BOOST "Use Boost as the underlying logging library provider (Default: ON)" ON)
option(MENDER_TAR_LIBARCHIVE "Use libarchive as the underlying tar library provider (Default: ON)" ON)
option(MENDER_SHA_OPENSSL "Use OpenSSL as the underlying shasum provider (Default: ON)" ON)
option(MENDER_CRYPTO_OPENSSL "Use OpenSSL as the underlying cryptography provider (Default: ON)" ON)
option(MENDER_USE_SYSTEMD "Use systemd as the watchdog provider (Default: ON)" ON)

option(MENDER_ARTIFACT_GZIP_COMPRESSION "Enable GZIP compression support when downloading and extracting Artifacts (Default: ON)" ON)
option(MENDER_ARTIFACT_LZMA_COMPRESSION "Enable LZMA compression support when downloading and extracting Artifacts (Default: ON)" ON)
option(MENDER_ARTIFACT_ZSTD_COMPRESSION "Enable Zstd compression support when downloading and extracting Artifacts (Default: ON)" ON)

if(ENABLE_CCACHE)
find_program(CCACHE ccache)
Expand All @@ -61,30 +45,6 @@ if(ENABLE_CCACHE)
endif()
endif()


if (${PLATFORM} STREQUAL linux_x86)
set(MENDER_USE_ASIO_LIBDBUS 1)
set(MENDER_USE_BOOST_ASIO 1)
set(MENDER_USE_BOOST_BEAST 1)
set(MENDER_USE_LMDB 1)
set(MENDER_USE_NLOHMANN_JSON 1)
set(MENDER_USE_TINY_PROC_LIB 1)
add_subdirectory(vendor/tiny-process-library)
else()
set(MENDER_USE_ASIO_LIBDBUS 0)
set(MENDER_USE_BOOST_ASIO 0)
set(MENDER_USE_BOOST_BEAST 0)
set(MENDER_USE_LMDB 0)
set(MENDER_USE_NLOHMANN_JSON 0)
set(MENDER_USE_TINY_PROC_LIB 0)
endif()

add_subdirectory(vendor/expected)
include_directories(${CMAKE_SOURCE_DIR}/vendor/expected/include)

add_subdirectory(vendor/optional-lite)
include_directories(${CMAKE_SOURCE_DIR}/vendor/optional-lite/include)

include(cmake/build_mode.cmake)

if("${STD_FILESYSTEM_LIB_NAME}" STREQUAL "")
Expand Down
13 changes: 1 addition & 12 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
execute_process(
COMMAND sh -c "git describe --tags --dirty --exact-match 2>/dev/null || git rev-parse --short HEAD"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE MENDER_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE GIT_CMD_ERROR
Expand All @@ -11,24 +11,13 @@ endif()

configure_file(mender-version.h.in mender-version.h)

# Default for all components.
add_compile_options(-Werror -Wall -Wsuggest-override)

set(MENDER_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(MENDER_BINARY_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR})

# This is in order for all of our components to be available inside all targets.
include_directories(${MENDER_SRC_DIR})

# This is in order to pick up our generated header files (`configure_file`).
include_directories(${MENDER_BINARY_SRC_DIR})

add_subdirectory(api)
add_subdirectory(common)
add_subdirectory(client_shared)
add_subdirectory(mender-update)
add_subdirectory(mender-auth)
add_subdirectory(artifact)

configure_file(config.h.in config.h)

2 changes: 2 additions & 0 deletions src/api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
project(mender-api)

add_library(api_auth STATIC auth/auth.cpp)
target_link_libraries(api_auth PUBLIC
common_log
Expand Down
5 changes: 3 additions & 2 deletions src/artifact/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
project(mender-artifact)

add_subdirectory(sha)
add_subdirectory(tar)
add_subdirectory(v3/scripts)
Expand Down Expand Up @@ -29,5 +31,4 @@ target_link_libraries(artifact_parser PUBLIC
target_compile_options(common_json PRIVATE ${PLATFORM_SPECIFIC_COMPILE_OPTIONS})
add_library(artifact STATIC artifact.cpp)
target_link_libraries(artifact PUBLIC artifact_parser)
target_include_directories(artifact PUBLIC ${MENDER_SRC_DIR}/artifact/)

target_include_directories(artifact PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
4 changes: 1 addition & 3 deletions src/artifact/sha/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ add_library(sha STATIC platform/openssl/sha.cpp)
target_link_libraries(sha PUBLIC OpenSSL::SSL OpenSSL::Crypto)
target_link_libraries(sha PUBLIC common_log common_error common_io)
target_compile_options(sha PRIVATE ${PLATFORM_SPECIFIC_COMPILE_OPTIONS})
target_include_directories(sha PUBLIC
${MENDER_SRC_DIR}/artifact/sha
)
target_include_directories(sha PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

2 changes: 1 addition & 1 deletion src/artifact/sha/sha.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_SHA_HPP
#define MENDER_SHA_HPP

#include <config.h>
#include <common/config.h>

#include <ctime>
#include <iomanip>
Expand Down
5 changes: 1 addition & 4 deletions src/artifact/tar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@ target_sources(common_tar PRIVATE
platform/libarchive/tar.cpp
platform/libarchive/wrapper.cpp
)
target_include_directories(common_tar PUBLIC
${MENDER_SRC_DIR}/artifact/tar/platform/
)

target_include_directories(common_tar PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/platform)
2 changes: 1 addition & 1 deletion src/artifact/tar/platform/libarchive/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_LIB_ARCHIVE_WRAPPER_HPP
#define MENDER_LIB_ARCHIVE_WRAPPER_HPP

#include <config.h>
#include <common/config.h>

#include <archive.h>
#include <archive_entry.h>
Expand Down
2 changes: 1 addition & 1 deletion src/artifact/tar/tar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_COMMON_TAR_HPP
#define MENDER_COMMON_TAR_HPP

#include <config.h>
#include <common/config.h>

#include <string>
#include <vector>
Expand Down
5 changes: 1 addition & 4 deletions src/artifact/v3/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
add_library(artifact_scripts_executor STATIC executor.cpp error.cpp)
target_include_directories(artifact_scripts_executor
PUBLIC
${MENDER_SRC_DIR}/artifact/
)
target_include_directories(artifact_scripts_executor PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../..)

target_link_libraries(artifact_scripts_executor
PUBLIC
Expand Down
58 changes: 55 additions & 3 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,72 @@
project(mender-common)

include(CheckCXXSourceCompiles)

# TODO: proper platform detection
set(PLATFORM linux_x86)

set(MENDER_BUFSIZE 16384 CACHE STRING "Size of most internal block buffers. Can be reduced to conserve memory, but increases CPU usage.")
option(MENDER_LOG_BOOST "Use Boost as the underlying logging library provider (Default: ON)" ON)
option(MENDER_TAR_LIBARCHIVE "Use libarchive as the underlying tar library provider (Default: ON)" ON)
option(MENDER_SHA_OPENSSL "Use OpenSSL as the underlying shasum provider (Default: ON)" ON)
option(MENDER_CRYPTO_OPENSSL "Use OpenSSL as the underlying cryptography provider (Default: ON)" ON)
option(MENDER_USE_SYSTEMD "Use systemd as the watchdog provider (Default: ON)" ON)

option(MENDER_ARTIFACT_GZIP_COMPRESSION "Enable GZIP compression support when downloading and extracting Artifacts (Default: ON)" ON)
option(MENDER_ARTIFACT_LZMA_COMPRESSION "Enable LZMA compression support when downloading and extracting Artifacts (Default: ON)" ON)
option(MENDER_ARTIFACT_ZSTD_COMPRESSION "Enable Zstd compression support when downloading and extracting Artifacts (Default: ON)" ON)

if (${PLATFORM} STREQUAL linux_x86)
set(MENDER_USE_ASIO_LIBDBUS 1)
set(MENDER_USE_BOOST_ASIO 1)
set(MENDER_USE_BOOST_BEAST 1)
set(MENDER_USE_LMDB 1)
set(MENDER_USE_NLOHMANN_JSON 1)
set(MENDER_USE_TINY_PROC_LIB 1)
else()
set(MENDER_USE_ASIO_LIBDBUS 0)
set(MENDER_USE_BOOST_ASIO 0)
set(MENDER_USE_BOOST_BEAST 0)
set(MENDER_USE_LMDB 0)
set(MENDER_USE_NLOHMANN_JSON 0)
set(MENDER_USE_TINY_PROC_LIB 0)
endif()

configure_file(config.h.in config.h)

if(${MENDER_USE_NLOHMANN_JSON})
add_subdirectory(vendor/tiny-process-library)
endif()

include(cmake/boost.cmake)

add_library(common_cpp INTERFACE)
target_compile_options(common_cpp INTERFACE -Werror -Wall -Wsuggest-override)
target_include_directories(common_cpp SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/expected/include)
target_include_directories(common_cpp SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/optional-lite/include)

add_library(common STATIC common.cpp)
target_include_directories(common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
# For the generated config.h.
target_include_directories(common PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/..)
target_link_libraries(common PUBLIC common_cpp)

add_library(common_setup STATIC setup/platform/posix/setup.cpp)
target_link_libraries(common_setup PUBLIC common)

add_library(common_error STATIC
error.cpp
error/platform/c++11/error.cpp
)
target_link_libraries(common_error PUBLIC common)

add_library(common_io STATIC
io/io.cpp
io/platform/c++17/io.cpp
io/platform/posix/io.cpp
)
target_compile_options(common_io PRIVATE ${PLATFORM_SPECIFIC_COMPILE_OPTIONS})
target_link_libraries(common_io PUBLIC common)

add_library(common_path STATIC)
target_sources(common_path PRIVATE
Expand All @@ -39,7 +91,7 @@ set(procs_sources "$<$<EQUAL:${MENDER_USE_TINY_PROC_LIB},1>:tiny_process_library
add_library(common_json STATIC json/json.cpp json/platform/${json_sources})
target_compile_options(common_json PRIVATE ${PLATFORM_SPECIFIC_COMPILE_OPTIONS})
if(${json_sources} MATCHES ".*nlohmann.*")
target_include_directories(common_json PUBLIC ${CMAKE_SOURCE_DIR}/vendor/json/include/)
target_include_directories(common_json PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/vendor/json/include/)
target_link_libraries(common_json PUBLIC common common_io common_error)
endif()

Expand All @@ -50,7 +102,7 @@ target_compile_options(common_key_value_database PRIVATE ${PLATFORM_SPECIFIC_COM
if(MENDER_USE_LMDB)
target_sources(common_key_value_database PRIVATE key_value_database/platform/lmdb/lmdb.cpp)
# Note: Use SYSTEM include style, since lmdbxx triggers some of our warnings.
target_include_directories(common_key_value_database SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/vendor/lmdbxx)
target_include_directories(common_key_value_database SYSTEM PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/lmdbxx)
target_link_libraries(common_key_value_database PUBLIC
lmdb
common
Expand Down Expand Up @@ -108,7 +160,7 @@ add_library(common_processes STATIC processes/processes.cpp processes/platform/$
target_compile_options(common_processes PRIVATE ${PLATFORM_SPECIFIC_COMPILE_OPTIONS})
target_link_libraries(common_processes PUBLIC common_error common_events common_log common)
if(${procs_sources} MATCHES ".*tiny_process_library.*")
target_include_directories(common_processes PRIVATE ${CMAKE_SOURCE_DIR}/vendor/tiny-process-library)
target_include_directories(common_processes PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/tiny-process-library)
target_link_libraries(common_processes PUBLIC tiny-process-library::tiny-process-library common_path)
target_compile_definitions(common_processes PUBLIC MENDER_USE_TINY_PROC_LIB=${MENDER_USE_TINY_PROC_LIB})
endif()
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/common/dbus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_COMMON_DBUS_HPP
#define MENDER_COMMON_DBUS_HPP

#include <config.h>
#include <common/config.h>

#include <functional>
#include <memory>
Expand Down
2 changes: 1 addition & 1 deletion src/common/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_COMMON_EVENTS_HPP
#define MENDER_COMMON_EVENTS_HPP

#include <config.h>
#include <common/config.h>

#include <functional>
#include <system_error>
Expand Down
2 changes: 1 addition & 1 deletion src/common/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <boost/asio/ssl/stream.hpp>
#endif // MENDER_USE_BOOST_BEAST

#include <config.h>
#include <common/config.h>

#include <common/common.hpp>
#include <common/error.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/common/io/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <common/io.hpp>

#include <config.h>
#include <common/config.h>

#include <cerrno>
#include <cstdint>
Expand Down
2 changes: 1 addition & 1 deletion src/common/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_COMMON_JSON_HPP
#define MENDER_COMMON_JSON_HPP

#include <config.h>
#include <common/config.h>

#include <string>
#include <map>
Expand Down
2 changes: 1 addition & 1 deletion src/common/key_value_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_COMMON_KEY_VALUE_DATABASE_HPP
#define MENDER_COMMON_KEY_VALUE_DATABASE_HPP

#include <config.h>
#include <common/config.h>

#include <functional>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion src/common/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_LOG_HPP
#define MENDER_LOG_HPP

#include <config.h>
#include <common/config.h>

#ifdef MENDER_LOG_BOOST
#include <boost/log/common.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/common/processes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#ifndef MENDER_COMMON_PROCESSES_HPP
#define MENDER_COMMON_PROCESSES_HPP
#include <config.h>
#include <common/config.h>
#include <chrono>
#include <future>
#include <memory>
Expand Down
2 changes: 1 addition & 1 deletion src/mender-update/deployments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef MENDER_UPDATE_DEPLOYMENTS_HPP
#define MENDER_UPDATE_DEPLOYMENTS_HPP

#include <config.h>
#include <common/config.h>

#ifdef MENDER_LOG_BOOST
#include <boost/log/sinks/sync_frontend.hpp>
Expand Down
1 change: 0 additions & 1 deletion tests/src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ target_link_libraries(key_value_database_test PRIVATE
main_test
gmock
)
target_compile_definitions(key_value_database_test PRIVATE MENDER_USE_LMDB=${MENDER_USE_LMDB})
# Use NO_PRETTY_VALUES to avoid very long byte strings in the output due to
# parametrized tests that have binary objects as input.
gtest_discover_tests(key_value_database_test NO_PRETTY_VALUES)
Expand Down
Loading

0 comments on commit d994692

Please sign in to comment.