This commit is contained in:
2026-02-28 14:40:03 +08:00
parent 6a80e6c76b
commit 6ceef5119d
58 changed files with 13236 additions and 0 deletions
@@ -0,0 +1,93 @@
# For building the bdist_wininst subdir
include_directories(
${SRC_DIR}/Include
${SRC_DIR}/Modules/zlib
)
set(WININST_HEADERS
${SRC_DIR}/PC/bdist_wininst/archive.h
${SRC_DIR}/PC/bdist_wininst/resource.h
)
set(WININST_SOURCES
${SRC_DIR}/PC/bdist_wininst/extract.c
${SRC_DIR}/PC/bdist_wininst/install.c
${SRC_DIR}/Modules/zlib/adler32.c
${SRC_DIR}/Modules/zlib/crc32.c
${SRC_DIR}/Modules/zlib/inffast.c
${SRC_DIR}/Modules/zlib/inflate.c
${SRC_DIR}/Modules/zlib/inftrees.c
${SRC_DIR}/Modules/zlib/zutil.c
)
# Install tree directory
set(WININST_INSTALL_DIR ${PYTHONHOME}/distutils/command)
# Build tree directory
set(WININST_BUILD_DIR ${PROJECT_BINARY_DIR}/${WININST_INSTALL_DIR})
# Compiler version
set(version "unknown")
foreach(msvcmajver IN ITEMS 90 10 11 12 14)
if(${MSVC${msvcmajver}})
string(REGEX REPLACE "0$" "" msvcmajver ${msvcmajver})
set(version "${msvcmajver}.0")
endif()
endforeach()
# System architecture
set(arch "")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(arch "-amd64")
endif()
# Set installer executable name
set(WININST_EXE_NAME "wininst-${version}${arch}")
message(STATUS "bdist_wininst: Generated 'Windows Installer' name is '${WININST_EXE_NAME}'")
# Check if windows installer is already bundled in the source tree
set(_have_wininst_in_source 0)
set(_msg "bdist_wininst: Looking for executable named '${WININST_EXE_NAME}${CMAKE_EXECUTABLE_SUFFIX}' in source tree")
message(STATUS "${_msg}")
set(_expected_wininst ${SRC_DIR}/${WININST_INSTALL_DIR}/${WININST_EXE_NAME}${CMAKE_EXECUTABLE_SUFFIX})
if(EXISTS ${_expected_wininst})
set(_have_wininst_in_source 1)
message(STATUS "${_msg} - found \"${_expected_wininst}\"")
else()
message(STATUS "${_msg} - not found")
endif()
# Skip if installer is already bundled in the source tree and BUILD_WININST_ALWAYS is OFF
set(_configure_reason "")
if(_have_wininst_in_source)
if(NOT BUILD_WININST_ALWAYS)
message(STATUS "bdist_wininst: Skipping installer configuration because it is already provided in the source tree.")
return()
else()
set(_configure_reason " because BUILD_WININST_ALWAYS is ON")
endif()
endif()
message(STATUS "bdist_wininst: Configuring 'Windows Installer' named '${WININST_EXE_NAME}'${_configure_reason}")
# Configure installer
add_executable(bdist_wininst WIN32 ${WININST_HEADERS} ${WININST_SOURCES})
target_link_libraries(bdist_wininst
imagehlp
comctl32
)
set_target_properties(bdist_wininst PROPERTIES
OUTPUT_NAME ${WININST_EXE_NAME}
RUNTIME_OUTPUT_DIRECTORY ${WININST_BUILD_DIR}
)
if(CMAKE_CONFIGURATION_TYPES)
add_custom_command(TARGET bdist_wininst POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:bdist_wininst>
${WININST_BUILD_DIR}/${WININST_EXE_NAME}${CMAKE_EXECUTABLE_SUFFIX}
COMMENT "bdist_wininst: Copying installer into '${WININST_INSTALL_DIR}'"
)
endif()
install(TARGETS bdist_wininst DESTINATION ${WININST_INSTALL_DIR})
+139
View File
@@ -0,0 +1,139 @@
function(_add_executable_without_windows target_name)
# Work around the lack of a "target_remove_definitions()" CMake command by
# explicitly undefining _WINDOWS. The following alternatives are either ineffective
# or not applicable:
#
# (1) Modifying and restoring CMAKE_C_FLAGS does not work reliably, as CMake appears
# to apply cached values during the generation step after the function executes.
#
# (2) Using get_target_property/set_property to manipulate COMPILE_DEFINITIONS
# is ineffective, since /D_WINDOWS is introduced via CMAKE_C_FLAGS and not
# associated with the target's properties.
#
# See: https://gitlab.kitware.com/cmake/cmake/-/issues/19796
add_executable(${target_name} ${ARGN})
# Note: /U_WINDOWS overrides the implicit /D_WINDOWS flag, resulting in MSVC warning D9025.
# This warning is harmless and cannot be suppressed directly.
target_compile_options(${target_name} PRIVATE /U_WINDOWS)
endfunction()
# Install tree directory
set(LAUNCHER_INSTALL_DIR ${BIN_BUILD_DIR})
# Build tree directory
set(LAUNCHER_BUILD_DIR ${PROJECT_BINARY_DIR}/${LAUNCHER_INSTALL_DIR})
set(launcher_targets)
set(build_venvlauncher 0)
# While support for building "venvlauncher" was introduced in Python 3.3,
# we require at least Python 3.9 to avoid the need to generate "pythonnt_rc.h",
# which was removed in python/cpython@4efc3360c9a ("bpo-41054: Simplify resource compilation on Windows (GH-21004)", 2020-06-24).
if(PY_VERSION VERSION_GREATER_EQUAL "3.9")
set(build_venvlauncher 1)
endif()
if(build_venvlauncher)
set(target_sources
${SRC_DIR}/PC/launcher.c
${SRC_DIR}/PC/pylauncher.rc
)
set(target_include_dirs
${SRC_DIR}/PC/
)
set(target_libraries
version
)
# venvlauncher
set(target_name "venvlauncher")
_add_executable_without_windows(${target_name} ${target_sources})
target_include_directories(${target_name} PRIVATE ${target_include_dirs})
target_link_libraries(${target_name} PRIVATE ${target_libraries})
target_compile_definitions(${target_name}
PRIVATE
_CONSOLE
_UNICODE
VENV_REDIRECT
PY_ICON # For "PC/pylauncher.rc"
FIELD3=${PY_FIELD3_VALUE}
)
list(APPEND launcher_targets ${target_name})
# venvwlauncher
set(target_name "venvwlauncher")
add_executable(${target_name} WIN32 ${target_sources})
target_include_directories(${target_name} PRIVATE ${target_include_dirs})
target_link_libraries(${target_name} PRIVATE ${target_libraries})
target_compile_definitions(${target_name}
PRIVATE
_UNICODE
_WINDOWS
VENV_REDIRECT
PYW_ICON # For "PC/pylauncher.rc"
FIELD3=${PY_FIELD3_VALUE}
)
list(APPEND launcher_targets ${target_name})
endif()
set(build_pylauncher 0)
if(PY_VERSION VERSION_GREATER_EQUAL "3.11")
set(build_pylauncher 1)
endif()
if(build_pylauncher)
set(target_sources
${SRC_DIR}/PC/launcher2.c
${SRC_DIR}/PC/pylauncher.rc
)
set(target_include_dirs
${SRC_DIR}/PC/
)
set(target_libraries
pathcch
shell32
)
# pylauncher
set(target_name "pylauncher")
_add_executable_without_windows(${target_name} ${target_sources})
target_include_directories(${target_name} PRIVATE ${target_include_dirs})
target_link_libraries(${target_name} PRIVATE ${target_libraries})
target_compile_definitions(${target_name}
PRIVATE
_CONSOLE
_UNICODE
FIELD3=${PY_FIELD3_VALUE}
)
list(APPEND launcher_targets ${target_name})
# pywlauncher
set(target_name "pywlauncher")
add_executable(${target_name} WIN32 ${target_sources})
target_include_directories(${target_name} PRIVATE ${target_include_dirs})
target_link_libraries(${target_name} PRIVATE ${target_libraries})
target_compile_definitions(${target_name}
PRIVATE
_UNICODE
_WINDOWS
FIELD3=${PY_FIELD3_VALUE}
)
list(APPEND launcher_targets ${target_name})
endif()
if(launcher_targets)
set_target_properties(${launcher_targets}
PROPERTIES
LINK_FLAGS "/MANIFEST:NO"
RUNTIME_OUTPUT_DIRECTORY ${LAUNCHER_INSTALL_DIR}
)
install(TARGETS ${launcher_targets} RUNTIME DESTINATION ${BIN_INSTALL_DIR} COMPONENT Runtime)
endif()