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