Skip to content
Draft
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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,11 +95,15 @@ 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()

include(cmake/external_libs.cmake)
if(MINGW)
include_directories(BEFORE "${YDB_SDK_MINGW_COMPAT_DIR}")
endif()

if (YDB_SDK_TESTS)
include(cmake/testing.cmake)
Expand Down Expand Up @@ -126,6 +133,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)
Expand Down
11 changes: 11 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -136,6 +143,10 @@
}
],
"buildPresets": [
{
"name": "release-windows-mingw",
"configurePreset": "release-windows-mingw"
},
{
"name": "default",
"displayName": "Default Build",
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- ninja 1.10+
- ragel
- yasm
- OpenSSL
- OpenSSL (the pinned gRPC does not support OpenSSL 4.x)
- Iconv
- IDN

Expand Down Expand Up @@ -44,6 +44,26 @@ 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
```

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`.

### Clone the ydb-cpp-sdk repository

```bash
Expand Down
17 changes: 17 additions & 0 deletions cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${_libc_root}/include/windows>
$<BUILD_INTERFACE:${_libc_root}/queue>)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 14)
Expand Down Expand Up @@ -224,6 +234,10 @@ function(_ydb_sdk_add_library Tgt)
endif()

add_library(${Tgt} ${libraryMode})
if(MINGW)
target_include_directories(${Tgt} ${includeMode}
$<BUILD_INTERFACE:${YDB_SDK_MINGW_COMPAT_DIR}>)
endif()
target_include_directories(${Tgt} ${includeMode}
$<BUILD_INTERFACE:${YDB_SDK_SOURCE_DIR}>
$<BUILD_INTERFACE:${YDB_SDK_BINARY_DIR}>
Expand Down Expand Up @@ -272,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()
Expand Down
5 changes: 5 additions & 0 deletions cmake/external_libs.cmake
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
5 changes: 5 additions & 0 deletions cmake/global_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

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} "\
-fexceptions \
-fno-common \
Expand Down
123 changes: 123 additions & 0 deletions cmake/mingw_compat.cmake
Original file line number Diff line number Diff line change
@@ -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 <sys/time.h>")
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 <stdio.h>" " #include <stdio.h>\n #include <errno.h>")
_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()
25 changes: 25 additions & 0 deletions cmake/windows.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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()

_ydb_sdk_mingw_remap_sources("${YDB_SDK_SOURCE_DIR}")
if(TARGET yt-memory)
# GCC 16 requires the declaration before shared_range templates use New<T>.
target_compile_options(yt-memory PRIVATE
-include "${YDB_SDK_SOURCE_DIR}/library/cpp/yt/memory/new.h")
endif()
Loading