Skip to content

Commit

Permalink
Fix ada.o object build
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshilliard committed Aug 29, 2024
1 parent 1aa99ef commit 5cdee76
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 18 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ concurrency:
jobs:
build_wheels:
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest", "macos-latest", "windows-latest"]

Expand All @@ -35,10 +36,10 @@ jobs:
- run: make requirements
- name: Set up QEMU # Needed to build aarch64 wheels
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3
with:
platforms: all
- uses: pypa/cibuildwheel@v2.17.0
- uses: pypa/cibuildwheel@v2.20.0
- uses: actions/upload-artifact@v4
with:
path: wheelhouse/*.whl
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Install dependencies
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ concurrency:
jobs:
build_test:
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest", "macos-latest"]

Expand All @@ -28,7 +29,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.8
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Install dependencies
Expand Down
5 changes: 5 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include ada_url/*.c
include ada_url/*.cpp
include ada_url/*.h
exclude ada_url/*.o
exclude ada_url/_ada_wrapper.*
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ clean:
$(RM) ada_url/_ada_wrapper.abi3.so
$(RM) ada_url/ada.o

.PHONY: c_lib
c_lib:
$(CXX) -c "ada_url/ada.cpp" -fPIC -std="c++17" -O2 -o "ada_url/ada.o" $(ARCHFLAGS)

.PHONY: package
package: c_lib
package:
python -m build --no-isolation
python ./update_sdist.py
twine check dist/*
15 changes: 13 additions & 2 deletions ada_url/ada_build.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
from cffi import FFI
from os.path import dirname, join
from setuptools.extension import Extension
from sys import platform

file_dir = dirname(__file__)

compile_args = ['/std:c++17'] if platform == 'win32' else ['-std=c++17']

ada_obj = Extension(
'ada',
language="c++",
sources=['ada_url/ada.cpp'],
include_dirs=[file_dir],
extra_compile_args=compile_args,
)

libraries = ['stdc++'] if platform == 'linux' else []

ffi_builder = FFI()
ffi_builder.set_source(
'ada_url._ada_wrapper',
'# include "ada_c.h"',
include_dirs=[file_dir],
libraries=libraries,
extra_objects=[join(file_dir, 'ada.o')],
include_dirs=[file_dir],
extra_objects=[ada_obj],
)

cdef_lines = []
Expand Down
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ After that, you're ready to build the package:
.. code-block:: sh
python -m pip install -r requirements/development.txt
c++ -c "ada_url/ada.cpp" -fPIC -std="c++17" -O2 -o "ada_url/ada.o"
python -m build --no-isolation
This will create a `.whl` file in the `dist` directory. You can install it in other
Expand Down
13 changes: 9 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
[build-system]
requires = ["cffi", "setuptools", "urllib3", "wheel"]
requires = [
"cffi",
"setuptools; sys_platform!='win32'",
# https://github.com/python-cffi/cffi/issues/117
"setuptools!=74.0.0; sys_platform=='win32'",
"urllib3",
"wheel"
]
build-backend = "setuptools.build_meta"

[project]
Expand Down Expand Up @@ -73,13 +80,11 @@ build = [

[tool.cibuildwheel.linux]
archs = ["x86_64", "aarch64"]
before-all = "make c_lib"

[tool.cibuildwheel.macos]
archs = ["x86_64", "universal2", "arm64"]
environment = { MACOSX_DEPLOYMENT_TARGET="10.15" }
before-build = "make clean && make c_lib"
before-build = "make clean"

[tool.cibuildwheel.windows]
archs = ["AMD64"]
before-build = '"C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat" && cl "ada_url\\ada.cpp" /c /nologo /Fo"ada_url\\ada.o" /O2 /GL /MD /W3 /EHsc /std:c++17'
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
from setuptools import setup
from setuptools.command.build_ext import build_ext as _build_ext
from setuptools.extension import Extension


class build_ext(_build_ext):
def build_extension(self, ext):
for i, extra in enumerate(ext.extra_objects):
if isinstance(extra, Extension):
sources = sorted(extra.sources)
extra_args = extra.extra_compile_args or []
macros = extra.define_macros[:]
for undef in extra.undef_macros:
macros.append((undef,))
objects = self.compiler.compile(
sources,
output_dir=self.build_temp,
macros=macros,
include_dirs=extra.include_dirs,
debug=self.debug,
extra_postargs=extra_args,
depends=extra.depends,
)
ext.extra_objects[i] = objects[0]
return super().build_extension(ext)

setup(
cmdclass={'build_ext': build_ext},
cffi_modules=[
'./ada_url/ada_build.py:ffi_builder',
],
Expand Down

0 comments on commit 5cdee76

Please sign in to comment.