Skip to content

Commit 8a344fa

Browse files
authored
Merge pull request #13036 from BratishkaErik/fix-installing
2 parents 7d6596e + cb635e0 commit 8a344fa

File tree

8 files changed

+77
-34
lines changed

8 files changed

+77
-34
lines changed

CMakeLists.txt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,9 +1017,9 @@ if("${ZIG_EXECUTABLE}" STREQUAL "")
10171017
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
10181018
)
10191019
if (WIN32)
1020-
set(ZIG_EXECUTABLE "${zig2_BINARY_DIR}/zig2.exe")
1020+
set(ZIG_EXECUTABLE "${CMAKE_BINARY_DIR}/zig2.exe")
10211021
else()
1022-
set(ZIG_EXECUTABLE "${zig2_BINARY_DIR}/zig2")
1022+
set(ZIG_EXECUTABLE "${CMAKE_BINARY_DIR}/zig2")
10231023
endif()
10241024
else()
10251025
add_custom_command(
@@ -1045,15 +1045,8 @@ elseif(MINGW)
10451045
target_link_libraries(zig2 ntdll)
10461046
endif()
10471047

1048-
# Dummy install command so that the "install" target is not missing.
1049-
# This is redundant from the "stage3" custom target below.
1050-
if(NOT ZIG_SKIP_INSTALL_LIB_FILES)
1051-
install(FILES "lib/compiler_rt.zig" DESTINATION "lib/zig")
1052-
endif()
1053-
1054-
set(ZIG_INSTALL_ARGS "build"
1048+
set(ZIG_BUILD_ARGS
10551049
--zig-lib-dir "${CMAKE_SOURCE_DIR}/lib"
1056-
--prefix "${CMAKE_INSTALL_PREFIX}"
10571050
"-Dconfig_h=${ZIG_CONFIG_H_OUT}"
10581051
"-Denable-llvm"
10591052
"-Denable-stage1"
@@ -1067,8 +1060,14 @@ set(ZIG_INSTALL_ARGS "build"
10671060
)
10681061

10691062
add_custom_target(stage3 ALL
1070-
COMMAND zig2 ${ZIG_INSTALL_ARGS}
1063+
COMMAND zig2 build compile ${ZIG_BUILD_ARGS}
10711064
DEPENDS zig2
10721065
COMMENT STATUS "Building stage3"
10731066
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
10741067
)
1068+
1069+
install(CODE "set(ZIG_EXECUTABLE \"${ZIG_EXECUTABLE}\")")
1070+
install(CODE "set(ZIG_BUILD_ARGS \"${ZIG_BUILD_ARGS}\")")
1071+
install(CODE "set(CMAKE_INSTALL_PREFIX \"${CMAKE_INSTALL_PREFIX}\")")
1072+
install(CODE "set(CMAKE_SOURCE_DIR \"${CMAKE_SOURCE_DIR}\")")
1073+
install(SCRIPT "${CMAKE_SOURCE_DIR}/cmake/install.cmake")

build.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ pub fn build(b: *Builder) !void {
142142
};
143143

144144
const exe = b.addExecutable("zig", main_file);
145+
146+
const compile_step = b.step("compile", "Build the self-hosted compiler");
147+
compile_step.dependOn(&exe.step);
148+
145149
exe.stack_size = stack_size;
146150
exe.strip = strip;
147151
exe.sanitize_thread = sanitize_thread;

cmake/install.cmake

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
set(ZIG_INSTALL_ARGS build ${ZIG_BUILD_ARGS} --prefix "${CMAKE_INSTALL_PREFIX}")
2+
execute_process(
3+
COMMAND "${ZIG_EXECUTABLE}" ${ZIG_INSTALL_ARGS}
4+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
5+
RESULT_VARIABLE _result)
6+
7+
if(_result)
8+
message("::")
9+
message(":: ERROR: ${_result}")
10+
message(":: (execute_process)")
11+
12+
list(JOIN ZIG_INSTALL_ARGS " " s_INSTALL_LIBSTAGE2_ARGS)
13+
message("::")
14+
message(":: argv: ${ZIG_EXECUTABLE} ${s_INSTALL_LIBSTAGE2_ARGS}")
15+
16+
set(_args ${ZIG_EXECUTABLE} ${ZIG_INSTALL_ARGS})
17+
list(LENGTH _args _len)
18+
math(EXPR _len "${_len} - 1")
19+
message("::")
20+
foreach(_i RANGE 0 ${_len})
21+
list(GET _args ${_i} _arg)
22+
message(":: argv[${_i}]: ${_arg}")
23+
endforeach()
24+
25+
message("::")
26+
message(FATAL_ERROR)
27+
endif()

lib/std/os/windows.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN
134134
.OBJECT_NAME_COLLISION => return error.PathAlreadyExists,
135135
.FILE_IS_A_DIRECTORY => return error.IsDir,
136136
.NOT_A_DIRECTORY => return error.NotDir,
137+
.INVALID_HANDLE => unreachable,
137138
else => return unexpectedStatus(rc),
138139
}
139140
}

lib/std/zig/system/NativePaths.zig

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
9696
return self;
9797
}
9898

99-
if (comptime native_target.os.tag == .solaris) {
99+
if (builtin.os.tag == .solaris) {
100100
try self.addLibDir("/usr/lib/64");
101101
try self.addLibDir("/usr/local/lib/64");
102102
try self.addLibDir("/lib/64");
@@ -107,7 +107,7 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
107107
return self;
108108
}
109109

110-
if (native_target.os.tag != .windows) {
110+
if (builtin.os.tag != .windows) {
111111
const triple = try native_target.linuxTriple(allocator);
112112
defer allocator.free(triple);
113113

@@ -136,11 +136,10 @@ pub fn detect(allocator: Allocator, native_info: NativeTargetInfo) !NativePaths
136136
// libz.so.1 is in /lib/x86_64-linux-gnu (added here)
137137
try self.addLibDirFmt("/lib/{s}", .{triple});
138138

139-
// NOTE: distro like guix doesn't use FHS, so it relies on envorinment
140-
// variables (C_INCLUDE_PATH, CPLUS_INCLUDE_PATH and LIBRARY_PATH) to
141-
// search for headers and libraries
142-
// NOTE: we use os.getenv here since this part won't be executed on
143-
// windows, to get rid of unnecessary error handling
139+
// Distros like guix don't use FHS, so they rely on environment
140+
// variables to search for headers and libraries.
141+
// We use os.getenv here since this part won't be executed on
142+
// windows, to get rid of unnecessary error handling.
144143
if (std.os.getenv("C_INCLUDE_PATH")) |c_include_path| {
145144
var it = mem.tokenize(u8, c_include_path, ":");
146145
while (it.next()) |dir| {

src/Compilation.zig

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,11 +1109,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11091109

11101110
const use_stage1 = options.use_stage1 orelse false;
11111111

1112-
const cache_mode = if (use_stage1 and !options.disable_lld_caching)
1113-
CacheMode.whole
1114-
else
1115-
options.cache_mode;
1116-
11171112
// Make a decision on whether to use LLVM or our own backend.
11181113
const use_llvm = build_options.have_llvm and blk: {
11191114
if (options.use_llvm) |explicit|
@@ -1154,6 +1149,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11541149
}
11551150
}
11561151

1152+
// TODO: once we support incremental compilation for the LLVM backend via
1153+
// saving the LLVM module into a bitcode file and restoring it, along with
1154+
// compiler state, the second clause here can be removed so that incremental
1155+
// cache mode is used for LLVM backend too. We need some fuzz testing before
1156+
// that can be enabled.
1157+
const cache_mode = if ((use_stage1 and !options.disable_lld_caching) or
1158+
(use_llvm and !options.disable_lld_caching)) CacheMode.whole else options.cache_mode;
1159+
11571160
const tsan = options.want_tsan orelse false;
11581161
// TSAN is implemented in C++ so it requires linking libc++.
11591162
const link_libcpp = options.link_libcpp or tsan;
@@ -2387,9 +2390,21 @@ pub fn update(comp: *Compilation) !void {
23872390
const o_sub_path = try std.fs.path.join(comp.gpa, &[_][]const u8{ "o", &digest });
23882391
defer comp.gpa.free(o_sub_path);
23892392

2393+
// Work around windows `AccessDenied` if any files within this directory are open
2394+
// by doing the makeExecutable/makeWritable dance.
2395+
const need_writable_dance = builtin.os.tag == .windows and comp.bin_file.file != null;
2396+
if (need_writable_dance) {
2397+
try comp.bin_file.makeExecutable();
2398+
}
2399+
23902400
try comp.bin_file.renameTmpIntoCache(comp.local_cache_directory, tmp_dir_sub_path, o_sub_path);
23912401
comp.wholeCacheModeSetBinFilePath(&digest);
23922402

2403+
// Has to be after the `wholeCacheModeSetBinFilePath` above.
2404+
if (need_writable_dance) {
2405+
try comp.bin_file.makeWritable();
2406+
}
2407+
23932408
// This is intentionally sandwiched between renameTmpIntoCache() and writeManifest().
23942409
if (comp.bin_file.options.module) |module| {
23952410
// We need to set the zig_cache_artifact_directory for -femit-asm, -femit-llvm-ir,
@@ -3204,8 +3219,8 @@ fn processOneJob(comp: *Compilation, job: Job) !void {
32043219
// TODO Surface more error details.
32053220
comp.lockAndSetMiscFailure(
32063221
.mingw_crt_file,
3207-
"unable to build mingw-w64 CRT file: {s}",
3208-
.{@errorName(err)},
3222+
"unable to build mingw-w64 CRT file {s}: {s}",
3223+
.{ @tagName(crt_file), @errorName(err) },
32093224
);
32103225
};
32113226
},

src/link.zig

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,8 @@ pub const File = struct {
403403
try emit.directory.handle.copyFile(emit.sub_path, emit.directory.handle, emit.sub_path, .{});
404404
}
405405
}
406-
if (base.intermediary_basename == null) {
407-
f.close();
408-
base.file = null;
409-
}
406+
f.close();
407+
base.file = null;
410408
},
411409
.coff, .elf, .plan9, .wasm => if (base.file) |f| {
412410
if (base.intermediary_basename != null) {
@@ -777,7 +775,7 @@ pub const File = struct {
777775
_ = base;
778776
while (true) {
779777
if (builtin.os.tag == .windows) {
780-
// workaround windows `renameW` can't fail with `PathAlreadyExists`
778+
// Work around windows `renameW` can't fail with `PathAlreadyExists`
781779
// See https://github.com/ziglang/zig/issues/8362
782780
if (cache_directory.handle.access(o_sub_path, .{})) |_| {
783781
try cache_directory.handle.deleteTree(o_sub_path);
@@ -791,9 +789,9 @@ pub const File = struct {
791789
tmp_dir_sub_path,
792790
cache_directory.handle,
793791
o_sub_path,
794-
) catch |err| switch (err) {
795-
error.AccessDenied => unreachable, // We are most likely trying to move a dir with open handles to its resources
796-
else => |e| return e,
792+
) catch |err| {
793+
log.err("unable to rename cache dir {s} to {s}: {s}", .{ tmp_dir_sub_path, o_sub_path, @errorName(err) });
794+
return err;
797795
};
798796
break;
799797
} else {

src/link/Elf.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
12821282
// linked are in the hash that namespaces the directory we are outputting to. Therefore,
12831283
// we must hash those now, and the resulting digest will form the "id" of the linking
12841284
// job we are about to perform.
1285-
// After a successful link, we store the id in the metadata of a symlink named "id.txt" in
1285+
// After a successful link, we store the id in the metadata of a symlink named "lld.id" in
12861286
// the artifact directory. So, now, we check if this symlink exists, and if it matches
12871287
// our digest. If so, we can skip linking. Otherwise, we proceed with invoking LLD.
12881288
const id_symlink_basename = "lld.id";

0 commit comments

Comments
 (0)