1Quick Start Guide 2----------------- 3 41. Install Microsoft Visual Studio 2017 or later with Python workload and 5 Python native development component. 61a. Optionally install Python 3.6 or later. If not installed, 7 get_externals.bat (via build.bat) will download and use Python via 8 NuGet. 92. Run "build.bat" to build Python in 32-bit Release configuration. 103. (Optional, but recommended) Run the test suite with "rt.bat -q". 11 12 13Building Python using Microsoft Visual C++ 14------------------------------------------ 15 16This directory is used to build CPython for Microsoft Windows on 32- and 64- 17bit platforms. Using this directory requires an installation of 18Microsoft Visual Studio (MSVC) with the *Python workload* and 19its optional *Python native development* component selected. 20 21Building from the command line is recommended in order to obtain any 22external dependencies. To build, simply run the "build.bat" script without 23any arguments. After this succeeds, you can open the "pcbuild.sln" 24solution in Visual Studio to continue development. 25 26To build an installer package, refer to the README in the Tools/msi folder. 27 28The solution currently supports two platforms. The Win32 platform is 29used to build standard x86-compatible 32-bit binaries, output into the 30win32 sub-directory. The x64 platform is used for building 64-bit AMD64 31(aka x86_64 or EM64T) binaries, output into the amd64 sub-directory. 32The Itanium (IA-64) platform is no longer supported. 33 34Four configuration options are supported by the solution: 35Debug 36 Used to build Python with extra debugging capabilities, equivalent 37 to using ./configure --with-pydebug on UNIX. All binaries built 38 using this configuration have "_d" added to their name: 39 python310_d.dll, python_d.exe, parser_d.pyd, and so on. Both the 40 build and rt (run test) batch files in this directory accept a -d 41 option for debug builds. If you are building Python to help with 42 development of CPython, you will most likely use this configuration. 43PGInstrument, PGUpdate 44 Used to build Python in Release configuration using PGO, which 45 requires Premium Edition of Visual Studio. See the "Profile 46 Guided Optimization" section below for more information. Build 47 output from each of these configurations lands in its own 48 sub-directory of this directory. The official Python releases may 49 be built using these configurations. 50Release 51 Used to build Python as it is meant to be used in production 52 settings, though without PGO. 53 54 55Building Python using the build.bat script 56---------------------------------------------- 57 58In this directory you can find build.bat, a script designed to make 59building Python on Windows simpler. This script will use the env.bat 60script to detect either Visual Studio 2017 or 2015, either of 61which may be used to build Python. Currently Visual Studio 2017 is 62officially supported. 63 64By default, build.bat will build Python in Release configuration for 65the 32-bit Win32 platform. It accepts several arguments to change 66this behavior, try `build.bat -h` to learn more. 67 68 69C Runtime 70--------- 71 72Visual Studio 2017 uses version 14.0 of the C runtime (vcruntime140). 73The executables no longer use the "Side by Side" assemblies used in 74previous versions of the compiler. This simplifies distribution of 75applications. 76 77The run time libraries are available under the redist folder of your 78Visual Studio distribution. For more info, see the Readme in the 79redist folder. 80 81 82Sub-Projects 83------------ 84 85The CPython project is split up into several smaller sub-projects which 86are managed by the pcbuild.sln solution file. Each sub-project is 87represented by a .vcxproj and a .vcxproj.filters file starting with the 88name of the sub-project. These sub-projects fall into a few general 89categories: 90 91The following sub-projects represent the bare minimum required to build 92a functioning CPython interpreter. If nothing else builds but these, 93you'll have a very limited but usable python.exe: 94pythoncore 95 .dll and .lib 96python 97 .exe 98 99These sub-projects provide extra executables that are useful for running 100CPython in different ways: 101pythonw 102 pythonw.exe, a variant of python.exe that doesn't open a Command 103 Prompt window 104pylauncher 105 py.exe, the Python Launcher for Windows, see 106 https://docs.python.org/3/using/windows.html#launcher 107pywlauncher 108 pyw.exe, a variant of py.exe that doesn't open a Command Prompt 109 window 110_testembed 111 _testembed.exe, a small program that embeds Python for testing 112 purposes, used by test_capi.py 113 114These are miscellaneous sub-projects that don't really fit the other 115categories: 116_freeze_module 117 _freeze_module.exe, used to regenerate frozen modules in Python 118 after changes have been made to the corresponding source files 119 (e.g. Lib\importlib\_bootstrap.py). 120pyshellext 121 pyshellext.dll, the shell extension deployed with the launcher 122python3dll 123 python3.dll, the PEP 384 Stable ABI dll 124xxlimited 125 builds an example module that makes use of the PEP 384 Stable ABI, 126 see Modules\xxlimited.c 127xxlimited_35 128 ditto for testing the Python 3.5 stable ABI, see 129 Modules\xxlimited_35.c 130 131The following sub-projects are for individual modules of the standard 132library which are implemented in C; each one builds a DLL (renamed to 133.pyd) of the same name as the project: 134 * _asyncio 135 * _ctypes 136 * _ctypes_test 137 * _zoneinfo 138 * _decimal 139 * _elementtree 140 * _hashlib 141 * _multiprocessing 142 * _overlapped 143 * _socket 144 * _testbuffer 145 * _testcapi 146 * _testlimitedcapi 147 * _testinternalcapi 148 * _testclinic 149 * _testclinic_limited 150 * _testconsole 151 * _testimportmultiple 152 * _testmultiphase 153 * _testsinglephase 154 * _tkinter 155 * pyexpat 156 * select 157 * unicodedata 158 * winsound 159 160The following Python-controlled sub-projects wrap external projects. 161Note that these external libraries are not necessary for a working 162interpreter, but they do implement several major features. See the 163"Getting External Sources" section below for additional information 164about getting the source for building these libraries. The sub-projects 165are: 166_bz2 167 Python wrapper for version 1.0.8 of the libbzip2 compression library 168 Homepage: 169 http://www.bzip.org/ 170_lzma 171 Python wrapper for version 5.2.2 of the liblzma compression library 172 Homepage: 173 https://tukaani.org/xz/ 174_ssl 175 Python wrapper for version 3.0 of the OpenSSL secure sockets 176 library, which is downloaded from our binaries repository at 177 https://github.com/python/cpython-bin-deps. 178 179 Homepage: 180 https://www.openssl.org/ 181 182 Building OpenSSL requires Perl on your path, and can be performed by 183 running PCbuild\prepare_ssl.bat. This will retrieve the version of 184 the sources matched to the current commit from the OpenSSL branch 185 in our source repository at 186 https://github.com/python/cpython-source-deps. 187 188 To use an alternative build of OpenSSL completely, you should replace 189 the files in the externals/openssl-bin-<version> folder with your own. 190 As long as this folder exists, its contents will not be downloaded 191 again when building. 192 193_sqlite3 194 Wraps SQLite 3.45.3, which is itself built by sqlite3.vcxproj 195 Homepage: 196 https://www.sqlite.org/ 197_tkinter 198 Wraps version 8.6.15 of the Tk windowing system, which is downloaded 199 from our binaries repository at 200 https://github.com/python/cpython-bin-deps. 201 202 Homepage: 203 https://www.tcl.tk/ 204 205 Building Tcl and Tk can be performed by running 206 PCbuild\prepare_tcltk.bat. This will retrieve the version of the 207 sources matched to the current commit from the Tcl and Tk branches 208 in our source repository at 209 https://github.com/python/cpython-source-deps. 210 211 The two projects install their respective components in a 212 directory alongside the source directories called "tcltk" on 213 Win32 and "tcltk64" on x64. They also copy the Tcl and Tk DLLs 214 into the current output directory, which should ensure that Tkinter 215 is able to load Tcl/Tk without having to change your PATH. 216 217 218Getting External Sources 219------------------------ 220 221The last category of sub-projects listed above wrap external projects 222Python doesn't control, and as such a little more work is required in 223order to download the relevant source files for each project before they 224can be built. However, a simple script is provided to make this as 225painless as possible, called "get_externals.bat" and located in this 226directory. This script extracts all the external sub-projects from 227 https://github.com/python/cpython-source-deps 228and 229 https://github.com/python/cpython-bin-deps 230via a Python script called "get_external.py", located in this directory. 231Everything downloaded by these scripts is stored in ..\externals 232(relative to this directory), or the path specified by the EXTERNALS_DIR 233environment variable. 234 235The path or command to use for Python can be provided with the 236PYTHON_FOR_BUILD environment variable. If this is not set, an active 237virtual environment will be used. If none is active, and HOST_PYTHON is 238set to a recent enough version or "py.exe" is able to find a recent 239enough version, those will be used. If all else fails, a copy of Python 240will be downloaded from NuGet and extracted to the externals directory. 241This will then be used for later builds (see PCbuild/find_python.bat 242for the full logic). 243 244It is also possible to download sources from each project's homepage, 245though you may have to change folder names or pass the names to MSBuild 246as the values of certain properties in order for the build solution to 247find them. This is an advanced topic and not necessarily fully 248supported. 249 250The get_externals.bat script is called automatically by build.bat 251unless you pass the '-E' option. 252 253 254Profile Guided Optimization 255--------------------------- 256 257The solution has two configurations for PGO. The PGInstrument 258configuration must be built first. The PGInstrument binaries are linked 259against a profiling library and contain extra debug information. The 260PGUpdate configuration takes the profiling data and generates optimized 261binaries. 262 263The build.bat script has an argument `--pgo` that automate the creation 264of optimized binaries. 265It creates the PGI files, runs the unit test suite with the PGI python, 266and finally creates the optimized files. 267You can customize the job for profiling with `--pgo-job <job>` option. 268 269See 270 https://docs.microsoft.com/en-us/cpp/build/profile-guided-optimizations 271for more on this topic. 272 273 274Static library 275-------------- 276 277The solution has no configuration for static libraries. However it is 278easy to build a static library instead of a DLL. You simply have to set 279the "Configuration Type" to "Static Library (.lib)" and alter the 280preprocessor macro "Py_ENABLE_SHARED" to "Py_NO_ENABLE_SHARED". You may 281also have to change the "Runtime Library" from "Multi-threaded DLL 282(/MD)" to "Multi-threaded (/MT)". 283 284 285Visual Studio properties 286------------------------ 287 288The PCbuild solution makes use of Visual Studio property files (*.props) 289to simplify each project. The properties can be viewed in the Property 290Manager (View -> Other Windows -> Property Manager) but should be 291carefully modified by hand. 292 293The property files used are: 294 * python (versions, directories and build names) 295 * pyproject (base settings for all projects) 296 * openssl (used by projects dependent upon OpenSSL) 297 * tcltk (used by _tkinter, tcl, and tk projects) 298 299The pyproject property file defines all of the build settings for each 300project, with some projects overriding certain specific values. The GUI 301doesn't always reflect the correct settings and may confuse the user 302with false information, especially for settings that automatically adapt 303for different configurations. 304 305Add a new project 306----------------- 307 308For example, add a new _testclinic_limited project to build a new 309_testclinic_limited extension, the file Modules/_testclinic_limited.c: 310 311* In PCbuild/, copy _testclinic.vcxproj to _testclinic_limited.vcxproj, 312 replace RootNamespace value with `_testclinic_limited`, replace 313 `_asyncio.c` with `_testclinic_limited.c`. 314* In PCbuild/, copy _testclinic.vcxproj.filters to 315 _testclinic_limited.vcxproj.filters, edit the list of files in the new file. 316* Open Visual Studio, open PCbuild\pcbuild.sln solution, add the 317 PCbuild\_testclinic_limited.vcxproj project to the solution ("add existing 318 project). 319* Add a dependency on the python project to the new _testclinic_limited 320 project. 321* Save and exit Visual Studio. 322* Add `;_testclinic_limited` to `<TestModules Include="...">` in 323 PCbuild\pcbuild.proj. 324* Update "exts" in Tools\msi\lib\lib_files.wxs file or in 325 Tools\msi\test\test_files.wxs file (for tests). 326* PC\layout\main.py needs updating if you add a test-only extension whose name 327 doesn't start with "_test". 328* Add the extension to PCbuild\readme.txt (this file). 329* Build Python from scratch (clean the solution) to check that the new project 330 is built successfully. 331* Ensure the new .vcxproj and .vcxproj.filters files are added to your commit, 332 as well as the changes to pcbuild.sln, pcbuild.proj and any other modified 333 files. 334