Skip to content

Commit 1988805

Browse files
committed
Updated examples/version
1 parent 58affed commit 1988805

3 files changed

Lines changed: 77 additions & 28 deletions

File tree

examples/version/CMakeLists.txt

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
cmake_minimum_required(VERSION 3.20)
22

3-
# TODO 1: Add the `project()`'s `VERSION` and set it to `2.7.1`
43
project(Project LANGUAGES C)
54

6-
add_executable(version)
5+
add_executable(target)
76

8-
target_sources(version PRIVATE main.c)
7+
target_sources(target PRIVATE main.c)
98

10-
# TODO 2: Configure `version.h.in` to generate `version.h`
11-
configure_file()
9+
# TODO 1: Set the target's `VERSION` to 1.2.3
1210

13-
# TODO 3: Add the Project Binary Directory to the target's include directories
14-
target_include_directories()
1511

16-
target_compile_options(version PRIVATE --cpu=cortex-m4)
1712

18-
target_link_options(version PRIVATE
13+
14+
# TODO 2: Expose the target's `VERSION` to the application
15+
16+
17+
18+
19+
20+
21+
# TODO 3: Configure `version.h` from `version.h.in`
22+
23+
24+
25+
26+
27+
# TODO 4: Add `build/include/` to target's include directories
28+
29+
30+
target_compile_options(target PRIVATE
31+
--cpu=cortex-m4
32+
--no_wrap_diagnostics
33+
)
34+
35+
target_link_options(target PRIVATE
1936
--cpu=cortex-m4
2037
--semihosting)
2138

@@ -24,17 +41,27 @@ enable_testing()
2441
cmake_path(GET CMAKE_C_COMPILER PARENT_PATH BIN_DIR)
2542
cmake_path(GET BIN_DIR PARENT_PATH TOOLKIT_DIR)
2643
cmake_path(GET TOOLKIT_DIR FILENAME TOOLKIT)
27-
add_test(NAME version-test
28-
COMMAND ${BIN_DIR}/../../common/bin/CSpyBat
29-
# C-SPY drivers for the Arm simulator via command line interface
30-
${BIN_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${TOOLKIT}PROC${CMAKE_SHARED_LIBRARY_SUFFIX}
31-
${BIN_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${TOOLKIT}SIM2${CMAKE_SHARED_LIBRARY_SUFFIX}
32-
--plugin=${BIN_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${TOOLKIT}LibsupportUniversal${CMAKE_SHARED_LIBRARY_SUFFIX}
33-
# The target executable (built with debug information)
34-
--debug_file=$<TARGET_FILE:version>
35-
# C-SPY driver options
36-
--backend
37-
--cpu=cortex-m4
38-
--semihosting)
39-
40-
set_tests_properties(version-test PROPERTIES PASS_REGULAR_EXPRESSION "2.7.1")
44+
45+
macro(iar_cspysim TESTNAME EXPECTED)
46+
add_test(NAME ${TESTNAME}-test
47+
COMMAND ${BIN_DIR}/../../common/bin/CSpyBat
48+
# C-SPY drivers for the Arm simulator via command line interface
49+
${BIN_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${TOOLKIT}PROC${CMAKE_SHARED_LIBRARY_SUFFIX}
50+
${BIN_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${TOOLKIT}SIM2${CMAKE_SHARED_LIBRARY_SUFFIX}
51+
--plugin=${BIN_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}${TOOLKIT}LibsupportUniversal${CMAKE_SHARED_LIBRARY_SUFFIX}
52+
# The target executable (built with debug information)
53+
--debug_file=$<TARGET_FILE:target>
54+
# C-SPY driver options
55+
--backend
56+
--cpu=cortex-m4
57+
--semihosting
58+
)
59+
set_tests_properties(${TESTNAME}-test PROPERTIES
60+
PASS_REGULAR_EXPRESSION ${EXPECTED}
61+
)
62+
endmacro()
63+
64+
# Add tests for the string-formatted version and for the integer-formatted version
65+
iar_cspysim("string-version" "String version: $<TARGET_PROPERTY:target,VERSION>")
66+
iar_cspysim("integer-version" "Integer version: $<TARGET_PROPERTY:target,VERSION>")
67+

examples/version/main.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
#include "version.h"
44

5+
#if !defined(App_VERSION_MAJOR)
6+
#define App_VERSION_MAJOR 0
7+
#warning "App_VERSION_MAJOR defined but empty. Fallback to 0."
8+
#endif
9+
10+
#if !defined(App_VERSION_MINOR)
11+
#define App_VERSION_MINOR 0
12+
#warning "App_VERSION_MINOR defined but empty. Fallback to 0."
13+
#endif
14+
15+
#if !defined(App_VERSION_PATCH)
16+
#define App_VERSION_PATCH 0
17+
#warning "App_VERSION_PATCH defined but empty. Fallback to 0."
18+
#endif
19+
20+
#if !defined(App_VERSION)
21+
#define App_VERSION "0.0.0"
22+
#warning "App_VERSION_PATCH defined but empty. Fallback to 0.0.0."
23+
#endif
24+
525
void main() {
6-
printf("%d.%d.%d\n", Project_VERSION_MAJOR, Project_VERSION_MINOR, Project_VERSION_PATCH);
26+
printf("Integer version: %d.%d.%d\n", App_VERSION_MAJOR, App_VERSION_MINOR, App_VERSION_PATCH);
27+
printf("String version: %s\n", App_VERSION);
728
}

examples/version/version.h.in

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

3-
// TODO 4: Define the C macros with the CMake placeholders for version
4-
#define Project_VERSION_MAJOR
5-
#define Project_VERSION_MINOR
6-
#define Project_VERSION_PATCH
3+
// TODO 5: Import CMake's version variables as preprocessor-defined symbols
4+
5+
6+
7+

0 commit comments

Comments
 (0)