|
| 1 | +/* |
| 2 | + * iris_compat.h - Windows portability shims |
| 3 | + * |
| 4 | + * Iris is written against POSIX. This header provides the small subset of |
| 5 | + * POSIX functionality the sources actually use (memory-mapped files, a |
| 6 | + * monotonic-ish wall clock, case-insensitive compares, and getopt_long) so the |
| 7 | + * engine builds with the MSVC/clang toolchain on Windows. On non-Windows |
| 8 | + * platforms this header is empty and the normal system headers are used. |
| 9 | + * |
| 10 | + * Include this BEFORE the POSIX headers it replaces. The build guards those |
| 11 | + * includes (<sys/mman.h>, <unistd.h>, <sys/time.h>, <dirent.h>, <getopt.h>) |
| 12 | + * with #ifndef _WIN32, and includes this header in the #else branch. |
| 13 | + */ |
| 14 | +#ifndef IRIS_COMPAT_H |
| 15 | +#define IRIS_COMPAT_H |
| 16 | + |
| 17 | +#ifdef _WIN32 |
| 18 | + |
| 19 | +/* Quiet the deprecation warnings on the POSIX CRT names (open/close/strdup/...) |
| 20 | + * and keep <windows.h> from dragging in winsock + min/max macros. */ |
| 21 | +#ifndef WIN32_LEAN_AND_MEAN |
| 22 | +#define WIN32_LEAN_AND_MEAN |
| 23 | +#endif |
| 24 | +#ifndef NOMINMAX |
| 25 | +#define NOMINMAX |
| 26 | +#endif |
| 27 | +#ifndef _CRT_SECURE_NO_WARNINGS |
| 28 | +#define _CRT_SECURE_NO_WARNINGS |
| 29 | +#endif |
| 30 | +#ifndef _CRT_NONSTDC_NO_WARNINGS |
| 31 | +#define _CRT_NONSTDC_NO_WARNINGS |
| 32 | +#endif |
| 33 | + |
| 34 | +#include <windows.h> |
| 35 | + |
| 36 | +#include <fcntl.h> |
| 37 | +#include <io.h> |
| 38 | +#include <stdint.h> |
| 39 | +#include <stdio.h> |
| 40 | +#include <stdlib.h> |
| 41 | +#include <string.h> |
| 42 | +#include <sys/stat.h> |
| 43 | +#include <time.h> |
| 44 | + |
| 45 | +/* --- <strings.h> -------------------------------------------------------- */ |
| 46 | +#define strcasecmp _stricmp |
| 47 | +#define strncasecmp _strnicmp |
| 48 | + |
| 49 | +/* --- 64-bit stat -------------------------------------------------------- */ |
| 50 | +/* The default Windows `struct stat` stores st_size in a 32-bit long, which |
| 51 | + * truncates the multi-GB model files. Route stat()/fstat() through the 64-bit |
| 52 | + * variants (struct _stat64 / _stat64 / _fstat64 share the same spelling). */ |
| 53 | +#define stat _stat64 |
| 54 | +#define fstat _fstat64 |
| 55 | + |
| 56 | +/* --- <sys/mman.h> (read-only file mappings) ----------------------------- */ |
| 57 | +#define PROT_READ 0x1 |
| 58 | +#define PROT_WRITE 0x2 |
| 59 | +#define MAP_SHARED 0x1 |
| 60 | +#define MAP_PRIVATE 0x2 |
| 61 | +#define MAP_FAILED ((void *)-1) |
| 62 | + |
| 63 | +/* Map a CRT file descriptor. offset is 0 in every iris call site. The view |
| 64 | + * stays valid after the mapping object is closed, so munmap only unmaps. */ |
| 65 | +static __inline void *mmap(void *addr, size_t length, int prot, int flags, int fd, long long offset) { |
| 66 | + (void)addr; |
| 67 | + (void)prot; |
| 68 | + (void)flags; |
| 69 | + HANDLE fh = (HANDLE)_get_osfhandle(fd); |
| 70 | + if (fh == INVALID_HANDLE_VALUE) |
| 71 | + return MAP_FAILED; |
| 72 | + HANDLE mh = CreateFileMappingA(fh, NULL, PAGE_READONLY, 0, 0, NULL); |
| 73 | + if (!mh) |
| 74 | + return MAP_FAILED; |
| 75 | + void *p = MapViewOfFile(mh, FILE_MAP_READ, (DWORD)((unsigned long long)offset >> 32), (DWORD)((unsigned long long)offset & 0xFFFFFFFFu), length); |
| 76 | + CloseHandle(mh); |
| 77 | + return p ? p : MAP_FAILED; |
| 78 | +} |
| 79 | + |
| 80 | +static __inline int munmap(void *addr, size_t length) { |
| 81 | + (void)length; |
| 82 | + return UnmapViewOfFile(addr) ? 0 : -1; |
| 83 | +} |
| 84 | + |
| 85 | +/* --- <sys/time.h> ------------------------------------------------------- */ |
| 86 | +/* WIN32_LEAN_AND_MEAN keeps winsock (and its struct timeval) out, so we own |
| 87 | + * the definition here. */ |
| 88 | +struct timeval { |
| 89 | + long tv_sec; |
| 90 | + long tv_usec; |
| 91 | +}; |
| 92 | + |
| 93 | +static __inline int gettimeofday(struct timeval *tv, void *tz) { |
| 94 | + (void)tz; |
| 95 | + FILETIME ft; |
| 96 | + ULARGE_INTEGER t; |
| 97 | + GetSystemTimeAsFileTime(&ft); |
| 98 | + t.LowPart = ft.dwLowDateTime; |
| 99 | + t.HighPart = ft.dwHighDateTime; |
| 100 | + /* 100ns ticks since 1601-01-01 -> microseconds since the Unix epoch. */ |
| 101 | + t.QuadPart -= 116444736000000000ULL; |
| 102 | + tv->tv_sec = (long)(t.QuadPart / 10000000ULL); |
| 103 | + tv->tv_usec = (long)((t.QuadPart % 10000000ULL) / 10ULL); |
| 104 | + return 0; |
| 105 | +} |
| 106 | + |
| 107 | +/* --- <unistd.h>: sysconf(_SC_NPROCESSORS_ONLN) -------------------------- */ |
| 108 | +#define _SC_NPROCESSORS_ONLN 1 |
| 109 | + |
| 110 | +static __inline long sysconf(int name) { |
| 111 | + if (name == _SC_NPROCESSORS_ONLN) { |
| 112 | + SYSTEM_INFO si; |
| 113 | + GetSystemInfo(&si); |
| 114 | + return (long)si.dwNumberOfProcessors; |
| 115 | + } |
| 116 | + return -1; |
| 117 | +} |
| 118 | + |
| 119 | +/* --- <getopt.h> --------------------------------------------------------- */ |
| 120 | +/* Compiled into the single translation unit (main.c) that defines |
| 121 | + * IRIS_NEED_GETOPT before including this header. */ |
| 122 | +#ifdef IRIS_NEED_GETOPT |
| 123 | + |
| 124 | +#define no_argument 0 |
| 125 | +#define required_argument 1 |
| 126 | +#define optional_argument 2 |
| 127 | + |
| 128 | +struct option { |
| 129 | + const char *name; |
| 130 | + int has_arg; |
| 131 | + int *flag; |
| 132 | + int val; |
| 133 | +}; |
| 134 | + |
| 135 | +char *optarg = NULL; |
| 136 | +int optind = 1; |
| 137 | +int opterr = 1; |
| 138 | +int optopt = 0; |
| 139 | + |
| 140 | +static int iris_optpos = 1; /* position within a clustered short-option group */ |
| 141 | + |
| 142 | +static int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex) { |
| 143 | + optarg = NULL; |
| 144 | + if (optind >= argc) |
| 145 | + return -1; |
| 146 | + |
| 147 | + char *arg = argv[optind]; |
| 148 | + if (arg[0] != '-' || arg[1] == '\0') |
| 149 | + return -1; /* not an option; stop (no permutation) */ |
| 150 | + |
| 151 | + if (arg[1] == '-') { |
| 152 | + /* "--" terminator or a long option */ |
| 153 | + if (arg[2] == '\0') { |
| 154 | + optind++; |
| 155 | + return -1; |
| 156 | + } |
| 157 | + const char *name = arg + 2; |
| 158 | + const char *eq = strchr(name, '='); |
| 159 | + size_t namelen = eq ? (size_t)(eq - name) : strlen(name); |
| 160 | + for (int i = 0; longopts && longopts[i].name; i++) { |
| 161 | + if (strlen(longopts[i].name) == namelen && strncmp(name, longopts[i].name, namelen) == 0) { |
| 162 | + optind++; |
| 163 | + if (longindex) |
| 164 | + *longindex = i; |
| 165 | + if (longopts[i].has_arg == required_argument) { |
| 166 | + if (eq) |
| 167 | + optarg = (char *)eq + 1; |
| 168 | + else if (optind < argc) |
| 169 | + optarg = argv[optind++]; |
| 170 | + else { |
| 171 | + if (opterr) |
| 172 | + fprintf(stderr, "%s: option '--%s' requires an argument\n", argv[0], longopts[i].name); |
| 173 | + return '?'; |
| 174 | + } |
| 175 | + } |
| 176 | + else if (longopts[i].has_arg == optional_argument && eq) { |
| 177 | + optarg = (char *)eq + 1; |
| 178 | + } |
| 179 | + if (longopts[i].flag) { |
| 180 | + *longopts[i].flag = longopts[i].val; |
| 181 | + return 0; |
| 182 | + } |
| 183 | + return longopts[i].val; |
| 184 | + } |
| 185 | + } |
| 186 | + if (opterr) |
| 187 | + fprintf(stderr, "%s: unrecognized option '--%s'\n", argv[0], name); |
| 188 | + optind++; |
| 189 | + return '?'; |
| 190 | + } |
| 191 | + |
| 192 | + /* short option (possibly clustered, e.g. -qv) */ |
| 193 | + int c = arg[iris_optpos]; |
| 194 | + const char *p = (c != ':') ? strchr(optstring, c) : NULL; |
| 195 | + if (!p) { |
| 196 | + optopt = c; |
| 197 | + if (opterr) |
| 198 | + fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c); |
| 199 | + if (arg[++iris_optpos] == '\0') { |
| 200 | + optind++; |
| 201 | + iris_optpos = 1; |
| 202 | + } |
| 203 | + return '?'; |
| 204 | + } |
| 205 | + |
| 206 | + if (p[1] == ':') { |
| 207 | + /* option takes an argument */ |
| 208 | + if (arg[iris_optpos + 1] != '\0') { |
| 209 | + optarg = &arg[iris_optpos + 1]; |
| 210 | + optind++; |
| 211 | + } |
| 212 | + else if (optind + 1 < argc) { |
| 213 | + optarg = argv[optind + 1]; |
| 214 | + optind += 2; |
| 215 | + } |
| 216 | + else { |
| 217 | + optopt = c; |
| 218 | + optind++; |
| 219 | + iris_optpos = 1; |
| 220 | + if (opterr) |
| 221 | + fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c); |
| 222 | + return '?'; |
| 223 | + } |
| 224 | + iris_optpos = 1; |
| 225 | + return c; |
| 226 | + } |
| 227 | + |
| 228 | + /* option without an argument */ |
| 229 | + if (arg[++iris_optpos] == '\0') { |
| 230 | + optind++; |
| 231 | + iris_optpos = 1; |
| 232 | + } |
| 233 | + return c; |
| 234 | +} |
| 235 | + |
| 236 | +#endif /* IRIS_NEED_GETOPT */ |
| 237 | + |
| 238 | +#endif /* _WIN32 */ |
| 239 | +#endif /* IRIS_COMPAT_H */ |
0 commit comments