2 # Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
4 # This file is part of simavr.
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.
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.
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/>.
19 SIMAVR_VERSION = ${shell git tag |tail -1}
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
29 all: obj config ${target}
31 include ../Makefile.common
33 cores = ${wildcard cores/*.c}
34 sim = ${wildcard sim/sim_*.c} ${wildcard sim/avr_*.c}
35 sim_o = ${patsubst sim/%.c, ${OBJ}/%.o, ${sim}}
48 ${OBJ}/libsimavr.a : ${sim_o}
50 @$(AR) cru $@ $^ && $(RANLIB) $@
53 # Shared library (Linux)
55 ${OBJ}/libsimavr.so.1 : ${sim_o}
57 @$(CC) -shared -Wl,-soname,libsimavr.so.1 -o $@ $^
59 ${OBJ}/libsimavr.so : ${OBJ}/libsimavr.so.1
60 ln -sf libsimavr.so.1 $@
62 libsimavr : config ${OBJ}/libsimavr.a
63 # shared library won't work that easily on non-linux
64 ifeq (${shell uname}, Linux)
65 libsimavr : ${OBJ}/libsimavr.so
68 ${OBJ}/${target}.o : libsimavr
69 ${OBJ}/${target}.elf : ${OBJ}/${target}.o
71 ${target} : ${OBJ}/${target}.elf
75 rm -rf ${target} *.a *.so
80 $(MKDIR) $(DESTDIR)/include/simavr/avr
81 $(INSTALL) sim/*.h $(DESTDIR)/include/simavr/
82 $(INSTALL) sim_core_*.h $(DESTDIR)/include/simavr/
83 $(INSTALL) ../include/*.h $(DESTDIR)/include/simavr/avr/
84 $(MKDIR) $(DESTDIR)/lib
85 $(INSTALL) ${OBJ}/libsimavr.a $(DESTDIR)/lib/
86 $(MKDIR) $(DESTDIR)/lib/pkgconfig/
87 sed -e "s|PREFIX|${DESTDIR}|g" -e "s|VERSION|${SIMAVR_VERSION}|g" \
88 simavr.pc >$(DESTDIR)/lib/pkgconfig/simavr.pc
89 ifeq (${shell uname}, Linux)
90 $(INSTALL) ${OBJ}/libsimavr.so.1 $(DESTDIR)/lib/
91 ln -sf libsimavr.so.1 $(DESTDIR)/lib/libsimavr.so
93 $(MKDIR) $(DESTDIR)/bin
94 $(INSTALL) ${OBJ}/${target}.elf $(DESTDIR)/bin/simavr
96 config: ${OBJ}/cores.deps sim_core_config.h sim_core_decl.h
99 # this tries to preprocess all the cores and decide
100 # which ones have a chance of compiling at all. Some platform
101 # ship with old version of avr-gcc and associated and needs
104 sim_core_config.h ${OBJ}/cores.deps: $(cores) Makefile
106 @conf=""; decl=""; array=""; \
107 mkdir -p ${OBJ} ; echo >${OBJ}/cores.deps ; \
108 for core in cores/*.c ; do \
109 file=$$core; global=$${core/cores\/sim_}; global=$${global/.c}; \
110 upper=$$(echo $$global|tr '[a-z]' '[A-Z]'); \
111 if $(CC) -E $(CFLAGS) ${AVR_CPPFLAGS} $$file \
112 >/dev/null 2>&1 ; then \
113 conf+="#define CONFIG_$$upper 1\n"; \
114 obj=$${file/.c/.o} ; obj=$${obj/cores\/}; \
115 printf "\$${OBJ}/libsimavr.a: \$${OBJ}/$$obj\n">>${OBJ}/cores.deps ; \
116 printf "\$${OBJ}/libsimavr.so.1: \$${OBJ}/$$obj\n">>${OBJ}/cores.deps ; \
118 echo WARNING $$file did not compile, check your avr-gcc toolchain; \
121 ( printf "// Autogenerated do not edit\n"; \
122 printf "#ifndef __SIM_CORE_CONFIG_H__\n#define __SIM_CORE_CONFIG_H__\n\n"; \
128 # This take the config file that was generated, and create the static
129 # table of all available cores for name lookups, as well as a C
132 sim_core_decl.h: sim_core_config.h $(cores) Makefile
134 @decl=""; array=""; \
135 for core in $$(grep -r avr_kind_t cores/|awk -F '[ :]' '{print $$1 "=" $$3;}') ; do \
136 file=$${core/=*}; global=$${core/*=}; \
137 upper=$${file/cores\/sim_}; upper=$${upper/.c}; \
138 upper=$$(echo $$upper|tr '[a-z]' '[A-Z]'); \
139 decl+="#if CONFIG_$$upper\nextern avr_kind_t $$global;\n#endif\n"; \
140 array+="#if CONFIG_$$upper\n\t&$$global,\n#endif\n"; \
142 ( printf "// Autogenerated do not edit\n"; \
143 printf "#ifndef __SIM_CORE_DECL_H__\n#define __SIM_CORE_DECL_H__\n\n"; \
144 printf "#include \"sim_core_config.h\"\n";\
145 printf "$$decl\n" ; \
146 printf "extern avr_kind_t * avr_kind[];\n"; \
147 printf "#ifdef AVR_KIND_DECL\navr_kind_t * avr_kind[] = {\n$$array\tNULL\n};\n#endif\n"; \
151 -include ${OBJ}/cores.deps