Skip to content

Commit 16bed53

Browse files
authored
Updated cmake-tutorial (#14)
1 parent daa1820 commit 16bed53

File tree

179 files changed

+1998
-2226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+1998
-2226
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
_builds
2+
hello-world
23
Testing
34

README.md

+160-232
Large diffs are not rendered by default.

examples/430/iar-cspy-430.cmake

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Example for creating a test for CTest
2+
# to execute the `IAR C-SPY Command-line Utility (cspybat.exe)`
3+
4+
function(iar_cspy_add_test TARGET TEST_NAME EXPECTED_OUTPUT)
5+
# Add a test for CTest
6+
add_test(NAME ${TEST_NAME}
7+
COMMAND ${TOOLKIT_DIR}/../common/bin/cspybat --silent
8+
# C-SPY drivers
9+
"${TOOLKIT_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}proc.dll"
10+
"${TOOLKIT_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}sim.dll"
11+
"--plugin=${TOOLKIT_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}bat.dll"
12+
--debug_file=$<TARGET_FILE:${TARGET}>
13+
# C-SPY macros settings
14+
"--macro=${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.mac"
15+
"--macro_param=testName=\"${TEST_NAME}\""
16+
"--macro_param=testExpected=${EXPECTED_OUTPUT}"
17+
# C-SPY backend setup
18+
--backend
19+
-p $<IF:$<BOOL:$<TARGET_PROPERTY:${TARGET},DDF>>,$<TARGET_PROPERTY:${TARGET},DDF>,${TOOLKIT_DIR}/config/debugger/msp430f149.ddf>
20+
--hwmul_base=0x130
21+
--hardware_multiplier=16
22+
--hwmult_type=1
23+
--iv_base=0xFFE0
24+
--odd_word_check
25+
--derivativeSim=MSP430F149
26+
-d sim )
27+
28+
# Set the test to interpret a C-SPY's message containing `PASS`
29+
set_tests_properties(${TEST_NAME} PROPERTIES PASS_REGULAR_EXPRESSION "PASS")
30+
endfunction()

examples/430/mix-c-asm/CMakeLists.txt

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
3+
# Set the project name, [description] and [version],
4+
# while enabling its required languages
5+
project(Example1
6+
DESCRIPTION "Mixing C and Assembly"
7+
VERSION 1.0.0
8+
LANGUAGES C ASM )
9+
10+
# Fallback option for generators other than `Ninja Multi-Config`
11+
if (NOT DEFINED CMAKE_BUILD_TYPE)
12+
set(CMAKE_BUILD_TYPE Debug)
13+
endif()
14+
15+
# Enable CTest
16+
enable_testing()
17+
18+
# Add the executable for the "mixLanguages" target
19+
add_executable(mixLanguages
20+
# Source files
21+
main.c
22+
mynum.asm )
23+
24+
# Set a preprocessor symbol, usable from "mixLanguages" target
25+
target_compile_definitions(mixLanguages PUBLIC USE_ASM=1)
26+
27+
# Set the compiler flags for the "mixLanguages" target
28+
target_compile_options(mixLanguages PRIVATE
29+
$<$<COMPILE_LANGUAGE:C>:--multiplier=16 --dlib_config ${TOOLKIT_DIR}/lib/dlib/dl430fn.h>
30+
-D__MSP430F149__ )
31+
32+
# Set the linker options for the "mixLanguages" target
33+
target_link_options(mixLanguages PRIVATE
34+
-f ${TOOLKIT_DIR}/config/linker/lnk430f149.xcl
35+
${TOOLKIT_DIR}/lib/dlib/dl430fn.r43
36+
# The `SHELL:` prefix prevents option de-duplication
37+
"SHELL:-D_DATA16_HEAP_SIZE=50"
38+
"SHELL:-D_STACK_SIZE=50"
39+
"SHELL:-D_DATA20_HEAP_SIZE=50" )
40+
41+
# Optional: test the project with C-SPY
42+
include(../iar-cspy-430.cmake)
43+
iar_cspy_add_test(mixLanguages test_mynum 42)
File renamed without changes.
File renamed without changes.
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Add the executable for the "myProgram" target,
2+
# specifying its source files
3+
add_executable (myProgram
4+
# Source files
5+
main.c )
6+
7+
# Set the properties for the "myProgram" target
8+
set_target_properties(myProgram PROPERTIES
9+
XCL "${TOOLKIT_DIR}/config/linker/lnk430f149.xcl"
10+
# C-SPY-related properties
11+
DDF "${TOOLKIT_DIR}/config/debugger/msp430f149.ddf" )
12+
13+
# Set the compiler flags for the target
14+
target_compile_options(myProgram PRIVATE
15+
$<$<COMPILE_LANGUAGE:C>:--multiplier=16 --double=32 --dlib_config ${TOOLKIT_DIR}/lib/dlib/dl430fn.h>
16+
-D__MSP430F149__ )
17+
18+
# Set the linker flags for the target
19+
target_link_options(myProgram PRIVATE
20+
-s __program_start
21+
# Create a map file from the target's UBROF
22+
-l $<TARGET_PROPERTY:NAME>.map
23+
# The `SHELL:` prefix prevents option de-duplication
24+
"SHELL:-D_DATA16_HEAP_SIZE=50"
25+
"SHELL:-D_STACK_SIZE=50"
26+
"SHELL:-D_DATA20_HEAP_SIZE=50"
27+
# Set the linker script
28+
-f $<TARGET_PROPERTY:XCL>
29+
# The `SHELL:` prefix prevents option de-duplication
30+
"SHELL:-f ${TOOLKIT_DIR}/config/linker/multiplier.xcl"
31+
-rt ${TOOLKIT_DIR}/lib/dlib/dl430fn.r43 )
32+
33+
# Link "myProgram" against the "myMath" library
34+
target_link_libraries(myProgram LINK_PUBLIC myMath)
35+
36+
# Optional: test the project with C-SPY
37+
include(../../iar-cspy-430.cmake)
38+
iar_cspy_add_test(myProgram test_add 42)
39+
iar_cspy_add_test(myProgram test_sub 38)
40+
iar_cspy_add_test(myProgram test_mul 80)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# The top-level CMakeLists.txt add this subdirectory
22
# This CMakeLists.txt builds the target "myMath" library
33
add_library(myMath
4-
src/add.c
5-
src/sub.c
6-
src/mul.c )
4+
add.c
5+
sub.c
6+
mul.c )
77

88
# Set the compiler flags for the "myMath" target
99
target_compile_options(myMath PUBLIC
@@ -14,5 +14,4 @@ target_compile_options(myMath PUBLIC
1414
# PUBLIC headers are used for building the library
1515
# PRIVATE sources, only used in this target
1616
target_include_directories(myMath
17-
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
18-
PRIVATE src )
17+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/.> )

examples/8051/iar-cspy-8051.cmake

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Example for creating a test for CTest
2+
# to execute the `IAR C-SPY Command-line Utility (cspybat.exe)`
3+
4+
function(iar_cspy_add_test TARGET TEST_NAME EXPECTED_OUTPUT)
5+
# Add a test for CTest
6+
add_test(NAME ${TEST_NAME}
7+
COMMAND ${TOOLKIT_DIR}/../common/bin/cspybat --silent
8+
# C-SPY drivers
9+
"${TOOLKIT_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}proc.dll"
10+
"${TOOLKIT_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}sim.dll"
11+
"--plugin=${TOOLKIT_DIR}/bin/${CMAKE_SYSTEM_PROCESSOR}bat.dll"
12+
--debug_file=$<TARGET_FILE:${TARGET}>
13+
# C-SPY macros settings
14+
"--macro=${CMAKE_CURRENT_SOURCE_DIR}/${TARGET}.mac"
15+
"--macro_param=testName=\"${TEST_NAME}\""
16+
"--macro_param=testExpected=${EXPECTED_OUTPUT}"
17+
# C-SPY backend setup
18+
--backend
19+
--proc_core=plain
20+
--proc_code_model=near
21+
--proc_nr_virtual_regs 8
22+
--proc_pdata_bank_reg_addr 0xA0
23+
--proc_dptr_nr_of=1
24+
--proc_data_model=small
25+
-p $<IF:$<BOOL:$<TARGET_PROPERTY:${TARGET},DDF>>,$<TARGET_PROPERTY:${TARGET},DDF>,${TOOLKIT_DIR}/config/devices/_generic/io8051.ddf>
26+
--proc_driver sim )
27+
28+
# Set the test to interpret a C-SPY's message containing `PASS`
29+
set_tests_properties(${TEST_NAME} PROPERTIES PASS_REGULAR_EXPRESSION "PASS")
30+
endfunction()
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
3+
# Set the project name, [description] and [version],
4+
# while enabling its required languages
5+
project(Example1
6+
DESCRIPTION "Mixing C and Assembly"
7+
VERSION 1.0.0
8+
LANGUAGES C ASM )
9+
10+
# Fallback option for generators other than `Ninja Multi-Config`
11+
if (NOT DEFINED CMAKE_BUILD_TYPE)
12+
set(CMAKE_BUILD_TYPE Debug)
13+
endif()
14+
15+
# Enable CTest
16+
enable_testing()
17+
18+
# Add the executable for the "mixLanguages" target
19+
add_executable(mixLanguages
20+
# Source files
21+
main.c
22+
mynum.asm )
23+
24+
# Set a preprocessor symbol, usable from "mixLanguages" target
25+
target_compile_definitions(mixLanguages PUBLIC USE_ASM=1)
26+
27+
# Set compile options for the target
28+
target_compile_options(mixLanguages PRIVATE
29+
$<$<COMPILE_LANGUAGE:C>:--core=plain --dptr=16,1 --data_model=large --code_model=near --calling_convention=xdata_reentrant --place_constants=data --nr_virtual_regs 8 --dlib --dlib_config ${TOOLKIT_DIR}/lib/dlib/dl8051Normal.h>
30+
$<$<COMPILE_LANGUAGE:ASM>:-v0 -D__CORE__=1 -D__CODE_MODEL__=1 -D__DATA_MODEL__=1 -D__CALLING_CONVENTION__=2 -D__NUMBER_OF_DPTRS__=1> )
31+
32+
# Set the link options for the target
33+
target_link_options(mixLanguages PRIVATE
34+
-rt
35+
-f ${TOOLKIT_DIR}/config/devices/_generic/lnk51ew_8051.xcl
36+
${TOOLKIT_DIR}/lib/dlib/dl-pli-nlxd-1e16x01n.r51
37+
# The `SHELL:` prefix prevents option de-duplication
38+
"SHELL:-D_NR_OF_BANKS=0"
39+
"SHELL:-D_CODEBANK_END=0"
40+
"SHELL:-D_CODEBANK_START=0"
41+
"SHELL:-D_NR_OF_VIRTUAL_REGISTERS=8"
42+
"SHELL:-D?PBANK=0xA0"
43+
"SHELL:-D_IDATA_STACK_SIZE=0x40"
44+
"SHELL:-D?ESP=0"
45+
"SHELL:-D?ESP_MASK=0"
46+
"SHELL:-D_EXTENDED_STACK_START=0"
47+
"SHELL:-D_EXTENDED_STACK_SIZE=0"
48+
"SHELL:-D_PDATA_STACK_SIZE=0x80"
49+
"SHELL:-D_XDATA_STACK_SIZE=0xEFF"
50+
"SHELL:-D_XDATA_HEAP_SIZE=0xFF"
51+
"SHELL:-D_FAR_HEAP_SIZE=0xFFF"
52+
"SHELL:-D_HUGE_HEAP_SIZE=0xFFF"
53+
"SHELL:-D_FAR22_HEAP_SIZE=0xFFF" )
54+
55+
# Optional: test the project with CTest and IAR C-SPY
56+
include(../iar-cspy-8051.cmake)
57+
iar_cspy_add_test(mixLanguages test_mynum 42)

examples/8051/mix-c-asm/main.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include "mynum.h"
3+
4+
int answer;
5+
6+
int main(void)
7+
{
8+
#if USE_ASM
9+
answer = mynum();
10+
#else
11+
answer = 10;
12+
#endif
13+
/* NDEBUG is set automatically for when
14+
bulding with -DCMAKE_BUILD_TYPE=Release */
15+
#ifndef NDEBUG
16+
printf("-- app debug output begin --\n");
17+
printf("mixLanguages v%d.%d.%d\n", 1, 0, 0);
18+
printf("The answer is: %d.\n",answer);
19+
printf("-- app debug output end --\n");
20+
#endif
21+
22+
return 0;
23+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
__param testName;
2+
__param testExpected;
3+
4+
__var _breakID;
5+
__var _result;
6+
7+
execUserSetup()
8+
{
9+
__message "C-SPY TEST: started...";
10+
__message "C-SPY TEST: When the `answer` variable is read, `checkanswer()` will execute.";
11+
_breakID = __setSimBreak("answer", "R", "checkAnswer()");
12+
}
13+
14+
Done()
15+
{
16+
__message "-- C-SPY TEST: Done()";
17+
if (_result == testExpected)
18+
{
19+
__message "-- C-SPY TEST:", testName, ". Result: PASS";
20+
} else {
21+
__message "-- C-SPY TEST:", testName, ". Expected: ", testExpected, " Result: FAIL";
22+
}
23+
}
24+
25+
checkAnswer()
26+
{
27+
__message "-- C-SPY TEST: checkAnswer()";
28+
if (testName == "test_mynum")
29+
{
30+
_result = answer;
31+
Done();
32+
}
33+
}

examples/mix-c-asm/sources/mynum-8051.asm renamed to examples/8051/mix-c-asm/mynum.asm

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
NAME mynum
2+
13
RSEG DOVERLAY:DATA:NOROOT(0)
24
RSEG IOVERLAY:IDATA:NOROOT(0)
35
RSEG ISTACK:IDATA:NOROOT(0)
@@ -15,4 +17,4 @@ mynum:
1517
MOV R3,#0x0
1618
RET
1719

18-
END
20+
END

examples/8051/mix-c-asm/mynum.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef MYNUM_H
2+
#define MYNUM_H
3+
extern int mynum();
4+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Add the executable for the "myProgram" target,
2+
# specifying its source files
3+
add_executable (myProgram
4+
# Source files
5+
main.c )
6+
7+
# Set the properties for the "myProgram" target
8+
set_target_properties(myProgram PROPERTIES
9+
XCL "${TOOLKIT_DIR}/config/devices/_generic/lnk51ew_8051.xcl"
10+
# C-SPY-related properties
11+
DDF "${TOOLKIT_DIR}/config/devices/_generic/io8051.ddf" )
12+
13+
# Set the compiler flags for the "myProgram" target
14+
target_compile_options(myProgram PRIVATE
15+
$<$<COMPILE_LANGUAGE:C>:--core=plain --dptr=16,1 --data_model=large --code_model=near --calling_convention=xdata_reentrant --place_constants=data --nr_virtual_regs 8 --dlib --dlib_config ${TOOLKIT_DIR}/lib/dlib/dl8051Normal.h> )
16+
17+
# Set the linker flags for the target
18+
target_link_options(myProgram PRIVATE
19+
-rt
20+
# Set the linker script
21+
-f $<TARGET_PROPERTY:XCL>
22+
${TOOLKIT_DIR}/lib/dlib/dl-pli-nlxd-1e16x01n.r51
23+
# Create a map file from the target's UBROF
24+
-l $<TARGET_PROPERTY:NAME>.map
25+
# The `SHELL:` prefix prevents option de-duplication
26+
"SHELL:-D_NR_OF_BANKS=0"
27+
"SHELL:-D_CODEBANK_END=0"
28+
"SHELL:-D_CODEBANK_START=0"
29+
"SHELL:-D_NR_OF_VIRTUAL_REGISTERS=8"
30+
"SHELL:-D?PBANK=0xA0"
31+
"SHELL:-D_IDATA_STACK_SIZE=0x40"
32+
"SHELL:-D?ESP=0"
33+
"SHELL:-D?ESP_MASK=0"
34+
"SHELL:-D_EXTENDED_STACK_START=0"
35+
"SHELL:-D_EXTENDED_STACK_SIZE=0"
36+
"SHELL:-D_PDATA_STACK_SIZE=0x80"
37+
"SHELL:-D_XDATA_STACK_SIZE=0xEFF"
38+
"SHELL:-D_XDATA_HEAP_SIZE=0xFF"
39+
"SHELL:-D_FAR_HEAP_SIZE=0xFFF"
40+
"SHELL:-D_HUGE_HEAP_SIZE=0xFFF"
41+
"SHELL:-D_FAR22_HEAP_SIZE=0xFFF" )
42+
43+
# Link "myProgram" against the "myMath" library
44+
target_link_libraries(myProgram LINK_PUBLIC myMath)
45+
46+
# Optional: test the project with CTest and IAR C-SPY
47+
include(../../iar-cspy-8051.cmake)
48+
iar_cspy_add_test(myProgram test_add 42)
49+
iar_cspy_add_test(myProgram test_sub 38)
50+
iar_cspy_add_test(myProgram test_mul 80)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# The top-level CMakeLists.txt add this subdirectory
22
# This CMakeLists.txt builds the target "myMath" library
33
add_library(myMath
4-
src/add.c
5-
src/sub.c
6-
src/mul.c )
4+
add.c
5+
sub.c
6+
mul.c )
77

88
# Set the compiler flags for the "myMath" target
99
target_compile_options(myMath PUBLIC
@@ -14,5 +14,4 @@ target_compile_options(myMath PUBLIC
1414
# PUBLIC headers are used for building the library
1515
# PRIVATE sources, only used in this target
1616
target_include_directories(myMath
17-
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
18-
PRIVATE src )
17+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/.> )

0 commit comments

Comments
 (0)