Class: Buildr::Scala::Scalac
- Inherits:
-
Compiler::Base
- Object
- Compiler::Base
- Buildr::Scala::Scalac
- Defined in:
- lib/buildr/scala/compiler.rb
Overview
Scalac compiler:
compile.using(:scalac)
Used by default if .scala files are found in the src/main/scala directory (or src/test/scala) and sets the target directory to target/classes (or target/test/classes).
Accepts the following options:
-
:warnings – Generate warnings if true (opposite of -nowarn).
-
:deprecation – Output source locations where deprecated APIs are used.
-
:optimise – Generates faster bytecode by applying optimisations to the program.
-
:target – Class file compatibility with specified release.
-
:debug – Generate debugging info.
-
:other – Array of options to pass to the Scalac compiler as is, e.g. -Xprint-types
Constant Summary collapse
- REQUIRES =
The scalac compiler jars are added to classpath at load time, if you want to customize artifact versions, you must set them on the
artifact_ns['Buildr::Compiler::Scalac'].library = '2.7.5'
namespace before this file is required. This is of course, only if SCALA_HOME is not set or invalid.
ArtifactNamespace.for(self) do |ns| ns.library! 'org.scala-lang:scala-library:jar:>=' + DEFAULT_VERSION ns.compiler! 'org.scala-lang:scala-compiler:jar:>=' + DEFAULT_VERSION end
- Javac =
Buildr::Compiler::Javac
- OPTIONS =
[:warnings, :deprecation, :optimise, :target, :debug, :other, :javac]
Instance Attribute Summary
Attributes inherited from Compiler::Base
Class Method Summary collapse
-
.applies_to?(project, task) ⇒ Boolean
:nodoc:.
- .dependencies ⇒ Object
- .installed? ⇒ Boolean
- .scala_home ⇒ Object
- .use_fsc ⇒ Object
Instance Method Summary collapse
-
#compile(sources, target, dependencies) ⇒ Object
:nodoc:.
-
#initialize(project, options) ⇒ Scalac
constructor
:nodoc:.
Methods inherited from Compiler::Base
#dependencies, #needed?, specify, to_sym
Constructor Details
#initialize(project, options) ⇒ Scalac
:nodoc:
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/buildr/scala/compiler.rb', line 122 def initialize(project, ) #:nodoc: super [:debug] = Buildr..debug if [:debug].nil? [:warnings] = verbose if [:warnings].nil? [:deprecation] ||= false [:optimise] ||= false [:javac] ||= {} @java = Javac.new(project, [:javac]) end |
Class Method Details
.applies_to?(project, task) ⇒ Boolean
:nodoc:
103 104 105 106 107 108 109 |
# File 'lib/buildr/scala/compiler.rb', line 103 def applies_to?(project, task) #:nodoc: paths = task.sources + [sources].flatten.map { |src| Array(project.path_to(:source, task.usage, src.to_sym)) } paths.flatten! # Just select if we find .scala files paths.any? { |path| !Dir["#{path}/**/*.scala"].empty? } end |
.dependencies ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/buildr/scala/compiler.rb', line 91 def dependencies if installed? ['scala-library', 'scala-compiler'].map { |s| File.("lib/#{s}.jar", scala_home) } else REQUIRES.artifacts.map(&:to_s) end end |
.installed? ⇒ Boolean
87 88 89 |
# File 'lib/buildr/scala/compiler.rb', line 87 def installed? !scala_home.nil? end |
.scala_home ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/buildr/scala/compiler.rb', line 77 def scala_home env_home = ENV['SCALA_HOME'] @home ||= (if !env_home.nil? && File.exists?(env_home + '/lib/scala-library.jar') && File.exists?(env_home + '/lib/scala-compiler.jar') env_home else nil end) end |
.use_fsc ⇒ Object
99 100 101 |
# File 'lib/buildr/scala/compiler.rb', line 99 def use_fsc installed? && ENV["USE_FSC"] =~ /^(yes|on|true)$/i end |
Instance Method Details
#compile(sources, target, dependencies) ⇒ Object
:nodoc:
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 |
# File 'lib/buildr/scala/compiler.rb', line 133 def compile(sources, target, dependencies) #:nodoc: , OPTIONS cmd_args = [] cmd_args << '-classpath' << dependencies.join(File::PATH_SEPARATOR) source_paths = sources.select { |source| File.directory?(source) } cmd_args << '-sourcepath' << source_paths.join(File::PATH_SEPARATOR) unless source_paths.empty? cmd_args << '-d' << File.(target) cmd_args += scalac_args cmd_args += files_from_sources(sources) unless Buildr.application..dryrun trace((['scalac'] + cmd_args).join(' ')) if Scalac.use_fsc system(([File.('bin/fsc', Scalac.scala_home)] + cmd_args).join(' ')) or fail 'Failed to compile, see errors above' else Java.load begin Java.scala.tools.nsc.Main.process(cmd_args.to_java(Java.java.lang.String)) rescue => e fail "Scala compiler crashed:\n#{e.inspect}" end fail 'Failed to compile, see errors above' if Java.scala.tools.nsc.Main.reporter.hasErrors end java_sources = java_sources(sources) unless java_sources.empty? trace 'Compiling mixed Java/Scala sources' # TODO includes scala-compiler.jar deps = dependencies + Scalac.dependencies + [ File.(target) ] @java.compile(java_sources, target, deps) end end end |