[buildout]
extends =
  ../bzip2/buildout.cfg
  ../gdbm/buildout.cfg
  ../libexpat/buildout.cfg
  ../libffi/buildout.cfg
  ../ncurses/buildout.cfg
  ../openssl/buildout.cfg
  ../patch/buildout.cfg
  ../pkgconfig/buildout.cfg
  ../python-2.7/buildout.cfg
  ../sqlite3/buildout.cfg
  ../zlib/buildout.cfg

[pypy2]
recipe = slapos.recipe.build
shared = true
version = 2.7
url = https://downloads.python.org/pypy/pypy${:version}-v7.3.11-src.tar.bz2
md5sum = 249ad8b0ddffbb16a9d416c3ae85dd2c
pycparser-url = https://github.com/eliben/pycparser/archive/release_v2.21.tar.gz
pycparser-md5sum = cf4b60f0beca2a25bb599e9e858a8223
patch-binary = ${patch:location}/bin/patch
patches = ${python2.7-lib-patches:patches}
init =
  import os, sys
  options['executable'] = os.path.join(location, 'bin', 'pypy')
  if sys.version_info.major == 2:
    self.python = sys.executable
  else:
    # XXX: We don't care which Python 2 we use to build PyPy
    #      and we don't want to rebootstrap twice.
    depends = options.depends
    options.depends = set(depends)
    self.python = self.buildout['python2.7']['executable']
    options.depends = depends
install =
  import os
  env = self.environ
  pycparser = self.extract(self.download(
    options['pycparser-url'], options['pycparser-md5sum']))
  pycparser_lib = os.path.join(pycparser, 'lib')
  call((self.python, 'setup.py', 'install', '--install-lib', pycparser_lib),
       cwd=guessworkdir(pycparser), env=env)
  pypy_src = guessworkdir(self.extract(self.download()))
  build_cffi_imports = 'lib_pypy/pypy_tools/build_cffi_imports.py'
  version = options['version']
  self.applyPatchList(options['patches'].replace('#',' '),
    '--no-backup-if-mismatch -p2', options['patch-binary'],
    os.path.join(pypy_src, 'lib-python', version))
  with open(os.path.join(pypy_src, 'Makefile'), 'w') as f:
    f.write(options['makefile'].replace('\n|','\n')[1:] % {
      'build_cffi_imports': build_cffi_imports,
      'location': location,
      'pycparser': pycparser_lib,
      'python': self.python,
      'version': version,
    })
  for cmd in ( ('sed', '-i', '/"_tkinter",/s/^/#/', build_cffi_imports)
             , ('make',)
             , ('make', 'install')
             ):
    call(cmd, cwd=pypy_src, env=env)
  for x in '', version[0], version:
    os.symlink('pypy', os.path.join(location, 'bin', 'python' + x))
# WKRD: Buildout does not preserve leading tabs in .installed.cfg
#       so prefix with a dummy character.
makefile =
  |PREFIX = %(location)s
  |export PYPY_USESSION_BASENAME=slapos
  |export TMPDIR=$(realpath ..)
  |all: pypy/goal/pypy-c %(build_cffi_imports)s
  |	$^
  |c_src_dir:
  |	cd pypy/goal && PYTHONPATH=%(pycparser)s %(python)s ../../rpython/bin/rpython --batch --source --opt=jit --shared targetpypystandalone
  |	ln -s ../usession-$$PYPY_USESSION_BASENAME-$$USER/testing_1 $@
  |pypy/goal/pypy-c: c_src_dir
  |	$(MAKE) -C $<
  |	mv $</libpypy-c.so $</pypy-c pypy/goal
  |	touch $@
  |install:
  |	mkdir -p $(PREFIX)/bin $(PREFIX)/include
  |	find lib_pypy lib-python/%(version)s -type d '(' '(' -name __pycache__ -o -name _tkinter -o -name test -o -name tests ')' -prune -o -print ')' \
  |	|while read d; do mkdir -p $(PREFIX)/$$d && find $$d -maxdepth 1 -type f ! -name '*.o' ! -name '*.c' ! -name '*.pyc' |xargs -r cp -t $(PREFIX)/$$d; done
  |	d=lib-python/%(version)s/test && mkdir -p $(PREFIX)/$$d && for x in __init__ pystone regrtest test_support; do echo $$d/$$x.py; done |xargs -r cp -t $(PREFIX)/$$d
  |	cd lib-python && cp conftest.py stdlib-version.* $(PREFIX)/lib-python
  |	cp -r include/pypy_*.h pypy/module/cpyext/include/* pypy/module/cpyext/parse/* $(PREFIX)/include
  |	cd pypy/goal && cp libpypy-c.so $(PREFIX)/bin && cp pypy-c $(PREFIX)/bin/pypy
  |	rm $(PREFIX)/lib_pypy/_cffi_ssl/tools/make_ssl_data.py # this is a Py3 script
  |	rmdir $(PREFIX)/lib_pypy/_cffi_ssl/tools
  |	cd $(PREFIX) && find lib_pypy lib-python/%(version)s -name '*.py' |bin/pypy -Bm py_compile -
# the entry "-Wl,-rpath=${file:location}/lib" below is needed by python-magic,
# which would otherwise load the system libmagic.so with ctypes
environment =
  C_INCLUDE_PATH=${bzip2:location}/include:${gdbm:location}/include:${libexpat:location}/include:${ncurses:location}/include:${ncurses:location}/include:${openssl:location}/include:${sqlite3:location}/include:${zlib:location}/include
  LDFLAGS=-L${bzip2:location}/lib -L${gdbm:location}/lib -L${libexpat:location}/lib -L${libffi:location}/lib -L${ncurses:location}/lib -L${openssl:location}/lib -L${sqlite3:location}/lib -L${zlib:location}/lib -Wl,-rpath=${bzip2:location}/lib -Wl,-rpath=${file:location}/lib -Wl,-rpath=${gdbm:location}/lib -Wl,-rpath=${libexpat:location}/lib -Wl,-rpath=${libffi:location}/lib -Wl,-rpath=${ncurses:location}/lib -Wl,-rpath=${openssl:location}/lib -Wl,-rpath=${sqlite3:location}/lib -Wl,-rpath=${zlib:location}/lib
  PATH=${pkgconfig:location}/bin:%(PATH)s
  PKG_CONFIG_PATH=${libffi:location}/lib/pkgconfig
