-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplatform_windows.c
More file actions
217 lines (192 loc) · 6.3 KB
/
platform_windows.c
File metadata and controls
217 lines (192 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#pragma warning(push)
#pragma warning(disable : 4668)
#include <intrin.h>
#pragma warning(pop)
#define PRI_size "zi"
#define PLATFORM_FORMAT_I64 "lld"
#define PLATFORM_FORMAT_U64 "llu"
#if 0
#if !defined(__clang__)
extern int sprintf_s(char * buffer, rsize_t bufsz, const char * format, ...);
extern int vsnprintf( char * buffer, size_t bufsz, const char * format, va_list vlist );
#elif defined(__clang__)
extern int sprintf_s(char * buffer, unsigned long long bufsz, const char * format, ...);
//extern int vsnprintf( char * buffer, unsigned long long bufsz, const char * format, va_list vlist );
extern int vsnprintf( char * buffer, unsigned long bufsz, const char * format, va_list vlist );
#endif
#endif
u64
platformFirstBitSet(u64 val) {
unsigned long bit = 0;
_BitScanReverse64(&bit, val);
return (u64)bit;
}
void
winPrintError(int err_code) {
char* msg = 0;
char display [PathMax] = Zero;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &msg,
0, NULL );
snprintf(display, PathMax, "Error: %s", msg);
// MessageBoxA(NULL, (LPCTSTR)display, "Error", MB_OK);
fprintf(stderr, "%s\n", display);
LocalFree(msg);
}
ErrorCode
platformRunProcess(char** args, sz n_args, i32 expected_return) {
ErrorCode result = Fail;
if (n_args != 0) {
STARTUPINFO startup_info = {
.cb = sizeof(STARTUPINFO),
.hStdError = GetStdHandle(STD_OUTPUT_HANDLE),
.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE),
.hStdInput = GetStdHandle(STD_INPUT_HANDLE),
.dwFlags = STARTF_USESTDHANDLES,
};
PROCESS_INFORMATION proc_info = Zero;
// Create the child process.
char cmdline[1024] = Zero;
for (int i =0; i < n_args; ++i) {
strncat(cmdline, args[i], ArrayCount(cmdline));
strncat(cmdline, " ", ArrayCount(cmdline));
}
printf("Command line: %s\n", cmdline);
BOOL cpr = CreateProcess(/*lpApplicationName*/ NULL, // Will grab first arg as app name.
/*lpCommandLine*/ cmdline,
/*lpProcessAttributes*/ NULL,
/*lpThreadAttributes*/ NULL,
/*bInheritHandles*/TRUE,
/*dwCreationFlags*/0,
/*lpEnvironment*/NULL,
/*lpCurrentDirectory*/NULL,
/*lpStartupInfo*/&startup_info,
/*lpProcessInformation*/&proc_info);
if (!cpr) {
int err = GetLastError();
winPrintError(err);
fprintf(stderr, "CreateProcess failed: Error %d\n", err);
}
else {
DWORD wait_res = WaitForSingleObject(proc_info.hProcess, 2000);
if (wait_res == WAIT_OBJECT_0) {
DWORD exit_code = 0;
GetExitCodeProcess(proc_info.hProcess, &exit_code);
if (exit_code != (DWORD)expected_return) {
fprintf(stderr, "Program failed with error code %ld\n", exit_code);
}
else {
result = Ok;
}
}
else {
fprintf(stderr, "WaitForSingleObject failed. Probably a timeout. %ld\n", wait_res);
}
CloseHandle(proc_info.hProcess);
CloseHandle(proc_info.hThread);
}
}
return result;
}
ErrorCode
platformCompileAndLinkAsmFile(char* outfile /*Filename without extension*/) {
ErrorCode result = Ok;
// TODO: Remove hardcoded path
char asmfile[PathMax] = Zero; {
snprintf(asmfile, ArrayCount(asmfile), "%s.asm", outfile);
}
char* args[] = {
"C:\\Program Files\\NASM\\nasm.exe",
asmfile,
"-f win64",
"-F cv8"
};
if (Ok != platformRunProcess(args, ArrayCount(args), 0)) {
fprintf(stderr, "Failure running nasm. %s\n", asmfile);
result = CouldNotAssemble;
}
else {
char exearg[PathMax] = Zero; {
snprintf(exearg, ArrayCount(exearg), "/OUT:%s.exe", outfile);
}
char objfile[PathMax] = Zero; {
snprintf(objfile, ArrayCount(objfile), "%s.obj", outfile);
}
char pdbarg[PathMax] = Zero; {
snprintf(pdbarg, ArrayCount(pdbarg), "/PDB:%s.pdb", outfile);
}
char* args[] = {
// "link.exe",
"link.exe",
objfile,
exearg,
"/DEBUG",
pdbarg,
"/ENTRY:_start",
"/SUBSYSTEM:CONSOLE", "kernel32.lib"
};
if ( Ok != platformRunProcess(args, ArrayCount(args), 0)) {
fprintf(stderr, "Link fail\n");
result = CouldNotLink;
}
}
return result;
}
ErrorCode
platformListDirectory(char*** out_files, char* dirname, b32 (*filter)(char*)) {
ErrorCode err = Ok;
WIN32_FIND_DATAA data = {0};
char glob[PathMax] = {0};
strncat(glob, dirname, PathMax);
strncat(glob, "\\*", PathMax);
HANDLE h = FindFirstFileA(glob, &data);
if (h) {
if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
err = NotADirectory;
}
else {
while (FindNextFileA(h, &data)) {
if (data.cFileName[0] != '.') {
if (filter(data.cFileName)) {
char file[PathMax] = {0};
strncat(file, dirname, PathMax);
strncat(file, "\\", PathMax);
strncat(file, data.cFileName, PathMax);
bufPush(*out_files, getString(file));
}
}
}
}
}
return err;
}
void
platformPathAtBinary(char* path, sz size) {
char* tmp = (char*)calloc(1, size);
strcpy(tmp, path);
// TODO: Wide char paths on Windows..
GetModuleFileNameA(NULL, path, (DWORD)size);
{ // Remove the exe name
char* last_slash = path;
for ( char* iter = path;
*iter != '\0';
++iter ) {
if ( *iter == '\\' ) {
last_slash = iter;
}
}
*(last_slash+1) = '\0';
}
strcat(path, tmp);
free(tmp);
}
char*
platformOutputBinaryFilename(Arena* arena, char* fname_without_extension) {
char* appended = appendString(arena, fname_without_extension, ".exe");
return appended;
}