From 2da84d90638723f673873f6e4b26ecf24186da60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Fri, 8 Jun 2018 13:40:38 +0200 Subject: [PATCH] Rakefile: use gcc as linker instead of ld to profit from library finding --- Rakefile | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/Rakefile b/Rakefile index 271eb17..4693088 100644 --- a/Rakefile +++ b/Rakefile @@ -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