1ifneq ($(shell echo),)
2  CMD_EXE = 1
3endif
4
5CBMS = c128   \
6       c16    \
7       c64    \
8       cbm510 \
9       cbm610 \
10       cx16   \
11       pet    \
12       plus4  \
13       vic20
14
15GEOS = geos-apple \
16       geos-cbm
17
18TARGETS = apple2       \
19          apple2enh    \
20          atari        \
21          atarixl      \
22          atari2600    \
23          atari5200    \
24          atmos        \
25          creativision \
26          $(CBMS)      \
27          $(GEOS)      \
28          gamate       \
29          lynx         \
30          nes          \
31          none         \
32          osic1p       \
33          pce          \
34          sim6502      \
35          sim65c02     \
36          supervision  \
37          telestrat
38
39DRVTYPES = emd \
40           joy \
41           mou \
42           ser \
43           tgi
44
45OUTPUTDIRS := lib                                                                             \
46              asminc                                                                          \
47              cfg                                                                             \
48              include                                                                         \
49              $(subst ../,,$(filter-out $(wildcard ../include/*.*),$(wildcard ../include/*))) \
50              $(subst ../,,$(wildcard ../target/*/drv/*))                                     \
51              $(subst ../,,$(wildcard ../target/*/util))
52
53.PHONY: all mostlyclean clean install zip lib $(TARGETS)
54
55.SUFFIXES:
56
57ifdef CMD_EXE
58  DIRLIST = $(strip $(foreach dir,$1,$(wildcard $(dir))))
59  MKDIR = mkdir $(subst /,\,$1)
60  RMDIR = $(if $(DIRLIST),rmdir /s /q $(subst /,\,$(DIRLIST)))
61else
62  MKDIR = mkdir -p $1
63  RMDIR = $(RM) -r $1
64endif
65
66# Every target requires its individual vpath setting but the vpath directive
67# acts globally. Therefore each target is built in a separate make instance.
68
69ifeq ($(words $(MAKECMDGOALS)),1)
70  ifeq ($(MAKECMDGOALS),$(filter $(MAKECMDGOALS),$(TARGETS)))
71    TARGET = $(MAKECMDGOALS)
72  endif
73endif
74
75ifndef TARGET
76
77datadir = $(PREFIX)/share/cc65
78
79all lib: $(TARGETS)
80
81mostlyclean:
82	$(call RMDIR,../libwrk)
83
84clean:
85	$(call RMDIR,../libwrk ../lib ../target)
86
87ifdef CMD_EXE
88
89install:
90
91else # CMD_EXE
92
93INSTALL = install
94
95define INSTALL_recipe
96
97$(if $(PREFIX),,$(error variable "PREFIX" must be set))
98$(INSTALL) -d $(DESTDIR)$(datadir)/$(dir)
99$(INSTALL) -m0644 ../$(dir)/*.* $(DESTDIR)$(datadir)/$(dir)
100
101endef # INSTALL_recipe
102
103install:
104	$(foreach dir,$(OUTPUTDIRS),$(INSTALL_recipe))
105
106endif # CMD_EXE
107
108define ZIP_recipe
109
110@cd .. && zip cc65 $(dir)/*.*
111
112endef # ZIP_recipe
113
114zip:
115	$(foreach dir,$(OUTPUTDIRS),$(ZIP_recipe))
116
117$(TARGETS):
118	@$(MAKE) --no-print-directory $@
119
120else # TARGET
121
122CA65FLAGS =
123CC65FLAGS = -Or -W error
124
125EXTZP = cbm510 \
126        cbm610 \
127        lynx
128
129MKINC = $(GEOS) \
130        atari   \
131        atarixl \
132        nes
133
134TARGETUTIL = apple2    \
135             apple2enh \
136             atari     \
137             geos-apple
138
139GEOSDIRS = common      \
140           conio       \
141           disk        \
142           dlgbox      \
143           file        \
144           graph       \
145           memory      \
146           menuicon    \
147           mousesprite \
148           process     \
149           runtime     \
150           system
151
152ifeq ($(TARGET),apple2enh)
153  SRCDIR = apple2
154  OBJPFX = a2
155  DRVPFX = a2e
156else ifeq ($(TARGET),atarixl)
157  SRCDIR = atari
158  OBJPFX = atr
159  DRVPFX = atrx
160else ifeq ($(TARGET),sim65c02)
161  SRCDIR = sim6502
162else
163  SRCDIR = $(TARGET)
164endif
165
166SRCDIRS = $(SRCDIR)
167
168ifeq ($(TARGET),$(filter $(TARGET),$(CBMS)))
169  SRCDIRS += cbm
170endif
171
172ifeq ($(TARGET),$(filter $(TARGET),$(GEOS)))
173  SRCDIRS += $(addprefix $(TARGET)/,  $(GEOSDIRS))
174  SRCDIRS += $(addprefix geos-common/,$(GEOSDIRS))
175endif
176
177SRCDIRS += common   \
178           conio    \
179           dbg      \
180           em       \
181           joystick \
182           mouse    \
183           runtime  \
184           serial   \
185           tgi      \
186           zlib
187
188vpath %.s $(SRCDIRS)
189vpath %.c $(SRCDIRS)
190
191OBJS := $(patsubst %.s,%.o,$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.s)))
192OBJS += $(patsubst %.c,%.o,$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c)))
193
194OBJS := $(addprefix ../libwrk/$(TARGET)/,$(sort $(notdir $(OBJS))))
195
196DEPS = $(OBJS:.o=.d)
197
198EXTRA_SRCPAT = $(SRCDIR)/extra/%.s
199EXTRA_OBJPAT = ../lib/$(TARGET)-%.o
200EXTRA_OBJS := $(patsubst $(EXTRA_SRCPAT),$(EXTRA_OBJPAT),$(wildcard $(SRCDIR)/extra/*.s))
201DEPS += $(EXTRA_OBJS:../lib/%.o=../libwrk/$(TARGET)/%.d)
202
203ZPOBJ = ../libwrk/$(TARGET)/zeropage.o
204ifeq ($(TARGET),$(filter $(TARGET),$(EXTZP)))
205  ZPOBJ += ../libwrk/$(TARGET)/extzp.o
206endif
207
208ifeq ($(TARGET),$(filter $(TARGET),$(MKINC)))
209  include $(SRCDIR)/Makefile.inc
210endif
211
212ifeq ($(TARGET),$(filter $(TARGET),$(TARGETUTIL)))
213  include $(SRCDIR)/targetutil/Makefile.inc
214endif
215
216define DRVTYPE_template
217
218$1_SRCDIR = $$(SRCDIR)/$1
219$1_STCDIR = ../libwrk/$$(TARGET)
220$1_DYNDIR = ../libwrk/$$(TARGET)/$1
221$1_DRVDIR = ../target/$$(TARGET)/drv/$1
222
223$1_SRCPAT = $$($1_SRCDIR)/$$(OBJPFX)%.s
224$1_STCPAT = $$($1_STCDIR)/$$(OBJPFX)%-$1.o
225$1_DYNPAT = $$($1_DYNDIR)/$$(OBJPFX)%.o
226$1_DRVPAT = $$($1_DRVDIR)/$$(DRVPFX)%.$1
227
228$1_SRCS := $$(wildcard $$($1_SRCDIR)/*.s)
229$1_STCS = $$(patsubst $$($1_SRCPAT),$$($1_STCPAT),$$($1_SRCS))
230$1_DYNS = $$(patsubst $$($1_SRCPAT),$$($1_DYNPAT),$$($1_SRCS))
231$1_DRVS = $$(patsubst $$($1_DYNPAT),$$($1_DRVPAT),$$($1_DYNS))
232
233$$($1_STCPAT): $$($1_SRCPAT)
234	@echo $$(TARGET) - $$< - static
235	@$$(CA65) -t $$(TARGET) -D DYN_DRV=0 $$(CA65FLAGS) --create-dep $$(@:.o=.d) -o $$@ $$<
236
237OBJS += $$($1_STCS)
238DEPS += $$($1_STCS:.o=.d)
239
240$$($1_DYNS): | $$($1_DYNDIR)
241
242$$($1_DRVPAT): $$($1_DYNPAT) $$(ZPOBJ) | $$($1_DRVDIR)
243	@echo $$(TARGET) - $$(<F)
244	@$$(LD65) -o $$@ -t module $$^
245
246$$($1_DYNDIR) $$($1_DRVDIR):
247	@$$(call MKDIR,$$@)
248
249$(TARGET): $$($1_DRVS)
250
251DEPS += $$($1_DYNS:.o=.d)
252
253endef # DRVTYPE_template
254
255$(foreach drvtype,$(DRVTYPES),$(eval $(call DRVTYPE_template,$(drvtype))))
256
257AR65 := $(if $(wildcard ../bin/ar65*),../bin/ar65,ar65)
258CA65 := $(if $(wildcard ../bin/ca65*),../bin/ca65,ca65)
259CC65 := $(if $(wildcard ../bin/cc65*),../bin/cc65,cc65)
260LD65 := $(if $(wildcard ../bin/ld65*),../bin/ld65,ld65)
261
262export CC65_HOME := $(abspath ..)
263
264define ASSEMBLE_recipe
265
266$(if $(QUIET),,@echo $(TARGET) - $<)
267@$(CA65) -t $(TARGET) $(CA65FLAGS) --create-dep $(@:.o=.d) -o $@ $<
268
269endef # ASSEMBLE_recipe
270
271define COMPILE_recipe
272
273$(if $(QUIET),,@echo $(TARGET) - $<)
274@$(CC65) -t $(TARGET) $(CC65FLAGS) --create-dep $(@:.o=.d) --dep-target $@ -o $(@:.o=.s) $<
275@$(CA65) -t $(TARGET) -o $@ $(@:.o=.s)
276
277endef # COMPILE_recipe
278
279../libwrk/$(TARGET)/%.o: %.s | ../libwrk/$(TARGET)
280	$(ASSEMBLE_recipe)
281
282../libwrk/$(TARGET)/%.o: %.c | ../libwrk/$(TARGET)
283	$(COMPILE_recipe)
284
285$(EXTRA_OBJPAT): $(EXTRA_SRCPAT) | ../libwrk/$(TARGET) ../lib
286	@echo $(TARGET) - $(<F)
287	@$(CA65) -t $(TARGET) $(CA65FLAGS) --create-dep $(@:../lib/%.o=../libwrk/$(TARGET)/%.d) -o $@ $<
288
289../lib/$(TARGET).lib: $(OBJS) | ../lib
290	$(AR65) a $@ $?
291
292../libwrk/$(TARGET) ../lib ../target/$(TARGET)/util:
293	@$(call MKDIR,$@)
294
295$(TARGET): $(EXTRA_OBJS) ../lib/$(TARGET).lib
296
297-include $(DEPS)
298
299endif # TARGET
300