Ticket #35: compDirOpts2.patch
| File compDirOpts2.patch, 45.8 kB (added by hopscc, 3 months ago) |
|---|
-
Source/Compiler.cobra
 56 56  57 57 var _globalNS as NameSpace 58 58 var _modules as List<of Module>  59 var _parsedModules as List<of Module>? # accumulated as files parsed 59 60 var _firstFileName as String? 60 61  61 62 var _curModule as Module? # set during bindInt and bindImp … …  232 233 Related resources such as CobraLang.cs reside there. 233 234 """ 234 235 return Path.getDirectoryName(Assembly.getEntryAssembly.location) to ! # actually could be nil: if you stuck Cobra in the root dir, the .getDirectoryName docs say it will return nil 235    236  236 237 def compileFilesNamed(paths as IList<of String>) 237 238 .compileFilesNamed(paths) 238 239  … …  293 294 .options = options 294 295 bar = '----------------------------------------------------------------------------------------------------' 295 296 .parseFilesNamed(fileNames)  297  296 298 assert .modules.count 297 299 if verbose 298 300 .dumpModulesForTestify(resultsWriter, 'Modules after parsing') … …  326 328 .printMessages 327 329 print bar 328 330   331 def _addRuntimeRef(opts as Options)  332 if not opts.containsKey('reference')  333 opts['reference'] = List<of String>()  334 refs = opts['reference'] to List<of String>  335 libName = 'Cobra.Lang.dll'  336 if libName not in refs  337 if .verbosity, print 'Adding reference to [libName]'  338 refs.add(libName)  339  329 340 def parseFilesNamed(filenames as IList<of String>) as List<of Module> 330 341 """ 331 342 Returns the modules for the newly parsed files. … …  341 352 embedRunTime = .options.boolValue('embed-run-time') 342 353  343 354 if not embedRunTime 344  # then reference it 345  if not .options.containsKey('reference') 346  .options['reference'] = List<of String>() 347  refs = .options['reference'] to List<of String> 348  libName = 'Cobra.Lang.dll' 349  if libName not in refs 350  if .verbosity, print 'Adding reference to [libName]' 351  refs.add(libName)  355 _addRuntimeRef(.options) # then reference runtime dll 352 356  353 357 Node.setCompiler(this) 354 358 try 355 359 if _modules.count == 0 # attempt at caching modules during testify. incomplete. 356 360 .writeSharpInfoClass 357 361 .readSystemTypes 358  .readAssemblyTypes  362 .readAssemblyTypes(.options) 359 363  360 364 if true # _modules.count == 0 361 365 path = Path.combine(.cobraExeDir, 'CobraInfo.cs') … …  376 380 filenames.insert(3, path) 377 381  378 382 modules = List<of Module>()  383 _parsedModules = modules 379 384 for filename in filenames 380 385 if filename.endsWith('.cs') 381 386 if _verbosity … …  404 409 else 405 410 assert modules.count 406 411 _modules.addRange(modules)  412 _parsedModules = nil 407 413 return modules 408 414   415 def augmentOptions(opts as Options)  416 """  417 Update Options and accumulator lists generated from it with additional options settings.  418 Used from inside parseFiles for args compilerDirective handling  419 """  420 if .verbosity  421 print 'preAugment Options Dictionary'  422 .options.print  423   424 # special cases   425 _fixupLibRefs(opts)  426 _fixupEmbedRunTime(opts) # need this after fixup libs  427   428 .options.combineNew(opts)  429 v = .options.getDefault('verbosity', 0) to int  430 if v > _verbosity, _verbosity = v  431 if .verbosity  432 print 'postAugment Options Dictionary'  433 .options.print  434   435   436 def _fixupEmbedRunTime(opts as Options)  437 #print 'before', _parsedModules  438 if opts.boolValue('embed-run-time') <> .options.boolValue('embed-run-time')  439 if not opts.boolValue('embed-run-time') # changed True to false  440 # remove parsed rtSrc modules  441 rl = for m in _parsedModules where m.isCobraLibrary and not m.fileName.endsWith('.dll')  442 for m in rl  443 _parsedModules.remove(m)  444 _addRuntimeRef(opts) # add ref to runtime dll  445 else  446 opts['embed-run-time'] = .options['embed-run-time']  447 #errchannel.throwError('Cannot switch -ert:no to -ert:yes in compilerDirective')  448 # To support this need to determine where/what recorded for Cobra.dll   449 # ref,clear it out and insert rtl src to be parsed AFTER finish current file  450 #print 'after fixup', _parsedModules  451   452 def _fixupLibRefs(opts as Options)  453 haveRefs = false  454 for key in ['library-directory', 'pkg', 'reference']  455 if opts.containsKey(key)  456 if key == 'reference'  457 references = opts.getDefaultLOStr('reference')  458 _fixupLibSuffix(references)  459 if not .options.containsKey(key)  460 .options[key] = List<of String>()  461 existList = .options[key] to List<of String>  462 augList = opts[key] to List<of String>  463 dupCount = 0  464 for item in augList  465 if item not in existList  466 if .verbosity, print 'Adding [key] "[item]"'  467 existList.add(item)  468 else  469 if .verbosity, print '[key] "[item]" already in options.[key]'  470 dupCount += 1 # augList.remove(item)   471 if not haveRefs  472 haveRefs = key <> 'library-directory' and augList.count - dupCount >0  473 .options.didSpecify(key) # so not overwrite when combine  474   475 if haveRefs  476 .readAssemblyTypes(opts)  477 if .verbosity > 1  478 references = .options.getDefaultLOStr('reference')  479 _printRefs(references)  480   481  409 482 def runProcess as Process 410 483 """ 411 484 Returns a new Process with startInfo.fileName and p.startInfo.arguments set appropriately … …  598 671 t = System.Diagnostics.Process.getType 599 672 .readAssembly(t.assembly) # System.dll 600 673  601  def readAssemblyTypes 602  references = .options.getDefault('reference', List<of String>()) to List<of String> 603   604  # Excluding the extension can be problematic  674 def _fixupLibSuffix(references as List<of String>) 605 675 i = 0 606 676 for reference in List<of String>(references) 607 677 if not reference.endsWith('.dll') and not reference.endsWith('.exe') 608 678 reference += '.dll' 609 679 references[i] = reference 610 680 i += 1  681   682 def _printRefs(references as List<of String>)  683 if references.count == 0  684 print 'No additional assembly references.'  685 else  686 print 'Final assembly reference list:'  687 i = 0  688 for refPath in references  689 print '[i]. [refPath]'  690 i += 1  691   692 def readAssemblyTypes(options as Options)  693 references = options.getDefaultLOStr('reference') 611 694   695 # Excluding the extension can be problematic  696 _fixupLibSuffix(references)  697  612 698 # now that references are fixed, make a copy so that .options['references'] is not modified further 613 699 references = List<of String>(references) 614 700  615  if .options.containsKey('pkg')Â616  for pkgName in .options['pkg'] to List<of String> 701 if options.containsKey('pkg')  702 for pkgName in options['pkg'] to List<of String> 617 703 references.addRange(.refsForPackage(pkgName)) 618 704  619  if .verbosity > 1 620  if references.count == 0 621  print 'No additional assembly references.' 622  else 623  print 'Final assembly reference list:' 624  i = 0 625  for refPath in references 626  print '[i]. [refPath]' 627  i += 1  705 if .verbosity > 1   706 _printRefs(references) 628 707  629 708 # Here be "reflectionOnlyLoad" code... which does not work on Mono 1.2.4 630 709 # The run-time error message says: -
Source/CommandLine.cobra
 32 32 # not the current Source directory. And Snapshot can be a final release such as '0.7.4' for a 33 33 # period of time where this Cobra source represents an svn-post-RELEASE. 34 34 return '0.8.0 post-release' 35   36  var _optionSpecs as List<of Dictionary<of String, Object>> 37    35  38 36 var _rawOptionSpecs = [ 39 37 { 40 38 'name': 'about', … …  201 199 }, 202 200 { 203 201 'name': 'reference', 204  'synonyms': ['r'],  202 'synonyms': ['r'], # TODO: lose 'r' ->'Ref', 'R', something else 205 203 'isAccumulator': true, 206 204 'description': 'Add a DLL reference.', 207 205 'args': 'Some.dll', … …  220 218 # 'args': ':Qualified.Type.Name', 221 219 # }, 222 220 { 223  'name': 'run',  221 'name': 'run', # TODO: Synonym 'r' after change 'reference' switch 224 222 'description': 'Runs the Cobra program. This is the default behavior if specify any Cobra source files.', 225 223 'type': 'main', 226 224 }, … …  281 279 'type': 'main', 282 280 }, 283 281 ]  282  284 283  285 284 var _startTime as DateTime 286 285 var _verbosity = 0 … …  290 289 var _htmlWriter as HtmlWriter? 291 290  292 291 var _compiler as Compiler? 293    292 var _argParser as ArgParser  293  294 294 def init 295 295 _startTime = DateTime.now 296  # prep the option specs 297  _optionSpecs = List<of Dictionary<of String, Object>>() 298  for specObj in _rawOptionSpecs 299  # since some _optionSpecs are Dictionary<of String, Object> and others are 300  # Dictionary<of String, String> then _optionSpecs ends up being 301  # Dictionary<of String, Object>  296 _argParser = ArgParser(.versionString, _rawOptionSpecs) 302 297  303  if specObj inherits Dictionary<of String, Object>Â304  d = specObjÂ305  else if specObj inherits Dictionary<of String, String>Â306  d = Dictionary<of String, Object>()Â307  for key in specObj.keysÂ308  d[key] = specObj[key]Â309  elseÂ310  throw FallThroughException(specObj.getType)Â311  _optionSpecs.add(d)Â312  Â313 298 get compiler from var 314 299  315 300 get options from var … …  324 309 get verbosity as int 325 310 return _verbosity 326 311   312 def parseArgs(args as IList<of String>, options as out Options?, paths as out List<of String>?)  313 _argParser.parseArgs(args, out options, out paths)  314 _verbosity = _argParser.verbosity  315 CobraMain.willTimeIt = _argParser.willTimeIt  316  327 317 def run 328 318 """ 329 319 Run the command line using the command line arguments. … …  339 329 .doAbout 340 330 return 341 331 .parseArgs(args, out _options, out _pathList)  332  342 333 if _options.boolValue('output-html') 343 334 _htmlWriter = HtmlWriter(Console.out) 344 335 dest = _htmlWriter to TextWriter … …  389 380 if _htmlWriter 390 381 _htmlWriter.writeHtml('</body></html>[_htmlWriter.newLine]') 391 382  392  def isOptionSpecRestrictionViolated(optionSpec as Dictionary<of String, Object>) as boolÂ393  """Â394  Returns true if the option spec has a 'restriction' key and the check against that restriction is true.Â395  """Â396  if optionSpec.containsKey('restriction')Â397  branch optionSpec['restriction'] to StringÂ398  on 'mono-only'Â399  return not CobraCore.isRunningOnMonoÂ400  return falseÂ401  Â402  def parseArgs(args as IList<of String>, options as out Options?, paths as out List<of String>?)Â403  """Â404  Parse command line arguments.Â405  The `args` should include only the arguments and not the executable/program name.Â406  """Â407  ensureÂ408  optionsÂ409  pathsÂ410  bodyÂ411  optionPrefix = '-'Â412  valuePrefix = c':'Â413  if not args.countÂ414  options = Options()Â415  options.add('help', true)Â416  paths = List<of String>()Â417  returnÂ418  Â419  specDict = Dictionary<of String, Dictionary<of String, Object>>()Â420  # ^ will contain keys for all spec names and their synonymsÂ421  synToName = Dictionary<of String, String>()Â422  # ^ maps synonyms to their full namesÂ423  synList = List<of String>()Â424  for d in _optionSpecsÂ425  if .isOptionSpecRestrictionViolated(d)Â426  continueÂ427  specDict[d['name'] to String] = dÂ428  if d.containsKey('synonyms')Â429  syns = d['synonyms'] to System.Collections.IListÂ430  for syn as String in synsÂ431  assert not specDict.containsKey(syn)Â432  specDict[syn] = dÂ433  synToName[syn] = d['name'] to StringÂ434  synList.add(syn)Â435  if not d.containsKey('type')Â436  d.add('type', 'string')Â437  Â438  # set up initial valueDictÂ439  valueDict = Dictionary<of String, Object>()Â440  if Utils.isDevMachineÂ441  valueDict['reveal-internal-exceptions'] = true # this is a specially computed default, but can still be overridden on the command lineÂ442  Â443  fileList = List<of String>()Â444  value = 'no-value' to dynamicÂ445  mainOptions = List<of String>()Â446  didSpecify = Dictionary<of String, bool>() # CC: could just be a SetÂ447  for arg in argsÂ448  if arg.trim.length == 0Â449  continueÂ450  if arg.startsWith(optionPrefix)Â451  isOption = trueÂ452  while arg.startsWith(optionPrefix)Â453  arg = arg[1:]Â454  elseÂ455  isOption = falseÂ456  if isOptionÂ457  parts = arg.split(@[valuePrefix], 2)Â458  if parts.length == 1Â459  name = parts[0]Â460  if name.endsWith('+')Â461  name = name[:-1]Â462  valueStr = 'on'Â463  else if name.endsWith('-')Â464  name = name[:-1]Â465  valueStr = 'off'Â466  elseÂ467  valueStr = 'on'Â468  elseÂ469  assert parts.length == 2Â470  name = parts[0]Â471  valueStr = parts[1]Â472  assert name.length, partsÂ473  name = Utils.getSS(synToName to passthrough, name, name) to !Â474  if not specDict.containsKey(name)Â475  msg = 'No such option "[name]".'Â476  if name.contains('=')Â477  msg += ' If you meant to specify an option value, use colon (:) instead of equals (=).'Â478  .error(msg)Â479  spec = specDict[name]Â480  if Utils.getSB(spec to passthrough, 'isAccumulator', false)Â481  # accumulators are always treated as strings. TODO: assert thatÂ482  if valueDict.containsKey(name)Â483  (valueDict[name] to System.Collections.IList).add(valueStr to passthrough)Â484  elseÂ485  valueDict[name] = [valueStr]Â486  didSpecify[name] = trueÂ487  elseÂ488  cannotProcess = falseÂ489  if name=='debug'Â490  # special caseÂ491  if valueStr=='pdbonly' or valueStr=='full'Â492  value = valueStrÂ493  elseÂ494  tryÂ495  value = .boolForString(valueStr)Â496  catch FormatExceptionÂ497  cannotProcess = trueÂ498  successÂ499  value = if(value, '+', '-')Â500  elseÂ501  if spec['type'] == 'main'Â502  mainOptions.add(name)Â503  value = trueÂ504  elseÂ505  possible = .interpretValue(valueStr, spec)Â506  if possible is not nilÂ507  value = possibleÂ508  elseÂ509  cannotProcess = trueÂ510  if cannotProcessÂ511  .error('Cannot process value "[valueStr]" for option "[name]".')Â512  valueDict[name] = valueÂ513  didSpecify[name] = trueÂ514  else # not isOptionÂ515  if File.exists(arg)Â516  fileList.add(arg)Â517  else if File.exists(arg+'.cobra')Â518  fileList.add(arg+'.cobra')Â519  else if Directory.exists(arg)Â520  fileList.add(arg)Â521  elseÂ522  msg = 'Cannot find "[arg]" as a file.'Â523  if arg.startsWith('/')Â524  msg += ' If you meant to specify an option, use dash (-) instead of slash (/).'Â525  .error(msg)Â526  Â527  # handle synonymsÂ528  for syn in synListÂ529  if valueDict.containsKey(syn)Â530  valueDict[synToName[syn]] = valueDict[syn]Â531  valueDict.remove(syn)Â532  Â533  # add in defaultsÂ534  for d in _optionSpecsÂ535  defaultName = d['name'] to StringÂ536  if not valueDict.containsKey(defaultName) and d.containsKey('default')Â537  defaultValue = .interpretValue(d['default'] to String, d) to !Â538  if .verbosityÂ539  print 'Setting option "[defaultName]" to default value [defaultValue].'Â540  valueDict[defaultName] = defaultValueÂ541  Â542  # TODO: make the option names case-insensitiveÂ543  Â544  # check for more than one main optionÂ545  if mainOptions.count > 1Â546  .error('Cannot have these main options at the same time: [Utils.join(", ", mainOptions)]')Â547  Â548  # unpack certain options into specific class fieldsÂ549  if valueDict.containsKey('verbosity')Â550  _verbosity = valueDict['verbosity'] to intÂ551  if not valueDict.containsKey('timeit') and valueDict.containsKey('testify')Â552  valueDict['timeit'] = trueÂ553  if valueDict.containsKey('timeit')Â554  CobraMain.willTimeIt = valueDict['timeit'] to boolÂ555  if valueDict.containsKey('files')Â556  for fileName as String in valueDict['files'] to System.Collections.IListÂ557  tryÂ558  for line in File.readAllLines(fileName)Â559  line = line.trimÂ560  if line.length==0 or line.startsWith('#')Â561  continueÂ562  # TODO: dup'ed aboveÂ563  arg = lineÂ564  if File.exists(arg)Â565  fileList.add(arg)Â566  else if File.exists(arg+'.cobra')Â567  fileList.add(arg+'.cobra')Â568  else if Directory.exists(arg)Â569  fileList.add(arg)Â570  elseÂ571  msg = 'Cannot find "[arg]" as a file.'Â572  #if arg.startsWith('/')Â573  # msg += ' If you meant to specify an option, use dash (-) instead of slash (/).'Â574  .error(msg)Â575  # end dupÂ576  catch IOExceptionÂ577  .error('Cannot open file "[fileName]".')Â578  Â579  # set the out parametersÂ580  options = Options(valueDict)Â581  for name in didSpecify.keysÂ582  options.didSpecify(name)Â583  paths = fileListÂ584  Â585  .computeArgImplications(options to !)Â586  Â587  Â588  def computeArgImplications(options as Options)Â589  if options.getDefault('target', '') == 'lib' and not options.isSpecified('compile')Â590  options['compile'] = trueÂ591  if options.getDefault('debug', '') not in ['', '0', '-'] and not options.isSpecified('debugging-tips')Â592  options['debugging-tips'] = falseÂ593  if options.boolValue('turbo')Â594  options['contracts'] = 'none'Â595  options['include-asserts'] = falseÂ596  options['include-nil-checks'] = falseÂ597  options['include-tests'] = falseÂ598  options['optimize'] = trueÂ599  Â600  def interpretValue(valueStr as String, spec as Dictionary<of String, Object>) as dynamic?Â601  value as dynamic?Â602  branch spec['type'] to StringÂ603  on 'main'Â604  throw InvalidOperationException('This method does not handle the main type.')Â605  on 'bool'Â606  tryÂ607  value = .boolForString(valueStr)Â608  catch FormatExceptionÂ609  cannotProcess = trueÂ610 Â
