Skip to content

Commit

Permalink
fix: make link.exe find PyPy's python*.lib
Browse files Browse the repository at this point in the history
Point `LIB` environment variable to `libs` directory of extracted PyPy
on Windows, so that `link.exe` find it while building Python extensions:
https://learn.microsoft.com/en-us/cpp/build/reference/linking?view=msvc-170#link-environment-variables

Add a test for building Python extension.
  • Loading branch information
rominf committed Jul 9, 2024
1 parent a0d74c0 commit b89030b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 1 deletion.
41 changes: 41 additions & 0 deletions .github/workflows/e2e-extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: e2e-cache

on:
pull_request:
paths-ignore:
- '**.md'
push:
branches:
- main
- releases/*
paths-ignore:
- '**.md'
workflow_dispatch:

permissions:
contents: read
jobs:
python-extension:
name: Test building extensions (Python ${{ matrix.python-version}}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ['3.9', 'pypy-3.9-v7.x']
steps:
- uses: actions/checkout@v4
- name: Setup Python
uses: ./
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: pip install setuptools
- name: Build extension
run: pip install .
workdir: __tests__/data/extension/
- name: Check output
run: |
output=$(python3 -c "import helloworld; helloworld.say_hello('world')")
test "$output" == "Hello, world!"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Ignore build/ and .egg-info/ directories of Python extension
__tests__/data/extension/build/
__tests__/data/extension/helloworld.egg-info/

# Ignore node_modules, ncc is used to compile nodejs modules into a single file in the releases branch
node_modules/
__tests__/runner/*
Expand Down
27 changes: 27 additions & 0 deletions __tests__/data/extension/hello_world.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <Python.h>

static PyObject * say_hello(PyObject *self, PyObject *args) {
const char *name;
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;
PySys_WriteStdout("Hello, %s!", name);
Py_RETURN_NONE;
}

static PyMethodDef HelloWorldMethods[] = {
{"say_hello", say_hello, METH_VARARGS, "Say hello"},
{NULL, NULL, 0, NULL}
};

static struct PyModuleDef hello_world_module = {
PyModuleDef_HEAD_INIT,
"helloworld",
NULL,
-1,
HelloWorldMethods
};

PyMODINIT_FUNC PyInit_helloworld(void)
{
return PyModule_Create(&hello_world_module);
}
7 changes: 7 additions & 0 deletions __tests__/data/extension/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "helloworld"
version = "1.0"
10 changes: 10 additions & 0 deletions __tests__/data/extension/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from setuptools import Extension, setup

setup(
ext_modules=[
Extension(
name="helloworld",
sources=["hello_world.c"],
),
]
)
5 changes: 4 additions & 1 deletion dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90880,6 +90880,9 @@ function findPyPyVersion(versionSpec, architecture, updateEnvironment, checkLate
// https://cmake.org/cmake/help/latest/module/FindPython3.html#module:FindPython3
core.exportVariable('Python3_ROOT_DIR', installDir);
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
if (utils_1.IS_WINDOWS) {
core.exportVariable('LIB', pythonLocation + '/libs');
}
core.addPath(pythonLocation);
core.addPath(_binDir);
}
Expand Down Expand Up @@ -94123,4 +94126,4 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
;
;
4 changes: 4 additions & 0 deletions src/find-pypy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export async function findPyPyVersion(
// https://cmake.org/cmake/help/latest/module/FindPython3.html#module:FindPython3
core.exportVariable('Python3_ROOT_DIR', installDir);
core.exportVariable('PKG_CONFIG_PATH', pythonLocation + '/lib/pkgconfig');
// https://learn.microsoft.com/en-us/cpp/build/reference/linking?view=msvc-170#link-environment-variables
if (IS_WINDOWS) {
core.exportVariable('LIB', pythonLocation + '/libs');
}
core.addPath(pythonLocation);
core.addPath(_binDir);
}
Expand Down

0 comments on commit b89030b

Please sign in to comment.