initial commit
[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 # Example makefile:
34 # PROJECT=HelloWorld
35 # SRC=.
36 # GCC4MBED_DIR=../..
37 # LIBS_PREFIX=../agutil/agutil.ar
38 # LIBS_SUFFIX=
39 #
40 # include ../../build/gcc4mbed.mk
41 #
42 ###############################################################################
43
44 # Default project source to be located in current directory.
45 ifndef SRC
46 SRC=./src
47 endif
48
49 # List of sources to be compiled/assembled
50 CSRCS = $(wildcard $(SRC)/*.c) $(wildcard $(SRC)/*/*.c) $(wildcard $(SRC)/*/*/*.c) $(wildcard $(SRC)/*/*/*/*.c)
51 ASRCS = $(wildcard $(SRC)/*.S) $(wildcard $(SRC)/*/*.S) $(wildcard $(SRC)/*/*/*.S) $(wildcard $(SRC)/*/*/*/*.S)
52 CPPSRCS = $(wildcard $(SRC)/*.cpp) $(wildcard $(SRC)/*/*.cpp) $(wildcard $(SRC)/*/*/*.cpp) $(wildcard $(SRC)/*/*/*/*.cpp)
53
54 # Add in the gcc4mbed shim sources that allow mbed code build under GCC
55 CSRCS += $(GCC4MBED_DIR)/src/gcc4mbed.c $(GCC4MBED_DIR)/src/syscalls.c
56
57 # List of the objects files to be compiled/assembled
58 OBJECTS= $(CSRCS:.c=.o) $(ASRCS:.S=.o) $(CPPSRCS:.cpp=.o)
59 LSCRIPT=$(GCC4MBED_DIR)/build/mbed.ld
60
61 # Location of external library and header dependencies.
62 EXTERNAL_DIR = $(GCC4MBED_DIR)/external
63
64 # Include path
65 INCDIRS += $(EXTERNAL_DIR)/mbed $(EXTERNAL_DIR)/mbed/LPC1768 $(EXTERNAL_DIR)/FATFileSystem $(SRC)
66
67 # DEFINEs to be used when building C/C++ code
68 DEFINES = -DTARGET_LPC1768
69
70 # Libraries to be linked into final binary
71 LIBS = $(LIBS_PREFIX) $(EXTERNAL_DIR)/mbed/LPC1768/mbed.ar $(EXTERNAL_DIR)/mbed/LPC1768/capi.ar $(EXTERNAL_DIR)/FATFileSystem/LPC1768/FATFileSystem.ar $(LIBS_SUFFIX)
72
73 # Optimization level
74 OPTIMIZATION = 2
75
76 # Compiler Options
77 GPFLAGS = -O$(OPTIMIZATION) -gdwarf-2 -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
78 GPFLAGS += $(patsubst %,-I%,$(INCDIRS))
79 GPFLAGS += $(DEFINES)
80
81 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
82
83 ASFLAGS = $(LISTING) -mcpu=cortex-m3 -mthumb -x assembler-with-cpp
84 ASFLAGS += $(patsubst %,-I%,$(INCDIRS))
85
86 # Compiler/Assembler/Linker Paths
87 GPP = arm-none-eabi-g++
88 AS = arm-none-eabi-gcc
89 LD = arm-none-eabi-g++
90 OBJCOPY = arm-none-eabi-objcopy
91 OBJDUMP = arm-none-eabi-objdump
92 REMOVE = rm -f
93 SIZE = arm-none-eabi-size
94
95 #########################################################################
96
97 all:: $(PROJECT).hex $(PROJECT).bin $(PROJECT).disasm
98
99 $(PROJECT).bin: $(PROJECT).elf
100 $(OBJCOPY) -O binary $(PROJECT).elf $(PROJECT).bin
101
102 $(PROJECT).hex: $(PROJECT).elf
103 $(OBJCOPY) -R .stack -O ihex $(PROJECT).elf $(PROJECT).hex
104
105 $(PROJECT).disasm: $(PROJECT).elf
106 $(OBJDUMP) -d $(PROJECT).elf >$(PROJECT).disasm
107
108 $(PROJECT).elf: $(LSCRIPT) $(OBJECTS)
109 $(LD) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(PROJECT).elf
110 $(SIZE) $(PROJECT).elf
111
112 stats: $(PROJECT).elf
113 $(SIZE) $(PROJECT).elf
114
115 clean:
116 $(REMOVE) $(OBJECTS)
117 $(REMOVE) $(PROJECT).hex
118 $(REMOVE) $(PROJECT).elf
119 $(REMOVE) $(PROJECT).map
120 $(REMOVE) $(PROJECT).bin
121 $(REMOVE) $(PROJECT).disasm
122
123 #########################################################################
124 # Default rules to compile .c and .cpp file to .o
125 # and assemble .s files to .o
126
127 .c.o :
128 $(GPP) $(GPFLAGS) -c $< -o $(<:.c=.o)
129
130 .cpp.o :
131 $(GPP) $(GPFLAGS) -c $< -o $(<:.cpp=.o)
132
133 .S.o :
134 $(AS) $(ASFLAGS) -c $< -o $(<:.S=.o)
135
136 #########################################################################