Skip to content
Merged
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
157 changes: 31 additions & 126 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,136 +14,21 @@
#
# SPDX-License-Identifier: Apache-2.0

# Enforce out-of-source builds
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(
FATAL_ERROR
"In-source builds are not allowed. Please create a build directory and run CMake from there.\n"
"You will need to remove CMakeCache.txt and CMakeFiles/ to clean up the generated files."
)
endif()

# Ensure one of the two acceptable build types are selected: DEBUG or RELEASE.
# Default to DEBUG
if(NOT CMAKE_BUILD_TYPE)
# No build type specified, default to Debug and force it into the cache
message(STATUS "No build type specified, defaulting to Debug")
set(CMAKE_BUILD_TYPE
Debug
CACHE STRING "Build type" FORCE)
else()
# Check they chose an acceptable one
set(valid_build_types "Debug" "Release")
list(FIND valid_build_types ${CMAKE_BUILD_TYPE} _index)
if(${_index} EQUAL -1)
message(
FATAL_ERROR
"Unknown build type ${CMAKE_BUILD_TYPE}. Supported build types are Debug and Release"
)
endif()
unset(valid_build_types)
endif()

cmake_minimum_required(VERSION 3.28.3)
project(
OscmsCoerExplorer
VERSION 1.0.0
LANGUAGES C CXX)

# set cmake build options
option(BUILD_TESTS "Build unit tests" ON)
option(RUN_CPPCHECK "Enable cppcheck" ON)

# set compiler flags Standard compile options
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED TRUE)
set(C_EXTENSIONS ON) # Enable gnu11
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CXX_EXTENSIONS ON) # Enable gnu11

# Enforce C compiler minimum version of 11 (which is what comes with Ubuntu
# 22.04). CI jobs actually use gcc:12
#
# C++ is only used for unit tests, so we don't care about its version

if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
if(CMAKE_C_COMPILER_VERSION VERSION_LESS 11)
message(
FATAL_ERROR
"C compiler version must be at least 11.0.0, you are running ${CMAKE_C_COMPILER_VERSION}. Please update your C compiler.\n"
"You will need to remove CMakeCache.txt and CMakeFiles/ to clean up the generated files."
)
endif()
else()
message(
FATAL_ERROR
"C compiler must be GNU, version >= 11. Please update your C compiler.\n"
"You will need to remove CMakeCache.txt and CMakeFiles/ to clean up the generated files."
)
endif()

if(BUILD_TESTS)
# Set up for unit testing using googletest
include(FetchContent)

# Disable installation of googletest into the install tree
set(INSTALL_GTEST
OFF
CACHE BOOL "" FORCE)

# Disable installation of gmock
set(INSTALL_GMOCK
OFF
CACHE BOOL "" FORCE)

# Download and unpack googletest at configure time
FetchContent_Declare(
googletest # Specify the commit you depend on and update it regularly.
URL https://github.com/google/googletest/archive/refs/tags/v1.16.0.zip)

# For Windows: Prevent overriding the parent project's compiler/linker
# settings
set(gtest_force_shared_crt
ON
CACHE BOOL "" FORCE)

# Add googletest directly to our build. This adds the following targets: *
# gtest * gtest_main
FetchContent_MakeAvailable(googletest)
# FetchContent_MakeAvailable(googlemock)

# Enable unit testing
enable_testing()

include(GoogleTest)

set(MEMORYCHECK_COMMAND_OPTIONS
"--tool=memcheck --leak-check=full --num-callers=50 --show-reachable=yes \
--suppressions=${PROJECT_SOURCE_DIR}/.valgrind.suppressions \
${EXTRA_MEMCHECK_OPTIONS}")
include(CTest)
endif()
include(FetchContent)
FetchContent_Declare(
oscms_cmake_helpers
GIT_REPOSITORY https://github.com/OpenSCMS/oscms-cmake-helpers.git
GIT_TAG main)
FetchContent_MakeAvailable(oscms_cmake_helpers)

# Find cppcheck
option(RUN_CPPCHECK "Enable cppcheck" ON)
if(RUN_CPPCHECK)
find_program(CPPCHECK cppcheck)
if(NOT CPPCHECK)
message(WARNING "cppcheck not found. Disabling cppcheck.")
set(RUN_CPPCHECK OFF)
else()
message(STATUS "Found cppcheck: ${CPPCHECK}")
set(CPPCHECK_OPTIONS
--language=c++
--inline-suppr
--platform=unix64
--enable=all
"--suppressions-list=${PROJECT_SOURCE_DIR}/.cppcheck-suppress"
--force
--error-exitcode=1)
endif()
endif()
oscms_enable_cppcheck()
oscms_enable_testing()

# Find OpenSSL
find_package(OpenSSL REQUIRED)
Expand Down Expand Up @@ -173,7 +58,13 @@ else()
endif()

# Build the common CODECS library
add_subdirectory(oscms-codecs-bridge EXCLUDE_FROM_ALL)
#
# We want our install target to only include the explorer executable, so we
# override any command line options and force SKIP_INSTALL to be set
set(SKIP_INSTALL
ON
CACHE BOOL "" FORCE)
add_subdirectory(oscms-codecs-bridge)

# add source files
file(GLOB sources "src/*.cpp" CMAKE_CONFIGURE_DEPENDS)
Expand Down Expand Up @@ -225,18 +116,28 @@ target_compile_definitions(

include(GNUInstallDirs)

# We don't respect the SKIP_INSTALL option ourselves
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})

if(RUN_CPPCHECK)
message(STATUS "Adding cppcheck custom command")

# The standard CPPCHECK_OPTIONS are defined in oscms-cmake-helpers
# specifically for the C stack, but this is the only C++ project. So
# manipulate them here by changing the `language` option while keeping the
# others the same.

list(TRANSFORM CPPCHECK_OPTIONS REPLACE "--language=c" "--language=c++"
OUTPUT_VARIABLE OUR_CPPCHECK_OPTIONS)

# we use a custom command to ensure that cppcheck is run after the build, sees
# all the source files at once, and stops the build if there are errors

add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND
"${CPPCHECK}" ARGS "${CPPCHECK_OPTIONS}"
"${CPPCHECK}" ARGS "${OUR_CPPCHECK_OPTIONS}"
# Don't analyze codecs
-i"${PROJECT_SOURCE_DIR}/oscms-codecs-bridge/"
"$<JOIN:$<TARGET_PROPERTY:oscms_codecs_api,INCLUDE_DIRECTORIES>,;-I>"
Expand All @@ -245,4 +146,8 @@ if(RUN_CPPCHECK)
COMMAND_EXPAND_LISTS)
endif()

# if(BUILD_TESTS) # Bring in the Unit tests add_subdirectory(tests) endif()
# Bring in any Unit tests

if(BUILD_TESTS)
# add_subdirectory(tests)
endif()
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ The list of repositories, and their relative submodule dependencies is as follow
- [etsi_ts103097-asn](<https://github.com/OpenSCMS/etsi_ts103097-asn>)
- [ieee1609dot2dot1-asn](<https://github.com/OpenSCMS/ieee1609dot2dot1-asn>)

The project also makes use of the [Cmake Helpers project](https://github.com/OpenSCMS/oscms-cmake-helpers.git)

### Building the code

All C code is built using `CMake` and the `CMake` scripts will enforce out-of-source builds.
Expand All @@ -139,6 +141,9 @@ The CMake script supports a common set of options and command line definitions.
| CMAKE_BUILD_TYPE | Debug | Defines the build type. Acceptable values are Debug or Release. This primarily affects debug symbols and optimization levels. |
| EXTRA_MEMCHECK_OPTIONS | empty | Allows the specification of additional arguments to `valgrind`|
| RUN_CPPCHECK | On | Enables or disables running `cppcheck` on all code during the build. |
| SKIP_INSTALL | Off | If set to On, suppresses generation of any `install` targets |

Note that this project does NOT respect the `SKIP_INSTALL` option itself, as it is a standalone CLI tool and you will probably want to install it at some time. It does, however, override anything specified on the command line when building the lower level projects. This avoids all the libraries and include files etc being included in the generated `make install` target.

### Installing

Expand Down
2 changes: 1 addition & 1 deletion oscms-codecs-bridge