]> git.sur5r.net Git - cc65/blob - src/cc65/main.c
Made several options that can be changed by #pragmas stackable.
[cc65] / src / cc65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                             cc65 main program                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2004 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 52                                              */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <errno.h>
40
41 /* common */
42 #include "abend.h"
43 #include "chartype.h"
44 #include "cmdline.h"
45 #include "cpu.h"
46 #include "debugflag.h"
47 #include "fname.h"
48 #include "mmodel.h"
49 #include "print.h"
50 #include "segnames.h"
51 #include "target.h"
52 #include "tgttrans.h"
53 #include "version.h"
54 #include "xmalloc.h"
55
56 /* cc65 */
57 #include "asmcode.h"
58 #include "compile.h"
59 #include "codeopt.h"
60 #include "error.h"
61 #include "global.h"
62 #include "incpath.h"
63 #include "input.h"
64 #include "macrotab.h"
65 #include "scanner.h"
66 #include "segments.h"
67
68
69
70 /*****************************************************************************/
71 /*                                   Code                                    */
72 /*****************************************************************************/
73
74
75
76 static void Usage (void)
77 /* Print usage information to stderr */
78 {
79     fprintf (stderr,
80              "Usage: %s [options] file\n"
81              "Short options:\n"
82              "  -A\t\t\tStrict ANSI mode\n"
83              "  -Cl\t\t\tMake local variables static\n"
84              "  -Dsym[=defn]\t\tDefine a symbol\n"
85              "  -I dir\t\tSet an include directory search path\n"
86              "  -O\t\t\tOptimize code\n"
87              "  -Oi\t\t\tOptimize code, inline more code\n"
88              "  -Or\t\t\tEnable register variables\n"
89              "  -Os\t\t\tInline some known functions\n"
90              "  -T\t\t\tInclude source as comment\n"
91              "  -V\t\t\tPrint the compiler version number\n"
92              "  -W\t\t\tSuppress warnings\n"
93              "  -d\t\t\tDebug mode\n"
94              "  -g\t\t\tAdd debug info to object file\n"
95              "  -h\t\t\tHelp (this text)\n"
96              "  -j\t\t\tDefault characters are signed\n"
97              "  -mm model\t\tSet the memory model\n"
98              "  -o name\t\tName the output file\n"
99              "  -r\t\t\tEnable register variables\n"
100              "  -t sys\t\tSet the target system\n"
101              "  -v\t\t\tIncrease verbosity\n"
102              "\n"
103              "Long options:\n"
104              "  --add-source\t\tInclude source as comment\n"
105              "  --ansi\t\tStrict ANSI mode\n"
106              "  --bss-name seg\tSet the name of the BSS segment\n"
107              "  --check-stack\t\tGenerate stack overflow checks\n"
108              "  --code-name seg\tSet the name of the CODE segment\n"
109              "  --codesize x\t\tAccept larger code by factor x\n"
110              "  --cpu type\t\tSet cpu type (6502, 65c02)\n"
111              "  --create-dep\t\tCreate a make dependency file\n"
112              "  --data-name seg\tSet the name of the DATA segment\n"
113              "  --debug\t\tDebug mode\n"
114              "  --debug-info\t\tAdd debug info to object file\n"
115              "  --debug-opt name\tDebug optimization steps\n"
116              "  --disable-opt name\tDisable an optimization step\n"
117              "  --enable-opt name\tEnable an optimization step\n"
118              "  --forget-inc-paths\tForget include search paths\n"
119              "  --help\t\tHelp (this text)\n"
120              "  --include-dir dir\tSet an include directory search path\n"
121              "  --list-opt-steps\tList all optimizer steps and exit\n"
122              "  --memory-model model\tSet the memory model\n"
123              "  --register-space b\tSet space available for register variables\n"
124              "  --register-vars\tEnable register variables\n"
125              "  --rodata-name seg\tSet the name of the RODATA segment\n"
126              "  --signed-chars\tDefault characters are signed\n"
127              "  --static-locals\tMake local variables static\n"
128              "  --target sys\t\tSet the target system\n"
129              "  --verbose\t\tIncrease verbosity\n"
130              "  --version\t\tPrint the compiler version number\n",
131              ProgName);
132 }
133
134
135
136 static void cbmsys (const char* sys)
137 /* Define a CBM system */
138 {
139     DefineNumericMacro ("__CBM__", 1);
140     DefineNumericMacro (sys, 1);
141 }
142
143
144
145 static void SetSys (const char* Sys)
146 /* Define a target system */
147 {
148     switch (Target = FindTarget (Sys)) {
149
150         case TGT_NONE:
151             break;
152
153         case TGT_MODULE:
154             AbEnd ("Cannot use `module' as a target for the compiler");
155             break;
156
157         case TGT_ATARI:
158             DefineNumericMacro ("__ATARI__", 1);
159             break;
160
161         case TGT_C16:
162             cbmsys ("__C16__");
163             break;
164
165         case TGT_C64:
166             cbmsys ("__C64__");
167             break;
168
169         case TGT_VIC20:
170             cbmsys ("__VIC20__");
171             break;
172
173         case TGT_C128:
174             cbmsys ("__C128__");
175             break;
176
177         case TGT_ACE:
178             cbmsys ("__ACE__");
179             break;
180
181         case TGT_PLUS4:
182             cbmsys ("__PLUS4__");
183             break;
184
185         case TGT_CBM510:
186             cbmsys ("__CBM510__");
187             break;
188
189         case TGT_CBM610:
190             cbmsys ("__CBM610__");
191             break;
192
193         case TGT_PET:
194             cbmsys ("__PET__");
195             break;
196
197         case TGT_BBC:
198             DefineNumericMacro ("__BBC__", 1);
199             break;
200
201         case TGT_APPLE2:
202             DefineNumericMacro ("__APPLE2__", 1);
203             break;
204
205         case TGT_GEOS:
206             /* Do not handle as a CBM system */
207             DefineNumericMacro ("__GEOS__", 1);
208             break;
209
210         case TGT_LUNIX:
211             DefineNumericMacro ("__LUNIX__", 1);
212             break;
213
214         case TGT_ATMOS:
215             DefineNumericMacro ("__ATMOS__", 1);
216             break;
217
218         case TGT_NES:
219             DefineNumericMacro ("__NES__", 1);
220             break;
221
222         case TGT_SUPERVISION:
223             DefineNumericMacro ("__SUPERVISION__", 1);
224             break;
225
226         default:
227             AbEnd ("Unknown target system type %d", Target);
228     }
229
230     /* Initialize the translation tables for the target system */
231     TgtTranslateInit ();
232 }
233
234
235
236 static void DoCreateDep (const char* OutputName)
237 /* Create the dependency file */
238 {
239     /* Make the dependency file name from the output file name */
240     char* DepName = MakeFilename (OutputName, ".u");
241
242     /* Open the file */
243     FILE* F = fopen (DepName, "w");
244     if (F == 0) {
245         Fatal ("Cannot open dependency file `%s': %s", DepName, strerror (errno));
246     }
247
248     /* Write the dependencies to the file */
249     WriteDependencies (F, OutputName);
250
251     /* Close the file, check for errors */
252     if (fclose (F) != 0) {
253         remove (DepName);
254         Fatal ("Cannot write to dependeny file (disk full?)");
255     }
256
257     /* Free the name */
258     xfree (DepName);
259 }
260
261
262
263 static void DefineSym (const char* Def)
264 /* Define a symbol on the command line */
265 {
266     const char* P = Def;
267
268     /* The symbol must start with a character or underline */
269     if (Def [0] != '_' && !IsAlpha (Def [0])) {
270         InvDef (Def);
271     }
272
273     /* Check the symbol name */
274     while (IsAlNum (*P) || *P == '_') {
275         ++P;
276     }
277
278     /* Do we have a value given? */
279     if (*P != '=') {
280         if (*P != '\0') {
281             InvDef (Def);
282         }
283         /* No value given. Define the macro with the value 1 */
284         DefineNumericMacro (Def, 1);
285     } else {
286         /* We have a value, P points to the '=' character. Since the argument
287          * is const, create a copy and replace the '=' in the copy by a zero
288          * terminator.
289          */
290         char* Q;
291         unsigned Len = strlen (Def)+1;
292         char* S = (char*) xmalloc (Len);
293         memcpy (S, Def, Len);
294         Q = S + (P - Def);
295         *Q++ = '\0';
296
297         /* Define this as a macro */
298         DefineTextMacro (S, Q);
299
300         /* Release the allocated memory */
301         xfree (S);
302     }
303 }
304
305
306
307 static void CheckSegName (const char* Seg)
308 /* Abort if the given name is not a valid segment name */
309 {
310     /* Print an error and abort if the name is not ok */
311     if (!ValidSegName (Seg)) {
312         AbEnd ("Segment name `%s' is invalid", Seg);
313     }
314 }
315
316
317
318 static void OptAddSource (const char* Opt attribute ((unused)),
319                           const char* Arg attribute ((unused)))
320 /* Add source lines as comments in generated assembler file */
321 {
322     AddSource = 1;
323 }
324
325
326
327 static void OptAnsi (const char* Opt attribute ((unused)),
328                      const char* Arg attribute ((unused)))
329 /* Compile in strict ANSI mode */
330 {
331     ANSI = 1;
332 }
333
334
335
336 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
337 /* Handle the --bss-name option */
338 {
339     /* Check for a valid name */
340     CheckSegName (Arg);
341
342     /* Set the name */
343     NewSegName (SEG_BSS, Arg);
344 }
345
346
347
348 static void OptCheckStack (const char* Opt attribute ((unused)),
349                            const char* Arg attribute ((unused)))
350 /* Handle the --check-stack option */
351 {
352     IS_Set (&CheckStack, 1);
353 }
354
355
356
357 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
358 /* Handle the --code-name option */
359 {
360     /* Check for a valid name */
361     CheckSegName (Arg);
362
363     /* Set the name */
364     NewSegName (SEG_CODE, Arg);
365 }
366
367
368
369 static void OptCodeSize (const char* Opt, const char* Arg)
370 /* Handle the --codesize option */
371 {
372     /* Numeric argument expected */
373     if (sscanf (Arg, "%u", &CodeSizeFactor) != 1 ||
374         CodeSizeFactor < 100 ||
375         CodeSizeFactor > 1000) {
376         AbEnd ("Argument for %s is invalid", Opt);
377     }
378 }
379
380
381
382 static void OptCreateDep (const char* Opt attribute ((unused)),
383                           const char* Arg attribute ((unused)))
384 /* Handle the --create-dep option */
385 {
386     CreateDep = 1;
387 }
388
389
390
391 static void OptCPU (const char* Opt, const char* Arg)
392 /* Handle the --cpu option */
393 {
394     /* Find the CPU from the given name */
395     CPU = FindCPU (Arg);
396     if (CPU != CPU_6502 && CPU != CPU_65SC02 &&
397         CPU != CPU_65C02 && CPU != CPU_65816) {
398         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
399     }
400 }
401
402
403
404 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
405 /* Handle the --data-name option */
406 {
407     /* Check for a valid name */
408     CheckSegName (Arg);
409
410     /* Set the name */
411     NewSegName (SEG_DATA, Arg);
412 }
413
414
415
416 static void OptDebug (const char* Opt attribute ((unused)),
417                       const char* Arg attribute ((unused)))
418 /* Compiler debug mode */
419 {
420     ++Debug;
421 }
422
423
424
425 static void OptDebugInfo (const char* Opt attribute ((unused)),
426                           const char* Arg attribute ((unused)))
427 /* Add debug info to the object file */
428 {
429     DebugInfo = 1;
430 }
431
432
433
434 static void OptDebugOpt (const char* Opt attribute ((unused)), const char* Arg)
435 /* Debug optimization steps */
436 {
437     char Buf [128];
438     char* Line;
439
440     /* Open the file */
441     FILE* F = fopen (Arg, "r");
442     if (F == 0) {
443         AbEnd ("Cannot open `%s': %s", Arg, strerror (errno));
444     }
445
446     /* Read line by line, ignore empty lines and switch optimization
447      * steps on/off.
448      */
449     while (fgets (Buf, sizeof (Buf), F) != 0) {
450
451         /* Remove trailing control chars. This will also remove the
452          * trailing newline.
453          */
454         unsigned Len = strlen (Buf);
455         while (Len > 0 && IsControl (Buf[Len-1])) {
456             --Len;
457         }
458         Buf[Len] = '\0';
459
460         /* Get a pointer to the buffer and remove leading white space */
461         Line = Buf;
462         while (IsBlank (*Line)) {
463             ++Line;
464         }
465
466         /* Check the first character and enable/disable the step or
467          * ignore the line
468          */
469         switch (*Line) {
470
471             case '\0':
472             case '#':
473             case ';':
474                 /* Empty or comment line */
475                 continue;
476
477             case '-':
478                 DisableOpt (Line+1);
479                 break;
480
481             case '+':
482                 ++Line;
483                 /* FALLTHROUGH */
484
485             default:
486                 EnableOpt (Line);
487                 break;
488
489         }
490
491     }
492
493     /* Close the file, no error check here since we were just reading and
494      * this is only a debug function.
495      */
496     (void) fclose (F);
497 }
498
499
500
501 static void OptDisableOpt (const char* Opt attribute ((unused)), const char* Arg)
502 /* Disable an optimization step */
503 {
504     DisableOpt (Arg);
505 }
506
507
508
509 static void OptEnableOpt (const char* Opt attribute ((unused)), const char* Arg)
510 /* Enable an optimization step */
511 {
512     EnableOpt (Arg);
513 }
514
515
516
517 static void OptForgetIncPaths (const char* Opt attribute ((unused)),
518                                const char* Arg attribute ((unused)))
519 /* Forget all currently defined include paths */
520 {
521     ForgetAllIncludePaths ();
522 }
523
524
525
526 static void OptHelp (const char* Opt attribute ((unused)),
527                      const char* Arg attribute ((unused)))
528 /* Print usage information and exit */
529 {
530     Usage ();
531     exit (EXIT_SUCCESS);
532 }
533
534
535
536 static void OptIncludeDir (const char* Opt attribute ((unused)), const char* Arg)
537 /* Add an include search path */
538 {
539     AddIncludePath (Arg, INC_SYS | INC_USER);
540 }
541
542
543
544 static void OptListOptSteps (const char* Opt attribute ((unused)),
545                              const char* Arg attribute ((unused)))
546 /* List all optimizer steps */
547 {
548     /* List the optimizer steps */
549     ListOptSteps (stdout);
550
551     /* Terminate */
552     exit (EXIT_SUCCESS);
553 }
554
555
556
557 static void OptMemoryModel (const char* Opt, const char* Arg)
558 /* Set the memory model */
559 {
560     mmodel_t M;
561
562     /* Check the current memory model */
563     if (MemoryModel != MMODEL_UNKNOWN) {
564         AbEnd ("Cannot use option `%s' twice", Opt);
565     }
566
567     /* Translate the memory model name and check it */
568     M = FindMemoryModel (Arg);
569     if (M == MMODEL_UNKNOWN) {
570         AbEnd ("Unknown memory model: %s", Arg);
571     } else if (M == MMODEL_HUGE) {
572         AbEnd ("Unsupported memory model: %s", Arg);
573     }
574
575     /* Set the memory model */
576     SetMemoryModel (M);
577 }
578
579
580
581 static void OptRegisterSpace (const char* Opt, const char* Arg)
582 /* Handle the --register-space option */
583 {
584     /* Numeric argument expected */
585     if (sscanf (Arg, "%u", &RegisterSpace) != 1 || RegisterSpace > 256) {
586         AbEnd ("Argument for option %s is invalid", Opt);
587     }
588 }
589
590
591
592 static void OptRegisterVars (const char* Opt attribute ((unused)),
593                              const char* Arg attribute ((unused)))
594 /* Handle the --register-vars option */
595 {
596     IS_Set (&EnableRegVars, 1);
597 }
598
599
600
601 static void OptRodataName (const char* Opt attribute ((unused)), const char* Arg)
602 /* Handle the --rodata-name option */
603 {
604     /* Check for a valid name */
605     CheckSegName (Arg);
606
607     /* Set the name */
608     NewSegName (SEG_RODATA, Arg);
609 }
610
611
612
613 static void OptSignedChars (const char* Opt attribute ((unused)),
614                             const char* Arg attribute ((unused)))
615 /* Make default characters signed */
616 {
617     IS_Set (&SignedChars, 1);
618 }
619
620
621
622 static void OptStaticLocals (const char* Opt attribute ((unused)),
623                              const char* Arg attribute ((unused)))
624 /* Place local variables in static storage */
625 {
626     IS_Set (&StaticLocals, 1);
627 }
628
629
630
631 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
632 /* Set the target system */
633 {
634     SetSys (Arg);
635 }
636
637
638
639 static void OptVerbose (const char* Opt attribute ((unused)),
640                         const char* Arg attribute ((unused)))
641 /* Increase verbosity */
642 {
643     ++Verbosity;
644 }
645
646
647
648 static void OptVersion (const char* Opt attribute ((unused)),
649                         const char* Arg attribute ((unused)))
650 /* Print the assembler version */
651 {
652     fprintf (stderr,
653              "cc65 V%u.%u.%u\n",
654              VER_MAJOR, VER_MINOR, VER_PATCH);
655 }
656
657
658
659 int main (int argc, char* argv[])
660 {
661     /* Program long options */
662     static const LongOpt OptTab[] = {
663         { "--add-source",       0,      OptAddSource            },
664         { "--ansi",             0,      OptAnsi                 },
665         { "--bss-name",         1,      OptBssName              },
666         { "--check-stack",      0,      OptCheckStack           },
667         { "--code-name",        1,      OptCodeName             },
668         { "--codesize",         1,      OptCodeSize             },
669         { "--cpu",              1,      OptCPU                  },
670         { "--create-dep",       0,      OptCreateDep            },
671         { "--data-name",        1,      OptDataName             },
672         { "--debug",            0,      OptDebug                },
673         { "--debug-info",       0,      OptDebugInfo            },
674         { "--debug-opt",        1,      OptDebugOpt             },
675         { "--disable-opt",      1,      OptDisableOpt           },
676         { "--enable-opt",       1,      OptEnableOpt            },
677         { "--forget-inc-paths", 0,      OptForgetIncPaths       },
678         { "--help",             0,      OptHelp                 },
679         { "--include-dir",      1,      OptIncludeDir           },
680         { "--list-opt-steps",   0,      OptListOptSteps         },
681         { "--memory-model",     1,      OptMemoryModel          },
682         { "--register-space",   1,      OptRegisterSpace        },
683         { "--register-vars",    0,      OptRegisterVars         },
684         { "--rodata-name",      1,      OptRodataName           },
685         { "--signed-chars",     0,      OptSignedChars          },
686         { "--static-locals",    0,      OptStaticLocals         },
687         { "--target",           1,      OptTarget               },
688         { "--verbose",          0,      OptVerbose              },
689         { "--version",          0,      OptVersion              },
690     };
691
692     unsigned I;
693
694     /* Initialize the output file name */
695     const char* OutputFile = 0;
696     const char* InputFile  = 0;
697
698     /* Initialize the cmdline module */
699     InitCmdLine (&argc, &argv, "cc65");
700
701     /* Initialize the default segment names */
702     InitSegNames ();
703
704     /* Initialize the include search paths */
705     InitIncludePaths ();
706
707     /* Parse the command line */
708     I = 1;
709     while (I < ArgCount) {
710
711         const char* P;
712
713         /* Get the argument */
714         const char* Arg = ArgVec[I];
715
716         /* Check for an option */
717         if (Arg [0] == '-') {
718
719             switch (Arg [1]) {
720
721                 case '-':
722                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
723                     break;
724
725                 case 'd':
726                     OptDebug (Arg, 0);
727                     break;
728
729                 case 'h':
730                 case '?':
731                     OptHelp (Arg, 0);
732                     break;
733
734                 case 'g':
735                     OptDebugInfo (Arg, 0);
736                     break;
737
738                 case 'j':
739                     OptSignedChars (Arg, 0);
740                     break;
741
742                 case 'o':
743                     OutputFile = GetArg (&I, 2);
744                     break;
745
746                 case 'r':
747                     OptRegisterVars (Arg, 0);
748                     break;
749
750                 case 't':
751                     OptTarget (Arg, GetArg (&I, 2));
752                     break;
753
754                 case 'u':
755                     OptCreateDep (Arg, 0);
756                     break;
757
758                 case 'v':
759                     OptVerbose (Arg, 0);
760                     break;
761
762                 case 'A':
763                     OptAnsi (Arg, 0);
764                     break;
765
766                 case 'C':
767                     P = Arg + 2;
768                     while (*P) {
769                         switch (*P++) {
770                             case 'l':
771                                 OptStaticLocals (Arg, 0);
772                                 break;
773                             default:
774                                 UnknownOption (Arg);
775                                 break;
776                         }
777                     }
778                     break;
779
780                 case 'D':
781                     DefineSym (GetArg (&I, 2));
782                     break;
783
784                 case 'I':
785                     OptIncludeDir (Arg, GetArg (&I, 2));
786                     break;
787
788                 case 'O':
789                     Optimize = 1;
790                     P = Arg + 2;
791                     while (*P) {
792                         switch (*P++) {
793                             case 'f':
794                                 sscanf (P, "%lx", (long*) &OptDisable);
795                                 break;
796                             case 'i':
797                                 FavourSize = 0;
798                                 CodeSizeFactor = 200;
799                                 break;
800                             case 'r':
801                                 IS_Set (&EnableRegVars, 1);
802                                 break;
803                             case 's':
804                                 IS_Set (&InlineStdFuncs, 1);
805                                 break;
806                         }
807                     }
808                     break;
809
810                 case 'T':
811                     OptAddSource (Arg, 0);
812                     break;
813
814                 case 'V':
815                     OptVersion (Arg, 0);
816                     break;
817
818                 case 'W':
819                     NoWarn = 1;
820                     break;
821
822                 default:
823                     UnknownOption (Arg);
824                     break;
825             }
826         } else {
827             if (InputFile) {
828                 fprintf (stderr, "additional file specs ignored\n");
829             } else {
830                 InputFile = Arg;
831             }
832         }
833
834         /* Next argument */
835         ++I;
836     }
837
838     /* Did we have a file spec on the command line? */
839     if (InputFile == 0) {
840         AbEnd ("No input files");
841     }
842
843     /* Create the output file name if it was not explicitly given */
844     if (OutputFile == 0) {
845         OutputFile = MakeFilename (InputFile, ".s");
846     }
847
848     /* If no CPU given, use the default CPU for the target */
849     if (CPU == CPU_UNKNOWN) {
850         if (Target != TGT_UNKNOWN) {
851             CPU = DefaultCPU[Target];
852         } else {
853             CPU = CPU_6502;
854         }
855     }
856
857     /* If no memory model was given, use the default */
858     if (MemoryModel == MMODEL_UNKNOWN) {
859         SetMemoryModel (MMODEL_NEAR);
860     }
861
862     /* Go! */
863     Compile (InputFile);
864
865     /* Create the output file if we didn't had any errors */
866     if (ErrorCount == 0 || Debug) {
867
868         /* Open the file */
869         FILE* F = fopen (OutputFile, "w");
870         if (F == 0) {
871             Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
872         }
873
874         /* Write the output to the file */
875         WriteOutput (F);
876
877         /* Close the file, check for errors */
878         if (fclose (F) != 0) {
879             remove (OutputFile);
880             Fatal ("Cannot write to output file (disk full?)");
881         }
882
883         /* Create dependencies if requested */
884         if (CreateDep) {
885             DoCreateDep (OutputFile);
886         }
887
888     }
889
890     /* Return an apropriate exit code */
891     return (ErrorCount > 0)? EXIT_FAILURE : EXIT_SUCCESS;
892 }
893
894
895