Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zig it #340

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions file-events/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.zig-cache/
zig-out/
73 changes: 73 additions & 0 deletions file-events/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const lib = b.addSharedLibrary(.{ .name = "file-events", .target = target, .optimize = optimize });

const env = std.process.getEnvMap(b.allocator) catch unreachable;
const java_home = env.get("JAVA_HOME") orelse unreachable;
const java_include_path = std.fmt.allocPrint(b.allocator, "{s}/include", .{java_home}) catch unreachable;
const java_darwin_include_path = std.fmt.allocPrint(b.allocator, "{s}/include/darwin", .{java_home}) catch unreachable;

// Add include directories
lib.addIncludePath(b.path("build/generated/sources/headers/java/main"));
lib.addIncludePath(b.path("build/generated/version/header"));
lib.addIncludePath(b.path("src/file-events/headers"));
lib.addSystemIncludePath(.{ .cwd_relative = java_include_path });
lib.addSystemIncludePath(.{ .cwd_relative = java_darwin_include_path });

const base_cpp_args = &[_][]const u8{
"--std=c++17",
"-g",
"-pedantic",
"-Wall",
"-Wextra",
"-Wformat=2",
"-Werror",
"-Wno-deprecated-declarations",
"-Wno-format-nonliteral",
"-Wno-unguarded-availability-new",
};

const cpp_args = if (target.result.os.tag == .windows)
base_cpp_args ++ &[_][]const u8{
"-DNTDDI_VERSION=NTDDI_WIN10_RS3",
}
else
base_cpp_args;

// Add source files
lib.addCSourceFiles(.{
.files = &.{
"src/file-events/cpp/apple_fsnotifier.cpp",
"src/file-events/cpp/file-events-version.cpp",
"src/file-events/cpp/generic_fsnotifier.cpp",
"src/file-events/cpp/jni_support.cpp",
"src/file-events/cpp/linux_fsnotifier.cpp",
"src/file-events/cpp/logging.cpp",
"src/file-events/cpp/services.cpp",
"src/file-events/cpp/win_fsnotifier.cpp",
},
.flags = cpp_args,
});

// Link against libc and libstdc++
lib.linkLibC();
lib.linkLibCpp();

if (target.result.os.tag == .macos) {
lib.linkFramework("CoreFoundation");
lib.linkFramework("CoreServices");
}

// lib.verbose_cc = true;
// lib.verbose_link = true;

const install = b.addInstallArtifact(lib, .{});

// Ensure the library is built
const build_step = b.step("build", "Build the file-events shared library");
build_step.dependOn(&install.step);
}
10 changes: 5 additions & 5 deletions file-events/src/file-events/cpp/win_fsnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ bool resolveFinalPath(HANDLE handle, wstring& path) {
//

WatchPoint::WatchPoint(Server* server, size_t eventBufferSize, const wstring& path)
: registeredPath(path)
, status(WatchPointStatus::NOT_LISTENING)
, server(server) {
: server(server)
, registeredPath(path)
, status(WatchPointStatus::NOT_LISTENING) {
wstring longPath = path;
convertToLongPathIfNeeded(longPath);
HANDLE directoryHandle = CreateFileW(
longPath.c_str(), // pointer to the file name
longPath.c_str(), // pointer to the file name
FILE_LIST_DIRECTORY, // access (read/write) mode
CREATE_SHARE, // share mode
NULL, // security descriptor
Expand Down Expand Up @@ -477,7 +477,7 @@ void Server::stopWatchingMovedPaths(jobject droppedPaths) {
//

JNIEXPORT jobject JNICALL
Java_net_rubygrapefruit_platform_internal_jni_WindowsFileEventFunctions_startWatcher0(JNIEnv* env, jclass target, jint eventBufferSize, jlong commandTimeoutInMillis, jobject javaCallback) {
Java_net_rubygrapefruit_platform_internal_jni_WindowsFileEventFunctions_startWatcher0(JNIEnv* env, jclass, jint eventBufferSize, jlong commandTimeoutInMillis, jobject javaCallback) {
return wrapServer(env, new Server(env, eventBufferSize, (long) commandTimeoutInMillis, javaCallback));
}

Expand Down
7 changes: 2 additions & 5 deletions file-events/src/file-events/headers/win_fsnotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#ifdef _WIN32

#include <Shlwapi.h>
#include <functional>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -75,6 +74,8 @@ class WatchPoint {
ListenResult listen();
bool cancel();

void handleEventsInBuffer(DWORD errorCode, DWORD bytesTransferred);

private:
bool isValidDirectory();
void close();
Expand Down Expand Up @@ -120,9 +121,6 @@ class WatchPoint {
* Whether the watch point is watching, has been cancelled or fully closed.
*/
WatchPointStatus status;

void handleEventsInBuffer(DWORD errorCode, DWORD bytesTransferred);
friend static void CALLBACK handleEventCallback(DWORD errorCode, DWORD bytesTransferred, LPOVERLAPPED overlapped);
};

class Server : public AbstractServer {
Expand Down Expand Up @@ -156,7 +154,6 @@ class Server : public AbstractServer {
const long commandTimeoutInMillis;
unordered_map<wstring, WatchPoint> watchPoints;
bool shouldTerminate = false;
friend void CALLBACK executeOnRunLoopCallback(_In_ ULONG_PTR info);
jmethodID listAddMethod;
};

Expand Down
Loading