windows build fixes

This commit is contained in:
CJ van den Berg 2024-06-05 20:32:30 +02:00
parent 3121af2ec4
commit 1d116f8fa8
15 changed files with 137 additions and 31 deletions

View file

@ -3,6 +3,7 @@ const std = @import("std");
const CrossTarget = std.zig.CrossTarget; const CrossTarget = std.zig.CrossTarget;
const cppflags = [_][]const u8{ const cppflags = [_][]const u8{
"-DASIO_HAS_THREADS",
"-fcolor-diagnostics", "-fcolor-diagnostics",
"-std=c++20", "-std=c++20",
"-Wall", "-Wall",
@ -65,6 +66,10 @@ pub fn build(b: *std.Build) void {
lib.linkLibrary(tracy_dep.artifact("tracy")); lib.linkLibrary(tracy_dep.artifact("tracy"));
} }
lib.linkLibrary(asio_dep.artifact("asio")); lib.linkLibrary(asio_dep.artifact("asio"));
if (lib.rootModuleTarget().os.tag == .windows) {
lib.linkSystemLibrary("mswsock");
lib.linkSystemLibrary("ws2_32");
}
lib.linkLibCpp(); lib.linkLibCpp();
b.installArtifact(lib); b.installArtifact(lib);

View file

@ -3,7 +3,12 @@
#include "cbor.hpp" #include "cbor.hpp"
#include <cstring> #include <cstring>
#if !defined(_WIN32)
#include <netinet/in.h> #include <netinet/in.h>
#else
#include <in6addr.h>
#endif
namespace cbor { namespace cbor {

View file

@ -1,10 +1,15 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <netinet/in.h>
#include <string_view> #include <string_view>
#include <vector> #include <vector>
#if !defined(_WIN32)
#include <netinet/in.h>
#else
#include <in6addr.h>
#endif
using port_t = unsigned short; using port_t = unsigned short;
namespace thespian::tcp { namespace thespian::tcp {

View file

@ -1,4 +1,4 @@
#ifndef __APPLE__ #if !defined(_WIN32) && !defined(__APPLE__)
#include <csignal> #include <csignal>
#include <cstdio> #include <cstdio>

View file

@ -5,10 +5,15 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <netinet/in.h>
#include <system_error> #include <system_error>
#include <vector> #include <vector>
#if !defined(_WIN32)
#include <netinet/in.h>
#else
#include <in6addr.h>
#endif
using port_t = unsigned short; using port_t = unsigned short;
namespace thespian::executor { namespace thespian::executor {

View file

@ -1,4 +1,3 @@
#include "asio/error_code.hpp"
#include "asio/posix/descriptor_base.hpp" #include "asio/posix/descriptor_base.hpp"
#include "executor.hpp" #include "executor.hpp"
#include <cstddef> #include <cstddef>
@ -11,13 +10,19 @@
#include <asio/ip/tcp.hpp> #include <asio/ip/tcp.hpp>
#include <asio/ip/udp.hpp> #include <asio/ip/udp.hpp>
#include <asio/local/stream_protocol.hpp> #include <asio/local/stream_protocol.hpp>
#include <asio/posix/stream_descriptor.hpp>
#include <asio/signal_set.hpp> #include <asio/signal_set.hpp>
#include <asio/socket_base.hpp> #include <asio/socket_base.hpp>
#include <asio/strand.hpp> #include <asio/strand.hpp>
#include <asio/system_timer.hpp> #include <asio/system_timer.hpp>
#include <asio/thread.hpp> #include <asio/thread.hpp>
#if !defined(_WIN32)
#include <asio/posix/stream_descriptor.hpp>
#else
#include <asio/windows/stream_handle.hpp>
#include <windows.h>
#endif
#include <csignal> #include <csignal>
#include <vector> #include <vector>
@ -27,7 +32,6 @@ using asio::io_context;
using asio::string_view; using asio::string_view;
using asio::system_timer; using asio::system_timer;
using asio::thread; using asio::thread;
using asio::posix::stream_descriptor;
using std::atomic; using std::atomic;
using std::error_code; using std::error_code;
using std::function; using std::function;
@ -46,13 +50,30 @@ using std::chrono::microseconds;
using std::chrono::system_clock; using std::chrono::system_clock;
using std::chrono::time_point; using std::chrono::time_point;
#if !defined(_WIN32)
using asio::posix::stream_descriptor;
#else
using asio::windows::stream_handle;
#endif
namespace thespian::executor { namespace thespian::executor {
const char *MAX_THREAD_STR = getenv("MAX_THREAD"); // NOLINT const char *MAX_THREAD_STR = getenv("MAX_THREAD"); // NOLINT
const auto MAX_THREAD = const auto MAX_THREAD =
static_cast<long>(atoi(MAX_THREAD_STR ? MAX_THREAD_STR : "64")); // NOLINT static_cast<long>(atoi(MAX_THREAD_STR ? MAX_THREAD_STR : "64")); // NOLINT
#if !defined(_WIN32)
const auto threads = min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREAD); const auto threads = min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREAD);
#else
namespace {
static long get_num_processors() {
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwNumberOfProcessors;
}
} // namespace
const auto threads = min(get_num_processors(), MAX_THREAD);
#endif
struct context_impl { struct context_impl {
context_impl() : asio{make_unique<io_context>(threads)} {} context_impl() : asio{make_unique<io_context>(threads)} {}
@ -62,8 +83,10 @@ struct context_impl {
}; };
auto context::create() -> context { auto context::create() -> context {
#if !defined(_WIN32)
if (::signal(SIGPIPE, SIG_IGN) == SIG_ERR) // NOLINT if (::signal(SIGPIPE, SIG_IGN) == SIG_ERR) // NOLINT
abort(); abort();
#endif
return {context_ref(new context_impl(), [](context_impl *p) { delete p; })}; return {context_ref(new context_impl(), [](context_impl *p) { delete p; })};
} }
@ -225,7 +248,7 @@ struct socket_impl {
socket_{*ctx->asio, ::asio::ip::udp::v6()} {} socket_{*ctx->asio, ::asio::ip::udp::v6()} {}
auto bind(const in6_addr &ip, port_t port) -> error_code { auto bind(const in6_addr &ip, port_t port) -> error_code {
error_code ec; error_code ec;
socket_.bind(to_endpoint_udp(ip, port), ec); ec = socket_.bind(to_endpoint_udp(ip, port), ec);
return ec; return ec;
} }
[[nodiscard]] auto send_to(string_view data, in6_addr ip, port_t port) [[nodiscard]] auto send_to(string_view data, in6_addr ip, port_t port)
@ -305,7 +328,7 @@ struct socket_impl {
socket_{*ctx->asio, asio::ip::tcp::v6(), fd} {} socket_{*ctx->asio, asio::ip::tcp::v6(), fd} {}
auto bind(const in6_addr &ip, port_t port) -> error_code { auto bind(const in6_addr &ip, port_t port) -> error_code {
error_code ec; error_code ec;
socket_.bind(to_endpoint_tcp(ip, port), ec); ec = socket_.bind(to_endpoint_tcp(ip, port), ec);
return ec; return ec;
} }
void connect(const in6_addr &ip, port_t port, socket::connect_handler h) { void connect(const in6_addr &ip, port_t port, socket::connect_handler h) {
@ -382,7 +405,9 @@ void socket::write(const vector<uint8_t> &data, write_handler h) {
void socket::read(read_handler h) { ref->read(*this, move(h)); } void socket::read(read_handler h) { ref->read(*this, move(h)); }
void socket::close() { ref->close(); } void socket::close() { ref->close(); }
#if !defined(_WIN32)
void socket::close(int fd) { ::close(fd); } void socket::close(int fd) { ::close(fd); }
#endif
auto socket::release() -> int { return ref->release(); } auto socket::release() -> int { return ref->release(); }
auto socket::local_endpoint() const -> const endpoint & { auto socket::local_endpoint() const -> const endpoint & {
@ -395,12 +420,12 @@ struct acceptor_impl {
acceptor_{*ctx->asio, asio::ip::tcp::v6()}, socket_{*ctx->asio} {} acceptor_{*ctx->asio, asio::ip::tcp::v6()}, socket_{*ctx->asio} {}
auto bind(const in6_addr &ip, port_t port) -> error_code { auto bind(const in6_addr &ip, port_t port) -> error_code {
error_code ec; error_code ec;
acceptor_.bind(to_endpoint_tcp(ip, port), ec); ec = acceptor_.bind(to_endpoint_tcp(ip, port), ec);
return ec; return ec;
} }
auto listen() -> error_code { auto listen() -> error_code {
error_code ec; error_code ec;
acceptor_.listen(asio::socket_base::max_listen_connections, ec); ec = acceptor_.listen(asio::socket_base::max_listen_connections, ec);
return ec; return ec;
} }
void accept(acceptor::handler h) { void accept(acceptor::handler h) {
@ -412,7 +437,9 @@ struct acceptor_impl {
t = move(weak_token)](const error_code &ec) { t = move(weak_token)](const error_code &ec) {
c->pending.fetch_sub(1, memory_order_relaxed); c->pending.fetch_sub(1, memory_order_relaxed);
if (auto p = t.lock()) if (auto p = t.lock())
h(ec ? 0 : s->release(), ec); h(ec ? static_cast<asio::ip::tcp::socket::native_handle_type>(0)
: s->release(),
ec);
})); }));
} }
void close() { acceptor_.close(); } void close() { acceptor_.close(); }
@ -454,14 +481,14 @@ namespace unx {
struct socket_impl { struct socket_impl {
explicit socket_impl(strand &strand) explicit socket_impl(strand &strand)
: ctx{strand.ref->ctx}, strand_{strand.ref->strand_}, socket_{ : ctx{strand.ref->ctx}, strand_{strand.ref->strand_},
*ctx->asio} {} socket_{*ctx->asio} {}
explicit socket_impl(strand &strand, int fd) explicit socket_impl(strand &strand, int fd)
: ctx{strand.ref->ctx}, strand_{strand.ref->strand_}, socket_{*ctx->asio, : ctx{strand.ref->ctx}, strand_{strand.ref->strand_},
fd} {} socket_{*ctx->asio, fd} {}
auto bind(string_view path) -> error_code { auto bind(string_view path) -> error_code {
error_code ec; error_code ec;
socket_.bind(path, ec); ec = socket_.bind(path, ec);
return ec; return ec;
} }
void connect(string_view path, socket::connect_handler h) { void connect(string_view path, socket::connect_handler h) {
@ -537,12 +564,12 @@ struct acceptor_impl {
socket_{*ctx->asio} {} socket_{*ctx->asio} {}
auto bind(string_view path) -> error_code { auto bind(string_view path) -> error_code {
error_code ec; error_code ec;
acceptor_.bind(path, ec); ec = acceptor_.bind(path, ec);
return ec; return ec;
} }
auto listen() -> error_code { auto listen() -> error_code {
error_code ec; error_code ec;
acceptor_.listen(asio::socket_base::max_listen_connections, ec); ec = acceptor_.listen(asio::socket_base::max_listen_connections, ec);
return ec; return ec;
} }
void accept(acceptor::handler h) { void accept(acceptor::handler h) {
@ -554,7 +581,10 @@ struct acceptor_impl {
t = move(weak_token)](const error_code &ec) { t = move(weak_token)](const error_code &ec) {
c->pending.fetch_sub(1, memory_order_relaxed); c->pending.fetch_sub(1, memory_order_relaxed);
if (auto p = t.lock()) if (auto p = t.lock())
h(ec ? 0 : s->release(), ec); h(ec ? static_cast<asio::local::stream_protocol::socket::
native_handle_type>(0)
: s->release(),
ec);
})); }));
} }
void close() { acceptor_.close(); } void close() { acceptor_.close(); }
@ -579,12 +609,13 @@ void acceptor::close() { ref->close(); }
} // namespace unx } // namespace unx
#if !defined(_WIN32)
namespace file_descriptor { namespace file_descriptor {
struct watcher_impl { struct watcher_impl {
explicit watcher_impl(strand &strand, int fd) explicit watcher_impl(strand &strand, int fd)
: ctx{strand.ref->ctx}, strand_{strand.ref->strand_}, fd_{*ctx->asio, : ctx{strand.ref->ctx}, strand_{strand.ref->strand_},
fd} {} fd_{*ctx->asio, fd} {}
void wait_read(watcher::handler h) { void wait_read(watcher::handler h) {
if (!read_in_progress_) { if (!read_in_progress_) {
@ -641,5 +672,6 @@ void watcher::wait_write(watcher::handler h) { ref->wait_write(move(h)); }
void watcher::cancel() { ref->cancel(); } void watcher::cancel() { ref->cancel(); }
} // namespace file_descriptor } // namespace file_descriptor
#endif
} // namespace thespian::executor } // namespace thespian::executor

View file

@ -100,7 +100,9 @@ struct hub_impl {
string_view desc; string_view desc;
if (msg("listen", extract(desc))) { if (msg("listen", extract(desc))) {
#if !defined(_WIN32)
thespian::endpoint::unx::listen(desc); thespian::endpoint::unx::listen(desc);
#endif
} }
if (msg("shutdown")) if (msg("shutdown"))

View file

@ -3,7 +3,6 @@
#include <chrono> #include <chrono>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <initializer_list>
#include <memory> #include <memory>
#include <thespian/context.hpp> #include <thespian/context.hpp>
#include <thespian/debug.hpp> #include <thespian/debug.hpp>
@ -26,13 +25,25 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <netinet/in.h>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <system_error> #include <system_error>
#include <utility> #include <utility>
#if !defined(_WIN32)
#include <netinet/in.h>
#else
#include <in6addr.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#endif
#ifdef TRACY_ENABLE #ifdef TRACY_ENABLE
#include <tracy/Tracy.hpp> #include <tracy/Tracy.hpp>
#endif #endif
@ -850,6 +861,7 @@ auto udp::create(string tag) -> udp {
return {udp_ref(new udp_impl(move(tag)), [](udp_impl *p) { delete p; })}; return {udp_ref(new udp_impl(move(tag)), [](udp_impl *p) { delete p; })};
} }
#if !defined(_WIN32)
struct file_descriptor_impl { struct file_descriptor_impl {
file_descriptor_impl(const file_descriptor_impl &) = delete; file_descriptor_impl(const file_descriptor_impl &) = delete;
file_descriptor_impl(file_descriptor_impl &&) = delete; file_descriptor_impl(file_descriptor_impl &&) = delete;
@ -913,6 +925,7 @@ void file_descriptor::wait_write(file_descriptor_impl *p) { p->wait_write(); }
void file_descriptor::wait_read(file_descriptor_impl *p) { p->wait_read(); } void file_descriptor::wait_read(file_descriptor_impl *p) { p->wait_read(); }
void file_descriptor::cancel(file_descriptor_impl *p) { p->cancel(); } void file_descriptor::cancel(file_descriptor_impl *p) { p->cancel(); }
void file_descriptor::destroy(file_descriptor_impl *p) { delete p; } void file_descriptor::destroy(file_descriptor_impl *p) { delete p; }
#endif
struct socket_impl { struct socket_impl {
socket_impl(const socket_impl &) = delete; socket_impl(const socket_impl &) = delete;
@ -1568,6 +1581,7 @@ auto connect(in6_addr ip, port_t port, milliseconds retry_time,
} // namespace tcp } // namespace tcp
#if !defined(_WIN32)
namespace unx { namespace unx {
struct connector { struct connector {
@ -1651,8 +1665,8 @@ struct acceptor {
string listen_path; string listen_path;
acceptor(string_view path, mode m, handle o) acceptor(string_view path, mode m, handle o)
: a{::thespian::unx::acceptor::create(tag)}, owner{move(o)}, listen_path{ : a{::thespian::unx::acceptor::create(tag)}, owner{move(o)},
path} { listen_path{path} {
a.listen(path, m); a.listen(path, m);
} }
~acceptor() = default; ~acceptor() = default;
@ -1703,6 +1717,7 @@ auto connect(string_view path, mode m, milliseconds retry_time,
} }
} // namespace unx } // namespace unx
#endif
} // namespace endpoint } // namespace endpoint
namespace debug { namespace debug {

View file

@ -7,10 +7,16 @@
#include <thespian/tcp.hpp> #include <thespian/tcp.hpp>
#include <thespian/trace.hpp> #include <thespian/trace.hpp>
#include <array>
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#if defined(_WIN32)
#include <winsock2.h>
#include <in6addr.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#endif
using cbor::buffer; using cbor::buffer;
using cbor::extract; using cbor::extract;
using std::make_shared; using std::make_shared;

View file

@ -9,9 +9,15 @@
#include <thespian/tcp.hpp> #include <thespian/tcp.hpp>
#include <cstring> #include <cstring>
#include <map>
#include <utility> #include <utility>
#if defined(_WIN32)
#include <winsock2.h>
#include <in6addr.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#endif
using cbor::any; using cbor::any;
using cbor::buffer; using cbor::buffer;
using cbor::extract; using cbor::extract;

View file

@ -7,7 +7,6 @@
#include <thespian/unx.hpp> #include <thespian/unx.hpp>
#include <cstring> #include <cstring>
#include <map>
#include <sstream> #include <sstream>
#include <unistd.h> #include <unistd.h>
#include <utility> #include <utility>
@ -38,6 +37,7 @@ using thespian::unx::mode;
using namespace std::chrono_literals; using namespace std::chrono_literals;
#if !defined(_WIN32)
namespace { namespace {
struct controller { struct controller {
@ -102,3 +102,12 @@ auto endpoint_unx(context &ctx, bool &result, env_t env_) -> ::result {
}, },
"endpoint_unx", move(env_))); "endpoint_unx", move(env_)));
} }
#else
auto endpoint_unx(context &, bool &result, env_t) -> ::result {
result = true;
return ok();
}
#endif

View file

@ -7,7 +7,14 @@
#include <thespian/socket.hpp> #include <thespian/socket.hpp>
#include <thespian/tcp.hpp> #include <thespian/tcp.hpp>
#if !defined(_WIN32)
#include <netinet/in.h> #include <netinet/in.h>
#else
#include <winsock2.h>
#include <in6addr.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#endif
using cbor::buffer; using cbor::buffer;
using cbor::extract; using cbor::extract;

View file

@ -6,7 +6,14 @@
#include <thespian/timeout.hpp> #include <thespian/timeout.hpp>
#include <thespian/udp.hpp> #include <thespian/udp.hpp>
#if !defined(_WIN32)
#include <netinet/in.h> #include <netinet/in.h>
#else
#include <winsock2.h>
#include <in6addr.h>
#include <ws2ipdef.h>
#include <ws2tcpip.h>
#endif
using cbor::array; using cbor::array;
using cbor::buffer; using cbor::buffer;

View file

@ -16,7 +16,6 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector>
using cbor::buffer; using cbor::buffer;
using std::cerr; using std::cerr;
@ -81,8 +80,10 @@ struct logger {
extern "C" auto runtestcase(const char *name) -> int { extern "C" auto runtestcase(const char *name) -> int {
mutex trace_m; mutex trace_m;
#if !defined(_WIN32)
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) // NOLINT if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) // NOLINT
abort(); abort();
#endif
auto gdb = getenv("JITDEBUG"); // NOLINT auto gdb = getenv("JITDEBUG"); // NOLINT

5
zig
View file

@ -47,10 +47,11 @@ get_zig() {
get_zig get_zig
if [ "$1" == "cdb" ] ; then if [ "$1" == "cdb" ] ; then
rm -rf zig-cache shift
rm -rf .zig-cache
rm -rf .cache/cdb rm -rf .cache/cdb
$ZIG build $ZIG build "$@"
(echo \[ ; cat .cache/cdb/* ; echo {}\]) | perl -0777 -pe 's/,\n\{\}//igs' | jq . | grep -v 'no-default-config' > compile_commands.json (echo \[ ; cat .cache/cdb/* ; echo {}\]) | perl -0777 -pe 's/,\n\{\}//igs' | jq . | grep -v 'no-default-config' > compile_commands.json
exit 0 exit 0