forked from buserror/simavr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.common
230 lines (199 loc) · 6.28 KB
/
Makefile.common
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#
# This makefile take each "at*" file, extracts it's part name
# And compile it into an ELF binary.
# It also disassemble it for debugging purposes.
#
# The code is compiled "optimized" to the max.
#
# The weird "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
# is used to tell the linker not to discard the .mmcu section,
# otherwise the --gc-sections will delete it.
#
# Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
#
# This file is part of simavr.
#
# simavr is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# simavr is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with simavr. If not, see <http://www.gnu.org/licenses/>.
# simavr directory
SIMAVR := ${shell for p in . .. ../.. ../../..;do test -d $$p/simavr/sim && echo $$p/simavr; done}
# You can override the build settings with local changes in this file
# for example:
# export CC=clang
# export CFLAGS=-march=corei7-avx
# etc
-include ${wildcard ${SIMAVR}/../.make.options*}
# get the first character of what the compiler says it is, unless it's 'x86_64' doh
ARCH := ${shell $(CC) -dumpmachine | sed -e 's/^x/i/' -e 's/\(.\).*/\1/'}
CFLAGS += -O2 -Wall -Wextra -Wno-unused-parameter \
-Wno-unused-result -Wno-missing-field-initializers \
-Wno-sign-compare
CFLAGS += -g
CORE_CFLAGS = -DAVR_CORE=1
ifeq (${shell uname}, Darwin)
# gcc 4.2 from MacOS is really not up to scratch anymore
CC = clang
AVR_ROOT := "/Applications/Arduino.app/Contents/Java/hardware/tools/avr/"
AVR := ${AVR_ROOT}/bin/avr-
# Thats for MacPorts libelf
ifeq (${shell test -d /opt/local && echo Exists}, Exists)
ifneq (${shell test -d /opt/local/avr && echo Exists}, Exists)
$(error Please install avr-gcc: port install avr-gcc avr-libc)
endif
ifneq (${shell test -d /opt/local/include/libelf && echo Exists}, Exists)
$(error Please install libelf: port install libelf)
endif
CC = clang
IPATH += /opt/local/include /opt/local/include/libelf
LFLAGS = -L/opt/local/lib/
AVR := /opt/local/bin/avr-
else
# That's for Homebrew libelf and avr-gcc support
HOMEBREW_PREFIX ?= /usr/local
ifeq (${shell test -d $(HOMEBREW_PREFIX)/Cellar && echo Exists}, Exists)
ifneq (${shell test -d $(HOMEBREW_PREFIX)/Cellar/avr-gcc/ && echo Exists}, Exists)
$(error Please install avr-gcc: brew tap osx-cross/homebrew-avr ; brew install avr-libc)
endif
ifneq (${shell test -d $(HOMEBREW_PREFIX)/Cellar/libelf/ && echo Exists}, Exists)
$(error Please install libelf: brew install libelf)
endif
CC = clang
IPATH += $(HOMEBREW_PREFIX)/include
LFLAGS = -L$(HOMEBREW_PREFIX)/lib/
CFLAGS += -I/$(HOMEBREW_PREFIX)/include/libelf
AVR_ROOT := $(firstword $(wildcard $(HOMEBREW_PREFIX)/Cellar/avr-libc/*/))
AVR := $(HOMEBREW_PREFIX)/bin/avr-
endif
endif
else
AVR := avr-
endif
# FIXME uname -o doesn't work on bsd derivatives
#WIN := ${shell uname -o}
ifeq (${WIN}, Msys)
AVR_ROOT := ${shell echo "${AVR32_HOME}" | tr '\\' '/'}
AVR := ${AVR_ROOT}/bin/avr-
IPATH += ${PREFIX}/include
CFLAGS += -I${PREFIX}/include
LDFLAGS += -L/lib -L/local/lib
CFLAGS += -DNO_COLOR
else
CFLAGS += -fPIC
endif
CPPFLAGS += --std=gnu99 -Wall
CPPFLAGS += ${patsubst %,-I%,${subst :, ,${IPATH}}}
AVR_CPPFLAGS = ${CPPFLAGS} -I${SIMAVR}/cores
CC ?= clang
AR ?= ar
RANLIB ?= ranlib
MKDIR ?= mkdir -p
INSTALL ?= install
SHELL := ${shell which bash}
OBJ := obj-${shell $(CC) -dumpmachine}
LIBDIR := ${shell pwd}/${SIMAVR}/${OBJ}
LDFLAGS += -L${LIBDIR} -lsimavr -lm
LDFLAGS += -lelf
ifeq (${WIN}, Msys)
LDFLAGS += -lws2_32
endif
# for clock_gettime on RHEL 6.9
ifneq ("$(wildcard /usr/lib/librt.so)","")
LDFLAGS += -lrt
endif
ifeq (${shell uname}, Linux)
ifeq ($(RELEASE),1)
# allow the shared library to be found in the build directory
# only for linking, the install time location is used at runtime
LFLAGS += -Wl,-rpath-link,${LIBDIR} -Wl,-rpath,${PREFIX}/lib
else
# allow the shared library to be found in the build directory
LFLAGS += -Wl,-rpath,${LIBDIR}
endif
endif
ifeq (${V}, 1)
E =
else
E = @
endif
# The code is compiled "optimized" to the max.
#
# The weird "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
# is used to tell the linker not to discard the .mmcu section,
# otherwise the --gc-sections will delete it.
%.hex: %.axf
@${AVR}objcopy -j .text -j .data -j .eeprom -O ihex ${<} ${@}
%.s: %.axf
@${AVR}objdump -j .text -j .data -j .bss -d ${<} > ${@}
# --mcall-prologues can be used here, but messes up debugging a little
%.axf: %.c
@echo AVR-CC ${<}
@part=${<} ; part=$${part/_*}; \
${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
-mmcu=$$part \
-DF_CPU=8000000 \
-fno-inline-small-functions \
-ffunction-sections -fdata-sections \
-Wl,--relax,--gc-sections \
-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
-I../simavr/sim/avr -I../../simavr/sim/avr \
${^} -o ${@}
@${AVR}size ${@}|sed '1d'
# this rule has precedence
${OBJ}/sim_%.o : cores/sim_%.c
ifneq ($(E),)
@echo CORE $<
endif
${E}$(CC) $(CPPFLAGS) $(CFLAGS) $(CORE_CFLAGS) -MMD \
${AVR_CPPFLAGS} \
$< -c -o $@
${OBJ}/%.o: %.c
ifneq ($(E),)
@echo CC $<
endif
${E}$(CC) $(CPPFLAGS) $(CFLAGS) -MMD \
$< -c -o $@
${OBJ}/%.elf:
ifneq ($(E),)
@echo LD $@
endif
${E}$(CC) -MMD ${CFLAGS} ${LFLAGS} -o $@ ${filter %.o,$^} $(LDFLAGS)
.PRECIOUS: ${OBJ}/%.a ${OBJ}/%.so.1
#
# Static library
#
${OBJ}/%.a:
ifneq ($(E),)
@echo AR $@
endif
${E}$(AR) cru $@ ${filter %.o,$^} && $(RANLIB) $@
#
# Shared library (Linux)
#
${OBJ}/%.so.1: ${OBJ}/%.a
ifneq ($(E),)
@echo SHARED $@
endif
${E}$(CC) -o $@ -shared \
-Wl,--whole-archive,-soname,${basename ${notdir $@}}.1 \
${filter %.o %.a,$^} \
-Wl,--no-whole-archive \
${filter-out -l%, $(LDFLAGS)} ${EXTRA_LDFLAGS}
${OBJ}/%.so: ${OBJ}/%.so.1
ln -sf ${notdir $<} $@
obj: ${OBJ}
${OBJ}:
${E}mkdir -p ${OBJ}
clean-${OBJ}:
rm -rf ${OBJ}
# include the dependency files generated by gcc, if any
-include ${wildcard ${OBJ}/*.d}