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.
6 # The code is compiled "optimized" to the max.
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.
12 # Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
14 # This file is part of simavr.
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.
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.
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/>.
30 SIMAVR := ${shell for p in . .. ../.. ../../..;do test -d $$p/simavr/sim && echo $$p/simavr; done}
32 # You can override the build settings with local changes in this file
35 # export CFLAGS=-march=corei7-avx
37 -include ${wildcard ${SIMAVR}/../.make.options*}
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/'}
42 CFLAGS += -O2 -Wall -Wextra -Wno-unused-parameter \
43 -Wno-unused-result -Wno-missing-field-initializers \
46 CORE_CFLAGS = -DAVR_CORE=1
48 ifeq (${shell uname}, Darwin)
49 # gcc 4.2 from MacOS is really not up to scratch anymore
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)
58 ifneq (${shell test -d /opt/local/include/libelf && echo Exists}, Exists)
59 $(error Please install libelf: port install libelf)
62 IPATH += /opt/local/include /opt/local/include/libelf
63 LFLAGS = -L/opt/local/lib/
64 AVR := /opt/local/bin/avr-
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)
72 ifneq (${shell test -d $(HOMEBREW_PREFIX)/Cellar/libelf/ && echo Exists}, Exists)
73 $(error Please install libelf: brew install libelf)
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-
87 # FIXME uname -o doesn't work on bsd derivatives
88 #WIN := ${shell uname -o}
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
101 CPPFLAGS += --std=gnu99 -Wall
102 CPPFLAGS += ${patsubst %,-I%,${subst :, ,${IPATH}}}
104 AVR_CPPFLAGS = ${CPPFLAGS} -I${SIMAVR}/cores
111 SHELL := ${shell which bash}
113 OBJ := obj-${shell $(CC) -dumpmachine}
114 LIBDIR := ${shell pwd}/${SIMAVR}/${OBJ}
115 LDFLAGS += -L${LIBDIR} -lsimavr -lm
123 # for clock_gettime on RHEL 6.X
124 ifneq ("$(wildcard /usr/lib/librt.so /usr/lib64/librt.so)","")
128 ifeq (${shell uname}, Linux)
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
134 # allow the shared library to be found in the build directory
135 LFLAGS += -Wl,-rpath,${LIBDIR}
145 # The code is compiled "optimized" to the max.
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.
152 @${AVR}objcopy -j .text -j .data -j .eeprom -O ihex ${<} ${@}
155 @${AVR}objdump -j .text -j .data -j .bss -d ${<} > ${@}
157 # --mcall-prologues can be used here, but messes up debugging a little
162 ${E}part=${<} ; part=$${part/_*}; \
163 ${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
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 \
172 @${AVR}size ${@}|sed '1d'
174 # this rule has precedence
175 ${OBJ}/sim_%.o : cores/sim_%.c
179 ${E}$(CC) $(CPPFLAGS) $(CFLAGS) $(CORE_CFLAGS) -MMD \
187 ${E}$(CC) $(CPPFLAGS) $(CFLAGS) -MMD \
194 ${E}$(CC) -MMD ${CFLAGS} ${LFLAGS} -o $@ ${filter %.o,$^} $(LDFLAGS)
197 .PRECIOUS: ${OBJ}/%.a ${OBJ}/%.so.1
205 ${E}$(AR) cru $@ ${filter %.o,$^} && $(RANLIB) $@
208 # Shared library (Linux)
210 ${OBJ}/%.so.1: ${OBJ}/%.a
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}
220 ${OBJ}/%.so: ${OBJ}/%.so.1
221 ln -sf ${notdir $<} $@
231 # include the dependency files generated by gcc, if any
232 -include ${wildcard ${OBJ}/*.d}