8000 Add back in support for windows by jojojames · Pull Request #21 · dangduc/fzf-native · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add back in support for windows #21

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

Merged
merged 5 commits into from
Nov 30, 2024
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions .dir-locals.el < 10000 clipboard-copy data-copy-feedback="Copied!" aria-label="Copy" value=".dir-locals.el" data-view-component="true" class="Link--onHover color-fg-muted ml-2 mr-2">
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
((c-mode . ((flycheck-cppcheck-standards . "c99")
(flycheck-clang-language-standard . "c99")
(flycheck-gcc-language-standard . "c99"))))
((c-mode . ((flycheck-cppcheck-standards . "c11")
(flycheck-clang-language-standard . "c11")
(flycheck-gcc-language-standard . "c11"))))
45 changes: 45 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,58 @@ endif()
# See https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Enable experimental C11 atomics for MSVC
# https://developercommunity.visualstudio.com/t/C11-atomic-types-are-broken-in-CL/10560673?sort=newest
# Needed for #include <stdatomic.h>
if(MSVC)
# Add /experimental:c11atomics for MSVC (C11 atomic operations)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /experimental:c11atomics")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /experimental:c11atomics")

# Ensure the compiler uses C11 standard (MSVC may default to an older standard)
add_compile_options(/std:c11)

# Optionally, ensure that C11 is used for C and C++ separately
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
endif()

# Add pthreads-w32 on Windows
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
# Path to the pthreads-w32 directory
set(PTHREADS_W32_DIR "${CMAKE_SOURCE_DIR}/pthreads-w32-2-9-1-release/Pre-built.2")

# Determine architecture
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(ARCH_DIR "x64")
else()
set(ARCH_DIR "x86")
endif()

# Assume compiler MSVC.
# Link the appropriate library
# Use pthreadVC2.lib for Visual Studio, pthreadGC2.lib for MinGW
# Conditionally select the correct library based on the compiler
if(MSVC)
message(STATUS "Setting ${PTHREADS_W32_DIR}/lib/${ARCH_DIR}/pthreadVC2.lib")
set(PTHREADS_W32_LIB "${PTHREADS_W32_DIR}/lib/${ARCH_DIR}/pthreadVC2.lib")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
message(STATUS "Setting ${PTHREADS_W32_DIR}/lib/${ARCH_DIR}/libpthreadGC2.a")
set(PTHREADS_W32_LIB "${PTHREADS_W32_DIR}/lib/${ARCH_DIR}/libpthreadGC2.a")
else()
message(FATAL_ERROR "Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

add_library(fzf-native-module SHARED fzf-native-module.c fzf.c fzf.h)
# Include the pthreads-w32 directory for Windows
target_include_directories(fzf-native-module PRIVATE ${PTHREADS_W32_DIR}/include)
# Link pthreads to fzf-native-module
target_link_libraries(fzf-native-module PRIVATE ${PTHREADS_W32_LIB})
else()
add_library(fzf-native-module MODULE fzf-native-module.c fzf.c fzf.h)
endif()

# Set library output directory
set(OUTPUT_DIR ${CMAKE_SOURCE_DIR}/${FZF_NATIVE_MODULE_OUTPUT_DIR})
set_target_properties(
fzf-native-module
Expand Down
Binary file modified bin/Linux/fzf-native-module.so
Binary file not shown.
Binary file modified bin/Windows/Release/fzf-native-module.dll
Binary file not shown.
Binary file modified bin/Windows/Release/fzf-native-module.exp
Binary file not shown.
Binary file modified bin/Windows/Release/fzf-native-module.lib
Binary file not shown.
23 changes: 20 additions & 3 deletions fzf-native-module.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
#include <ctype.h>
#include <pthread.h>
#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "emacs-module.h"
#include "fzf.h"
#include <stdatomic.h>
#include <stdio.h>

// Windows pthread.h conflicts with the system Time.
// e.g. 'timespec': 'struct' type redefinition
#ifdef _WIN32
#define _TIMESPEC_DEFINED
#endif
#include <pthread.h>

#if defined(__APPLE__) || defined(__linux__)
// for sysconf(_SC_NPROCESSORS_ONLN);
#include <unistd.h>
#endif

#ifdef _WIN32
# define EXPORT __declspec(dllexport)
#else
Expand All @@ -27,6 +37,9 @@
#define UNUSED(x) __attribute__((unused)) x
#endif

#ifdef _WIN32
typedef long ssize_t;
#endif

#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define BATCH_SIZE 2048
Expand Down Expand Up @@ -246,10 +259,14 @@ emacs_value fzf_native_score_all(emacs_env *env,
// Print the shared value.
/* ssize_t value = atomic_load(&shared.remaining); */
/* printf("shared Remaining: %zd\n", value); */

#if defined(__APPLE__) || defined(__linux__)
// Set up max number of workers according to processor.
// It's 8 on M1 Macbook.
unsigned max_workers = sysconf(_SC_NPROCESSORS_ONLN);
#else
unsigned max_workers = 4; // Random safe guess number.
#endif

if (!(data = malloc(sizeof *data + max_workers * sizeof *data->threads))) {
goto err;
}
Expand Down
Loading
Loading
0