Skip to content

Commit abff1b6

Browse files
committedNov 2, 2017
windows: use the same libc search within a compilation unit
1 parent f7837f4 commit abff1b6

8 files changed

+87
-66
lines changed
 

‎src/all_types.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1462,10 +1462,11 @@ struct CodeGen {
14621462
bool have_winmain_crt_startup;
14631463
bool have_dllmain_crt_startup;
14641464
bool have_pub_panic;
1465-
ZigList<Buf*> libc_lib_dirs_list;
14661465
Buf *libc_lib_dir;
14671466
Buf *libc_static_lib_dir;
14681467
Buf *libc_include_dir;
1468+
Buf *msvc_lib_dir;
1469+
Buf *kernel32_lib_dir;
14691470
Buf *zig_lib_dir;
14701471
Buf *zig_std_dir;
14711472
Buf *zig_c_headers_dir;

‎src/analyze.cpp

+36-37
Original file line numberDiff line numberDiff line change
@@ -3370,63 +3370,62 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
33703370
zig_unreachable();
33713371
}
33723372

3373+
static ZigWindowsSDK *get_windows_sdk(CodeGen *g) {
3374+
if (g->win_sdk == nullptr) {
3375+
if (os_find_windows_sdk(&g->win_sdk)) {
3376+
zig_panic("Unable to determine Windows SDK path.");
3377+
}
3378+
}
3379+
assert(g->win_sdk != nullptr);
3380+
return g->win_sdk;
3381+
}
3382+
33733383
void find_libc_include_path(CodeGen *g) {
3374-
#ifdef ZIG_OS_WINDOWS
33753384
if (!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
3376-
if (g->win_sdk == nullptr) {
3377-
if (os_find_windows_sdk(&g->win_sdk)) {
3378-
zig_panic("Unable to determine Windows SDK path.");
3379-
}
3380-
}
3385+
ZigWindowsSDK *sdk = get_windows_sdk(g);
33813386

33823387
if (g->zig_target.os == ZigLLVM_Win32) {
3383-
if (os_get_win32_ucrt_include_path(g->win_sdk, g->libc_include_dir)) {
3388+
if (os_get_win32_ucrt_include_path(sdk, g->libc_include_dir)) {
33843389
zig_panic("Unable to determine libc include path.");
33853390
}
33863391
}
33873392
}
3388-
return;
3389-
#endif
3393+
33903394
// TODO find libc at runtime for other operating systems
33913395
if(!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
33923396
zig_panic("Unable to determine libc include path.");
33933397
}
33943398
}
33953399

33963400
void find_libc_lib_path(CodeGen *g) {
3397-
#ifdef ZIG_OS_WINDOWS
3398-
if (g->zig_target.os == ZigLLVM_Win32) {
3399-
if (g->win_sdk == nullptr) {
3400-
if (os_find_windows_sdk(&g->win_sdk)) {
3401-
zig_panic("Unable to determine Windows SDK path.");
3401+
// later we can handle this better by reporting an error via the normal mechanism
3402+
if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0 ||
3403+
(g->zig_target.os == ZigLLVM_Win32 && (g->msvc_lib_dir == nullptr || g->kernel32_lib_dir == nullptr)))
3404+
{
3405+
if (g->zig_target.os == ZigLLVM_Win32) {
3406+
ZigWindowsSDK *sdk = get_windows_sdk(g);
3407+
3408+
Buf* vc_lib_dir = buf_alloc();
3409+
if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) {
3410+
zig_panic("Unable to determine vcruntime path.");
34023411
}
3403-
}
34043412

3405-
Buf* vc_lib_dir = buf_alloc();
3406-
if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) {
3407-
zig_panic("Unable to determine vcruntime path.");
3408-
}
3413+
Buf* ucrt_lib_path = buf_alloc();
3414+
if (os_get_win32_ucrt_lib_path(sdk, ucrt_lib_path, g->zig_target.arch.arch)) {
3415+
zig_panic("Unable to determine ucrt path.");
3416+
}
34093417

3410-
Buf* ucrt_lib_path = buf_alloc();
3411-
if (os_get_win32_ucrt_lib_path(g->win_sdk, ucrt_lib_path, g->zig_target.arch.arch)) {
3412-
zig_panic("Unable to determine ucrt path.");
3413-
}
3418+
Buf* kern_lib_path = buf_alloc();
3419+
if (os_get_win32_kern32_path(sdk, kern_lib_path, g->zig_target.arch.arch)) {
3420+
zig_panic("Unable to determine kernel32 path.");
3421+
}
34143422

3415-
Buf* kern_lib_path = buf_alloc();
3416-
if (os_get_win32_kern32_path(g->win_sdk, kern_lib_path, g->zig_target.arch.arch)) {
3417-
zig_panic("Unable to determine kernel32 path.");
3423+
g->msvc_lib_dir = vc_lib_dir;
3424+
g->libc_lib_dir = ucrt_lib_path;
3425+
g->kernel32_lib_dir = kern_lib_path;
3426+
} else {
3427+
zig_panic("Unable to determine libc lib path.");
34183428
}
3419-
3420-
g->libc_lib_dirs_list.append(vc_lib_dir);
3421-
g->libc_lib_dirs_list.append(ucrt_lib_path);
3422-
g->libc_lib_dirs_list.append(kern_lib_path);
3423-
}
3424-
return;
3425-
#endif
3426-
3427-
// later we can handle this better by reporting an error via the normal mechanism
3428-
if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0) {
3429-
zig_panic("Unable to determine libc lib path.");
34303429
}
34313430
if (!g->libc_static_lib_dir || buf_len(g->libc_static_lib_dir) == 0) {
34323431
zig_panic("Unable to determine libc static lib path.");

‎src/codegen.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
114114
g->libc_lib_dir = buf_create_from_str("");
115115
g->libc_static_lib_dir = buf_create_from_str("");
116116
g->libc_include_dir = buf_create_from_str("");
117+
g->msvc_lib_dir = nullptr;
118+
g->kernel32_lib_dir = nullptr;
117119
g->each_lib_rpath = false;
118120
} else {
119121
// native compilation, we can rely on the configuration stuff
@@ -123,6 +125,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
123125
g->libc_lib_dir = buf_create_from_str(ZIG_LIBC_LIB_DIR);
124126
g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
125127
g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
128+
g->msvc_lib_dir = nullptr; // find it at runtime
129+
g->kernel32_lib_dir = nullptr; // find it at runtime
126130

127131
#ifdef ZIG_EACH_LIB_RPATH
128132
g->each_lib_rpath = true;
@@ -221,20 +225,20 @@ void codegen_set_libc_include_dir(CodeGen *g, Buf *libc_include_dir) {
221225
g->libc_include_dir = libc_include_dir;
222226
}
223227

224-
void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker) {
225-
g->dynamic_linker = dynamic_linker;
228+
void codegen_set_msvc_lib_dir(CodeGen *g, Buf *msvc_lib_dir) {
229+
g->msvc_lib_dir = msvc_lib_dir;
226230
}
227231

228-
void codegen_add_lib_dir(CodeGen *g, const char *dir) {
229-
g->lib_dirs.append(dir);
232+
void codegen_set_kernel32_lib_dir(CodeGen *g, Buf *kernel32_lib_dir) {
233+
g->kernel32_lib_dir = kernel32_lib_dir;
230234
}
231235

232-
void codegen_set_ucrt_lib_dir(CodeGen *g, Buf *ucrt_lib_dir) {
233-
g->libc_lib_dirs_list.append(ucrt_lib_dir);
236+
void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker) {
237+
g->dynamic_linker = dynamic_linker;
234238
}
235239

236-
void codegen_set_kernel32_lib_dir(CodeGen *g, Buf *kernel32_lib_dir) {
237-
g->libc_lib_dirs_list.append(kernel32_lib_dir);
240+
void codegen_add_lib_dir(CodeGen *g, const char *dir) {
241+
g->lib_dirs.append(dir);
238242
}
239243

240244
void codegen_add_rpath(CodeGen *g, const char *name) {

‎src/codegen.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
3030
void codegen_set_libc_lib_dir(CodeGen *codegen, Buf *libc_lib_dir);
3131
void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
3232
void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);
33-
void codegen_set_ucrt_lib_dir(CodeGen *g, Buf *ucrt_lib_dir);
33+
void codegen_set_msvc_lib_dir(CodeGen *g, Buf *msvc_lib_dir);
3434
void codegen_set_kernel32_lib_dir(CodeGen *codegen, Buf *kernel32_lib_dir);
3535
void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker);
3636
void codegen_set_windows_subsystem(CodeGen *g, bool mwindows, bool mconsole);

‎src/link.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ static void construct_linker_job_coff(LinkJob *lj) {
402402
lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));
403403

404404
if (g->libc_link_lib != nullptr) {
405-
if (g->libc_link_lib != nullptr) {
406-
for (uint32_t i = 0; i < g->libc_lib_dirs_list.length; ++i) {
407-
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dirs_list.items[i]))));
408-
}
409-
}
405+
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->msvc_lib_dir))));
406+
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->kernel32_lib_dir))));
407+
408+
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dir))));
409+
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_static_lib_dir))));
410410
}
411411

412412
if (lj->link_in_crt) {

‎src/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ int main(int argc, char **argv) {
758758
if (libc_include_dir)
759759
codegen_set_libc_include_dir(g, buf_create_from_str(libc_include_dir));
760760
if (msvc_lib_dir)
761-
codegen_set_ucrt_lib_dir(g, buf_create_from_str(msvc_lib_dir));
761+
codegen_set_msvc_lib_dir(g, buf_create_from_str(msvc_lib_dir));
762762
if (kernel32_lib_dir)
763763
codegen_set_kernel32_lib_dir(g, buf_create_from_str(kernel32_lib_dir));
764764
if (dynamic_linker)

‎src/os.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,8 @@ void os_stderr_set_color(TermColor color) {
10011001
#endif
10021002
}
10031003

1004-
#if defined ZIG_OS_WINDOWS
10051004
int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {
1005+
#if defined(ZIG_OS_WINDOWS)
10061006
ZigWindowsSDK *result_sdk = allocate<ZigWindowsSDK>(1);
10071007
buf_resize(&result_sdk->path10, 0);
10081008
buf_resize(&result_sdk->path81, 0);
@@ -1099,9 +1099,13 @@ int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {
10991099

11001100
*out_sdk = result_sdk;
11011101
return 0;
1102+
#else
1103+
return ErrorFileNotFound;
1104+
#endif
11021105
}
11031106

11041107
int os_get_win32_vcruntime_path(Buf* output_buf, ZigLLVM_ArchType platform_type) {
1108+
#if defined(ZIG_OS_WINDOWS)
11051109
buf_resize(output_buf, 0);
11061110
//COM Smart Pointerse requires explicit scope
11071111
{
@@ -1226,9 +1230,13 @@ com_done:;
12261230
buf_resize(output_buf, 0);
12271231
return ErrorFileNotFound;
12281232
}
1233+
#else
1234+
return ErrorFileNotFound;
1235+
#endif
12291236
}
12301237

12311238
int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchType platform_type) {
1239+
#if defined(ZIG_OS_WINDOWS)
12321240
buf_resize(output_buf, 0);
12331241
buf_appendf(output_buf, "%s\\Lib\\%s\\ucrt\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
12341242
switch (platform_type) {
@@ -1254,9 +1262,13 @@ int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_Arch
12541262
buf_resize(output_buf, 0);
12551263
return ErrorFileNotFound;
12561264
}
1265+
#else
1266+
return ErrorFileNotFound;
1267+
#endif
12571268
}
12581269

12591270
int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf* output_buf) {
1271+
#if defined(ZIG_OS_WINDOWS)
12601272
buf_resize(output_buf, 0);
12611273
buf_appendf(output_buf, "%s\\Include\\%s\\ucrt", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
12621274
if (GetFileAttributesA(buf_ptr(output_buf)) != INVALID_FILE_ATTRIBUTES) {
@@ -1266,9 +1278,13 @@ int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf* output_buf) {
12661278
buf_resize(output_buf, 0);
12671279
return ErrorFileNotFound;
12681280
}
1281+
#else
1282+
return ErrorFileNotFound;
1283+
#endif
12691284
}
12701285

12711286
int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchType platform_type) {
1287+
#if defined(ZIG_OS_WINDOWS)
12721288
{
12731289
buf_resize(output_buf, 0);
12741290
buf_appendf(output_buf, "%s\\Lib\\%s\\um\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
@@ -1316,5 +1332,7 @@ int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchTy
13161332
}
13171333
}
13181334
return ErrorFileNotFound;
1319-
}
1335+
#else
1336+
return ErrorFileNotFound;
13201337
#endif
1338+
}

‎src/os.hpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,27 @@ bool os_is_sep(uint8_t c);
7676

7777
int os_self_exe_path(Buf *out_path);
7878

79-
#if defined(__APPLE__)
80-
#define ZIG_OS_DARWIN
81-
#elif defined(_WIN32)
82-
#define ZIG_OS_WINDOWS
83-
#elif defined(__linux__)
84-
#define ZIG_OS_LINUX
85-
#else
86-
#define ZIG_OS_UNKNOWN
87-
#endif
88-
8979
struct ZigWindowsSDK {
9080
Buf path10;
9181
Buf version10;
9282
Buf path81;
9383
Buf version81;
9484
};
95-
#if defined(ZIG_OS_WINDOWS)
85+
9686
int os_find_windows_sdk(ZigWindowsSDK **out_sdk);
9787
int os_get_win32_vcruntime_path(Buf *output_buf, ZigLLVM_ArchType platform_type);
9888
int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
9989
int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
10090
int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
91+
92+
#if defined(__APPLE__)
93+
#define ZIG_OS_DARWIN
94+
#elif defined(_WIN32)
95+
#define ZIG_OS_WINDOWS
96+
#elif defined(__linux__)
97+
#define ZIG_OS_LINUX
98+
#else
99+
#define ZIG_OS_UNKNOWN
101100
#endif
102101

103102
#if defined(__x86_64__)

0 commit comments

Comments
 (0)
Please sign in to comment.