summaryrefslogtreecommitdiff
path: root/cli/src/main/scala/scala/scalajs/cli/Scalajsld.scala
blob: 55e61af3f98274ec3298c1011422a13a443dd01d (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
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js CLI               **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013-2014, LAMP/EPFL   **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */


package scala.scalajs.cli

import scala.scalajs.ir.ScalaJSVersions

import scala.scalajs.tools.sem._
import scala.scalajs.tools.io._
import scala.scalajs.tools.logging._
import scala.scalajs.tools.classpath._
import scala.scalajs.tools.classpath.builder._

import CheckedBehavior.Compliant

import scala.scalajs.tools.optimizer.{
  ScalaJSOptimizer,
  ScalaJSClosureOptimizer,
  ParIncOptimizer
}

import scala.collection.immutable.Seq

import java.io.File
import java.net.URI

object Scalajsld {

  case class Options(
    cp: Seq[File] = Seq.empty,
    output: File = null,
    jsoutput: Option[File] = None,
    semantics: Semantics = Semantics.Defaults,
    noOpt: Boolean = false,
    fullOpt: Boolean = false,
    prettyPrint: Boolean = false,
    sourceMap: Boolean = false,
    relativizeSourceMap: Option[URI] = None,
    checkIR: Boolean = false,
    stdLib: Option[File] = None,
    logLevel: Level = Level.Info)

  def main(args: Array[String]): Unit = {
    val parser = new scopt.OptionParser[Options]("scalajsld") {
      head("scalajsld", ScalaJSVersions.current)
      arg[File]("<value> ...")
        .unbounded()
        .action { (x, c) => c.copy(cp = c.cp :+ x) }
        .text("Entries of Scala.js classpath to link")
      opt[File]('o', "output")
        .valueName("<file>")
        .required()
        .action { (x, c) => c.copy(output = x) }
        .text("Output file of linker (required)")
      opt[File]("jsoutput")
        .valueName("<file>")
        .abbr("jo")
        .action { (x, c) => c.copy(jsoutput = Some(x)) }
        .text("Concatenate all JavaScript libary dependencies to this file")
      opt[Unit]('f', "fastOpt")
        .action { (_, c) => c.copy(noOpt = false, fullOpt = false) }
        .text("Optimize code (this is the default)")
      opt[Unit]('n', "noOpt")
        .action { (_, c) => c.copy(noOpt = true, fullOpt = false) }
        .text("Don't optimize code")
      opt[Unit]('u', "fullOpt")
        .action { (_, c) => c.copy(noOpt = false, fullOpt = true) }
        .text("Fully optimize code (uses Google Closure Compiler)")
      opt[Unit]('p', "prettyPrint")
        .action { (_, c) => c.copy(prettyPrint = true) }
        .text("Pretty print full opted code (meaningful with -u)")
      opt[Unit]('s', "sourceMap")
        .action { (_, c) => c.copy(sourceMap = true) }
        .text("Produce a source map for the produced code")
      opt[Unit]("compliantAsInstanceOfs")
        .action { (_, c) => c.copy(semantics =
          c.semantics.withAsInstanceOfs(Compliant))
        }
        .text("Use compliant asInstanceOfs")
      opt[Unit]('c', "checkIR")
        .action { (_, c) => c.copy(checkIR = true) }
        .text("Check IR before optimizing")
      opt[File]('r', "relativizeSourceMap")
        .valueName("<path>")
        .action { (x, c) => c.copy(relativizeSourceMap = Some(x.toURI)) }
        .text("Relativize source map with respect to given path (meaningful with -s)")
      opt[Unit]("noStdlib")
        .action { (_, c) => c.copy(stdLib = None) }
        .text("Don't automatcially include Scala.js standard library")
      opt[File]("stdlib")
        .valueName("<scala.js stdlib jar>")
        .hidden()
        .action { (x, c) => c.copy(stdLib = Some(x)) }
        .text("Location of Scala.js standard libarary. This is set by the " +
            "runner script and automatically prepended to the classpath. " +
            "Use -n to not include it.")
      opt[Unit]('d', "debug")
        .action { (_, c) => c.copy(logLevel = Level.Debug) }
        .text("Debug mode: Show full log")
      opt[Unit]('q', "quiet")
        .action { (_, c) => c.copy(logLevel = Level.Warn) }
        .text("Only show warnings & errors")
      opt[Unit]("really-quiet")
        .abbr("qq")
        .action { (_, c) => c.copy(logLevel = Level.Error) }
        .text("Only show errors")
      version("version")
        .abbr("v")
        .text("Show scalajsld version")
      help("help")
        .abbr("h")
        .text("prints this usage text")

      override def showUsageOnError = true
    }

    for (options <- parser.parse(args, Options())) {
      val cpFiles = options.stdLib.toList ++ options.cp
      // Load and resolve classpath
      val cp = PartialClasspathBuilder.build(cpFiles).resolve()

      // Write JS dependencies if requested
      for (jsout <- options.jsoutput)
        IO.concatFiles(WritableFileVirtualJSFile(jsout), cp.jsLibs.map(_.lib))

      // Link Scala.js code
      val outFile = WritableFileVirtualJSFile(options.output)
      if (options.fullOpt)
        fullOpt(cp, outFile, options)
      else
        fastOpt(cp, outFile, options)
    }
  }

  private def fullOpt(cp: IRClasspath,
      output: WritableVirtualJSFile, options: Options) = {
    import ScalaJSClosureOptimizer._

    val semantics = options.semantics.optimized

    new ScalaJSClosureOptimizer(semantics).optimizeCP(
        newScalaJSOptimizer(semantics),
        Inputs(ScalaJSOptimizer.Inputs(cp)),
        OutputConfig(
            output = output,
            wantSourceMap = options.sourceMap,
            relativizeSourceMapBase = options.relativizeSourceMap,
            checkIR = options.checkIR,
            prettyPrint = options.prettyPrint),
        newLogger(options))
  }

  private def fastOpt(cp: IRClasspath,
      output: WritableVirtualJSFile, options: Options) = {
    import ScalaJSOptimizer._

    newScalaJSOptimizer(options.semantics).optimizeCP(
        Inputs(cp),
        OutputConfig(
            output = output,
            wantSourceMap = options.sourceMap,
            checkIR = options.checkIR,
            disableOptimizer = options.noOpt,
            relativizeSourceMapBase = options.relativizeSourceMap),
        newLogger(options))
  }

  private def newLogger(options: Options) =
    new ScalaConsoleLogger(options.logLevel)

  private def newScalaJSOptimizer(semantics: Semantics) =
    new ScalaJSOptimizer(semantics, new ParIncOptimizer(_))

}