1
- set (sources main.c)
1
+ set (sources main.c)
2
2
3
- set (elf_file ${app_name} .elf)
4
- set (bin_file ${app_name} .bin)
5
- set (hex_file ${app_name} .hex)
6
- set (map_file ${app_name} .map)
7
- set (lss_file ${app_name} .lss)
3
+ # GCC optimization level: use -O0 in debug build, otherwise -O2
4
+ if (CMAKE_BUILD_TYPE MATCHES Debug)
5
+ set (opt_level -O0)
6
+ else ()
7
+ set (opt_level -Os)
8
+ endif ()
9
+
10
+ # set some project constants
11
+ set (elf_file ${application_name} .elf)
12
+ set (bin_file ${application_name} .bin)
13
+ set (hex_file ${application_name} .hex)
14
+ set (map_file ${application_name} .map)
15
+ set (lss_file ${application_name} .lss)
8
16
9
- add_executable (${elf_file} ${sources} )
10
17
11
- target_compile_definitions (${elf_file} PUBLIC ${DEVICE_FAMILY} )
18
+ # add sources to elf file
19
+ add_executable (${elf_file} ${sources} )
12
20
target_link_libraries (${elf_file} PUBLIC stm32f4_system)
13
21
14
- # link with linker file
15
- target_link_libraries (${elf_file} PUBLIC -T${LINKER_SCRIPT} )
16
22
17
- target_link_libraries (${elf_file} PUBLIC
18
- "--specs=nano.specs \
19
- --specs=nosys.specs \
20
- -mthumb -mcpu=cortex-m4 \
21
- -Wl,--gc-sections \
22
- -Wl,-Map=${PROJECT_NAME} .map" )
23
-
24
- target_compile_options (${elf_file} PUBLIC
25
- -g
26
- -O0
27
- -mthumb
28
- -mcpu=cortex-m4
29
- -ffunction-sections
30
- -fdata-sections
31
- -D__STARTUP_CLEAR_BSS
32
- -D__START=main)
23
+ # set additional for compiler and linker: optimization and generate map file
24
+ set (additional_compiler_flags ${opt_level} )
25
+ set (additional_linker_flags -Wl,-Map=${map_file} ,--cref,--no -warn-mismatch)
26
+ target_compile_options (${elf_file} PRIVATE ${additional_compiler_flags} )
27
+ target_link_libraries (${elf_file} PRIVATE ${additional_linker_flags} )
28
+
29
+ # remove unused sections
30
+ target_link_libraries (${elf_file} PUBLIC "-g -Wl,--gc-sections" )
33
31
34
32
# link with linker file
35
33
target_link_libraries (${elf_file} PUBLIC -T${LINKER_SCRIPT} )
36
34
37
35
# show size of resulting firmware image
38
- add_custom_target (${elf_file} -size ALL DEPENDS ${elf_file} COMMAND ${ARM_SIZE_EXECUTABLE} -B ${elf_file} )
36
+ add_custom_target (${elf_file} -size DEPENDS ${elf_file} COMMAND ${ARM_SIZE_EXECUTABLE} -B ${elf_file} )
39
37
40
38
# generate extended listing
41
- add_custom_target (${lss_file} ALL DEPENDS ${elf_file} COMMAND ${ARM_OBJDUMP_EXECUTABLE} -S ${elf_file} > ${lss_file} )
39
+ add_custom_target (${lss_file} DEPENDS ${elf_file} COMMAND ${ARM_OBJDUMP_EXECUTABLE} -S ${elf_file} > ${lss_file} )
42
40
43
41
# create binary and hex files
44
- add_custom_target (${hex_file} ALL DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Oihex ${elf_file} ${hex_file} )
45
- add_custom_target (${bin_file} ALL DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Obinary ${elf_file} ${bin_file} )
42
+ add_custom_target (${hex_file} DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Oihex ${elf_file} ${hex_file} )
43
+ add_custom_target (${bin_file} DEPENDS ${elf_file} COMMAND ${ARM_OBJCOPY_EXECUTABLE} -Obinary ${elf_file} ${bin_file} )
46
44
add_custom_target (${application_name} ALL DEPENDS ${elf_file} -size ${bin_file} ${hex_file} ${lss_file} )
0 commit comments