Reactos

[GCC-COMPAT] Add support library for GCC's libsupc++ and libstdc++

__throw_out_of_range_fmt is implemented in libstdc++, but it is needed by libsupc++ and that would mean we would have to link all C++ code to libstdc++.

+97 -2
+2 -2
sdk/cmake/gcc.cmake
··· 579 579 execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libsupc++.a OUTPUT_VARIABLE LIBSUPCXX_LOCATION) 580 580 string(STRIP ${LIBSUPCXX_LOCATION} LIBSUPCXX_LOCATION) 581 581 set_target_properties(libsupc++ PROPERTIES IMPORTED_LOCATION ${LIBSUPCXX_LOCATION}) 582 - # libsupc++ requires libgcc 583 - target_link_libraries(libsupc++ INTERFACE libgcc) 582 + # libsupc++ requires libgcc and stdc++compat 583 + target_link_libraries(libsupc++ INTERFACE libgcc stdc++compat) 584 584 585 585 add_library(libmingwex STATIC IMPORTED) 586 586 execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libmingwex.a OUTPUT_VARIABLE LIBMINGWEX_LOCATION)
+2
sdk/lib/CMakeLists.txt
··· 17 17 if(MSVC) 18 18 add_subdirectory(cpprt) 19 19 add_subdirectory(RunTmChk) 20 + else() 21 + add_subdirectory(gcc-compat) 20 22 endif() 21 23 22 24 add_subdirectory(delayimp)
+15
sdk/lib/gcc-compat/CMakeLists.txt
··· 1 + 2 + include_directories( 3 + ${REACTOS_SOURCE_DIR}/sdk/lib/crt/include) 4 + 5 + list(APPEND SOURCE 6 + __mingw_vsprintf.c 7 + __throw_out_of_range_fmt.cpp 8 + StringCchCopyNA.c) 9 + 10 + add_library(stdc++compat ${SOURCE}) 11 + set_target_cpp_properties(stdc++compat WITH_EXCEPTIONS) 12 + add_dependencies(stdc++compat xdk) 13 + target_link_libraries(stdc++compat libmsvcrt) 14 + remove_target_compile_option(stdc++compat "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<NOT:$<IN_LIST:cppstl,$<TARGET_PROPERTY:LINK_LIBRARIES>>>>:-nostdinc>") 15 + target_compile_definitions(stdc++compat PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:PAL_STDCPP_COMPAT>")
+23
sdk/lib/gcc-compat/StringCchCopyNA.c
··· 1 + /* 2 + * PROJECT: GCC c++ support library 3 + * LICENSE: MIT (https://spdx.org/licenses/MIT) 4 + * PURPOSE: StringCchCopyNA implementation 5 + * COPYRIGHT: Copyright 2024 Timo Kreuzer <timo.kreuzer@reactos.org> 6 + */ 7 + 8 + #include <windows.h> 9 + #define StringCchCopyNA _StringCchCopyNA 10 + #include <strsafe.h> 11 + 12 + #undef StringCchCopyNA 13 + 14 + HRESULT 15 + WINAPI 16 + StringCchCopyNA( 17 + _Out_writes_(cchDest) _Always_(_Post_z_) STRSAFE_LPSTR pszDest, 18 + _In_ size_t cchDest, 19 + _In_reads_or_z_(cchToCopy) STRSAFE_LPCSTR pszSrc, 20 + _In_ size_t cchToCopy) 21 + { 22 + return _StringCchCopyNA(pszDest, cchDest, pszSrc, cchToCopy); 23 + }
+13
sdk/lib/gcc-compat/__mingw_vsprintf.c
··· 1 + /* 2 + * PROJECT: GCC c++ support library 3 + * LICENSE: MIT (https://spdx.org/licenses/MIT) 4 + * PURPOSE: __mingw_vsprintf implementation 5 + * COPYRIGHT: Copyright 2024 Timo Kreuzer <timo.kreuzer@reactos.org> 6 + */ 7 + 8 + #include <stdio.h> 9 + 10 + int __cdecl __mingw_vsprintf(char* dest, const char* format, va_list arglist) 11 + { 12 + return vsprintf(dest, format, arglist); 13 + }
+42
sdk/lib/gcc-compat/__throw_out_of_range_fmt.cpp
··· 1 + /* 2 + * PROJECT: GCC c++ support library 3 + * LICENSE: MIT (https://spdx.org/licenses/MIT) 4 + * PURPOSE: __throw_out_of_range_fmt implementation 5 + * COPYRIGHT: Copyright 2024 Timo Kreuzer <timo.kreuzer@reactos.org> 6 + */ 7 + 8 + #include <stdexcept> 9 + #include <cstdarg> 10 + #include <cstring> 11 + #include <malloc.h> 12 + 13 + namespace std { 14 + 15 + class out_of_range_error : public exception 16 + { 17 + char* m_msg; 18 + public: 19 + explicit out_of_range_error(const char* msg) noexcept 20 + { 21 + size_t len = strlen(msg) + 1; 22 + m_msg = (char*)malloc(len); 23 + strcpy(m_msg, msg); 24 + } 25 + virtual ~out_of_range_error() { free(m_msg); }; 26 + virtual const char* what() const noexcept { return m_msg; } 27 + }; 28 + 29 + void __throw_out_of_range_fmt(const char *format, ...) 30 + { 31 + char buffer[1024]; 32 + va_list argptr; 33 + 34 + va_start(argptr, format); 35 + _vsnprintf(buffer, sizeof(buffer), format, argptr); 36 + buffer[sizeof(buffer) - 1] = 0; 37 + va_end(argptr); 38 + 39 + throw out_of_range_error(buffer); 40 + } 41 + 42 + } // namespace std