Skip to content

Commit 7eca2be

Browse files
jere8184heinezen
authored andcommitted
working windows debug build
1 parent 423cfed commit 7eca2be

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

buildsystem/HandlePythonOptions.cmake

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015-2023 the openage authors. See copying.md for legal info.
1+
# Copyright 2015-2025 the openage authors. See copying.md for legal info.
22

33
# finds the python interpreter, install destination and extension flags.
44

@@ -39,6 +39,17 @@ if(PYTHON_VER VERSION_GREATER_EQUAL 3.8 AND PYTHON_VERSION VERSION_LESS 3.9)
3939
endif()
4040

4141
set(PYEXT_LIBRARY "${PYTHON_LIBRARIES}")
42+
43+
#Windows always uses optimized version of Python lib
44+
if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
45+
#get index of string "optimized" and increment it by 1 so index points at the path of the optimized lib
46+
list (FIND PYEXT_LIBRARY "optimized" _index)
47+
if (${_index} GREATER -1)
48+
MATH(EXPR _index "${_index}+1")
49+
list(GET PYEXT_LIBRARY ${_index} PYEXT_LIBRARY)
50+
endif()
51+
endif()
52+
4253
set(PYEXT_INCLUDE_DIRS "${PYTHON_INCLUDE_DIRS};${NUMPY_INCLUDE_DIR}")
4354

4455
if(NOT CMAKE_PY_INSTALL_PREFIX)

buildsystem/cythonize.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
#
3-
# Copyright 2015-2021 the openage authors. See copying.md for legal info.
3+
# Copyright 2015-2025 the openage authors. See copying.md for legal info.
44

55
"""
66
Runs Cython on all modules that were listed via add_cython_module.
@@ -73,7 +73,7 @@ def remove_if_exists(filename):
7373
filename.unlink()
7474

7575

76-
def cythonize_wrapper(modules, **kwargs):
76+
def cythonize_wrapper(modules, force_optimized_lib = False, **kwargs):
7777
""" Calls cythonize, filtering useless warnings """
7878
bin_dir, bin_modules = kwargs['build_dir'], []
7979
src_dir, src_modules = Path.cwd(), []
@@ -88,29 +88,40 @@ def cythonize_wrapper(modules, **kwargs):
8888
with redirect_stdout(cython_filter):
8989
if src_modules:
9090
cythonize(src_modules, **kwargs)
91-
windows_include_python_debug_build_wrapper(src_modules, bin_dir)
91+
if sys.platform == 'win32' and force_optimized_lib:
92+
windows_use_optimized_lib_python(src_modules, bin_dir)
9293

9394
if bin_modules:
9495
os.chdir(bin_dir)
9596
cythonize(bin_modules, **kwargs)
96-
windows_include_python_debug_build_wrapper(bin_modules, bin_dir)
97+
if sys.platform == 'win32' and force_optimized_lib:
98+
windows_use_optimized_lib_python(bin_modules, bin_dir)
9799
os.chdir(src_dir)
98100

99101

100-
def windows_include_python_debug_build_wrapper(modules, path):
102+
def windows_use_optimized_lib_python(modules, path):
103+
"""
104+
Add an #ifdef statement in cythonized .cpp files to temporarily undefine _DEBUG before
105+
#include "Python.h"
106+
107+
This function modifies the generated C++ files from Cython to prevent linking to
108+
the debug version of the Python library on Windows. The debug version of the
109+
Python library cannot import Python libraries that contain extension modules.
110+
"""
111+
101112
for module in modules:
102113
module = str(module)
103-
if (path):
114+
if path:
104115
module = path + "\\" + module
105116
module = module.removesuffix(".py").removesuffix(".pyx")
106117
module = module + ".cpp"
107-
with open(module, "r") as file:
118+
with open(module, "r", encoding='utf8') as file:
108119
text = file.read()
109120
text = text.replace("#include \"Python.h\"",
110121
"#ifdef _DEBUG\n#define _DEBUG_WAS_DEFINED\n#undef _DEBUG\n#endif\n\
111122
#include \"Python.h\"\n#ifdef _DEBUG_WAS_DEFINED\n#define _DEBUG\n\
112123
#undef _DEBUG_WAS_DEFINED\n#endif", 1)
113-
with open(module, "w") as file:
124+
with open(module, "w", encoding='utf8') as file:
114125
file.write(text)
115126

116127

@@ -144,6 +155,8 @@ def main():
144155
))
145156
cli.add_argument("--threads", type=int, default=cpu_count(),
146157
help="number of compilation threads to use")
158+
cli.add_argument("--force_optimized_lib", action="store_true",
159+
help= "edit compiled .cpp files to link to optimized version of python libary")
147160
args = cli.parse_args()
148161

149162
# cython emits warnings on using absolute paths to modules
@@ -178,12 +191,12 @@ def main():
178191
# writing funny lines at the head of each file.
179192
cythonize_args['language'] = 'c++'
180193

181-
cythonize_wrapper(modules, **cythonize_args)
194+
cythonize_wrapper(modules, args.force_optimized_lib, **cythonize_args)
182195

183196
# build standalone executables that embed the py interpreter
184197
Options.embed = "main"
185198

186-
cythonize_wrapper(embedded_modules, **cythonize_args)
199+
cythonize_wrapper(embedded_modules, args.force_optimized_lib, **cythonize_args)
187200

188201
# verify depends
189202
from Cython.Build.Dependencies import _dep_tree

buildsystem/python.cmake

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2014-2020 the openage authors. See copying.md for legal info.
1+
# Copyright 2014-2025 the openage authors. See copying.md for legal info.
22

33
# provides macros for defining python extension modules and pxdgen sources.
44
# and a 'finalize' function that must be called in the end.
@@ -128,7 +128,8 @@ function(add_cython_modules)
128128
if(MINGW)
129129
set_target_properties("${TARGETNAME}" PROPERTIES LINK_FLAGS "-municode")
130130
endif()
131-
target_link_libraries("${TARGETNAME}" PRIVATE C:/vcpkg/installed/x64-windows/lib/python311.lib)
131+
132+
target_link_libraries("${TARGETNAME}" PRIVATE ${PYEXT_LIBRARY})
132133
else()
133134
set_property(GLOBAL APPEND PROPERTY SFT_CYTHON_MODULES "${source}")
134135
add_library("${TARGETNAME}" MODULE "${CPPNAME}")
@@ -139,7 +140,7 @@ function(add_cython_modules)
139140
)
140141

141142
if(WIN32)
142-
target_link_libraries("${TARGETNAME}" PRIVATE C:/vcpkg/installed/x64-windows/lib/python311.lib)
143+
target_link_libraries("${TARGETNAME}" PRIVATE ${PYEXT_LIBRARY})
143144
endif()
144145
endif()
145146

@@ -374,6 +375,9 @@ function(python_finalize)
374375

375376

376377
# cythonize (.pyx -> .cpp)
378+
if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
379+
set(force_optimized_lib "--force_optimized_lib")
380+
endif()
377381

378382
get_property(cython_modules GLOBAL PROPERTY SFT_CYTHON_MODULES)
379383
write_on_change("${CMAKE_BINARY_DIR}/py/cython_modules" "${cython_modules}")
@@ -392,6 +396,7 @@ function(python_finalize)
392396
"${CMAKE_BINARY_DIR}/py/cython_modules"
393397
"${CMAKE_BINARY_DIR}/py/cython_modules_embed"
394398
"${CMAKE_BINARY_DIR}/py/pxd_list"
399+
${force_optimized_lib}
395400
"--build-dir" "${CMAKE_BINARY_DIR}"
396401
COMMAND "${CMAKE_COMMAND}" -E touch "${CYTHONIZE_TIMEFILE}"
397402
DEPENDS
@@ -513,7 +518,6 @@ function(python_finalize)
513518
${INPLACEMODULES_LISTFILE}
514519
"$<CONFIG>"
515520
)
516-
message(hello jeremiah, ${INPLACEMODULES_LISTFILE})
517521
set(INPLACEMODULES_TIMEFILE "${CMAKE_BINARY_DIR}/py/inplacemodules_timefile")
518522
add_custom_command(OUTPUT "${INPLACEMODULES_TIMEFILE}"
519523
COMMAND ${INPLACEMODULES_INVOCATION}

0 commit comments

Comments
 (0)