Skip to content

Commit 0a8e572

Browse files
authored
bpo-36721: Add --embed option to python-config (GH-13500)
To embed Python into an application, a new --embed option must be passed to "python3-config --libs --embed" to get "-lpython3.8" (link the application to libpython). To support both 3.8 and older, try "python3-config --libs --embed" first and fallback to "python3-config --libs" (without --embed) if the previous command fails. Add a pkg-config "python-3.8-embed" module to embed Python into an application: "pkg-config python-3.8-embed --libs" includes "-lpython3.8". To support both 3.8 and older, try "pkg-config python-X.Y-embed --libs" first and fallback to "pkg-config python-X.Y --libs" (without --embed) if the previous command fails (replace "X.Y" with the Python version). On the other hand, "pkg-config python3.8 --libs" no longer contains "-lpython3.8". C extensions must not be linked to libpython (except on Android, case handled by the script); this change is backward incompatible on purpose. "make install" now also installs "python-3.8-embed.pc".
1 parent ef5bb25 commit 0a8e572

File tree

10 files changed

+78
-9
lines changed

10 files changed

+78
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Lib/test/data/*
3636
Makefile
3737
Makefile.pre
3838
Misc/python.pc
39+
Misc/python-embed.pc
3940
Misc/python-config.sh
4041
Modules/Setup
4142
Modules/Setup.config

Doc/whatsnew/3.8.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ extensions compiled in release mode and for C extensions compiled with the
149149
stable ABI.
150150
(Contributed by Victor Stinner in :issue:`36722`.)
151151

152+
To embed Python into an application, a new ``--embed`` option must be passed to
153+
``python3-config --libs --embed`` to get ``-lpython3.8`` (link the application
154+
to libpython). To support both 3.8 and older, try ``python3-config --libs
155+
--embed`` first and fallback to ``python3-config --libs`` (without ``--embed``)
156+
if the previous command fails.
157+
158+
Add a pkg-config ``python-3.8-embed`` module to embed Python into an
159+
application: ``pkg-config python-3.8-embed --libs`` includes ``-lpython3.8``.
160+
To support both 3.8 and older, try ``pkg-config python-X.Y-embed --libs`` first
161+
and fallback to ``pkg-config python-X.Y --libs`` (without ``--embed``) if the
162+
previous command fails (replace ``X.Y`` with the Python version).
163+
164+
On the other hand, ``pkg-config python3.8 --libs`` no longer contains
165+
``-lpython3.8``. C extensions must not be linked to libpython (except on
166+
Android, case handled by the script); this change is backward incompatible on
167+
purpose.
168+
(Contributed by Victor Stinner in :issue:`36721`.)
169+
152170
f-strings now support = for quick and easy debugging
153171
-----------------------------------------------------
154172

Makefile.pre.in

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,11 +1258,15 @@ bininstall: altbininstall
12581258
(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(LDVERSION)-config python$(VERSION)-config); \
12591259
rm -f $(DESTDIR)$(LIBPC)/python-$(LDVERSION).pc; \
12601260
(cd $(DESTDIR)$(LIBPC); $(LN) -s python-$(VERSION).pc python-$(LDVERSION).pc); \
1261+
rm -f $(DESTDIR)$(LIBPC)/python-embed-$(LDVERSION).pc; \
1262+
(cd $(DESTDIR)$(LIBPC); $(LN) -s python-embed-$(VERSION).pc python-embed-$(LDVERSION).pc); \
12611263
fi
12621264
-rm -f $(DESTDIR)$(BINDIR)/python3-config
12631265
(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)-config python3-config)
12641266
-rm -f $(DESTDIR)$(LIBPC)/python3.pc
12651267
(cd $(DESTDIR)$(LIBPC); $(LN) -s python-$(VERSION).pc python3.pc)
1268+
-rm -f $(DESTDIR)$(LIBPC)/python3-embed.pc
1269+
(cd $(DESTDIR)$(LIBPC); $(LN) -s python-$(VERSION)-embed.pc python3-embed.pc)
12661270
-rm -f $(DESTDIR)$(BINDIR)/idle3
12671271
(cd $(DESTDIR)$(BINDIR); $(LN) -s idle$(VERSION) idle3)
12681272
-rm -f $(DESTDIR)$(BINDIR)/pydoc3
@@ -1552,6 +1556,7 @@ libainstall: @DEF_MAKE_RULE@ python-config
15521556
$(INSTALL_DATA) $(srcdir)/Modules/Setup $(DESTDIR)$(LIBPL)/Setup
15531557
$(INSTALL_DATA) Modules/Setup.local $(DESTDIR)$(LIBPL)/Setup.local
15541558
$(INSTALL_DATA) Misc/python.pc $(DESTDIR)$(LIBPC)/python-$(VERSION).pc
1559+
$(INSTALL_DATA) Misc/python-embed.pc $(DESTDIR)$(LIBPC)/python-$(VERSION)-embed.pc
15551560
$(INSTALL_SCRIPT) $(srcdir)/Modules/makesetup $(DESTDIR)$(LIBPL)/makesetup
15561561
$(INSTALL_SCRIPT) $(srcdir)/install-sh $(DESTDIR)$(LIBPL)/install-sh
15571562
$(INSTALL_SCRIPT) python-config.py $(DESTDIR)$(LIBPL)/python-config.py
@@ -1766,7 +1771,7 @@ distclean: clobber
17661771
done
17671772
-rm -f core Makefile Makefile.pre config.status Modules/Setup.local \
17681773
Modules/ld_so_aix Modules/python.exp Misc/python.pc \
1769-
Misc/python-config.sh
1774+
Misc/python-embed.pc Misc/python-config.sh
17701775
-rm -f python*-gdb.py
17711776
# Issue #28258: set LC_ALL to avoid issues with Estonian locale.
17721777
# Expansion is performed here by shell (spawned by make) itself before
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
To embed Python into an application, a new ``--embed`` option must be passed to
2+
``python3-config --libs --embed`` to get ``-lpython3.8`` (link the application
3+
to libpython). To support both 3.8 and older, try ``python3-config --libs
4+
--embed`` first and fallback to ``python3-config --libs`` (without ``--embed``)
5+
if the previous command fails.
6+
7+
Add a pkg-config ``python-3.8-embed`` module to embed Python into an
8+
application: ``pkg-config python-3.8-embed --libs`` includes ``-lpython3.8``.
9+
To support both 3.8 and older, try ``pkg-config python-X.Y-embed --libs`` first
10+
and fallback to ``pkg-config python-X.Y --libs`` (without ``--embed``) if the
11+
previous command fails (replace ``X.Y`` with the Python version).
12+
13+
On the other hand, ``pkg-config python3.8 --libs`` no longer contains
14+
``-lpython3.8``. C extensions must not be linked to libpython (except on
15+
Android, case handled by the script); this change is backward incompatible on
16+
purpose.

Misc/python-config.in

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import sys
99
import sysconfig
1010

1111
valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
12-
'ldflags', 'extension-suffix', 'help', 'abiflags', 'configdir']
12+
'ldflags', 'extension-suffix', 'help', 'abiflags', 'configdir',
13+
'embed']
1314

1415
def exit_with_usage(code=1):
1516
print("Usage: {0} [{1}]".format(
@@ -47,8 +48,13 @@ for opt in opt_flags:
4748
print(' '.join(flags))
4849

4950
elif opt in ('--libs', '--ldflags'):
50-
libpython = getvar('LIBPYTHON')
51-
libs = [libpython] if libpython else []
51+
libs = []
52+
if '--embed' in opt_flags:
53+
libs.append('-lpython' + pyver + sys.abiflags)
54+
else:
55+
libpython = getvar('LIBPYTHON')
56+
if libpython:
57+
libs.append(libpython)
5258
libs.extend(getvar('LIBS').split() + getvar('SYSLIBS').split())
5359

5460
# add the prefix/lib/pythonX.Y/config dir, but only if there is no

Misc/python-config.sh.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ LIBC="@LIBC@"
4242
SYSLIBS="$LIBM $LIBC"
4343
ABIFLAGS="@ABIFLAGS@"
4444
LIBS="@LIBPYTHON@ @LIBS@ $SYSLIBS"
45+
LIBS_EMBED="-lpython${VERSION}${ABIFLAGS} @LIBS@ $SYSLIBS"
4546
BASECFLAGS="@BASECFLAGS@"
4647
LDLIBRARY="@LDLIBRARY@"
4748
OPT="@OPT@"
@@ -53,6 +54,7 @@ SO="@EXT_SUFFIX@"
5354
PYTHONFRAMEWORK="@PYTHONFRAMEWORK@"
5455
INCDIR="-I$includedir/python${VERSION}${ABIFLAGS}"
5556
PLATINCDIR="-I$includedir/python${VERSION}${ABIFLAGS}"
57+
PY_EMBED=0
5658

5759
# Scan for --help or unknown argument.
5860
for ARG in $*
@@ -61,6 +63,9 @@ do
6163
--help)
6264
exit_with_usage 0
6365
;;
66+
--embed)
67+
PY_EMBED=1
68+
;;
6469
--prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--abiflags|--configdir)
6570
;;
6671
*)
@@ -69,6 +74,10 @@ do
6974
esac
7075
done
7176

77+
if [ $PY_EMBED = 1 ] ; then
78+
LIBS="$LIBS_EMBED"
79+
fi
80+
7281
for ARG in "$@"
7382
do
7483
case "$ARG" in

Misc/python-embed.pc.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# See: man pkg-config
2+
prefix=@prefix@
3+
exec_prefix=@exec_prefix@
4+
libdir=@libdir@
5+
includedir=@includedir@
6+
7+
Name: Python
8+
Description: Embed Python into an application
9+
Requires:
10+
Version: @VERSION@
11+
Libs.private: @LIBS@
12+
Libs: -L${libdir} -lpython@VERSION@@ABIFLAGS@
13+
Cflags: -I${includedir}/python@VERSION@@ABIFLAGS@

Misc/python.pc.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ libdir=@libdir@
55
includedir=@includedir@
66

77
Name: Python
8-
Description: Python library
9-
Requires:
8+
Description: Build a C extension for Python
9+
Requires:
1010
Version: @VERSION@
1111
Libs.private: @LIBS@
12-
Libs: -L${libdir} -lpython@VERSION@@ABIFLAGS@
12+
Libs:
1313
Cflags: -I${includedir}/python@VERSION@@ABIFLAGS@

configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17315,7 +17315,7 @@ fi
1731517315

1731617316

1731717317
# generate output files
17318-
ac_config_files="$ac_config_files Makefile.pre Misc/python.pc Misc/python-config.sh"
17318+
ac_config_files="$ac_config_files Makefile.pre Misc/python.pc Misc/python-embed.pc Misc/python-config.sh"
1731917319

1732017320
ac_config_files="$ac_config_files Modules/ld_so_aix"
1732117321

@@ -18018,6 +18018,7 @@ do
1801818018
"Mac/Resources/app/Info.plist") CONFIG_FILES="$CONFIG_FILES Mac/Resources/app/Info.plist" ;;
1801918019
"Makefile.pre") CONFIG_FILES="$CONFIG_FILES Makefile.pre" ;;
1802018020
"Misc/python.pc") CONFIG_FILES="$CONFIG_FILES Misc/python.pc" ;;
18021+
"Misc/python-embed.pc") CONFIG_FILES="$CONFIG_FILES Misc/python-embed.pc" ;;
1802118022
"Misc/python-config.sh") CONFIG_FILES="$CONFIG_FILES Misc/python-config.sh" ;;
1802218023
"Modules/ld_so_aix") CONFIG_FILES="$CONFIG_FILES Modules/ld_so_aix" ;;
1802318024

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5589,7 +5589,7 @@ AC_DEFINE(PY_SSL_DEFAULT_CIPHERS, 1)
55895589

55905590

55915591
# generate output files
5592-
AC_CONFIG_FILES(Makefile.pre Misc/python.pc Misc/python-config.sh)
5592+
AC_CONFIG_FILES(Makefile.pre Misc/python.pc Misc/python-embed.pc Misc/python-config.sh)
55935593
AC_CONFIG_FILES([Modules/ld_so_aix], [chmod +x Modules/ld_so_aix])
55945594
AC_OUTPUT
55955595

0 commit comments

Comments
 (0)