[buildout]
extends =
  python-2.7/buildout.cfg
  python3/buildout.cfg
python = python

# Unless a software release needs several versions of either Python or GCC
# at the same time, the [pythonX.Y] & [gcc-X.Y] must not be referred directly,
# even if a component works only with specific versions.
# There may be exceptions in profiles that were written before this one.

[python]
recipe = slapos.recipe.build
part = python2.7
init =
  python = self.buildout[options['part']]
  for x in 'location', 'executable', 'version':
    options[x] = python[x]
update =
  import os
  path, os.environ['PYTHON'] = os.path.split(options['executable'])
  PATH = os.environ['PATH']
  if path not in PATH.split(os.pathsep):
    os.environ['PATH'] = path + os.pathsep + PATH
depends = ${gcc:recipe}

[gcc]
recipe = slapos.recipe.build
# Latest version provided by SlapOS.
part = gcc-8.2
# Minimum version for all components that might be required for
# slapos.rebootstrap (see https://bugs.python.org/issue34112 about Python 3.7+).
min_version = 5.4
init =
  import os, subprocess
  parse_version = lambda ver: tuple(map(int, ver.strip().split('.')))
  try:
    current = subprocess.check_output(('gcc', '-dumpfullversion'),
                                      stderr=subprocess.STDOUT,
                                      universal_newlines=True).strip()
  except subprocess.CalledProcessError: # BBB: old GCC
    current = subprocess.check_output(('gcc', '-dumpversion'),
                                      universal_newlines=True).strip()
  self.system_version = current
  # If we're still going to use the same GCC,
  # the conditions have no impact on the dependant parts.
  min_version = options.pop('min_version', None)
  max_version = options.pop('max_version', None)
  ###
  if (parse_version(min_version or current)
      <= parse_version(current)
      <= parse_version(max_version or current)):
    del options['part']
    for path in os.getenv('PATH', '').split(os.pathsep): # PY3: shutil.which
      gcc = os.path.join(path, 'gcc')
      if os.access(gcc, os.X_OK) and not os.path.isdir(gcc):
        options['prefix'] = os.path.dirname(path)
        break
  else:
    options['prefix'] = self.buildout[options['part']]['location']
  options.barrier()
update =
  if 'part' in options:
    import os
    env = os.environ
    env['PATH'] = os.pathsep.join((
      os.path.join(options['prefix'], 'bin'),
      env['PATH']
    ))
  else:
    print("Using system GCC (%s)" % self.system_version)
