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
138 changes: 14 additions & 124 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,135 +14,25 @@
#
# 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(
OscmsApi
OscmsAsn1cCodecs
VERSION 1.0.0
LANGUAGES C CXX) # CXX added for testig support

# set cmake build options
option(BUILD_TESTS "Build unit tests" ON)
option(RUN_CPPCHECK "Enable cppcheck" ON)
option(ENABLE_ASN1C_DEBUG "Enable ASN1C DEBUG output in the generated code" OFF)
LANGUAGES C CXX) # CXX added for testing support

# 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)
# Pull in the CMake helpers module
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)

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

# 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 ${EXTRA_MEMCHECK_OPTIONS}"
)
include(CTest)
endif()

# Find cppcheck
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()
# set additional cmake build options
option(ENABLE_ASN1C_DEBUG "Enable ASN1C DEBUG output in the generated code" OFF)

# Build the common API source
add_subdirectory(submodules/api)
Expand Down Expand Up @@ -215,7 +105,7 @@ if(RUN_CPPCHECK)
POST_BUILD
COMMAND
"${CPPCHECK}" ARGS "${CPPCHECK_OPTIONS}"
# Don't analyze geenerated code
# Don't analyze generated code
-i"${PROJECT_SOURCE_DIR}/submodules/asn1c-generated"
"$<JOIN:$<TARGET_PROPERTY:oscms_codecs_api,INCLUDE_DIRECTORIES>,;-I>"
${CODEC_SOURCES}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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 @@ -125,6 +127,7 @@ All CMake scripts support 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 |

This repository supports an additional option, `ENABLE_ASN1C_DEBUG`, which defaults to `Off'. If defined as 'On', it enables extensive debugging logs from the generated CODECs. This is only really useful as a last resort, when you suspect a fault in the generated code, as it will literally swamp you in logs.

Expand Down
2 changes: 1 addition & 1 deletion submodules/api
8 changes: 0 additions & 8 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,4 @@ target_compile_options(asn1c_codecs_tests PRIVATE -std=c++17 -g -O0 -fPIC)

add_test(NAME asn1c_codecs_tests COMMAND asn1c_codecs_tests)

# file(GLOB TEST_JSON CONFIGURE_DEPENDS "*.json") file(GLOB TEST_CSV
# CONFIGURE_DEPENDS "*.csv") file(GLOB TEST_TXT CONFIGURE_DEPENDS "*.txt")

# foreach(TEST_FILE ${TEST_JSON} ${TEST_CSV} ${TEST_TXT}) cmake_path(GET
# TEST_FILE FILENAME TEST_BASENAME) message(STATUS "Configuring TEST FILE:
# ${TEST_BASENAME}") configure_file(${TEST_BASENAME} ${TEST_BASENAME} COPYONLY)
# endforeach()

gtest_discover_tests(asn1c_codecs_tests)