major bug fixes, way too many to enumerate
[clinton/Smoothieware.git] / gcc4mbed / build / gcc4mbed.mk
1 #Copyright (C) 2011 by Sagar G V
2 #
3 #Permission is hereby granted, free of charge, to any person obtaining a copy
4 #of this software and associated documentation files (the "Software"), to deal
5 #in the Software without restriction, including without limitation the rights
6 #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 #copies of the Software, and to permit persons to whom the Software is
8 #furnished to do so, subject to the following conditions:
9 #
10 #The above copyright notice and this permission notice shall be included in
11 #all copies or substantial portions of the Software.
12 #
13 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 #THE SOFTWARE.
20 #
21 # Updates:
22 # Arthur Wolf & Adam Green in 2011 - Updated to work with mbed.
23 ###############################################################################
24 # USAGE:
25 # Variables that must be defined in including makefile.
26 # PROJECT: Name to be given to the output binary for this project.
27 # SRC: The root directory for the sources of your project.
28 # GCC4MED_DIR: The root directory for where the gcc4mbed sources are located
29 # in your project. This should point to the parent directory
30 # of the build directory which contains this gcc4mbed.mk file.
31 # LIBS_PREFIX: List of library/object files to prepend to mbed.ar capi.ar libs.
32 # LIBS_SUFFIX: List of library/object files to append to mbed.ar capi.ar libs.
33 # GCC4MBED_DELAYED_STDIO_INIT: Set to non-zero value to have intialization of
34 # stdin/stdout/stderr delayed which will
35 # shrink the size of the resulting binary if
36 # APIs like printf(), scanf(), etc. aren't used.
37 # Example makefile:
38 # PROJECT=HelloWorld
39 # SRC=.
40 # GCC4MBED_DIR=../..
41 # LIBS_PREFIX=../agutil/agutil.ar
42 # LIBS_SUFFIX=
43 #
44 # include ../../build/gcc4mbed.mk
45 #
46 ###############################################################################
47
48 # Default project source to be located in current directory.
49 ifndef SRC
50 SRC=.
51 endif
52
53 # Default the init of stdio/stdout/stderr to occur before global constructors.
54 ifndef GCC4MBED_DELAYED_STDIO_INIT
55 GCC4MBED_DELAYED_STDIO_INIT=0
56 endif
57
58 # List of sources to be compiled/assembled
59 CSRCS = $(wildcard $(SRC)/*.c $(SRC)/*/*.c $(SRC)/*/*/*.c $(SRC)/*/*/*/*.c $(SRC)/*/*/*/*/*.c)
60 ASRCS = $(wildcard $(SRC)/*.S $(SRC)/*/*.S $(SRC)/*/*/*.S $(SRC)/*/*/*/*.S $(SRC)/*/*/*/*/*.S)
61 CPPSRCS = $(wildcard $(SRC)/*.cpp $(SRC)/*/*.cpp $(SRC)/*/*/*.cpp $(SRC)/*/*/*/*.cpp $(SRC)/*/*/*/*/*.cpp)
62
63 # Add in the gcc4mbed shim sources that allow mbed code build under GCC
64 CSRCS += $(GCC4MBED_DIR)/src/gcc4mbed.c $(GCC4MBED_DIR)/src/syscalls.c
65
66 # List of the objects files to be compiled/assembled
67 OBJECTS= $(CSRCS:.c=.o) $(ASRCS:.S=.o) $(CPPSRCS:.cpp=.o)
68 LSCRIPT=$(GCC4MBED_DIR)/build/mbed.ld
69
70 # Location of external library and header dependencies.
71 EXTERNAL_DIR = $(GCC4MBED_DIR)/external
72
73 # Include path which points to external library headers and to subdirectories of this project which contain headers.
74 SUBDIRS = $(wildcard $(SRC)/* $(SRC)/*/* $(SRC)/*/*/* $(SRC)/*/*/*/* $(SRC)/*/*/*/*/*)
75 PROJINCS = $(sort $(dir $(SUBDIRS)))
76 INCDIRS += $(PROJINCS) $(EXTERNAL_DIR)/mbed $(EXTERNAL_DIR)/mbed/LPC1768 $(EXTERNAL_DIR)/FATFileSystem $(GCC4MBED_DIR)/mri
77
78 # DEFINEs to be used when building C/C++ code
79 DEFINES = -DTARGET_LPC1768 -DGCC4MBED_DELAYED_STDIO_INIT=$(GCC4MBED_DELAYED_STDIO_INIT)
80
81 # Libraries to be linked into final binary
82 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)
83
84 # Optimization level
85 OPTIMIZATION = 2
86
87 # Compiler Options
88 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
89 GPFLAGS += $(patsubst %,-I%,$(INCDIRS))
90 GPFLAGS += $(DEFINES)
91
92 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
93
94 ASFLAGS = $(LISTING) -mcpu=cortex-m3 -mthumb -x assembler-with-cpp
95 ASFLAGS += $(patsubst %,-I%,$(INCDIRS))
96
97 # Compiler/Assembler/Linker Paths
98 GPP = arm-none-eabi-g++
99 AS = arm-none-eabi-gcc
100 LD = arm-none-eabi-g++
101 OBJCOPY = arm-none-eabi-objcopy
102 OBJDUMP = arm-none-eabi-objdump
103 SIZE = arm-none-eabi-size
104 REMOVE = rm
105
106 # Switch to cs-rm on Windows and make sure that cmd.exe is used as shell.
107 ifeq "$(MAKE)" "cs-make"
108 REMOVE = cs-rm
109 SHELL=cmd.exe
110 endif
111
112 #########################################################################
113 .PHONY: all clean deploy
114
115 all:: $(PROJECT).hex $(PROJECT).bin $(PROJECT).disasm
116
117 $(PROJECT).bin: $(PROJECT).elf
118 $(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin
119
120 $(PROJECT).hex: $(PROJECT).elf
121 $(OBJCOPY) -R .stack -O ihex $(PROJECT).elf $(PROJECT).hex
122
123 $(PROJECT).disasm: $(PROJECT).elf
124 $(OBJDUMP) -d $(PROJECT).elf >$(PROJECT).disasm
125
126 $(PROJECT).elf: $(LSCRIPT) $(OBJECTS) $(LIBS)
127 $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(PROJECT).elf
128 $(SIZE) $(PROJECT).elf
129
130 clean:
131 $(REMOVE) -f $(OBJECTS)
132 $(REMOVE) -f $(PROJECT).hex
133 $(REMOVE) -f $(PROJECT).elf
134 $(REMOVE) -f $(PROJECT).map
135 $(REMOVE) -f $(PROJECT).bin
136 $(REMOVE) -f $(PROJECT).disasm
137
138 ifdef LPC_DEPLOY
139 DEPLOY_COMMAND = $(subst PROJECT,$(PROJECT),$(LPC_DEPLOY))
140 deploy:
141 $(DEPLOY_COMMAND)
142 endif
143
144 #########################################################################
145 # Default rules to compile .c and .cpp file to .o
146 # and assemble .s files to .o
147
148 .c.o :
149 $(GPP) $(GPFLAGS) -c $< -o $(<:.c=.o)
150
151 .cpp.o :
152 $(GPP) $(GPFLAGS) -c $< -o $(<:.cpp=.o)
153
154 .S.o :
155 $(AS) $(ASFLAGS) -c $< -o $(<:.S=.o)
156
157 #########################################################################