Skip to content

Commit 6eeda71

Browse files
committed
Split shared Python library to libpython
Fixes #843 Signed-off-by: Michał Górny <mgorny@quansight.com>
1 parent 622caf6 commit 6eeda71

6 files changed

Lines changed: 147 additions & 63 deletions

File tree

recipe/build_base.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,6 @@ if [[ ${_OPTIMIZED} == yes ]]; then
385385
_FLAGS_REPLACE+=("")
386386
done
387387
fi
388-
# Install the shared library (for people who embed Python only, e.g. GDB).
389-
# Linking module extensions to this on Linux is redundant (but harmless).
390-
# Linking module extensions to this on Darwin is harmful (multiply defined symbols).
391-
shopt -s extglob
392-
cp -pf ${_buildd_shared}/libpython*${SHLIB_EXT}!(.lto) ${PREFIX}/lib/
393-
shopt -u extglob
394-
if [[ ${target_platform} =~ .*linux.* ]]; then
395-
ln -sf ${PREFIX}/lib/libpython${VERABI}${SHLIB_EXT}.1.0 ${PREFIX}/lib/libpython${VERABI}${SHLIB_EXT}
396-
fi
397388

398389
# Use sysconfigdata and build-details.json from the shared build, as we want packages to prefer
399390
# linking against the shared library. Issue #565.
@@ -520,7 +511,6 @@ rm ${PREFIX}/lib/libpython${VERABI}.a
520511
if [[ ${PY_INTERP_DEBUG} == yes ]]; then
521512
rm ${PREFIX}/bin/python${VER}
522513
ln -s ${PREFIX}/bin/python${VERABI} ${PREFIX}/bin/python${VER}
523-
ln -s ${PREFIX}/lib/libpython${VERABI}${SHLIB_EXT} ${PREFIX}/lib/libpython${VERABI_NO_DBG}${SHLIB_EXT}
524514
ln -s ${PREFIX}/include/python${VERABI} ${PREFIX}/include/python${VER}
525515
fi
526516

recipe/build_shared.bat

Whitespace-only changes.

recipe/build_shared.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
_buildd_shared=build-shared
5+
if [[ ${PY_INTERP_DEBUG} == yes ]]; then
6+
DBG=d
7+
else
8+
DBG=
9+
fi
10+
if [[ ${PY_FREETHREADING} == yes ]]; then
11+
# This Python will not be usable with non-free threading Python modules.
12+
THREAD=t
13+
else
14+
THREAD=
15+
fi
16+
17+
VER=${PKG_VERSION%.*}
18+
ABIFLAGS=${DBG}${THREAD}
19+
VERABI=${VER}${THREAD}${DBG}
20+
VERABI_NO_DBG=${VER}${THREAD}
21+
22+
# Install the shared library (for people who embed Python only, e.g. GDB).
23+
# Linking module extensions to this on Linux is redundant (but harmless).
24+
# Linking module extensions to this on Darwin is harmful (multiply defined symbols).
25+
shopt -s extglob
26+
cp -pf ${_buildd_shared}/libpython*${SHLIB_EXT}!(.lto) ${PREFIX}/lib/
27+
shopt -u extglob
28+
if [[ ${target_platform} =~ .*linux.* ]]; then
29+
ln -sf ${PREFIX}/lib/libpython${VERABI}${SHLIB_EXT}.1.0 ${PREFIX}/lib/libpython${VERABI}${SHLIB_EXT}
30+
fi
31+
if [[ ${PY_INTERP_DEBUG} == yes ]]; then
32+
ln -s ${PREFIX}/lib/libpython${VERABI}${SHLIB_EXT} ${PREFIX}/lib/libpython${VERABI_NO_DBG}${SHLIB_EXT}
33+
fi

recipe/meta.yaml

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ outputs:
221221
files:
222222
- tests/cmake/*
223223
- tests/cython/*
224-
- tests/prefix-replacement/*
225224
- run_test.py
226225
commands:
227226
- echo on # [win]
@@ -247,8 +246,11 @@ outputs:
247246
- _CONDA_PYTHON_SYSCONFIGDATA_NAME=_sysconfigdata_x86_64_conda_linux_gnu python -c "import sysconfig; print(sysconfig.get_config_var('CC'))" # [linux64]
248247
# check for unreplaced @ symbols in sysconfig files, excluding e.g. '"$@".lto'
249248
- for f in ${CONDA_PREFIX}/lib/python*/_sysconfig*.py; do echo "Checking $f:"; if [[ `rg @[^@]*@ $f` ]]; then echo "FAILED ON $f"; cat $f; exit 1; fi; done # [linux64 or osx]
250-
- test ! -f ${PREFIX}/lib/libpython${PKG_VERSION%.*}.a # [unix]
251-
- test ! -f ${PREFIX}/lib/libpython${PKG_VERSION%.*}.nolto.a # [unix]
249+
- test ! -f ${PREFIX}/lib/libpython${PKG_VERSION%.*}.so # [unix]
250+
- test ! -f ${PREFIX}/lib/libpython${PKG_VERSION%.*}.a # [unix]
251+
- test ! -f ${PREFIX}/lib/libpython${PKG_VERSION%.*}.so # [linux]
252+
- test ! -f ${PREFIX}/lib/libpython${PKG_VERSION%.*}.dylib # [osx]
253+
- test ! -f ${PREFIX}/lib/libpython${PKG_VERSION%.*}.nolto.a # [unix]
252254
{% if freethreading == "yes" %}
253255
- if not exist %PREFIX%\\libs\\python3t.lib exit 1 # [win]
254256
- if not exist %PREFIX%\\libs\\python{{ ver2nd }}t.lib exit 1 # [win]
@@ -265,14 +267,8 @@ outputs:
265267
- if not exist %PREFIX%\\Scripts\\pydoc.exe exit 1 # [win]
266268
- if not exist %PREFIX%\\include\\pyconfig.h exit 1 # [win]
267269
- pushd tests
268-
- pushd prefix-replacement # [unix]
269-
- bash build-and-test.sh # [unix]
270-
- popd # [unix]
271270
- pushd cmake
272-
{% if freethreading == "no" %}
273-
# TODO: use the new FindPython3 which supports freethreading
274-
- cmake -GNinja -DPY_VER={{ version }} --debug-find --trace --debug-output --debug-trycompile .
275-
{% endif %}
271+
- cmake -GNinja -DPY_VER={{ version }} -DEXTRA_COMPONENTS="Development.Module" --debug-find --trace --debug-output --debug-trycompile .
276272
- popd
277273
- popd
278274
- python run_test.py
@@ -283,6 +279,61 @@ outputs:
283279
# Test for wide character supported via ncursesw
284280
- TERM=xterm >/dev/null python -c "import curses; scr = curses.initscr(); curses.unget_wch('x'); assert 'x' == scr.get_wch()" # [unix]
285281

282+
- name: libpython
283+
script: build_shared.sh # [unix]
284+
script: build_shared.bat # [win]
285+
build:
286+
number: {{ build_number }}
287+
activate_in_script: true
288+
ignore_run_exports:
289+
- python_abi
290+
script_env:
291+
- PY_INTERP_DEBUG={{ py_interp_debug }}
292+
- PY_FREETHREADING={{ freethreading }}
293+
string: h{{ PKG_HASH }}_{{ PKG_BUILDNUM }}{{ debug }}_{{ abi_tag }}
294+
requirements:
295+
build:
296+
- {{ compiler('c') }}
297+
- {{ stdlib('c') }}
298+
- {{ compiler('cxx') }}
299+
host:
300+
- {{ pin_subpackage('python', exact=True) }}
301+
run:
302+
- {{ pin_subpackage('python', exact=True) }}
303+
test:
304+
files:
305+
- tests/cmake/*
306+
- tests/prefix-replacement/*
307+
requires:
308+
- {{ stdlib('c') }}
309+
- {{ compiler('c') }}
310+
# cmake expects a C++ compiler for some reason
311+
- {{ compiler('cxx') }}
312+
- ripgrep
313+
- cmake
314+
- ninja
315+
commands:
316+
- VER=${PKG_VERSION%.*} # [not win]
317+
- VERABI=${VER} # [not win]
318+
{% if freethreading == "yes" %}
319+
- VERABI=${VERABI}t # [not win]
320+
{% endif %}
321+
- VERABI_NO_DBG=${VERABI} # [not win]
322+
{% if py_interp_debug == "yes" %}
323+
- VERABI=${VERABI}d # [not win]
324+
{% endif %}
325+
- test -f ${PREFIX}/lib/libpython${VERABI}.so # [linux]
326+
- test -f ${PREFIX}/lib/libpython3.so # [linux and build_type == "release"]
327+
- test -f ${PREFIX}/lib/libpython${VERABI}.dylib # [osx]
328+
- pushd tests
329+
- pushd prefix-replacement # [unix]
330+
- bash build-and-test.sh # [unix]
331+
- popd # [unix]
332+
- pushd cmake
333+
- cmake -GNinja -DPY_VER={{ version }} -DEXTRA_COMPONENTS="Development.Embed" --debug-find --trace --debug-output --debug-trycompile .
334+
- popd
335+
- popd
336+
286337
- name: libpython-static
287338
script: build_static.sh # [unix]
288339
script: build_static.bat # [win]
@@ -306,11 +357,16 @@ outputs:
306357
- {{ pin_subpackage('python', exact=True) }}
307358
test:
308359
files:
360+
- tests/cmake/*
309361
- tests/prefix-replacement/*
310362
requires:
311363
- {{ stdlib('c') }}
312364
- {{ compiler('c') }}
365+
# cmake expects a C++ compiler for some reason
366+
- {{ compiler('cxx') }}
313367
- ripgrep
368+
- cmake
369+
- ninja
314370
commands:
315371
- VER=${PKG_VERSION%.*} # [not win]
316372
- VERABI=${VER} # [not win]
@@ -324,9 +380,14 @@ outputs:
324380
- test -f ${PREFIX}/lib/libpython${VERABI}.a # [unix]
325381
- test -f ${PREFIX}/lib/libpython${VERABI_NO_DBG}.nolto.a # [unix]
326382
- test -f ${PREFIX}/lib/python${VERABI_NO_DBG}/config-${VERABI}-darwin/libpython${VERABI}.a # [osx]
327-
- pushd tests/prefix-replacement # [unix]
328-
- bash build-and-test.sh # [unix]
329-
- popd # [unix]
383+
- pushd tests
384+
- pushd prefix-replacement # [unix]
385+
- bash build-and-test.sh # [unix]
386+
- popd # [unix]
387+
- pushd cmake
388+
- cmake -GNinja -DPY_VER={{ version }} -DEXTRA_COMPONENTS="Development.Embed" --debug-find --trace --debug-output --debug-trycompile .
389+
- popd
390+
- popd
330391

331392
- name: cpython
332393
build:

recipe/tests/cmake/CMakeLists.txt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
# https://martinopilia.com/posts/2018/09/15/building-python-extension.html
2-
# FindPythonInterp and FindPythonLibs are deprecated since cmake 3.12
3-
# These can be replaced by find_package(Python ${PY_VER} REQUIRED)
4-
# But these are still used by other packages, so we keep them.
1+
# https://scikit-build-core.readthedocs.io/en/latest/guide/cmakelists.html#finding-python
52

63
cmake_minimum_required(VERSION 3.10)
74
enable_language(C)
85
project(mymath)
96

10-
option(PY_VER, "Python version to use")
7+
option(PY_VER "Python version to use")
8+
option(EXTRA_COMPONENTS "Additional Python components to check for")
119

12-
find_package(PythonInterp ${PY_VER} REQUIRED)
13-
# PATHS $ENV{CONDA_PREFIX}
14-
15-
# This goes after, since it uses PythonInterp as hint
16-
find_package(PythonLibs ${PY_VER} REQUIRED)
10+
find_package(Python ${PY_VER} COMPONENTS Interpreter ${EXTRA_COMPONENTS} REQUIRED)

recipe/tests/prefix-replacement/build-and-test.sh

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,47 @@
22

33
set -ex
44

5-
# see bpo44182 for why -L${CONDA_PREFIX}/lib is added
6-
${CC} a.c $(python3-config --cflags) \
7-
$(python3-config --embed --ldflags) \
8-
-L${CONDA_PREFIX}/lib -Wl,-rpath,${CONDA_PREFIX}/lib \
9-
-o ${CONDA_PREFIX}/bin/embedded-python-shared
5+
case ${PKG_NAME} in
6+
libpython)
7+
# see bpo44182 for why -L${CONDA_PREFIX}/lib is added
8+
${CC} a.c $(python3-config --cflags) \
9+
$(python3-config --embed --ldflags) \
10+
-L${CONDA_PREFIX}/lib -Wl,-rpath,${CONDA_PREFIX}/lib \
11+
-o ${CONDA_PREFIX}/bin/embedded-python-shared
1012

11-
if [[ "$target_platform" == linux-* ]]; then
12-
if ! ${READELF} -d ${CONDA_PREFIX}/bin/embedded-python-shared | rg libpython; then
13-
echo "ERROR :: Embedded python linked to static python library. We tried to force it to use the shared library."
14-
fi
15-
elif [[ "$target_platform" == osx-* ]]; then
16-
if ! ${OTOOL} -l ${CONDA_PREFIX}/bin/embedded-python-shared | rg libpython; then
17-
echo "ERROR :: Embedded python linked to static python library. We tried to force it to use the shared library."
18-
fi
19-
fi
20-
${CONDA_PREFIX}/bin/embedded-python-shared
21-
22-
if [[ "$PKG_NAME" == "libpython-static" ]]; then
23-
# remove the shared library to force static linking
24-
rm -f ${CONDA_PREFIX}/lib/libpython*.{so,dylib}
13+
if [[ "$target_platform" == linux-* ]]; then
14+
if ! ${READELF} -d ${CONDA_PREFIX}/bin/embedded-python-shared | rg libpython; then
15+
echo "ERROR :: Embedded python linked to static python library. We tried to force it to use the shared library."
16+
fi
17+
elif [[ "$target_platform" == osx-* ]]; then
18+
if ! ${OTOOL} -l ${CONDA_PREFIX}/bin/embedded-python-shared | rg libpython; then
19+
echo "ERROR :: Embedded python linked to static python library. We tried to force it to use the shared library."
20+
fi
21+
fi
22+
${CONDA_PREFIX}/bin/embedded-python-shared
23+
;;
2524

26-
${CC} a.c $(python3-config --cflags) \
25+
libpython-static)
26+
${CC} a.c $(python3-config --cflags) \
2727
$(python3-config --embed --ldflags) \
2828
-L${CONDA_PREFIX}/lib -Wl,-rpath,${CONDA_PREFIX}/lib \
2929
-o ${CONDA_PREFIX}/bin/embedded-python-static
30-
if [[ "$target_platform" == linux-* ]]; then
31-
if ${READELF} -d ${CONDA_PREFIX}/bin/embedded-python-static | rg libpython; then
32-
echo "ERROR :: Embedded python linked to shared python library. It is expected to link to the static library."
30+
if [[ "$target_platform" == linux-* ]]; then
31+
if ${READELF} -d ${CONDA_PREFIX}/bin/embedded-python-static | rg libpython; then
32+
echo "ERROR :: Embedded python linked to shared python library. It is expected to link to the static library."
33+
fi
34+
elif [[ "$target_platform" == osx-* ]]; then
35+
if ${OTOOL} -l ${CONDA_PREFIX}/bin/embedded-python-static | rg libpython; then
36+
echo "ERROR :: Embedded python linked to shared python library. It is expected to link to the static library."
37+
fi
3338
fi
34-
elif [[ "$target_platform" == osx-* ]]; then
35-
if ${OTOOL} -l ${CONDA_PREFIX}/bin/embedded-python-static | rg libpython; then
36-
echo "ERROR :: Embedded python linked to shared python library. It is expected to link to the static library."
37-
fi
38-
fi
39-
${CONDA_PREFIX}/bin/embedded-python-static
40-
fi
39+
${CONDA_PREFIX}/bin/embedded-python-static
40+
;;
41+
42+
*)
43+
# invalid package
44+
exit 1
45+
;;
46+
esac
4147

4248
set +x

0 commit comments

Comments
 (0)