make uart_pty threadsafe
[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
62 # FIXME uname -o doesn't work on bsd derivatives
63 #WIN := ${shell uname -o}
64
65 ifeq (${WIN}, Msys)
66 AVR_ROOT    := ${shell echo "${AVR32_HOME}" | tr '\\' '/'}
67 AVR_INC     := ${AVR_ROOT}/avr
68 AVR         := ${AVR_ROOT}/bin/avr-
69 IPATH       += ${PREFIX}/include
70 CFLAGS      += -I${PREFIX}/include
71 LDFLAGS         += -L/lib -L/local/lib
72 CFLAGS          += -DNO_COLOR
73 else
74 CFLAGS          += -fPIC
75 endif
76 endif
77
78 CPPFLAGS        += --std=gnu99 -Wall
79 CPPFLAGS        += ${patsubst %,-I%,${subst :, ,${IPATH}}}
80
81 AVR_CPPFLAGS= ${CPPFLAGS} -idirafter ${AVR_INC}/include
82
83 CC                      ?= clang
84 AR                      ?= ar
85 RANLIB          ?= ranlib
86 MKDIR           ?= mkdir -p
87 INSTALL         ?= install
88 SHELL           := ${shell which bash}
89         
90 OBJ             = obj-${shell $(CC) -dumpmachine}
91 LIBDIR          = ${shell pwd}/${SIMAVR}/${OBJ}
92 LDFLAGS         += -L${LIBDIR} -lsimavr 
93
94 LDFLAGS         += -lelf 
95
96 ifeq (${WIN}, Msys)
97 LDFLAGS      += -lws2_32
98 endif
99
100 ifeq (${shell uname}, Linux)
101 ifeq ($(RELEASE),1)
102 # allow the shared library to be found in the build directory
103 # only for linking, the install time location is used at runtime
104 LFLAGS          += -Wl,-rpath-link,${LIBDIR}
105 else
106 # allow the shared library to be found in the build directory
107 LFLAGS          += -Wl,-rpath,${LIBDIR}
108 endif
109 endif
110
111 # The code is compiled "optimized" to the max.
112
113 # The weird "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
114 # is used to tell the linker not to discard the .mmcu section,
115 # otherwise the --gc-sections will delete it.
116
117 %.hex: %.axf
118         @${AVR}objcopy -j .text -j .data -j .eeprom -O ihex ${<} ${@}
119
120 %.s: %.axf
121         @${AVR}objdump -j .text -j .data -j .bss -d  ${<} > ${@}
122
123 # --mcall-prologues can be used here, but messes up debugging a little
124 %.axf: %.c 
125         @echo AVR-CC ${<}
126         @part=${<} ; part=$${part/_*}; \
127         ${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
128                         -mmcu=$$part \
129                         -DF_CPU=8000000 \
130                         -fno-inline-small-functions \
131                         -ffunction-sections -fdata-sections \
132                         -Wl,--relax,--gc-sections \
133                         -Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
134                         -I../simavr/sim/avr -I../../simavr/sim/avr \
135                         ${^} -o ${@}
136         @${AVR}size ${@}|sed '1d'
137
138 # this rule has precedence
139 ${OBJ}/sim_%.o : cores/sim_%.c
140 ifeq ($(V),1)
141         $(CC) $(CPPFLAGS) $(CFLAGS) $(CORE_CFLAGS) -MMD \
142                 ${AVR_CPPFLAGS} \
143                 $<  -c -o $@
144 else
145         @$(CC) $(CPPFLAGS) $(CFLAGS) $(CFLAGS) $(CORE_CFLAGS) -MMD \
146                 ${AVR_CPPFLAGS} \
147                 $<  -c -o $@
148         @echo CORE $<
149 endif
150
151 ${OBJ}/%.o: %.c
152 ifeq ($(V),1)
153         $(CC) $(CPPFLAGS) $(CFLAGS) -MMD \
154                 $<  -c -o $@
155 else
156         @$(CC) $(CPPFLAGS) $(CFLAGS) -MMD \
157                 $<  -c -o $@
158         @echo CC $<
159 endif
160
161 ${OBJ}/%.elf:
162 ifeq ($(V),1)
163         $(CC) -MMD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
164 else
165         @echo LD $@
166         @$(CC) -MMD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
167 endif
168
169 obj: ${OBJ}
170  
171 ${OBJ}: 
172         @mkdir -p ${OBJ}
173
174 clean-${OBJ}:
175         rm -rf ${OBJ}
176
177 # include the dependency files generated by gcc, if any
178 -include ${wildcard ${OBJ}/*.d}