From 026b840f5cff664fe16e24964baf1dbe7b0176af Mon Sep 17 00:00:00 2001 From: Ermoshkin Artem <94714022+Shfdis@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:44:53 +0300 Subject: [PATCH 1/2] Configure standalone Windows builds for MinGW-w64 --- CMakeLists.txt | 3 +++ CMakePresets.json | 11 +++++++++++ README.md | 17 ++++++++++++++++- cmake/common.cmake | 10 ++++++++++ cmake/external_libs.cmake | 5 +++++ cmake/global_flags.cmake | 4 ++++ cmake/windows.cmake | 18 ++++++++++++++++++ 7 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 cmake/windows.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index e28d30dca43..34e5ecfcf6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,7 @@ if (YDB_CPP_SDK_SLO_USE_INSTALLED_SDK) add_subdirectory(util) target_sources(yutil PRIVATE "${YDB_SDK_SOURCE_DIR}/cmake/tstring_abi.cpp") add_subdirectory(tests/slo_workloads) + include(cmake/windows.cmake) return() endif() @@ -126,6 +127,8 @@ if (YDB_SDK_TESTS) add_subdirectory(tests) endif() +include(cmake/windows.cmake) + if (YDB_SDK_INSTALL) function(_ydb_sdk_create_package_library PackageProp TargetName ComponentName) foreach(Tgt IN LISTS YDB_CPP_${PackageProp}_COMPONENT_TARGETS) diff --git a/CMakePresets.json b/CMakePresets.json index 74a2bdf5517..0b5b1b3a500 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -89,6 +89,13 @@ "inherits": ["release-base", "gcc-toolchain"], "displayName": "CPM Release (GCC)" }, + { + "name": "release-windows-mingw", + "inherits": ["release-base", "gcc-toolchain"], + "displayName": "Windows x64 Release (MinGW-w64)", + "binaryDir": "${sourceDir}/build-windows-mingw", + "condition": {"type": "equals", "lhs": "${hostSystemName}", "rhs": "Windows"} + }, { "name": "release-test-clang", "inherits": ["release-base", "clang-toolchain", "test"], @@ -136,6 +143,10 @@ } ], "buildPresets": [ + { + "name": "release-windows-mingw", + "configurePreset": "release-windows-mingw" + }, { "name": "default", "displayName": "Default Build", diff --git a/README.md b/README.md index d89af4311a3..064733d3bf7 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ - ninja 1.10+ - ragel - yasm -- OpenSSL +- OpenSSL (the pinned gRPC does not support OpenSSL 4.x) - Iconv - IDN @@ -44,6 +44,21 @@ xcode-select --install # if the Command Line Tools are not installed yet brew install ccache cmake git libidn ninja openssl@3 python ragel yasm ``` +Windows x64 (MinGW-w64): + +Use dependencies built with the same MinGW-w64 toolchain and C runtime: +OpenSSL 3.x, GNU Libidn (the `idna.h` API), and Iconv. Put the MinGW `bin` +directory, Python, Ragel, Yasm, Ninja, and dependency DLL directories on `PATH`. +Run from PowerShell: + +```powershell +cmake --preset release-windows-mingw -DCMAKE_PREFIX_PATH=C:/deps/mingw +cmake --build --preset release-windows-mingw --parallel +``` + +If your dependency prefix contains OpenSSL 4.x, use a separate OpenSSL 3.x +prefix and pass `-DOPENSSL_ROOT_DIR=C:/deps/openssl3`. + ### Clone the ydb-cpp-sdk repository ```bash diff --git a/cmake/common.cmake b/cmake/common.cmake index e32407e16cb..4da5de9605e 100644 --- a/cmake/common.cmake +++ b/cmake/common.cmake @@ -3,6 +3,16 @@ find_package(Python3 REQUIRED) function(_ydb_sdk_prepare_imported_libc_compat TargetName) + if(MINGW) + set(_libc_root "${YDB_SDK_SOURCE_DIR}/contrib/libs/libc_compat") + set(_windows_sources memrchr.c explicit_bzero.c stpcpy.c strlcat.c strlcpy.c + strcasestr.c strsep.c reallocarray/reallocarray.c src/windows/sys/uio.c) + list(TRANSFORM _windows_sources PREPEND "${_libc_root}/") + set_property(TARGET ${TargetName} PROPERTY SOURCES "${_windows_sources}") + target_include_directories(${TargetName} PUBLIC + $ + $) + endif() if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 14) diff --git a/cmake/external_libs.cmake b/cmake/external_libs.cmake index 1e6120bc3cd..73d6c062272 100644 --- a/cmake/external_libs.cmake +++ b/cmake/external_libs.cmake @@ -1,7 +1,12 @@ # Platform prerequisites deliberately remain outside CPM. find_package(IDN REQUIRED) find_package(Iconv REQUIRED) +# The pinned gRPC still uses OpenSSL APIs whose signatures changed in 4.x. find_package(OpenSSL REQUIRED) +if(OPENSSL_VERSION VERSION_GREATER_EQUAL 4) + message(FATAL_ERROR + "The pinned gRPC does not support OpenSSL 4.x; configure with an OpenSSL 3.x prefix") +endif() find_package(Threads REQUIRED) include("${CMAKE_CURRENT_LIST_DIR}/dependencies.cmake") diff --git a/cmake/global_flags.cmake b/cmake/global_flags.cmake index 6cee71ada62..b2d689983b5 100644 --- a/cmake/global_flags.cmake +++ b/cmake/global_flags.cmake @@ -3,6 +3,10 @@ set(flagPrefixSymbol "-") +if(MINGW) + add_compile_definitions(NOMINMAX WIN32_LEAN_AND_MEAN) +endif() + string(APPEND ${COMMON_C_CXX_FLAGS} "\ -fexceptions \ -fno-common \ diff --git a/cmake/windows.cmake b/cmake/windows.cmake new file mode 100644 index 00000000000..5f92f8b2fe1 --- /dev/null +++ b/cmake/windows.cmake @@ -0,0 +1,18 @@ +# Standalone platform adjustments belong here, outside imported directories. +if(NOT MINGW) + return() +endif() + +if(TARGET yutil) + target_link_libraries(yutil PUBLIC ws2_32 iphlpapi psapi advapi32) + # Backslashes in these stringized macros would become C++ escape sequences. + foreach(_property IN ITEMS COMPILE_DEFINITIONS INTERFACE_COMPILE_DEFINITIONS) + get_target_property(_definitions yutil ${_property}) + list(FILTER _definitions EXCLUDE REGEX "^ARCADIA_(BUILD_)?ROOT=") + set_property(TARGET yutil PROPERTY ${_property} "${_definitions}") + endforeach() + file(TO_CMAKE_PATH "${ARCADIA_ROOT}" _source_root) + file(TO_CMAKE_PATH "${ARCADIA_BUILD_ROOT}" _build_root) + target_compile_definitions(yutil PUBLIC + ARCADIA_ROOT=${_source_root} ARCADIA_BUILD_ROOT=${_build_root}) +endif() From 85372ae07e544da24291f0d3e3d7e8a101b76e45 Mon Sep 17 00:00:00 2001 From: Ermoshkin Artem <94714022+Shfdis@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:10:48 +0300 Subject: [PATCH 2/2] Generate MinGW compatibility sources in the build directory --- CMakeLists.txt | 6 ++ README.md | 5 ++ cmake/common.cmake | 7 +++ cmake/global_flags.cmake | 1 + cmake/mingw_compat.cmake | 123 +++++++++++++++++++++++++++++++++++++++ cmake/windows.cmake | 7 +++ 6 files changed, 149 insertions(+) create mode 100644 cmake/mingw_compat.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 34e5ecfcf6e..5cc4e6a776a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,6 +67,9 @@ include(cmake/protobuf.cmake) if (YDB_CPP_SDK_SLO_USE_INSTALLED_SDK) set(YDB_SDK_DEPENDENCIES_HDR_ONLY ON) include(cmake/dependencies.cmake) + if(MINGW) + include_directories(BEFORE "${YDB_SDK_MINGW_COMPAT_DIR}") + endif() include(cmake/testing.cmake) find_package(ydb-cpp-sdk REQUIRED COMPONENTS @@ -98,6 +101,9 @@ if (YDB_CPP_SDK_SLO_USE_INSTALLED_SDK) endif() include(cmake/external_libs.cmake) +if(MINGW) + include_directories(BEFORE "${YDB_SDK_MINGW_COMPAT_DIR}") +endif() if (YDB_SDK_TESTS) include(cmake/testing.cmake) diff --git a/README.md b/README.md index 064733d3bf7..588c1fb843b 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,11 @@ cmake --preset release-windows-mingw -DCMAKE_PREFIX_PATH=C:/deps/mingw cmake --build --preset release-windows-mingw --parallel ``` +The MinGW configuration creates compatibility copies under the build directory +(`mingw-compat/util`); imported files in the checkout are never rewritten. +CMake stops with an explicit error if an import changes a source pattern that +the compatibility adaptation expects. + If your dependency prefix contains OpenSSL 4.x, use a separate OpenSSL 3.x prefix and pass `-DOPENSSL_ROOT_DIR=C:/deps/openssl3`. diff --git a/cmake/common.cmake b/cmake/common.cmake index 4da5de9605e..fa737465dab 100644 --- a/cmake/common.cmake +++ b/cmake/common.cmake @@ -234,6 +234,10 @@ function(_ydb_sdk_add_library Tgt) endif() add_library(${Tgt} ${libraryMode}) + if(MINGW) + target_include_directories(${Tgt} ${includeMode} + $) + endif() target_include_directories(${Tgt} ${includeMode} $ $ @@ -282,6 +286,9 @@ function(_ydb_sdk_apply_aklomp_base64_simd_file_flags BASE64_SRC_ROOT) endfunction() function(_ydb_sdk_apply_source_compile_opts src opts) + if(MINGW) + string(REPLACE "${YDB_SDK_SOURCE_DIR}/util/" "${YDB_SDK_MINGW_COMPAT_DIR}/util/" opts "${opts}") + endif() if (NOT opts) return() endif() diff --git a/cmake/global_flags.cmake b/cmake/global_flags.cmake index b2d689983b5..e54081ad6d8 100644 --- a/cmake/global_flags.cmake +++ b/cmake/global_flags.cmake @@ -5,6 +5,7 @@ set(flagPrefixSymbol "-") if(MINGW) add_compile_definitions(NOMINMAX WIN32_LEAN_AND_MEAN) + include("${CMAKE_CURRENT_LIST_DIR}/mingw_compat.cmake") endif() string(APPEND ${COMMON_C_CXX_FLAGS} "\ diff --git a/cmake/mingw_compat.cmake b/cmake/mingw_compat.cmake new file mode 100644 index 00000000000..965223fb629 --- /dev/null +++ b/cmake/mingw_compat.cmake @@ -0,0 +1,123 @@ +# Adapt build-tree copies only; never rewrite the imported source checkout. +set(YDB_SDK_MINGW_COMPAT_DIR "${YDB_SDK_BINARY_DIR}/mingw-compat") +set(_mingw_adapted_files + util/system/compat.h util/system/compat.cpp util/system/yassert.h + util/system/cpu_id.cpp util/system/thread.cpp + util/datetime/systime.h util/datetime/systime.cpp util/folder/dirent_win.c) + +# Copy the complete util include hierarchy so quoted relative includes and +# angle-bracket includes resolve to the same files (including pragma once). +file(GLOB_RECURSE _mingw_util_files CONFIGURE_DEPENDS + RELATIVE "${YDB_SDK_SOURCE_DIR}" "${YDB_SDK_SOURCE_DIR}/util/*") +foreach(_path IN LISTS _mingw_util_files) + if(NOT _path IN_LIST _mingw_adapted_files) + configure_file("${YDB_SDK_SOURCE_DIR}/${_path}" + "${YDB_SDK_MINGW_COMPAT_DIR}/${_path}" COPYONLY) + endif() +endforeach() + +macro(_ydb_sdk_mingw_replace Before After) + string(FIND "${_text}" "${Before}" _match) + if(_match EQUAL -1) + message(FATAL_ERROR "MinGW adaptation no longer matches ${_path}; review the imported source") + endif() + string(REPLACE "${Before}" "${After}" _text "${_text}") +endmacro() + +foreach(_path IN LISTS _mingw_adapted_files) + set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${YDB_SDK_SOURCE_DIR}/${_path}") + file(READ "${YDB_SDK_SOURCE_DIR}/${_path}" _text) + string(REPLACE "\r\n" "\n" _text "${_text}") + if(_path STREQUAL "util/system/compat.h") + _ydb_sdk_mingw_replace("#if defined(_unix_)" "#if defined(_unix_) || defined(__MINGW32__)") + _ydb_sdk_mingw_replace(" using pid_t = int;" "") + _ydb_sdk_mingw_replace("#if defined(_MSC_VER)" "#if defined(_win_)") + _ydb_sdk_mingw_replace("#ifdef _win_\n\n #ifdef sleep" "#if defined(_win_) && !defined(__MINGW32__)\n\n #ifdef sleep") + _ydb_sdk_mingw_replace("#ifdef _win_\nint ftruncate" "#if defined(_win_) && !defined(__MINGW32__)\nint ftruncate") + elseif(_path STREQUAL "util/system/compat.cpp") + _ydb_sdk_mingw_replace("#ifdef _win_\n\nvoid sleep" "#if defined(_win_) && !defined(__MINGW32__)\n\nvoid sleep") + elseif(_path STREQUAL "util/system/yassert.h") + _ydb_sdk_mingw_replace("#if !defined(_MSC_VER)" "#if !defined(_MSC_VER) && !defined(__MINGW32__)") + elseif(_path STREQUAL "util/system/thread.cpp") + _ydb_sdk_mingw_replace("#elif defined(_MSC_VER)" "#elif defined(_win_)") + elseif(_path STREQUAL "util/system/cpu_id.cpp") + _ydb_sdk_mingw_replace("#if defined(_x86_) && !defined(_win_)" "#if defined(_x86_)") + _ydb_sdk_mingw_replace("_xgetbv(" "YdbSdkReadXcr(") + _ydb_sdk_mingw_replace("#if defined(_MSC_VER)" "#if defined(_win_)") + elseif(_path STREQUAL "util/datetime/systime.h") + _ydb_sdk_mingw_replace("int gettimeofday(struct timeval* tp, void*);" "#include ") + elseif(_path STREQUAL "util/datetime/systime.cpp") + _ydb_sdk_mingw_replace("int gettimeofday(timeval* tp, void*) {" "#ifndef __MINGW32__\nint gettimeofday(timeval* tp, void*) {") + _ydb_sdk_mingw_replace("tm* localtime_r(" "#endif\n\ntm* localtime_r(") + elseif(_path STREQUAL "util/folder/dirent_win.c") + _ydb_sdk_mingw_replace(" #include " " #include \n #include ") + _ydb_sdk_mingw_replace(" __acrt_errno_map_os_error(GetLastError());" [=[ // MinGW does not export the private UCRT error mapper. + switch (GetLastError()) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_DRIVE: + case ERROR_BAD_NETPATH: + case ERROR_BAD_NET_NAME: errno = ENOENT; break; + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + case ERROR_LOCK_VIOLATION: errno = EACCES; break; + case ERROR_NOT_ENOUGH_MEMORY: + case ERROR_OUTOFMEMORY: errno = ENOMEM; break; + case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break; + case ERROR_DIRECTORY: errno = ENOTDIR; break; + case ERROR_FILENAME_EXCED_RANGE: errno = ENAMETOOLONG; break; + default: errno = EINVAL; break; + }]=]) + endif() + set(_output "${YDB_SDK_MINGW_COMPAT_DIR}/${_path}") + set(_previous "") + if(EXISTS "${_output}") + file(READ "${_output}" _previous) + endif() + if(NOT _previous STREQUAL _text) + get_filename_component(_directory "${_output}" DIRECTORY) + file(MAKE_DIRECTORY "${_directory}") + file(WRITE "${_output}" "${_text}") + endif() +endforeach() + + +function(_ydb_sdk_mingw_remap_sources Directory) + get_property(_targets DIRECTORY "${Directory}" PROPERTY BUILDSYSTEM_TARGETS) + foreach(_target IN LISTS _targets) + get_target_property(_type ${_target} TYPE) + if(_type STREQUAL "INTERFACE_LIBRARY" OR _type STREQUAL "UTILITY") + continue() + endif() + foreach(_property IN ITEMS COMPILE_OPTIONS INTERFACE_COMPILE_OPTIONS + INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES) + get_target_property(_value ${_target} ${_property}) + if(_value) + string(REPLACE "${YDB_SDK_SOURCE_DIR}/util/" + "${YDB_SDK_MINGW_COMPAT_DIR}/util/" _value "${_value}") + set_property(TARGET ${_target} PROPERTY ${_property} "${_value}") + endif() + endforeach() + get_target_property(_sources ${_target} SOURCES) + get_target_property(_source_dir ${_target} SOURCE_DIR) + set(_mapped "") + foreach(_source IN LISTS _sources) + if(NOT _source MATCHES "^\\$<") + get_filename_component(_absolute "${_source}" ABSOLUTE BASE_DIR "${_source_dir}") + string(FIND "${_absolute}" "${YDB_SDK_SOURCE_DIR}/util/" _util_prefix) + if(_util_prefix EQUAL 0) + file(RELATIVE_PATH _relative "${YDB_SDK_SOURCE_DIR}" "${_absolute}") + set(_source "${YDB_SDK_MINGW_COMPAT_DIR}/${_relative}") + endif() + endif() + list(APPEND _mapped "${_source}") + endforeach() + if(_sources) + set_property(TARGET ${_target} PROPERTY SOURCES "${_mapped}") + endif() + endforeach() + get_property(_subdirs DIRECTORY "${Directory}" PROPERTY SUBDIRECTORIES) + foreach(_subdir IN LISTS _subdirs) + _ydb_sdk_mingw_remap_sources("${_subdir}") + endforeach() +endfunction() diff --git a/cmake/windows.cmake b/cmake/windows.cmake index 5f92f8b2fe1..bb04c0c1e29 100644 --- a/cmake/windows.cmake +++ b/cmake/windows.cmake @@ -16,3 +16,10 @@ if(TARGET yutil) target_compile_definitions(yutil PUBLIC ARCADIA_ROOT=${_source_root} ARCADIA_BUILD_ROOT=${_build_root}) endif() + +_ydb_sdk_mingw_remap_sources("${YDB_SDK_SOURCE_DIR}") +if(TARGET yt-memory) + # GCC 16 requires the declaration before shared_range templates use New. + target_compile_options(yt-memory PRIVATE + -include "${YDB_SDK_SOURCE_DIR}/library/cpp/yt/memory/new.h") +endif()