Skip to content

Commit bd88ccb

Browse files
authored
bpo-43774: Document the Python Build System (GH-25302)
* Elaborate PGO and LTO documentaton. * Document MAINCC, PURIFY and LINKCC variables.
1 parent f7be26a commit bd88ccb

File tree

1 file changed

+116
-7
lines changed

1 file changed

+116
-7
lines changed

Doc/using/configure.rst

Lines changed: 116 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ recommended for best performance.
146146
Enable Profile Guided Optimization (PGO) using :envvar:`PROFILE_TASK`
147147
(disabled by default).
148148

149+
The C compiler Clang requires ``llvm-profdata`` program for PGO. On
150+
macOS, GCC also requires it: GCC is just an alias to Clang on macOS.
151+
149152
Disable also semantic interposition in libpython if ``--enable-shared`` and
150153
GCC is used: add ``-fno-semantic-interposition`` to the compiler and linker
151154
flags.
@@ -168,6 +171,9 @@ recommended for best performance.
168171

169172
Enable Link Time Optimization (LTO) in any build (disabled by default).
170173

174+
The C compiler Clang requires ``llvm-ar`` for LTO, as well as an LTO-aware
175+
linker (``ld.gold`` or ``lld``).
176+
171177
.. versionadded:: 3.6
172178

173179
.. cmdoption:: --with-computed-gotos
@@ -469,19 +475,100 @@ See ``Mac/README.rst``.
469475
:option:`--enable-framework` is set (default: ``Python``).
470476

471477

478+
Python Build System
479+
===================
480+
481+
Main files of the build system
482+
------------------------------
483+
484+
* :file:`configure.ac` => :file:`configure`;
485+
* :file:`Makefile.pre.in` => :file:`Makefile` (created by :file:`configure`);
486+
* :file:`pyconfig.h` (created by :file:`configure`);
487+
* :file:`Modules/Setup`: C extensions built by the Makefile using
488+
:file:`Module/makesetup` shell script;
489+
* :file:`setup.py`: C extensions built using the :mod:`distutils` module.
490+
491+
Main build steps
492+
----------------
493+
494+
* C files (``.c``) are built as object files (``.o``).
495+
* A static ``libpython`` library (``.a``) is created from objects files.
496+
* ``python.o`` and the static ``libpython`` library are linked into the
497+
final ``python`` program.
498+
* C extensions are built by the Makefile (see :file:`Modules/Setup`)
499+
and ``python setup.py build``.
500+
501+
Main Makefile targets
502+
---------------------
503+
504+
* ``make``: Build Python with the standard library.
505+
* ``make platform:``: build the ``python`` program, but don't build the
506+
standard library extension modules.
507+
* ``make profile-opt``: build Python using Profile Guided Optimization (PGO).
508+
You can use the configure :option:`--enable-optimizations` option to make
509+
this the default target of the ``make`` command (``make all`` or just
510+
``make``).
511+
* ``make buildbottest``: Build Python and run the Python test suite, the same
512+
way than buildbots test Python. Set ``TESTTIMEOUT`` variable (in seconds)
513+
to change the test timeout (1200 by default: 20 minutes).
514+
* ``make install``: Build and install Python.
515+
* ``make regen-all``: Regenerate (almost) all generated files;
516+
``make regen-stdlib-module-names`` and ``autoconf`` must be run separately
517+
for the remaining generated files.
518+
* ``make clean``: Remove built files.
519+
* ``make distclean``: Same than ``make clean``, but remove also files created
520+
by the configure script.
521+
522+
C extensions
523+
------------
524+
525+
Some C extensions are built as built-in modules, like the ``sys`` module.
526+
They are built with the ``Py_BUILD_CORE_BUILTIN`` macro defined.
527+
Built-in modules have no ``__file__`` attribute::
528+
529+
>>> import sys
530+
>>> sys
531+
<module 'sys' (built-in)>
532+
>>> sys.__file__
533+
Traceback (most recent call last):
534+
File "<stdin>", line 1, in <module>
535+
AttributeError: module 'sys' has no attribute '__file__'
536+
537+
Other C extensins are built as dynamic libraires, like the ``_asyncio`` module.
538+
They are built with the ``Py_BUILD_CORE_MODULE`` macro defined.
539+
Example on Linux x86-64::
540+
541+
>>> import _asyncio
542+
>>> _asyncio
543+
<module '_asyncio' from '/usr/lib64/python3.9/lib-dynload/_asyncio.cpython-39-x86_64-linux-gnu.so'>
544+
>>> _asyncio.__file__
545+
'/usr/lib64/python3.9/lib-dynload/_asyncio.cpython-39-x86_64-linux-gnu.so'
546+
547+
:file:`Modules/Setup` is used to generate Makefile targets to build C extensions.
548+
At the beginning of the files, C extensions are built as built-in modules.
549+
Extensions defined after the ``*shared*`` marker are built as dynamic libraries.
550+
551+
The :file:`setup.py` script only builds C extensions as shared libraries using
552+
the :mod:`distutils` module.
553+
554+
The :c:macro:`PyAPI_FUNC()`, :c:macro:`PyAPI_API()` and
555+
:c:macro:`PyMODINIT_FUNC()` macros of :file:`Include/pyport.h` are defined
556+
differently depending if the ``Py_BUILD_CORE_MODULE`` macro is defined:
557+
558+
* Use ``Py_EXPORTED_SYMBOL`` if the ``Py_BUILD_CORE_MODULE`` is defined
559+
* Use ``Py_IMPORTED_SYMBOL`` otherwise.
560+
561+
If the ``Py_BUILD_CORE_BUILTIN`` macro is used by mistake on a C extension
562+
built as a shared library, its ``PyInit_xxx()`` function is not exported,
563+
causing an :exc:`ImportError` on import.
564+
565+
472566
Compiler and linker flags
473567
=========================
474568

475569
Options set by the ``./configure`` script and environment variables and used by
476570
``Makefile``.
477571

478-
Main files of the Python build system:
479-
480-
* :file:`configure.ac` => :file:`configure`;
481-
* :file:`Makefile.pre.in` => :file:`Makefile` (created by :file:`configure`);
482-
* :file:`pyconfig.h` (created by :file:`configure`);
483-
* :file:`Modules/Setup`.
484-
485572
Preprocessor flags
486573
------------------
487574

@@ -521,6 +608,16 @@ Compiler flags
521608

522609
Example: ``gcc -pthread``.
523610

611+
.. envvar:: MAINCC
612+
613+
C compiler command used to build the ``main()`` function of programs like
614+
``python``.
615+
616+
Variable set by the :option:`--with-cxx-main` option of the configure
617+
script.
618+
619+
Default: ``$(CC)``.
620+
524621
.. envvar:: CXX
525622

526623
C++ compiler command.
@@ -619,10 +716,22 @@ Compiler flags
619716

620717
.. versionadded:: 3.8
621718

719+
.. envvar:: PURIFY
720+
721+
Purify command. Purify is a memory debugger program.
722+
723+
Default: empty string (not used).
724+
622725

623726
Linker flags
624727
------------
625728

729+
.. envvar:: LINKCC
730+
731+
Linker command used to build programs like ``python`` and ``_testembed``.
732+
733+
Default: ``$(PURIFY) $(MAINCC)``.
734+
626735
.. envvar:: CONFIGURE_LDFLAGS
627736

628737
Value of :envvar:`LDFLAGS` variable passed to the ``./configure`` script.

0 commit comments

Comments
 (0)