17e69aa3367ee98b4aed2a8fbb0a74745d95c596
[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  = 1.0a10
20 SIMAVR_REVISION = 1
21 SHELL=/bin/bash
22
23 target  = run_avr
24
25 CFLAGS  += -O3 -Wall -Werror
26
27 # tracing id useful especialy if you develop simavr core.
28 # it otherwise eat quite a bit of few cycles, even disabled
29 #CFLAGS += -DCONFIG_SIMAVR_TRACE=1
30
31 all:    obj config ${target}
32
33 include ../Makefile.common
34
35 cores   = ${wildcard cores/*.c}
36 sim             = ${wildcard sim/sim_*.c} ${wildcard sim/avr_*.c}
37 sim_o   = ${patsubst sim/%.c, ${OBJ}/%.o, ${sim}}
38
39 VPATH   = cores
40 VPATH   += sim
41
42 IPATH   = sim
43 IPATH   += .
44 IPATH   += ../../shared
45 IPATH   += ../include
46     
47 #
48 # Static library
49 #
50 ${OBJ}/libsimavr.a      :       ${sim_o}
51         @echo AR $@
52         @$(AR) cru $@ $^ && $(RANLIB) $@
53
54 #
55 # Shared library (Linux)
56 #
57 ${OBJ}/libsimavr.so.1   :       ${sim_o}
58         @echo SHARED $@
59         @$(CC) -shared -Wl,-soname,libsimavr.so.1 -o $@ $^
60
61 ${OBJ}/libsimavr.so             : ${OBJ}/libsimavr.so.1
62         ln -sf libsimavr.so.1 $@
63
64 libsimavr       : config ${OBJ}/libsimavr.a
65 # shared library won't work that easily on non-linux
66 ifeq (${shell uname}, Linux)
67 libsimavr       :       ${OBJ}/libsimavr.so
68 endif
69
70 ${OBJ}/${target}.o              : libsimavr 
71 ${OBJ}/${target}.elf    : ${OBJ}/${target}.o
72
73 ${target}       : ${OBJ}/${target}.elf
74         ln -sf $< $@
75  
76 clean: clean-${OBJ}
77         rm -rf ${target} *.a *.so
78
79 DESTDIR = /usr/local
80
81 install : all
82         $(MKDIR) $(DESTDIR)/include/simavr/avr
83         $(INSTALL) sim/*.h $(DESTDIR)/include/simavr/
84         $(INSTALL) sim_core_*.h $(DESTDIR)/include/simavr/
85         $(INSTALL) ../include/*.h $(DESTDIR)/include/simavr/avr/
86         $(MKDIR) $(DESTDIR)/lib
87         $(INSTALL) ${OBJ}/libsimavr.a $(DESTDIR)/lib/
88         $(MKDIR) $(DESTDIR)/lib/pkgconfig/
89         sed -e "s|PREFIX|${DESTDIR}|g" -e "s|VERSION|${SIMAVR_VERSION}|g" \
90                 simavr.pc >$(DESTDIR)/lib/pkgconfig/simavr.pc
91 ifeq (${shell uname}, Linux)
92         $(INSTALL) ${OBJ}/libsimavr.so.1 $(DESTDIR)/lib/
93         ln -sf libsimavr.so.1 $(DESTDIR)/lib/libsimavr.so
94 endif
95         $(MKDIR) $(DESTDIR)/bin
96         $(INSTALL) ${OBJ}/${target}.elf $(DESTDIR)/bin/simavr
97
98 config: ${OBJ}/cores.deps sim_core_config.h sim_core_decl.h
99
100 sim_core_config.h ${OBJ}/cores.deps: $(cores) Makefile
101         @echo CONF $@
102         @conf=""; decl=""; array=""; \
103         mkdir -p ${OBJ} ; echo >${OBJ}/cores.deps ; \
104         for core in cores/*.c ; do \
105                 file=$$core; global=$${core/cores\/sim_}; global=$${global/.c}; \
106                 upper=$$(echo $$global|tr '[a-z]' '[A-Z]'); \
107                 if $(CC) -E $(CFLAGS) -I$(AVR_INC)/include $$file \
108                         >/dev/null 2>&1 ; then \
109                         conf+="#define CONFIG_$$upper 1\n"; \
110                         obj=$${file/.c/.o} ; obj=$${obj/cores\/}; \
111                         printf "\$${OBJ}/libsimavr.a: \$${OBJ}/$$obj\n">>${OBJ}/cores.deps ; \
112                         printf "\$${OBJ}/libsimavr.so.1: \$${OBJ}/$$obj\n">>${OBJ}/cores.deps ; \
113                 else \
114                         echo WARNING $$file did not compile, check your avr-gcc toolchain; \
115                 fi \
116         done ; \
117         ( printf "// Autogenerated do not edit\n"; \
118           printf "#ifndef __SIM_CORE_CONFIG_H__\n#define __SIM_CORE_CONFIG_H__\n\n"; \
119           printf "$$conf\n"; \
120           printf "#endif\n"; \
121         ) >sim_core_config.h
122
123 sim_core_decl.h: sim_core_config.h $(cores) Makefile
124         @echo CONF $@
125         @decl=""; array=""; \
126         for core in $$(grep -r avr_kind_t cores/|awk -F '[ :]' '{print $$1 "=" $$3;}') ; do \
127                 file=$${core/=*}; global=$${core/*=}; \
128                 upper=$${file/cores\/sim_}; upper=$${upper/.c}; \
129                 upper=$$(echo $$upper|tr '[a-z]' '[A-Z]'); \
130                 decl+="#if CONFIG_$$upper\nextern avr_kind_t $$global;\n#endif\n"; \
131                 array+="#if CONFIG_$$upper\n\t&$$global,\n#endif\n"; \
132         done ; \
133         ( printf "// Autogenerated do not edit\n"; \
134           printf "#ifndef __SIM_CORE_DECL_H__\n#define __SIM_CORE_DECL_H__\n\n"; \
135           printf "#include \"sim_core_config.h\"\n";\
136           printf "$$decl\n" ;  \
137           printf "extern avr_kind_t * avr_kind[];\n"; \
138           printf "#ifdef AVR_KIND_DECL\navr_kind_t * avr_kind[] = {\n$$array\tNULL\n};\n#endif\n"; \
139           printf "#endif\n"; \
140         ) >sim_core_decl.h
141
142 -include ${OBJ}/cores.deps