Skip to content

Commit d447993

Browse files
committed
Add support for windows builds
For allowing python and rust users to develop OSDP apps on windows, we need to fix this repo in multiple places. Some of it looks uglier than it ought to be but that's the cost of usability :( Signed-off-by: Siddharth <Siddharth.Chandrasekaran@huawei.com>
1 parent 06259c4 commit d447993

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

include/utils/utils.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,23 @@ extern "C" {
119119
#define __packed __attribute__((__packed__))
120120
#endif
121121

122+
#ifdef __GNUC__
123+
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
124+
#endif
125+
126+
#ifdef _MSC_VER
127+
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
128+
#endif
129+
122130
#undef __weak
123131
#define __weak __attribute__((weak))
124132

133+
#if (defined(_WIN32) || defined(_WIN64))
134+
#define __format_printf(x, y)
135+
#else
136+
#define __format_printf(x, y) __attribute__((format(printf, x, y)))
137+
#endif
138+
125139
/**
126140
* @brief Return random number between 0 and `limit` both inclusive.
127141
*
@@ -193,7 +207,7 @@ void dump_trace(void);
193207

194208
static inline bool char_is_space(int c)
195209
{
196-
unsigned char d = c - 9;
210+
unsigned char d = (unsigned char)(c - 9);
197211
return (0x80001FU >> (d & 31)) & (1U >> (d >> 5));
198212
}
199213

src/disjoint_set.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ void disjoint_set_union(struct disjoint_set *set, int a, int b)
4949
if (a == b)
5050
return;
5151

52-
if (set->rank[a] < set->rank[b])
53-
SWAP(a, b);
52+
if (set->rank[a] < set->rank[b]) {
53+
int tmp;
54+
tmp = a;
55+
a = b;
56+
b = tmp;
57+
}
5458

5559
set->parent[b] = a;
5660
if (set->rank[a] == set->rank[b])

src/logger.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
#include <stdarg.h>
77
#include <stdio.h>
88
#include <stdlib.h>
9-
#include <unistd.h>
109
#include <string.h>
1110
#include <assert.h>
1211
#include <time.h>
12+
#if (!defined(_WIN32) && !defined(_WIN64))
13+
#include <unistd.h>
14+
#endif
1315

1416
#include <utils/logger.h>
1517

@@ -98,7 +100,7 @@ static int terminate_log_line(char *buf, int len)
98100

99101
#define LOG_BUF_LEN 192
100102

101-
__attribute__((format(printf, 5, 6)))
103+
__format_printf(5, 6)
102104
int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long line,
103105
const char *fmt, ...)
104106
{

src/slab.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
#include <utils/slab.h>
1212

13-
struct slab_unit {
13+
PACK(struct slab_unit {
1414
uint32_t leased;
1515
uint32_t canary;
1616
uint8_t data[];
17-
} __packed;
17+
});
1818

1919
int slab_init(slab_t *slab, size_t slab_size,
2020
uint8_t *blob, size_t blob_size)

src/utils.c

+38-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <stddef.h>
1010
#include <ctype.h>
1111
#include <stdlib.h>
12-
#include <time.h>
13-
#include <sys/time.h>
1412

1513
#include <utils/utils.h>
1614

@@ -49,7 +47,7 @@ int num_digits_in_number(int num)
4947
return digits;
5048
}
5149

52-
__attribute__((format(printf, 3, 4)))
50+
__format_printf(3, 4)
5351
void hexdump(const void *p, size_t len, const char *fmt, ...)
5452
{
5553
size_t i;
@@ -87,6 +85,43 @@ void hexdump(const void *p, size_t len, const char *fmt, ...)
8785
printf("\n");
8886
}
8987

88+
#if (defined(_WIN32) || defined(_WIN64))
89+
#define WIN32_LEAN_AND_MEAN
90+
#include <Windows.h>
91+
#include <stdint.h> // portable: uint64_t MSVC: __int64
92+
93+
// MSVC defines this in winsock2.h!?
94+
typedef struct timeval {
95+
long tv_sec;
96+
long tv_usec;
97+
} timeval;
98+
99+
int gettimeofday(struct timeval * tp, struct timezone * tzp)
100+
{
101+
// Note: some broken versions only have 8 trailing zero's, the correct
102+
// epoch has 9 trailing zero's This magic number is the number of 100
103+
// nanosecond intervals since January 1, 1601 (UTC) until 00:00:00
104+
// January 1, 1970
105+
static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
106+
107+
SYSTEMTIME system_time;
108+
FILETIME file_time;
109+
uint64_t time;
110+
111+
GetSystemTime( &system_time );
112+
SystemTimeToFileTime( &system_time, &file_time );
113+
time = ((uint64_t)file_time.dwLowDateTime ) ;
114+
time += ((uint64_t)file_time.dwHighDateTime) << 32;
115+
116+
tp->tv_sec = (long) ((time - EPOCH) / 10000000L);
117+
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
118+
return 0;
119+
}
120+
#else
121+
#include <sys/time.h>
122+
#include <time.h>
123+
#endif
124+
90125
int64_t usec_now()
91126
{
92127
int64_t usec;

0 commit comments

Comments
 (0)