MSVC: POSIX compatibility files
There's a bunch of packages on vcpkg, let's use them this way. I wasn't
able to find anything for <unistd.h>, so I took that from
https://stackoverflow.com/a/826027/2245623 .
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1764a5f..9a6bad1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -343,10 +343,32 @@
target_compile_definitions(yangobj PRIVATE LIBYANG_BUILD)
add_library(yang SHARED $<TARGET_OBJECTS:yangobj>)
+ if(WIN32)
+ find_package(dlfcn-win32 REQUIRED)
+ set(CMAKE_DL_LIBS dlfcn-win32::dl)
+ endif()
+
#link dl
target_link_libraries(yang ${CMAKE_DL_LIBS})
endif()
+if(WIN32)
+ find_path(DIRENT_INCLUDE_DIR NAMES dirent.h REQUIRED)
+ message(STATUS "Found <dirent.h> at ${DIRENT_INCLUDE_DIR}")
+
+ set(COMPAT_POSIX_INCLUDES
+ ${CMAKE_CURRENT_SOURCE_DIR}/compat/posix-shims
+ ${DIRENT_INCLUDE_DIR})
+
+ if(TARGET yangobj)
+ target_include_directories(yangobj PRIVATE ${COMPAT_POSIX_INCLUDES})
+ endif()
+ target_include_directories(yang PRIVATE ${COMPAT_POSIX_INCLUDES})
+
+ find_package(pthreads REQUIRED)
+ target_link_libraries(yang PThreads4W::PThreads4W)
+endif()
+
set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION})
# link math
diff --git a/compat/posix-shims/unistd.h b/compat/posix-shims/unistd.h
new file mode 100644
index 0000000..e01a73d
--- /dev/null
+++ b/compat/posix-shims/unistd.h
@@ -0,0 +1,55 @@
+#ifndef _UNISTD_H
+#define _UNISTD_H 1
+
+/* This is intended as a drop-in replacement for unistd.h on Windows.
+ * Please add functionality as neeeded.
+ * https://stackoverflow.com/a/826027/1202830
+ */
+
+#include <stdlib.h>
+#include <io.h>
+#include <process.h> /* for getpid() and the exec..() family */
+#include <direct.h> /* for _getcwd() and _chdir() */
+
+#define srandom srand
+#define random rand
+
+/* Values for the second argument to access.
+ These may be OR'd together. */
+#define R_OK 4 /* Test for read permission. */
+#define W_OK 2 /* Test for write permission. */
+//#define X_OK 1 /* execute permission - unsupported in windows*/
+#define F_OK 0 /* Test for existence. */
+
+#define access _access
+#define dup2 _dup2
+#define execve _execve
+#define ftruncate _chsize
+#define unlink _unlink
+#define fileno _fileno
+#define getcwd _getcwd
+#define chdir _chdir
+#define isatty _isatty
+#define lseek _lseek
+/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */
+
+#ifdef _WIN64
+#define ssize_t __int64
+#else
+#define ssize_t long
+#endif
+
+#define STDIN_FILENO 0
+#define STDOUT_FILENO 1
+#define STDERR_FILENO 2
+/* should be in some equivalent to <sys/types.h> */
+typedef __int8 int8_t;
+typedef __int16 int16_t;
+typedef __int32 int32_t;
+typedef __int64 int64_t;
+typedef unsigned __int8 uint8_t;
+typedef unsigned __int16 uint16_t;
+typedef unsigned __int32 uint32_t;
+typedef unsigned __int64 uint64_t;
+
+#endif /* unistd.h */