Skip to content

Commit afcb5bd

Browse files
committed
Use CMake Helpers
1 parent 4e21e3f commit afcb5bd

2 files changed

Lines changed: 22 additions & 151 deletions

File tree

CMakeLists.txt

Lines changed: 10 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -14,154 +14,22 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17-
# Enforce out-of-source builds
18-
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
19-
message(
20-
FATAL_ERROR
21-
"In-source builds are not allowed. Please create a build directory and run CMake from there.\n"
22-
"You will need to remove CMakeCache.txt and CMakeFiles/ to clean up the generated files."
23-
)
24-
endif()
25-
26-
# Ensure one of the two acceptable build types are selected: DEBUG or RELEASE.
27-
# Default to DEBUG
28-
if(NOT CMAKE_BUILD_TYPE)
29-
# No build type specified, default to Debug and force it into the cache
30-
message(STATUS "No build type specified, defaulting to Debug")
31-
set(CMAKE_BUILD_TYPE
32-
Debug
33-
CACHE STRING "Build type" FORCE)
34-
else()
35-
# Check they chose an acceptable one
36-
set(valid_build_types "Debug" "Release")
37-
list(FIND valid_build_types ${CMAKE_BUILD_TYPE} _index)
38-
if(${_index} EQUAL -1)
39-
message(
40-
FATAL_ERROR
41-
"Unknown build type ${CMAKE_BUILD_TYPE}. Supported build types are Debug and Release"
42-
)
43-
endif()
44-
unset(valid_build_types)
45-
endif()
46-
4717
cmake_minimum_required(VERSION 3.28.3)
4818
project(
4919
OscmsApi
20+
DESCRIPTION "OpenSCMS Codecs Abstract API"
5021
VERSION 1.0.0
51-
LANGUAGES C CXX) # CXX added for testig support
52-
53-
# set cmake build options
54-
option(BUILD_TESTS "Build unit tests" ON)
55-
option(RUN_CPPCHECK "Enable cppcheck" ON)
56-
57-
# set compiler flags Standard compile options
58-
set(CMAKE_C_STANDARD 11)
59-
set(CMAKE_C_STANDARD_REQUIRED TRUE)
60-
set(C_EXTENSIONS ON) # Enable gnu11
61-
set(CMAKE_CXX_STANDARD 17)
62-
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
63-
set(CXX_EXTENSIONS ON) # Enable gnu11
64-
65-
# Enforce C compiler minimum version of 11 (which is what comes with Ubuntu
66-
# 22.04). CI jobs actually use gcc:12
67-
#
68-
# C++ is only used for unit tests, so we don't care about its version
69-
70-
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
71-
if(CMAKE_C_COMPILER_VERSION VERSION_LESS 11)
72-
message(
73-
FATAL_ERROR
74-
"C compiler version must be at least 11.0.0, you are running ${CMAKE_C_COMPILER_VERSION}. Please update your C compiler.\n"
75-
"You will need to remove CMakeCache.txt and CMakeFiles/ to clean up the generated files."
76-
)
77-
endif()
78-
else()
79-
message(
80-
FATAL_ERROR
81-
"C compiler must be GNU, version >= 11. Please update your C compiler.\n"
82-
"You will need to remove CMakeCache.txt and CMakeFiles/ to clean up the generated files."
83-
)
84-
endif()
85-
86-
# Find cppcheck
87-
if(RUN_CPPCHECK)
88-
find_program(CPPCHECK cppcheck)
89-
if(NOT CPPCHECK)
90-
message(WARNING "cppcheck not found. Disabling cppcheck.")
91-
set(RUN_CPPCHECK OFF)
92-
else()
93-
message(STATUS "Found cppcheck: ${CPPCHECK}")
94-
set(CPPCHECK_OPTIONS
95-
--language=c
96-
--inline-suppr
97-
--platform=unix64
98-
--enable=all
99-
"--suppressions-list=${PROJECT_SOURCE_DIR}/.cppcheck-suppress"
100-
--force
101-
--error-exitcode=1)
102-
endif()
103-
endif()
22+
LANGUAGES C CXX) # CXX added for testing support
10423

105-
if(BUILD_TESTS)
106-
# Set up for unit testing using googletest
107-
include(FetchContent)
108-
109-
# Disable installation of googletest into the install tree
110-
set(INSTALL_GTEST
111-
OFF
112-
CACHE BOOL "" FORCE)
113-
114-
# Disable installation of gmock
115-
set(INSTALL_GMOCK
116-
OFF
117-
CACHE BOOL "" FORCE)
118-
119-
# Download and unpack googletest at configure time
120-
FetchContent_Declare(
121-
googletest # Specify the commit you depend on and update it regularly.
122-
URL https://github.com/google/googletest/archive/refs/tags/v1.16.0.zip)
123-
124-
# For Windows: Prevent overriding the parent project's compiler/linker
125-
# settings
126-
set(gtest_force_shared_crt
127-
ON
128-
CACHE BOOL "" FORCE)
129-
130-
# Add googletest directly to our build. This adds the following targets: *
131-
# gtest * gtest_main
132-
FetchContent_MakeAvailable(googletest)
133-
# FetchContent_MakeAvailable(googlemock)
134-
135-
# Enable unit testing
136-
enable_testing()
24+
include(FetchContent)
25+
FetchContent_Declare(
26+
oscms_cmake_helpers
27+
GIT_REPOSITORY https://github.com/OpenSCMS/oscms-cmake-helpers.git
28+
GIT_TAG main)
29+
FetchContent_MakeAvailable(oscms_cmake_helpers)
13730

138-
include(GoogleTest)
139-
140-
set(MEMORYCHECK_COMMAND_OPTIONS
141-
"--tool=memcheck --leak-check=full --num-callers=50 --show-reachable=yes ${EXTRA_MEMCHECK_OPTIONS}"
142-
)
143-
include(CTest)
144-
endif()
145-
146-
# Ensure the explicit_bzero function is available
147-
include(CheckSourceCompiles)
148-
149-
check_source_compiles(
150-
C
151-
"
152-
#include <string.h>
153-
int main() {
154-
char buf[10];
155-
explicit_bzero(buf, 10);
156-
return 0;
157-
}
158-
"
159-
HAVE_EXPLICIT_BZERO)
160-
161-
if(NOT HAVE_EXPLICIT_BZERO)
162-
message(
163-
FATAL_ERROR "Target platform does not provide explicit_bzero() function")
164-
endif()
31+
oscms_enable_cppcheck()
32+
oscms_enable_testing()
16533

16634
# add source files
16735
file(GLOB SOURCES "src/*.c" CMAKE_CONFIGURE_DEPENDS)

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ At present, the API is restricted to only those entities sent or received by the
3131
<!-- omit from toc -->
3232
## Table of Contents
3333

34-
- [Development](#development)
35-
- [Installing Dependencies](#installing-dependencies)
36-
- [Using Docker](#using-docker)
37-
- [Getting the Code](#getting-the-code)
38-
- [Building the code](#building-the-code)
39-
- [Running Unit Tests](#running-unit-tests)
40-
- [Running Valgrind](#running-valgrind)
41-
- [Contributing](#contributing)
42-
- [License](#license)
34+
* [Development](#development)
35+
* [Installing Dependencies](#installing-dependencies)
36+
* [Using Docker](#using-docker)
37+
* [Getting the Code](#getting-the-code)
38+
* [Building the code](#building-the-code)
39+
* [Running Unit Tests](#running-unit-tests)
40+
* [Running Valgrind](#running-valgrind)
41+
* [Contributing](#contributing)
42+
* [License](#license)
4343

4444
## Development
4545

@@ -107,6 +107,8 @@ The list of repositories, and their relative submodule dependencies, is as follo
107107
* [etsi_ts103097-asn](<https://github.com/OpenSCMS/etsi_ts103097-asn>)
108108
* [ieee1609dot2dot1-asn](<https://github.com/OpenSCMS/ieee1609dot2dot1-asn>)
109109

110+
The project also makes use of the [Cmake Helpers project](https://github.com/OpenSCMS/oscms-cmake-helpers.git)
111+
110112
### Building the code
111113

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

134137
### Running Unit Tests
135138

0 commit comments

Comments
 (0)