mingw: make simavr compilable with MinGW
[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 weird "-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 # simavr directory
30 SIMAVR          := ${shell for p in . .. ../.. ../../..;do test -d $$p/simavr/sim && echo $$p/simavr; done}
31
32 # You can override the build settings with local changes in this file
33 # for example:
34 # export CC=clang
35 # export CFLAGS=-march=corei7-avx
36 # etc
37 -include ${wildcard ${SIMAVR}/../.make.options*}
38
39 # get the first character of what the compiler says it is, unless it's 'x86_64' doh
40 ARCH            = ${shell $(CC) -dumpmachine | sed -e 's/^x/i/' -e 's/\(.\).*/\1/'}
41
42 CFLAGS          += -O2 -Wall
43 CFLAGS          += -g
44 CORE_CFLAGS     = -nostdinc
45
46 ifeq (${shell uname}, Darwin)
47 # gcc 4.2 from MacOS is really not up to scratch anymore 
48 CC                      = clang
49 AVR_ROOT        := "/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/"
50 AVR_INC         := ${AVR_ROOT}/avr-4/
51 AVR             := ${AVR_ROOT}/bin/avr-
52 # Thats for MacPorts libelf
53 ifeq (${shell test -d /opt/local && echo Exists}, Exists)
54 IPATH           += /opt/local/include
55 LFLAGS          = -L/opt/local/lib/
56 endif
57 else
58 AVR_ROOT        := /usr/lib/avr
59 AVR_INC         := ${AVR_ROOT}
60 AVR             := avr-
61 ifeq (${shell uname -o}, Msys)
62 AVR_ROOT    := ${shell echo "${AVR32_HOME}" | tr '\\' '/'}
63 AVR_INC     := ${AVR_ROOT}/avr
64 AVR         := ${AVR_ROOT}/bin/avr-
65 IPATH       += ${PREFIX}/include
66 CFLAGS      += -I${PREFIX}/include
67 LDFLAGS         += -L/lib -L/local/lib
68 CFLAGS          += -DNO_COLOR
69 else
70 CFLAGS          += -fPIC
71 endif
72 endif
73
74 CPPFLAGS        += --std=gnu99 -Wall
75 CPPFLAGS        += ${patsubst %,-I%,${subst :, ,${IPATH}}}
76
77 AVR_CPPFLAGS= ${CPPFLAGS} -idirafter ${AVR_INC}/include
78
79 CC                      ?= clang
80 AR                      ?= ar
81 RANLIB          ?= ranlib
82 MKDIR           ?= mkdir -p
83 INSTALL         ?= install
84 SHELL           := ${shell which bash}
85         
86 OBJ             = obj-${shell $(CC) -dumpmachine}
87 LIBDIR          = ${shell pwd}/${SIMAVR}/${OBJ}
88 LDFLAGS         += -L${LIBDIR} -lsimavr 
89
90 LDFLAGS         += -lelf 
91
92 ifeq (${shell uname -o}, Msys)
93 LDFLAGS      += -lws2_32
94 endif
95
96 ifeq (${shell uname}, Linux)
97 ifeq ($(RELEASE),1)
98 # allow the shared library to be found in the build directory
99 # only for linking, the install time location is used at runtime
100 LFLAGS          += -Wl,-rpath-link,${LIBDIR}
101 else
102 # allow the shared library to be found in the build directory
103 LFLAGS          += -Wl,-rpath,${LIBDIR}
104 endif
105 endif
106
107 # The code is compiled "optimized" to the max.
108
109 # The weird "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
110 # is used to tell the linker not to discard the .mmcu section,
111 # otherwise the --gc-sections will delete it.
112
113 %.hex: %.axf
114         @${AVR}objcopy -j .text -j .data -j .eeprom -O ihex ${<} ${@}
115
116 %.s: %.axf
117         @${AVR}objdump -j .text -j .data -j .bss -d  ${<} > ${@}
118
119 # --mcall-prologues can be used here, but messes up debugging a little
120 %.axf: %.c 
121         @echo AVR-CC ${<}
122         @part=${<} ; part=$${part/_*}; \
123         ${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
124                         -mmcu=$$part \
125                         -DF_CPU=8000000 \
126                         -fno-inline-small-functions \
127                         -ffunction-sections -fdata-sections \
128                         -Wl,--relax,--gc-sections \
129                         -Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
130                         -I../simavr/sim/avr -I../../simavr/sim/avr \
131                         ${^} -o ${@}
132         @${AVR}size ${@}|sed '1d'
133
134 # this rule has precedence
135 ${OBJ}/sim_%.o : cores/sim_%.c
136 ifeq ($(V),1)
137         $(CC) $(CPPFLAGS) $(CFLAGS) $(CORE_CFLAGS) -MMD \
138                 ${AVR_CPPFLAGS} \
139                 $<  -c -o $@
140 else
141         @$(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS) $(CORE_CFLAGS) -MMD \
142                 ${AVR_CPPFLAGS} \
143                 $<  -c -o $@
144         @echo CORE $<
145 endif
146
147 ${OBJ}/%.o: %.c
148 ifeq ($(V),1)
149         $(CC) $(CPPFLAGS) $(CFLAGS) -MMD \
150                 $<  -c -o $@
151 else
152         @$(CC) $(CPPFLAGS) $(CFLAGS) -MMD \
153                 $<  -c -o $@
154         @echo CC $<
155 endif
156
157 ${OBJ}/%.elf:
158 ifeq ($(V),1)
159         $(CC) -MMD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
160 else
161         @echo LD $@
162         @$(CC) -MMD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
163 endif
164
165 obj: ${OBJ}
166  
167 ${OBJ}: 
168         @mkdir -p ${OBJ}
169
170 clean-${OBJ}:
171         rm -rf ${OBJ}
172
173 # include the dependency files generated by gcc, if any
174 -include ${wildcard ${OBJ}/*.d}