Skip to content

Commit 8f550ff

Browse files
committed
[libc] Add osutils for Windows and make Windows tests build
This patch first adds osutils for Windows targets to make Windows tests build. It then disables certain tests on Windows that does not pass due to implementation issues.
1 parent 7616145 commit 8f550ff

File tree

17 files changed

+115
-11
lines changed

17 files changed

+115
-11
lines changed

libc/include/llvm-libc-macros/float16-macros.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
#if defined(__FLT16_MANT_DIG__) && \
1515
(!defined(__GNUC__) || __GNUC__ >= 13 || defined(__clang__)) && \
16-
!defined(__arm__) && !defined(_M_ARM) && !defined(__riscv)
16+
!defined(__arm__) && !defined(_M_ARM) && !defined(__riscv) && \
17+
!defined(_WIN32)
1718
#define LIBC_TYPES_HAS_FLOAT16
1819

1920
// TODO: This would no longer be required if HdrGen let us guard function

libc/include/llvm-libc-macros/stdckdint-macros.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#define LLVM_LIBC_MACROS_STDCKDINT_MACROS_H
1111

1212
// We need to use __builtin_*_overflow from GCC/Clang to implement the overflow
13-
// macros. Check __GNUC__ for availability of such builtins.
14-
#ifdef __GNUC__
13+
// macros. Check __GNUC__ or __clang__ for availability of such builtins.
14+
// Note that clang-cl defines __clang__ only and does not define __GNUC__ so we
15+
// have to check for both.
16+
#if defined(__GNUC__) || defined(__clang__)
1517
// clang/gcc overlay may provides similar macros, we need to avoid redefining
1618
// them.
1719
#ifndef __STDC_VERSION_STDCKDINT_H__

libc/src/__support/CPP/new.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,29 @@ void operator delete(void *mem, std::align_val_t) noexcept { ::free(mem); }
1616
void operator delete(void *mem, size_t) noexcept { ::free(mem); }
1717

1818
void operator delete(void *mem, size_t, std::align_val_t) noexcept {
19+
#ifdef _WIN32
20+
::_aligned_free(mem);
21+
#else
1922
::free(mem);
23+
#endif
2024
}
2125

2226
void operator delete[](void *mem) noexcept { ::free(mem); }
2327

24-
void operator delete[](void *mem, std::align_val_t) noexcept { ::free(mem); }
28+
void operator delete[](void *mem, std::align_val_t) noexcept {
29+
#ifdef _WIN32
30+
::_aligned_free(mem);
31+
#else
32+
::free(mem);
33+
#endif
34+
}
2535

2636
void operator delete[](void *mem, size_t) noexcept { ::free(mem); }
2737

2838
void operator delete[](void *mem, size_t, std::align_val_t) noexcept {
39+
#ifdef _WIN32
40+
::_aligned_free(mem);
41+
#else
2942
::free(mem);
43+
#endif
3044
}

libc/src/__support/CPP/new.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ class AllocChecker {
4747

4848
LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align,
4949
AllocChecker &ac) {
50+
#ifdef _WIN32
51+
// std::aligned_alloc is not available on Windows because std::free on
52+
// Windows cannot deallocate any over-aligned memory. Microsoft provides an
53+
// alternative for std::aligned_alloc named _aligned_malloc, but it must be
54+
// paired with _aligned_free instead of std::free.
55+
void *mem = ::_aligned_malloc(static_cast<size_t>(align), s);
56+
#else
5057
void *mem = ::aligned_alloc(static_cast<size_t>(align), s);
58+
#endif
5159
ac = (mem != nullptr);
5260
return mem;
5361
}

libc/src/__support/OSUtil/io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "linux/io.h"
2020
#elif defined(__Fuchsia__)
2121
#include "fuchsia/io.h"
22+
#elif defined(_WIN32)
23+
#include "windows/io.h"
2224
#elif defined(__ELF__)
2325
// TODO: Ideally we would have LIBC_TARGET_OS_IS_BAREMETAL.
2426
#include "baremetal/io.h"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_object_library(
2+
windows_util
3+
SRCS
4+
exit.cpp
5+
io.cpp
6+
HDRS
7+
io.h
8+
DEPENDS
9+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------- Windows implementation of an exit function -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/macros/config.h"
10+
11+
#include <Windows.h>
12+
13+
namespace LIBC_NAMESPACE_DECL {
14+
namespace internal {
15+
16+
__attribute__((noreturn)) void exit(int status) {
17+
ExitProcess(status);
18+
}
19+
20+
} // namespace internal
21+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===------------- Windows implementation of IO utils -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/macros/config.h"
10+
#include "io.h"
11+
12+
#include <Windows.h>
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
void write_to_stderr(cpp::string_view msg) {
17+
HANDLE stream = GetStdHandle(STD_ERROR_HANDLE);
18+
WriteFile(stream, msg.data(), msg.size(), nullptr, nullptr);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===------------- Windows implementation of IO utils -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_WINDOWS_IO_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_WINDOWS_IO_H
11+
12+
#include "src/__support/CPP/string_view.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
void write_to_stderr(cpp::string_view msg);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_WINDOWS_IO_H

libc/src/__support/macros/properties/types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#endif // UINT64_MAX
3636

3737
// int128 / uint128 support
38-
#if defined(__SIZEOF_INT128__)
38+
#if defined(__SIZEOF_INT128__) && !defined(_WIN32)
3939
#define LIBC_TYPES_HAS_INT128
4040
#endif // defined(__SIZEOF_INT128__)
4141

libc/test/src/__support/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ add_libc_test(
142142
libc.src.__support.arg_list
143143
)
144144

145-
if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
145+
if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX AND NOT LIBC_TARGET_OS_IS_WINDOWS)
146146
add_libc_test(
147147
big_int_test
148148
SUITE

libc/test/src/__support/FPUtil/fpbits_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,9 @@ TEST(LlvmLibcFPBitsTest, DoubleType) {
427427

428428
#ifdef LIBC_TARGET_ARCH_IS_X86
429429
TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {
430+
#ifndef LIBC_TYPES_LONG_DOUBLE_IS_FLOAT64
430431
using LongDoubleBits = FPBits<long double>;
431432

432-
if constexpr (sizeof(long double) == sizeof(double))
433-
return; // The tests for the "double" type cover for this case.
434-
435433
EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::POS)).c_str(),
436434
"(+Infinity)");
437435
EXPECT_STREQ(LIBC_NAMESPACE::str(LongDoubleBits::inf(Sign::NEG)).c_str(),
@@ -501,6 +499,7 @@ TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {
501499

502500
LongDoubleBits quiet_nan = LongDoubleBits::quiet_nan();
503501
EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
502+
#endif
504503
}
505504
#else
506505
TEST(LlvmLibcFPBitsTest, LongDoubleType) {

libc/test/src/__support/arg_list_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ TEST(LlvmLibcArgListTest, TestStructTypes) {
120120
}
121121

122122
// Test vector extensions from clang.
123-
#if __has_attribute(ext_vector_type)
123+
#if !defined(_WIN32) && __has_attribute(ext_vector_type)
124124

125125
using int1 = int __attribute__((ext_vector_type(1)));
126126
using int2 = int __attribute__((ext_vector_type(2)));

libc/test/src/fenv/getenv_and_setenv_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
using LlvmLibcFEnvTest = LIBC_NAMESPACE::testing::FEnvSafeTest;
2222

23+
#ifndef _WIN32
2324
TEST_F(LlvmLibcFEnvTest, GetEnvAndSetEnv) {
2425
// We will disable all exceptions to prevent invocation of the exception
2526
// handler.
@@ -71,6 +72,7 @@ TEST_F(LlvmLibcFEnvTest, Set_FE_DFL_ENV) {
7172
int rm = LIBC_NAMESPACE::fegetround();
7273
EXPECT_EQ(rm, FE_TONEAREST);
7374
}
75+
#endif
7476

7577
#ifdef _WIN32
7678
TEST_F(LlvmLibcFEnvTest, Windows_Set_Get_Test) {

libc/test/src/math/smoke/AddTest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
5353
}
5454

5555
void test_range_errors(AddFunc func) {
56+
#ifndef _WIN32
5657
using namespace LIBC_NAMESPACE::fputil::testing;
5758

5859
if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
@@ -121,6 +122,7 @@ class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
121122
FE_UNDERFLOW | FE_INEXACT);
122123
EXPECT_MATH_ERRNO(ERANGE);
123124
}
125+
#endif
124126
}
125127

126128
void test_inexact_results(AddFunc func) {

libc/test/src/math/smoke/SubTest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
5252
}
5353

5454
void test_range_errors(SubFunc func) {
55+
#ifndef _WIN32
5556
using namespace LIBC_NAMESPACE::fputil::testing;
5657

5758
if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
@@ -123,6 +124,7 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
123124
FE_UNDERFLOW | FE_INEXACT);
124125
EXPECT_MATH_ERRNO(ERANGE);
125126
}
127+
#endif
126128
}
127129

128130
void test_inexact_results(SubFunc func) {

libc/test/src/string/memory_utils/op_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ TYPED_TEST(LlvmLibcOpTest, Bcmp, BcmpImplementations) {
294294
#endif // LIBC_TARGET_ARCH_IS_X86_64
295295

296296
using MemcmpImplementations = testing::TypeList<
297-
#ifdef LIBC_TARGET_ARCH_IS_X86_64
297+
#if defined(LIBC_TARGET_ARCH_IS_X86_64) && !defined(LIBC_TARGET_OS_IS_WINDOWS)
298298
#ifdef __SSE2__
299299
generic::Memcmp<__m128i>, //
300300
#endif

0 commit comments

Comments
 (0)