]> git.sur5r.net Git - cc65/blob - samples/Makefile
Fixed typo.
[cc65] / samples / Makefile
1 #
2 # Makefile for cc65 samples
3 #
4 # This Makefile requires GNU make
5 #
6
7 # Run 'make SYS=<target>'; or, set a SYS env.
8 # var. to build for another target system.
9 SYS ?= c64
10
11 # Just the usual way to define a variable
12 # containing a single space character.
13 SPACE :=
14 SPACE +=
15
16 # Just the usual way to find out if we're
17 # using cmd.exe to execute make rules.
18 ifneq ($(shell echo),)
19   CMD_EXE = 1
20 endif
21
22 ifdef CMD_EXE
23   NULLDEV = nul:
24   DEL = -del /f
25   RMDIR = rmdir /s /q
26 else
27   NULLDEV = /dev/null
28   DEL = $(RM)
29   RMDIR = $(RM) -r
30 endif
31
32 ifdef CC65_HOME
33   AS = $(CC65_HOME)/bin/ca65
34   CC = $(CC65_HOME)/bin/cc65
35   CL = $(CC65_HOME)/bin/cl65
36   LD = $(CC65_HOME)/bin/ld65
37 else
38   AS := $(if $(wildcard ../bin/ca65*),../bin/ca65,ca65)
39   CC := $(if $(wildcard ../bin/cc65*),../bin/cc65,cc65)
40   CL := $(if $(wildcard ../bin/cl65*),../bin/cl65,cl65)
41   LD := $(if $(wildcard ../bin/ld65*),../bin/ld65,ld65)
42 endif
43
44 ifneq ($(filter disk samples.%,$(MAKECMDGOALS)),)
45   ifdef CC65_HOME
46     TARGET_PATH = $(CC65_HOME)/target
47   else
48     TARGET_PATH := $(if $(wildcard ../target),../target,$(shell $(CL) --print-target-path))
49   endif
50
51   # If TARGET_PATH contains spaces then it is presumed to contain escaped spaces. GNU make
52   # has very limited support for paths containing spaces. $(wildcard) is the only function
53   # that is aware of escaped spaces. However, $(wildcard) never returns paths with escaped
54   # spaces !!! So if it e.g. finds in a path with 2 spaces in 4 files then one ends up with
55   # a return value consisting of 12 plain words :-((
56   #
57   # Fortunately we can work around that behaviour here because we know that the files we
58   # are looking for have known extensions. So we can $(filter) the in our example above 12
59   # words for file extensions so we come up with 4 path fragments. Then we remove those
60   # path fragments with $(notdir) from the file names.
61   #
62   # So far so good. But here we want to process files from different paths in a single
63   # recipe further down below and therefore want to prepend the paths to the files with
64   # $(addprefix). However, $(foreach) isn't aware of escaped spaces (only $(wildcard) is).
65   # Therefore, we need to replace the spaces with some other character temporarily in order
66   # to have $(foreach) generate one invocation per file. We use the character '?' for that
67   # purpose here, just because it is known to not be part of file names.
68   #
69   # Inside the recipe generated per file we then replace the '?' again with a space. As we
70   # want to be compatible with cmd.exe for execution we're not using an escaped space but
71   # rather double-quote the whole path.
72   #
73   # Note: The "strange" $(wildcard) further down below just serves the purpose to unescape
74   #       spaces for cmd.exe. This could have as well been done with another $(subst).
75
76   SUBST_TARGET_PATH := $(subst \$(SPACE),?,$(TARGET_PATH))
77
78   EMD := $(wildcard $(TARGET_PATH)/$(SYS)/drv/emd/*)
79   MOU := $(wildcard $(TARGET_PATH)/$(SYS)/drv/mou/*)
80   TGI := $(wildcard $(TARGET_PATH)/$(SYS)/drv/tgi/*)
81
82   EMD := $(addprefix $(SUBST_TARGET_PATH)/$(SYS)/drv/emd/,$(notdir $(filter %.emd,$(EMD))))
83   MOU := $(addprefix $(SUBST_TARGET_PATH)/$(SYS)/drv/mou/,$(notdir $(filter %.mou,$(MOU))))
84   TGI := $(addprefix $(SUBST_TARGET_PATH)/$(SYS)/drv/tgi/,$(notdir $(filter %.tgi,$(TGI))))
85
86   # This one comes with the VICE emulator.
87   # See http://vice-emu.sourceforge.net/
88   C1541 ?= c1541
89
90   # For this one, see https://applecommander.github.io/
91   AC ?= ac.jar
92
93   # For this one, see http://www.horus.com/~hias/atari/
94   DIR2ATR ?= dir2atr
95
96   DISK_c64       = samples.d64
97   DISK_apple2    = samples.dsk
98   DISK_apple2enh = samples.dsk
99   DISK_atari     = samples.atr
100   DISK_atarixl   = samples.atr
101 endif
102
103 # --------------------------------------------------------------------------
104 # System-dependent settings
105 # For convenience, these groups and lines are sorted alphabetically, first
106 # by target-machine group, then by mission, then by program and sub-target.
107
108 # The Apple machines need the start address adjusted when using TGI
109 LDFLAGS_mandelbrot_apple2    = --start-addr 0x4000
110 LDFLAGS_mandelbrot_apple2enh = --start-addr 0x4000
111 LDFLAGS_tgidemo_apple2       = --start-addr 0x4000
112 LDFLAGS_tgidemo_apple2enh    = --start-addr 0x4000
113
114 # The Apple ][ needs the start address adjusted for the mousedemo
115 LDFLAGS_mousedemo_apple2 = --start-addr 0x4000
116
117 # The Apple machines need the end address adjusted for large programs
118 LDFLAGS_gunzip65_apple2    = -D __HIMEM__=0xBF00
119 LDFLAGS_gunzip65_apple2enh = -D __HIMEM__=0xBF00
120
121 # The atari target needs to reserve some memory when using TGI
122 LDFLAGS_mandelbrot_atari = -D __RESERVED_MEMORY__=0x2000
123 LDFLAGS_tgidemo_atari    = -D __RESERVED_MEMORY__=0x2000
124
125 # The atarixl target needs the start address adjusted when using TGI
126 LDFLAGS_mandelbrot_atarixl = --start-addr 0x4000
127 LDFLAGS_tgidemo_atarixl    = --start-addr 0x4000
128
129 # --------------------------------------------------------------------------
130 # Generic rules
131
132 .PHONY: all mostlyclean clean install zip samples disk
133
134 %: %.c
135 %: %.s
136
137 .c.o:
138         $(CC) $(CFLAGS) -Ors --codesize 500 -T -g -t $(SYS) $<
139         $(AS) $(<:.c=.s)
140
141 .s.o:
142         $(AS) $(ASFLAGS) -t $(SYS) $<
143
144 .PRECIOUS: %.o
145
146 .o:
147 ifeq ($(SYS),vic20)
148         $(LD) $(LDFLAGS_$(@F)_$(SYS)) $(LDFLAGS) -o $@ -C vic20-32k.cfg -m $@.map $^ $(SYS).lib
149 else
150         $(LD) $(LDFLAGS_$(@F)_$(SYS)) $(LDFLAGS) -o $@ -t $(SYS) -m $@.map $^ $(SYS).lib
151 endif
152
153 # --------------------------------------------------------------------------
154 # Lists of executables
155
156 EXELIST_c64 =      \
157         ascii      \
158         enumdevdir \
159         fire       \
160         gunzip65   \
161         hello      \
162         mandelbrot \
163         mousedemo  \
164         multdemo   \
165         nachtm     \
166         ovrldemo   \
167         plasma     \
168         sieve      \
169         tgidemo
170
171 EXELIST_apple2 =   \
172         ascii      \
173         diodemo    \
174         enumdevdir \
175         gunzip65   \
176         hello      \
177         mandelbrot \
178         mousedemo  \
179         multdemo   \
180         ovrldemo   \
181         sieve      \
182         tgidemo
183
184 EXELIST_apple2enh = $(EXELIST_apple2)
185
186 EXELIST_atari =    \
187         ascii      \
188         gunzip65   \
189         hello      \
190         mandelbrot \
191         mousedemo  \
192         multdemo   \
193         ovrldemo   \
194         sieve      \
195         tgidemo
196
197 EXELIST_atarixl = $(EXELIST_atari)
198
199 EXELIST_atari2600 = \
200         atari2600hello
201
202 # Unlisted targets will try to build everything.
203 # That lets us learn what they cannot build, and what settings
204 # we need to use for programs that can be built and run.
205 ifndef EXELIST_$(SYS)
206 EXELIST_$(SYS) := ${patsubst %.c,%,$(wildcard *.c)}
207 endif
208
209 # --------------------------------------------------------------------------
210 # Rules to make the binaries and the disk
211
212 samples: $(EXELIST_$(SYS))
213
214 disk: $(DISK_$(SYS))
215
216 all:
217
218 # --------------------------------------------------------------------------
219 # Overlay rules. Overlays need special ld65 configuration files.  Also, the
220 # overlay file-names are shortenned to fit the Atari's 8.3-character limit.
221
222 multdemo: multidemo.o
223         $(LD) $(LDFLAGS) -o $@ -C $(SYS)-overlay.cfg -m $@.map $^ $(SYS).lib
224
225 ovrldemo: overlaydemo.o
226         $(LD) $(LDFLAGS) -o $@ -C $(SYS)-overlay.cfg -m $@.map $^ $(SYS).lib
227
228 OVERLAYLIST := $(foreach I,1 2 3,multdemo.$I ovrldemo.$I)
229
230 # --------------------------------------------------------------------------
231 # Rule to make a CBM disk with all samples. Needs the c1541 program that comes
232 # with the VICE emulator.
233
234 define D64_WRITE_recipe
235
236 $(C1541) -attach $@ -write "$(subst ?,$(SPACE),$(file))" $(notdir $(file)) >$(NULLDEV)
237
238 endef # D64_WRITE_recipe
239
240 samples.d64: samples
241         @$(C1541) -format samples,AA d64 $@ >$(NULLDEV)
242         $(foreach file,$(EXELIST_$(SYS)),$(D64_WRITE_recipe))
243         $(foreach file,$(OVERLAYLIST),$(D64_WRITE_recipe))
244         $(foreach file,$(EMD) $(MOU) $(TGI),$(D64_WRITE_recipe))
245
246 # --------------------------------------------------------------------------
247 # Rule to make an Apple II disk with all samples. Needs the AppleCommander
248 # program, available at https://applecommander.github.io/, and a template disk
249 # named 'prodos.dsk'.
250
251 define DSK_WRITE_BIN_recipe
252
253 $(if $(findstring BF00,$(LDFLAGS_$(notdir $(file))_$(SYS))), \
254   java -jar $(AC) -p $@ $(notdir $(file)).system sys <"$(wildcard $(TARGET_PATH)/$(SYS)/util/loader.system)")
255 java -jar $(AC) -as $@ $(notdir $(file)) <"$(file)"
256
257 endef # DSK_WRITE_BIN_recipe
258
259 define DSK_WRITE_REL_recipe
260
261 java -jar $(AC) -p $@ $(notdir $(file)) rel 0 <"$(subst ?,$(SPACE),$(file))"
262
263 endef # DSK_WRITE_REL_recipe
264
265 samples.dsk: samples
266         cp prodos.dsk $@
267         $(foreach file,$(EXELIST_$(SYS)),$(DSK_WRITE_BIN_recipe))
268         $(foreach file,$(OVERLAYLIST),$(DSK_WRITE_REL_recipe))
269         $(foreach file,$(EMD) $(MOU) $(TGI),$(DSK_WRITE_REL_recipe))
270
271 # --------------------------------------------------------------------------
272 # Rule to make an Atari disk with all samples. Needs the dir2atr program
273 # available at http://www.horus.com/~hias/atari/ and the MyDos4534 variant
274 # of dos.sys and dup.sys.
275
276 define ATR_WRITE_recipe
277
278 cp "$(subst ?,$(SPACE),$(file))" atr/$(notdir $(file))
279
280 endef # ATR_WRITE_recipe
281
282 samples.atr: samples
283         @mkdir atr
284         cp "dos.sys" atr/dos.sys
285         cp "dup.sys" atr/dup.sys
286         @$(foreach file,$(EXELIST_$(SYS)),$(ATR_WRITE_recipe))
287         @$(foreach file,$(OVERLAYLIST),$(ATR_WRITE_recipe))
288         @$(foreach file,$(EMD) $(MOU) $(TGI),$(ATR_WRITE_recipe))
289         $(DIR2ATR) -d -b MyDos4534 3200 $@ atr
290         @$(RMDIR) atr
291
292 # --------------------------------------------------------------------------
293 # Installation rules
294
295 INSTALL = install
296 samplesdir = $(PREFIX)/share/cc65/samples
297
298 install:
299         $(if $(PREFIX),,$(error variable "PREFIX" must be set))
300         $(INSTALL) -d $(DESTDIR)$(samplesdir)
301         $(INSTALL) -d $(DESTDIR)$(samplesdir)/geos
302         $(INSTALL) -d $(DESTDIR)$(samplesdir)/tutorial
303         $(INSTALL) -m0644 *.* $(DESTDIR)$(samplesdir)
304         $(INSTALL) -m0644 README $(DESTDIR)$(samplesdir)
305         $(INSTALL) -m0644 Makefile $(DESTDIR)$(samplesdir)
306         $(INSTALL) -m0644 geos/*.* $(DESTDIR)$(samplesdir)/geos
307         $(INSTALL) -m0644 tutorial/*.* $(DESTDIR)$(samplesdir)/tutorial
308
309 # --------------------------------------------------------------------------
310 # Packaging rules
311
312 zip:
313         @cd .. && zip -r cc65 samples/
314
315 # --------------------------------------------------------------------------
316 # Clean-up rules
317
318 mostlyclean:
319         @$(DEL) *.lbl *.map *.o *.s 2>$(NULLDEV)
320
321 clean: mostlyclean
322         @$(DEL) $(EXELIST_$(SYS)) $(DISK_$(SYS)) 2>$(NULLDEV)
323         @$(DEL) multdemo.? ovrldemo.? 2>$(NULLDEV)