Skip to content

Commit

Permalink
Add a minimal pyproject.toml (celiagg#114)
Browse files Browse the repository at this point in the history
This is enough to permit installation without needing numpy and cython in the environment via PEP-517 style isolated builds.  Text rendering in the builds is now controlled by an environment variable.

Fixes celiagg#105
  • Loading branch information
corranwebster authored Apr 14, 2023
1 parent e5bc04a commit 5e298c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: python -m pip install cython numpy
- name: Install library (w/ text rendering support)
- name: Install library and dependencies
run: python -m pip install -e .
if: matrix.os == 'ubuntu-latest'
- name: Install library (w/o text rendering support)
run: python -m pip install -e . --install-option --no-text-rendering
- name: Install library and dependencies (no text rendering)
run: python -m pip install -e .
env:
CELIAGG_NO_TEXT_RENDERING: 1
if: matrix.os != 'ubuntu-latest'
- name: Run tests
run: python -m unittest discover -v celiagg
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Building from source with the Freetype font library on macOS requires
the `pkg-config` tool which can be installed via Homebrew, MacPorts, or
other macOS package management systems.

To build without text support, set the ``CELIAGG_NO_TEXT_RENDERING``
environment variable while building, eg.
``CELIAGG_NO_TEXT_RENDERING=1 pip install celiagg``


Dependencies
------------

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["cython", "oldest-supported-numpy", "setuptools", "wheel"]
build-backend = "setuptools.build_meta"
65 changes: 36 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@
print("Cython does not appear to be installed. Will attempt to use "
"pre-made cpp file...")

# Disable text rendering with this option
if '--no-text-rendering' in sys.argv:
del sys.argv[sys.argv.index('--no-text-rendering')]
with_text_rendering = False
else:
with_text_rendering = True
with_pkgconfig = True
# Disable pkg-config use with this option
if '--no-pkg-config' in sys.argv:
del sys.argv[sys.argv.index('--no-pkg-config')]
with_pkgconfig = False


class PatchedSdist(_sdist):
""" Make sure the compiled Cython files are included
Expand All @@ -63,6 +51,30 @@ def run(self):
_sdist.run(self)


def has_text_rendering():
return not os.environ.get("CELIAGG_NO_TEXT_RENDERING", None)


def has_pkgconfig():
try:
subprocess.run(
['pkg-config', '--version'],
check=True,
capture_output=True,
)
return True
except subprocess.CalledProcessError:
print(
"Failed to execute pkg-config. Either disable building with "
"text rendering using CELIAGG_NO_TEXT_RENDERING environment "
"variable; install pkg-config; or supply appropriate "
"CFLAGS and LDFLAGS environment variables for FreeType2 and "
"Harfbuzz.\n\n",
file=sys.stderr,
)
return False


def run_pkgconfig(name, exit_on_fail=True):
""" Use pkg-config to locate a library installed on the system.
Expand Down Expand Up @@ -96,19 +108,15 @@ def collect_data(env=None):

if len(data) < 2:
msg = ("Failed to execute pkg-config {name}. If {name} is "
"installed in standard system locations, it may work to run "
"this script with --no-pkg-config. Otherwise, appropriate "
"CFLAGS and LDFLAGS environment variables must be set.\n\n"
"installed in standard system locations, this may still work. "
"Otherwise, appropriate CFLAGS and LDFLAGS environment "
"variables must be set.\n\n"
"If you wish to disable text rendering, you can re-run this "
"script with the --no-text-rendering flag.")
print(msg.format(name=name), file=sys.stderr)

# NOTE: Avoid exiting when pip is running an egg_info command. Without
# this, it's not possible to avoid freetype when installing from pip.
if exit_on_fail and 'egg_info' not in sys.argv:
exit(-1)
else:
return [], []
# couldn't find flags, so return empty lists
return [], []
return data['cflags'], data['ldflags']


Expand Down Expand Up @@ -168,15 +176,16 @@ def create_extension():
('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION'),
]

if with_text_rendering:
if has_text_rendering():
print("Text rendering enabled.", file=sys.stderr)
if platform.system() == 'Windows':
extra_link_args.extend(['Gdi32.lib', 'User32.lib'])
include_dirs.append('agg-svn/agg-2.4/font_win32_tt')
font_source = 'agg-svn/agg-2.4/font_win32_tt/agg_font_win32_tt.cpp'

# XXX: Figure out how to enable Harfbuzz!
else:
if with_pkgconfig:
if has_pkgconfig():
cflags, ldflags = run_pkgconfig('freetype2')
extra_compile_args.extend(cflags)
extra_link_args.extend(ldflags)
Expand All @@ -191,8 +200,11 @@ def create_extension():
define_macros.append(('_USE_FREETYPE', None))
include_dirs.append('agg-svn/agg-2.4/font_freetype')
font_source = 'agg-svn/agg-2.4/font_freetype/agg_font_freetype.cpp'

sources.append(font_source)
define_macros.append(('_ENABLE_TEXT_RENDERING', None))
else:
print("Text rendering disabled.", file=sys.stderr)

return Extension(
'celiagg._celiagg',
Expand All @@ -208,11 +220,6 @@ def create_extension():
with open('README.rst', 'r') as fp:
long_description = fp.read()

requires = ['numpy']
if sys.platform not in ('win32', 'cygwin'):
# Windows doesn't use FreeType.
requires.append('freetype')

cmdclass = {'sdist': PatchedSdist}
if cython_build_ext is not None:
cmdclass['build_ext'] = cython_build_ext
Expand Down Expand Up @@ -240,7 +247,7 @@ def create_extension():
'Operating System :: Unix',
'Operating System :: MacOS',
],
requires=requires,
install_requires=['numpy'],
cmdclass=cmdclass,
ext_modules=[create_extension()],
packages=['celiagg', 'celiagg.tests'],
Expand Down

0 comments on commit 5e298c8

Please sign in to comment.