2009-01-22 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / build / README.makefiles
blob610468d5cdf6e66573dc352ce48244676ffd7796
1 The MCS makefiles (-*- outline -*-)
2 Peter Williams <peter@newton.cx>
4 The new makefiles try to abstract building on Windows and Linux. They
5 try to provide a consistent set of ways to express the things that our
6 build system needs to let us do, specifically:
8       * Build recursively
9       * Build libraries and executables easily
10       * Let developers use different runtimes and class libaries
11       * Make distributions easily
12       * Provide a framework for testing
13       * Build platform-independently whenever possible
14       * Generate, update, and build monodoc documentation.
20 ** Makefile structure
22 A general makefile looks like this:
24 ========================================
25 thisdir = class/Mono.My.Library
26 SUBDIRS =
27 include ../../build/rules.make
29 all-local:
30         do some stuff
32 install-local:
33         $(MKINSTALLDIRS) $(DESTDIR)$(prefix)/share/
34         $(INSTALL_DATA) myfile.txt $(DESTDIR)$(prefix)/share/myfiledir
36 clean-local:
37         rm -f my-generated-file
39 test-local: my_test_program.exe
41 run-test-local:
42         $(RUNTIME) my_test_program.exe
44 run-test-ondotnet-local:
45         $(RUNTIME) my_test_program.exe
47 DISTFILES = myfile.txt my_test_source.cs
49 dist-local: dist-default
51 doc-update-local:
53 my_test_program.exe: my_test_source.cs
54         $(CSCOMPILE) /target:exe /out:$@ $<
55 ========================================
57 Each makefile follows the same pattern: it does some setup, includes
58 the standard make rules, and provides rules for eight standard targets:
59 all, install, test, run-test, clean, dist, and doc-update.
61 "Some setup" is defining two variables: $(thisdir) and
62 $(SUBDIRS). $(thisdir) is the directory that the makefile lives in,
63 relative to the top directory (ie, class/corlib) and $(SUBDIRS)
64 defines the subdirectories that should be built in.
66 The eight targets do the following:
68         * all-local builds whatever someone would expect to be built
69 when they just type 'make'. Most likely Foo.dll or Foo.exe
71         * install-local installs whatever got built by all-local.
73         * test-local _builds_ the test programs or libraries but does
74 _not_ run them.
76         * run-test-local actually runs the tests. It shouldn't
77 necessarily exit in an error if the test fails, but should make that
78 situation obvious. It should only run tests that take care of
79 themselves automatically; interactive tests should have an individual
80 target. The idea is that 'make run-test' from the toplevel should be
81 able to proceed unsupervised and test everything that can be tested in
82 such a manner.
84         * run-test-ondotnet-local is a variant of run-test-local. It is used only to validate if our tests themselves works fine under Microsoft runtime (on Windows). Basically, in this target, we should not use $(TEST_RUNTIME) to test our libraries.
86         * clean-local removes built files; 'make clean' should leave
87 only files that go into a distribution tarball. (But it is not necessarily
88 true that all files that go into a tarball need to be left after a make clean.)
90         * dist-local copies files into the distribution tree, which is
91 given by the variable $(distdir). dist-local always depends on the
92 target 'dist-default'. See ** 'make dist' below.
94         * doc-update-local should generate or update monodoc documentation,
95 if appropriate.  This is usually only appropriate for libraries.  It's
96 defined as a standard target so that it can easily be run recursively
97 across all libraries within the module.
103 ** Build configuration
105 In general, MCS needs to be able to build relying only on the
106 existence of a runtime and core libraries (corlib, System,
107 System.Xml). So there shouldn't be any checking for libraries or
108 whatnot; MCS should be able to build out of the box. We try to keep
109 platform detection and feature testing (ie, for HP/UX echo) inside
110 the makefiles; right now, there's no configuration script, and it'd
111 be nice to keep it that way. (I am told that some people build on
112 both Windows and Linux in the same tree, which would be impossible to
113 do if we cached platform-related configury values.)
115 That being said, it's very convenient for developers to be able to
116 customize their builds to suit their needs. To allow this, the
117 Makefile rules are set up to allow people to override pretty much any
118 important variable.
120 Configuration variables are given defaults in `config-default.make';
121 `rules.make' optionally includes `$(topdir)/build/config.make', so you
122 can customize your build without CVS trying to commit your modified
123 `config-default.make' all the time.  Platform-specific variables are
124 defined in `$(topdir)/build/platforms/$(PLATFORM).make', where
125 $(PLATFORM) is detected in config-default.make. (Currently, the only
126 choices are linux.make and win32.make.)
128 The best way to learn what the configuration variables are is to read
129 `config.make' and `platform.make'. There aren't too many and hopefully
130 they should be self-explanatory; see the numerous examples below for
131 more information if you're confused.
138 ** Recommendations for platform specifics
140 If you find yourself needing a platform-specific customization, try
141 and express it in terms of a feature, rather than a platform test. In
142 other words, this is good:
144 ========================================
145 run-test-local: my-test.exe
146 ifdef PLATFORM_NEEDS_CRAZY_CRAP
147         crazy-crap
148 endif
149         $(RUNTIME) my-test.exe
150 ========================================
152 and this is bad:
154 ========================================
155 run-test-local: my-test.exe
156 ifdef WINDOWS
157         crazy-crap
158 else
159 ifdef AMIGA
160         crazy-crap
161 endif
162 endif
163         $(RUNTIME) my-test.exe
164 ========================================
166 The latter accumulates and gets unpleasant and it sucks. Granted,
167 right now we only have two platforms, so it's not a big deal, but it's
168 good form to get used to and practice. Anyway, take a look at how we
169 do the various corlib building hacks for examples of how we've done
170 platform-specificity. It certainly isn't pretty, but at least it's a
171 little structured.
178 ** Saving effort
180         The point of the build system is to abstract things and take
181 care of all the easy stuff. So if you find yourself writing a
182 Makefile, know that there's probably already infrastructure to do what
183 you want. Here are all the common cases I can think of ...
190 * Compiling C# code? use:
192 ========================================
193 my-program.exe: my-source.cs
194          $(CSCOMPILE) /target:exe /out:$@ $^
195 ========================================
197         or
199 ========================================
200 my-lib.dll: my-source.cs
201          $(CSCOMPILE) /target:library /out:$@ $^
202 ========================================
204 Note the '$@' and '$^' variables. The former means "the name of the
205 file that I am trying to make" and the latter means "all the
206 dependencies of the file I am trying to make." USE THESE VARIABLES
207 AGGRESSIVELY. Say that you add a new source to your program:
209 ========================================
210 my-program.exe: my-source.cs my-new-source.cs
211          $(CSCOMPILE) /target:exe /out:$@ $^
212 ========================================
214 Because of the $^ variable, you don't need to remember to add another
215 file to the command line. Similarly, if you rename your program, you
216 won't need to remember to change the rule:
218 ========================================
219 MonoVaporizer.exe: my-source.cs my-new-source.cs
220          $(CSCOMPILE) /target:exe /out:$@ $^
221 ========================================
223 will still work. Another useful variable is $<, which means "the first
224 dependency of whatever I'm building." If you order your dependencies
225 carefully it can be extremely useful.
231 * Just building an executable? use:
233 ========================================
234 PROGRAM = myprogram.exe
235 LOCAL_MCS_FLAGS = /r:System.Xml.dll
237 include ../build/executable.make
238 ========================================
240 executable.make builds a program in the current directory. Its name is
241 held in $(PROGRAM), and its sources are listed in the file
242 $(PROGRAM).sources. It might seem to make more sense to just list the
243 program's sources in the Makefile, but when we build on Windows we
244 need to change slashes around, which is much easier to do if the
245 sources are listed in a file. The variable $(LOCAL_MCS_FLAGS) changes
246 the flags given to the compiler; it is included in $(CSCOMPILE) so you
247 don't need to worry about it.
249 executable.make does a lot for you: it builds the program in 'make
250 all-local', installs the program in $(prefix)/bin, distributes the
251 sources, and defines empty test targets. Now, if your program has a
252 test, set the variable HAS_TEST:
254 ========================================
255 PROGRAM = myprogram.exe
256 LOCAL_MCS_FLAGS = /r:System.Xml.dll
257 HAS_TEST = yes
258 include ../build/executable.make
260 test-local: mytester.exe
262 run-test-local: mytester.exe
263         $(RUNTIME) $<
265 mytester.exe: mytester.cs
266         $(CSCOMPILE) /target:exe /out:$@ mytester.cs
267 ========================================
269 If your program has 'built sources', that is, source files generated
270 from other files (say, generated by jay), define a variable called
271 BUILT_SOURCES and do *not* list the sources in $(PROGRAM).sources:
273 ========================================
274 PROGRAM = myprogram.exe
275 LOCAL_MCS_FLAGS = /r:System.Xml.dll
276 BUILT_SOURCES = parser.cs
277 CLEAN_FILES = y.output
279 include ../build/executable.make
281 parser.cs: parser.jay
282         $(topdir)/jay/jay $< > $@
283 ========================================
285 executable.make will automatically delete the $(BUILT_SOURCES) files
286 on 'make clean'. Since this situation is a common occurrence and jay
287 happens to leave behind y.output files, you can also define a variable
288 called $(CLEAN_FILES) that lists extra files to be deleted when 'make clean' is
289 called. (That's in addition to your executable and the built sources).
296 * Buildling a library? Use
298 ========================================
299 LIBRARY = Mono.MyLib.dll
300 LIB_MCS_FLAGS = /unsafe
301 TEST_MCS_FLAGS = /r:System.Xml.dll
303 include ../../build/library.make
304 ========================================
306 Where you library is called $(LIBRARY); it will be put into
307 $(topdir)/class/lib. LIB_MCS_FLAGS is the set of MCS flags to use when
308 compiling the library; in addition, a global set of flags called
309 $(LIBRARY_FLAGS) is added (that variable is defined in
310 config-defaults.make), as well as the usual $(LOCAL_MCS_FLAGS).
312 As in executable.make, the sources for your library are listed in
313 $(LIBRARY).sources. Note: these source lists should have Unix forward
314 slashes and Unix newlines (\n, not \r\n.) If you get an error about
315 "touch: need a filename", that means your .sources file doesn't end in
316 a newline. It should.
318 Now library.make also assumes that your library has an NUnit2 test
319 harness. The files should be in a subdirectory called Test/, and if
320 your library is called Mono.Foo.dll, they should be listed in
321 Mono.Foo_test.dll.sources. The names in that files should *not* have
322 the Test/ prefix. 'make test' will build Mono.Foo_test.dll in the
323 current directory, automatically supplying the flags to reference the
324 original library and NUnit.Framework.dll. 
326 If you don't have a test, just do this:
328 ========================================
329 LIBRARY = Mono.MyLib.dll
330 LIB_MCS_FLAGS = /unsafe
331 NO_TEST = yes
333 include ../../build/library.make
334 ========================================
336 and feel ashamed. Every good library has a test suite!
338 Extra flags needed to compile the test library should be listed in
339 $(TEST_MCS_FLAGS); often you will have a line like this:
341 ========================================
342 TEST_MCS_FLAGS = $(LIB_MCS_FLAGS)
343 ========================================
345 Again, library.make does a lot for you: it builds the dll, it
346 generates makefile fragments to track the dependencies, it installs
347 the library, it builds the test dll on 'make test', it runs
348 $(TEST_HARNESS) on it on 'make run-test', it removes the appropriate
349 files on 'make clean', and it distributes all the source files on
350 'make dist'. (TEST_HARNESS defaults to be nunit-console.exe but it may
351 be overridden to, say, nunit-gtk). If you have extra files to
352 distribute when using either library.make or executable.make, use the
353 variable $(EXTRA_DISTFILES):
355 ========================================
356 EXTRA_DISTFILES = \
357         Test/testcase1.in               \
358         Test/testcase1.out              \
359         README
360 ========================================
362 Again, library.make and executable.make do the right things so that we
363 can build on Windows, doing some trickery to invert slashes and
364 overcome command-line length limitations. Use them unless you have a
365 really good reason not to. If you're building a bunch of small
366 executables, check out tools/Makefile or tools/security/Makefile; if
367 all the files are in the current directory, changing slashes isn't a
368 big deal, and command-line lengths won't be a problem, so
369 executable.make isn't necessary (and indeed it won't work, since it
370 can only build one .exe in a directory).
372 If you're building a library, library.make is highly recommended; the
373 only DLL that doesn't use it is corlib, because building corlib is a
374 fair bit more complicated than it should be. Oh well.
376 library.make also automatically supports generating and updating 
377 monodoc documentation.  Documentation is stored within the 
378 Documentation directory (a sibling to the Test directory), and is 
379 generated/updated whenever the doc-update target is executed.  
380 Assembling of the documentation so that the monodoc browser can
381 display the documentation is handled separately within the mcs/docs
382 all-local target; see mcs/docs/Makefile for details.
389 * Running a C# program? Use $(RUNTIME)
391 ========================================
392 run-test-local: myprog.exe
393         $(RUNTIME) myprog.exe
394 ========================================
396 $(RUNTIME) might be empty (if you're on windows), so don't expect to
397 be able to give it any arguments. If you're on a platform which has an
398 interpreter or jitter, $(RUNTIME_FLAGS) is included in $(RUNTIME), so
399 set that variable.
401 $(TEST_RUNTIME) is the runtime to use when running tests. Right now it's
402 just "mono --debug".
406 * Calling the compiler directly? Use $(MCS).
408 Really, you should use $(CSCOMPILE) whenever possible, but $(MCS) is
409 out there. $(BOOTSTRAP_MCS) is the C# compiler that we use to build
410 mcs.exe; on Linux, we then use mcs.exe to build everything else, but
411 on Windows, we use csc.exe to build everything. Only use
412 $(BOOTSTRAP_MCS) if you know what you're doing.
418 * Compiling C code? Use $(CCOMPILE)
420 To give it flags, set $(LOCAL_CFLAGS). As with compiling C#, the
421 variable $(CFLAGS) will automatically be included on the command line.
427 * Documentation-related needs? Use $(MDOC)
429 $(MDOC) is a front-end to the monodoc documentation system, supporting
430 documentation generation, updating, importing from Microsoft XML
431 Documentation and ECMA documentation formats, assembling documentation
432 for use within the monodoc documentation browser, and exporting
433 documentation to various other output formats such as static HTML.
435 It is currently only used for library.make's doc-update-local target
436 and for assembling documentation within $topdir/docs.
442 * Installing files? Use $(MKINSTALLDIRS), $(INSTALL_DATA) or
443 $(INSTALL_BIN), $(prefix), and $(DESTDIR).
445 Every time a file is installed the commands should look like this:
447 ========================================
448 install-local:
449         $(MKINSTALLDIRS) $(DESTDIR)$(prefix)/my/dir
450         $(INSTALL_DATA) myfile $(DESTDIR)$(prefix)/my/dir
451 ========================================
453 This way the directory is created recursively if needed (admittedly, we could
454 probably rely on mkdir -p), the file is given the correct permissions,
455 the user can override $(MKINSTALLDIRS) and $(INSTALL) if they need to,
456 and we can support $(DESTDIR) installs. We use $(DESTDIR) to make
457 monocharge tarballs, and it's useful otherwise, so try and use it
458 consistently.
464 * 'make dist'? Use $(DISTFILES)
466 The 'dist-default' target will copy the files listed in $(DISTFILES)
467 into the distribution directory, as well as Makefile and ChangeLog if
468 they exist. This is almost always all that you need, so ideally your
469 make dist support should only be:
471 ========================================
472 DISTFILES = README Test/thoughts.txt
474 dist-local: dist-default
475 ========================================
477 DISTFILES will cope correctly with files in subdirectories, by the
478 way. Note that if you put a nonexistant file or a directory in
479 DISTFILES it will *not* complain; it will just ignore it.
481 If you want to test your 'make dist' code, you can try
483 ========================================
484 $ cd class/Mono.MyClass
485 $ make dist-local distdir=TEST
486 ========================================
488 And your files should be copied into TEST/ in the current directory.
489 There is a toplevel 'make distcheck' target, which will build a dist
490 tarball, try to build it, install files to a temporary prefix, make
491 clean it, make a distribution, and compare the files left over to the
492 files originally in the tarball: they should be the same. But this
493 takes about 15 minutes to run on my 1.1 Ghz computer, so it's not for
494 the faint of heart.
500 * Lots of files? Use $(wildcard *.foo)
502 When specifying the sources to a library or executable, wildcards are
503 not encouraged; in fact they're not allowed if you use library.make or
504 executable.make. But there are times when they're useful, eg:
506 ========================================
507 DISTFILES = $(wildcard Test/*.in) $(wildcard Test/*.out)
508 ========================================
510 Just so you know that 'make' has this feature.
517 * Referencing files in other directories? Use $(topdir).
519 $(topdir) is the path to the top directory from the current build
520 directory. Basically it's a sequence of ../.. computed from the value
521 that you give $(thisdir) at the top of your Makefile. Try to reference
522 things from $(topdir), so your code can be moved or cut-and-pasted
523 around with a minimum of fuss.
530 * Conditional building? Use ifdef/ifndef/endif
532 Now in general we want to avoid conditional building, but sometimes
533 something doesn't work on Linux or already exists on Windows or
534 whatnot. (See below on recommended form for how to build
535 platform-specifically.) GNU Make supports the following construction:
537 ========================================
538 BUILD_EXPERIMENTAL = yes
540 ifdef BUILD_EXPERIMENTAL
541 experimental_stuff = my-experiment.exe
542 else
543 experimental_stuff = 
544 endif
546 all-local: my-sane.exe $(experimental_stuff)
547 ========================================
549 'ifdef' means 'if the variable is set to nonempty', so you could have 
551 ========================================
552 BUILD_EXPERIMENTAL = colorless green ideas sleep furiously
553 ========================================
555 and Make would be happy. I hope that the meaning of 'ifndef' should be
556 obvious. If you want to only sometimes build a target, the above
557 construction is the recommended way to go about it; it's nice to have
558 the rules exist in a Makefile even if they aren't invoked.
560 If you want to see why conditionals aren't nice, take a look at
561 library.make or class/corlib/Makefile.
567 * 'Private' directories that shouldn't be built by default? Use DIST_ONLY_SUBDIRS
569 Several of the MCS class libraries have demo or experimental
570 implementations that depend on things not included with MCS (say,
571 Gtk#). We don't want to build them by default, because the user might
572 not have those dependencies installed, but it's nice to have a
573 Makefile for them to be built nicely.
575 First of all, there's nothing stopping you from writing a Makefile for
576 such a directory; just don't put it in the SUBDIRS line of its parent
577 directory. That way, you can do all the normal build things like 'make
578 all' or 'make clean' in that directory, but people trying to bootstrap
579 their system won't run into problems.
581 At the same time you probably want to include this directory in the
582 distribution so that people can use your demo or experimental code if
583 they know what they're doing. Hence the variable
584 $(DIST_ONLY_SUBDIRS). As you might guess, it's like the SUBDIRS
585 variable: it lists subdirectories that a regular shouldn't recurse
586 into, but should have their 'make dist' rules invoked. 
588 Say you've written Mono.MyFancyLib.dll and you have
589 a demo app using Gtk# called MyFancyDemo. The Makefile rules might
590 look like this:
592 class/Mono.MyFancyLib/Makefile
593 ========================================
594 thisdir = class/Mono.MyFancyLib
595 SUBDIRS =
596 DIST_ONLY_SUBDIRS = MyFancyDemo
597 include ../../build/rules.make
599 LIBRARY = Mono.MyFancyLib.dll
600 LIB_MCS_FLAGS = /r:System.dll
601 TEST_MCS_FLAGS = $(LIB_MCS_FLAGS)
603 include ../../build/library.make
604 ========================================
606 class/Mono.MyFancyLib/MyFancyDemo/Makefile
607 ========================================
608 thisdir = class/Mono.MyFancyLib/MyFancyDemo
609 SUBDIRS =
610 include ../../../build/rules.make
612 PROGRAM = FancyDemo.exe
613 LOCAL_MCS_FLAGS = /r:gtk-sharp.dll
615 include ../../../build/executable.make
616 ========================================
621 * Special recursion needs?
623 By default, rules.make defines the all, install, clean, etc. targets
624 to look something like this:
626    all: all-recursive
627         $(MAKE) all-local
629 Sometimes that doesn't cut it; say for example you want to check for
630 something before doing a lengthy recursive build (see
631 $(topdir)/Makefile) or you have a something like this
633         class/MyLibrary:
634                 Build MyLibrary.dll
635         class/MyLibrary/Test:
636                 Build TestMyLibrary.exe
638 'make clean test' will fail here, because the build will happen in
639 the Test subdirectory first, so there will be no MyLibrary.dll to link
640 against. (Unless you write a nasty evil relative path rule which is
641 strongly discouraged.)
643 Anyway, to solve this problem you can do
645 ========================================
646 thisdir = class/MyLibrary
647 SUBDIRS = Test
648 include ../../build/rules.make
650 # Normally, make runs 'all-recursive' first, and then 'all-local'
651 # With this, we ensure that 'all-local' is executed first.
652 all-recursive: all-local
654 test-recursive: test-local
656 ========================================
662 ** A few implementation details
664 The way rules.make does its recursion is very standard; it maps
665 {all,install,clean, dist,test} to $@-recursive, which executes that rule
666 in each directory in $(SUBDIRS), and then calls $@-local in the current
667 directory. So something that gets built in a subdirectory cannot rely on
668 something that gets built in its parent directory. If this is a problem,
669 see the previous section.  Note that the recursive rule for 'dist' is
670 different; it makes dist-recursive in subdirectories, so you at least
671 have to define that rule.
673 Note that even a directory that doesn't, for example, have any tests
674 must still define test-local; otherwise 'make test' run from the
675 toplevel directory will break.
682 ** Flags for Tools
684 We want to make it so that the user can specify certain flags to
685 always be given to a tool, so there's a general way of implementing
686 FLAGS variables:
688         * $(foo_FLAGS) remains unset or defaulted to something
689           sensible; the user can provide overrides this way.
691         * $(LOCAL_foo_FLAGS) is set in a specific Makefile to
692           provide necessary values.
694         * $(PLATFORM_foo_FLAGS) is set in the platform configuration
695           to provide platform-specific values.
697         * $(PROFILE_foo_FLAGS) is set in the profile configuration
698           to provide profile-specific values.
700         * $(USE_foo_FLAGS) is defined to be the combination of all of
701           the above, and it's what is actually passed to $(foo).
703 $(MCS_FLAGS) and $(CFLAGS) follow this model. If you end up finding
704 that another tool is used commonly (hm, jay...), please follow this form.
711 ** Portability tips
713 Always use the icky Windows /argument way of passing parameters to the C#
714 compiler so that csc can be used.
716 Always use /r:foo.dll, not /r:foo. Windows requires the former.
718 Use /r:$(corlib), not /r:corlib.
720 If you're writing shell script code as part of a make rule, remember
721 that Windows has command-line length limits. So something like
723 ========================================
724 mytool $(all_the_sources_to_corlib)
725 ========================================
727 Is probably going to cause problems. As I understand it, 
729 ========================================
730 for f in  $(all_the_sources_to_corlib) ; do ...
731 ========================================
733 is ok, since the shell itself doesn't have those limitations. Other
734 than that, you should still try to write fairly portable shell
735 script. Linux and Cygwin both use the GNU utilities, but there's at
736 least one hardy soul trying to build Mono on HP/UX, and no doubt there
737 will be ports to more Unices as time goes on.
744 ** Misc
746 We still don't use /d:NET_1_1 ; it causes some build problems right
747 now.
749 There's a hack in class/System.Data/Makefile to work around a very
750 strange crash in the runtime with some custom attribute stuff. It'd be
751 nice to fix it.
753 Also, there's a /lib:$(prefix)/lib in the System.dll Makefile, which
754 is for some reason necessary if System.Xml.dll hasn't been built yet.
755 (Well, it's necessary because of the /r:System.Xml.dll, but that
756 should be in the search path, it seems.)
758 A lot of the weird targets in the old makefiles have been dropped; I
759 have a feeling that a lot of them are archaic and not needed anymore.
761 I'd really like to write a build tool in C#. It would be nice to have
762 something really extensible and well-designed and clean. NAnt is,
763 IMHO, an apalling abomination and a tragically bad attempt at solving
764 the software building problem. Just so you know.
766 (On the other hand, NUnit is really neat.)
768 Peter