Skip to content

Commit 0ca3110

Browse files
committed
msvc baremetal build fix
1 parent db3d3f0 commit 0ca3110

File tree

6 files changed

+237
-25
lines changed

6 files changed

+237
-25
lines changed

cutils.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,28 @@
2929
#include <string.h>
3030
#include <inttypes.h>
3131

32+
#ifdef _MSC_VER
33+
#include <intrin.h>
34+
#endif
35+
36+
/* set if CPU is big endian */
37+
#undef WORDS_BIGENDIAN
38+
39+
#if defined(__GNUC__) || defined(__clang__)
3240
#define likely(x) __builtin_expect(!!(x), 1)
3341
#define unlikely(x) __builtin_expect(!!(x), 0)
3442
#define force_inline inline __attribute__((always_inline))
3543
#define no_inline __attribute__((noinline))
3644
#define __maybe_unused __attribute__((unused))
45+
#else
46+
#define likely(x) (x)
47+
#define unlikely(x) (x)
48+
#define force_inline inline
49+
#define no_inline
50+
#define __maybe_unused
51+
#define __attribute__(x)
52+
#define __attribute(x)
53+
#endif
3754

3855
#define xglue(x, y) x ## y
3956
#define glue(x, y) xglue(x, y)
@@ -128,25 +145,49 @@ static inline int64_t min_int64(int64_t a, int64_t b)
128145
/* WARNING: undefined if a = 0 */
129146
static inline int clz32(unsigned int a)
130147
{
148+
#ifdef _MSC_VER
149+
unsigned long idx;
150+
_BitScanReverse(&idx, a);
151+
return 31 ^ idx;
152+
#else
131153
return __builtin_clz(a);
154+
#endif
132155
}
133156

134157
/* WARNING: undefined if a = 0 */
135158
static inline int clz64(uint64_t a)
136159
{
160+
#ifdef _MSC_VER
161+
unsigned long idx;
162+
_BitScanReverse64(&idx, a);
163+
return 63 ^ idx;
164+
#else
137165
return __builtin_clzll(a);
166+
#endif
138167
}
139168

140169
/* WARNING: undefined if a = 0 */
141170
static inline int ctz32(unsigned int a)
142171
{
172+
#ifdef _MSC_VER
173+
unsigned long idx;
174+
_BitScanForward(&idx, a);
175+
return 31 ^ idx;
176+
#else
143177
return __builtin_ctz(a);
178+
#endif
144179
}
145180

146181
/* WARNING: undefined if a = 0 */
147182
static inline int ctz64(uint64_t a)
148183
{
184+
#ifdef _MSC_VER
185+
unsigned long idx;
186+
_BitScanForward64(&idx, a);
187+
return 63 ^ idx;
188+
#else
149189
return __builtin_ctzll(a);
190+
#endif
150191
}
151192

152193
struct __attribute__((packed)) packed_u64 {

qjs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
#include <inttypes.h>
2929
#include <string.h>
3030
#include <assert.h>
31+
32+
#if !defined(_MSC_VER)
3133
#include <unistd.h>
34+
#endif
35+
3236
#include <errno.h>
3337
#include <fcntl.h>
3438
#include <time.h>

qjsc.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
#include <inttypes.h>
2828
#include <string.h>
2929
#include <assert.h>
30+
31+
#if !defined(_MSC_VER)
3032
#include <unistd.h>
33+
#endif
34+
3135
#include <errno.h>
3236
#if !defined(_WIN32)
3337
#include <sys/wait.h>
@@ -521,6 +525,80 @@ static const char *get_short_optarg(int *poptind, int opt,
521525
return optarg;
522526
}
523527

528+
#if defined(_MSC_VER)
529+
530+
static int opterr = 1; /* if error message should be printed */
531+
static int optind = 1; /* index into parent argv vector */
532+
static int optopt; /* character checked for validity */
533+
static int optreset; /* reset getopt */
534+
static char* optarg; /* argument associated with option */
535+
536+
#define BADCH (int)'?'
537+
#define BADARG (int)':'
538+
#define EMSG ""
539+
540+
/*
541+
* getopt --
542+
* Parse argc/argv argument vector.
543+
*/
544+
static int getopt(int nargc, char* const nargv[], const char* ostr)
545+
{
546+
static char* place = EMSG; /* option letter processing */
547+
const char* oli; /* option letter list index */
548+
549+
if (optreset || !*place) { /* update scanning pointer */
550+
optreset = 0;
551+
if (optind >= nargc || *(place = nargv[optind]) != '-') {
552+
place = EMSG;
553+
return (-1);
554+
}
555+
if (place[1] && *++place == '-') { /* found "--" */
556+
++optind;
557+
place = EMSG;
558+
return (-1);
559+
}
560+
} /* option letter okay? */
561+
if ((optopt = (int)*place++) == (int)':' ||
562+
!(oli = strchr(ostr, optopt))) {
563+
/*
564+
* if the user didn't specify '-' as an option,
565+
* assume it means -1.
566+
*/
567+
if (optopt == (int)'-')
568+
return (-1);
569+
if (!*place)
570+
++optind;
571+
if (opterr && *ostr != ':')
572+
(void)printf("illegal option -- %c\n", optopt);
573+
return (BADCH);
574+
}
575+
if (*++oli != ':') { /* don't need argument */
576+
optarg = NULL;
577+
if (!*place)
578+
++optind;
579+
}
580+
else { /* need an argument */
581+
if (*place) /* no white space */
582+
optarg = place;
583+
else if (nargc <= ++optind) { /* no arg */
584+
place = EMSG;
585+
if (*ostr == ':')
586+
return (BADARG);
587+
if (opterr)
588+
(void)printf("option requires an argument -- %c\n", optopt);
589+
return (BADCH);
590+
}
591+
else /* white space */
592+
optarg = nargv[optind];
593+
place = EMSG;
594+
++optind;
595+
}
596+
return (optopt); /* dump back option letter */
597+
}
598+
599+
#endif
600+
601+
524602
int main(int argc, char **argv)
525603
{
526604
int i, verbose, strip_flags;

quickjs-libc.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@
2828
#include <inttypes.h>
2929
#include <string.h>
3030
#include <assert.h>
31+
#if defined(_MSC_VER)
32+
#include <winsock.h>
33+
#include <sys/utime.h>
34+
#else
3135
#include <unistd.h>
36+
#include <sys/time.h>
37+
#include <utime.h>
38+
#endif
3239
#include <errno.h>
3340
#include <fcntl.h>
34-
#include <sys/time.h>
3541
#include <time.h>
3642
#include <signal.h>
3743
#include <limits.h>
@@ -40,7 +46,6 @@
4046
#if defined(_WIN32)
4147
#include <windows.h>
4248
#include <conio.h>
43-
#include <utime.h>
4449
#else
4550
#include <dlfcn.h>
4651
#include <termios.h>
@@ -72,6 +77,13 @@ typedef sig_t sighandler_t;
7277
#include <stdatomic.h>
7378
#endif
7479

80+
#if defined(_MSC_VER)
81+
#include <BaseTsd.h>
82+
typedef SSIZE_T ssize_t;
83+
#define pclose(...) _pclose(__VA_ARGS__)
84+
#define popen(...) _popen(__VA_ARGS__)
85+
#endif
86+
7587
#include "cutils.h"
7688
#include "list.h"
7789
#include "quickjs-libc.h"
@@ -2011,6 +2023,24 @@ static int64_t get_time_ns(void)
20112023
clock_gettime(CLOCK_MONOTONIC, &ts);
20122024
return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
20132025
}
2026+
2027+
#elif defined(_MSC_VER)
2028+
static int64_t get_time_ms(void)
2029+
{
2030+
return GetTickCount();
2031+
}
2032+
2033+
static int64_t get_time_ns(void)
2034+
{
2035+
LARGE_INTEGER frequency;
2036+
LARGE_INTEGER counter;
2037+
2038+
QueryPerformanceFrequency(&frequency);
2039+
QueryPerformanceCounter(&counter);
2040+
2041+
return counter.QuadPart * 1000000000 / frequency.QuadPart;
2042+
}
2043+
20142044
#else
20152045
/* more portable, but does not work if the date is updated */
20162046
static int64_t get_time_ms(void)
@@ -2721,7 +2751,7 @@ static JSValue js_os_stat(JSContext *ctx, JSValueConst this_val,
27212751
return make_obj_error(ctx, obj, err);
27222752
}
27232753

2724-
#if !defined(_WIN32)
2754+
#if defined(_MSC_VER) || !defined(_WIN32)
27252755
static void ms_to_timeval(struct timeval *tv, uint64_t v)
27262756
{
27272757
tv->tv_sec = v / 1000;

0 commit comments

Comments
 (0)