makefile: Make -j works
[simavr] / Makefile.common
1 #
2 # This makefile take each "at*" file, extracts it's part name
3 # And compile it into an ELF binary.
4 # It also disassemble it for debugging purposes.
5
6 # The code is compiled "optimized" to the max.
7
8 # The wierd "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
9 # is used to tell the linker not to discard the .mmcu section,
10 # otherwise the --gc-sections will delete it.
11
12 #       Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
13 #
14 #       This file is part of simavr.
15 #
16 #       simavr is free software: you can redistribute it and/or modify
17 #       it under the terms of the GNU General Public License as published by
18 #       the Free Software Foundation, either version 3 of the License, or
19 #       (at your option) any later version.
20 #
21 #       simavr is distributed in the hope that it will be useful,
22 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
23 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 #       GNU General Public License for more details.
25 #
26 #       You should have received a copy of the GNU General Public License
27 #       along with simavr.  If not, see <http://www.gnu.org/licenses/>.
28
29 # get the first character of what the compiler says it is, unless it's 'x86_64' doh
30 ARCH = ${shell $(CC) -dumpmachine | sed -e 's/^x/i/' -e 's/\(.\).*/\1/'}
31
32 ifeq ($(ARCH), i)
33 CFLAGS  += -mfpmath=sse -msse2
34 endif
35
36 CFLAGS  += -g --std=gnu99
37 CFLAGS  += ${patsubst %,-I%,${subst :, ,${IPATH}}}
38 LDFLAGS += -lelf 
39
40 ifeq (${shell uname}, Darwin)
41 AVR_ROOT := "/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/"
42 AVR_INC := ${AVR_ROOT}/avr-4/
43 AVR := ${AVR_ROOT}/bin/avr-
44 LFLAGS += -L/opt/local/lib
45 else
46 AVR_ROOT := /usr/lib/avr
47 AVR_INC := ${AVR_ROOT}
48 AVR := avr-
49 CFLAGS +=  -fPIC
50 endif
51
52 CC ?= gcc
53 AR ?= ar
54 RANLIB ?= ranlib
55
56 # The code is compiled "optimized" to the max.
57
58 # The wierd "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
59 # is used to tell the linker not to discard the .mmcu section,
60 # otherwise the --gc-sections will delete it.
61
62 %.hex: %.axf
63         @${AVR}objcopy -j .text -j .data -j .eeprom -O ihex ${<} ${@}
64
65 %.s: %.axf
66         @${AVR}objdump -j .text -j .data -j .bss -d  ${<} > ${@}
67
68 # --mcall-prologues can be used here, but messes up debugging a little
69 %.axf: %.c 
70         @echo AVR-CC ${<}
71         @part=${<} ; part=$${part/_*}; \
72         ${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
73                         -mmcu=$$part \
74                         -DF_CPU=8000000 \
75                         -fno-inline-small-functions \
76                         -ffunction-sections -fdata-sections \
77                         -Wl,--relax,--gc-sections \
78                         -Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
79                         -I../include -I../../include \
80                         ${^} -o ${@}
81         @${AVR}size ${@}|sed '1d'
82
83 OBJ = obj-${shell $(CC) -dumpmachine}
84
85 # this rule has precedence
86 ${OBJ}/sim_%.o : cores/sim_%.c
87 ifeq ($(V),1)
88         $(CC) $(CFLAGS) -MMD \
89                 -I${AVR_INC}/include/ \
90                 $<  -c -o $@
91 else
92         @$(CC) $(CFLAGS) -MMD \
93                 -I${AVR_INC}/include/ \
94                 $<  -c -o $@
95         @echo CORE $<
96 endif
97
98 ${OBJ}/%.o: %.c
99 ifeq ($(V),1)
100         $(CC) $(CFLAGS) -MMD \
101                 $<  -c -o $@
102 else
103         @$(CC) $(CFLAGS) -MMD \
104                 $<  -c -o $@
105         @echo CC $<
106 endif
107
108 ${OBJ}/%.elf:
109 ifeq ($(V),1)
110         echo $^ / $<
111         $(CC) -MMD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
112 else
113         @echo LD $@
114         @$(CC) -MMD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
115 endif
116
117 obj: ${OBJ}
118  
119 ${OBJ}: 
120         @mkdir -p ${OBJ}
121
122 clean-${OBJ}:
123         rm -rf ${OBJ}
124
125 # include the dependency files generated by gcc, if any
126 -include ${wildcard ${OBJ}/*.d}