Skip to content

Commit 0471eea

Browse files
committed
build: first pass on geting stage3 building under x64_64-windows-msvc
1 parent c3945d9 commit 0471eea

6 files changed

Lines changed: 46 additions & 10 deletions

File tree

CMakeLists.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ set(ZIG_STATIC off CACHE BOOL "Attempt to build a static zig executable (not com
9191
set(ZIG_SHARED_LLVM off CACHE BOOL "Prefer linking against shared LLVM libraries")
9292
set(ZIG_STATIC_LLVM off CACHE BOOL "Prefer linking against static LLVM libraries")
9393
set(ZIG_STATIC_ZLIB off CACHE BOOL "Prefer linking against static zlib")
94+
set(ZIG_ENABLE_ZSTD on CACHE BOOL "Enable linking zstd")
9495
set(ZIG_STATIC_ZSTD off CACHE BOOL "Prefer linking against static zstd")
9596
set(ZIG_USE_CCACHE off CACHE BOOL "Use ccache")
9697

@@ -138,19 +139,24 @@ find_package(clang 15)
138139
find_package(lld 15)
139140

140141
if(ZIG_STATIC_ZLIB)
141-
list(REMOVE_ITEM LLVM_LIBRARIES "-lz")
142+
if (MSVC)
143+
list(REMOVE_ITEM LLVM_SYSTEM_LIBRARIES "z.lib")
144+
else()
145+
list(REMOVE_ITEM LLVM_SYSTEM_LIBRARIES "-lz")
146+
endif()
147+
142148
find_library(ZLIB NAMES libz.a libzlibstatic.a z zlib libz NAMES_PER_DIR)
143149
list(APPEND LLVM_LIBRARIES "${ZLIB}")
144150
endif()
145151

146-
if(ZIG_STATIC_ZSTD)
147-
list(REMOVE_ITEM LLVM_LIBRARIES "-lzstd")
152+
if(ZIG_STATIC_ZSTD AND ZIG_ENABLE_ZSTD)
153+
list(REMOVE_ITEM LLVM_SYSTEM_LIBRARIES "-lzstd")
148154
find_library(ZSTD NAMES libzstd.a libzstdstatic.a zstd NAMES_PER_DIR)
149155
list(APPEND LLVM_LIBRARIES "${ZSTD}")
150156
endif()
151157

152158
if(APPLE AND ZIG_STATIC)
153-
list(REMOVE_ITEM LLVM_LIBRARIES "-lcurses")
159+
list(REMOVE_ITEM LLVM_SYSTEM_LIBRARIES "-lcurses")
154160
find_library(CURSES NAMES libcurses.a libncurses.a NAMES_PER_DIR
155161
PATHS
156162
/usr/local/opt/ncurses/lib
@@ -706,6 +712,7 @@ target_link_libraries(zigcpp LINK_PUBLIC
706712
${CLANG_LIBRARIES}
707713
${LLD_LIBRARIES}
708714
${LLVM_LIBRARIES}
715+
${LLVM_SYSTEM_LIBRARIES}
709716
${CMAKE_THREAD_LIBS_INIT}
710717
)
711718

@@ -839,7 +846,7 @@ if(ZIG_SINGLE_THREADED)
839846
else()
840847
set(ZIG_SINGLE_THREADED_ARG "")
841848
endif()
842-
if(ZIG_STATIC)
849+
if(ZIG_STATIC AND NOT MSVC)
843850
set(ZIG_STATIC_ARG "-Duse-zig-libcxx")
844851
else()
845852
set(ZIG_STATIC_ARG "")

build.zig

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ fn addCmakeCfgOptionsToExe(
552552
addCMakeLibraryList(exe, cfg.clang_libraries);
553553
addCMakeLibraryList(exe, cfg.lld_libraries);
554554
addCMakeLibraryList(exe, cfg.llvm_libraries);
555+
addCMakeSystemLibraryList(exe, cfg.clang_system_libraries);
555556
addCMakeSystemLibraryList(exe, cfg.llvm_system_libraries);
556557

557558
if (use_zig_libcxx) {
@@ -627,10 +628,20 @@ fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void {
627628
}
628629

629630
exe.linkSystemLibrary("z");
630-
exe.linkSystemLibrary("zstd");
631631

632-
// This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
633-
exe.linkSystemLibrary("c++");
632+
if (exe.target.getOs().tag != .windows and exe.target.getAbi() != .msvc) {
633+
// TODO: Support this on msvc
634+
exe.linkSystemLibrary("zstd");
635+
636+
// This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
637+
exe.linkSystemLibrary("c++");
638+
}
639+
640+
if (exe.target.getOs().tag == .windows) {
641+
exe.linkSystemLibrary("version");
642+
exe.linkSystemLibrary("uuid");
643+
exe.linkSystemLibrary("ole32");
644+
}
634645
}
635646

636647
fn addCxxKnownPath(
@@ -707,6 +718,7 @@ const CMakeConfig = struct {
707718
lld_include_dir: []const u8,
708719
lld_libraries: []const u8,
709720
clang_libraries: []const u8,
721+
clang_system_libraries: []const u8,
710722
llvm_lib_dir: []const u8,
711723
llvm_include_dir: []const u8,
712724
llvm_libraries: []const u8,
@@ -773,6 +785,7 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig {
773785
.lld_include_dir = undefined,
774786
.lld_libraries = undefined,
775787
.clang_libraries = undefined,
788+
.clang_system_libraries = undefined,
776789
.llvm_lib_dir = undefined,
777790
.llvm_include_dir = undefined,
778791
.llvm_libraries = undefined,
@@ -813,6 +826,10 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig {
813826
.prefix = "#define ZIG_CLANG_LIBRARIES ",
814827
.field = "clang_libraries",
815828
},
829+
.{
830+
.prefix = "#define ZIG_CLANG_SYSTEM_LIBRARIES ",
831+
.field = "clang_system_libraries",
832+
},
816833
.{
817834
.prefix = "#define ZIG_LLVM_LIBRARIES ",
818835
.field = "llvm_libraries",

cmake/Findclang.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# CLANG_FOUND
66
# CLANG_INCLUDE_DIRS
77
# CLANG_LIBRARIES
8+
# CLANG_SYSTEM_LIBRARIES
89
# CLANG_LIBDIRS
910

1011
find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
@@ -68,6 +69,10 @@ else()
6869
FIND_AND_ADD_CLANG_LIB(clangSupport)
6970
endif()
7071

72+
if (MSVC)
73+
set(CLANG_SYSTEM_LIBRARIES "version.lib")
74+
endif()
75+
7176
include(FindPackageHandleStandardArgs)
7277
find_package_handle_standard_args(clang DEFAULT_MSG CLANG_LIBRARIES CLANG_INCLUDE_DIRS)
7378

src/Compilation.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,8 +3297,8 @@ fn processOneJob(comp: *Compilation, job: Job) !void {
32973297
// TODO Surface more error details.
32983298
comp.lockAndSetMiscFailure(
32993299
.windows_import_lib,
3300-
"unable to generate DLL import .lib file: {s}",
3301-
.{@errorName(err)},
3300+
"unable to generate DLL import .lib file for {s}: {s}",
3301+
.{link_lib, @errorName(err)},
33023302
);
33033303
};
33043304
},

src/link/Coff/lld.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
486486
continue;
487487
}
488488
}
489+
if (target.abi == .msvc) { // TODO: Do this at the top, if we detect we're using the native libc?
490+
log.warn("adding system lib {s}", .{ lib_basename });
491+
argv.appendAssumeCapacity(lib_basename);
492+
continue;
493+
}
494+
489495
log.err("DLL import library for -l{s} not found", .{key});
490496
return error.DllImportLibraryNotFound;
491497
}

stage1/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
// Used by build.zig for communicating build information to self hosted build.
1818
#define ZIG_CLANG_LIBRARIES "@CLANG_LIBRARIES@"
19+
#define ZIG_CLANG_SYSTEM_LIBRARIES "@CLANG_SYSTEM_LIBRARIES@"
1920
#define ZIG_CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@"
2021
#define ZIG_CMAKE_PREFIX_PATH "@ZIG_CMAKE_PREFIX_PATH@"
2122
#define ZIG_CMAKE_STATIC_LIBRARY_PREFIX "@CMAKE_STATIC_LIBRARY_PREFIX@"

0 commit comments

Comments
 (0)