[buildout]
extends =
  ../defaults.cfg
  ../unzip/buildout.cfg
  ../zip/buildout.cfg
parts =
  bazel
# The bazel binary contains a zip file. It must not be stripped.
do-not-strip-path = ${buildout:parts-directory}/bazel/bin/bazel

[zulu]
recipe = slapos.recipe.build:download-unpacked
shared = true
url = http://cdn.azul.com/zulu/bin/zulu8.20.0.5-jdk8.0.121-linux_x64.tar.gz
md5sum = e5f4b1d997e50ffe4998c68c8ec45403

[template-bazel-crosstool]
recipe = slapos.recipe.template:jinja2
location = ${buildout:parts-directory}/${:_buildout_section_name_}
filename = bazel_tools_cpp_CROSSTOOL
template = ${:_profile_base_location_}/${:filename}.in
rendered = ${:location}/${:filename}
context =
    key gcc_location gcc:prefix
    key binutils_location binutils:location

[template-bazel-src-main-cpp-build]
recipe = slapos.recipe.template:jinja2
location = ${buildout:parts-directory}/${:_buildout_section_name_}
filename = bazel_src_main_cpp_BUILD
template = ${:_profile_base_location_}/${:filename}.in
rendered = ${:location}/${:filename}
# We previously passed an argument that we don't want anymore. Rather than
# changing templates, we pass a dummy argument that will have no effect.
linkopts = -DSLAPOS_DUMMY=
context =
    key linkopts :linkopts

[template-bazel-src-main-tools-build]
<= template-bazel-src-main-cpp-build
filename = bazel_src_main_tools_BUILD

[template-bazel-src-tools-singlejar-build]
<= template-bazel-src-main-cpp-build
filename = bazel_src_tools_singlejar_BUILD

[bazel]
recipe = slapos.recipe.build
url = https://github.com/bazelbuild/bazel/releases/download/0.6.1/bazel-0.6.1-dist.zip
md5sum = 8c5c827e33d3ff74c263c1299810b485
patch-binary = ${patch:location}/bin/patch
patch-file-path = ${:_profile_base_location_}/bazel-0.6.1.patch
bazel-crosstool-modified-file-path = ${template-bazel-crosstool:rendered}
bazel-src-main-cpp-build-path = ${template-bazel-src-main-cpp-build:rendered}
bazel-src-main-tools-build-path = ${template-bazel-src-main-tools-build:rendered}
bazel-src-tools-singlejar-build-path = ${template-bazel-src-tools-singlejar-build:rendered}
unzip-bin = ${unzip:location}/bin
zip-bin = ${zip:location}/bin
java_home = ${zulu:location}
bazelrc = ${:_profile_base_location_}/bazelrc
install =
  import os, shutil
  extract_dir = self.extract(self.download(options['url'], options['md5sum']))
  crosstool_path = os.path.join(extract_dir, 'tools', 'cpp', 'CROSSTOOL')
  os.chmod(crosstool_path, 0644)
  shutil.copy(options['bazel-crosstool-modified-file-path'],
              crosstool_path)
  src_main_cpp_build_path = os.path.join(extract_dir, 'src', 'main', 'cpp', 'BUILD')
  os.chmod(src_main_cpp_build_path, 0644)
  shutil.copy(options['bazel-src-main-cpp-build-path'],
              src_main_cpp_build_path)
  src_main_tools_build_path = os.path.join(extract_dir, 'src', 'main', 'tools', 'BUILD')
  os.chmod(src_main_tools_build_path, 0644)
  shutil.copy(options['bazel-src-main-tools-build-path'],
              src_main_tools_build_path)
  src_tools_singlejar_build_path = os.path.join(extract_dir, 'src', 'tools', 'singlejar', 'BUILD')
  os.chmod(src_tools_singlejar_build_path, 0644)
  shutil.copy(options['bazel-src-tools-singlejar-build-path'],
              src_tools_singlejar_build_path)

  target_path = extract_dir+'/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleImplementationFunctions.java'
  os.chmod(target_path, 0644)
  call([self.buildout['bazel']['patch-binary'], '-p1', '-d', extract_dir, '-i', self.buildout['bazel']['patch-file-path']])
  path = ':'.join((
    options['unzip-bin'],
    options['zip-bin'],
    os.environ['PATH']
    ))
  env = {'JAVA_HOME':options['java_home'],
         'PATH':path,
         'BAZELRC':options['bazelrc'],
         'VERBOSE':'yes',
        }
  bin_dir = os.path.join(location, 'bin')
  os.makedirs(bin_dir)
  call(['bash', 'compile.sh', 'compile'], cwd=extract_dir, env=env)
  shutil.copy(os.path.join(extract_dir, 'output', 'bazel'),
              os.path.join(bin_dir, 'bazel'))
