Skip to content

Commit 1bd3cfa

Browse files
authored
Merge pull request #1746 from jere8184/windows_debug_build
Fix windows debug builds
2 parents 74aaf33 + de9ab1d commit 1bd3cfa

File tree

6 files changed

+72
-11
lines changed

6 files changed

+72
-11
lines changed

.github/workflows/windows-server-2019.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
mkdir build
5252
cd build
5353
cmake -DCMAKE_TOOLCHAIN_FILE="$TOOLCHAIN_FILE" -DCMAKE_BUILD_TYPE=Release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release -DCMAKE_CXX_FLAGS='/Zc:__cplusplus /permissive- /EHsc' -DCMAKE_EXE_LINKER_FLAGS='' -DCMAKE_MODULE_LINKER_FLAGS='' -DCMAKE_SHARED_LINKER_FLAGS='' -DDOWNLOAD_NYAN=YES -DCXX_OPTIMIZATION_LEVEL=auto -DCXX_SANITIZE_FATAL=False -DCXX_SANITIZE_MODE=none -DWANT_BACKTRACE=if_available -DWANT_GPERFTOOLS_PROFILER=if_available -DWANT_GPERFTOOLS_TCMALLOC=False -DWANT_INOTIFY=if_available -DWANT_NCURSES=if_available -DWANT_OPENGL=if_available -DWANT_VULKAN=if_available -DFLEX_EXECUTABLE="$FLEX_PATH" -G "Visual Studio 16 2019" -A x64 ../source
54-
cmake --build . --config RelWithDebInfo -- -nologo -maxCpuCount
54+
cmake --build . --config Debug -- -nologo -maxCpuCount
5555
shell: pwsh
5656
- name: Package
5757
run: |

.github/workflows/windows-server-2022.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
mkdir build
5252
cd build
5353
cmake -DCMAKE_TOOLCHAIN_FILE="$TOOLCHAIN_FILE" -DCMAKE_BUILD_TYPE=Release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release -DCMAKE_CXX_FLAGS='/Zc:__cplusplus /permissive- /EHsc' -DCMAKE_EXE_LINKER_FLAGS='' -DCMAKE_MODULE_LINKER_FLAGS='' -DCMAKE_SHARED_LINKER_FLAGS='' -DDOWNLOAD_NYAN=YES -DCXX_OPTIMIZATION_LEVEL=auto -DCXX_SANITIZE_FATAL=False -DCXX_SANITIZE_MODE=none -DWANT_BACKTRACE=if_available -DWANT_GPERFTOOLS_PROFILER=if_available -DWANT_GPERFTOOLS_TCMALLOC=False -DWANT_INOTIFY=if_available -DWANT_NCURSES=if_available -DWANT_OPENGL=if_available -DWANT_VULKAN=if_available -DFLEX_EXECUTABLE="$FLEX_PATH" -G "Visual Studio 17 2022" -A x64 ../source
54-
cmake --build . --config RelWithDebInfo -- -nologo -maxCpuCount
54+
cmake --build . --config Debug -- -nologo -maxCpuCount
5555
shell: pwsh
5656
- name: Package
5757
run: |

buildsystem/HandlePythonOptions.cmake

+13-1
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,18 @@ 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+
set(force_optimized_lib_flag "--force_optimized_lib")
51+
endif()
52+
endif()
53+
4254
set(PYEXT_INCLUDE_DIRS "${PYTHON_INCLUDE_DIRS};${NUMPY_INCLUDE_DIR}")
4355

4456
if(NOT CMAKE_PY_INSTALL_PREFIX)

buildsystem/cythonize.py

+52-4
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,13 +88,59 @@ def cythonize_wrapper(modules, **kwargs):
8888
with redirect_stdout(cython_filter):
8989
if src_modules:
9090
cythonize(src_modules, **kwargs)
91+
if sys.platform == 'win32' and force_optimized_lib:
92+
win_use_optimized_lib_python(src_modules, bin_dir)
9193

9294
if bin_modules:
9395
os.chdir(bin_dir)
9496
cythonize(bin_modules, **kwargs)
97+
if sys.platform == 'win32' and force_optimized_lib:
98+
win_use_optimized_lib_python(bin_modules, bin_dir)
9599
os.chdir(src_dir)
96100

97101

102+
def win_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+
see: https://github.com/python/cpython/issues/127619 (To unserstand the problem)
111+
see: https://stackoverflow.com/a/59566420 (To understand the soloution)
112+
"""
113+
114+
for module in modules:
115+
module = str(module)
116+
if path:
117+
module = path + "\\" + module
118+
module = module.removesuffix(".py").removesuffix(".pyx")
119+
module = module + ".cpp"
120+
with open(module, "r", encoding='utf8') as file:
121+
text = file.read()
122+
if not text.count("OPENAGE: UNDEF_DEBUG_INSERTED"):
123+
text = text.replace(
124+
'#include "Python.h"',
125+
(
126+
"\n\n// OPENAGE: UNDEF_DEBUG_INSERTED\n"
127+
"// Avoid linking to the debug version of the Python library on Windows\n\n"
128+
"#ifdef _DEBUG\n"
129+
"#define _DEBUG_WAS_DEFINED\n"
130+
"#undef _DEBUG\n#endif\n"
131+
"#include \"Python.h\"\n"
132+
"#ifdef _DEBUG_WAS_DEFINED\n"
133+
"#define _DEBUG\n"
134+
"#undef _DEBUG_WAS_DEFINED\n"
135+
"#endif\n\n"
136+
"// OPENAGE: UNDEF_DEBUG_INSERTED\n\n"
137+
),
138+
1
139+
)
140+
with open(module, "w", encoding='utf8') as file:
141+
file.write(text)
142+
143+
98144
def main():
99145
""" CLI entry point """
100146
cli = argparse.ArgumentParser()
@@ -125,6 +171,8 @@ def main():
125171
))
126172
cli.add_argument("--threads", type=int, default=cpu_count(),
127173
help="number of compilation threads to use")
174+
cli.add_argument("--force_optimized_lib", action="store_true",
175+
help= "edit compiled .cpp files to link to optimized version of python libary")
128176
args = cli.parse_args()
129177

130178
# cython emits warnings on using absolute paths to modules
@@ -159,12 +207,12 @@ def main():
159207
# writing funny lines at the head of each file.
160208
cythonize_args['language'] = 'c++'
161209

162-
cythonize_wrapper(modules, **cythonize_args)
210+
cythonize_wrapper(modules, args.force_optimized_lib, **cythonize_args)
163211

164212
# build standalone executables that embed the py interpreter
165213
Options.embed = "main"
166214

167-
cythonize_wrapper(embedded_modules, **cythonize_args)
215+
cythonize_wrapper(embedded_modules, args.force_optimized_lib, **cythonize_args)
168216

169217
# verify depends
170218
from Cython.Build.Dependencies import _dep_tree

buildsystem/python.cmake

+2-1
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.
@@ -393,6 +393,7 @@ function(python_finalize)
393393
"${CMAKE_BINARY_DIR}/py/cython_modules"
394394
"${CMAKE_BINARY_DIR}/py/cython_modules_embed"
395395
"${CMAKE_BINARY_DIR}/py/pxd_list"
396+
${force_optimized_lib_flag}
396397
"--build-dir" "${CMAKE_BINARY_DIR}"
397398
COMMAND "${CMAKE_COMMAND}" -E touch "${CYTHONIZE_TIMEFILE}"
398399
DEPENDS

libopenage/util/enum.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
1+
// Copyright 2015-2025 the openage authors. See copying.md for legal info.
22

33
#pragma once
44

@@ -44,7 +44,7 @@ namespace util {
4444
* NumericType numeric
4545
*/
4646
template <typename DerivedType, typename NumericType = int>
47-
class OAAPI EnumValue {
47+
class EnumValue {
4848
public:
4949
constexpr EnumValue(const char *value_name, NumericType numeric_value) :
5050
name(value_name), numeric(numeric_value) {}
@@ -124,7 +124,7 @@ class OAAPI EnumValue {
124124
* bool operator >=(Enum[DerivedType] other) except +
125125
*/
126126
template <typename DerivedType>
127-
class OAAPI Enum {
127+
class Enum {
128128
using this_type = Enum<DerivedType>;
129129

130130
public:

0 commit comments

Comments
 (0)