Skip to content

Commit 3088d59

Browse files
committed
changed so the linkerscript is passed through the preprocessor
1 parent 648d954 commit 3088d59

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

project/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# include the linkerscript generator needed to preprocess the linkerscripts
2+
include (${CMAKE_SOURCE_DIR}/targets/arm/linkerscript/linkerscript.cmake)
3+
14
# set the sources
25
set(SOURCES
36
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
@@ -40,8 +43,7 @@ target_link_libraries(klib_project PUBLIC target_cpu)
4043
target_link_libraries(klib_project PUBLIC m)
4144

4245
# link to the linkerscript of the target cpu
43-
target_link_options(klib_project PUBLIC "-T${TARGET_LINKERSCRIPT}")
44-
set_target_properties(klib_project PROPERTIES LINK_DEPENDS ${TARGET_LINKERSCRIPT})
46+
add_linkerscript(klib_project ${TARGET_LINKERSCRIPT})
4547

4648
# add the project directory to the include directories
4749
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# helper function to preprocess a linker script. This allows
2+
# using C preprocessor in the linkerscript files (including
3+
# macroes, conditionals, etc)
4+
function(add_linkerscript target linkerscript options)
5+
set(output "${CMAKE_BINARY_DIR}/linkerscript.ld")
6+
set(CURRENT_DIR "${CMAKE_SOURCE_DIR}/targets/arm/linkerscript")
7+
8+
# run the preprocessor on the linkerscript
9+
add_custom_command(
10+
OUTPUT ${output}
11+
# run the linkerscript through the C preprocessor. Force include the base linkerscript template.
12+
COMMAND ${CMAKE_C_COMPILER} -E -P -x c ${linkerscript} -o ${output} -include ${CURRENT_DIR}/linkerscript.ld ${options}
13+
DEPENDS ${linkerscript} ${CURRENT_DIR}/linkerscript.ld
14+
COMMENT "Preprocessing linker script ${linkerscript}"
15+
VERBATIM
16+
)
17+
18+
# create a custom target to generate the linkerscript
19+
add_custom_target(generate_linkerscript DEPENDS "${output}")
20+
add_dependencies(${target} generate_linkerscript)
21+
22+
# add the linkerscript to the target
23+
target_link_options(${target} PUBLIC "-T${output}")
24+
set_target_properties(${target} PROPERTIES LINK_DEPENDS ${output})
25+
endfunction()
26+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* macro to define the vectors section. Should be pointed to the correct memory region */
2+
#define VECTORS() \
3+
.vectors (READONLY) : \
4+
{ \
5+
. = ALIGN(4); \
6+
/* vector table */ \
7+
KEEP(*(.vectors .vectors.*)); \
8+
. = ALIGN(4); \
9+
}

0 commit comments

Comments
 (0)