summary refs log tree commit diff stats
path: root/nim/commands.pas
blob: d87a8f0842df97cd42b5ef3ae25cc26e66235393 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//
//
//           The Nimrod Compiler
//        (c) Copyright 2008 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//

unit commands;

// This module handles the parsing of command line arguments.

interface

{$include 'config.inc'}

uses
  nsystem, charsets, nos, msgs, options, nversion, condsyms, strutils, extccomp, 
  platform, lists, wordrecg;

procedure writeCommandLineUsage;

type
  TCmdLinePass = (
    passCmd1,                // first pass over the command line
    passCmd2,                // second pass over the command line
    passPP                   // preprocessor called ProcessCommand()
  );

procedure ProcessCommand(const switch: string; pass: TCmdLinePass);
procedure processSwitch(const switch, arg: string; pass: TCmdlinePass;
                        const info: TLineInfo);

implementation

{@ignore}
const
{$ifdef fpc}
  compileDate = {$I %date%};
{$else}
  compileDate = '2009-0-0';
{$endif}
{@emit}

const
  HelpMessage = 'Nimrod Compiler Version $1 (' +{&}
    compileDate +{&} ') [$2: $3]' +{&} nl +{&}
    'Copyright (c) 2004-2009 by Andreas Rumpf' +{&} nl;

const
  Usage = ''
//[[[cog
//from string import replace
//def f(x): return "+{&} '" + replace(x, "'", "''")[:-1] + "' +{&} nl"
//for line in open("data/basicopt.txt").readlines():
//  cog.outl(f(line))
//]]]
+{&} 'Usage::' +{&} nl
+{&} '  nimrod command [options] inputfile [arguments]' +{&} nl
+{&} 'Command::' +{&} nl
+{&} '  compile, c                compile project with default code generator (C)' +{&} nl
+{&} '  compile_to_c, cc          compile project with C code generator' +{&} nl
+{&} '  doc                       generate the documentation for inputfile' +{&} nl
+{&} '  rst2html                  converts a reStructuredText file to HTML' +{&} nl
+{&} 'Arguments:' +{&} nl
+{&} '  arguments are passed to the program being run (if --run option is selected)' +{&} nl
+{&} 'Options:' +{&} nl
+{&} '  -p, --path:PATH           add path to search paths' +{&} nl
+{&} '  -o, --out:FILE            set the output filename' +{&} nl
+{&} '  -d, --define:SYMBOL       define a conditional symbol' +{&} nl
+{&} '  -u, --undef:SYMBOL        undefine a conditional symbol' +{&} nl
+{&} '  -f, --force_build         force rebuilding of all modules' +{&} nl
+{&} '  --symbol_files:on|off     use symbol files to speed up compilation (buggy!)' +{&} nl
+{&} '  --stack_trace:on|off      code generation for stack trace ON|OFF' +{&} nl
+{&} '  --line_trace:on|off       code generation for line trace ON|OFF' +{&} nl
+{&} '  --debugger:on|off         turn Embedded Nimrod Debugger ON|OFF' +{&} nl
+{&} '  -x, --checks:on|off       code generation for all runtime checks ON|OFF' +{&} nl
+{&} '  --obj_checks:on|off       code generation for obj conversion checks ON|OFF' +{&} nl
+{&} '  --field_checks:on|off     code generation for case record fields ON|OFF' +{&} nl
+{&} '  --range_checks:on|off     code generation for range checks ON|OFF' +{&} nl
+{&} '  --bound_checks:on|off     code generation for bound checks ON|OFF' +{&} nl
+{&} '  --overflow_checks:on|off  code generation for over-/underflow checks ON|OFF' +{&} nl
+{&} '  -a, --assertions:on|off   code generation for assertions ON|OFF' +{&} nl
+{&} '  --dead_code_elim:on|off   whole program dead code elimination ON|OFF' +{&} nl
+{&} '  --opt:none|speed|size     optimize not at all or for speed|size' +{&} nl
+{&} '  --app:console|gui|lib     generate a console|GUI application or a shared lib' +{&} nl
+{&} '  -r, --run                 run the compiled program with given arguments' +{&} nl
+{&} '  --advanced                show advanced command line switches' +{&} nl
+{&} '  -h, --help                show this help' +{&} nl
//[[[end]]]
  ;

  AdvancedUsage = ''
//[[[cog
//for line in open("data/advopt.txt").readlines():
//  cog.outl(f(line))
//]]]
+{&} 'Advanced commands::' +{&} nl
+{&} '  pas                       convert a Pascal file to Nimrod syntax' +{&} nl
+{&} '  pretty                    pretty print the inputfile' +{&} nl
+{&} '  gen_depend                generate a DOT file containing the' +{&} nl
+{&} '                            module dependency graph' +{&} nl
+{&} '  list_def                  list all defined conditionals and exit' +{&} nl
+{&} '  check                     checks the project for syntax and semantic' +{&} nl
+{&} '  parse                     parses a single file (for debugging Nimrod)' +{&} nl
+{&} 'Advanced options:' +{&} nl
+{&} '  -w, --warnings:on|off     warnings ON|OFF' +{&} nl
+{&} '  --warning[X]:on|off       specific warning X ON|OFF' +{&} nl
+{&} '  --hints:on|off            hints ON|OFF' +{&} nl
+{&} '  --hint[X]:on|off          specific hint X ON|OFF' +{&} nl
+{&} '  --lib:PATH                set the system library path' +{&} nl
+{&} '  -c, --compile_only        compile only; do not assemble or link' +{&} nl
+{&} '  --no_linking              compile but do not link' +{&} nl
+{&} '  --gen_script              generate a compile script (in the ''nimcache''' +{&} nl
+{&} '                            subdirectory named ''compile_$project$scriptext'')' +{&} nl
+{&} '  --os:SYMBOL               set the target operating system (cross-compilation)' +{&} nl
+{&} '  --cpu:SYMBOL              set the target processor (cross-compilation)' +{&} nl
+{&} '  --debuginfo               enables debug information' +{&} nl
+{&} '  -t, --passc:OPTION        pass an option to the C compiler' +{&} nl
+{&} '  -l, --passl:OPTION        pass an option to the linker' +{&} nl
+{&} '  --gen_mapping             generate a mapping file containing' +{&} nl
+{&} '                            (Nimrod, mangled) identifier pairs' +{&} nl
+{&} '  --line_dir:on|off         generation of #line directive ON|OFF' +{&} nl
+{&} '  --checkpoints:on|off      turn on|off checkpoints; for debugging Nimrod' +{&} nl
+{&} '  --skip_cfg                do not read the general configuration file' +{&} nl
+{&} '  --skip_proj_cfg           do not read the project''s configuration file' +{&} nl
+{&} '  --import:MODULE_FILE      import the given module implicitly for each module' +{&} nl
+{&} '  --index:FILE              use FILE to generate a documenation index file' +{&} nl
+{&} '  --putenv:key=value        set an environment variable' +{&} nl
+{&} '  --list_cmd                list the commands used to execute external programs' +{&} nl
+{&} '  --verbosity:0|1|2|3       set Nimrod''s verbosity level (0 is default)' +{&} nl
+{&} '  -v, --version             show detailed version information' +{&} nl
//[[[end]]]
  ;

function getCommandLineDesc: string;
begin
  result := format(HelpMessage, [VersionAsString, 
    platform.os[platform.hostOS].name, cpu[platform.hostCPU].name]) +{&} Usage
end;

var
  helpWritten: boolean;  // BUGFIX 19
  versionWritten: boolean;
  advHelpWritten: boolean;

procedure HelpOnError(pass: TCmdLinePass);
begin
  if (pass = passCmd1) and not helpWritten then begin
    // BUGFIX 19
    MessageOut(getCommandLineDesc());
    helpWritten := true
  end
end;

procedure writeAdvancedUsage(pass: TCmdLinePass);
begin
  if (pass = passCmd1) and not advHelpWritten then begin
    // BUGFIX 19
    MessageOut(format(HelpMessage, [VersionAsString, 
                                    platform.os[platform.hostOS].name,
                                    cpu[platform.hostCPU].name]) +{&}
                                    AdvancedUsage);
    advHelpWritten := true;
    helpWritten := true;
    halt(0);
  end
end;

procedure writeVersionInfo(pass: TCmdLinePass);
begin
  if (pass = passCmd1) and not versionWritten then begin
    versionWritten := true;
    helpWritten := true;
    messageOut(format(HelpMessage, [VersionAsString, 
                                    platform.os[platform.hostOS].name,
                                    cpu[platform.hostCPU].name]))
  end
end;

procedure writeCommandLineUsage;
begin
  if not helpWritten then begin
    messageOut(getCommandLineDesc());
    helpWritten := true
  end
end;

procedure InvalidCmdLineOption(pass: TCmdLinePass; const switch: string;
                               const info: TLineInfo);
begin
  liMessage(info, errInvalidCmdLineOption, switch)
end;

procedure splitSwitch(const switch: string; out cmd, arg: string;
                      pass: TCmdLinePass; const info: TLineInfo);
var
  i: int;
begin
  cmd := '';
  i := strStart;
  if (i < length(switch)+strStart) and (switch[i] = '-') then inc(i);
  if (i < length(switch)+strStart) and (switch[i] = '-') then inc(i);
  while i < length(switch) + strStart do begin
    case switch[i] of
      'a'..'z', 'A'..'Z', '0'..'9', '_', '.':
        addChar(cmd, switch[i]);
      else break;
    end;
    inc(i);
  end;
  if i >= length(switch) + strStart then
    arg := ''
  else if switch[i] in [':', '=', '['] then
    arg := ncopy(switch, i + 1)
  else
    InvalidCmdLineOption(pass, switch, info)
end;

procedure ProcessOnOffSwitch(const op: TOptions; const arg: string;
                             pass: TCmdlinePass; const info: TLineInfo);
begin
  case whichKeyword(arg) of
    wOn:  gOptions := gOptions + op;
    wOff: gOptions := gOptions - op;
    else  liMessage(info, errOnOrOffExpectedButXFound, arg)
  end
end;

procedure ProcessOnOffSwitchG(const op: TGlobalOptions; const arg: string;
                              pass: TCmdlinePass; const info: TLineInfo);
begin
  case whichKeyword(arg) of
    wOn:  gGlobalOptions := gGlobalOptions + op;
    wOff: gGlobalOptions := gGlobalOptions - op;
    else  liMessage(info, errOnOrOffExpectedButXFound, arg)
  end
end;

procedure ExpectArg(const switch, arg: string; pass: TCmdLinePass;
                    const info: TLineInfo);
begin
  if (arg = '') then
    liMessage(info, errCmdLineArgExpected, switch)
end;

procedure ProcessSpecificNote(const arg: string; state: TSpecialWord;
                              pass: TCmdlinePass; const info: TLineInfo);
var
  i, x: int;
  n: TNoteKind;
  id: string;
begin
  id := '';
  // arg = "X]:on|off"
  i := strStart;
  n := hintMin;
  while (i < length(arg)+strStart) and (arg[i] <> ']') do begin
    addChar(id, arg[i]);
    inc(i)
  end;
  if (i < length(arg)+strStart) and (arg[i] = ']') then
    inc(i)
  else
    InvalidCmdLineOption(pass, arg, info);
  if (i < length(arg)+strStart) and (arg[i] in [':', '=']) then
    inc(i)
  else
    InvalidCmdLineOption(pass, arg, info);
  if state = wHint then begin
    x := findStr(msgs.HintsToStr, id);
    if x >= 0 then
      n := TNoteKind(x + ord(hintMin))
    else
      InvalidCmdLineOption(pass, arg, info)
  end
  else begin
    x := findStr(msgs.WarningsToStr, id);
    if x >= 0 then
      n := TNoteKind(x + ord(warnMin))
    else
      InvalidCmdLineOption(pass, arg, info)
  end;
  case whichKeyword(ncopy(arg, i)) of
    wOn: include(gNotes, n);
    wOff: exclude(gNotes, n);
    else liMessage(info, errOnOrOffExpectedButXFound, arg)
  end
end;

function processPath(const path: string): string;
begin
  result := UnixToNativePath(format(path,
    ['nimrod', getPrefixDir(), 'lib', libpath]))
end;

procedure processCompile(const filename: string);
var
  found, trunc, ext: string;
begin
  found := findFile(filename);
  if found = '' then found := filename;
  splitFilename(found, trunc, ext);
  extccomp.addExternalFileToCompile(trunc);
  extccomp.addFileToLink(completeCFilePath(trunc, false));
end;

procedure processSwitch(const switch, arg: string; pass: TCmdlinePass;
                        const info: TLineInfo);
var
  theOS: TSystemOS;
  cpu: TSystemCPU;
  key, val, path: string;
begin
  case whichKeyword(switch) of
    wPath, wP: begin
      expectArg(switch, arg, pass, info);
      path := processPath(arg);
      {@discard} lists.IncludeStr(options.searchPaths, path)
    end;
    wOut, wO: begin
      expectArg(switch, arg, pass, info);
      options.outFile := arg;
    end;
    wDefine, wD: begin
      expectArg(switch, arg, pass, info);
      DefineSymbol(arg)
    end;
    wUndef, wU: begin
      expectArg(switch, arg, pass, info);
      UndefSymbol(arg)
    end;
    wCompile: begin
      expectArg(switch, arg, pass, info);
      if pass in {@set}[passCmd2, passPP] then
        processCompile(arg);
    end;
    wLink: begin
      expectArg(switch, arg, pass, info);
      if pass in {@set}[passCmd2, passPP] then
        addFileToLink(arg);
    end;
    wDebuginfo: include(gGlobalOptions, optCDebug);
    wCompileOnly, wC: include(gGlobalOptions, optCompileOnly);
    wNoLinking: include(gGlobalOptions, optNoLinking);
    wForceBuild, wF: include(gGlobalOptions, optForceFullMake);
    wGC: begin
      case whichKeyword(arg) of
        wBoehm: begin
          include(gGlobalOptions, optBoehmGC);
          exclude(gGlobalOptions, optRefcGC);
          DefineSymbol('boehmgc');
        end;
        wRefc: begin
          exclude(gGlobalOptions, optBoehmGC);
          include(gGlobalOptions, optRefcGC)
        end;
        wNone: begin
          exclude(gGlobalOptions, optRefcGC);
          exclude(gGlobalOptions, optBoehmGC);
          defineSymbol('nogc');
        end
        else
          liMessage(info, errNoneBoehmRefcExpectedButXFound, arg)
      end
    end;
    wWarnings, wW: ProcessOnOffSwitch({@set}[optWarns], arg, pass, info);
    wWarning: ProcessSpecificNote(arg, wWarning, pass, info);
    wHint: ProcessSpecificNote(arg, wHint, pass, info);
    wHints: ProcessOnOffSwitch({@set}[optHints], arg, pass, info);
    wCheckpoints: ProcessOnOffSwitch({@set}[optCheckpoints], arg, pass, info);
    wStackTrace: ProcessOnOffSwitch({@set}[optStackTrace], arg, pass, info);
    wLineTrace: ProcessOnOffSwitch({@set}[optLineTrace], arg, pass, info);
    wDebugger: begin
      ProcessOnOffSwitch({@set}[optEndb], arg, pass, info);
      if optEndb in gOptions then
        DefineSymbol('endb')
      else
        UndefSymbol('endb')
    end;
    wProfiler: begin
      ProcessOnOffSwitch({@set}[optProfiler], arg, pass, info);
      if optProfiler in gOptions then DefineSymbol('profiler')
      else UndefSymbol('profiler')
    end;
    wChecks, wX: ProcessOnOffSwitch(checksOptions, arg, pass, info);
    wObjChecks: ProcessOnOffSwitch({@set}[optObjCheck], arg, pass, info);
    wFieldChecks: ProcessOnOffSwitch({@set}[optFieldCheck], arg, pass, info);
    wRangeChecks: ProcessOnOffSwitch({@set}[optRangeCheck], arg, pass, info);
    wBoundChecks: ProcessOnOffSwitch({@set}[optBoundsCheck], arg, pass, info);
    wOverflowChecks: ProcessOnOffSwitch({@set}[optOverflowCheck], arg, pass, info);
    wLineDir: ProcessOnOffSwitch({@set}[optLineDir], arg, pass, info);
    wAssertions, wA: ProcessOnOffSwitch({@set}[optAssert], arg, pass, info);
    wDeadCodeElim: ProcessOnOffSwitchG({@set}[optDeadCodeElim], arg, pass, info);
    wOpt: begin
      case whichKeyword(arg) of
        wSpeed: begin
          include(gOptions, optOptimizeSpeed);
          exclude(gOptions, optOptimizeSize)
        end;
        wSize: begin
          exclude(gOptions, optOptimizeSpeed);
          include(gOptions, optOptimizeSize)
        end;
        wNone: begin
          exclude(gOptions, optOptimizeSpeed);
          exclude(gOptions, optOptimizeSize)
        end
        else
          liMessage(info, errNoneSpeedOrSizeExpectedButXFound, arg)
      end
    end;
    wApp: begin
      case whichKeyword(arg) of
        wGui: begin
          include(gGlobalOptions, optGenGuiApp);
          defineSymbol('guiapp')
        end;
        wConsole:
          exclude(gGlobalOptions, optGenGuiApp);
        wLib: begin
          include(gGlobalOptions, optGenDynLib);
          exclude(gGlobalOptions, optGenGuiApp);
          defineSymbol('library')
        end;
        else
          liMessage(info, errGuiConsoleOrLibExpectedButXFound, arg)
      end
    end;
    wListDef: begin
      if pass in {@set}[passCmd2, passPP] then
        condsyms.listSymbols();
    end;
    wPassC, wT: begin
      expectArg(switch, arg, pass, info);
      if pass in {@set}[passCmd2, passPP] then
        extccomp.addCompileOption(arg)
    end;
    wPassL, wL: begin
      expectArg(switch, arg, pass, info);
      if pass in {@set}[passCmd2, passPP] then
        extccomp.addLinkOption(arg)
    end;
    wIndex: begin
      expectArg(switch, arg, pass, info);
      if pass in {@set}[passCmd2, passPP] then
        gIndexFile := arg
    end;
    wImport: begin
      expectArg(switch, arg, pass, info);
      options.addImplicitMod(arg);
    end;
    wListCmd: include(gGlobalOptions, optListCmd);
    wGenMapping: include(gGlobalOptions, optGenMapping);
    wOS: begin
      if (pass = passCmd1) then begin
        theOS := platform.NameToOS(arg);
        if theOS = osNone then
          liMessage(info, errUnknownOS, arg);
        if theOS <> platform.hostOS then begin
          setTarget(theOS, targetCPU);
          include(gGlobalOptions, optCompileOnly);
          condsyms.InitDefines()
        end
      end
    end;
    wCPU: begin
      if (pass = passCmd1) then begin
        cpu := platform.NameToCPU(arg);
        if cpu = cpuNone then
          liMessage(info, errUnknownCPU, arg);
        if cpu <> platform.hostCPU then begin
          setTarget(targetOS, cpu);
          include(gGlobalOptions, optCompileOnly);
          condsyms.InitDefines()
        end
      end
    end;
    wRun, wR: include(gGlobalOptions, optRun);
    wVerbosity: begin
      expectArg(switch, arg, pass, info);
      gVerbosity := parseInt(arg);
    end;
    wVersion, wV: writeVersionInfo(pass);
    wAdvanced: writeAdvancedUsage(pass);
    wHelp, wH: helpOnError(pass);
    wSymbolFiles: ProcessOnOffSwitchG({@set}[optSymbolFiles], arg, pass, info);
    wSkipCfg: include(gGlobalOptions, optSkipConfigFile);
    wSkipProjCfg: include(gGlobalOptions, optSkipProjConfigFile);
    wGenScript: include(gGlobalOptions, optGenScript);
    wLib: begin
      expectArg(switch, arg, pass, info);
      libpath := processPath(arg)
    end;
    wPutEnv: begin
      expectArg(switch, arg, pass, info);
      splitSwitch(arg, key, val, pass, info);
      nos.putEnv(key, val);
    end;
    wCC: begin
      expectArg(switch, arg, pass, info);
      setCC(arg)
    end;
    wMaxErr: begin
      expectArg(switch, arg, pass, info);
      gErrorMax := parseInt(arg);
    end;
    else if findSubStr('.', switch) >= strStart then
      options.setConfigVar(switch, arg)
    else
      InvalidCmdLineOption(pass, switch, info)
  end
end;

procedure ProcessCommand(const switch: string; pass: TCmdLinePass);
var
  cmd, arg: string;
  info: TLineInfo;
begin
  info := newLineInfo('command line', 1, 1);
  splitSwitch(switch, cmd, arg, pass, info);
  ProcessSwitch(cmd, arg, pass, info)
end;

end.