Class: Buildr::Scala::Scalac

Inherits:
Compiler::Base show all
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|
  version = Buildr.settings.build['scala.version'] || DEFAULT_VERSION
  ns.library!      'org.scala-lang:scala-library:jar:>=' + version
  ns.compiler!     'org.scala-lang:scala-compiler:jar:>=' + version
end
Javac =
Buildr::Compiler::Javac
OPTIONS =
[:warnings, :deprecation, :optimise, :target, :debug, :other, :javac]

Instance Attribute Summary

Attributes inherited from Compiler::Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Compiler::Base

#dependencies, #needed?, specify, to_sym

Constructor Details

#initialize(project, options) ⇒ Scalac

:nodoc:



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/buildr/scala/compiler.rb', line 153

def initialize(project, options) #:nodoc:
  super
  options[:debug] = Buildr.options.debug if options[:debug].nil?
  options[:warnings] = verbose if options[:warnings].nil?
  options[:deprecation] ||= false
  options[:optimise] ||= false
  options[:make] ||= :transitivenocp if Scala.compatible_28?
  options[:javac] ||= {}

  @java = Javac.new(project, options[:javac])
end

Class Method Details

.applies_to?(project, task) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


134
135
136
137
138
139
140
# File 'lib/buildr/scala/compiler.rb', line 134

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

.dependenciesObject



122
123
124
125
126
127
128
# File 'lib/buildr/scala/compiler.rb', line 122

def dependencies
  if use_installed?
    ['scala-library', 'scala-compiler'].map { |s| File.expand_path("lib/#{s}.jar", scala_home) }
  else
    REQUIRES.artifacts.map(&:to_s)
  end
end

.installed?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/buildr/scala/compiler.rb', line 110

def installed?
  !scala_home.nil?
end

.scala_homeObject



100
101
102
103
104
105
106
107
108
# File 'lib/buildr/scala/compiler.rb', line 100

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_fscObject



130
131
132
# File 'lib/buildr/scala/compiler.rb', line 130

def use_fsc
  use_installed? && ENV["USE_FSC"] =~ /^(yes|on|true)$/i
end

.use_installed?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/buildr/scala/compiler.rb', line 114

def use_installed?
  if installed? && Buildr.settings.build['scala.version']
    Buildr.settings.build['scala.version'] == Scala.installed_version
  else
    Buildr.settings.build['scala.version'].nil? && installed?
  end
end

Instance Method Details

#compile(sources, target, dependencies) ⇒ Object

:nodoc:



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
# File 'lib/buildr/scala/compiler.rb', line 165

def compile(sources, target, dependencies) #:nodoc:
  check_options(options, OPTIONS + (Scala.compatible_28? ? [:make] : []))

  java_sources = java_sources(sources)
  enable_dep_tracing = Scala.compatible_28? && java_sources.empty?

  dependencies.unshift target if enable_dep_tracing

  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.expand_path(target)
  cmd_args += scalac_args

  if enable_dep_tracing
    dep_dir = File.expand_path(target)
    Dir.mkdir dep_dir unless File.exists? dep_dir

    cmd_args << '-make:' + options[:make].to_s
    cmd_args << '-dependencyfile'
    cmd_args << File.expand_path('.scala-deps', dep_dir)
  end

  cmd_args += files_from_sources(sources)

  unless Buildr.application.options.dryrun
    trace((['scalac'] + cmd_args).join(' '))

    if Scalac.use_fsc
      system(([File.expand_path('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

    unless java_sources.empty?
      trace 'Compiling mixed Java/Scala sources'

      # TODO  includes scala-compiler.jar
      deps = dependencies + Scalac.dependencies + [ File.expand_path(target) ]
      @java.compile(java_sources, target, deps)
    end
  end
end