Skip to content

Setting repo wide compilation defaults

P. Oscar Boykin edited this page Apr 30, 2016 · 1 revision

It is common to want to set options for your site that apply to all targets. The recommended pattern is to wrap the rules in this repo with macros that add options. For instance:

# in file local_tools/scala/scala.bzl
load("@io_bazel_rules_scala//scala:scala.bzl",
  uppstream_lib = "scala_library",
  uppstream_macro = "scala_macro_library",
  uppstream_bin = "scala_binary",
  uppstream_test = "scala_test")

_default_scalac_opts = [
  "-Ywarn-dead-code",
  "-Ywarn-unused-import", # this option can be dangerous
  "-Ywarn-value-discard",
  "-Xmax-classfile-name", "128", # Linux laptops with encrypted home don't like long file names
  "-Xlint",
  "-Xfuture",
  ]

def _with_default_so(opts):
  # this might need to be more sophisticated
  return opts + _default_scalac_opts

def scala_library(name, srcs = [], deps = [], runtime_deps = [], data = [], resources = [],
  scalacopts = [], jvm_flags = [], main_class = "", exports = [], visibility = None):
  uppstream_lib(name = name, srcs = srcs, deps = deps, runtime_deps = runtime_deps,
    resources = resources, scalacopts = _with_default_so(scalacopts),
    jvm_flags = jvm_flags, main_class = main_class, exports = exports, visibility = visibility)

def scala_macro_library(name, srcs = [], deps = [], runtime_deps = [], data = [], resources = [],
  scalacopts = [], jvm_flags = [], main_class = "", exports = [], visibility = None):
  uppstream_macro(name = name, srcs = srcs, deps = deps, runtime_deps = runtime_deps,
    resources = resources, scalacopts = _with_default_so(scalacopts),
    jvm_flags = jvm_flags, main_class = main_class, exports = exports, visibility = visibility)

def scala_binary(name, srcs = [], deps = [], runtime_deps = [], data = [], resources = [],
  scalacopts = [], jvm_flags = [], main_class = "", visibility = None):
  uppstream_bin(name = name, srcs = srcs, deps = deps, runtime_deps = runtime_deps,
    resources = resources, scalacopts = _with_default_so(scalacopts),
    jvm_flags = jvm_flags, main_class = main_class, visibility = visibility)

def scala_test(name, srcs = [], deps = [], runtime_deps = [], data = [], resources = [],
  scalacopts = [], jvm_flags = [], visibility = None, size = None):
  uppstream_test(name = name, srcs = srcs, deps = deps, runtime_deps = runtime_deps,
    resources = resources, scalacopts = _with_default_so(scalacopts),
    jvm_flags = jvm_flags, visibility = visibility, size = None)

Then load these rules in your tools/build_tools/prelude:

load("/local_tools/scala/scala", "scala_library", "scala_macro_library","scala_binary", "scala_test")
Clone this wiki locally