Skip to content

Commit

Permalink
windows: use the same libc search within a compilation unit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Nov 2, 2017
1 parent f7837f4 commit abff1b6
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 66 deletions.
3 changes: 2 additions & 1 deletion src/all_types.hpp
Expand Up @@ -1462,10 +1462,11 @@ struct CodeGen {
bool have_winmain_crt_startup;
bool have_dllmain_crt_startup;
bool have_pub_panic;
ZigList<Buf*> libc_lib_dirs_list;
Buf *libc_lib_dir;
Buf *libc_static_lib_dir;
Buf *libc_include_dir;
Buf *msvc_lib_dir;
Buf *kernel32_lib_dir;
Buf *zig_lib_dir;
Buf *zig_std_dir;
Buf *zig_c_headers_dir;
Expand Down
73 changes: 36 additions & 37 deletions src/analyze.cpp
Expand Up @@ -3370,63 +3370,62 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
zig_unreachable();
}

static ZigWindowsSDK *get_windows_sdk(CodeGen *g) {
if (g->win_sdk == nullptr) {
if (os_find_windows_sdk(&g->win_sdk)) {
zig_panic("Unable to determine Windows SDK path.");
}
}
assert(g->win_sdk != nullptr);
return g->win_sdk;
}

void find_libc_include_path(CodeGen *g) {
#ifdef ZIG_OS_WINDOWS
if (!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
if (g->win_sdk == nullptr) {
if (os_find_windows_sdk(&g->win_sdk)) {
zig_panic("Unable to determine Windows SDK path.");
}
}
ZigWindowsSDK *sdk = get_windows_sdk(g);

if (g->zig_target.os == ZigLLVM_Win32) {
if (os_get_win32_ucrt_include_path(g->win_sdk, g->libc_include_dir)) {
if (os_get_win32_ucrt_include_path(sdk, g->libc_include_dir)) {
zig_panic("Unable to determine libc include path.");
}
}
}
return;
#endif

// TODO find libc at runtime for other operating systems
if(!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
zig_panic("Unable to determine libc include path.");
}
}

void find_libc_lib_path(CodeGen *g) {
#ifdef ZIG_OS_WINDOWS
if (g->zig_target.os == ZigLLVM_Win32) {
if (g->win_sdk == nullptr) {
if (os_find_windows_sdk(&g->win_sdk)) {
zig_panic("Unable to determine Windows SDK path.");
// later we can handle this better by reporting an error via the normal mechanism
if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0 ||
(g->zig_target.os == ZigLLVM_Win32 && (g->msvc_lib_dir == nullptr || g->kernel32_lib_dir == nullptr)))
{
if (g->zig_target.os == ZigLLVM_Win32) {
ZigWindowsSDK *sdk = get_windows_sdk(g);

Buf* vc_lib_dir = buf_alloc();
if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) {
zig_panic("Unable to determine vcruntime path.");
}
}

Buf* vc_lib_dir = buf_alloc();
if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) {
zig_panic("Unable to determine vcruntime path.");
}
Buf* ucrt_lib_path = buf_alloc();
if (os_get_win32_ucrt_lib_path(sdk, ucrt_lib_path, g->zig_target.arch.arch)) {
zig_panic("Unable to determine ucrt path.");
}

Buf* ucrt_lib_path = buf_alloc();
if (os_get_win32_ucrt_lib_path(g->win_sdk, ucrt_lib_path, g->zig_target.arch.arch)) {
zig_panic("Unable to determine ucrt path.");
}
Buf* kern_lib_path = buf_alloc();
if (os_get_win32_kern32_path(sdk, kern_lib_path, g->zig_target.arch.arch)) {
zig_panic("Unable to determine kernel32 path.");
}

Buf* kern_lib_path = buf_alloc();
if (os_get_win32_kern32_path(g->win_sdk, kern_lib_path, g->zig_target.arch.arch)) {
zig_panic("Unable to determine kernel32 path.");
g->msvc_lib_dir = vc_lib_dir;
g->libc_lib_dir = ucrt_lib_path;
g->kernel32_lib_dir = kern_lib_path;
} else {
zig_panic("Unable to determine libc lib path.");
}

g->libc_lib_dirs_list.append(vc_lib_dir);
g->libc_lib_dirs_list.append(ucrt_lib_path);
g->libc_lib_dirs_list.append(kern_lib_path);
}
return;
#endif

// later we can handle this better by reporting an error via the normal mechanism
if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0) {
zig_panic("Unable to determine libc lib path.");
}
if (!g->libc_static_lib_dir || buf_len(g->libc_static_lib_dir) == 0) {
zig_panic("Unable to determine libc static lib path.");
Expand Down
20 changes: 12 additions & 8 deletions src/codegen.cpp
Expand Up @@ -114,6 +114,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
g->libc_lib_dir = buf_create_from_str("");
g->libc_static_lib_dir = buf_create_from_str("");
g->libc_include_dir = buf_create_from_str("");
g->msvc_lib_dir = nullptr;
g->kernel32_lib_dir = nullptr;
g->each_lib_rpath = false;
} else {
// native compilation, we can rely on the configuration stuff
Expand All @@ -123,6 +125,8 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
g->libc_lib_dir = buf_create_from_str(ZIG_LIBC_LIB_DIR);
g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
g->msvc_lib_dir = nullptr; // find it at runtime
g->kernel32_lib_dir = nullptr; // find it at runtime

#ifdef ZIG_EACH_LIB_RPATH
g->each_lib_rpath = true;
Expand Down Expand Up @@ -221,20 +225,20 @@ void codegen_set_libc_include_dir(CodeGen *g, Buf *libc_include_dir) {
g->libc_include_dir = libc_include_dir;
}

void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker) {
g->dynamic_linker = dynamic_linker;
void codegen_set_msvc_lib_dir(CodeGen *g, Buf *msvc_lib_dir) {
g->msvc_lib_dir = msvc_lib_dir;
}

void codegen_add_lib_dir(CodeGen *g, const char *dir) {
g->lib_dirs.append(dir);
void codegen_set_kernel32_lib_dir(CodeGen *g, Buf *kernel32_lib_dir) {
g->kernel32_lib_dir = kernel32_lib_dir;
}

void codegen_set_ucrt_lib_dir(CodeGen *g, Buf *ucrt_lib_dir) {
g->libc_lib_dirs_list.append(ucrt_lib_dir);
void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker) {
g->dynamic_linker = dynamic_linker;
}

void codegen_set_kernel32_lib_dir(CodeGen *g, Buf *kernel32_lib_dir) {
g->libc_lib_dirs_list.append(kernel32_lib_dir);
void codegen_add_lib_dir(CodeGen *g, const char *dir) {
g->lib_dirs.append(dir);
}

void codegen_add_rpath(CodeGen *g, const char *name) {
Expand Down
2 changes: 1 addition & 1 deletion src/codegen.hpp
Expand Up @@ -30,7 +30,7 @@ void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
void codegen_set_libc_lib_dir(CodeGen *codegen, Buf *libc_lib_dir);
void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
void codegen_set_libc_include_dir(CodeGen *codegen, Buf *libc_include_dir);
void codegen_set_ucrt_lib_dir(CodeGen *g, Buf *ucrt_lib_dir);
void codegen_set_msvc_lib_dir(CodeGen *g, Buf *msvc_lib_dir);
void codegen_set_kernel32_lib_dir(CodeGen *codegen, Buf *kernel32_lib_dir);
void codegen_set_dynamic_linker(CodeGen *g, Buf *dynamic_linker);
void codegen_set_windows_subsystem(CodeGen *g, bool mwindows, bool mconsole);
Expand Down
10 changes: 5 additions & 5 deletions src/link.cpp
Expand Up @@ -402,11 +402,11 @@ static void construct_linker_job_coff(LinkJob *lj) {
lj->args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(&lj->out_file))));

if (g->libc_link_lib != nullptr) {
if (g->libc_link_lib != nullptr) {
for (uint32_t i = 0; i < g->libc_lib_dirs_list.length; ++i) {
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dirs_list.items[i]))));
}
}
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->msvc_lib_dir))));
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->kernel32_lib_dir))));

lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dir))));
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_static_lib_dir))));
}

if (lj->link_in_crt) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Expand Up @@ -758,7 +758,7 @@ int main(int argc, char **argv) {
if (libc_include_dir)
codegen_set_libc_include_dir(g, buf_create_from_str(libc_include_dir));
if (msvc_lib_dir)
codegen_set_ucrt_lib_dir(g, buf_create_from_str(msvc_lib_dir));
codegen_set_msvc_lib_dir(g, buf_create_from_str(msvc_lib_dir));
if (kernel32_lib_dir)
codegen_set_kernel32_lib_dir(g, buf_create_from_str(kernel32_lib_dir));
if (dynamic_linker)
Expand Down
22 changes: 20 additions & 2 deletions src/os.cpp
Expand Up @@ -1001,8 +1001,8 @@ void os_stderr_set_color(TermColor color) {
#endif
}

#if defined ZIG_OS_WINDOWS
int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {
#if defined(ZIG_OS_WINDOWS)
ZigWindowsSDK *result_sdk = allocate<ZigWindowsSDK>(1);
buf_resize(&result_sdk->path10, 0);
buf_resize(&result_sdk->path81, 0);
Expand Down Expand Up @@ -1099,9 +1099,13 @@ int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {

*out_sdk = result_sdk;
return 0;
#else
return ErrorFileNotFound;
#endif
}

int os_get_win32_vcruntime_path(Buf* output_buf, ZigLLVM_ArchType platform_type) {
#if defined(ZIG_OS_WINDOWS)
buf_resize(output_buf, 0);
//COM Smart Pointerse requires explicit scope
{
Expand Down Expand Up @@ -1226,9 +1230,13 @@ com_done:;
buf_resize(output_buf, 0);
return ErrorFileNotFound;
}
#else
return ErrorFileNotFound;
#endif
}

int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchType platform_type) {
#if defined(ZIG_OS_WINDOWS)
buf_resize(output_buf, 0);
buf_appendf(output_buf, "%s\\Lib\\%s\\ucrt\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
switch (platform_type) {
Expand All @@ -1254,9 +1262,13 @@ int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_Arch
buf_resize(output_buf, 0);
return ErrorFileNotFound;
}
#else
return ErrorFileNotFound;
#endif
}

int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf* output_buf) {
#if defined(ZIG_OS_WINDOWS)
buf_resize(output_buf, 0);
buf_appendf(output_buf, "%s\\Include\\%s\\ucrt", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
if (GetFileAttributesA(buf_ptr(output_buf)) != INVALID_FILE_ATTRIBUTES) {
Expand All @@ -1266,9 +1278,13 @@ int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf* output_buf) {
buf_resize(output_buf, 0);
return ErrorFileNotFound;
}
#else
return ErrorFileNotFound;
#endif
}

int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchType platform_type) {
#if defined(ZIG_OS_WINDOWS)
{
buf_resize(output_buf, 0);
buf_appendf(output_buf, "%s\\Lib\\%s\\um\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
Expand Down Expand Up @@ -1316,5 +1332,7 @@ int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchTy
}
}
return ErrorFileNotFound;
}
#else
return ErrorFileNotFound;
#endif
}
21 changes: 10 additions & 11 deletions src/os.hpp
Expand Up @@ -76,28 +76,27 @@ bool os_is_sep(uint8_t c);

int os_self_exe_path(Buf *out_path);

#if defined(__APPLE__)
#define ZIG_OS_DARWIN
#elif defined(_WIN32)
#define ZIG_OS_WINDOWS
#elif defined(__linux__)
#define ZIG_OS_LINUX
#else
#define ZIG_OS_UNKNOWN
#endif

struct ZigWindowsSDK {
Buf path10;
Buf version10;
Buf path81;
Buf version81;
};
#if defined(ZIG_OS_WINDOWS)

int os_find_windows_sdk(ZigWindowsSDK **out_sdk);
int os_get_win32_vcruntime_path(Buf *output_buf, ZigLLVM_ArchType platform_type);
int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);
int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);

#if defined(__APPLE__)
#define ZIG_OS_DARWIN
#elif defined(_WIN32)
#define ZIG_OS_WINDOWS
#elif defined(__linux__)
#define ZIG_OS_LINUX
#else
#define ZIG_OS_UNKNOWN
#endif

#if defined(__x86_64__)
Expand Down

0 comments on commit abff1b6

Please sign in to comment.