--- /dev/null
+<!doctype linuxdoc system>\r
+\r
+<article>\r
+\r
+<title>Using GNU Make with cc65\r
+<author>Oliver Schmidt, <htmlurl url="mailto:ol.sc@web.de" name="ol.sc@web.de">\r
+<date>26.06.2009\r
+\r
+<abstract>\r
+How to build your program using the GNU Make utility.\r
+</abstract>\r
+\r
+<!-- Table of contents -->\r
+<toc>\r
+\r
+<!-- Begin the document -->\r
+\r
+<sect>Overview<p>\r
+\r
+This document describes how to build your programs using the cc65 development\r
+tools and the GNU Make utility.\r
+\r
+The cc65 development package doesn't come with a make utility. However this is\r
+no issue because GNU Make works very nicely with cc65.\r
+\r
+\r
+\r
+<sect>What is GNU Make?<p>\r
+\r
+GNU Make is a both very powerful and very popular make utility. It might even\r
+be called the de facto standard for make utilities. For more information see\r
+the GNU Make home page:\r
+\r
+<url url="http://www.gnu.org/software/make/">\r
+\r
+The cc65 development package is available as binaries for several host systems\r
+and can easily built for quite some additional systems. The very same is true\r
+for GNU Make so a cc65-based project coming with a GNU Make Makefile can easily\r
+be built by any cc65 developer no matter what host system is used.\r
+\r
+Because of the strong alignment of the cc65 compiler with the ISO C standard it\r
+is very well feasable to compile a single C code base both with the cc65\r
+compiler and other C compilers like for example GCC. GNU Make turns out to be\r
+very well suited to build projects for several target systems using multiple\r
+compilers as it isn't tied to any C compiler.\r
+\r
+\r
+\r
+<sect>A sample Makefile<p>\r
+\r
+This Makefile is a fully functional sample for compiling several C sources\r
+(here <tt/foo.c/ and <tt/bar.c/) and link the resulting object files into an\r
+executable program (here <tt/foobar/):\r
+\r
+<tscreen><verb>\r
+SOURCES = foo.c bar.c\r
+\r
+PROGRAM = foobar\r
+\r
+ifdef CC65_TARGET\r
+CC = $(CC65_HOME)/bin/cl65\r
+CFLAGS = -t $(CC65_TARGET) --create-dep -O\r
+LDFLAGS = -t $(CC65_TARGET) -m $(PROGRAM).map\r
+else\r
+CC = gcc\r
+CFLAGS = -MMD -MP -O\r
+LDFLAGS = -Wl,-Map,$(PROGRAM).map\r
+endif\r
+\r
+########################################\r
+\r
+ifdef CC65_TARGET\r
+define MAKEDEPEND\r
+sed -e"s!.s:!.o:!p" -e"s![^\t]*\t\(.*\)!\1:!" < $(<:.c=.u) > $(@:.o=.d)\r
+rm -f $(<:.c=.u)\r
+endef\r
+endif\r
+\r
+.SUFFIXES:\r
+.PHONY: all\r
+all: $(PROGRAM)\r
+\r
+ifneq ($(MAKECMDGOALS),clean)\r
+-include $(SOURCES:.c=.d)\r
+endif\r
+\r
+clean:\r
+ rm -f $(SOURCES:.c=.o) $(SOURCES:.c=.d) $(PROGRAM) $(PROGRAM).map\r
+\r
+%.o: %.c\r
+ $(CC) -c $(CFLAGS) $< -o $@\r
+ @$(MAKEDEPEND)\r
+\r
+$(PROGRAM): $(SOURCES:.c=.o)\r
+ $(CC) $(LDFLAGS) $^ -o $@\r
+</verb></tscreen>\r
+\r
+\r
+<sect1>Invoking the sample Makefile<p>\r
+\r
+Without any specific configuration the sample Makefile will compile and link\r
+using GCC. In order to rather use cc65 the variable <tt/CC65_TARGET/ needs to be\r
+defined. This may by done as an environment variable or simply as part of the\r
+Makefile. However to quickly switch between compilers and/or cc65 targets it is\r
+best done on the GNU Make command line like this:\r
+\r
+<tscreen><verb>\r
+make CC65_TARGET=c64\r
+</verb></tscreen>\r
+\r
+The sample Makefile doesn't require cc65 to be "installed" in any way. Rather it\r
+only presumes the single variable <tt/CC65_HOME/ to point to the directory the\r
+cc65 packages were unpacked into. Again there are several ways to define this\r
+variable but as its value typically won't change often it is best done as an\r
+environment variable.\r
+\r
+\r
+<sect1>Understanding the sample Makefile<p>\r
+\r
+Most parts of the sample Makefile follow the guidlines in the\r
+<htmlurl url="http://www.gnu.org/software/make/manual/make.html" name="GNU Make Manual">\r
+that can be searched online for background information. The automatic generation of\r
+dependency however rather works as described by the GNU Make maintainer Paul D. Smith in\r
+<htmlurl url="http://make.paulandlesley.org/autodep.html#advanced" name="Advanced Auto-Dependencies">.\r
+\r
+In the meantime GCC supports this method directly with the preprocessor option\r
+<tt/-MP/ while cc65 requires some post-processing of the dependency file with\r
+<tt/sed/ adding a second line like in this example:\r
+\r
+<tscreen><verb>\r
+foo.o: foo.c foo.h bar.h\r
+foo.c foo.h bar.h:\r
+</verb></tscreen>\r
+\r
+\r
+<sect1>Invoking the sample Makefile on Windows<p>\r
+\r
+The recommended way to use GNU Make on Windows is to install it as part of a\r
+Cygwin environment. For more information see the Cygwin home page:\r
+\r
+<url url="http://www.cygwin.com/">\r
+ \r
+If however installing Cygwin shouldn't be an option for one or the other reason\r
+then the sample Makefile may be invoked from the Windows Command Prompt (cmd.exe)\r
+by downloading the following programs:\r
+\r
+<itemize>\r
+<item>make.exe: <url url="http://gnuwin32.sourceforge.net/packages/make.htm">\r
+<item>sed.exe: <url url="http://gnuwin32.sourceforge.net/packages/sed.htm">\r
+<item>rm.exe: <url url="http://gnuwin32.sourceforge.net/packages/coreutils.htm">\r
+</itemize>\r
+\r
+\r
+\r
+<sect>Target-specific Variable Values<p>\r
+ \r
+The very limited resources of the cc65 target machines now and then require\r
+manual optimization of the build process by compiling individual source files\r
+with different compiler options. GNU Make offers\r
+<htmlurl url="http://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html" name="Target-specific Variable Values">\r
+perfectly suited for doing so. For example placing the code of the two modules \r
+<tt/foo/ and <tt/bar/ in the segment <tt/FOOBAR/ can be archived with this\r
+target-specific variable definition:\r
+\r
+<tscreen><verb>\r
+foo.o bar.o: CFLAGS += --code-name FOOBAR\r
+</verb></tscreen>\r
+ \r
+</article>\r