From 18257604ebffbac2dca2e802573dd219836c4be0 Mon Sep 17 00:00:00 2001 From: Diego Iastrubni Date: Tue, 20 Sep 2022 10:15:12 +0300 Subject: [PATCH] Lagom: Win32 support baby steps This is the initial port of Lagom to win32. This will enable developers to use Lagom as an alternative to vanilla STL/StandardC++Library - which gives a much richer environment (think QtCore - but modern). My main incentive - is to have a native Windows Ladybird working. I am starting with AK, which does not yet fully compile (on mingw). When AK is compiling (currently fails building StringBuffer.cpp) - I will continue to LibCore and then the rest of the user space libraries (excluding the GUI, which will be another different effort). Most of the code is happily stollen from Andrew Kaster's fork - he deserves the credit. Co-authored-by: Andrew Kaster --- AK/Platform.h | 10 +++++++++- AK/Random.h | 10 ++++++++++ AK/Singleton.h | 6 ++++++ AK/Time.cpp | 5 ++++- AK/Time.h | 4 ++++ AK/Types.h | 4 ++++ Meta/Lagom/CMakeLists.txt | 6 ++++++ 7 files changed, 43 insertions(+), 2 deletions(-) diff --git a/AK/Platform.h b/AK/Platform.h index 14a81609080..1a4e8a2620c 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -33,6 +33,10 @@ # define AK_OS_BSD_GENERIC #endif +#if defined(_WIN32) || defined(_WIN64) +# define AK_OS_WINDOWS +#endif + // FIXME: Remove clang-format suppression after https://github.com/llvm/llvm-project/issues/56602 resolved // clang-format off #if defined(__ANDROID__) @@ -127,7 +131,11 @@ extern "C" { # endif #endif -#ifdef AK_OS_BSD_GENERIC +#if defined(AK_OS_WINDOWS) +# define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC +#endif + +#if defined(AK_OS_BSD_GENERIC) # define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC # define CLOCK_REALTIME_COARSE CLOCK_REALTIME #endif diff --git a/AK/Random.h b/AK/Random.h index fe5961c3fa1..4767c93e5a2 100644 --- a/AK/Random.h +++ b/AK/Random.h @@ -21,6 +21,11 @@ # include #endif +#if defined(AK_OS_WINDOWS) +# include +# include +#endif + namespace AK { inline void fill_with_random([[maybe_unused]] void* buffer, [[maybe_unused]] size_t length) @@ -30,6 +35,11 @@ inline void fill_with_random([[maybe_unused]] void* buffer, [[maybe_unused]] siz #elif defined(OSS_FUZZ) #elif defined(__unix__) or defined(AK_OS_MACOS) [[maybe_unused]] int rc = getentropy(buffer, length); +#else + char* char_buffer = static_cast(buffer); + for (size_t i = 0; i < length; i++) { + char_buffer[i] = std::rand(); + } #endif } diff --git a/AK/Singleton.h b/AK/Singleton.h index a0c6fa32902..c74b3324697 100644 --- a/AK/Singleton.h +++ b/AK/Singleton.h @@ -13,6 +13,12 @@ # include # include # include +#elif defined(AK_OS_WINDOWS) +// Forward declare to avoid pulling Windows.h into every file in existence. +extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long); +# ifndef sched_yield +# define sched_yield() Sleep(0) +# endif #else # include #endif diff --git a/AK/Time.cpp b/AK/Time.cpp index bb01b1e9070..6eb9d2bdedb 100644 --- a/AK/Time.cpp +++ b/AK/Time.cpp @@ -179,7 +179,10 @@ timespec Time::to_timespec() const timeval Time::to_timeval() const { VERIFY(m_nanoseconds < 1'000'000'000); - return { static_cast(m_seconds), static_cast(m_nanoseconds) / 1000 }; + // This is done because winsock defines tv_sec and tv_usec as long, and Linux64 as long int. + using sec_type = decltype(declval().tv_sec); + using usec_type = decltype(declval().tv_usec); + return { static_cast(m_seconds), static_cast(m_nanoseconds) / 1000 }; } Time Time::operator+(Time const& other) const diff --git a/AK/Time.h b/AK/Time.h index d64e1b9ac5a..8e5d128269d 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -16,6 +16,10 @@ struct timeval; struct timespec; +#if defined(AK_OS_WINDOWS) +# include +#endif + namespace AK { // Concept to detect types which look like timespec without requiring the type. diff --git a/AK/Types.h b/AK/Types.h index 7e8100cc46f..06e35d4d9cb 100644 --- a/AK/Types.h +++ b/AK/Types.h @@ -50,6 +50,10 @@ using pid_t = int; using __ptrdiff_t = __PTRDIFF_TYPE__; # endif +# if defined(AK_OS_WINDOWS) +using ssize_t = MakeSigned; +using mode_t = unsigned short; +# endif #endif using FlatPtr = Conditional; diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index e680c708a9f..4635207a5b3 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -265,6 +265,12 @@ if (ANDROID) list(REMOVE_ITEM LIBCORE_SOURCES "${CMAKE_CURRENT_LIST_DIR}/../../Userland/Libraries/LibCore/Account.cpp") list(REMOVE_ITEM LIBCORE_SOURCES "${CMAKE_CURRENT_LIST_DIR}/../../Userland/Libraries/LibCore/LocalServer.cpp") endif() +if (WIN32) + list(REMOVE_ITEM LIBCORE_SOURCES "${CMAKE_CURRENT_LIST_DIR}/../../Userland/Libraries/LibCore/Account.cpp") + list(REMOVE_ITEM LIBCORE_SOURCES "${CMAKE_CURRENT_LIST_DIR}/../../Userland/Libraries/LibCore/FilePermissionsMask.cpp") + list(REMOVE_ITEM LIBCORE_SOURCES "${CMAKE_CURRENT_LIST_DIR}/../../Userland/Libraries/LibCore/Group.cpp") + list(REMOVE_ITEM LIBCORE_SOURCES "${CMAKE_CURRENT_LIST_DIR}/../../Userland/Libraries/LibCore/GetPassword.cpp") +endif() lagom_lib(Core core SOURCES ${AK_SOURCES} ${LIBCORE_SOURCES} LIBS Threads::Threads