Add setting of The WRAP flag in the Wrap property's set method.
[mono.git] / mcs / build / README.makefiles
1 The MCS makefiles (-*- outline -*-)
2 Peter Williams <peter@newton.cx>
3
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:
7
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
15
16
17
18
19 ** Makefile structure
20
21 A general makefile looks like this:
22
23 ========================================
24 thisdir = class/Mono.My.Library
25 SUBDIRS =
26 include ../../build/rules.make
27
28 all-local:
29         do some stuff
30
31 install-local:
32         $(MKINSTALLDIRS) $(DESTDIR)$(prefix)/share/
33         $(INSTALL_DATA) myfile.txt $(DESTDIR)$(prefix)/share/myfiledir
34
35 clean-local:
36         rm -f my-generated-file
37
38 test-local: my_test_program.exe
39
40 run-test-local:
41         $(RUNTIME) my_test_program.exe
42
43 DISTFILES = myfile.txt my_test_source.cs
44
45 dist-local: dist-default
46
47 my_test_program.exe: my_test_source.cs
48         $(CSCOMPILE) /target:exe /out:$@ $<
49 ========================================
50
51 Each makefile follows the same pattern: it does some setup, includes
52 the standard make rules, and provides rules for six standard targets:
53 all, install, test, run-test, clean, and dist.
54
55 "Some setup" is defining two variables: $(thisdir) and
56 $(SUBDIRS). $(thisdir) is the directory that the makefile lives in,
57 relative to the top directory (ie, class/corlib) and $(SUBDIRS)
58 defines the subdirectories that should be built in.
59
60 The six targets do the following:
61
62         * all-local builds whatever someone would expect to be built
63 when they just type 'make'. Most likely Foo.dll or Foo.exe
64
65         * install-local installs whatever got built by all-local.
66
67         * test-local _builds_ the test programs or libraries but does
68 _not_ run them.
69
70         * run-test-local actually runs the tests. It shouldn't
71 necessarily exit in an error if the test fails, but should make that
72 situation obvious. It should only run tests that take care of
73 themselves automatically; interactive tests should have an individual
74 target. The idea is that 'make run-test' from the toplevel should be
75 able to proceed unsupervised and test everything that can be tested in
76 such a manner.
77
78         * clean-local removes built files; 'make clean' should leave
79 only files that go into a distribution tarball. (But it is not necessarily
80 true that all files that go into a tarball need to be left after a make clean.)
81
82         * dist-local copies files into the distribution tree, which is
83 given by the variable $(distdir). dist-local always depends on the
84 target 'dist-default'. See ** 'make dist' below.
85
86
87
88
89
90 ** Build configuration
91
92 In general, MCS needs to be able to build relying only on the
93 existence of a runtime and core libraries (corlib, System,
94 System.Xml). So there shouldn't be any checking for libraries or
95 whatnot; MCS should be able to build out of the box. We try to keep
96 platform detection and feature testing (ie, for HP/UX echo) inside
97 the makefiles; right now, there's no configuration script, and it'd
98 be nice to keep it that way. (I am told that some people build on
99 both Windows and Linux in the same tree, which would be impossible to
100 do if we cached platform-related configury values.)
101
102 That being said, it's very convenient for developers to be able to
103 customize their builds to suit their needs. To allow this, the
104 Makefile rules are set up to allow people to override pretty much any
105 important variable.
106
107 Configuration variables are given defaults in `config-default.make';
108 `rules.make' optionally includes `$(topdir)/build/config.make', so you
109 can customize your build without CVS trying to commit your modified
110 `config-default.make' all the time.  Platform-specific variables are
111 defined in `$(topdir)/build/platforms/$(PLATFORM).make', where
112 $(PLATFORM) is detected in config-default.make. (Currently, the only
113 choices are linux.make and win32.make.)
114
115 The best way to learn what the configuration variables are is to read
116 `config.make' and `platform.make'. There aren't too many and hopefully
117 they should be self-explanatory; see the numerous examples below for
118 more information if you're confused.
119
120
121
122
123
124
125 ** Recommendations for platform specifics
126
127 If you find yourself needing a platform-specific customization, try
128 and express it in terms of a feature, rather than a platform test. In
129 other words, this is good:
130
131 ========================================
132 run-test-local: my-test.exe
133 ifdef PLATFORM_NEEDS_CRAZY_CRAP
134         crazy-crap
135 endif
136         $(RUNTIME) my-test.exe
137 ========================================
138
139 and this is bad:
140
141 ========================================
142 run-test-local: my-test.exe
143 ifdef WINDOWS
144         crazy-crap
145 else
146 ifdef AMIGA
147         crazy-crap
148 endif
149 endif
150         $(RUNTIME) my-test.exe
151 ========================================
152
153 The latter accumulates and gets unpleasant and it sucks. Granted,
154 right now we only have two platforms, so it's not a big deal, but it's
155 good form to get used to and practice. Anyway, take a look at how we
156 do the various corlib building hacks for examples of how we've done
157 platform-specificity. It certainly isn't pretty, but at least it's a
158 little structured.
159
160
161
162
163
164
165 ** Saving effort
166
167         The point of the build system is to abstract things and take
168 care of all the easy stuff. So if you find yourself writing a
169 Makefile, know that there's probably already infrastructure to do what
170 you want. Here are all the common cases I can think of ...
171
172
173
174
175
176
177 * Compiling C# code? use:
178
179 ========================================
180 my-program.exe: my-source.cs
181          $(CSCOMPILE) /target:exe /out:$@ $^
182 ========================================
183
184         or
185
186 ========================================
187 my-lib.dll: my-source.cs
188          $(CSCOMPILE) /target:library /out:$@ $^
189 ========================================
190
191 Note the '$@' and '$^' variables. The former means "the name of the
192 file that I am trying to make" and the latter means "all the
193 dependencies of the file I am trying to make." USE THESE VARIABLES
194 AGGRESSIVELY. Say that you add a new source to your program:
195
196 ========================================
197 my-program.exe: my-source.cs my-new-source.cs
198          $(CSCOMPILE) /target:exe /out:$@ $^
199 ========================================
200
201 Because of the $^ variable, you don't need to remember to add another
202 file to the command line. Similarly, if you rename your program, you
203 won't need to remember to change the rule:
204
205 ========================================
206 MonoVaporizer.exe: my-source.cs my-new-source.cs
207          $(CSCOMPILE) /target:exe /out:$@ $^
208 ========================================
209
210 will still work. Another useful variable is $<, which means "the first
211 dependency of whatever I'm building." If you order your dependencies
212 carefully it can be extremely useful.
213
214
215
216
217
218 * Just building an executable? use:
219
220 ========================================
221 PROGRAM = myprogram.exe
222 LOCAL_MCS_FLAGS = /r:System.Xml.dll
223
224 include ../build/executable.make
225 ========================================
226
227 executable.make builds a program in the current directory. Its name is
228 held in $(PROGRAM), and its sources are listed in the file
229 $(PROGRAM).sources. It might seem to make more sense to just list the
230 program's sources in the Makefile, but when we build on Windows we
231 need to change slashes around, which is much easier to do if the
232 sources are listed in a file. The variable $(LOCAL_MCS_FLAGS) changes
233 the flags given to the compiler; it is included in $(CSCOMPILE) so you
234 don't need to worry about it.
235
236 executable.make does a lot for you: it builds the program in 'make
237 all-local', installs the program in $(prefix)/bin, distributes the
238 sources, and defines empty test targets. Now, if your program has a
239 test, set the variable HAS_TEST:
240
241 ========================================
242 PROGRAM = myprogram.exe
243 LOCAL_MCS_FLAGS = /r:System.Xml.dll
244 HAS_TEST = yes
245 include ../build/executable.make
246
247 test-local: mytester.exe
248
249 run-test-local: mytester.exe
250         $(RUNTIME) $<
251
252 mytester.exe: mytester.cs
253         $(CSCOMPILE) /target:exe /out:$@ mytester.cs
254 ========================================
255
256 If your program has 'built sources', that is, source files generated
257 from other files (say, generated by jay), define a variable called
258 BUILT_SOURCES and do *not* list the sources in $(PROGRAM).sources:
259
260 ========================================
261 PROGRAM = myprogram.exe
262 LOCAL_MCS_FLAGS = /r:System.Xml.dll
263 BUILT_SOURCES = parser.cs
264 CLEAN_FILES = y.output
265
266 include ../build/executable.make
267
268 parser.cs: parser.jay
269         $(topdir)/jay/jay $< > $@
270 ========================================
271
272 executable.make will automatically delete the $(BUILT_SOURCES) files
273 on 'make clean'. Since this situation is a common occurrence and jay
274 happens to leave behind y.output files, you can also define a variable
275 called $(CLEAN_FILES) that lists extra files to be deleted when 'make clean' is
276 called. (That's in addition to your executable and the built sources).
277
278
279
280
281
282
283 * Buildling a library? Use
284
285 ========================================
286 LIBRARY = Mono.MyLib.dll
287 LIB_MCS_FLAGS = /unsafe
288 TEST_MCS_FLAGS = /r:System.Xml.dll
289
290 include ../../build/library.make
291 ========================================
292
293 Where you library is called $(LIBRARY); it will be put into
294 $(topdir)/class/lib. LIB_MCS_FLAGS is the set of MCS flags to use when
295 compiling the library; in addition, a global set of flags called
296 $(LIBRARY_FLAGS) is added (that variable is defined in
297 config-defaults.make), as well as the usual $(LOCAL_MCS_FLAGS).
298
299 As in executable.make, the sources for your library are listed in
300 $(LIBRARY).sources. Note: these source lists should have Unix forward
301 slashes and Unix newlines (\n, not \r\n.) If you get an error about
302 "touch: need a filename", that means your .sources file doesn't end in
303 a newline. It should.
304
305 Now library.make also assumes that your library has an NUnit2 test
306 harness. The files should be in a subdirectory called Test/, and if
307 your library is called Mono.Foo.dll, they should be listed in
308 Mono.Foo_test.dll.sources. The names in that files should *not* have
309 the Test/ prefix. 'make test' will build Mono.Foo_test.dll in the
310 current directory, automatically supplying the flags to reference the
311 original library and NUnit.Framework.dll. 
312
313 If you don't have a test, just do this:
314
315 ========================================
316 LIBRARY = Mono.MyLib.dll
317 LIB_MCS_FLAGS = /unsafe
318 NO_TEST = yes
319
320 include ../../build/library.make
321 ========================================
322
323 and feel ashamed. Every good library has a test suite!
324
325 Extra flags needed to compile the test library should be listed in
326 $(TEST_MCS_FLAGS); often you will have a line like this:
327
328 ========================================
329 TEST_MCS_FLAGS = $(LIB_MCS_FLAGS)
330 ========================================
331
332 Again, library.make does a lot for you: it builds the dll, it
333 generates makefile fragments to track the dependencies, it installs
334 the library, it builds the test dll on 'make test', it runs
335 $(TEST_HARNESS) on it on 'make run-test', it removes the appropriate
336 files on 'make clean', and it distributes all the source files on
337 'make dist'. (TEST_HARNESS defaults to be nunit-console.exe but it may
338 be overridden to, say, nunit-gtk). If you have extra files to
339 distribute when using either library.make or executable.make, use the
340 variable $(EXTRA_DISTFILES):
341
342 ========================================
343 EXTRA_DISTFILES = \
344         Test/testcase1.in               \
345         Test/testcase1.out              \
346         README
347 ========================================
348
349 Again, library.make and executable.make do the right things so that we
350 can build on Windows, doing some trickery to invert slashes and
351 overcome command-line length limitations. Use them unless you have a
352 really good reason not to. If you're building a bunch of small
353 executables, check out tools/Makefile or tools/security/Makefile; if
354 all the files are in the current directory, changing slashes isn't a
355 big deal, and command-line lengths won't be a problem, so
356 executable.make isn't necessary (and indeed it won't work, since it
357 can only build one .exe in a directory).
358
359 If you're building a library, library.make is highly recommended; the
360 only DLL that doesn't use it is corlib, because building corlib is a
361 fair bit more complicated than it should be. Oh well.
362
363
364
365
366
367
368 * Running a C# program? Use $(RUNTIME)
369
370 ========================================
371 run-test-local: myprog.exe
372         $(RUNTIME) myprog.exe
373 ========================================
374
375 $(RUNTIME) might be empty (if you're on windows), so don't expect to
376 be able to give it any arguments. If you're on a platform which has an
377 interpreter or jitter, $(RUNTIME_FLAGS) is included in $(RUNTIME), so
378 set that variable.
379
380 $(TEST_RUNTIME) is the runtime to use when running tests. Right now it's
381 just "mono --debug".
382
383
384
385 * Calling the compiler directly? Use $(MCS).
386
387 Really, you should use $(CSCOMPILE) whenever possible, but $(MCS) is
388 out there. $(BOOTSTRAP_MCS) is the C# compiler that we use to build
389 mcs.exe; on Linux, we then use mcs.exe to build everything else, but
390 on Windows, we use csc.exe to build everything. Only use
391 $(BOOTSTRAP_MCS) if you know what you're doing.
392
393
394
395
396
397 * Compiling C code? Use $(CCOMPILE)
398
399 To give it flags, set $(LOCAL_CFLAGS). As with compiling C#, the
400 variable $(CFLAGS) will automatically be included on the command line.
401
402
403
404
405
406 * Installing files? Use $(MKINSTALLDIRS), $(INSTALL_DATA) or
407 $(INSTALL_BIN), $(prefix), and $(DESTDIR).
408
409 Every time a file is installed the commands should look like this:
410
411 ========================================
412 install-local:
413         $(MKINSTALLDIRS) $(DESTDIR)$(prefix)/my/dir
414         $(INSTALL_DATA) myfile $(DESTDIR)$(prefix)/my/dir
415 ========================================
416
417 This way the directory is created recursively if needed (admittedly, we could
418 probably rely on mkdir -p), the file is given the correct permissions,
419 the user can override $(MKINSTALLDIRS) and $(INSTALL) if they need to,
420 and we can support $(DESTDIR) installs. We use $(DESTDIR) to make
421 monocharge tarballs, and it's useful otherwise, so try and use it
422 consistently.
423
424
425
426
427
428 * 'make dist'? Use $(DISTFILES)
429
430 The 'dist-default' target will copy the files listed in $(DISTFILES)
431 into the distribution directory, as well as Makefile and ChangeLog if
432 they exist. This is almost always all that you need, so ideally your
433 make dist support should only be:
434
435 ========================================
436 DISTFILES = README Test/thoughts.txt
437
438 dist-local: dist-default
439 ========================================
440
441 DISTFILES will cope correctly with files in subdirectories, by the
442 way. Note that if you put a nonexistant file or a directory in
443 DISTFILES it will *not* complain; it will just ignore it.
444
445 If you want to test your 'make dist' code, you can try
446
447 ========================================
448 $ cd class/Mono.MyClass
449 $ make dist-local distdir=TEST
450 ========================================
451
452 And your files should be copied into TEST/ in the current directory.
453 There is a toplevel 'make distcheck' target, which will build a dist
454 tarball, try to build it, install files to a temporary prefix, make
455 clean it, make a distribution, and compare the files left over to the
456 files originally in the tarball: they should be the same. But this
457 takes about 15 minutes to run on my 1.1 Ghz computer, so it's not for
458 the faint of heart.
459
460
461
462
463
464 * Lots of files? Use $(wildcard *.foo)
465
466 When specifying the sources to a library or executable, wildcards are
467 not encouraged; in fact they're not allowed if you use library.make or
468 executable.make. But there are times when they're useful, eg:
469
470 ========================================
471 DISTFILES = $(wildcard Test/*.in) $(wildcard Test/*.out)
472 ========================================
473
474 Just so you know that 'make' has this feature.
475
476
477
478
479
480
481 * Referencing files in other directories? Use $(topdir).
482
483 $(topdir) is the path to the top directory from the current build
484 directory. Basically it's a sequence of ../.. computed from the value
485 that you give $(thisdir) at the top of your Makefile. Try to reference
486 things from $(topdir), so your code can be moved or cut-and-pasted
487 around with a minimum of fuss.
488
489
490
491
492
493
494 * Conditional building? Use ifdef/ifndef/endif
495
496 Now in general we want to avoid conditional building, but sometimes
497 something doesn't work on Linux or already exists on Windows or
498 whatnot. (See below on recommended form for how to build
499 platform-specifically.) GNU Make supports the following construction:
500
501 ========================================
502 BUILD_EXPERIMENTAL = yes
503
504 ifdef BUILD_EXPERIMENTAL
505 experimental_stuff = my-experiment.exe
506 else
507 experimental_stuff = 
508 endif
509
510 all-local: my-sane.exe $(experimental_stuff)
511 ========================================
512
513 'ifdef' means 'if the variable is set to nonempty', so you could have 
514
515 ========================================
516 BUILD_EXPERIMENTAL = colorless green ideas sleep furiously
517 ========================================
518
519 and Make would be happy. I hope that the meaning of 'ifndef' should be
520 obvious. If you want to only sometimes build a target, the above
521 construction is the recommended way to go about it; it's nice to have
522 the rules exist in a Makefile even if they aren't invoked.
523
524 If you want to see why conditionals aren't nice, take a look at
525 library.make or class/corlib/Makefile.
526
527
528
529
530
531 * 'Private' directories that shouldn't be built by default? Use DIST_ONLY_SUBDIRS
532
533 Several of the MCS class libraries have demo or experimental
534 implementations that depend on things not included with MCS (say,
535 Gtk#). We don't want to build them by default, because the user might
536 not have those dependencies installed, but it's nice to have a
537 Makefile for them to be built nicely.
538
539 First of all, there's nothing stopping you from writing a Makefile for
540 such a directory; just don't put it in the SUBDIRS line of its parent
541 directory. That way, you can do all the normal build things like 'make
542 all' or 'make clean' in that directory, but people trying to bootstrap
543 their system won't run into problems.
544
545 At the same time you probably want to include this directory in the
546 distribution so that people can use your demo or experimental code if
547 they know what they're doing. Hence the variable
548 $(DIST_ONLY_SUBDIRS). As you might guess, it's like the SUBDIRS
549 variable: it lists subdirectories that a regular shouldn't recurse
550 into, but should have their 'make dist' rules invoked. 
551
552 Say you've written Mono.MyFancyLib.dll and you have
553 a demo app using Gtk# called MyFancyDemo. The Makefile rules might
554 look like this:
555
556 class/Mono.MyFancyLib/Makefile
557 ========================================
558 thisdir = class/Mono.MyFancyLib
559 SUBDIRS =
560 DIST_ONLY_SUBDIRS = MyFancyDemo
561 include ../../build/rules.make
562
563 LIBRARY = Mono.MyFancyLib.dll
564 LIB_MCS_FLAGS = /r:System.dll
565 TEST_MCS_FLAGS = $(LIB_MCS_FLAGS)
566
567 include ../../build/library.make
568 ========================================
569
570 class/Mono.MyFancyLib/MyFancyDemo/Makefile
571 ========================================
572 thisdir = class/Mono.MyFancyLib/MyFancyDemo
573 SUBDIRS =
574 include ../../../build/rules.make
575
576 PROGRAM = FancyDemo.exe
577 LOCAL_MCS_FLAGS = /r:gtk-sharp.dll
578
579 include ../../../build/executable.make
580 ========================================
581
582
583
584
585 * Special recursion needs? Use OVERRIDE_BARE_TARGETS
586
587 By default, rules.make defines the all, install, clean, etc. targets
588 to look something like this:
589
590    all: all-recursive all-local
591
592 Sometimes that doesn't cut it; say for example you want to check for
593 something before doing a lengthy recursive build (see
594 $(topdir)/Makefile) or you have a something like this
595
596         class/MyLibrary:
597                 Build MyLibrary.dll
598         class/MyLibrary/Test:
599                 Build TestMyLibrary.exe
600
601 'make clean test' will fail here, because the build will happen in
602 the Test subdirectory first, so there will be no MyLibrary.dll to link
603 against. (Unless you write a nasty evil relative path rule which is
604 strongly discouraged.)
605
606 Anyway, to solve this problem you can do
607
608 ========================================
609 OVERRIDE_BARE_TARGETS = yes
610 thisdir = class/MyLibrary
611 SUBDIRS = Test
612 include ../../build/rules.make
613
614 # This is normally "all-recursive all-local"
615 all: all-local all-recursive
616
617 test: test-local test-recursive
618 ...
619 ========================================
620
621
622
623
624
625 ** A few implementation details
626
627 The way rules.make does its recursion is very standard; it maps
628 {all,install,clean, dist,test} to $@-recursive, which executes that
629 rule in each directory in $(SUBDIRS), and then calls $@-local in the
630 current directory. So something that gets built in a subdirectory
631 cannot rely on something that gets built in its parent directory. If
632 this is a problem, see the bit about using OVERRIDE_BARE_TARGETS;
633 since the recursive rules do $(MAKE) $* in their subdirectories,
634 changing the 'all' target will do the right thing in a recursive
635 build. Note that the recursive rule for 'dist' is different; it makes
636 dist-recursive in subdirectories, so you at least have to define that
637 rule, even if you use OVERRIDE_BARE_TARGETS.
638
639 Note that even a directory that doesn't, for example, have any tests
640 must still define test-local; otherwise 'make test' run from the
641 toplevel directory will break.
642
643
644
645
646
647
648 ** Flags for Tools
649
650 We want to make it so that the user can specify certain flags to
651 always be given to a tool, so there's a general way of implementing
652 FLAGS variables:
653
654         * $(foo_FLAGS) remains unset or defaulted to something
655           sensible; the user can provide overrides this way.
656
657         * $(LOCAL_foo_FLAGS) is set in a specific Makefile to
658           provide necessary values.
659
660         * $(PLATFORM_foo_FLAGS) is set in the platform configuration
661           to provide platform-specific values.
662
663         * $(PROFILE_foo_FLAGS) is set in the profile configuration
664           to provide profile-specific values.
665
666         * $(USE_foo_FLAGS) is defined to be the combination of all of
667           the above, and it's what is actually passed to $(foo).
668
669 $(MCS_FLAGS) and $(CFLAGS) follow this model. If you end up finding
670 that another tool is used commonly (hm, jay...), please follow this form.
671
672
673
674
675
676
677 ** Portability tips
678
679 Always use the icky Windows /argument way of passing parameters to the C#
680 compiler so that csc can be used.
681
682 Always use /r:foo.dll, not /r:foo. Windows requires the former.
683
684 Use /r:$(corlib), not /r:corlib. Windows wants mscorlib.dll and Linux
685 wants corlib.dll; eventually, the Mono runtime is supposed to accept
686 mscorlib.dll too, but in the meantime, use the variable.
687
688 If you're writing shell script code as part of a make rule, remember
689 that Windows has command-line length limits. So something like
690
691 ========================================
692 mytool $(all_the_sources_to_corlib)
693 ========================================
694
695 Is probably going to cause problems. As I understand it, 
696
697 ========================================
698 for f in  $(all_the_sources_to_corlib) ; do ...
699 ========================================
700
701 is ok, since the shell itself doesn't have those limitations. Other
702 than that, you should still try to write fairly portable shell
703 script. Linux and Cygwin both use the GNU utilities, but there's at
704 least one hardy soul trying to build Mono on HP/UX, and no doubt there
705 will be ports to more Unices as time goes on.
706
707
708
709
710
711
712 ** Misc
713
714 We still don't use /d:NET_1_1 ; it causes some build problems right
715 now.
716
717 There's a hack in class/System.Data/Makefile to work around a very
718 strange crash in the runtime with some custom attribute stuff. It'd be
719 nice to fix it.
720
721 Also, there's a /lib:$(prefix)/lib in the System.dll Makefile, which
722 is for some reason necessary if System.Xml.dll hasn't been built yet.
723 (Well, it's necessary because of the /r:System.Xml.dll, but that
724 should be in the search path, it seems.)
725
726 A lot of the weird targets in the old makefiles have been dropped; I
727 have a feeling that a lot of them are archaic and not needed anymore.
728
729 I'd really like to write a build tool in C#. It would be nice to have
730 something really extensible and well-designed and clean. NAnt is,
731 IMHO, an apalling abomination and a tragically bad attempt at solving
732 the software building problem. Just so you know.
733
734 (On the other hand, NUnit is really neat.)
735
736 Peter