Skip to content

Commit cab7326

Browse files
committed
windows: support building for x86
If we run the main build script from a x86 Visual Studio command prompt, we should build a 32-bit Python.
1 parent 8cb609d commit cab7326

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

build-windows.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ def run():
4545
env = dict(os.environ)
4646
env['PYTHONUNBUFFERED'] = '1'
4747

48+
arch = 'x86' if os.environ.get('Platform') == 'x86' else 'amd64'
49+
4850
subprocess.run([str(PYTHON), 'build.py'],
4951
cwd=str(WINDOWS_DIR), env=env, check=True,
5052
bufsize=0)
5153

52-
source_path = BUILD / 'cpython-windows.tar'
54+
source_path = BUILD / ('cpython-windows-%s.tar' % arch)
5355

54-
compress_python_archive(source_path, DIST, 'cpython-%s-windows-amd64-%s' % (
55-
DOWNLOADS['cpython-3.7']['version'], now.strftime('%Y%m%dT%H%M')))
56+
compress_python_archive(source_path, DIST, 'cpython-%s-windows-%s-%s' % (
57+
DOWNLOADS['cpython-3.7']['version'], arch, now.strftime('%Y%m%dT%H%M')))
5658

5759

5860
if __name__ == '__main__':

cpython-windows/build.py

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def copy_link_to_lib(p: pathlib.Path):
532532
'''
533533

534534

535-
def hack_props(td: pathlib.Path, pcbuild_path: pathlib.Path):
535+
def hack_props(td: pathlib.Path, pcbuild_path: pathlib.Path, arch: str):
536536
# TODO can we pass props into msbuild.exe?
537537

538538
# Our dependencies are in different directories from what CPython's
@@ -550,7 +550,7 @@ def hack_props(td: pathlib.Path, pcbuild_path: pathlib.Path):
550550
xz_path = td / ('xz-%s' % xz_version)
551551
zlib_path = td / ('zlib-%s' % zlib_version)
552552

553-
openssl_root = td / 'openssl' / 'amd64'
553+
openssl_root = td / 'openssl' / arch
554554
openssl_libs_path = openssl_root / 'lib'
555555
openssl_include_path = openssl_root / 'include'
556556

@@ -609,12 +609,13 @@ def hack_props(td: pathlib.Path, pcbuild_path: pathlib.Path):
609609
b'libcrypto_static.lib;libssl_static.lib;')
610610

611611

612-
def hack_project_files(td: pathlib.Path, cpython_source_path: pathlib.Path):
612+
def hack_project_files(td: pathlib.Path, cpython_source_path: pathlib.Path,
613+
build_directory: str):
613614
"""Hacks Visual Studio project files to work with our build."""
614615

615616
pcbuild_path = cpython_source_path / 'PCbuild'
616617

617-
hack_props(td, pcbuild_path)
618+
hack_props(td, pcbuild_path, build_directory)
618619

619620
# Our SQLite directory is named weirdly. This throws off version detection
620621
# in the project file. Replace the parsing logic with a static string.
@@ -922,15 +923,15 @@ def hack_source_files(source_path: pathlib.Path):
922923

923924

924925
def run_msbuild(msbuild: pathlib.Path, pcbuild_path: pathlib.Path,
925-
configuration: str):
926+
configuration: str, platform: str):
926927
python_version = DOWNLOADS['cpython-3.7']['version']
927928

928929
args = [
929930
str(msbuild),
930931
str(pcbuild_path / 'pcbuild.proj'),
931932
'/target:Build',
932933
'/property:Configuration=%s' % configuration,
933-
'/property:Platform=x64',
934+
'/property:Platform=%s' % platform,
934935
'/maxcpucount',
935936
'/nologo',
936937
'/verbosity:normal',
@@ -1005,7 +1006,7 @@ def build_openssl_for_arch(perl_path, arch: str, openssl_archive, nasm_archive,
10051006
shutil.copyfile(source, dest)
10061007

10071008

1008-
def build_openssl(perl_path: pathlib.Path):
1009+
def build_openssl(perl_path: pathlib.Path, arch: str):
10091010
"""Build OpenSSL from sources using the Perl executable specified."""
10101011

10111012
# First ensure the dependencies are in place.
@@ -1026,19 +1027,26 @@ def build_openssl(perl_path: pathlib.Path):
10261027
# in order for this to work.
10271028
fs = []
10281029
with concurrent.futures.ThreadPoolExecutor(2) as e:
1029-
#fs.append(e.submit(build_openssl_for_arch, perl_path, 'x86',
1030-
# openssl_archive, nasm_archive, root_32))
1031-
fs.append(e.submit(build_openssl_for_arch, perl_path, 'amd64',
1032-
openssl_archive, nasm_archive, root_64))
1030+
if arch == 'x86':
1031+
fs.append(e.submit(build_openssl_for_arch, perl_path, 'x86',
1032+
openssl_archive, nasm_archive, root_32))
1033+
elif arch == 'amd64':
1034+
fs.append(e.submit(build_openssl_for_arch, perl_path, 'amd64',
1035+
openssl_archive, nasm_archive, root_64))
1036+
else:
1037+
raise ValueError('unhandled arch: %s' % arch)
10331038

10341039
for f in fs:
10351040
f.result()
10361041

10371042
install = td / 'out'
1038-
#shutil.copytree(root_32 / 'install' / '32', install / 'openssl' / 'win32')
1039-
shutil.copytree(root_64 / 'install' / '64', install / 'openssl' / 'amd64')
10401043

1041-
dest_archive = BUILD / 'openssl-windows.tar'
1044+
if arch == 'x86':
1045+
shutil.copytree(root_32 / 'install' / '32', install / 'openssl' / 'win32')
1046+
else:
1047+
shutil.copytree(root_64 / 'install' / '64', install / 'openssl' / 'amd64')
1048+
1049+
dest_archive = BUILD / ('openssl-windows-%s.tar' % arch)
10421050
with dest_archive.open('wb') as fh:
10431051
create_tar_from_directory(fh, install)
10441052

@@ -1254,7 +1262,7 @@ def find_additional_dependencies(project: pathlib.Path):
12541262
return res
12551263

12561264

1257-
def build_cpython(pgo=False):
1265+
def build_cpython(arch: str, pgo=False):
12581266
msbuild = find_msbuild()
12591267
log('found MSBuild at %s' % msbuild)
12601268

@@ -1273,7 +1281,18 @@ def build_cpython(pgo=False):
12731281
setuptools_archive = download_entry('setuptools', BUILD)
12741282
pip_archive = download_entry('pip', BUILD)
12751283

1276-
openssl_bin_archive = BUILD / 'openssl-windows.tar'
1284+
openssl_bin_archive = BUILD / ('openssl-windows-%s.tar' % arch)
1285+
1286+
if arch == 'amd64':
1287+
build_platform = 'x64'
1288+
build_directory = 'amd64'
1289+
json_arch = 'x86_64'
1290+
elif arch == 'x86':
1291+
build_platform = 'win32'
1292+
build_directory = 'win32'
1293+
json_arch = 'x86'
1294+
else:
1295+
raise ValueError('unhandled arch: %s' % arch)
12771296

12781297
with tempfile.TemporaryDirectory() as td:
12791298
td = pathlib.Path(td)
@@ -1304,11 +1323,12 @@ def build_cpython(pgo=False):
13041323

13051324
builtin_extensions = parse_config_c(config_c)
13061325

1307-
hack_project_files(td, cpython_source_path)
1326+
hack_project_files(td, cpython_source_path, build_directory)
13081327
hack_source_files(cpython_source_path)
13091328

13101329
if pgo:
1311-
run_msbuild(msbuild, pcbuild_path, configuration='PGInstrument')
1330+
run_msbuild(msbuild, pcbuild_path, configuration='PGInstrument',
1331+
platform=build_platform)
13121332

13131333
exec_and_log([
13141334
str(cpython_source_path / 'python.bat'), '-m', 'test', '--pgo'],
@@ -1322,17 +1342,19 @@ def build_cpython(pgo=False):
13221342
'/target:KillPython',
13231343
'/verbosity:normal',
13241344
'/property:Configuration=PGInstrument',
1325-
'/property:Platform=x64',
1345+
'/property:Platform=%s' % build_platform,
13261346
'/property:KillPython=true',
13271347
],
13281348
pcbuild_path,
13291349
os.environ)
13301350

1331-
run_msbuild(msbuild, pcbuild_path, configuration='PGUpdate')
1351+
run_msbuild(msbuild, pcbuild_path, configuration='PGUpdate',
1352+
platform=build_platform)
13321353
artifact_config = 'PGUpdate'
13331354

13341355
else:
1335-
run_msbuild(msbuild, pcbuild_path, configuration='Release')
1356+
run_msbuild(msbuild, pcbuild_path, configuration='Release',
1357+
platform=build_platform)
13361358
artifact_config = 'Release'
13371359

13381360
install_dir = out_dir / 'python' / 'install'
@@ -1352,7 +1374,7 @@ def build_cpython(pgo=False):
13521374
str(cpython_source_path / 'PC' / 'layout'),
13531375
'-vv',
13541376
'--source', str(cpython_source_path),
1355-
'--build', str(pcbuild_path / 'amd64'),
1377+
'--build', str(pcbuild_path / build_directory),
13561378
'--copy', str(install_dir),
13571379
'--temp', str(layout_tmp),
13581380
'--flat-dlls',
@@ -1383,7 +1405,7 @@ def build_cpython(pgo=False):
13831405

13841406
# Now copy the build artifacts into the output directory.
13851407
build_info = collect_python_build_artifacts(
1386-
pcbuild_path, out_dir / 'python', 'amd64', artifact_config)
1408+
pcbuild_path, out_dir / 'python', build_directory, artifact_config)
13871409

13881410
for ext, init_fn in sorted(builtin_extensions.items()):
13891411
if ext in build_info['extensions']:
@@ -1406,7 +1428,7 @@ def build_cpython(pgo=False):
14061428
# Copy OpenSSL libraries as a one-off.
14071429
for lib in ('crypto', 'ssl'):
14081430
name = 'lib%s_static.lib' % lib
1409-
source = td / 'openssl' / 'amd64' / 'lib' / name
1431+
source = td / 'openssl' / build_directory / 'lib' / name
14101432
dest = out_dir / 'python' / 'build' / 'lib' / name
14111433
log('copying %s to %s' % (source, dest))
14121434
shutil.copyfile(source, dest)
@@ -1421,7 +1443,7 @@ def build_cpython(pgo=False):
14211443
python_info = {
14221444
'version': '2',
14231445
'os': 'windows',
1424-
'arch': 'x86_64',
1446+
'arch': json_arch,
14251447
'python_flavor': 'cpython',
14261448
'python_version': python_version,
14271449
'python_exe': 'install/python.exe',
@@ -1435,7 +1457,7 @@ def build_cpython(pgo=False):
14351457
with (out_dir / 'python' / 'PYTHON.json').open('w', encoding='utf8') as fh:
14361458
json.dump(python_info, fh, sort_keys=True, indent=4)
14371459

1438-
dest_path = BUILD / 'cpython-windows.tar'
1460+
dest_path = BUILD / ('cpython-windows-%s.tar' % arch)
14391461

14401462
with dest_path.open('wb') as fh:
14411463
create_tar_from_directory(fh, td / 'out')
@@ -1458,15 +1480,18 @@ def main():
14581480
with log_path.open('wb') as log_fh:
14591481
LOG_FH[0] = log_fh
14601482

1483+
arch = 'x86' if os.environ.get('Platform') == 'x86' else 'amd64'
1484+
14611485
# TODO need better dependency checking.
1462-
openssl_out = BUILD / 'openssl-windows.tar'
1486+
openssl_out = BUILD / ('openssl-windows-%s.tar' % arch)
14631487
if not openssl_out.exists():
14641488
perl_path = fetch_strawberry_perl() / 'perl' / 'bin' / 'perl.exe'
14651489
LOG_PREFIX[0] = 'openssl'
1466-
build_openssl(perl_path)
1490+
build_openssl(perl_path, arch)
14671491

14681492
LOG_PREFIX[0] = 'cpython'
1469-
build_cpython()
1493+
build_cpython(arch)
1494+
14701495

14711496
if __name__ == '__main__':
14721497
sys.exit(main())

0 commit comments

Comments
 (0)