Remove 3rd letter of weekday, right justify the date (flush with seconds)
[simavr.git] / 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 -Wextra -Wno-unused-parameter \
43                         -Wno-unused-result -Wno-missing-field-initializers \
44                         -Wno-sign-compare
45 CFLAGS          += -g
46 CORE_CFLAGS     = -DAVR_CORE=1
47
48 ifeq (${shell uname}, Darwin)
49  # gcc 4.2 from MacOS is really not up to scratch anymore
50  CC                     = clang
51  AVR_ROOT       := "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/"
52  AVR            := ${AVR_ROOT}/bin/avr-
53  # Thats for MacPorts libelf
54  ifeq (${shell test -d /opt/local && echo Exists}, Exists)
55   ifneq (${shell test -d /opt/local/avr && echo Exists}, Exists)
56    $(error Please install avr-gcc: port install avr-gcc avr-libc)
57   endif
58   ifneq (${shell test -d /opt/local/include/libelf && echo Exists}, Exists)
59    $(error Please install libelf: port install libelf)
60   endif
61   CC            = clang
62   IPATH         += /opt/local/include /opt/local/include/libelf
63   LFLAGS        = -L/opt/local/lib/
64   AVR           := /opt/local/bin/avr-
65  else
66   # That's for Homebrew libelf and avr-gcc support
67   HOMEBREW_PREFIX ?= /usr/local
68   ifeq (${shell test -d $(HOMEBREW_PREFIX)/Cellar && echo Exists}, Exists)
69    ifneq (${shell test -d $(HOMEBREW_PREFIX)/Cellar/avr-gcc* && echo Exists}, Exists)
70     $(error Please install avr-gcc: brew tap osx-cross/homebrew-avr ; brew install avr-libc)
71    endif
72    ifneq (${shell test -d $(HOMEBREW_PREFIX)/Cellar/libelf/ && echo Exists}, Exists)
73     $(error Please install libelf: brew install libelf)
74    endif
75    CC                   = clang
76    IPATH                += $(HOMEBREW_PREFIX)/include
77    LFLAGS               = -L$(HOMEBREW_PREFIX)/lib/
78    CFLAGS               += -I/$(HOMEBREW_PREFIX)/include/libelf
79    AVR_ROOT             := $(firstword $(wildcard $(HOMEBREW_PREFIX)/Cellar/avr-libc/*/))
80    AVR                  := $(HOMEBREW_PREFIX)/bin/avr-
81   endif
82  endif
83 else
84  AVR            := avr-
85 endif
86
87 # FIXME uname -o doesn't work on bsd derivatives
88 #WIN := ${shell uname -o}
89
90 ifeq (${WIN}, Msys)
91 AVR_ROOT    := ${shell echo "${AVR32_HOME}" | tr '\\' '/'}
92 AVR         := ${AVR_ROOT}/bin/avr-
93 IPATH       += ${PREFIX}/include
94 CFLAGS      += -I${PREFIX}/include
95 LDFLAGS         += -L/lib -L/local/lib
96 CFLAGS          += -DNO_COLOR
97 else
98 CFLAGS          += -fPIC
99 endif
100
101 CPPFLAGS        += --std=gnu99 -Wall
102 CPPFLAGS        += ${patsubst %,-I%,${subst :, ,${IPATH}}}
103
104 AVR_CPPFLAGS    = ${CPPFLAGS} -I${SIMAVR}/cores
105
106 CC                      ?= clang
107 AR                      ?= ar
108 RANLIB          ?= ranlib
109 MKDIR           ?= mkdir -p
110 INSTALL         ?= install
111 SHELL           := ${shell which bash}
112
113 OBJ             := obj-${shell $(CC) -dumpmachine}
114 LIBDIR          := ${shell pwd}/${SIMAVR}/${OBJ}
115 LDFLAGS         += -L${LIBDIR} -lsimavr -lm
116
117 LDFLAGS         += -lelf
118
119 ifeq (${WIN}, Msys)
120 LDFLAGS      += -lws2_32
121 endif
122
123 # for clock_gettime on RHEL 6.X
124 ifneq ("$(wildcard /usr/lib/librt.so /usr/lib64/librt.so)","")
125 LDFLAGS            += -lrt
126 endif
127
128 ifeq (${shell uname}, Linux)
129 ifeq ($(RELEASE),1)
130 # allow the shared library to be found in the build directory
131 # only for linking, the install time location is used at runtime
132 LFLAGS          += -Wl,-rpath-link,${LIBDIR} -Wl,-rpath,${PREFIX}/lib
133 else
134 # allow the shared library to be found in the build directory
135 LFLAGS          += -Wl,-rpath,${LIBDIR}
136 endif
137 endif
138
139 ifeq (${V}, 1)
140 E               =
141 else
142 E               = @
143 endif
144
145 # The code is compiled "optimized" to the max.
146 #
147 # The weird "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
148 # is used to tell the linker not to discard the .mmcu section,
149 # otherwise the --gc-sections will delete it.
150
151 %.hex: %.axf
152         @${AVR}objcopy -j .text -j .data -j .eeprom -O ihex ${<} ${@}
153
154 %.s: %.axf
155         @${AVR}objdump -j .text -j .data -j .bss -d  ${<} > ${@}
156
157 # --mcall-prologues can be used here, but messes up debugging a little
158 %.axf: %.c
159 ifneq ($(E),)
160         @echo AVR-CC ${<}
161 endif
162         ${E}part=${<} ; part=$${part/_*}; \
163         ${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
164                         -mmcu=$$part \
165                         -DF_CPU=8000000 \
166                         -fno-inline-small-functions \
167                         -ffunction-sections -fdata-sections \
168                         -Wl,--relax,--gc-sections \
169                         -Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
170                         -I../simavr/sim/avr -I../../simavr/sim/avr \
171                         ${^} -o ${@}
172         @${AVR}size ${@}|sed '1d'
173
174 # this rule has precedence
175 ${OBJ}/sim_%.o : cores/sim_%.c
176 ifneq ($(E),)
177         @echo CORE $<
178 endif
179         ${E}$(CC) $(CPPFLAGS) $(CFLAGS) $(CORE_CFLAGS) -MMD \
180                 ${AVR_CPPFLAGS} \
181                 $<  -c -o $@
182
183 ${OBJ}/%.o: %.c
184 ifneq ($(E),)
185         @echo CC $<
186 endif
187         ${E}$(CC) $(CPPFLAGS) $(CFLAGS) -MMD \
188                 $<  -c -o $@
189
190 ${OBJ}/%.elf:
191 ifneq ($(E),)
192         @echo LD $@
193 endif
194         ${E}$(CC) -MMD ${CFLAGS}  ${LFLAGS} -o $@ ${filter %.o,$^} $(LDFLAGS)
195
196
197 .PRECIOUS: ${OBJ}/%.a ${OBJ}/%.so.1
198 #
199 # Static library
200 #
201 ${OBJ}/%.a:
202 ifneq ($(E),)
203         @echo AR $@
204 endif
205         ${E}$(AR) cru $@ ${filter %.o,$^} && $(RANLIB) $@
206
207 #
208 # Shared library (Linux)
209 #
210 ${OBJ}/%.so.1: ${OBJ}/%.a
211 ifneq ($(E),)
212         @echo SHARED $@
213 endif
214         ${E}$(CC) -o $@ -shared \
215                 -Wl,--whole-archive,-soname,${basename ${notdir $@}}.1 \
216                 ${filter %.o %.a,$^} \
217                  -Wl,--no-whole-archive \
218                 ${filter-out -lsimavr, $(LDFLAGS)} ${EXTRA_LDFLAGS}
219
220 ${OBJ}/%.so: ${OBJ}/%.so.1
221         ln -sf ${notdir $<} $@
222
223 obj: ${OBJ}
224
225 ${OBJ}:
226         ${E}mkdir -p ${OBJ}
227
228 clean-${OBJ}:
229         rm -rf ${OBJ}
230
231 # include the dependency files generated by gcc, if any
232 -include ${wildcard ${OBJ}/*.d}