Rakefile: use gcc as linker instead of ld to profit from library finding

This commit is contained in:
King Kévin 2018-06-08 13:40:38 +02:00
parent c6f78ad2ee
commit 2da84d9063
1 changed files with 13 additions and 16 deletions

View File

@ -30,8 +30,8 @@ SRC_DIRS = [".", "lib"]
# cross-compiler environment
PREFIX = ENV["PREFIX"] || "arm-none-eabi"
CC = PREFIX+"-gcc"
#CC = "clang -target #{PREFIX}" # to use clang instead of gcc
LD = PREFIX+"-ld"
# LD would be the proper linker, but gcc comes with essential library path finder
LD = PREFIX+"-gcc"
AR = PREFIX+"-ar"
AS = PREFIX+"-as"
OBJCOPY = PREFIX+"-objcopy"
@ -64,24 +64,21 @@ cflags << "-DSTM32F1 -D#{BOARD}"
cflags = cflags.compact*' '
# linker flags
ldflags = [ENV["LDFLAGS"]]
ldflags = []
# build static binary (no shared libraries on the micro-controller)
ldflags << "-static"
# don't include the system start files
ldflags << "-nostartfiles"
# linker specific flags
ldflags_linker = [ENV["LDFLAGS"]]
# only keep used sections
ldflags << "--gc-sections"
# add standard libraries (for libc, libm, libnosys, libgcc) because we don't use arm-none-eabi-gcc to locate them
library_paths = ["/usr/arm-none-eabi/lib/armv7-m/", "/usr/lib/arm-none-eabi/lib/armv7-m/", "/usr/lib/gcc/arm-none-eabi/*/armv7-m/"]
# add libopencm3
library_paths += [LIBOPENCM3_LIB]
# include libraries in flags
ldflags += library_paths.collect {|library_path| "--library-path #{library_path}"}
ldflags *= ' '
# used libraries (gcc provides the ARM ABI)
ldlibs = [STM32F1_LIB, "m", "c", "nosys", "gcc"]
ldlibs = ldlibs.collect {|library| "--library #{library}"}
ldlibs *= ' '
ldflags_linker = ["--gc-sections"]
# add libopencm3 libraries
library_paths = [LIBOPENCM3_LIB]
# project libraries
ldlibs = [STM32F1_LIB]
# general libraries (gcc provides the ARM ABI)
ldlibs_linker = ["m", "c", "nosys", "gcc"]
# target micro-controller information (ARM Cortex-M3 supports thumb and thumb2, but does not include a floating point unit)
archflags = "-mthumb -mcpu=cortex-m3 -msoft-float"
@ -155,7 +152,7 @@ end
desc "link binary"
rule '.elf' => ['.o', proc{|f| dependencies(f)}, '.ld', "#{LIBOPENCM3_LIB}/lib#{STM32F1_LIB}.a"] do |t|
sh "#{LD} #{ldflags} --script #{t.name.ext('ld')} #{t.prerequisites[0..-3].join(' ')} #{ldlibs} -o #{t.name}"
sh "#{LD} #{archflags} #{ldflags.join(' ')} #{t.prerequisites[0..-3].join(' ')} -T#{t.name.ext('ld')} #{ldflags_linker.collect{|flag| "-Wl,"+flag}.join(' ')} #{library_paths.collect{|path| "-L"+path}.join(' ')} #{ldlibs.collect{|lib| "-l"+lib}.join(' ')} -Wl,--start-group #{ldlibs_linker.collect{|lib| "-l"+lib}.join(' ')} -Wl,--end-group --output #{t.name}"
sh "size #{t.name}"
end