misc: Move avr_mcu_section.h to sim/avr
[simavr] / simavr / Makefile
1 #
2 #       Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
3 #
4 #       This file is part of simavr.
5 #
6 #       simavr is free software: you can redistribute it and/or modify
7 #       it under the terms of the GNU General Public License as published by
8 #       the Free Software Foundation, either version 3 of the License, or
9 #       (at your option) any later version.
10 #
11 #       simavr is distributed in the hope that it will be useful,
12 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #       GNU General Public License for more details.
15 #
16 #       You should have received a copy of the GNU General Public License
17 #       along with simavr.  If not, see <http://www.gnu.org/licenses/>.
18
19 SIMAVR_VERSION  = ${shell git tag |tail -1|sed 's/[^0-9]*//'}
20 SIMAVR_REVISION = 2
21
22 target  = run_avr
23
24 CFLAGS  += -Werror
25 # tracing id useful especialy if you develop simavr core.
26 # it otherwise eat quite a bit of few cycles, even disabled
27 #CFLAGS += -DCONFIG_SIMAVR_TRACE=1
28
29 all:    obj config libsimavr ${target}
30
31 include ../Makefile.common
32
33 cores   = ${wildcard cores/*.c}
34 sim             = ${wildcard sim/sim_*.c} ${wildcard sim/avr_*.c}
35 sim_o   = ${patsubst sim/%.c, ${OBJ}/%.o, ${sim}}
36
37 VPATH   = cores
38 VPATH   += sim
39
40 IPATH   = sim
41 IPATH   += .
42 IPATH   += ../../shared
43     
44 #
45 # Static library
46 #
47 ${OBJ}/libsimavr.a      :       ${sim_o}
48 ifeq ($(V),1)
49         $(AR) cru $@ $^ && $(RANLIB) $@
50 else
51         @echo AR $@
52         @$(AR) cru $@ $^ && $(RANLIB) $@
53 endif
54
55 #
56 # Shared library (Linux)
57 #
58 ${OBJ}/libsimavr.so.1   :       ${sim_o}
59 ifeq ($(V),1)
60         $(CC) -shared -Wl,-soname,libsimavr.so.1 -o $@ $^
61 else
62         @echo SHARED $@
63         @$(CC) -shared -Wl,-soname,libsimavr.so.1 -o $@ $^
64 endif
65
66 ${OBJ}/libsimavr.so             : ${OBJ}/libsimavr.so.1
67         ln -sf libsimavr.so.1 $@
68
69 libsimavr       : config ${OBJ}/libsimavr.a
70 # shared library won't work that easily on non-linux
71 ifeq (${shell uname}, Linux)
72 libsimavr       :       ${OBJ}/libsimavr.so
73 endif
74
75 ${OBJ}/${target}.elf    : ${OBJ}/${target}.o
76
77 ${target}       : ${OBJ}/${target}.elf
78         ln -sf $< $@
79  
80 clean: clean-${OBJ}
81         rm -rf ${target} *.a *.so
82
83 DESTDIR = /usr/local
84 PREFIX = ${DESTDIR}
85
86 install : all
87         $(MKDIR) $(DESTDIR)/include/simavr/avr
88         $(INSTALL) -m644 sim/*.h $(DESTDIR)/include/simavr/
89         $(INSTALL) -m644 sim_core_*.h $(DESTDIR)/include/simavr/
90         $(INSTALL) -m644 sim/avr/*.h $(DESTDIR)/include/simavr/avr/
91         $(MKDIR) $(DESTDIR)/lib
92         $(INSTALL) ${OBJ}/libsimavr.a $(DESTDIR)/lib/
93         $(MKDIR) $(DESTDIR)/lib/pkgconfig/
94         sed -e "s|PREFIX|${PREFIX}|g" -e "s|VERSION|${SIMAVR_VERSION}|g" \
95                 simavr-avr.pc >$(DESTDIR)/lib/pkgconfig/simavr-avr.pc
96         sed -e "s|PREFIX|${PREFIX}|g" -e "s|VERSION|${SIMAVR_VERSION}|g" \
97                 simavr.pc >$(DESTDIR)/lib/pkgconfig/simavr.pc
98 ifeq (${shell uname}, Linux)
99         $(INSTALL) ${OBJ}/libsimavr.so.1 $(DESTDIR)/lib/
100         ln -sf libsimavr.so.1 $(DESTDIR)/lib/libsimavr.so
101 endif
102         $(MKDIR) $(DESTDIR)/bin
103         $(INSTALL) ${OBJ}/${target}.elf $(DESTDIR)/bin/simavr
104
105 # Needs 'fpm', oneline package manager. Install with 'gem install fpm'
106 # This generates 'mock' debian files, without all the policy, scripts
107 # and all the things normal debian requires. If debian decides to roll
108 # in simavr officialy, this section shall diseapear
109 debian: 
110         rm -rf /tmp/simavr-tmp && mkdir -p /tmp/simavr-tmp/usr && \
111         make install DESTDIR=/tmp/simavr-tmp/usr && \
112         (cd /tmp/simavr-tmp && \
113         fpm -s dir -t deb -C /tmp/simavr-tmp -n libsimavr -v $(SIMAVR_VERSION) \
114                 --iteration $(SIMAVR_REVISION) \
115                 -d 'libelf' \
116         --description "lean and mean Atmel AVR simulator: Runtime library" \
117         usr/lib/lib*.so* && \
118         fpm -s dir -t deb -C /tmp/simavr-tmp -n libsimavr-dev -v $(SIMAVR_VERSION) \
119                 --iteration $(SIMAVR_REVISION) \
120                 -d 'libsimavr >= $(SIMAVR_VERSION)' \
121         --description "lean and mean Atmel AVR simulator: Development files" \
122         usr/include usr/lib/lib*.a usr/lib/pkgconfig && \
123         fpm -s dir -t deb -C /tmp/simavr-tmp -n simavr -v $(SIMAVR_VERSION) \
124                 --iteration $(SIMAVR_REVISION) \
125                 -d 'libsimavr >= $(SIMAVR_VERSION)' \
126         --description "lean and mean Atmel AVR simulator: Executable" \
127         usr/bin ) && \
128         echo Done
129
130 config: ${OBJ}/cores.deps sim_core_config.h sim_core_decl.h
131
132 #
133 # this tries to preprocess all the cores and decide
134 # which ones have a chance of compiling at all. Some platform
135 # ship with old version of avr-gcc and associated and needs
136 # to be verified
137 #
138 ifeq ($(V), 1)
139 DEBUGLOG=/tmp/simavr_conf.log
140 else
141 DEBUGLOG=/dev/null
142 endif
143 sim_core_config.h ${OBJ}/cores.deps: $(cores) Makefile
144         @echo CONF $@
145         @conf=""; decl=""; array=""; \
146         mkdir -p ${OBJ} ; echo >${OBJ}/cores.deps ; echo >$(DEBUGLOG) ;\
147         for core in cores/*.c ; do \
148                 file=$$core; global=$${core/cores\/sim_}; global=$${global/.c}; \
149                 upper=$$(echo $$global|tr '[a-z]' '[A-Z]'); \
150                 if $(CC) -E $(CFLAGS) ${AVR_CPPFLAGS} $$file \
151                         >>$(DEBUGLOG) 2>&1 ; then \
152                         conf+="#define CONFIG_$$upper 1\n"; \
153                         obj=$${file/.c/.o} ; obj=$${obj/cores\/}; \
154                         printf "\$${OBJ}/libsimavr.a: \$${OBJ}/$$obj\n">>${OBJ}/cores.deps ; \
155                         printf "\$${OBJ}/libsimavr.so.1: \$${OBJ}/$$obj\n">>${OBJ}/cores.deps ; \
156                 else \
157                         echo WARNING $$file did not compile, check your avr-gcc toolchain; \
158                 fi \
159         done ; \
160         ( printf "// Autogenerated do not edit\n"; \
161           printf "#ifndef __SIM_CORE_CONFIG_H__\n#define __SIM_CORE_CONFIG_H__\n\n"; \
162           printf "$$conf\n"; \
163           printf "#endif\n"; \
164         ) >sim_core_config.h
165
166 #
167 # This take the config file that was generated, and create the static
168 # table of all available cores for name lookups, as well as a C
169 # config file
170 #  
171 sim_core_decl.h: sim_core_config.h $(cores) Makefile
172         @echo CONF $@
173         @decl=""; array=""; \
174         for core in $$(grep -r avr_kind_t cores/|awk -F '[ :]' '{print $$1 "=" $$3;}') ; do \
175                 file=$${core/=*}; global=$${core/*=}; \
176                 upper=$${file/cores\/sim_}; upper=$${upper/.c}; \
177                 upper=$$(echo $$upper|tr '[a-z]' '[A-Z]'); \
178                 decl+="#if CONFIG_$$upper\nextern avr_kind_t $$global;\n#endif\n"; \
179                 array+="#if CONFIG_$$upper\n\t&$$global,\n#endif\n"; \
180         done ; \
181         ( printf "// Autogenerated do not edit\n"; \
182           printf "#ifndef __SIM_CORE_DECL_H__\n#define __SIM_CORE_DECL_H__\n\n"; \
183           printf "#include \"sim_core_config.h\"\n";\
184           printf "$$decl\n" ;  \
185           printf "extern avr_kind_t * avr_kind[];\n"; \
186           printf "#ifdef AVR_KIND_DECL\navr_kind_t * avr_kind[] = {\n$$array\tNULL\n};\n#endif\n"; \
187           printf "#endif\n"; \
188         ) >sim_core_decl.h
189
190 -include ${OBJ}/cores.deps