Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows debug builds #1746

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/windows-server-2019.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
mkdir build
cd build
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
cmake --build . --config RelWithDebInfo -- -nologo -maxCpuCount
cmake --build . --config Debug -- -nologo -maxCpuCount
shell: pwsh
- name: Package
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows-server-2022.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
mkdir build
cd build
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
cmake --build . --config RelWithDebInfo -- -nologo -maxCpuCount
cmake --build . --config Debug -- -nologo -maxCpuCount
shell: pwsh
- name: Package
run: |
Expand Down
14 changes: 13 additions & 1 deletion buildsystem/HandlePythonOptions.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015-2023 the openage authors. See copying.md for legal info.
# Copyright 2015-2025 the openage authors. See copying.md for legal info.

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

Expand Down Expand Up @@ -39,6 +39,18 @@ if(PYTHON_VER VERSION_GREATER_EQUAL 3.8 AND PYTHON_VERSION VERSION_LESS 3.9)
endif()

set(PYEXT_LIBRARY "${PYTHON_LIBRARIES}")

#Windows always uses optimized version of Python lib
if(WIN32 AND "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
#get index of string "optimized" and increment it by 1 so index points at the path of the optimized lib
list (FIND PYEXT_LIBRARY "optimized" _index)
if (${_index} GREATER -1)
MATH(EXPR _index "${_index}+1")
list(GET PYEXT_LIBRARY ${_index} PYEXT_LIBRARY)
set(force_optimized_lib_flag "--force_optimized_lib")
endif()
endif()

set(PYEXT_INCLUDE_DIRS "${PYTHON_INCLUDE_DIRS};${NUMPY_INCLUDE_DIR}")

if(NOT CMAKE_PY_INSTALL_PREFIX)
Expand Down
56 changes: 52 additions & 4 deletions buildsystem/cythonize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright 2015-2021 the openage authors. See copying.md for legal info.
# Copyright 2015-2025 the openage authors. See copying.md for legal info.

"""
Runs Cython on all modules that were listed via add_cython_module.
Expand Down Expand Up @@ -73,7 +73,7 @@ def remove_if_exists(filename):
filename.unlink()


def cythonize_wrapper(modules, **kwargs):
def cythonize_wrapper(modules, force_optimized_lib = False, **kwargs):
""" Calls cythonize, filtering useless warnings """
bin_dir, bin_modules = kwargs['build_dir'], []
src_dir, src_modules = Path.cwd(), []
Expand All @@ -88,13 +88,59 @@ def cythonize_wrapper(modules, **kwargs):
with redirect_stdout(cython_filter):
if src_modules:
cythonize(src_modules, **kwargs)
if sys.platform == 'win32' and force_optimized_lib:
win_use_optimized_lib_python(src_modules, bin_dir)

if bin_modules:
os.chdir(bin_dir)
cythonize(bin_modules, **kwargs)
if sys.platform == 'win32' and force_optimized_lib:
win_use_optimized_lib_python(bin_modules, bin_dir)
os.chdir(src_dir)


def win_use_optimized_lib_python(modules, path):
"""
Add an #ifdef statement in cythonized .cpp files to temporarily undefine _DEBUG before
#include "Python.h"

This function modifies the generated C++ files from Cython to prevent linking to
the debug version of the Python library on Windows. The debug version of the
Python library cannot import Python libraries that contain extension modules.
see: https://github.com/python/cpython/issues/127619 (To unserstand the problem)
see: https://stackoverflow.com/a/59566420 (To understand the soloution)
"""

for module in modules:
module = str(module)
if path:
module = path + "\\" + module
module = module.removesuffix(".py").removesuffix(".pyx")
module = module + ".cpp"
with open(module, "r", encoding='utf8') as file:
text = file.read()
if not text.count("OPENAGE: UNDEF_DEBUG_INSERTED"):
text = text.replace(
'#include "Python.h"',
(
"\n\n// OPENAGE: UNDEF_DEBUG_INSERTED\n"
"// Avoid linking to the debug version of the Python library on Windows\n\n"
"#ifdef _DEBUG\n"
"#define _DEBUG_WAS_DEFINED\n"
"#undef _DEBUG\n#endif\n"
"#include \"Python.h\"\n"
"#ifdef _DEBUG_WAS_DEFINED\n"
"#define _DEBUG\n"
"#undef _DEBUG_WAS_DEFINED\n"
"#endif\n\n"
"// OPENAGE: UNDEF_DEBUG_INSERTED\n\n"
),
1
)
with open(module, "w", encoding='utf8') as file:
file.write(text)


def main():
""" CLI entry point """
cli = argparse.ArgumentParser()
Expand Down Expand Up @@ -125,6 +171,8 @@ def main():
))
cli.add_argument("--threads", type=int, default=cpu_count(),
help="number of compilation threads to use")
cli.add_argument("--force_optimized_lib", action="store_true",
help= "edit compiled .cpp files to link to optimized version of python libary")
args = cli.parse_args()

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

cythonize_wrapper(modules, **cythonize_args)
cythonize_wrapper(modules, args.force_optimized_lib, **cythonize_args)

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

cythonize_wrapper(embedded_modules, **cythonize_args)
cythonize_wrapper(embedded_modules, args.force_optimized_lib, **cythonize_args)

# verify depends
from Cython.Build.Dependencies import _dep_tree
Expand Down
3 changes: 2 additions & 1 deletion buildsystem/python.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2014-2020 the openage authors. See copying.md for legal info.
# Copyright 2014-2025 the openage authors. See copying.md for legal info.

# provides macros for defining python extension modules and pxdgen sources.
# and a 'finalize' function that must be called in the end.
Expand Down Expand Up @@ -393,6 +393,7 @@ function(python_finalize)
"${CMAKE_BINARY_DIR}/py/cython_modules"
"${CMAKE_BINARY_DIR}/py/cython_modules_embed"
"${CMAKE_BINARY_DIR}/py/pxd_list"
${force_optimized_lib_flag}
"--build-dir" "${CMAKE_BINARY_DIR}"
COMMAND "${CMAKE_COMMAND}" -E touch "${CYTHONIZE_TIMEFILE}"
DEPENDS
Expand Down
6 changes: 3 additions & 3 deletions libopenage/util/enum.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
// Copyright 2015-2025 the openage authors. See copying.md for legal info.

#pragma once

Expand Down Expand Up @@ -44,7 +44,7 @@ namespace util {
* NumericType numeric
*/
template <typename DerivedType, typename NumericType = int>
class OAAPI EnumValue {
class EnumValue {
public:
constexpr EnumValue(const char *value_name, NumericType numeric_value) :
name(value_name), numeric(numeric_value) {}
Expand Down Expand Up @@ -124,7 +124,7 @@ class OAAPI EnumValue {
* bool operator >=(Enum[DerivedType] other) except +
*/
template <typename DerivedType>
class OAAPI Enum {
class Enum {
using this_type = Enum<DerivedType>;

public:
Expand Down
Loading