-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Unable to build stage3 on Windows: FileNotFound
followed by Unable to dump stack trace: InvalidPEMagic
#12703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I ran into this the other day. Note that the build command references some files that definitely don't exist, e.g.
Normally these are located somewhere under I'm not sure what the cause of this is since I haven't bothered building a debug version of LLVM, and I've had other strange problems with MSVC builds (like #12664) so I just switched to zig-bootstrap. It's honestly a much smoother experience and seems to be the recommended way going forward. |
Ah, good catch, I had missed those obviously wrong lib paths. Maybe something broke in the stage3 cmake stuff? For completeness I tried building with MSYS2 using clang/clang++, but this time it fails to build zig2.exe:
|
I built a debug LLVM (MTd) and attempting to build Zig debug results in this:
I imagine the stack trace breakage is related to the Note for anyone else trying this, this patch is required as
|
Narrowed down the unknown stack trace when building stage3 with the debugger:
|
I made a patch that fixes up how system libs are passed through, and I can get a bit further: diff --git a/build.zig b/build.zig
index c8e757dc4..7a614588e 100644
--- a/build.zig
+++ b/build.zig
@@ -555,6 +555,7 @@ fn addCmakeCfgOptionsToExe(
addCMakeLibraryList(exe, cfg.clang_libraries);
addCMakeLibraryList(exe, cfg.lld_libraries);
addCMakeLibraryList(exe, cfg.llvm_libraries);
+ addCMakeSystemLibraryList(exe, cfg.llvm_system_libraries);
if (use_zig_libcxx) {
exe.linkLibCpp();
@@ -674,6 +675,23 @@ fn addCMakeLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) void {
}
}
+fn addCMakeSystemLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) void {
+ var it = mem.tokenize(u8, list, ";");
+ while (it.next()) |lib| {
+ var start_offset: usize = 0;
+ var end_offset: usize = 0;
+ if (mem.startsWith(u8, lib, "-l")) {
+ start_offset = "-l".len;
+ }
+
+ if (exe.target.isWindows() and mem.endsWith(u8, lib, ".lib")) {
+ end_offset = ".lib".len;
+ }
+
+ exe.linkSystemLibrary(lib[start_offset..lib.len - end_offset]);
+ }
+}
+
const CMakeConfig = struct {
llvm_linkage: std.build.LibExeObjStep.Linkage,
cmake_binary_dir: []const u8,
@@ -683,6 +701,7 @@ const CMakeConfig = struct {
lld_libraries: []const u8,
clang_libraries: []const u8,
llvm_libraries: []const u8,
+ llvm_system_libraries: []const u8,
dia_guids_lib: []const u8,
};
@@ -744,6 +763,7 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig {
.lld_libraries = undefined,
.clang_libraries = undefined,
.llvm_libraries = undefined,
+ .llvm_system_libraries = undefined,
.dia_guids_lib = undefined,
};
@@ -776,6 +796,10 @@ fn parseConfigH(b: *Builder, config_h_text: []const u8) ?CMakeConfig {
.prefix = "#define ZIG_LLVM_LIBRARIES ",
.field = "llvm_libraries",
},
+ .{
+ .prefix = "#define ZIG_LLVM_SYSTEM_LIBRARIES ",
+ .field = "llvm_system_libraries",
+ },
.{
.prefix = "#define ZIG_DIA_GUIDS_LIB ",
.field = "dia_guids_lib",
diff --git a/cmake/Findllvm.cmake b/cmake/Findllvm.cmake
index d55abd2dc..a8c556466 100644
--- a/cmake/Findllvm.cmake
+++ b/cmake/Findllvm.cmake
@@ -5,6 +5,7 @@
# LLVM_FOUND
# LLVM_INCLUDE_DIRS
# LLVM_LIBRARIES
+# LLVM_SYSTEM_LIBRARIES
# LLVM_LIBDIRS
# LLVM_LINK_MODE
@@ -172,9 +173,9 @@ if(ZIG_USE_LLVM_CONFIG)
OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE " " ";" LLVM_STATIC_SYSTEM_LIBS "${LLVM_STATIC_SYSTEM_LIBS_SPACES}")
- set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS} ${LLVM_STATIC_SYSTEM_LIBS})
+ set(LLVM_SYSTEM_LIBRARIES ${LLVM_SYSTEM_LIBS} ${LLVM_STATIC_SYSTEM_LIBS})
else()
- set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS})
+ set(LLVM_SYSTEM_LIBRARIES ${LLVM_SYSTEM_LIBS})
endif()
execute_process(
@@ -366,7 +367,11 @@ else()
find_path(LLVM_INCLUDE_DIRS NAMES llvm/IR/IRBuilder.h)
endif()
+if(NOT LLVM_SYSTEM_LIBRARIES)
+ set(LLVM_SYSTEM_LIBRARIES "")
+endif()
+
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(llvm DEFAULT_MSG LLVM_LIBRARIES LLVM_INCLUDE_DIRS)
-mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARIES LLVM_LIBDIRS)
+mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARIES LLVM_SYSTEM_LIBRARIES LLVM_LIBDIRS)
diff --git a/src/stage1/config.h.in b/src/stage1/config.h.in
index 2be083999..fe8b35d90 100644
--- a/src/stage1/config.h.in
+++ b/src/stage1/config.h.in
@@ -23,6 +23,7 @@
#define ZIG_LLD_LIBRARIES "@LLD_LIBRARIES@"
#define ZIG_CLANG_LIBRARIES "@CLANG_LIBRARIES@"
#define ZIG_LLVM_LIBRARIES "@LLVM_LIBRARIES@"
+#define ZIG_LLVM_SYSTEM_LIBRARIES "@LLVM_SYSTEM_LIBRARIES@"
#define ZIG_DIA_GUIDS_LIB "@ZIG_DIA_GUIDS_LIB_ESCAPED@"
#endif This gets a bit further, where linking fails with:
|
Update, this is still happening on latest master with llvm 15.0.2 - with the same patch above I get the |
Updated for llvm 15.0.3: Still have the
The strange thing is that if I re-run the build after this, it doesn't notice that zig.exe wasn't built and tries to run the install step. I had to add this: diff --git a/CMakeLists.txt b/CMakeLists.txt
index cb2e2e2d6..5c0c38fbf 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -170,6 +170,7 @@ foreach(CONFIG_TYPE ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${CONFIG_TYPE} CONFIG_TYPE)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${ZIG_CPP_LIB_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${ZIG_CPP_LIB_DIR})
+ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONFIG_TYPE} ${CMAKE_BINARY_DIR})
endforeach(CONFIG_TYPE CMAKE_CONFIGURATION_TYPES)
include_directories(${LLVM_INCLUDE_DIRS}) To get the install working (otherwise zig2.exe ends up in Release/ instead of the root dir). Once that is running it ends up failing reading a file, with this trace from procmon:
I'm assuming this is the missing zig.exe that wasn't linked due to the atexit failure. It's seems like a bug that the build didn't try to rebuild this since it was missing. A |
I think the issue may be caused by LLVM being built by MSVC, but the zig target defaulting to gnu. This is why it links crt2.obj (mingw) but LIBCMT.lib comes from a dependency from llvm:
|
For completeness, here is the --verbose-link output with the failing command:
|
I believe the I ran into some compile errors with this, within libcxxabi:
It seems like using libcxxabi to target the MSVC ABI isn't a supported use case. There are a few FIXME in the llvm codebase around this, and also some issues with new.h and
It seems that libcxxabi is meant for Itanium conventions and not x86_64. So I tried just not building/linking libcxxabi, since oin the main llvm-project for windows, this doesn't seem to be a thing: From
My thinking was maybe the msvc runtime would fill in the blanks there: diff --git a/src/Compilation.zig b/src/Compilation.zig
index 958aac5e1..3b11de441 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -2036,7 +2036,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
}
if (build_options.have_llvm and is_exe_or_dyn_lib and comp.bin_file.options.link_libcpp) {
try comp.work_queue.writeItem(.libcxx);
- try comp.work_queue.writeItem(.libcxxabi);
+ if (target.os.tag != .windows and target.abi != .msvc) {
+ try comp.work_queue.writeItem(.libcxxabi);
+ }
}
if (build_options.have_llvm and comp.bin_file.options.tsan) {
try comp.work_queue.writeItem(.libtsan);
--- a/src/link/Coff/lld.zig
+++ b/src/link/Coff/lld.zig
@@ -451,7 +451,10 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
// libc++ dep
if (self.base.options.link_libcpp) {
- try argv.append(comp.libcxxabi_static_lib.?.full_object_path);
+ if (comp.libcxxabi_static_lib) |libcxxabi_static_lib| {
+ try argv.append(libcxxabi_static_lib.full_object_path);
+ }
+
try argv.append(comp.libcxx_static_lib.?.full_object_path);
} This compiles, but fails during linking with a Changing the name in `libcxx.zig (c++ -> libc++, plus a few changes to match closer with what the llvm-project cmake scripts do): diff --git a/src/libcxx.zig b/src/libcxx.zig
index 850da698c..d646f0660 100644
--- a/src/libcxx.zig
+++ b/src/libcxx.zig
@@ -108,7 +108,8 @@ pub fn buildLibCXX(comp: *Compilation) !void {
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
- const root_name = "c++";
+ const root_name = "libc++";
+
const output_mode = .Lib;
const link_mode = .Static;
const target = comp.getTarget();
@@ -160,10 +161,19 @@ pub fn buildLibCXX(comp: *Compilation) !void {
try cflags.append("-DNDEBUG");
try cflags.append("-D_LIBCPP_BUILDING_LIBRARY");
try cflags.append("-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER");
- try cflags.append("-DLIBCXX_BUILDING_LIBCXXABI");
+
+ if (!(target.os.tag == .windows and target.abi == .msvc)) {
+ try cflags.append("-DLIBCXX_BUILDING_LIBCXXABI");
+ }
+
try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS");
try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS");
- try cflags.append("-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS");
+
+ if (!(target.os.tag == .windows and target.abi == .msvc)) {
+ // https://reviews.llvm.org/D60717
+ try cflags.append("-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS");
+ }
+
try cflags.append("-D_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS");
try cflags.append(abi_version_arg);
@@ -311,6 +321,9 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
std.mem.startsWith(u8, cxxabi_src, "src/cxa_personality.cpp"))
continue;
try cflags.append("-fno-exceptions");
+ } else {
+ if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_noexception.cpp"))
+ continue;
}
// WASM targets are single threaded.
@@ -327,6 +340,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
try cflags.append("-D_LIBCPP_DISABLE_EXTERN_TEMPLATE");
try cflags.append("-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS");
try cflags.append("-D_LIBCXXABI_BUILDING_LIBRARY");
+ try cflags.append("-D_LIBCPP_BUILDING_LIBRARY");
try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS");
try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); This results in some different duplicate symbol issues:
If
This moves things a bit further, but now symbols are missing:
Maybe related: llvm/llvm-project#58318 |
Zig Version
26e9839
Steps to Reproduce
Within the zig folder:
llvm/clang/lld built with MSVC as per instructions on the wiki
Expected Behavior
stage3
buildsActual Behavior
The failing part of the build:
The text was updated successfully, but these errors were encountered: