[buildout]
extends =
  ../scipy/buildout.cfg
  ../protobuf-python/buildout.cfg
  ../tensorboard/buildout.cfg
parts =
  tensorflow

[numpy-egg]
recipe = zc.recipe.egg
initialization =
  import scipy.spatial.ckdtree # load our own libstdc++ explicitly at the very beginning
eggs =
  setuptools
  ${scipy:egg}
  ${numpy:egg}
interpreter = numpy-python
scripts = numpy-python

[tensorflow-repository]
recipe = plone.recipe.command
stop-on-error = true
repository = https://github.com/tensorflow/tensorflow
commit = v${versions:tensorflow}
location = ${buildout:parts-directory}/${:_buildout_section_name_}
command =
  set -e
  export PATH=${git:location}/bin:$PATH
  ( git clone --quiet -n ${:repository} ${:location}
    cd ${:location}
    git reset --merge ${:commit}
    git apply ${:_profile_base_location_}/tensorflow-r1.4.patch
  ) || { rm -fr ${:location}; exit 1; }


[cuda]
tf_need_cuda = 1
tf_cuda_version = 8.0
tf_cudnn_version = 5.1.10
tf_cuda_compute_capabilities = 5.2,6.1
cuda_toolkit_path = /usr/local/cuda
cudnn_install_path = /usr/local/cuda

[tensorflow]
recipe = slapos.recipe.build
egg = ${:_buildout_section_name_}
workdir = ${tensorflow-repository:location}
egg-name = ${:egg}-${versions:tensorflow}-py${python:version}-linux-x86_64.egg
location = ${buildout:eggs-directory}/${:egg-name}
numpy-python-command = ${buildout:bin-directory}/${numpy-egg:interpreter}
python-lib = ${python:location}/lib
java_home_bin = ${bazel:java_home}/bin
bazel_bin = ${bazel:location}/bin
need_cuda = ${cuda:cuda_toolkit_path}
depends =
  ${tensorflow_tensorboard:recipe}
  ${protobuf-python:egg}
install =
  import os, shutil
  from setuptools.archive_util import unpack_archive
  workdir = options['workdir']
  env = {'PATH':':'.join([options['java_home_bin'],
                          options['bazel_bin'],
                          os.environ['PATH']]),
         'PYTHON_BIN_PATH':options['numpy-python-command'],
         'PYTHON_LIB_PATH':options['python-lib'],
         'CC_OPT_FLAGS':'-march=native',
         'TF_NEED_JEMALLOC':'1',
         'TF_ENABLE_XLA':'0',
         'TF_NEED_GCP':'0',
         'TF_NEED_HDFS':'0',
         'TF_NEED_OPENCL':'0',
         'TF_NEED_MKL':'0',
         'TF_NEED_VERBS':'0',
         'TF_CUDA_CLANG':'0',
         'TF_NEED_MPI':'0',
         'TF_NEED_CUDA':self.buildout['cuda']['tf_need_cuda'],
         ##### FOR CUDA #####
         'TF_CUDA_VERSION':self.buildout['cuda']['tf_cuda_version'],
         'CUDA_TOOLKIT_PATH':self.buildout['cuda']['cuda_toolkit_path'],
         'TF_CUDNN_VERSION':self.buildout['cuda']['tf_cudnn_version'],
         'CUDNN_INSTALL_PATH':self.buildout['cuda']['cudnn_install_path'],
         'TF_CUDA_COMPUTE_CAPABILITIES':self.buildout['cuda']['tf_cuda_compute_capabilities'],
         ####################
         }
  if not os.path.exists(env['CUDA_TOOLKIT_PATH']): env['TF_NEED_CUDA'] = '0'
  call(['./configure'], cwd=workdir, env=env)
  bazel_command = (env['TF_NEED_CUDA'] == '1' and
    ['bazel', 'build', '-c', 'opt', '--copt', '-march=native', '--config', 'cuda', '-s', '--verbose_failures', '//tensorflow/tools/pip_package:build_pip_package']
    or
    ['bazel', 'build', '-c', 'opt', '--copt', '-march=native', '-s', '--verbose_failures', '//tensorflow/tools/pip_package:build_pip_package'])
  os.chdir(workdir)
  try:
    call(bazel_command, env=env)
    call(('bazel-bin/tensorflow/tools/pip_package/build_pip_package',
          os.path.join(workdir, 'dist')))
    unpack_archive(os.path.join('dist', options['egg-name']), location)
  finally:
    for d in 'dist',:
      if os.path.exists(d):
        shutil.rmtree(d)

[versions]
tensorflow = 1.4.0
