]> git.sur5r.net Git - cc65/blob - doc/make.sgml
b51e319a47c07aa9a6405d3eb12c17343ae52de2
[cc65] / doc / make.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4
5 <title>Using GNU Make with cc65
6 <author>Oliver Schmidt, <htmlurl url="mailto:ol.sc@web.de" name="ol.sc@web.de">
7 <date>2009-06-26
8
9 <abstract>
10 How to build your program using the GNU Make utility.
11 </abstract>
12
13 <!-- Table of contents -->
14 <toc>
15
16 <!-- Begin the document -->
17
18 <sect>Overview<p>
19
20 This document describes how to build your programs using the cc65 development
21 tools and the GNU Make utility.
22
23 The cc65 development package doesn't come with a make utility. However this is
24 no issue because GNU Make works very nicely with cc65.
25
26
27
28 <sect>What is GNU Make?<p>
29
30 GNU Make is a both very powerful and very popular make utility. It might even
31 be called the de facto standard for make utilities. For more information see
32 the GNU Make home page:
33
34 <url url="http://www.gnu.org/software/make/">
35
36 The cc65 development package is available as binaries for several host systems
37 and can easily built for quite some additional systems. The very same is true
38 for GNU Make so a cc65-based project coming with a GNU Make Makefile can easily
39 be built by any cc65 developer no matter what host system is used.
40
41 Because of the strong alignment of the cc65 compiler with the ISO C standard it
42 is very well feasible to compile a single C code base both with the cc65
43 compiler and other C compilers like for example GCC. GNU Make turns out to be
44 very well suited to build projects for several target systems using multiple
45 compilers as it isn't tied to any C compiler.
46
47
48
49 <sect>A sample Makefile<p>
50
51 This Makefile is a fully functional sample for compiling several C sources
52 (here <tt/foo.c/ and <tt/bar.c/) and link the resulting object files into an
53 executable program (here <tt/foobar/):
54
55 <tscreen><verb>
56 SOURCES = foo.c bar.c
57
58 PROGRAM = foobar
59
60 ifdef CC65_TARGET
61 CC      = $(CC65_HOME)/bin/cl65
62 CFLAGS  = -t $(CC65_TARGET) --create-dep -O
63 LDFLAGS = -t $(CC65_TARGET) -m $(PROGRAM).map
64 else
65 CC      = gcc
66 CFLAGS  = -MMD -MP -O
67 LDFLAGS = -Wl,-Map,$(PROGRAM).map
68 endif
69
70 ########################################
71
72 ifdef CC65_TARGET
73 define MAKEDEPEND
74 sed -e"s!.s:!.o:!p" -e"s![^\t]*\t\(.*\)!\1:!" < $(<:.c=.u) > $(@:.o=.d)
75 rm -f $(<:.c=.u)
76 endef
77 endif
78
79 .SUFFIXES:
80 .PHONY: all
81 all: $(PROGRAM)
82
83 ifneq ($(MAKECMDGOALS),clean)
84 -include $(SOURCES:.c=.d)
85 endif
86
87 clean:
88         rm -f $(SOURCES:.c=.o) $(SOURCES:.c=.d) $(PROGRAM) $(PROGRAM).map
89
90 %.o: %.c
91         $(CC) -c $(CFLAGS) $< -o $@
92         @$(MAKEDEPEND)
93
94 $(PROGRAM): $(SOURCES:.c=.o)
95         $(CC) $(LDFLAGS) $^ -o $@
96 </verb></tscreen>
97
98
99 <sect1>Invoking the sample Makefile<p>
100
101 Without any specific configuration the sample Makefile will compile and link
102 using GCC. In order to rather use cc65 the variable <tt/CC65_TARGET/ needs to be
103 defined. This may by done as an environment variable or simply as part of the
104 Makefile. However to quickly switch between compilers and/or cc65 targets it is
105 best done on the GNU Make command line like this:
106
107 <tscreen><verb>
108 make CC65_TARGET=c64
109 </verb></tscreen>
110
111 The sample Makefile doesn't require cc65 to be "installed" in any way. Rather it
112 only presumes the single variable <tt/CC65_HOME/ to point to the directory the
113 cc65 packages were unpacked into. Again there are several ways to define this
114 variable but as its value typically won't change often it is best done as an
115 environment variable.
116
117
118 <sect1>Understanding the sample Makefile<p>
119
120 Most parts of the sample Makefile follow the guidelines in the
121 <htmlurl url="http://www.gnu.org/software/make/manual/make.html" name="GNU Make Manual">
122 that can be searched online for background information. The automatic generation of
123 dependency however rather works as described by the GNU Make maintainer Paul D. Smith in
124 <htmlurl url="http://make.paulandlesley.org/autodep.html#advanced" name="Advanced Auto-Dependencies">.
125
126 In the meantime GCC supports this method directly with the preprocessor option
127 <tt/-MP/ while cc65 requires some post-processing of the dependency file with
128 <tt/sed/ adding a second line like in this example:
129
130 <tscreen><verb>
131 foo.o:  foo.c foo.h bar.h
132 foo.c foo.h bar.h:
133 </verb></tscreen>
134
135
136 <sect1>Invoking the sample Makefile on Windows<p>
137
138 The recommended way to use GNU Make on Windows is to install it as part of a
139 Cygwin environment. For more information see the Cygwin home page:
140
141 <url url="http://www.cygwin.com/">
142   
143 If however installing Cygwin shouldn't be an option for one or the other reason
144 then the sample Makefile may be invoked from the Windows Command Prompt (cmd.exe)
145 by downloading the following programs:
146
147 <itemize>
148 <item>make.exe: <url url="http://gnuwin32.sourceforge.net/packages/make.htm">
149 <item>sed.exe: <url url="http://gnuwin32.sourceforge.net/packages/sed.htm">
150 <item>rm.exe: <url url="http://gnuwin32.sourceforge.net/packages/coreutils.htm">
151 </itemize>
152
153
154
155 <sect>Target-specific Variable Values<p>
156   
157 The very limited resources of the cc65 target machines now and then require
158 manual optimization of the build process by compiling individual source files
159 with different compiler options. GNU Make offers
160 <htmlurl url="http://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html" name="Target-specific Variable Values">
161 perfectly suited for doing so. For example placing the code of the two modules 
162 <tt/foo/ and <tt/bar/ in the segment <tt/FOOBAR/ can be archived with this
163 target-specific variable definition:
164
165 <tscreen><verb>
166 foo.o bar.o: CFLAGS += --code-name FOOBAR
167 </verb></tscreen>
168   
169 </article>