summary refs log tree commit diff stats
path: root/nim/options.pas
blob: 5bbfbbbee9d9bd03cf1e4a27eb99b8d87dc3fa15 (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
//
//
//           The Nimrod Compiler
//        (c) Copyright 2008 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit options;

interface

{$include 'config.inc'}

uses
  nsystem, nos, lists, strutils, strtabs;

type
  // please make sure we have under 32 options
  // (improves code efficiency a lot!)
  TOption = (  // **keep binary compatible**
    optNone,
    optObjCheck,
    optFieldCheck, optRangeCheck,
    optBoundsCheck, optOverflowCheck, optNilCheck, optAssert, optLineDir,
    optWarns, optHints,
    optOptimizeSpeed,
    optOptimizeSize,
    optStackTrace,     // stack tracing support
    optLineTrace,      // line tracing support (includes stack tracing)
    optEndb,           // embedded debugger
    optByRef,          // use pass by ref for records (for interfacing with C)
    optCheckpoints,    // check for checkpoints (used for debugging)
    optProfiler        // profiler turned on
  );
  TOptions = set of TOption;

  TGlobalOption = (gloptNone, optForceFullMake, optBoehmGC,
    optRefcGC, optDeadCodeElim, optListCmd, optCompileOnly, optNoLinking,
    optSafeCode,       // only allow safe code
    optCDebug,         // turn on debugging information
    optGenDynLib,      // generate a dynamic library
    optGenGuiApp,      // generate a GUI application
    optGenScript,      // generate a script file to compile the *.c files
    optGenMapping,     // generate a mapping file
    optRun,            // run the compiled project
    optSymbolFiles,    // use symbol files for speeding up compilation
    optSkipConfigFile, // skip the general config file
    optSkipProjConfigFile // skip the project's config file
  );
  TGlobalOptions = set of TGlobalOption;

  TCommands = ( // Nimrod's commands
    cmdNone,
    cmdCompileToC,
    cmdCompileToCpp,
    cmdCompileToEcmaScript,
    cmdInterpret,
    cmdPretty,
    cmdDoc,
    cmdPas,
    cmdBoot,
    cmdGenDepend,
    cmdListDef,
    cmdCheck,      // semantic checking for whole project
    cmdParse,      // parse a single file (for debugging)
    cmdScan,       // scan a single file (for debugging)
    cmdDebugTrans, // debug a transformation pass
    cmdRst2html,   // convert a reStructuredText file to HTML
    cmdInteractive // start interactive session
  );
  TStringSeq = array of string;

const
  ChecksOptions = {@set}[optObjCheck, optFieldCheck, optRangeCheck,
                         optNilCheck, optOverflowCheck, optBoundsCheck,
                         optAssert];
  optionToStr: array [TOption] of string = (
    'optNone', 'optObjCheck', 'optFieldCheck', 'optRangeCheck',
    'optBoundsCheck', 'optOverflowCheck', 'optNilCheck', 'optAssert',
    'optLineDir', 'optWarns', 'optHints', 'optOptimizeSpeed',
    'optOptimizeSize', 'optStackTrace', 'optLineTrace', 'optEmdb',
    'optByRef', 'optCheckpoints', 'optProfiler'
  );
var
  gOptions: TOptions = {@set}[optObjCheck, optFieldCheck, optRangeCheck,
                              optBoundsCheck, optOverflowCheck,
                              optAssert, optWarns, optHints,
                              optStackTrace, optLineTrace];

  gGlobalOptions: TGlobalOptions = {@set}[optRefcGC];

  gExitcode: Byte;
  searchPaths: TLinkedList;
  outFile: string = '';
  gIndexFile: string = '';

  gCmd: TCommands = cmdNone; // the command

  gVerbosity: int; // how verbose the compiler is

function FindFile(const f: string): string;

const
  genSubDir = 'nimcache';
  NimExt = 'nim';
  RodExt = 'rod';
  HtmlExt = 'html';
  IniExt = 'ini';
  TmplExt = 'tmpl';
  DocConfig = 'nimdoc.cfg';

function completeGeneratedFilePath(const f: string;
                                   createSubDir: bool = true): string;

function toGeneratedFile(const path, ext: string): string;
// converts "/home/a/mymodule.nim", "rod" to "/home/a/nimcache/mymodule.rod"

function getPrefixDir: string;
// gets the application directory

function getFileTrunk(const filename: string): string;

// additional configuration variables:
var
  gConfigVars: PStringTable;
  libpath: string = '';
  projectPath: string = '';
  gKeepComments: boolean = true; // whether the parser needs to keep comments
  gImplicitMods: TStringSeq = {@ignore} nil {@emit @[]};
    // modules that are to be implicitly imported

function existsConfigVar(const key: string): bool;
function getConfigVar(const key: string): string;
procedure setConfigVar(const key, val: string);

procedure addImplicitMod(const filename: string);

function getOutFile(const filename, ext: string): string;

function binaryStrSearch(const x: array of string; const y: string): int;

implementation

function existsConfigVar(const key: string): bool;
begin
  result := hasKey(gConfigVars, key)
end;

function getConfigVar(const key: string): string;
begin
  result := strtabs.get(gConfigVars, key);
end;

procedure setConfigVar(const key, val: string);
begin
  strtabs.put(gConfigVars, key, val);
end;

function getOutFile(const filename, ext: string): string;
begin
  if options.outFile <> '' then result := options.outFile
  else result := changeFileExt(filename, ext)
end;

procedure addImplicitMod(const filename: string);
var
  len: int;
begin
  len := length(gImplicitMods);
  setLength(gImplicitMods, len+1);
  gImplicitMods[len] := filename;
end;

function getPrefixDir: string;
var
  appdir, bin: string;
begin
  appdir := getApplicationDir();
  SplitPath(appdir, result, bin);
end;

function getFileTrunk(const filename: string): string;
var
  f, e, dir: string;
begin
  splitPath(filename, dir, f);
  splitFilename(f, result, e);
end;

function shortenDir(const dir: string): string;
var 
  prefix: string;
begin
  // returns the interesting part of a dir
  prefix := getPrefixDir() +{&} dirSep;
  if startsWith(dir, prefix) then begin
    result := ncopy(dir, length(prefix) + strStart); exit
  end;
  prefix := getCurrentDir() +{&} dirSep;
  if startsWith(dir, prefix) then begin
    result := ncopy(dir, length(prefix) + strStart); exit
  end;
  prefix := projectPath +{&} dirSep;
  //writeln(output, prefix);
  //writeln(output, dir);
  if startsWith(dir, prefix) then begin
    result := ncopy(dir, length(prefix) + strStart); exit
  end;
  result := dir
end;

function toGeneratedFile(const path, ext: string): string;
var
  head, tail: string;
begin
  splitPath(path, head, tail);
  result := joinPath([projectPath, genSubDir, shortenDir(head +{&} dirSep), 
                      changeFileExt(tail, ext)])
end;

function completeGeneratedFilePath(const f: string;
                                   createSubDir: bool = true): string;
var
  head, tail, subdir: string;
begin
  splitPath(f, head, tail);
  subdir := joinPath([projectPath, genSubDir, shortenDir(head +{&} dirSep)]);
  if createSubDir then begin
    //Writeln(output, subdir);
    createDir(subdir);
  end;
  result := joinPath(subdir, tail)
end;

function rawFindFile(const f: string): string;
var
  it: PStrEntry;
begin
  if ExistsFile(f) then result := f
  else begin
    it := PStrEntry(SearchPaths.head);
    while it <> nil do begin
      result := JoinPath(it.data, f);
      if ExistsFile(result) then exit;
      it := PStrEntry(it.Next)
    end;
    result := ''
  end
end;

function FindFile(const f: string): string;
begin
  result := rawFindFile(f);
  if length(result) = 0 then
    result := rawFindFile(toLower(f));
end;

function binaryStrSearch(const x: array of string; const y: string): int;
var
  a, b, mid, c: int;
begin
  a := 0;
  b := length(x)-1;
  while a <= b do begin
    mid := (a + b) div 2;
    c := cmpIgnoreCase(x[mid], y);
    if c < 0 then
      a := mid + 1
    else if c > 0 then
      b := mid - 1
    else begin
      result := mid;
      exit
    end
  end;
  result := -1
end;

initialization
  gConfigVars := newStringTable([], modeStyleInsensitive);
end.