Skip to content

Commit 2e84fc5

Browse files
Merge pull request #378 from oscarbenjamin/pr_msvc_ext
Use MSVC to build python-flint's extension modules
2 parents fe1f577 + a512a41 commit 2e84fc5

File tree

6 files changed

+53
-18
lines changed

6 files changed

+53
-18
lines changed

.github/workflows/buildwheel.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ jobs:
3131

3232
# We have to set this here rather than in the cibuildwheel config
3333
# This is probably something to do with \ vs / in paths...
34-
- run: echo "PKG_CONFIG_PATH=${{ github.workspace }}/.local/lib/pkgconfig" >> $env:GITHUB_ENV
34+
- run: |
35+
$pkgConfigPath = "${{ github.workspace }}/.local/lib/pkgconfig"
36+
$pkgConfigPath = $pkgConfigPath.Replace('\', '/')
37+
echo "PKG_CONFIG_PATH=$pkgConfigPath" >> $env:GITHUB_ENV
3538
if: ${{ startsWith( matrix.os , 'windows' ) }}
3639
3740
- name: Build wheels

bin/cibw_before_all_windows.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,36 @@ bin/build_dependencies_unix.sh \
1818
--use-gmp-github-mirror\
1919
--patch-C23\
2020
#
21+
22+
# Assumes the standard GitHub Actions Windows 2022 runner layout.
23+
PATH="$PATH:$(find "/c/Program Files/Microsoft Visual Studio/2022/" -name "Hostx86")/x64/"
24+
25+
if [ "${RUNNER_ARCH}" = "ARM64" ]
26+
then
27+
msvc_machine=arm64
28+
else
29+
msvc_machine=x64
30+
fi
31+
32+
mkdir -p .local/lib
33+
cd .local/bin
34+
for dll_file in libgmp-*.dll libmpfr-*.dll libflint*.dll
35+
do
36+
lib_name=$(basename -s .dll ${dll_file})
37+
exports_file=${lib_name}-exports.txt
38+
def_file=${lib_name}.def
39+
lib_file=${lib_name}.lib
40+
name=$(echo ${lib_name}|sed 's/^lib//;s/[-.][0-9].*$//')
41+
42+
dumpbin //exports ${dll_file} > ${exports_file}
43+
44+
echo LIBRARY ${lib_name} > ${def_file}
45+
echo EXPORTS >> ${def_file}
46+
awk 'NR>19 && $4 != "" {print $4 " @"$1}' ${exports_file} >> ${def_file}
47+
sed -i 's/$/\r/' ${def_file}
48+
49+
lib //def:${def_file} //out:${lib_file} //machine:${msvc_machine}
50+
rm ${exports_file} ${def_file} ${lib_name}.exp
51+
mv ${lib_file} ../lib/${name}.lib
52+
done
53+
cd ../..

doc/source/build.rst

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,23 +356,17 @@ The git repo for ``python-flint`` has a script `bin/cibw_before_all_windows.sh
356356
<https://github.com/flintlib/python-flint/blob/master/bin/cibw_before_all_windows.sh>`_
357357
that installs the dependencies under MSYS2 and builds ``GMP``, ``MPFR``,
358358
``FLINT``. This script is used for building the Windows binaries for PyPI. We
359-
use the ``MinGW64`` (``mingw-w64-x86_64``) toolchain for building on Windows
360-
rather than MSVC because it makes it possible to have a fat build of ``GMP``
359+
use the ``UCRT64`` (``mingw-w64-ucrt-x86_64``) toolchain under MSYS2 to build
360+
``GMP``, ``MPFR`` and ``FLINT`` because it makes it possible to have a fat
361+
build of ``GMP``
361362
(``--enable-fat``) which bundles micro-architecture specific optimisations for
362363
``x86_64`` in a redistributable shared library. This is important for
363364
performance on modern ``x86_64`` CPUs and is not possible if building ``GMP``
364-
with MSVC. Since we need to use ``MinGW64`` for building ``GMP`` it is simplest
365-
to use it for building ``MPFR``, ``FLINT`` and ``python-flint`` as well and
366-
means that the same Unix-style build scripts can be used for all platforms.
367-
368-
The ``python-flint`` project does not have much experience using MSVC. Possibly
369-
it would be better to build ``GMP`` using ``MinGW64`` and then build ``MPFR``,
370-
``FLINT`` and ``python-flint`` using MSVC. It is also possible that it would be
371-
better to build ``GMP``, ``MPFR``, ``FLINT`` using MinGW64 and then build
372-
``python-flint`` using MSVC. Someone with more experience with MSVC would need
373-
to help with this. We would welcome contributions that explain how to build
374-
``python-flint`` and its dependencies using MSVC and/or that improve the build
375-
process for distributed binaries on Windows.
365+
with MSVC. The Python extension modules themselves are then built with MSVC via
366+
``meson --vsenv`` while linking against the MSYS2-built ``GMP``, ``MPFR`` and
367+
``FLINT`` libraries through ``pkg-config``. This mixed-toolchain arrangement
368+
keeps the ``GMP`` fat build while using the standard Windows compiler for the
369+
extension modules.
376370

377371

378372
.. _non_standard_location:

meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ project(
33
'cython',
44
'c',
55
meson_version : '>=1.3.0',
6-
default_options: ['python.allow_limited_api=false'],
6+
default_options: [
7+
'python.allow_limited_api=false',
8+
'c_std=c11',
9+
],
710
)
811
#
912
# The minimum versions are because we know that it will not work with earlier

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ repair-wheel-command = [
156156
[tool.cibuildwheel.windows]
157157
before-all = "C:\\msys64\\usr\\bin\\bash bin/cibw_before_all_windows.sh"
158158
before-build = "pip install wheel delvewheel"
159+
config-settings = {setup-args = ["--vsenv"], build-dir = "build"}
159160
repair-wheel-command = [
160161
"""python bin/cibw_repair_wheel_licenses.py {wheel} \
161162
--license LGPL-3.0-or-later \

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
if sys.platform == 'win32':
2626
#
27-
# This is used in CI to build wheels with mingw64
27+
# This can be used for local Windows builds against a .local install
28+
# produced by the MSYS2 UCRT64 dependency build.
2829
#
2930
if os.getenv('PYTHON_FLINT_MINGW64'):
3031
includedir = os.path.join(os.path.dirname(__file__), '.local', 'include')
@@ -35,7 +36,7 @@
3536
default_lib_dirs += librarydirs
3637
# Add gcc to the PATH in GitHub Actions when this setup.py is called by
3738
# cibuildwheel.
38-
os.environ['PATH'] += r';C:\msys64\mingw64\bin'
39+
os.environ['PATH'] += r';C:\msys64\ucrt64\bin'
3940
libraries += ["mpfr", "gmp"]
4041
elif os.getenv('PYTHON_FLINT_MINGW64_TMP'):
4142
# This would be used to build under Windows against these libraries if

0 commit comments

Comments
 (0)