Skip to content

Commit

Permalink
Add Bazel build and adapt CMake for our CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Ziegler committed Jan 12, 2024
1 parent 02c199d commit a495be4
Show file tree
Hide file tree
Showing 14 changed files with 482 additions and 26 deletions.
45 changes: 45 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
build --experimental_cc_shared_library
build --experimental_cc_implementation_deps
build --cxxopt "-std=c++17"

# Enable new platform resolution for C/+++
build --incompatible_enable_cc_toolchain_resolution

# Be strict on environment variables passed
build --incompatible_strict_action_env

## Address sanitizer
build:asan --define SANITIZERS_ASAN_ON=true
build:asan --copt="-fsanitize=address"
build:asan --copt="-no-canonical-prefixes"
build:asan --copt="-fno-omit-frame-pointer"
build:asan --copt="-fno-sanitize-recover"
build:asan --copt="-DADDRESS_SANITIZER"
build:asan --linkopt="-fsanitize=address"
build:asan --strip=never
build:asan --action_env=ASAN_OPTIONS=detect_leaks=1:color=always
build:asan --action_env=LSAN_OPTIONS="report_objects=1"
build:asan --cc_output_directory_tag=asan

## Thread sanitizer
build:tsan --define SANITIZERS_TSAN_ON=true
build:tsan --copt="-fsanitize=thread"
build:tsan --copt="-fno-omit-frame-pointer"
build:tsan --copt="-fno-sanitize-recover"
build:tsan --copt="-DTHREAD_SANITIZER"
build:tsan --linkopt="-fsanitize=thread"
build:tsan --strip=never
build:tsan --action_env=TSAN_OPTIONS="report_objects=1"
build:tsan --cc_output_directory_tag=tsan

## UndefinedBehavior sanitizer
build:ubsan --define SANITIZERS_UBSAN_ON=true
build:ubsan --copt="-fsanitize=undefined"
build:ubsan --copt="-fno-omit-frame-pointer"
build:ubsan --copt="-fno-sanitize-recover"
build:ubsan --copt="-DUNDEFINED_BEHAVIOR_SANITIZER"
build:ubsan --linkopt="-fsanitize=undefined"
build:ubsan --linkopt="-lubsan"
build:ubsan --strip=never
build:ubsan --action_env=UBSAN_OPTIONS="halt_on_error=1:print_stacktrace=1"
build:ubsan --cc_output_directory_tag=ubsan
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.3.2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,6 @@
/test/network_tests/suspend_resume_tests/suspend_resume_test_service.json
/Testing
!build_qnx/*

/.clwb/
/bazel-*
67 changes: 67 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
cc_shared_library(
name = "vsomeip3_shared",
shared_lib_name = "libvsomeip3.so",
tags = ["same-ros-pkg-as: vsomeip3"],
# Disabled due to linking problem when used as an external repository with sanitizers enabled.
#user_link_flags = [
# "-Wl,--no-undefined",
#],
deps = [
"//implementation",
],
)

cc_shared_library(
name = "vsomeip3_config_plugin",
dynamic_deps = [
":vsomeip3_shared",
],
shared_lib_name = "libvsomeip3-cfg.so.3",
tags = ["same-ros-pkg-as: vsomeip3"],
# Disabled due to linking problem when used as an external repository with sanitizers enabled.
#user_link_flags = [
# "-Wl,--no-undefined",
#],
deps = [
"//implementation:configuration",
],
)

cc_shared_library(
name = "vsomeip3_sd_plugin",
dynamic_deps = [
":vsomeip3_shared",
],
shared_lib_name = "libvsomeip3-sd.so.3",
tags = ["same-ros-pkg-as: vsomeip3"],
# Disabled due to linking problem when used as an external repository with sanitizers enabled.
#user_link_flags = [
# "-Wl,--no-undefined",
#],
deps = [
"//implementation:service_discovery",
],
)

cc_import(
name = "vsomeip3_import",
shared_library = ":vsomeip3_shared",
tags = ["same-ros-pkg-as: vsomeip3"],
deps = [
"//interface",
],
)

# interface library, use this target to depend on vsomeip
cc_library(
name = "vsomeip3",
data = [
":vsomeip3_config_plugin",
":vsomeip3_sd_plugin",
],
linkstatic = True, # no object files
visibility = ["//visibility:public"],
deps = [
":vsomeip3_import",
],
)
60 changes: 35 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,8 @@ add_definitions(-DVSOMEIP_INTERNAL_SUPPRESS_DEPRECATED)
find_package(Threads REQUIRED)

# Boost
find_package( Boost 1.55 COMPONENTS system thread filesystem REQUIRED )
if(${CMAKE_SYSTEM_NAME} MATCHES "QNX")
include_directories(${Boost_INCLUDE_DIR} )
else()
include_directories(SYSTEM ${Boost_INCLUDE_DIR} )
endif()
find_package( Boost 1.71 COMPONENTS system thread filesystem REQUIRED )
include_directories(${Boost_INCLUDE_DIRS})

if(Boost_FOUND)
if(Boost_LIBRARY_DIR)
Expand Down Expand Up @@ -251,13 +247,33 @@ if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS -D_WIN32_WINNT=${BOOST_WINDOWS_VERSION} -DWIN32 -DBOOST_ASIO_DISABLE_IOCP /EHsc /std:c++14 /wd4250")
set(USE_RT "")
link_directories(${Boost_LIBRARY_DIR_DEBUG})
elseif(${CMAKE_SYSTEM_NAME} MATCHES "QNX")
set(USE_RT "")
else()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OS_CXX_FLAGS} -g ${OPTIMIZE} -std=c++14 ${NO_DEPRECATED} ${EXPORTSYMBOLS}")
################################################################################
# Apex.AI additions
################################################################################
if (CMAKE_BUILD_TYPE STREQUAL "Release")
# Remove -g flag on Release builds, to remove debug_symbols
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OS_CXX_FLAGS} ${OPTIMIZE} -std=c++14 ${NO_DEPRECATED} ${EXPORTSYMBOLS}")
else()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OS_CXX_FLAGS} -g ${OPTIMIZE} -std=c++14 ${NO_DEPRECATED} ${EXPORTSYMBOLS}")
endif()
set(USE_RT "rt")
endif()


################################################################################
# Apex.AI additions
################################################################################
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-overloaded-virtual")
endif()

if ("${CMAKE_SYSTEM_NAME}" STREQUAL "QNX")
add_definitions(-DBOOST_ASIO_DISABLE_STRING_VIEW)
set (VSOMEIP_BASE_PATH "/var")
set(USE_RT "")
endif()

################################################################################
# Configuration library
################################################################################
Expand Down Expand Up @@ -304,7 +320,7 @@ add_library(${VSOMEIP_NAME} SHARED ${${VSOMEIP_NAME}_SRC})
set_target_properties (${VSOMEIP_NAME} PROPERTIES VERSION ${VSOMEIP_VERSION} SOVERSION ${VSOMEIP_MAJOR_VERSION})
if (MSVC)
set_target_properties(${VSOMEIP_NAME} PROPERTIES COMPILE_DEFINITIONS "VSOMEIP_DLL_COMPILATION")
else ()
elseif (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "QNX")
set_target_properties(${VSOMEIP_NAME} PROPERTIES LINK_FLAGS "-Wl,-wrap,socket -Wl,-wrap,accept -Wl,-wrap,open")
endif ()
target_include_directories(${VSOMEIP_NAME} INTERFACE
Expand Down Expand Up @@ -450,10 +466,10 @@ message(STATUS "Default configuration folder: ${DEFAULT_CONFIGURATION_FOLDER}")
set(DEFAULT_CONFIGURATION_FILE "/etc/vsomeip.json" CACHE FILEPATH "Default configuration file")
message(STATUS "Default configuration file: ${DEFAULT_CONFIGURATION_FILE}")

message("Predefined base path: ${VSOMEIP_BASE_PATH}")
message("Predefined unicast address: ${VSOMEIP_UNICAST_ADDRESS}")
message("Predefined diagnosis address: ${VSOMEIP_DIAGNOSIS_ADDRESS}")
message("Predefined wait times for internal communication ports (TCP):\
message(STATUS "Predefined base path: ${VSOMEIP_BASE_PATH}")
message(STATUS "Predefined unicast address: ${VSOMEIP_UNICAST_ADDRESS}")
message(STATUS "Predefined diagnosis address: ${VSOMEIP_DIAGNOSIS_ADDRESS}")
message(STATUS "Predefined wait times for internal communication ports (TCP):\
${VSOMEIP_LOCAL_TCP_PORT_WAIT_TIME}\
(max=${VSOMEIP_LOCAL_TCP_PORT_MAX_WAIT_TIME})")

Expand Down Expand Up @@ -577,10 +593,8 @@ install (
##############################################################################
add_custom_target(doc)

find_package(Doxygen)
if (NOT DOXYGEN_FOUND)
message(WARNING "Doxygen is not installed. Documentation can not be built.")
else()
find_package(Doxygen QUIET)
if (DOXYGEN_FOUND)
# set configuration variables for doxygen.in
set(PROJECT "vsomeip")
set(DOCDIR documentation)
Expand Down Expand Up @@ -615,13 +629,9 @@ else()
add_dependencies(doc doxygen-doc)
endif()

find_program(ASCIIDOC_PATH asciidoc)
find_program(SOURCE_HIGHLIGHT_PATH source-highlight)
if ("${ASCIIDOC_PATH}" STREQUAL "ASCIIDOC_PATH-NOTFOUND")
message(WARNING "asciidoc is not installed. Readme can not be built.")
elseif("${SOURCE_HIGHLIGHT_PATH}" STREQUAL "SOURCE_HIGHLIGHT_PATH-NOTFOUND")
message(WARNING "source-highlight is not installed. Readme can not be built.")
else()
find_program(ASCIIDOC_PATH asciidoc QUIET)
find_program(SOURCE_HIGHLIGHT_PATH source-highlight QUIET)
if (NOT ("${ASCIIDOC_PATH}" STREQUAL "ASCIIDOC_PATH-NOTFOUND" OR "${SOURCE_HIGHLIGHT_PATH}" STREQUAL "SOURCE_HIGHLIGHT_PATH-NOTFOUND"))
message("asciidoc found")
message("source-highlight found")
add_custom_command(TARGET doc
Expand Down
49 changes: 49 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
workspace(name = "vsomeip")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

BOOST_VERSION = "1.81.0"

BOOST_VERSION_us = BOOST_VERSION.replace(".", "_")

http_archive(
name = "boost",
build_file = "@//:boost.BUILD",
sha256 = "71feeed900fbccca04a3b4f2f84a7c217186f28a940ed8b7ed4725986baf99fa",
strip_prefix = "boost_{version_underscore}".format(version_underscore = BOOST_VERSION_us),
urls = ["https://boostorg.jfrog.io/artifactory/main/release/{version}/source/boost_{version_underscore}.tar.bz2".format(
version = BOOST_VERSION,
version_underscore = BOOST_VERSION_us,
)],
)

GOOGLETEST_VERSION = "1.11.0"

http_archive(
name = "googletest",
sha256 = "353571c2440176ded91c2de6d6cd88ddd41401d14692ec1f99e35d013feda55a",
strip_prefix = "googletest-release-{version}".format(version = GOOGLETEST_VERSION),
urls = ["https://github.com/google/googletest/archive/refs/tags/release-{version}.zip".format(version = GOOGLETEST_VERSION)],
)

# GOOGLETEST dependencies, taken from: https://raw.githubusercontent.com/google/googletest/release-1.11.0/WORKSPACE
http_archive(
name = "com_google_absl",
sha256 = "aeba534f7307e36fe084b452299e49b97420667a8d28102cf9a0daeed340b859",
strip_prefix = "abseil-cpp-7971fb358ae376e016d2d4fc9327aad95659b25e",
urls = ["https://github.com/abseil/abseil-cpp/archive/7971fb358ae376e016d2d4fc9327aad95659b25e.zip"], # 2021-05-20T02:59:16Z
)

http_archive(
name = "rules_cc",
sha256 = "1e19e9a3bc3d4ee91d7fcad00653485ee6c798efbbf9588d40b34cbfbded143d",
strip_prefix = "rules_cc-68cb652a71e7e7e2858c50593e5a9e3b94e5b9a9",
urls = ["https://github.com/bazelbuild/rules_cc/archive/68cb652a71e7e7e2858c50593e5a9e3b94e5b9a9.zip"], # 2021-05-14T14:51:14Z
)

http_archive(
name = "rules_python",
sha256 = "98b3c592faea9636ac8444bfd9de7f3fb4c60590932d6e6ac5946e3f8dbd5ff6",
strip_prefix = "rules_python-ed6cc8f2c3692a6a7f013ff8bc185ba77eb9b4d2",
urls = ["https://github.com/bazelbuild/rules_python/archive/ed6cc8f2c3692a6a7f013ff8bc185ba77eb9b4d2.zip"], # 2021-05-17T00:24:16Z
)
Empty file added bazel/BUILD
Empty file.
26 changes: 26 additions & 0 deletions bazel/cmake_like_configure_file.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
The configure_file rule imitates the similar CMake function for generating code: https://cmake.org/cmake/help/latest/command/configure_file.html
"""

def _cmake_like_configure_file_impl(ctx):
ctx.actions.expand_template(
template = ctx.file.src,
output = ctx.outputs.out,
substitutions = {
"@" + k + "@": v
for k, v in ctx.attr.config.items()
},
)
files = depset(direct = [ctx.outputs.out])
runfiles = ctx.runfiles(files = [ctx.outputs.out])
return [DefaultInfo(files = files, runfiles = runfiles)]

cmake_like_configure_file = rule(
implementation = _cmake_like_configure_file_impl,
provides = [DefaultInfo],
attrs = {
"src": attr.label(mandatory = True, allow_single_file = True),
"out": attr.output(mandatory = True),
"config": attr.string_dict(mandatory = True),
},
)
64 changes: 64 additions & 0 deletions boost.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "headers",
hdrs = glob(
[
"boost/**",
],
),
local_defines = ["BOOST_ALL_NO_LIB"],
include_prefix = ".",
visibility = ["//visibility:public"],
)

cc_library(
name = "_thread_detail",
hdrs = select({
"@platforms//os:linux": ["libs/thread/src/pthread/once_atomic.cpp"],
"//conditions:default": [],
}),
strip_include_prefix = "libs/thread/src/pthread",
)

cc_library(
name = "thread",
srcs = glob(
[
"libs/thread/src/*.cpp",
],
) + select({
"@platforms//os:linux": [
"libs/thread/src/pthread/once.cpp",
"libs/thread/src/pthread/thread.cpp",
],
"//conditions:default": [],
}),
implementation_deps = [
":_thread_detail",
],
linkopts = select({
"@platforms//os:linux": ["-lpthread"],
"//conditions:default": [],
}),
deps = [
":headers",
],
visibility = ["//visibility:public"],
)

cc_library(
name = "filesystem",
srcs = glob(
[
"libs/filesystem/src/**",
],
),
defines = [
"BOOST_FILESYSTEM_NO_CXX20_ATOMIC_REF"
],
deps = [
":headers",
],
visibility = ["//visibility:public"],
)
Loading

1 comment on commit a495be4

@goncaloalmeida
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@milan Ziegler why not add this to the main repository of vsomeip?

Please sign in to comment.