[buildout]
extends =
  ../gcc/buildout.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 = hexagonit.recipe.download
ignore-existing = true
url = http://cdn.azul.com/zulu/bin/zulu8.20.0.5-jdk8.0.121-linux_x64.tar.gz
md5sum = e5f4b1d997e50ffe4998c68c8ec45403
strip-top-level-dir = true

[template-bazel-crosstool]
recipe = slapos.recipe.template:jinja2
location = ${buildout:parts-directory}/${:_buildout_section_name_}
mode = 640
filename = bazel_tools_cpp_CROSSTOOL
template = ${:_profile_base_location_}/${:filename}.in
rendered = ${:location}/${:filename}
cpp_path = ${gcc:location}/bin/cpp
gcc_path = ${gcc:location}/bin/gcc
gconv_path = ${gcc:location}/bin/gconv
include_path = ${gcc:location}/include
gcc_lib64_path = ${gcc:location}/lib64
context =
    key cpp_path template-bazel-crosstool:cpp_path
    key gcc_path template-bazel-crosstool:gcc_path
    key gconv_path template-bazel-crosstool:gconv_path
    key include_path template-bazel-crosstool:include_path
    key gcc_lib64_path template-bazel-crosstool:gcc_lib64_path

[bazel]
recipe = slapos.recipe.build
url = https://github.com/bazelbuild/bazel/releases/download/0.4.3/bazel-0.4.3-dist.zip
md5sum = cbd53f6f59915506da8998dab2098921
patch-binary = ${patch:location}/bin/patch
patch-file-path = ${:_profile_base_location_}/bazel-0.4.3.patch
bazel-crosstool-modified-file-path = ${template-bazel-crosstool:rendered}
unzip-bin = ${unzip:location}/bin
zip-bin = ${zip:location}/bin
gcc-bin = ${gcc:location}/bin
gcc-lib = ${gcc:location}/lib
gcc-lib64 = ${gcc:location}/lib64
java_home = ${zulu:location}
script =
  extract_dir = self.extract(self.download(self.options['url'], self.options['md5sum']))
  crosstool_path = os.path.join(extract_dir, 'tools', 'cpp', 'CROSSTOOL')
  os.chmod(crosstool_path, 0644)
  shutil.copy(self.options['bazel-crosstool-modified-file-path'],
              crosstool_path)
  target_path = extract_dir+'/src/main/java/com/google/devtools/build/lib/rules/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((
    self.options['unzip-bin'],
    self.options['zip-bin'],
    self.options['gcc-bin'],
    os.environ['PATH']
    ))
  env = {'JAVA_HOME':self.options['java_home'],
         'PATH':path,
         'LD_LIBRARY_PATH':':'.join((
	   self.options['gcc-lib'],
	   self.options['gcc-lib64'],
           os.environ.get('LD_LIBRARY_PATH', '')
           )),
         'LDFLAGS':'-Wl,-rpath='+self.options['gcc-lib64'],
         'CC':self.options['gcc-bin']+'/gcc',
         'CXX':self.options['gcc-bin']+'/g++',
        }
  bin_dir = os.path.join(self.options['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'))