Skip to content

Commit

Permalink
Make sure setup.py sdist produces correct tarballs
Browse files Browse the repository at this point in the history
  • Loading branch information
elprans committed Nov 11, 2016
1 parent c31616c commit e7bedb8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
22 changes: 6 additions & 16 deletions Makefile
@@ -1,16 +1,16 @@
.PHONY: compile clean all distclean test debug sdist clean-libuv
.PHONY: release sdist-libuv docs
.PHONY: _default clean clean-libuv distclean compile debug docs test release


PYTHON ?= python


all: compile
_default: compile


clean:
rm -fr dist/ doc/_build/
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so build *.egg-info
rm -fr dist/ doc/_build/ *.egg-info
rm -fr build/lib.* build/temp.*
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so
rm -fr uvloop/handles/*.html uvloop/includes/*.html
find . -name '__pycache__' | xargs rm -rf

Expand All @@ -19,10 +19,6 @@ clean-libuv:
(cd vendor/libuv; git clean -dfX)


sdist-libuv: clean-libuv
/bin/sh vendor/libuv/autogen.sh


distclean: clean clean-libuv


Expand All @@ -47,11 +43,5 @@ test:
$(PYTHON) -m unittest discover -s tests


sdist: clean compile test sdist-libuv
$(PYTHON) setup.py sdist


# Don't change "clean" to "distclean"! Otherwise "clean-libuv" will
# only be called once, which will produce a broken sdist.
release: clean compile test sdist-libuv
release: distclean compile test
$(PYTHON) setup.py sdist bdist_wheel upload
77 changes: 56 additions & 21 deletions setup.py
@@ -1,5 +1,6 @@
import os
import re
import shutil
import subprocess
import sys
import unittest
Expand All @@ -17,12 +18,14 @@


from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.build_ext import build_ext as build_ext
from setuptools.command.sdist import sdist as sdist


VERSION = '0.6.5'
CFLAGS = ['-O2']
LIBUV_DIR = os.path.join(os.path.dirname(__file__), 'vendor', 'libuv')
LIBUV_BUILD_DIR = os.path.join(os.path.dirname(__file__), 'build', 'libuv')


def discover_tests():
Expand All @@ -31,7 +34,33 @@ def discover_tests():
return test_suite


class libuv_build_ext(build_ext):
def _libuv_build_env():
env = os.environ.copy()

cur_cflags = env.get('CFLAGS', '')
if not re.search('-O\d', cur_cflags):
cur_cflags += ' -O2'

env['CFLAGS'] = (cur_cflags + ' -fPIC ' + env.get('ARCHFLAGS', ''))

return env


def _libuv_autogen(env):
if not os.path.exists(os.path.join(LIBUV_DIR, 'configure')):
subprocess.run(
['/bin/sh', 'autogen.sh'], cwd=LIBUV_DIR, env=env, check=True)


class uvloop_sdist(sdist):
def run(self):
# Make sure sdist archive contains configure
# to avoid the dependency on autotools.
_libuv_autogen(_libuv_build_env())
super().run()


class uvloop_build_ext(build_ext):
user_options = build_ext.user_options + [
('cython-always', None,
'run cythonize() even if .c files are present'),
Expand Down Expand Up @@ -177,31 +206,34 @@ def _patch_cfile(self, cfile):
f.write(src)

def build_libuv(self):
env = os.environ.copy()

cur_cflags = env.get('CFLAGS', '')
if not re.search('-O\d', cur_cflags):
cur_cflags += ' -O2'
env = _libuv_build_env()

env['CFLAGS'] = (cur_cflags + ' -fPIC ' + env.get('ARCHFLAGS', ''))
# Make sure configure and friends are present in case
# we are building from a git checkout.
_libuv_autogen(env)

j_flag = '-j{}'.format(os.cpu_count() or 1)

if not os.path.exists(os.path.join(LIBUV_DIR, 'configure')):
subprocess.run(['/bin/sh', 'autogen.sh'], cwd=LIBUV_DIR, env=env,
check=True)
# Copy the libuv tree to build/ so that its build
# products don't pollute sdist accidentally.
if os.path.exists(LIBUV_BUILD_DIR):
shutil.rmtree(LIBUV_BUILD_DIR)
shutil.copytree(LIBUV_DIR, LIBUV_BUILD_DIR)

# Sometimes pip fails to preserve the timestamps correctly,
# in which case, make will try to run autotools again.
subprocess.run(['touch', 'configure.ac', 'aclocal.m4',
'configure', 'Makefile.am', 'Makefile.in'],
cwd=LIBUV_DIR, env=env, check=True)
subprocess.run(
['touch', 'configure.ac', 'aclocal.m4', 'configure',
'Makefile.am', 'Makefile.in'],
cwd=LIBUV_BUILD_DIR, env=env, check=True)

subprocess.run(['./configure'], cwd=LIBUV_DIR, env=env, check=True)
subprocess.run(
['./configure'],
cwd=LIBUV_BUILD_DIR, env=env, check=True)

j_flag = '-j{}'.format(os.cpu_count() or 1)
c_flag = "CFLAGS={}".format(env['CFLAGS'])
subprocess.run(['make', j_flag, c_flag],
cwd=LIBUV_DIR, env=env, check=True)
subprocess.run(
['make', j_flag, c_flag],
cwd=LIBUV_BUILD_DIR, env=env, check=True)

def build_extensions(self):
if self.use_system_libuv:
Expand All @@ -212,7 +244,7 @@ def build_extensions(self):
# Support macports on Mac OS X.
self.compiler.add_include_dir('/opt/local/include')
else:
libuv_lib = os.path.join(LIBUV_DIR, '.libs', 'libuv.a')
libuv_lib = os.path.join(LIBUV_BUILD_DIR, '.libs', 'libuv.a')
if not os.path.exists(libuv_lib):
self.build_libuv()
if not os.path.exists(libuv_lib):
Expand Down Expand Up @@ -241,7 +273,10 @@ def build_extensions(self):
platforms=['*nix'],
version=VERSION,
packages=['uvloop'],
cmdclass={'build_ext': libuv_build_ext},
cmdclass={
'sdist': uvloop_sdist,
'build_ext': uvloop_build_ext
},
ext_modules=[
Extension(
"uvloop.loop",
Expand Down

0 comments on commit e7bedb8

Please sign in to comment.