logo

class

build::CompileCs

sys::Obj
  build::Task
    build::CompileCs
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   10 Jan 06  Andy Frank  Creation
   7  //
   8  
   9  using compiler
  10  
  11  **
  12  ** Run the C# compiler to produce an exe or dll.
  13  **
  14  class CompileCs : Task
  15  {
  16  
  17  //////////////////////////////////////////////////////////////////////////
  18  // Construction
  19  //////////////////////////////////////////////////////////////////////////
  20  
  21    **
  22    ** Initialize the .NET environment fields for csc.exe.
  23    **
  24    new make(BuildScript script)
  25      : super(script)
  26    {
  27      // netHomeDir
  28      netHomeProp := Sys.env["fan.build.netHome"]
  29      try
  30      {
  31        netHomeDir = netHomeProp.toUri.toFile
  32        if (!netHomeDir.exists || !netHomeDir.isDir) throw Err.make
  33      }
  34      catch
  35      {
  36        throw fatal("Missing or invalid URI for fan.build.netHome: $netHomeProp")
  37      }
  38  
  39      // derived files
  40      cscExe = netHomeDir + `csc.exe`
  41    }
  42  
  43  //////////////////////////////////////////////////////////////////////////
  44  // Run
  45  //////////////////////////////////////////////////////////////////////////
  46  
  47    **
  48    ** Run the csc task
  49    **
  50    override Void run()
  51    {
  52      log.info("CompileCs")
  53  
  54      try
  55      {
  56        // build command
  57        cmd := [cscExe.osPath]
  58  
  59        // default paramaters
  60        cmd.add("/nologo")
  61        cmd.add("/fullpaths")
  62        cmd.add("/debug:full")
  63  
  64        // /out:output
  65        if (output != null)
  66        {
  67          cmd.add("/out:$output.osPath")
  68        }
  69  
  70        // /target:targetType
  71        if (targetType != null)
  72        {
  73          cmd.add("/target:$targetType")
  74        }
  75  
  76        // /r:<libs>
  77        if (libs != null)
  78        {
  79          s := libs.join(";") |File f->Str| { return f.osPath }
  80          cmd.add("/r:$s")
  81        }
  82  
  83        // src files/dirs
  84        src.each |File f|
  85        {
  86          if (f.isDir)
  87            cmd.add((f + `*.cs`).osPath)
  88          else
  89            cmd.add(f.osPath)
  90        }
  91  
  92        log.debug(cmd.join(" "))
  93        r := Process.make(cmd).run
  94        if (r != 0) throw Err.make
  95      }
  96      catch (Err err)
  97      {
  98        throw fatal("CompileCs failed")
  99      }
 100    }
 101  
 102  //////////////////////////////////////////////////////////////////////////
 103  // Fields
 104  //////////////////////////////////////////////////////////////////////////
 105  
 106    ** Home directory for .NET installation
 107    ** configured via Sys.env["fan.build.netHome"]
 108    File netHomeDir
 109  
 110    ** C# compiler executable: {netHomeDir}/csc.exe
 111    File cscExe
 112  
 113    ** Output file created by the compiler.
 114    File output
 115  
 116    ** Output target type
 117    Str targetType
 118  
 119    ** List of dll libraries to link in
 120    File[] libs
 121  
 122    ** List of source files or directories to compile
 123    File[] src := File[,]
 124  
 125  }