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|
  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

#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:



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

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[:javac] ||= {}

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

Class Method Details

.applies_to?(project, task) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


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

.dependenciesObject



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.expand_path("lib/#{s}.jar", scala_home) }
  else
    REQUIRES.artifacts.map(&:to_s)
  end
end

.installed?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/buildr/scala/compiler.rb', line 87

def installed?
  !scala_home.nil?
end

.scala_homeObject



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_fscObject



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

    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.expand_path(target) ]
      @java.compile(java_sources, target, deps)
    end
  end
end