Upgrade gcc4mbed project used by Smoothie.
[clinton/Smoothieware.git] / gcc4mbed / build / gcc4mbed.mk
index 2e6959f..08c0b29 100755 (executable)
 #                of the build directory which contains this gcc4mbed.mk file.
 #   LIBS_PREFIX: List of library/object files to prepend to mbed.ar capi.ar libs.
 #   LIBS_SUFFIX: List of library/object files to append to mbed.ar capi.ar libs.
-#   GCC4MBED_DELAYED_STDIO_INIT: Set to non-zero value to have intialization of
-#                                stdin/stdout/stderr delayed which will
-#                                shrink the size of the resulting binary if
-#                                APIs like printf(), scanf(), etc. aren't used.
 #   GCC4MBED_TYPE: Type of build to produce.  Allowed values are:
 #                  Debug - Build for debugging.  Disables optimizations and
 #                          links in debug MRI runtime.  Best debugging 
@@ -78,7 +74,6 @@ endif
 
 # Default variables.
 SRC ?= .
-GCC4MBED_DELAYED_STDIO_INIT ?= 0
 GCC4MBED_TYPE ?= Release
 MRI_BREAK_ON_INIT ?= 1
 MRI_UART ?= MRI_UART_MBED_USB
@@ -86,7 +81,7 @@ MRI_UART ?= MRI_UART_MBED_USB
 
 # Configure MRI variables based on GCC4MBED_TYPE build type variable.
 ifeq "$(GCC4MBED_TYPE)" "Release"
-OPTIMIZATION = 2
+OPTIMIZATION ?= 2
 MRI_ENABLE = 0
 MRI_SEMIHOST_STDIO ?= 0
 endif
@@ -100,7 +95,7 @@ endif
 
 
 ifeq "$(GCC4MBED_TYPE)" "Checked"
-OPTIMIZATION = 2
+OPTIMIZATION ?= 2
 MRI_ENABLE = 1
 MRI_SEMIHOST_STDIO ?= 1
 endif
@@ -108,16 +103,24 @@ endif
 MRI_INIT_PARAMETERS=$(MRI_UART)
 
 
+# Output Object Directory
+OUTDIR=LPC176x
+
 # List of sources to be compiled/assembled
 CSRCS = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c)
 ASRCS =  $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
 CPPSRCS = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp)
 
-# Add in the gcc4mbed shim sources that allow mbed code build under GCC
-CSRCS += $(GCC4MBED_DIR)/src/gcc4mbed.c $(GCC4MBED_DIR)/src/syscalls.c
-
 # List of the objects files to be compiled/assembled
-OBJECTS= $(CSRCS:.c=.o) $(ASRCS:.S=.o) $(CPPSRCS:.cpp=.o)
+OBJECTS = $(patsubst %.c,$(OUTDIR)/%.o,$(CSRCS)) $(patsubst %.S,$(OUTDIR)/%.o,$(ASRCS)) $(patsubst %.cpp,$(OUTDIR)/%.o,$(CPPSRCS))
+
+# Add in the GCC4MBED stubs which allow hooking in the MRI debug monitor.
+OBJECTS += $(OUTDIR)/gcc4mbed.o
+
+# List of the header dependency files, one per object file.
+DEPFILES = $(patsubst %.o,%.d,$(OBJECTS))
+
+# Linker script to be used.  Indicates what code should be placed where in memory.
 LSCRIPT=$(GCC4MBED_DIR)/build/mbed.ld
 
 # Location of external library and header dependencies.
@@ -126,45 +129,93 @@ EXTERNAL_DIR = $(GCC4MBED_DIR)/external
 # Include path which points to external library headers and to subdirectories of this project which contain headers.
 SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*)
 PROJINCS = $(sort $(dir $(SUBDIRS)))
-INCDIRS += $(PROJINCS) $(EXTERNAL_DIR)/mbed $(EXTERNAL_DIR)/mbed/LPC1768 $(EXTERNAL_DIR)/FATFileSystem $(GCC4MBED_DIR)/mri
+INCDIRS += $(PROJINCS) $(GCC4MBED_DIR)/mri $(EXTERNAL_DIR)/mbed $(EXTERNAL_DIR)/mbed/LPC1768
 
 # DEFINEs to be used when building C/C++ code
-DEFINES = -DTARGET_LPC1768 -DGCC4MBED_DELAYED_STDIO_INIT=$(GCC4MBED_DELAYED_STDIO_INIT)
-DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"' -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) 
-DEFINES += -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
+DEFINES = -DTARGET_LPC1768
+DEFINES += -DMRI_ENABLE=$(MRI_ENABLE) -DMRI_INIT_PARAMETERS='"$(MRI_INIT_PARAMETERS)"' 
+DEFINES += -DMRI_BREAK_ON_INIT=$(MRI_BREAK_ON_INIT) -DMRI_SEMIHOST_STDIO=$(MRI_SEMIHOST_STDIO)
 
 # Libraries to be linked into final binary
-LIBS = $(LIBS_PREFIX) $(GCC4MBED_DIR)/mri/mri.ar $(EXTERNAL_DIR)/mbed/LPC1768/mbed.ar $(EXTERNAL_DIR)/mbed/LPC1768/capi.ar $(EXTERNAL_DIR)/FATFileSystem/LPC1768/FATFileSystem.ar $(LIBS_SUFFIX)
+MBED_LIBS = $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libmbed.a $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/libcapi.a
+SYS_LIBS = -lstdc++ -lsupc++ -lm -lgcc -lc -lgcc -lc -lnosys
+LIBS = $(LIBS_PREFIX) 
+
+ifeq "$(MRI_ENABLE)" "1"
+LIBS += $(GCC4MBED_DIR)/mri/mri.ar
+endif
+
+LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/startup_LPC17xx.o
+LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/cmsis_nvic.o
+LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/core_cm3.o
+LIBS += $(EXTERNAL_DIR)/mbed/LPC1768/GCC_ARM/system_LPC17xx.o
+LIBS += $(MBED_LIBS)
+LIBS += $(SYS_LIBS)
+LIBS += $(LIBS_SUFFIX)
 
-#  Compiler Options
-GPFLAGS = -O$(OPTIMIZATION) -gstabs+3 -mcpu=cortex-m3 -mthumb -mthumb-interwork -fshort-wchar -ffunction-sections -fdata-sections -fpromote-loop-indices -Wall -Wextra -Wimplicit -Wcast-align -Wpointer-arith -Wredundant-decls -Wshadow -Wcast-qual -Wcast-align -fno-exceptions
+# Compiler flags used to enable creation of header dependencies.
+DEPFLAGS = -MMD -MP
+
+# Compiler Options
+GPFLAGS = -O$(OPTIMIZATION) -g -mcpu=cortex-m3 -mthumb -mthumb-interwork 
+GPFLAGS += -ffunction-sections -fdata-sections  -fno-exceptions 
 GPFLAGS += $(patsubst %,-I%,$(INCDIRS))
 GPFLAGS += $(DEFINES)
+GPFLAGS += $(DEPFLAGS)
+GPFLAGS += -Wall -Wextra -Wno-unused-parameter -Wcast-align -Wpointer-arith -Wredundant-decls -Wcast-qual -Wcast-align
+GCFLAGS = $(GPFLAGS)
+
+# Setup wraps for newlib read/writes to redirect to MRI debugger. 
+ifeq "$(MRI_ENABLE)" "1"
+MRI_WRAPS=,--wrap=_read,--wrap=_write,--wrap=semihost_connected
+else
+MRI_WRAP=
+endif
 
-LDFLAGS = -mcpu=cortex-m3 -mthumb -O$(OPTIMIZATION) -Wl,-Map=$(PROJECT).map,--cref,--gc-sections,--no-wchar-size-warning -T$(LSCRIPT) -L $(EXTERNAL_DIR)/gcc/LPC1768
+# Linker Options.
+LDFLAGS = -mcpu=cortex-m3 -mthumb -O$(OPTIMIZATION) -specs=$(GCC4MBED_DIR)/build/startfile.spec -Wl,-Map=$(OUTDIR)/$(PROJECT).map,--cref,--gc-sections,--wrap=_isatty$(MRI_WRAPS) -T$(LSCRIPT)  -L $(EXTERNAL_DIR)/gcc/LPC1768
 
 ASFLAGS = $(LISTING) -mcpu=cortex-m3 -mthumb -x assembler-with-cpp
 ASFLAGS += $(patsubst %,-I%,$(INCDIRS))
 
 #  Compiler/Assembler/Linker Paths
+GCC = arm-none-eabi-gcc
 GPP = arm-none-eabi-g++
 AS = arm-none-eabi-gcc
 LD = arm-none-eabi-g++
 OBJCOPY = arm-none-eabi-objcopy
 OBJDUMP = arm-none-eabi-objdump
 SIZE = arm-none-eabi-size
-REMOVE = rm
 
-# Switch to cs-rm on Windows and make sure that cmd.exe is used as shell.
-ifeq "$(MAKE)" "cs-make"
-REMOVE = cs-rm
+# Some tools are different on Windows in comparison to Unix.
+ifeq "$(OS)" "Windows_NT"
+REMOVE = del
 SHELL=cmd.exe
+REMOVE_DIR = rd /s /q
+MKDIR = mkdir
+QUIET=>nul 2>nul & exit 0
+else
+REMOVE = rm
+REMOVE_DIR = rm -r -f
+MKDIR = mkdir -p
+QUIET=> /dev/null 2>&1 ; exit 0
+endif
+
+# Create macro which will convert / to \ on Windows.
+ifeq "$(OS)" "Windows_NT"
+define convert-slash
+$(subst /,\,$1)
+endef
+else
+define convert-slash
+$1
+endef
 endif
 
 #########################################################################
 .PHONY: all clean deploy
 
-all:: $(PROJECT).hex $(PROJECT).bin $(PROJECT).disasm
+all:: $(PROJECT).hex $(PROJECT).bin $(OUTDIR)/$(PROJECT).disasm
 
 $(PROJECT).bin: $(PROJECT).elf
        $(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin
@@ -172,20 +223,24 @@ $(PROJECT).bin: $(PROJECT).elf
 $(PROJECT).hex: $(PROJECT).elf
        $(OBJCOPY) -R .stack -O ihex $(PROJECT).elf $(PROJECT).hex
        
-$(PROJECT).disasm: $(PROJECT).elf
-       $(OBJDUMP) -d $(PROJECT).elf >$(PROJECT).disasm
+$(OUTDIR)/$(PROJECT).disasm: $(PROJECT).elf
+       $(OBJDUMP) -d -f -M reg-names-std $(PROJECT).elf >$(OUTDIR)/$(PROJECT).disasm
        
-$(PROJECT).elf: $(LSCRIPT) $(OBJECTS) $(LIBS)
+$(PROJECT).elf: $(LSCRIPT) $(OBJECTS)
        $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(PROJECT).elf
        $(SIZE) $(PROJECT).elf
 
 clean:
-       $(REMOVE) -f $(OBJECTS)
-       $(REMOVE) -f $(PROJECT).hex
-       $(REMOVE) -f $(PROJECT).elf
-       $(REMOVE) -f $(PROJECT).map
-       $(REMOVE) -f $(PROJECT).bin
-       $(REMOVE) -f $(PROJECT).disasm
+       $(REMOVE) -f $(call convert-slash,$(OBJECTS)) $(QUIET)
+       $(REMOVE) -f $(call convert-slash,$(DEPFILES)) $(QUIET)
+       $(REMOVE_DIR) $(OUTDIR) $(QUIET)
+       $(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).map) $(QUIET)
+       $(REMOVE) -f $(call convert-slash,$(OUTDIR)/$(PROJECT).disasm) $(QUIET)
+       $(REMOVE) -f $(PROJECT).bin $(QUIET)
+       $(REMOVE) -f $(PROJECT).hex $(QUIET)
+       $(REMOVE) -f $(PROJECT).elf $(QUIET)
+
+-include $(DEPFILES)
 
 ifdef LPC_DEPLOY
 DEPLOY_COMMAND = $(subst PROJECT,$(PROJECT),$(LPC_DEPLOY))
@@ -197,13 +252,20 @@ endif
 #  Default rules to compile .c and .cpp file to .o
 #  and assemble .s files to .o
 
-.c.o :
-       $(GPP) $(GPFLAGS) -c $< -o $(<:.c=.o)
+$(OUTDIR)/gcc4mbed.o : $(GCC4MBED_DIR)/src/gcc4mbed.c
+       $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
+       $(GPP) $(GPFLAGS) -c $< -o $@
+
+$(OUTDIR)/%.o : %.cpp
+       $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
+       $(GPP) $(GPFLAGS) -c $< -o $@
 
-.cpp.o :
-       $(GPP) $(GPFLAGS) -c $< -o $(<:.cpp=.o)
+$(OUTDIR)/%.o : %.c
+       $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
+       $(GCC) $(GCFLAGS) -c $< -o $@
 
-.S.o :
-       $(AS) $(ASFLAGS) -c $< -o $(<:.S=.o)
+$(OUTDIR)/%.o : %.S
+       $(MKDIR) $(call convert-slash,$(dir $@)) $(QUIET)
+       $(AS) $(ASFLAGS) -c $< -o $@
 
 #########################################################################
\ No newline at end of file