From a6ec559438d33c5439d2366f445566296d1721b0 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sun, 26 Feb 2023 00:22:07 +0000 Subject: [PATCH 01/29] Add some CompileStep APIs to Build.Module. Functions added to Module so far: * `addIncludePath` * `addConfigHeader * `addLibraryPath` * `linkLibC` * `linkLibCpp` * `linkLibrary` * `linkSystemLibrary` --- lib/std/Build.zig | 47 ++++++++++++++++++++++++++++++++--- lib/std/Build/CompileStep.zig | 29 +++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 26919962e341..e9e6da72371b 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -556,11 +556,13 @@ pub const AddModuleOptions = struct { dependencies: []const ModuleDependency = &.{}, }; -pub fn addModule(b: *Build, options: AddModuleOptions) void { - b.modules.put(b.dupe(options.name), b.createModule(.{ +pub fn addModule(b: *Build, options: AddModuleOptions) *Module { + const module = b.createModule(.{ .source_file = options.source_file, .dependencies = options.dependencies, - })) catch @panic("OOM"); + }); + b.modules.put(b.dupe(options.name), module) catch @panic("OOM"); + return module; } pub const ModuleDependency = struct { @@ -1596,6 +1598,45 @@ pub const Module = struct { /// file of directory of files which constitute the module. source_file: FileSource, dependencies: std.StringArrayHashMap(*Module), + include_dirs: std.ArrayListUnmanaged(CompileStep.IncludeDir) = .{}, + lib_paths: std.ArrayListUnmanaged([]const u8) = .{}, + system_libs: std.ArrayListUnmanaged([]const u8) = .{}, + libs: std.ArrayListUnmanaged(*Build.CompileStep) = .{}, + config_headers: std.ArrayListUnmanaged(*Build.ConfigHeaderStep) = .{}, + link_libc: bool = false, + link_libcpp: bool = false, + + pub fn addIncludePath(m: *Module, path: []const u8) void { + m.include_dirs.append( + m.builder.allocator, + CompileStep.IncludeDir{ .raw_path = m.builder.dupe(path) }, + ) catch @panic("OOM"); + } + + pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { + m.config_headers.append(config_header) catch @panic("OOM"); + } + + pub fn addLibraryPath(m: *Module, library_path: []const u8) void { + m.lib_paths.append(m.builder.dupe(library_path)) catch @panic("OOM"); + } + + pub fn linkLibC(m: *Module) void { + m.link_libc = true; + } + + pub fn linkLibCpp(m: *Module) void { + m.link_libcpp = true; + } + + pub fn linkLibrary(m: *Module, lib: *Build.CompileStep) void { + m.libs.append(m.builder.allocator, lib) catch @panic("OOM"); + } + + pub fn linkSystemLibrary(m: *Module, lib: []const u8) void { + // TODO: Ignoring `needed` and `weak` variants for now. + m.system_libs.append(m.builder.allocator, m.builder.dupe(lib)) catch @panic("OOM"); + } }; /// A file that is generated by a build step. diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index db663fc767ad..5f5fc1b28c93 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -984,6 +984,35 @@ fn addRecursiveBuildDeps(cs: *CompileStep, module: *Module, done: *std.AutoHashM if (done.contains(module)) return; try done.put(module, {}); module.source_file.addStepDependencies(&cs.step); + + for (module.include_dirs.items) |include_dir| { + cs.include_dirs.append(include_dir) catch @panic("OOM"); + } + + for (module.lib_paths.items) |lib_path| { + cs.addLibraryPath(lib_path); + } + + for (module.config_headers.items) |config_header| { + cs.addConfigHeader(config_header); + } + + for (module.libs.items) |lib| { + cs.linkLibrary(lib); + } + + for (module.system_libs.items) |system_lib| { + cs.linkSystemLibrary(system_lib); + } + + if (module.link_libc) { + cs.linkLibC(); + } + + if (module.link_libcpp) { + cs.linkLibCpp(); + } + for (module.dependencies.values()) |dep| { try cs.addRecursiveBuildDeps(dep, done); } From 44ffc09cd1e8cfdc5d124be7406f17f71c7cbe26 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Mon, 27 Feb 2023 00:06:17 +0000 Subject: [PATCH 02/29] Add more CompileStep APIs. These implementations are not yet tested. Newly added functions are: * `linkSystemLibraryNeeded` * `linkSystemLibraryWeak` * `linkSystemLibraryName` * `linkSystemLibraryNeededName` * `linkSystemLibraryWeakName` * `installHeader` * `installConfigHeader` * `installHeadersDirectory` * `installHeadersDirectoryOptions` * `installLibraryHeaders` --- lib/std/Build.zig | 191 +++++++++++++++++++++++++++++----- lib/std/Build/CompileStep.zig | 17 +-- 2 files changed, 173 insertions(+), 35 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index e9e6da72371b..f1f1e917c306 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -579,22 +579,15 @@ pub const CreateModuleOptions = struct { /// packages which depend on this package. pub fn createModule(b: *Build, options: CreateModuleOptions) *Module { const module = b.allocator.create(Module) catch @panic("OOM"); - module.* = .{ - .builder = b, - .source_file = options.source_file, - .dependencies = moduleDependenciesToArrayHashMap(b.allocator, options.dependencies), - }; + module.* = Module.init( + b.allocator, + b, + options.source_file, + options.dependencies, + ); return module; } -fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { - var result = std.StringArrayHashMap(*Module).init(arena); - for (deps) |dep| { - result.put(dep.name, dep.module) catch @panic("OOM"); - } - return result; -} - /// Initializes a RunStep with argv, which must at least have the path to the /// executable. More command line arguments can be added with `addArg`, /// `addArgs`, and `addArtifactArg`. @@ -1598,17 +1591,61 @@ pub const Module = struct { /// file of directory of files which constitute the module. source_file: FileSource, dependencies: std.StringArrayHashMap(*Module), - include_dirs: std.ArrayListUnmanaged(CompileStep.IncludeDir) = .{}, - lib_paths: std.ArrayListUnmanaged([]const u8) = .{}, - system_libs: std.ArrayListUnmanaged([]const u8) = .{}, - libs: std.ArrayListUnmanaged(*Build.CompileStep) = .{}, - config_headers: std.ArrayListUnmanaged(*Build.ConfigHeaderStep) = .{}, - link_libc: bool = false, - link_libcpp: bool = false, + include_dirs: std.ArrayList(CompileStep.IncludeDir), + lib_paths: std.ArrayList([]const u8), + system_libs: std.ArrayList(CompileStep.SystemLib), + libs: std.ArrayList(*CompileStep), + config_headers: std.ArrayList(*ConfigHeaderStep), + installed_headers: std.ArrayList(InstalledHeader), + + const InstalledHeader = union(enum) { + header: struct { + source_path: []const u8, + dest_rel_path: []const u8, + }, + header_dir_step: InstallDirStep.Options, + config_header: struct { + step: *ConfigHeaderStep, + options: CompileStep.InstallConfigHeaderOptions, + }, + lib_step: *CompileStep, + }; + + pub fn init(arena: Allocator, builder: *Builder, source_file: FileSource, dependencies: []const ModuleDependency) Module { + return .{ + .builder = builder, + .source_file = source_file, + .dependencies = moduleDependenciesToArrayHashMap(arena, dependencies), + .include_dirs = std.ArrayList(CompileStep.IncludeDir).init(arena), + .lib_paths = std.ArrayList([]const u8).init(arena), + .system_libs = std.ArrayList(CompileStep.SystemLib).init(arena), + .libs = std.ArrayList(*CompileStep).init(arena), + .config_headers = std.ArrayList(*ConfigHeaderStep).init(arena), + .installed_headers = std.ArrayList(InstalledHeader).init(arena), + }; + } + + fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { + var result = std.StringArrayHashMap(*Module).init(arena); + for (deps) |dep| { + result.put(dep.name, dep.module) catch @panic("OOM"); + } + return result; + } + + pub fn deinit(m: *Module) void { + m.dependencies.deinit(); + m.include_dirs.deinit(); + m.lib_paths.deinit(); + m.system_libs.deinit(); + m.libs.deinit(); + m.config_headers.deinit(); + m.installed_headers.deinit(); + m.* = undefined; + } pub fn addIncludePath(m: *Module, path: []const u8) void { m.include_dirs.append( - m.builder.allocator, CompileStep.IncludeDir{ .raw_path = m.builder.dupe(path) }, ) catch @panic("OOM"); } @@ -1622,20 +1659,118 @@ pub const Module = struct { } pub fn linkLibC(m: *Module) void { - m.link_libc = true; + m.system_libs.append("c") catch @panic("OOM"); } pub fn linkLibCpp(m: *Module) void { - m.link_libcpp = true; + m.system_libs.append("c++") catch @panic("OOM"); + } + + pub fn linkLibrary(m: *Module, lib: *CompileStep) void { + m.libs.append(lib) catch @panic("OOM"); + } + + pub fn linkSystemLibrary(m: *Module, name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(name), + .needed = false, + .weak = false, + .use_pkg_config = .yes, + }) catch @panic("OOM"); + } + + pub fn linkSystemLibraryNeeded(m: *Module, name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(name), + .needed = true, + .weak = false, + .use_pkg_config = .yes, + }) catch @panic("OOM"); + } + + pub fn linkSystemLibraryWeak(m: *Module, name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(name), + .needed = false, + .weak = true, + .use_pkg_config = .yes, + }) catch @panic("OOM"); + } + + pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(name), + .needed = false, + .weak = false, + .use_pkg_config = .no, + }) catch @panic("OOM"); + } + + pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(name), + .needed = true, + .weak = false, + .use_pkg_config = .no, + }) catch @panic("OOM"); + } + + pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(name), + .needed = false, + .weak = true, + .use_pkg_config = .no, + }) catch @panic("OOM"); + } + + pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { + m.installed_headers.append(.{ + .header = .{ + .source_path = src_path, + .dest_rel_path = dest_rel_path, + } + }) catch @panic("OOM"); + } + + pub fn installConfigHeader( + m: *Module, + config_header: *ConfigHeaderStep, + options: CompileStep.InstallConfigHeaderOptions, + ) void { + m.installed_headers.append(.{ + .config_header = .{ + .config_header = config_header, + .options = options, + } + }) catch @panic("OOM"); + } + + pub fn installHeadersDirectory( + m: *Module, + src_dir_path: []const u8, + dest_rel_path: []const u8, + ) void { + return m.installHeadersDirectoryOptions(.{ + .source_dir = src_dir_path, + .install_dir = .header, + .install_subdir = dest_rel_path, + }); } - pub fn linkLibrary(m: *Module, lib: *Build.CompileStep) void { - m.libs.append(m.builder.allocator, lib) catch @panic("OOM"); + pub fn installHeadersDirectoryOptions( + m: *Module, + options: InstallDirStep.Options, + ) void { + m.installed_headers.append(.{ + .header_dir_step = options, + }) catch @panic("OOM"); } - pub fn linkSystemLibrary(m: *Module, lib: []const u8) void { - // TODO: Ignoring `needed` and `weak` variants for now. - m.system_libs.append(m.builder.allocator, m.builder.dupe(lib)) catch @panic("OOM"); + pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { + m.installed_headers.append(.{ + .lib_step = l, + }) catch @panic("OOM"); } }; diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 5f5fc1b28c93..2e8913d5d42e 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -1002,15 +1002,18 @@ fn addRecursiveBuildDeps(cs: *CompileStep, module: *Module, done: *std.AutoHashM } for (module.system_libs.items) |system_lib| { - cs.linkSystemLibrary(system_lib); + cs.link_objects.append(.{ + .system_lib = system_lib, + }) catch @panic("OOM"); } - if (module.link_libc) { - cs.linkLibC(); - } - - if (module.link_libcpp) { - cs.linkLibCpp(); + for (module.installed_headers.items) |installed_header| { + switch (installed_header) { + .header => |header| cs.installHeader(header.source_path, header.dest_rel_path), + .header_dir_step => |header_dir_step| cs.installHeadersDirectoryOptions(header_dir_step), + .config_header => |config_header| cs.installConfigHeader(config_header.step, config_header.options), + .lib_step => |lib_step| cs.installLibraryHeaders(lib_step), + } } for (module.dependencies.values()) |dep| { From 32c5e6c33dbefaf0a690699034e8d8195af9b5ef Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Mon, 27 Feb 2023 01:50:35 +0000 Subject: [PATCH 03/29] Add some tests for module API. Adds tests for: * `addIncludePath` * `linkLibrary` * `linkSystemLibrary` + `addLibraryPath` * `installHeader` --- lib/std/Build.zig | 4 +- test/standalone.zig | 5 +++ test/standalone/emit_asm_and_bin/.gitignore | 2 + test/standalone/issue_12588/.gitignore | 2 + .../module_add_include_path/build.zig | 26 +++++++++++++ .../module_add_include_path/include/header.h | 8 ++++ .../module_add_include_path/test.zig | 6 +++ .../module_add_include_path/test_module.zig | 3 ++ .../module_add_library_path/.gitignore | 1 + .../module_add_library_path/build.zig | 39 +++++++++++++++++++ .../module_add_library_path/lib/lib.c | 5 +++ .../module_add_library_path/lib/lib.h | 4 ++ .../module_add_library_path/test.zig | 6 +++ .../module_add_library_path/test_module.zig | 3 ++ .../module_install_header/build.zig | 24 ++++++++++++ .../module_install_header/include/header.h | 8 ++++ .../standalone/module_install_header/test.zig | 1 + .../module_install_header/test_module.zig | 0 test/standalone/module_link_library/build.zig | 35 +++++++++++++++++ test/standalone/module_link_library/lib/lib.c | 5 +++ test/standalone/module_link_library/lib/lib.h | 4 ++ test/standalone/module_link_library/test.zig | 6 +++ .../module_link_library/test_module.zig | 3 ++ 23 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 test/standalone/emit_asm_and_bin/.gitignore create mode 100644 test/standalone/issue_12588/.gitignore create mode 100644 test/standalone/module_add_include_path/build.zig create mode 100644 test/standalone/module_add_include_path/include/header.h create mode 100644 test/standalone/module_add_include_path/test.zig create mode 100644 test/standalone/module_add_include_path/test_module.zig create mode 100644 test/standalone/module_add_library_path/.gitignore create mode 100644 test/standalone/module_add_library_path/build.zig create mode 100644 test/standalone/module_add_library_path/lib/lib.c create mode 100644 test/standalone/module_add_library_path/lib/lib.h create mode 100644 test/standalone/module_add_library_path/test.zig create mode 100644 test/standalone/module_add_library_path/test_module.zig create mode 100644 test/standalone/module_install_header/build.zig create mode 100644 test/standalone/module_install_header/include/header.h create mode 100644 test/standalone/module_install_header/test.zig create mode 100644 test/standalone/module_install_header/test_module.zig create mode 100644 test/standalone/module_link_library/build.zig create mode 100644 test/standalone/module_link_library/lib/lib.c create mode 100644 test/standalone/module_link_library/lib/lib.h create mode 100644 test/standalone/module_link_library/test.zig create mode 100644 test/standalone/module_link_library/test_module.zig diff --git a/lib/std/Build.zig b/lib/std/Build.zig index f1f1e917c306..07810302a735 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1729,7 +1729,7 @@ pub const Module = struct { .header = .{ .source_path = src_path, .dest_rel_path = dest_rel_path, - } + }, }) catch @panic("OOM"); } @@ -1742,7 +1742,7 @@ pub const Module = struct { .config_header = .{ .config_header = config_header, .options = options, - } + }, }) catch @panic("OOM"); } diff --git a/test/standalone.zig b/test/standalone.zig index 965139235c35..895b47f586a6 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -113,4 +113,9 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.addBuildFile("test/standalone/dep_recursive/build.zig", .{}); cases.addBuildFile("test/standalone/dep_mutually_recursive/build.zig", .{}); cases.addBuildFile("test/standalone/dep_shared_builtin/build.zig", .{}); + + cases.addBuildFile("test/standalone/module_add_include_path/build.zig", .{}); + cases.addBuildFile("test/standalone/module_link_library/build.zig", .{}); + cases.addBuildFile("test/standalone/module_add_library_path/build.zig", .{}); + cases.addBuildFile("test/standalone/module_install_header/build.zig", .{}); } diff --git a/test/standalone/emit_asm_and_bin/.gitignore b/test/standalone/emit_asm_and_bin/.gitignore new file mode 100644 index 000000000000..8b8769c7cf39 --- /dev/null +++ b/test/standalone/emit_asm_and_bin/.gitignore @@ -0,0 +1,2 @@ +main +main.s \ No newline at end of file diff --git a/test/standalone/issue_12588/.gitignore b/test/standalone/issue_12588/.gitignore new file mode 100644 index 000000000000..2c8ac475a3fb --- /dev/null +++ b/test/standalone/issue_12588/.gitignore @@ -0,0 +1,2 @@ +main.bc +main.ll \ No newline at end of file diff --git a/test/standalone/module_add_include_path/build.zig b/test/standalone/module_add_include_path/build.zig new file mode 100644 index 000000000000..8fd4e3df33fa --- /dev/null +++ b/test/standalone/module_add_include_path/build.zig @@ -0,0 +1,26 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "test", + .root_source_file = .{ .path = "test.zig" }, + .optimize = optimize, + }); + var module = b.addModule(.{ + .name = "test_module", + .source_file = .{ + .path = "test_module.zig", + }, + .dependencies = &.{}, + }); + module.addIncludePath("include"); + + exe.addModule("test_module", module); + + const run = exe.run(); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&run.step); +} diff --git a/test/standalone/module_add_include_path/include/header.h b/test/standalone/module_add_include_path/include/header.h new file mode 100644 index 000000000000..48b916111ff6 --- /dev/null +++ b/test/standalone/module_add_include_path/include/header.h @@ -0,0 +1,8 @@ +#ifndef HEADER_H +#define HEADER_H + +int add(int a, int b) { + return a + b; +} + +#endif \ No newline at end of file diff --git a/test/standalone/module_add_include_path/test.zig b/test/standalone/module_add_include_path/test.zig new file mode 100644 index 000000000000..c0ae9efebe72 --- /dev/null +++ b/test/standalone/module_add_include_path/test.zig @@ -0,0 +1,6 @@ +const test_module = @import("test_module"); +const assert = @import("std").debug.assert; + +pub fn main() void { + assert(test_module.header.add(1, 2) == 3); +} diff --git a/test/standalone/module_add_include_path/test_module.zig b/test/standalone/module_add_include_path/test_module.zig new file mode 100644 index 000000000000..2793e36289d7 --- /dev/null +++ b/test/standalone/module_add_include_path/test_module.zig @@ -0,0 +1,3 @@ +pub const header = @cImport({ + @cInclude("header.h"); +}); diff --git a/test/standalone/module_add_library_path/.gitignore b/test/standalone/module_add_library_path/.gitignore new file mode 100644 index 000000000000..dfee02b4c69e --- /dev/null +++ b/test/standalone/module_add_library_path/.gitignore @@ -0,0 +1 @@ +lib_out/ \ No newline at end of file diff --git a/test/standalone/module_add_library_path/build.zig b/test/standalone/module_add_library_path/build.zig new file mode 100644 index 000000000000..785cac31adda --- /dev/null +++ b/test/standalone/module_add_library_path/build.zig @@ -0,0 +1,39 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "test", + .root_source_file = .{ .path = "test.zig" }, + .optimize = optimize, + }); + const lib = b.addStaticLibrary(.{ + .name = "lib", + .target = .{}, + .optimize = optimize, + }); + lib.addIncludePath("lib"); + lib.addCSourceFile("lib/lib.c", &.{""}); + lib.setOutputDir("test/standalone/module_add_library_path/lib_out"); + lib.install(); + + var module = b.addModule(.{ + .name = "test_module", + .source_file = .{ + .path = "test_module.zig", + }, + .dependencies = &.{}, + }); + module.addIncludePath("lib"); + module.addLibraryPath("test/standalone/module_add_library_path/lib_out"); + module.linkSystemLibrary("lib"); + + exe.addModule("test_module", module); + exe.step.dependOn(&lib.step); + + const run = exe.run(); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&run.step); +} diff --git a/test/standalone/module_add_library_path/lib/lib.c b/test/standalone/module_add_library_path/lib/lib.c new file mode 100644 index 000000000000..109cb04f3ff9 --- /dev/null +++ b/test/standalone/module_add_library_path/lib/lib.c @@ -0,0 +1,5 @@ +#include + +int add(int a, int b) { + return a + b; +} \ No newline at end of file diff --git a/test/standalone/module_add_library_path/lib/lib.h b/test/standalone/module_add_library_path/lib/lib.h new file mode 100644 index 000000000000..c3b18495cc6a --- /dev/null +++ b/test/standalone/module_add_library_path/lib/lib.h @@ -0,0 +1,4 @@ +#ifndef LIB_H +#define LIB_H +int add(int a, int b); +#endif \ No newline at end of file diff --git a/test/standalone/module_add_library_path/test.zig b/test/standalone/module_add_library_path/test.zig new file mode 100644 index 000000000000..c0ae9efebe72 --- /dev/null +++ b/test/standalone/module_add_library_path/test.zig @@ -0,0 +1,6 @@ +const test_module = @import("test_module"); +const assert = @import("std").debug.assert; + +pub fn main() void { + assert(test_module.header.add(1, 2) == 3); +} diff --git a/test/standalone/module_add_library_path/test_module.zig b/test/standalone/module_add_library_path/test_module.zig new file mode 100644 index 000000000000..7b0bf6143121 --- /dev/null +++ b/test/standalone/module_add_library_path/test_module.zig @@ -0,0 +1,3 @@ +pub const header = @cImport({ + @cInclude("lib.h"); +}); diff --git a/test/standalone/module_install_header/build.zig b/test/standalone/module_install_header/build.zig new file mode 100644 index 000000000000..714a7726f320 --- /dev/null +++ b/test/standalone/module_install_header/build.zig @@ -0,0 +1,24 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "test", + .root_source_file = .{ .path = "test.zig" }, + .optimize = optimize, + }); + var module = b.addModule(.{ + .name = "test_module", + .source_file = .{ + .path = "test_module.zig", + }, + .dependencies = &.{}, + }); + module.installHeader("include/header.h", "header.h"); + + exe.addModule("test_module", module); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(b.getInstallStep()); +} diff --git a/test/standalone/module_install_header/include/header.h b/test/standalone/module_install_header/include/header.h new file mode 100644 index 000000000000..48b916111ff6 --- /dev/null +++ b/test/standalone/module_install_header/include/header.h @@ -0,0 +1,8 @@ +#ifndef HEADER_H +#define HEADER_H + +int add(int a, int b) { + return a + b; +} + +#endif \ No newline at end of file diff --git a/test/standalone/module_install_header/test.zig b/test/standalone/module_install_header/test.zig new file mode 100644 index 000000000000..902b554db075 --- /dev/null +++ b/test/standalone/module_install_header/test.zig @@ -0,0 +1 @@ +pub fn main() void {} diff --git a/test/standalone/module_install_header/test_module.zig b/test/standalone/module_install_header/test_module.zig new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test/standalone/module_link_library/build.zig b/test/standalone/module_link_library/build.zig new file mode 100644 index 000000000000..2903786a5e31 --- /dev/null +++ b/test/standalone/module_link_library/build.zig @@ -0,0 +1,35 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "test", + .root_source_file = .{ .path = "test.zig" }, + .optimize = optimize, + }); + const lib = b.addStaticLibrary(.{ + .name = "lib", + .target = .{}, + .optimize = optimize, + }); + lib.addIncludePath("lib"); + lib.addCSourceFile("lib/lib.c", &.{""}); + + var module = b.addModule(.{ + .name = "test_module", + .source_file = .{ + .path = "test_module.zig", + }, + .dependencies = &.{}, + }); + module.addIncludePath("lib"); + module.linkLibrary(lib); + + exe.addModule("test_module", module); + + const run = exe.run(); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&run.step); +} diff --git a/test/standalone/module_link_library/lib/lib.c b/test/standalone/module_link_library/lib/lib.c new file mode 100644 index 000000000000..109cb04f3ff9 --- /dev/null +++ b/test/standalone/module_link_library/lib/lib.c @@ -0,0 +1,5 @@ +#include + +int add(int a, int b) { + return a + b; +} \ No newline at end of file diff --git a/test/standalone/module_link_library/lib/lib.h b/test/standalone/module_link_library/lib/lib.h new file mode 100644 index 000000000000..c3b18495cc6a --- /dev/null +++ b/test/standalone/module_link_library/lib/lib.h @@ -0,0 +1,4 @@ +#ifndef LIB_H +#define LIB_H +int add(int a, int b); +#endif \ No newline at end of file diff --git a/test/standalone/module_link_library/test.zig b/test/standalone/module_link_library/test.zig new file mode 100644 index 000000000000..c0ae9efebe72 --- /dev/null +++ b/test/standalone/module_link_library/test.zig @@ -0,0 +1,6 @@ +const test_module = @import("test_module"); +const assert = @import("std").debug.assert; + +pub fn main() void { + assert(test_module.header.add(1, 2) == 3); +} diff --git a/test/standalone/module_link_library/test_module.zig b/test/standalone/module_link_library/test_module.zig new file mode 100644 index 000000000000..7b0bf6143121 --- /dev/null +++ b/test/standalone/module_link_library/test_module.zig @@ -0,0 +1,3 @@ +pub const header = @cImport({ + @cInclude("lib.h"); +}); From 71f91cc1ada9dd3e9492f265d690b3b647f59525 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Mon, 27 Feb 2023 03:38:10 +0000 Subject: [PATCH 04/29] Add linkFramework Adds: * `linkFramework` * `linkFrameworkNeeded` * `linkFrameworkWeak` --- lib/std/Build.zig | 19 +++++++++++++++++++ lib/std/Build/CompileStep.zig | 6 +++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 07810302a735..24a7e1194e51 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1597,6 +1597,7 @@ pub const Module = struct { libs: std.ArrayList(*CompileStep), config_headers: std.ArrayList(*ConfigHeaderStep), installed_headers: std.ArrayList(InstalledHeader), + frameworks: std.StringArrayHashMap(CompileStep.FrameworkLinkInfo), const InstalledHeader = union(enum) { header: struct { @@ -1622,6 +1623,7 @@ pub const Module = struct { .libs = std.ArrayList(*CompileStep).init(arena), .config_headers = std.ArrayList(*ConfigHeaderStep).init(arena), .installed_headers = std.ArrayList(InstalledHeader).init(arena), + .frameworks = std.StringArrayHashMap(CompileStep.FrameworkLinkInfo).init(arena), }; } @@ -1641,6 +1643,7 @@ pub const Module = struct { m.libs.deinit(); m.config_headers.deinit(); m.installed_headers.deinit(); + m.frameworks.deinit(); m.* = undefined; } @@ -1724,6 +1727,22 @@ pub const Module = struct { }) catch @panic("OOM"); } + pub fn linkFramework(m: *Module, framework_name: []const u8) void { + m.frameworks.put(m.builder.dupe(framework_name), .{}) catch @panic("OOM"); + } + + pub fn linkFrameworkNeeded(m: *Module, framework_name: []const u8) void { + m.frameworks.put(m.builder.dupe(framework_name), .{ + .needed = true, + }) catch @panic("OOM"); + } + + pub fn linkFrameworkWeak(m: *Module, framework_name: []const u8) void { + m.frameworks.put(m.builder.dupe(framework_name), .{ + .weak = true, + }) catch @panic("OOM"); + } + pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { m.installed_headers.append(.{ .header = .{ diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 6ffe6f118d1f..83b764ccf500 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -257,7 +257,7 @@ pub const SystemLib = struct { }, }; -const FrameworkLinkInfo = struct { +pub const FrameworkLinkInfo = struct { needed: bool = false, weak: bool = false, }; @@ -1002,6 +1002,10 @@ fn addRecursiveBuildDeps(cs: *CompileStep, module: *Module, done: *std.AutoHashM cs.linkLibrary(lib); } + for (module.frameworks.keys(), module.frameworks.values()) |framework, link_options| { + cs.frameworks.put(cs.builder.dupe(framework), link_options) catch @panic("OOM"); + } + for (module.system_libs.items) |system_lib| { cs.link_objects.append(.{ .system_lib = system_lib, From 5422b2521e9098abd844c8e4c2726c01abe64e59 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Mon, 27 Feb 2023 04:05:14 +0000 Subject: [PATCH 05/29] Add more CompileStep APIs Adds * `addSystemIncludePath` * `addRPath` * `addFrameworkPath` * `linkSystemLibraryPkgConfigOnly` * `linkSystemLibraryNeededPkgConfigOnly` --- lib/std/Build.zig | 42 +++++++++++++++++++++++++++++++++-- lib/std/Build/CompileStep.zig | 8 +++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 24a7e1194e51..950557b7de55 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1593,6 +1593,8 @@ pub const Module = struct { dependencies: std.StringArrayHashMap(*Module), include_dirs: std.ArrayList(CompileStep.IncludeDir), lib_paths: std.ArrayList([]const u8), + rpaths: std.ArrayList([]const u8), + framework_dirs: std.ArrayList([]const u8), system_libs: std.ArrayList(CompileStep.SystemLib), libs: std.ArrayList(*CompileStep), config_headers: std.ArrayList(*ConfigHeaderStep), @@ -1619,6 +1621,8 @@ pub const Module = struct { .dependencies = moduleDependenciesToArrayHashMap(arena, dependencies), .include_dirs = std.ArrayList(CompileStep.IncludeDir).init(arena), .lib_paths = std.ArrayList([]const u8).init(arena), + .rpaths = std.ArrayList([]const u8).init(arena), + .framework_dirs = std.ArrayList([]const u8).init(arena), .system_libs = std.ArrayList(CompileStep.SystemLib).init(arena), .libs = std.ArrayList(*CompileStep).init(arena), .config_headers = std.ArrayList(*ConfigHeaderStep).init(arena), @@ -1639,6 +1643,8 @@ pub const Module = struct { m.dependencies.deinit(); m.include_dirs.deinit(); m.lib_paths.deinit(); + m.rpaths.deinit(); + m.framework_dirs.deinit(); m.system_libs.deinit(); m.libs.deinit(); m.config_headers.deinit(); @@ -1653,14 +1659,28 @@ pub const Module = struct { ) catch @panic("OOM"); } - pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { - m.config_headers.append(config_header) catch @panic("OOM"); + pub fn addSystemIncludePath(m: *Module, path: []const u8) void { + m.include_dirs.append( + CompileStep.IncludeDir{ .raw_path_system = m.builder.dupe(path) }, + ) catch @panic("OOM"); + } + + pub fn addRPath(m: *Module, path: []const u8) void { + m.rpaths.append(m.builder.dupe(path)) catch @panic("OOM"); + } + + pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { + m.framework_dirs.append(m.builder.dupe(dir_path)) catch @panic("OOM"); } pub fn addLibraryPath(m: *Module, library_path: []const u8) void { m.lib_paths.append(m.builder.dupe(library_path)) catch @panic("OOM"); } + pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { + m.config_headers.append(config_header) catch @panic("OOM"); + } + pub fn linkLibC(m: *Module) void { m.system_libs.append("c") catch @panic("OOM"); } @@ -1727,6 +1747,24 @@ pub const Module = struct { }) catch @panic("OOM"); } + pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(lib_name), + .needed = false, + .weak = false, + .use_pkg_config = .force, + }) catch @panic("OOM"); + } + + pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) void { + m.system_libs.append(.{ + .name = m.builder.dupe(lib_name), + .needed = true, + .weak = false, + .use_pkg_config = .force, + }) catch @panic("OOM"); + } + pub fn linkFramework(m: *Module, framework_name: []const u8) void { m.frameworks.put(m.builder.dupe(framework_name), .{}) catch @panic("OOM"); } diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 83b764ccf500..904901328405 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -994,6 +994,14 @@ fn addRecursiveBuildDeps(cs: *CompileStep, module: *Module, done: *std.AutoHashM cs.addLibraryPath(lib_path); } + for (module.framework_dirs.items) |framework_dir| { + cs.addFrameworkPath(framework_dir); + } + + for (module.rpaths.items) |rpath| { + cs.addRPath(rpath); + } + for (module.config_headers.items) |config_header| { cs.addConfigHeader(config_header); } From a668957104a3b282dab0d5c4a9508644293a7487 Mon Sep 17 00:00:00 2001 From: AdamGoertz <36753247+AdamGoertz@users.noreply.github.com> Date: Tue, 14 Mar 2023 22:42:12 -0400 Subject: [PATCH 06/29] Fix linkLibC & linkLibCpp Co-authored-by: Ian Johnson --- lib/std/Build.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index c3c44fdeecc2..c9899ba823b2 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1677,11 +1677,11 @@ pub const Module = struct { } pub fn linkLibC(m: *Module) void { - m.system_libs.append("c") catch @panic("OOM"); + m.linkSystemLibrary("c"); } pub fn linkLibCpp(m: *Module) void { - m.system_libs.append("c++") catch @panic("OOM"); + m.linkSystemLibrary("c++"); } pub fn linkLibrary(m: *Module, lib: *CompileStep) void { From 5cdf5a6793279ef64ee42f11ee7e7440c28458fc Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Wed, 15 Mar 2023 22:34:04 +0000 Subject: [PATCH 07/29] Add test for Module.linkLibC, Module.linkLibCpp --- test/standalone.zig | 1 + .../standalone/module_link_libc_cpp/build.zig | 27 +++++++++++++++++++ test/standalone/module_link_libc_cpp/test.zig | 9 +++++++ .../module_link_libc_cpp/test_module.zig | 3 +++ 4 files changed, 40 insertions(+) create mode 100644 test/standalone/module_link_libc_cpp/build.zig create mode 100644 test/standalone/module_link_libc_cpp/test.zig create mode 100644 test/standalone/module_link_libc_cpp/test_module.zig diff --git a/test/standalone.zig b/test/standalone.zig index 81a04ec7bbb5..2372cd15438a 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -119,4 +119,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.addBuildFile("test/standalone/module_link_library/build.zig", .{}); cases.addBuildFile("test/standalone/module_add_library_path/build.zig", .{}); cases.addBuildFile("test/standalone/module_install_header/build.zig", .{}); + cases.addBuildFile("test/standalone/module_link_libc_cpp/build.zig", .{}); } diff --git a/test/standalone/module_link_libc_cpp/build.zig b/test/standalone/module_link_libc_cpp/build.zig new file mode 100644 index 000000000000..4ea21e1368df --- /dev/null +++ b/test/standalone/module_link_libc_cpp/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "test", + .root_source_file = .{ .path = "test.zig" }, + .optimize = optimize, + }); + + var module = b.addModule("test_module", .{ + .source_file = .{ + .path = "test_module.zig", + }, + .dependencies = &.{}, + }); + module.linkLibC(); + module.linkLibCpp(); + + exe.addModule("test_module", module); + + const run = exe.run(); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&run.step); +} diff --git a/test/standalone/module_link_libc_cpp/test.zig b/test/standalone/module_link_libc_cpp/test.zig new file mode 100644 index 000000000000..404f880bd1f6 --- /dev/null +++ b/test/standalone/module_link_libc_cpp/test.zig @@ -0,0 +1,9 @@ +const test_module = @import("test_module"); +const assert = @import("std").debug.assert; + +extern fn fabs(num: f32) f32; + +pub fn main() void { + assert(test_module.c.abs(-1) == 1); + assert(fabs(fabs(-1.0) - 1.0) < 1e-7); +} diff --git a/test/standalone/module_link_libc_cpp/test_module.zig b/test/standalone/module_link_libc_cpp/test_module.zig new file mode 100644 index 000000000000..bb053b4bdfba --- /dev/null +++ b/test/standalone/module_link_libc_cpp/test_module.zig @@ -0,0 +1,3 @@ +pub const c = @cImport({ + @cInclude("stdlib.h"); +}); From 8b4053d3703eac06d84a221ac53bbe497b590d6f Mon Sep 17 00:00:00 2001 From: AdamGoertz Date: Fri, 24 Mar 2023 18:25:12 -0400 Subject: [PATCH 08/29] Fix formatting --- test/standalone/module_link_libc_cpp/build.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/standalone/module_link_libc_cpp/build.zig b/test/standalone/module_link_libc_cpp/build.zig index 27ce4c1c30c2..7b58c7107c47 100644 --- a/test/standalone/module_link_libc_cpp/build.zig +++ b/test/standalone/module_link_libc_cpp/build.zig @@ -22,5 +22,5 @@ pub fn build(b: *std.Build) void { const run = exe.run(); - b.default_step = &run.step; + b.default_step = &run.step; } From 2f61e412b6253e18686cbc3cd315b8d53e152f1b Mon Sep 17 00:00:00 2001 From: AdamGoertz Date: Fri, 24 Mar 2023 22:38:48 -0400 Subject: [PATCH 09/29] Ensure lib is installed before building exe --- test/standalone/module_add_library_path/build.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/test/standalone/module_add_library_path/build.zig b/test/standalone/module_add_library_path/build.zig index c72e0834dab0..cd904ee6ddea 100644 --- a/test/standalone/module_add_library_path/build.zig +++ b/test/standalone/module_add_library_path/build.zig @@ -30,6 +30,7 @@ pub fn build(b: *std.Build) void { exe.addModule("test_module", module); exe.step.dependOn(&lib.step); + exe.step.dependOn(b.getInstallStep()); const run = exe.run(); b.default_step = &run.step; From 3a34eb624e2f679203179b326281c91fd7f83b9f Mon Sep 17 00:00:00 2001 From: AdamGoertz Date: Sat, 25 Mar 2023 13:16:18 -0400 Subject: [PATCH 10/29] Make relative paths relative to module build root --- lib/std/Build.zig | 14 +++++++------- lib/std/Build/CompileStep.zig | 2 +- test/standalone/module_add_library_path/build.zig | 2 +- test/standalone/module_install_header/build.zig | 1 + 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index dc27c5d02914..7d18d299fc0c 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1569,26 +1569,26 @@ pub const Module = struct { pub fn addIncludePath(m: *Module, path: []const u8) void { m.include_dirs.append( - CompileStep.IncludeDir{ .raw_path = m.builder.dupe(path) }, + CompileStep.IncludeDir{ .raw_path = m.builder.pathFromRoot(path) }, ) catch @panic("OOM"); } pub fn addSystemIncludePath(m: *Module, path: []const u8) void { m.include_dirs.append( - CompileStep.IncludeDir{ .raw_path_system = m.builder.dupe(path) }, + CompileStep.IncludeDir{ .raw_path_system = m.builder.pathFromRoot(path) }, ) catch @panic("OOM"); } pub fn addRPath(m: *Module, path: []const u8) void { - m.rpaths.append(m.builder.dupe(path)) catch @panic("OOM"); + m.rpaths.append(m.builder.pathFromRoot(path)) catch @panic("OOM"); } pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { - m.framework_dirs.append(m.builder.dupe(dir_path)) catch @panic("OOM"); + m.framework_dirs.append(m.builder.pathFromRoot(dir_path)) catch @panic("OOM"); } pub fn addLibraryPath(m: *Module, library_path: []const u8) void { - m.lib_paths.append(m.builder.dupe(library_path)) catch @panic("OOM"); + m.lib_paths.append(m.builder.pathFromRoot(library_path)) catch @panic("OOM"); } pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { @@ -1698,7 +1698,7 @@ pub const Module = struct { pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { m.installed_headers.append(.{ .header = .{ - .source_path = src_path, + .source_path = m.builder.pathFromRoot(src_path), .dest_rel_path = dest_rel_path, }, }) catch @panic("OOM"); @@ -1723,7 +1723,7 @@ pub const Module = struct { dest_rel_path: []const u8, ) void { return m.installHeadersDirectoryOptions(.{ - .source_dir = src_dir_path, + .source_dir = m.builder.pathFromRoot(src_dir_path), .install_dir = .header, .install_subdir = dest_rel_path, }); diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 7777e0ba89ce..f1d4f5185827 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -453,7 +453,7 @@ fn computeOutFileNames(self: *CompileStep) void { pub fn setOutputDir(self: *CompileStep, dir: []const u8) void { const b = self.step.owner; - self.output_dir = b.dupePath(dir); + self.output_dir = b.pathFromRoot(b.dupePath(dir)); } pub fn install(self: *CompileStep) void { diff --git a/test/standalone/module_add_library_path/build.zig b/test/standalone/module_add_library_path/build.zig index cd904ee6ddea..f5a46213bf9d 100644 --- a/test/standalone/module_add_library_path/build.zig +++ b/test/standalone/module_add_library_path/build.zig @@ -26,7 +26,7 @@ pub fn build(b: *std.Build) void { }); module.addIncludePath("lib"); module.addLibraryPath("lib_out"); - module.linkSystemLibrary("lib"); + module.linkSystemLibraryName("lib"); exe.addModule("test_module", module); exe.step.dependOn(&lib.step); diff --git a/test/standalone/module_install_header/build.zig b/test/standalone/module_install_header/build.zig index 97bfa98834a0..994cc707e4db 100644 --- a/test/standalone/module_install_header/build.zig +++ b/test/standalone/module_install_header/build.zig @@ -20,4 +20,5 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test it"); test_step.dependOn(b.getInstallStep()); + b.default_step = test_step; } From 5ffcba220445840e073cc125d8b8a3f3293da5b9 Mon Sep 17 00:00:00 2001 From: AdamGoertz Date: Fri, 31 Mar 2023 22:36:15 -0400 Subject: [PATCH 11/29] #14979 Add addOptions to Module --- lib/std/Build.zig | 4 +++ lib/std/Build/OptionsStep.zig | 7 +++++ test/standalone.zig | 4 +++ test/standalone/module_add_options/build.zig | 30 +++++++++++++++++++ test/standalone/module_add_options/test.zig | 5 ++++ .../module_add_options/test_module.zig | 7 +++++ 6 files changed, 57 insertions(+) create mode 100644 test/standalone/module_add_options/build.zig create mode 100644 test/standalone/module_add_options/test.zig create mode 100644 test/standalone/module_add_options/test_module.zig diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 7d18d299fc0c..465731bb0cc7 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1743,6 +1743,10 @@ pub const Module = struct { .lib_step = l, }) catch @panic("OOM"); } + + pub fn addOptions(m: *Module, name: []const u8, options: *OptionsStep) void { + m.dependencies.put(m.builder.dupe(name), options.addModule(name)) catch @panic("OOM"); + } }; /// A file that is generated by a build step. diff --git a/lib/std/Build/OptionsStep.zig b/lib/std/Build/OptionsStep.zig index a0e72e3695f9..44ea0f8b662f 100644 --- a/lib/std/Build/OptionsStep.zig +++ b/lib/std/Build/OptionsStep.zig @@ -214,6 +214,13 @@ pub fn createModule(self: *OptionsStep) *std.Build.Module { }); } +pub fn addModule(self: *OptionsStep, name: []const u8) *std.Build.Module { + return self.step.owner.addModule(name, .{ + .source_file = self.getSource(), + .dependencies = &.{}, + }); +} + pub fn getSource(self: *OptionsStep) FileSource { return .{ .generated = &self.generated_file }; } diff --git a/test/standalone.zig b/test/standalone.zig index 527149669fa5..4e7a51882f2b 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -238,6 +238,10 @@ pub const build_cases = [_]BuildCase{ .build_root = "test/standalone/module_link_libc_cpp", .import = @import("standalone/module_link_libc_cpp/build.zig"), }, + .{ + .build_root = "test/standalone/module_add_options", + .import = @import("standalone/module_add_options/build.zig"), + }, }; const std = @import("std"); diff --git a/test/standalone/module_add_options/build.zig b/test/standalone/module_add_options/build.zig new file mode 100644 index 000000000000..a9932f6b6eb9 --- /dev/null +++ b/test/standalone/module_add_options/build.zig @@ -0,0 +1,30 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const optimize = b.standardOptimizeOption(.{}); + const test_opt = b.option(bool, "option", "test option") orelse true; + const opts = b.addOptions(); + opts.addOption(bool, "option", test_opt); + + const exe = b.addExecutable(.{ + .name = "test", + .root_source_file = .{ .path = "test.zig" }, + .optimize = optimize, + }); + + var module = b.addModule("test_module", .{ + .source_file = .{ + .path = "test_module.zig", + }, + .dependencies = &.{}, + }); + module.addOptions("test_options", opts); + + exe.addModule("test_module", module); + + const run = exe.run(); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&run.step); + b.default_step = test_step; +} diff --git a/test/standalone/module_add_options/test.zig b/test/standalone/module_add_options/test.zig new file mode 100644 index 000000000000..395d90619db1 --- /dev/null +++ b/test/standalone/module_add_options/test.zig @@ -0,0 +1,5 @@ +const test_module = @import("test_module"); + +pub fn main() void { + test_module.hasOption(); +} diff --git a/test/standalone/module_add_options/test_module.zig b/test/standalone/module_add_options/test_module.zig new file mode 100644 index 000000000000..e08760afd477 --- /dev/null +++ b/test/standalone/module_add_options/test_module.zig @@ -0,0 +1,7 @@ +const options = @import("test_options"); +const std = @import("std"); + +pub fn hasOption() void { + std.debug.print("Option: {}", .{options.option}); + std.debug.assert(options.option); +} From b6e9d8549d1360606a540670e6fa78dd867f245f Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sat, 22 Apr 2023 02:33:48 +0000 Subject: [PATCH 12/29] Fix simple requested changes --- lib/std/Build.zig | 118 +++++++----------- lib/std/Build/CompileStep.zig | 5 - test/standalone.zig | 4 - test/standalone/emit_asm_and_bin/.gitignore | 2 - .../module_add_include_path/build.zig | 2 +- .../module_add_library_path/.gitignore | 1 - .../module_add_library_path/build.zig | 37 ------ .../module_add_library_path/lib/lib.c | 5 - .../module_add_library_path/lib/lib.h | 4 - .../module_add_library_path/test.zig | 6 - .../module_add_library_path/test_module.zig | 3 - test/standalone/module_add_options/build.zig | 2 +- .../module_add_options/test_module.zig | 1 - .../standalone/module_link_libc_cpp/build.zig | 2 +- test/standalone/module_link_library/build.zig | 2 +- 15 files changed, 48 insertions(+), 146 deletions(-) delete mode 100644 test/standalone/emit_asm_and_bin/.gitignore delete mode 100644 test/standalone/module_add_library_path/.gitignore delete mode 100644 test/standalone/module_add_library_path/build.zig delete mode 100644 test/standalone/module_add_library_path/lib/lib.c delete mode 100644 test/standalone/module_add_library_path/lib/lib.h delete mode 100644 test/standalone/module_add_library_path/test.zig delete mode 100644 test/standalone/module_add_library_path/test_module.zig diff --git a/lib/std/Build.zig b/lib/std/Build.zig index ed347aa623f7..09989a8dd6d5 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -641,12 +641,11 @@ pub const CreateModuleOptions = struct { /// `addModule` can be used instead to create a public module. pub fn createModule(b: *Build, options: CreateModuleOptions) *Module { const module = b.allocator.create(Module) catch @panic("OOM"); - module.* = Module.init( - b.allocator, - b, - options.source_file, - options.dependencies, - ); + module.* = .{ + .builder = b, + .source_file = options.source_file, + .dependencies = moduleDependenciesToArrayHashMap(b.allocator, options.dependencies), + }; return module; } @@ -1541,6 +1540,14 @@ pub fn runBuild(b: *Build, build_zig: anytype) anyerror!void { } } +fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { + var result = std.StringArrayHashMap(*Module).init(arena); + for (deps) |dep| { + result.put(dep.name, dep.module) catch @panic("OOM"); + } + return result; +} + pub const Module = struct { builder: *Build, /// This could either be a generated file, in which case the module @@ -1548,15 +1555,15 @@ pub const Module = struct { /// file of directory of files which constitute the module. source_file: FileSource, dependencies: std.StringArrayHashMap(*Module), - include_dirs: std.ArrayList(CompileStep.IncludeDir), - lib_paths: std.ArrayList([]const u8), - rpaths: std.ArrayList([]const u8), - framework_dirs: std.ArrayList([]const u8), - system_libs: std.ArrayList(CompileStep.SystemLib), - libs: std.ArrayList(*CompileStep), - config_headers: std.ArrayList(*ConfigHeaderStep), - installed_headers: std.ArrayList(InstalledHeader), - frameworks: std.StringArrayHashMap(CompileStep.FrameworkLinkInfo), + include_dirs: std.ArrayListUnmanaged(CompileStep.IncludeDir) = .{}, + lib_paths: std.ArrayListUnmanaged([]const u8) = .{}, + rpaths: std.ArrayListUnmanaged([]const u8) = .{}, + framework_dirs: std.ArrayListUnmanaged([]const u8) = .{}, + system_libs: std.ArrayListUnmanaged(CompileStep.SystemLib) = .{}, + libs: std.ArrayListUnmanaged(*CompileStep) = .{}, + config_headers: std.ArrayListUnmanaged(*ConfigHeaderStep) = .{}, + installed_headers: std.ArrayListUnmanaged(InstalledHeader) = .{}, + frameworks: std.StringArrayHashMapUnmanaged(CompileStep.FrameworkLinkInfo) = .{}, const InstalledHeader = union(enum) { header: struct { @@ -1571,71 +1578,34 @@ pub const Module = struct { lib_step: *CompileStep, }; - pub fn init(arena: Allocator, builder: *Builder, source_file: FileSource, dependencies: []const ModuleDependency) Module { - return .{ - .builder = builder, - .source_file = source_file, - .dependencies = moduleDependenciesToArrayHashMap(arena, dependencies), - .include_dirs = std.ArrayList(CompileStep.IncludeDir).init(arena), - .lib_paths = std.ArrayList([]const u8).init(arena), - .rpaths = std.ArrayList([]const u8).init(arena), - .framework_dirs = std.ArrayList([]const u8).init(arena), - .system_libs = std.ArrayList(CompileStep.SystemLib).init(arena), - .libs = std.ArrayList(*CompileStep).init(arena), - .config_headers = std.ArrayList(*ConfigHeaderStep).init(arena), - .installed_headers = std.ArrayList(InstalledHeader).init(arena), - .frameworks = std.StringArrayHashMap(CompileStep.FrameworkLinkInfo).init(arena), - }; - } - - fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { - var result = std.StringArrayHashMap(*Module).init(arena); - for (deps) |dep| { - result.put(dep.name, dep.module) catch @panic("OOM"); - } - return result; - } - - pub fn deinit(m: *Module) void { - m.dependencies.deinit(); - m.include_dirs.deinit(); - m.lib_paths.deinit(); - m.rpaths.deinit(); - m.framework_dirs.deinit(); - m.system_libs.deinit(); - m.libs.deinit(); - m.config_headers.deinit(); - m.installed_headers.deinit(); - m.frameworks.deinit(); - m.* = undefined; - } - pub fn addIncludePath(m: *Module, path: []const u8) void { m.include_dirs.append( + m.builder.allocator, CompileStep.IncludeDir{ .raw_path = m.builder.pathFromRoot(path) }, ) catch @panic("OOM"); } pub fn addSystemIncludePath(m: *Module, path: []const u8) void { m.include_dirs.append( + m.builder.allocator, CompileStep.IncludeDir{ .raw_path_system = m.builder.pathFromRoot(path) }, ) catch @panic("OOM"); } pub fn addRPath(m: *Module, path: []const u8) void { - m.rpaths.append(m.builder.pathFromRoot(path)) catch @panic("OOM"); + m.rpaths.append(m.builder.allocator, m.builder.pathFromRoot(path)) catch @panic("OOM"); } pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { - m.framework_dirs.append(m.builder.pathFromRoot(dir_path)) catch @panic("OOM"); + m.framework_dirs.append(m.builder.allocator, m.builder.pathFromRoot(dir_path)) catch @panic("OOM"); } pub fn addLibraryPath(m: *Module, library_path: []const u8) void { - m.lib_paths.append(m.builder.pathFromRoot(library_path)) catch @panic("OOM"); + m.lib_paths.append(m.builder.allocator, m.builder.pathFromRoot(library_path)) catch @panic("OOM"); } pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { - m.config_headers.append(config_header) catch @panic("OOM"); + m.config_headers.append(m.builder.allocator, config_header) catch @panic("OOM"); } pub fn linkLibC(m: *Module) void { @@ -1647,11 +1617,11 @@ pub const Module = struct { } pub fn linkLibrary(m: *Module, lib: *CompileStep) void { - m.libs.append(lib) catch @panic("OOM"); + m.libs.append(m.builder.allocator, lib) catch @panic("OOM"); } pub fn linkSystemLibrary(m: *Module, name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(name), .needed = false, .weak = false, @@ -1660,7 +1630,7 @@ pub const Module = struct { } pub fn linkSystemLibraryNeeded(m: *Module, name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(name), .needed = true, .weak = false, @@ -1669,7 +1639,7 @@ pub const Module = struct { } pub fn linkSystemLibraryWeak(m: *Module, name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(name), .needed = false, .weak = true, @@ -1678,7 +1648,7 @@ pub const Module = struct { } pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(name), .needed = false, .weak = false, @@ -1687,7 +1657,7 @@ pub const Module = struct { } pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(name), .needed = true, .weak = false, @@ -1696,7 +1666,7 @@ pub const Module = struct { } pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(name), .needed = false, .weak = true, @@ -1705,7 +1675,7 @@ pub const Module = struct { } pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(lib_name), .needed = false, .weak = false, @@ -1714,7 +1684,7 @@ pub const Module = struct { } pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) void { - m.system_libs.append(.{ + m.system_libs.append(m.builder.allocator, .{ .name = m.builder.dupe(lib_name), .needed = true, .weak = false, @@ -1723,23 +1693,23 @@ pub const Module = struct { } pub fn linkFramework(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.dupe(framework_name), .{}) catch @panic("OOM"); + m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{}) catch @panic("OOM"); } pub fn linkFrameworkNeeded(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.dupe(framework_name), .{ + m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ .needed = true, }) catch @panic("OOM"); } pub fn linkFrameworkWeak(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.dupe(framework_name), .{ + m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ .weak = true, }) catch @panic("OOM"); } pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { - m.installed_headers.append(.{ + m.installed_headers.append(m.builder.allocator, .{ .header = .{ .source_path = m.builder.pathFromRoot(src_path), .dest_rel_path = dest_rel_path, @@ -1752,7 +1722,7 @@ pub const Module = struct { config_header: *ConfigHeaderStep, options: CompileStep.InstallConfigHeaderOptions, ) void { - m.installed_headers.append(.{ + m.installed_headers.append(m.builder.allocator, .{ .config_header = .{ .config_header = config_header, .options = options, @@ -1776,13 +1746,13 @@ pub const Module = struct { m: *Module, options: InstallDirStep.Options, ) void { - m.installed_headers.append(.{ + m.installed_headers.append(m.builder.allocator, .{ .header_dir_step = options, }) catch @panic("OOM"); } pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { - m.installed_headers.append(.{ + m.installed_headers.append(m.builder.allocator, .{ .lib_step = l, }) catch @panic("OOM"); } diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 1f6c2edbea8f..e6a29c904d6a 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -459,11 +459,6 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { return self; } -pub fn setOutputDir(self: *CompileStep, dir: []const u8) void { - const b = self.step.owner; - self.output_dir = b.pathFromRoot(b.dupePath(dir)); -} - pub fn installHeader(cs: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void { const b = cs.step.owner; const install_file = b.addInstallHeaderFile(src_path, dest_rel_path); diff --git a/test/standalone.zig b/test/standalone.zig index c484e84c57ff..d203e095d030 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -226,10 +226,6 @@ pub const build_cases = [_]BuildCase{ .build_root = "test/standalone/module_link_library", .import = @import("standalone/module_link_library/build.zig"), }, - .{ - .build_root = "test/standalone/module_add_library_path", - .import = @import("standalone/module_add_library_path/build.zig"), - }, .{ .build_root = "test/standalone/module_install_header", .import = @import("standalone/module_install_header/build.zig"), diff --git a/test/standalone/emit_asm_and_bin/.gitignore b/test/standalone/emit_asm_and_bin/.gitignore deleted file mode 100644 index 8b8769c7cf39..000000000000 --- a/test/standalone/emit_asm_and_bin/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -main -main.s \ No newline at end of file diff --git a/test/standalone/module_add_include_path/build.zig b/test/standalone/module_add_include_path/build.zig index 85f40151a99b..b3244abe16c0 100644 --- a/test/standalone/module_add_include_path/build.zig +++ b/test/standalone/module_add_include_path/build.zig @@ -18,6 +18,6 @@ pub fn build(b: *std.Build) void { exe.addModule("test_module", module); - const run = exe.run(); + const run = b.addRunArtifact(exe); b.default_step = &run.step; } diff --git a/test/standalone/module_add_library_path/.gitignore b/test/standalone/module_add_library_path/.gitignore deleted file mode 100644 index dfee02b4c69e..000000000000 --- a/test/standalone/module_add_library_path/.gitignore +++ /dev/null @@ -1 +0,0 @@ -lib_out/ \ No newline at end of file diff --git a/test/standalone/module_add_library_path/build.zig b/test/standalone/module_add_library_path/build.zig deleted file mode 100644 index f5a46213bf9d..000000000000 --- a/test/standalone/module_add_library_path/build.zig +++ /dev/null @@ -1,37 +0,0 @@ -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const optimize = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ - .name = "test", - .root_source_file = .{ .path = "test.zig" }, - .optimize = optimize, - }); - const lib = b.addStaticLibrary(.{ - .name = "lib", - .target = .{}, - .optimize = optimize, - }); - lib.addIncludePath("lib"); - lib.addCSourceFile("lib/lib.c", &.{""}); - lib.setOutputDir("lib_out"); - lib.install(); - - var module = b.addModule("test_module", .{ - .source_file = .{ - .path = "test_module.zig", - }, - .dependencies = &.{}, - }); - module.addIncludePath("lib"); - module.addLibraryPath("lib_out"); - module.linkSystemLibraryName("lib"); - - exe.addModule("test_module", module); - exe.step.dependOn(&lib.step); - exe.step.dependOn(b.getInstallStep()); - - const run = exe.run(); - b.default_step = &run.step; -} diff --git a/test/standalone/module_add_library_path/lib/lib.c b/test/standalone/module_add_library_path/lib/lib.c deleted file mode 100644 index 109cb04f3ff9..000000000000 --- a/test/standalone/module_add_library_path/lib/lib.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int add(int a, int b) { - return a + b; -} \ No newline at end of file diff --git a/test/standalone/module_add_library_path/lib/lib.h b/test/standalone/module_add_library_path/lib/lib.h deleted file mode 100644 index c3b18495cc6a..000000000000 --- a/test/standalone/module_add_library_path/lib/lib.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef LIB_H -#define LIB_H -int add(int a, int b); -#endif \ No newline at end of file diff --git a/test/standalone/module_add_library_path/test.zig b/test/standalone/module_add_library_path/test.zig deleted file mode 100644 index c0ae9efebe72..000000000000 --- a/test/standalone/module_add_library_path/test.zig +++ /dev/null @@ -1,6 +0,0 @@ -const test_module = @import("test_module"); -const assert = @import("std").debug.assert; - -pub fn main() void { - assert(test_module.header.add(1, 2) == 3); -} diff --git a/test/standalone/module_add_library_path/test_module.zig b/test/standalone/module_add_library_path/test_module.zig deleted file mode 100644 index 7b0bf6143121..000000000000 --- a/test/standalone/module_add_library_path/test_module.zig +++ /dev/null @@ -1,3 +0,0 @@ -pub const header = @cImport({ - @cInclude("lib.h"); -}); diff --git a/test/standalone/module_add_options/build.zig b/test/standalone/module_add_options/build.zig index a9932f6b6eb9..f8d09865dc2a 100644 --- a/test/standalone/module_add_options/build.zig +++ b/test/standalone/module_add_options/build.zig @@ -22,7 +22,7 @@ pub fn build(b: *std.Build) void { exe.addModule("test_module", module); - const run = exe.run(); + const run = b.addRunArtifact(exe); const test_step = b.step("test", "Test it"); test_step.dependOn(&run.step); diff --git a/test/standalone/module_add_options/test_module.zig b/test/standalone/module_add_options/test_module.zig index e08760afd477..6f15f16d30cd 100644 --- a/test/standalone/module_add_options/test_module.zig +++ b/test/standalone/module_add_options/test_module.zig @@ -2,6 +2,5 @@ const options = @import("test_options"); const std = @import("std"); pub fn hasOption() void { - std.debug.print("Option: {}", .{options.option}); std.debug.assert(options.option); } diff --git a/test/standalone/module_link_libc_cpp/build.zig b/test/standalone/module_link_libc_cpp/build.zig index 7b58c7107c47..6f1da04e54e0 100644 --- a/test/standalone/module_link_libc_cpp/build.zig +++ b/test/standalone/module_link_libc_cpp/build.zig @@ -20,7 +20,7 @@ pub fn build(b: *std.Build) void { exe.addModule("test_module", module); - const run = exe.run(); + const run = b.addRunArtifact(exe); b.default_step = &run.step; } diff --git a/test/standalone/module_link_library/build.zig b/test/standalone/module_link_library/build.zig index 0f37436910ca..1e2cae8b67a2 100644 --- a/test/standalone/module_link_library/build.zig +++ b/test/standalone/module_link_library/build.zig @@ -27,7 +27,7 @@ pub fn build(b: *std.Build) void { exe.addModule("test_module", module); - const run = exe.run(); + const run = b.addRunArtifact(exe); const test_step = b.step("test", "Test it"); test_step.dependOn(&run.step); From e0f80a9d9baffe0b2cc055e93458f8cb70aeeb01 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sat, 22 Apr 2023 02:56:28 +0000 Subject: [PATCH 13/29] Remove unnecessary usages of pathFromRoot --- lib/std/Build.zig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 09989a8dd6d5..dee511c896c2 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1581,27 +1581,27 @@ pub const Module = struct { pub fn addIncludePath(m: *Module, path: []const u8) void { m.include_dirs.append( m.builder.allocator, - CompileStep.IncludeDir{ .raw_path = m.builder.pathFromRoot(path) }, + CompileStep.IncludeDir{ .raw_path = m.builder.dupe(path) }, ) catch @panic("OOM"); } pub fn addSystemIncludePath(m: *Module, path: []const u8) void { m.include_dirs.append( m.builder.allocator, - CompileStep.IncludeDir{ .raw_path_system = m.builder.pathFromRoot(path) }, + CompileStep.IncludeDir{ .raw_path_system = m.builder.dupe(path) }, ) catch @panic("OOM"); } pub fn addRPath(m: *Module, path: []const u8) void { - m.rpaths.append(m.builder.allocator, m.builder.pathFromRoot(path)) catch @panic("OOM"); + m.rpaths.append(m.builder.allocator, m.builder.dupe(path)) catch @panic("OOM"); } pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { - m.framework_dirs.append(m.builder.allocator, m.builder.pathFromRoot(dir_path)) catch @panic("OOM"); + m.framework_dirs.append(m.builder.allocator, m.builder.dupe(dir_path)) catch @panic("OOM"); } pub fn addLibraryPath(m: *Module, library_path: []const u8) void { - m.lib_paths.append(m.builder.allocator, m.builder.pathFromRoot(library_path)) catch @panic("OOM"); + m.lib_paths.append(m.builder.allocator, m.builder.dupe(library_path)) catch @panic("OOM"); } pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { @@ -1711,7 +1711,7 @@ pub const Module = struct { pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { m.installed_headers.append(m.builder.allocator, .{ .header = .{ - .source_path = m.builder.pathFromRoot(src_path), + .source_path = m.builder.dupe(src_path), .dest_rel_path = dest_rel_path, }, }) catch @panic("OOM"); @@ -1736,7 +1736,7 @@ pub const Module = struct { dest_rel_path: []const u8, ) void { return m.installHeadersDirectoryOptions(.{ - .source_dir = m.builder.pathFromRoot(src_dir_path), + .source_dir = m.builder.dupe(src_dir_path), .install_dir = .header, .install_subdir = dest_rel_path, }); From 3383c6f5984e7a4791790f31a8016d79296063c8 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sat, 22 Apr 2023 17:07:56 +0000 Subject: [PATCH 14/29] Move Module to Build/Module.zig --- lib/std/Build.zig | 215 +------------------------------------ lib/std/Build/Module.zig | 221 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+), 214 deletions(-) create mode 100644 lib/std/Build/Module.zig diff --git a/lib/std/Build.zig b/lib/std/Build.zig index dee511c896c2..d643ff845c41 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -20,6 +20,7 @@ const Sha256 = std.crypto.hash.sha2.Sha256; const Build = @This(); pub const Cache = @import("Build/Cache.zig"); +pub const Module = @import("Build/Module.zig"); /// deprecated: use `CompileStep`. pub const LibExeObjStep = CompileStep; @@ -1548,220 +1549,6 @@ fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDepend return result; } -pub const Module = struct { - builder: *Build, - /// This could either be a generated file, in which case the module - /// contains exactly one file, or it could be a path to the root source - /// file of directory of files which constitute the module. - source_file: FileSource, - dependencies: std.StringArrayHashMap(*Module), - include_dirs: std.ArrayListUnmanaged(CompileStep.IncludeDir) = .{}, - lib_paths: std.ArrayListUnmanaged([]const u8) = .{}, - rpaths: std.ArrayListUnmanaged([]const u8) = .{}, - framework_dirs: std.ArrayListUnmanaged([]const u8) = .{}, - system_libs: std.ArrayListUnmanaged(CompileStep.SystemLib) = .{}, - libs: std.ArrayListUnmanaged(*CompileStep) = .{}, - config_headers: std.ArrayListUnmanaged(*ConfigHeaderStep) = .{}, - installed_headers: std.ArrayListUnmanaged(InstalledHeader) = .{}, - frameworks: std.StringArrayHashMapUnmanaged(CompileStep.FrameworkLinkInfo) = .{}, - - const InstalledHeader = union(enum) { - header: struct { - source_path: []const u8, - dest_rel_path: []const u8, - }, - header_dir_step: InstallDirStep.Options, - config_header: struct { - step: *ConfigHeaderStep, - options: CompileStep.InstallConfigHeaderOptions, - }, - lib_step: *CompileStep, - }; - - pub fn addIncludePath(m: *Module, path: []const u8) void { - m.include_dirs.append( - m.builder.allocator, - CompileStep.IncludeDir{ .raw_path = m.builder.dupe(path) }, - ) catch @panic("OOM"); - } - - pub fn addSystemIncludePath(m: *Module, path: []const u8) void { - m.include_dirs.append( - m.builder.allocator, - CompileStep.IncludeDir{ .raw_path_system = m.builder.dupe(path) }, - ) catch @panic("OOM"); - } - - pub fn addRPath(m: *Module, path: []const u8) void { - m.rpaths.append(m.builder.allocator, m.builder.dupe(path)) catch @panic("OOM"); - } - - pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { - m.framework_dirs.append(m.builder.allocator, m.builder.dupe(dir_path)) catch @panic("OOM"); - } - - pub fn addLibraryPath(m: *Module, library_path: []const u8) void { - m.lib_paths.append(m.builder.allocator, m.builder.dupe(library_path)) catch @panic("OOM"); - } - - pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { - m.config_headers.append(m.builder.allocator, config_header) catch @panic("OOM"); - } - - pub fn linkLibC(m: *Module) void { - m.linkSystemLibrary("c"); - } - - pub fn linkLibCpp(m: *Module) void { - m.linkSystemLibrary("c++"); - } - - pub fn linkLibrary(m: *Module, lib: *CompileStep) void { - m.libs.append(m.builder.allocator, lib) catch @panic("OOM"); - } - - pub fn linkSystemLibrary(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = false, - .use_pkg_config = .yes, - }) catch @panic("OOM"); - } - - pub fn linkSystemLibraryNeeded(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = true, - .weak = false, - .use_pkg_config = .yes, - }) catch @panic("OOM"); - } - - pub fn linkSystemLibraryWeak(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = true, - .use_pkg_config = .yes, - }) catch @panic("OOM"); - } - - pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = false, - .use_pkg_config = .no, - }) catch @panic("OOM"); - } - - pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = true, - .weak = false, - .use_pkg_config = .no, - }) catch @panic("OOM"); - } - - pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = true, - .use_pkg_config = .no, - }) catch @panic("OOM"); - } - - pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(lib_name), - .needed = false, - .weak = false, - .use_pkg_config = .force, - }) catch @panic("OOM"); - } - - pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(lib_name), - .needed = true, - .weak = false, - .use_pkg_config = .force, - }) catch @panic("OOM"); - } - - pub fn linkFramework(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{}) catch @panic("OOM"); - } - - pub fn linkFrameworkNeeded(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ - .needed = true, - }) catch @panic("OOM"); - } - - pub fn linkFrameworkWeak(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ - .weak = true, - }) catch @panic("OOM"); - } - - pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { - m.installed_headers.append(m.builder.allocator, .{ - .header = .{ - .source_path = m.builder.dupe(src_path), - .dest_rel_path = dest_rel_path, - }, - }) catch @panic("OOM"); - } - - pub fn installConfigHeader( - m: *Module, - config_header: *ConfigHeaderStep, - options: CompileStep.InstallConfigHeaderOptions, - ) void { - m.installed_headers.append(m.builder.allocator, .{ - .config_header = .{ - .config_header = config_header, - .options = options, - }, - }) catch @panic("OOM"); - } - - pub fn installHeadersDirectory( - m: *Module, - src_dir_path: []const u8, - dest_rel_path: []const u8, - ) void { - return m.installHeadersDirectoryOptions(.{ - .source_dir = m.builder.dupe(src_dir_path), - .install_dir = .header, - .install_subdir = dest_rel_path, - }); - } - - pub fn installHeadersDirectoryOptions( - m: *Module, - options: InstallDirStep.Options, - ) void { - m.installed_headers.append(m.builder.allocator, .{ - .header_dir_step = options, - }) catch @panic("OOM"); - } - - pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { - m.installed_headers.append(m.builder.allocator, .{ - .lib_step = l, - }) catch @panic("OOM"); - } - - pub fn addOptions(m: *Module, name: []const u8, options: *OptionsStep) void { - m.dependencies.put(m.builder.dupe(name), options.addModule(name)) catch @panic("OOM"); - } -}; - /// A file that is generated by a build step. /// This struct is an interface that is meant to be used with `@fieldParentPtr` to implement the actual path logic. pub const GeneratedFile = struct { diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig new file mode 100644 index 000000000000..ca4f98ddd8ef --- /dev/null +++ b/lib/std/Build/Module.zig @@ -0,0 +1,221 @@ +const Module = @This(); + +builder: *Build, +/// This could either be a generated file, in which case the module +/// contains exactly one file, or it could be a path to the root source +/// file of directory of files which constitute the module. +source_file: FileSource, +dependencies: std.StringArrayHashMap(*Module), +include_dirs: std.ArrayListUnmanaged(CompileStep.IncludeDir) = .{}, +lib_paths: std.ArrayListUnmanaged([]const u8) = .{}, +rpaths: std.ArrayListUnmanaged([]const u8) = .{}, +framework_dirs: std.ArrayListUnmanaged([]const u8) = .{}, +system_libs: std.ArrayListUnmanaged(CompileStep.SystemLib) = .{}, +libs: std.ArrayListUnmanaged(*CompileStep) = .{}, +config_headers: std.ArrayListUnmanaged(*ConfigHeaderStep) = .{}, +installed_headers: std.ArrayListUnmanaged(InstalledHeader) = .{}, +frameworks: std.StringArrayHashMapUnmanaged(CompileStep.FrameworkLinkInfo) = .{}, + +const InstalledHeader = union(enum) { + header: struct { + source_path: []const u8, + dest_rel_path: []const u8, + }, + header_dir_step: InstallDirStep.Options, + config_header: struct { + step: *ConfigHeaderStep, + options: CompileStep.InstallConfigHeaderOptions, + }, + lib_step: *CompileStep, +}; + +pub fn addIncludePath(m: *Module, path: []const u8) void { + m.include_dirs.append( + m.builder.allocator, + CompileStep.IncludeDir{ .raw_path = m.builder.dupe(path) }, + ) catch @panic("OOM"); +} + +pub fn addSystemIncludePath(m: *Module, path: []const u8) void { + m.include_dirs.append( + m.builder.allocator, + CompileStep.IncludeDir{ .raw_path_system = m.builder.dupe(path) }, + ) catch @panic("OOM"); +} + +pub fn addRPath(m: *Module, path: []const u8) void { + m.rpaths.append(m.builder.allocator, m.builder.dupe(path)) catch @panic("OOM"); +} + +pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { + m.framework_dirs.append(m.builder.allocator, m.builder.dupe(dir_path)) catch @panic("OOM"); +} + +pub fn addLibraryPath(m: *Module, library_path: []const u8) void { + m.lib_paths.append(m.builder.allocator, m.builder.dupe(library_path)) catch @panic("OOM"); +} + +pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { + m.config_headers.append(m.builder.allocator, config_header) catch @panic("OOM"); +} + +pub fn linkLibC(m: *Module) void { + m.linkSystemLibrary("c"); +} + +pub fn linkLibCpp(m: *Module) void { + m.linkSystemLibrary("c++"); +} + +pub fn linkLibrary(m: *Module, lib: *CompileStep) void { + m.libs.append(m.builder.allocator, lib) catch @panic("OOM"); +} + +pub fn linkSystemLibrary(m: *Module, name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(name), + .needed = false, + .weak = false, + .use_pkg_config = .yes, + }) catch @panic("OOM"); +} + +pub fn linkSystemLibraryNeeded(m: *Module, name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(name), + .needed = true, + .weak = false, + .use_pkg_config = .yes, + }) catch @panic("OOM"); +} + +pub fn linkSystemLibraryWeak(m: *Module, name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(name), + .needed = false, + .weak = true, + .use_pkg_config = .yes, + }) catch @panic("OOM"); +} + +pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(name), + .needed = false, + .weak = false, + .use_pkg_config = .no, + }) catch @panic("OOM"); +} + +pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(name), + .needed = true, + .weak = false, + .use_pkg_config = .no, + }) catch @panic("OOM"); +} + +pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(name), + .needed = false, + .weak = true, + .use_pkg_config = .no, + }) catch @panic("OOM"); +} + +pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(lib_name), + .needed = false, + .weak = false, + .use_pkg_config = .force, + }) catch @panic("OOM"); +} + +pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) void { + m.system_libs.append(m.builder.allocator, .{ + .name = m.builder.dupe(lib_name), + .needed = true, + .weak = false, + .use_pkg_config = .force, + }) catch @panic("OOM"); +} + +pub fn linkFramework(m: *Module, framework_name: []const u8) void { + m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{}) catch @panic("OOM"); +} + +pub fn linkFrameworkNeeded(m: *Module, framework_name: []const u8) void { + m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ + .needed = true, + }) catch @panic("OOM"); +} + +pub fn linkFrameworkWeak(m: *Module, framework_name: []const u8) void { + m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ + .weak = true, + }) catch @panic("OOM"); +} + +pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { + m.installed_headers.append(m.builder.allocator, .{ + .header = .{ + .source_path = m.builder.dupe(src_path), + .dest_rel_path = dest_rel_path, + }, + }) catch @panic("OOM"); +} + +pub fn installConfigHeader( + m: *Module, + config_header: *ConfigHeaderStep, + options: CompileStep.InstallConfigHeaderOptions, +) void { + m.installed_headers.append(m.builder.allocator, .{ + .config_header = .{ + .config_header = config_header, + .options = options, + }, + }) catch @panic("OOM"); +} + +pub fn installHeadersDirectory( + m: *Module, + src_dir_path: []const u8, + dest_rel_path: []const u8, +) void { + return m.installHeadersDirectoryOptions(.{ + .source_dir = m.builder.dupe(src_dir_path), + .install_dir = .header, + .install_subdir = dest_rel_path, + }); +} + +pub fn installHeadersDirectoryOptions( + m: *Module, + options: InstallDirStep.Options, +) void { + m.installed_headers.append(m.builder.allocator, .{ + .header_dir_step = options, + }) catch @panic("OOM"); +} + +pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { + m.installed_headers.append(m.builder.allocator, .{ + .lib_step = l, + }) catch @panic("OOM"); +} + +pub fn addOptions(m: *Module, name: []const u8, options: *OptionsStep) void { + m.dependencies.put(m.builder.dupe(name), options.addModule(name)) catch @panic("OOM"); +} + +const std = @import("std"); +const Build = std.Build; +const FileSource = Build.FileSource; +const CompileStep = Build.CompileStep; +const ConfigHeaderStep = Build.ConfigHeaderStep; +const OptionsStep = Build.OptionsStep; +const InstallDirStep = Build.InstallDirStep; From d6e471aa494fa1756e5ef6dcc069642b93180652 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sat, 22 Apr 2023 21:01:57 +0000 Subject: [PATCH 15/29] WIP: Begin moving CompileStep APIs to Module --- lib/std/Build.zig | 36 +- lib/std/Build/CompileStep.zig | 608 ++-------------------------------- lib/std/Build/Module.zig | 577 ++++++++++++++++++++++++-------- 3 files changed, 491 insertions(+), 730 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index d643ff845c41..9e43fddf3db3 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -627,13 +627,31 @@ pub fn addModule(b: *Build, name: []const u8, options: CreateModuleOptions) *Mod return module; } +pub const CSourceFiles = struct { + files: []const []const u8, + flags: []const []const u8, +}; + +pub const CSourceFile = struct { + source: FileSource, + args: []const []const u8, + + pub fn dupe(self: CSourceFile, b: *std.Build) CSourceFile { + return .{ + .source = self.source.dupe(b), + .args = b.dupeStrings(self.args), + }; + } +}; + pub const ModuleDependency = struct { name: []const u8, module: *Module, }; pub const CreateModuleOptions = struct { - source_file: FileSource, + source_file: ?FileSource = null, + c_source_files: ?CSourceFiles = null, dependencies: []const ModuleDependency = &.{}, }; @@ -641,13 +659,7 @@ pub const CreateModuleOptions = struct { /// but not exposed to other packages depending on this one. /// `addModule` can be used instead to create a public module. pub fn createModule(b: *Build, options: CreateModuleOptions) *Module { - const module = b.allocator.create(Module) catch @panic("OOM"); - module.* = .{ - .builder = b, - .source_file = options.source_file, - .dependencies = moduleDependenciesToArrayHashMap(b.allocator, options.dependencies), - }; - return module; + return Module.create(b, options); } /// Initializes a RunStep with argv, which must at least have the path to the @@ -1541,14 +1553,6 @@ pub fn runBuild(b: *Build, build_zig: anytype) anyerror!void { } } -fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { - var result = std.StringArrayHashMap(*Module).init(arena); - for (deps) |dep| { - result.put(dep.name, dep.module) catch @panic("OOM"); - } - return result; -} - /// A file that is generated by a build step. /// This struct is an interface that is meant to be used with `@fieldParentPtr` to implement the actual path logic. pub const GeneratedFile = struct { diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index e6a29c904d6a..b72df3eb223c 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -25,6 +25,8 @@ const CheckObjectStep = std.Build.CheckObjectStep; const RunStep = std.Build.RunStep; const OptionsStep = std.Build.OptionsStep; const ConfigHeaderStep = std.Build.ConfigHeaderStep; +const LinkObject = Module.LinkObject; +const FrameworkLinkInfo = Module.FrameworkLinkInfo; const CompileStep = @This(); pub const base_id: Step.Id = .compile; @@ -34,7 +36,6 @@ name: []const u8, target: CrossTarget, target_info: NativeTargetInfo, optimize: std.builtin.Mode, -linker_script: ?FileSource = null, version_script: ?[]const u8 = null, out_filename: []const u8, linkage: ?Linkage = null, @@ -46,10 +47,6 @@ strip: ?bool, unwind_tables: ?bool, // keep in sync with src/link.zig:CompressDebugSections compress_debug_sections: enum { none, zlib } = .none, -lib_paths: ArrayList(FileSource), -rpaths: ArrayList(FileSource), -framework_dirs: ArrayList(FileSource), -frameworks: StringHashMap(FrameworkLinkInfo), verbose_link: bool, verbose_cc: bool, emit_analysis: EmitOption = .default, @@ -80,7 +77,6 @@ initial_memory: ?u64 = null, max_memory: ?u64 = null, shared_memory: bool = false, global_base: ?u64 = null, -c_std: std.Build.CStd, zig_lib_dir: ?[]const u8, main_pkg_path: ?[]const u8, exec_cmd_args: ?[]const ?[]const u8, @@ -96,16 +92,11 @@ root_src: ?FileSource, out_h_filename: []const u8, out_lib_filename: []const u8, out_pdb_filename: []const u8, -modules: std.StringArrayHashMap(*Module), - -link_objects: ArrayList(LinkObject), -include_dirs: ArrayList(IncludeDir), -c_macros: ArrayList([]const u8), -installed_headers: ArrayList(*Step), -is_linking_libc: bool, -is_linking_libcpp: bool, + vcpkg_bin_path: ?[]const u8 = null, +main_module: *Module, + /// This may be set in order to override the default install directory override_dest_dir: ?InstallDir, installed_path: ?[]const u8, @@ -199,11 +190,6 @@ subsystem: ?std.Target.SubSystem = null, entry_symbol_name: ?[]const u8 = null, -/// List of symbols forced as undefined in the symbol table -/// thus forcing their resolution by the linker. -/// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. -force_undefined_symbols: std.StringHashMap(void), - /// Overrides the default stack size stack_size: ?u64 = null, @@ -223,63 +209,9 @@ output_h_path_source: GeneratedFile, output_pdb_path_source: GeneratedFile, output_dirname_source: GeneratedFile, -pub const CSourceFiles = struct { - files: []const []const u8, - flags: []const []const u8, -}; - -pub const CSourceFile = struct { - source: FileSource, - args: []const []const u8, - - pub fn dupe(self: CSourceFile, b: *std.Build) CSourceFile { - return .{ - .source = self.source.dupe(b), - .args = b.dupeStrings(self.args), - }; - } -}; - -pub const LinkObject = union(enum) { - static_path: FileSource, - other_step: *CompileStep, - system_lib: SystemLib, - assembly_file: FileSource, - c_source_file: *CSourceFile, - c_source_files: *CSourceFiles, -}; - -pub const SystemLib = struct { - name: []const u8, - needed: bool, - weak: bool, - use_pkg_config: enum { - /// Don't use pkg-config, just pass -lfoo where foo is name. - no, - /// Try to get information on how to link the library from pkg-config. - /// If that fails, fall back to passing -lfoo where foo is name. - yes, - /// Try to get information on how to link the library from pkg-config. - /// If that fails, error out. - force, - }, -}; - -pub const FrameworkLinkInfo = struct { - needed: bool = false, - weak: bool = false, -}; - -pub const IncludeDir = union(enum) { - raw_path: []const u8, - raw_path_system: []const u8, - other_step: *CompileStep, - config_header_step: *ConfigHeaderStep, -}; - pub const Options = struct { name: []const u8, - root_source_file: ?FileSource = null, + main_module: *Module, target: CrossTarget, optimize: std.builtin.Mode, kind: Kind, @@ -294,6 +226,8 @@ pub const Options = struct { use_lld: ?bool = null, }; +pub const Linkage = enum { dynamic, static }; + pub const Kind = enum { exe, lib, @@ -301,8 +235,6 @@ pub const Kind = enum { @"test", }; -pub const Linkage = enum { dynamic, static }; - pub const EmitOption = union(enum) { default: void, no_emit: void, @@ -321,7 +253,6 @@ pub const EmitOption = union(enum) { pub fn create(owner: *std.Build, options: Options) *CompileStep { const name = owner.dupe(options.name); - const root_src: ?FileSource = if (options.root_source_file) |rsrc| rsrc.dupe(owner) else null; if (mem.indexOf(u8, name, "/") != null or mem.indexOf(u8, name, "\\") != null) { panic("invalid name: '{s}'. It looks like a file path, but it is supposed to be the library or application name.", .{name}); } @@ -371,9 +302,8 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { .target = options.target, .linkage = options.linkage, .kind = options.kind, - .root_src = root_src, + .main_module = options.main_module, .name = name, - .frameworks = StringHashMap(FrameworkLinkInfo).init(owner.allocator), .step = Step.init(.{ .id = base_id, .name = step_name, @@ -388,15 +318,6 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { .out_pdb_filename = owner.fmt("{s}.pdb", .{name}), .major_only_filename = null, .name_only_filename = null, - .modules = std.StringArrayHashMap(*Module).init(owner.allocator), - .include_dirs = ArrayList(IncludeDir).init(owner.allocator), - .link_objects = ArrayList(LinkObject).init(owner.allocator), - .c_macros = ArrayList([]const u8).init(owner.allocator), - .lib_paths = ArrayList(FileSource).init(owner.allocator), - .rpaths = ArrayList(FileSource).init(owner.allocator), - .framework_dirs = ArrayList(FileSource).init(owner.allocator), - .installed_headers = ArrayList(*Step).init(owner.allocator), - .c_std = std.Build.CStd.C99, .zig_lib_dir = null, .main_pkg_path = null, .exec_cmd_args = null, @@ -408,7 +329,6 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { .rdynamic = false, .override_dest_dir = null, .installed_path = null, - .force_undefined_symbols = StringHashMap(void).init(owner.allocator), .output_path_source = GeneratedFile{ .step = &self.step }, .output_lib_path_source = GeneratedFile{ .step = &self.step }, @@ -418,8 +338,6 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { .target_info = target_info, - .is_linking_libc = options.link_libc orelse false, - .is_linking_libcpp = false, .single_threaded = options.single_threaded, .use_llvm = options.use_llvm, .use_lld = options.use_lld, @@ -454,85 +372,11 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { } } - if (root_src) |rs| rs.addStepDependencies(&self.step); + if (options.main_module.source_file) |rs| rs.addStepDependencies(&self.step); return self; } -pub fn installHeader(cs: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void { - const b = cs.step.owner; - const install_file = b.addInstallHeaderFile(src_path, dest_rel_path); - b.getInstallStep().dependOn(&install_file.step); - cs.installed_headers.append(&install_file.step) catch @panic("OOM"); -} - -pub const InstallConfigHeaderOptions = struct { - install_dir: InstallDir = .header, - dest_rel_path: ?[]const u8 = null, -}; - -pub fn installConfigHeader( - cs: *CompileStep, - config_header: *ConfigHeaderStep, - options: InstallConfigHeaderOptions, -) void { - const dest_rel_path = options.dest_rel_path orelse config_header.include_path; - const b = cs.step.owner; - const install_file = b.addInstallFileWithDir( - .{ .generated = &config_header.output_file }, - options.install_dir, - dest_rel_path, - ); - install_file.step.dependOn(&config_header.step); - b.getInstallStep().dependOn(&install_file.step); - cs.installed_headers.append(&install_file.step) catch @panic("OOM"); -} - -pub fn installHeadersDirectory( - a: *CompileStep, - src_dir_path: []const u8, - dest_rel_path: []const u8, -) void { - return installHeadersDirectoryOptions(a, .{ - .source_dir = src_dir_path, - .install_dir = .header, - .install_subdir = dest_rel_path, - }); -} - -pub fn installHeadersDirectoryOptions( - cs: *CompileStep, - options: std.Build.InstallDirStep.Options, -) void { - const b = cs.step.owner; - const install_dir = b.addInstallDirectory(options); - b.getInstallStep().dependOn(&install_dir.step); - cs.installed_headers.append(&install_dir.step) catch @panic("OOM"); -} - -pub fn installLibraryHeaders(cs: *CompileStep, l: *CompileStep) void { - assert(l.kind == .lib); - const b = cs.step.owner; - const install_step = b.getInstallStep(); - // Copy each element from installed_headers, modifying the builder - // to be the new parent's builder. - for (l.installed_headers.items) |step| { - const step_copy = switch (step.id) { - inline .install_file, .install_dir => |id| blk: { - const T = id.Type(); - const ptr = b.allocator.create(T) catch @panic("OOM"); - ptr.* = step.cast(T).?.*; - ptr.dest_builder = b; - break :blk &ptr.step; - }, - else => unreachable, - }; - cs.installed_headers.append(step_copy) catch @panic("OOM"); - install_step.dependOn(step_copy); - } - cs.installed_headers.appendSlice(l.installed_headers.items) catch @panic("OOM"); -} - pub fn addObjCopy(cs: *CompileStep, options: ObjCopyStep.Options) *ObjCopyStep { const b = cs.step.owner; var copy = options; @@ -558,58 +402,6 @@ pub fn checkObject(self: *CompileStep) *CheckObjectStep { return CheckObjectStep.create(self.step.owner, self.getOutputSource(), self.target_info.target.ofmt); } -pub fn setLinkerScriptPath(self: *CompileStep, source: FileSource) void { - const b = self.step.owner; - self.linker_script = source.dupe(b); - source.addStepDependencies(&self.step); -} - -pub fn forceUndefinedSymbol(self: *CompileStep, symbol_name: []const u8) void { - const b = self.step.owner; - self.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM"); -} - -pub fn linkFramework(self: *CompileStep, framework_name: []const u8) void { - const b = self.step.owner; - self.frameworks.put(b.dupe(framework_name), .{}) catch @panic("OOM"); -} - -pub fn linkFrameworkNeeded(self: *CompileStep, framework_name: []const u8) void { - const b = self.step.owner; - self.frameworks.put(b.dupe(framework_name), .{ - .needed = true, - }) catch @panic("OOM"); -} - -pub fn linkFrameworkWeak(self: *CompileStep, framework_name: []const u8) void { - const b = self.step.owner; - self.frameworks.put(b.dupe(framework_name), .{ - .weak = true, - }) catch @panic("OOM"); -} - -/// Returns whether the library, executable, or object depends on a particular system library. -pub fn dependsOnSystemLibrary(self: CompileStep, name: []const u8) bool { - if (isLibCLibrary(name)) { - return self.is_linking_libc; - } - if (isLibCppLibrary(name)) { - return self.is_linking_libcpp; - } - for (self.link_objects.items) |link_object| { - switch (link_object) { - .system_lib => |lib| if (mem.eql(u8, lib.name, name)) return true, - else => continue, - } - } - return false; -} - -pub fn linkLibrary(self: *CompileStep, lib: *CompileStep) void { - assert(lib.kind == .lib); - self.linkLibraryOrObject(lib); -} - pub fn isDynamicLibrary(self: *CompileStep) bool { return self.kind == .lib and self.linkage == Linkage.dynamic; } @@ -625,98 +417,6 @@ pub fn producesPdbFile(self: *CompileStep) bool { return self.isDynamicLibrary() or self.kind == .exe or self.kind == .@"test"; } -pub fn linkLibC(self: *CompileStep) void { - self.is_linking_libc = true; -} - -pub fn linkLibCpp(self: *CompileStep) void { - self.is_linking_libcpp = true; -} - -/// If the value is omitted, it is set to 1. -/// `name` and `value` need not live longer than the function call. -pub fn defineCMacro(self: *CompileStep, name: []const u8, value: ?[]const u8) void { - const b = self.step.owner; - const macro = std.Build.constructCMacro(b.allocator, name, value); - self.c_macros.append(macro) catch @panic("OOM"); -} - -/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1. -pub fn defineCMacroRaw(self: *CompileStep, name_and_value: []const u8) void { - const b = self.step.owner; - self.c_macros.append(b.dupe(name_and_value)) catch @panic("OOM"); -} - -/// This one has no integration with anything, it just puts -lname on the command line. -/// Prefer to use `linkSystemLibrary` instead. -pub fn linkSystemLibraryName(self: *CompileStep, name: []const u8) void { - const b = self.step.owner; - self.link_objects.append(.{ - .system_lib = .{ - .name = b.dupe(name), - .needed = false, - .weak = false, - .use_pkg_config = .no, - }, - }) catch @panic("OOM"); -} - -/// This one has no integration with anything, it just puts -needed-lname on the command line. -/// Prefer to use `linkSystemLibraryNeeded` instead. -pub fn linkSystemLibraryNeededName(self: *CompileStep, name: []const u8) void { - const b = self.step.owner; - self.link_objects.append(.{ - .system_lib = .{ - .name = b.dupe(name), - .needed = true, - .weak = false, - .use_pkg_config = .no, - }, - }) catch @panic("OOM"); -} - -/// Darwin-only. This one has no integration with anything, it just puts -weak-lname on the -/// command line. Prefer to use `linkSystemLibraryWeak` instead. -pub fn linkSystemLibraryWeakName(self: *CompileStep, name: []const u8) void { - const b = self.step.owner; - self.link_objects.append(.{ - .system_lib = .{ - .name = b.dupe(name), - .needed = false, - .weak = true, - .use_pkg_config = .no, - }, - }) catch @panic("OOM"); -} - -/// This links against a system library, exclusively using pkg-config to find the library. -/// Prefer to use `linkSystemLibrary` instead. -pub fn linkSystemLibraryPkgConfigOnly(self: *CompileStep, lib_name: []const u8) void { - const b = self.step.owner; - self.link_objects.append(.{ - .system_lib = .{ - .name = b.dupe(lib_name), - .needed = false, - .weak = false, - .use_pkg_config = .force, - }, - }) catch @panic("OOM"); -} - -/// This links against a system library, exclusively using pkg-config to find the library. -/// Prefer to use `linkSystemLibraryNeeded` instead. -pub fn linkSystemLibraryNeededPkgConfigOnly(self: *CompileStep, lib_name: []const u8) void { - const b = self.step.owner; - self.link_objects.append(.{ - .system_lib = .{ - .name = b.dupe(lib_name), - .needed = true, - .weak = false, - .use_pkg_config = .force, - }, - }) catch @panic("OOM"); -} - /// Run pkg-config for the given library name and parse the output, returning the arguments /// that should be passed to zig to link the given library. fn runPkgConfig(self: *CompileStep, lib_name: []const u8) ![]const []const u8 { @@ -813,72 +513,6 @@ fn runPkgConfig(self: *CompileStep, lib_name: []const u8) ![]const []const u8 { return zig_args.toOwnedSlice(); } -pub fn linkSystemLibrary(self: *CompileStep, name: []const u8) void { - self.linkSystemLibraryInner(name, .{}); -} - -pub fn linkSystemLibraryNeeded(self: *CompileStep, name: []const u8) void { - self.linkSystemLibraryInner(name, .{ .needed = true }); -} - -pub fn linkSystemLibraryWeak(self: *CompileStep, name: []const u8) void { - self.linkSystemLibraryInner(name, .{ .weak = true }); -} - -fn linkSystemLibraryInner(self: *CompileStep, name: []const u8, opts: struct { - needed: bool = false, - weak: bool = false, -}) void { - const b = self.step.owner; - if (isLibCLibrary(name)) { - self.linkLibC(); - return; - } - if (isLibCppLibrary(name)) { - self.linkLibCpp(); - return; - } - - self.link_objects.append(.{ - .system_lib = .{ - .name = b.dupe(name), - .needed = opts.needed, - .weak = opts.weak, - .use_pkg_config = .yes, - }, - }) catch @panic("OOM"); -} - -/// Handy when you have many C/C++ source files and want them all to have the same flags. -pub fn addCSourceFiles(self: *CompileStep, files: []const []const u8, flags: []const []const u8) void { - const b = self.step.owner; - const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM"); - - const files_copy = b.dupeStrings(files); - const flags_copy = b.dupeStrings(flags); - - c_source_files.* = .{ - .files = files_copy, - .flags = flags_copy, - }; - self.link_objects.append(.{ .c_source_files = c_source_files }) catch @panic("OOM"); -} - -pub fn addCSourceFile(self: *CompileStep, file: []const u8, flags: []const []const u8) void { - self.addCSourceFileSource(.{ - .args = flags, - .source = .{ .path = file }, - }); -} - -pub fn addCSourceFileSource(self: *CompileStep, source: CSourceFile) void { - const b = self.step.owner; - const c_source_file = b.allocator.create(CSourceFile) catch @panic("OOM"); - c_source_file.* = source.dupe(b); - self.link_objects.append(.{ .c_source_file = c_source_file }) catch @panic("OOM"); - source.source.addStepDependencies(&self.step); -} - pub fn setVerboseLink(self: *CompileStep, value: bool) void { self.verbose_link = value; } @@ -933,164 +567,9 @@ pub fn getOutputPdbSource(self: *CompileStep) FileSource { return .{ .generated = &self.output_pdb_path_source }; } -pub fn addAssemblyFile(self: *CompileStep, path: []const u8) void { - const b = self.step.owner; - self.link_objects.append(.{ - .assembly_file = .{ .path = b.dupe(path) }, - }) catch @panic("OOM"); -} - -pub fn addAssemblyFileSource(self: *CompileStep, source: FileSource) void { - const b = self.step.owner; - const source_duped = source.dupe(b); - self.link_objects.append(.{ .assembly_file = source_duped }) catch @panic("OOM"); - source_duped.addStepDependencies(&self.step); -} - -pub fn addObjectFile(self: *CompileStep, source_file: []const u8) void { - self.addObjectFileSource(.{ .path = source_file }); -} - -pub fn addObjectFileSource(self: *CompileStep, source: FileSource) void { - const b = self.step.owner; - self.link_objects.append(.{ .static_path = source.dupe(b) }) catch @panic("OOM"); - source.addStepDependencies(&self.step); -} - -pub fn addObject(self: *CompileStep, obj: *CompileStep) void { - assert(obj.kind == .obj); - self.linkLibraryOrObject(obj); -} - -pub const addSystemIncludeDir = @compileError("deprecated; use addSystemIncludePath"); -pub const addIncludeDir = @compileError("deprecated; use addIncludePath"); -pub const addLibPath = @compileError("deprecated, use addLibraryPath"); -pub const addFrameworkDir = @compileError("deprecated, use addFrameworkPath"); - -pub fn addSystemIncludePath(self: *CompileStep, path: []const u8) void { - const b = self.step.owner; - self.include_dirs.append(IncludeDir{ .raw_path_system = b.dupe(path) }) catch @panic("OOM"); -} - -pub fn addIncludePath(self: *CompileStep, path: []const u8) void { - const b = self.step.owner; - self.include_dirs.append(IncludeDir{ .raw_path = b.dupe(path) }) catch @panic("OOM"); -} - -pub fn addConfigHeader(self: *CompileStep, config_header: *ConfigHeaderStep) void { - self.step.dependOn(&config_header.step); - self.include_dirs.append(.{ .config_header_step = config_header }) catch @panic("OOM"); -} - -pub fn addLibraryPath(self: *CompileStep, path: []const u8) void { - const b = self.step.owner; - self.lib_paths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); -} - -pub fn addLibraryPathDirectorySource(self: *CompileStep, directory_source: FileSource) void { - self.lib_paths.append(directory_source) catch @panic("OOM"); - directory_source.addStepDependencies(&self.step); -} - -pub fn addRPath(self: *CompileStep, path: []const u8) void { - const b = self.step.owner; - self.rpaths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); -} - -pub fn addRPathDirectorySource(self: *CompileStep, directory_source: FileSource) void { - self.rpaths.append(directory_source) catch @panic("OOM"); - directory_source.addStepDependencies(&self.step); -} - -pub fn addFrameworkPath(self: *CompileStep, dir_path: []const u8) void { - const b = self.step.owner; - self.framework_dirs.append(.{ .path = b.dupe(dir_path) }) catch @panic("OOM"); -} - -pub fn addFrameworkPathDirectorySource(self: *CompileStep, directory_source: FileSource) void { - self.framework_dirs.append(directory_source) catch @panic("OOM"); - directory_source.addStepDependencies(&self.step); -} - -/// Adds a module to be used with `@import` and exposing it in the current -/// package's module table using `name`. -pub fn addModule(cs: *CompileStep, name: []const u8, module: *Module) void { - const b = cs.step.owner; - cs.modules.put(b.dupe(name), module) catch @panic("OOM"); - - var done = std.AutoHashMap(*Module, void).init(b.allocator); - defer done.deinit(); - cs.addRecursiveBuildDeps(module, &done) catch @panic("OOM"); -} - -/// Adds a module to be used with `@import` without exposing it in the current -/// package's module table. -pub fn addAnonymousModule(cs: *CompileStep, name: []const u8, options: std.Build.CreateModuleOptions) void { - const b = cs.step.owner; - const module = b.createModule(options); - return addModule(cs, name, module); -} - -pub fn addOptions(cs: *CompileStep, module_name: []const u8, options: *OptionsStep) void { - addModule(cs, module_name, options.createModule()); -} - -fn addRecursiveBuildDeps(cs: *CompileStep, module: *Module, done: *std.AutoHashMap(*Module, void)) !void { - if (done.contains(module)) return; - try done.put(module, {}); - module.source_file.addStepDependencies(&cs.step); - - for (module.include_dirs.items) |include_dir| { - cs.include_dirs.append(include_dir) catch @panic("OOM"); - } - - for (module.lib_paths.items) |lib_path| { - cs.addLibraryPath(lib_path); - } - - for (module.framework_dirs.items) |framework_dir| { - cs.addFrameworkPath(framework_dir); - } - - for (module.rpaths.items) |rpath| { - cs.addRPath(rpath); - } - - for (module.config_headers.items) |config_header| { - cs.addConfigHeader(config_header); - } - - for (module.libs.items) |lib| { - cs.linkLibrary(lib); - } - - for (module.frameworks.keys(), module.frameworks.values()) |framework, link_options| { - cs.frameworks.put(cs.step.owner.dupe(framework), link_options) catch @panic("OOM"); - } - - for (module.system_libs.items) |system_lib| { - cs.link_objects.append(.{ - .system_lib = system_lib, - }) catch @panic("OOM"); - } - - for (module.installed_headers.items) |installed_header| { - switch (installed_header) { - .header => |header| cs.installHeader(header.source_path, header.dest_rel_path), - .header_dir_step => |header_dir_step| cs.installHeadersDirectoryOptions(header_dir_step), - .config_header => |config_header| cs.installConfigHeader(config_header.step, config_header.options), - .lib_step => |lib_step| cs.installLibraryHeaders(lib_step), - } - } - - for (module.dependencies.values()) |dep| { - try cs.addRecursiveBuildDeps(dep, done); - } -} - /// If Vcpkg was found on the system, it will be added to include and lib /// paths for the specified target. -pub fn addVcpkgPaths(self: *CompileStep, linkage: CompileStep.Linkage) !void { +pub fn addVcpkgPaths(self: *CompileStep, linkage: Linkage) !void { const b = self.step.owner; // Ideally in the Unattempted case we would call the function recursively // after findVcpkgRoot and have only one switch statement, but the compiler @@ -1116,10 +595,10 @@ pub fn addVcpkgPaths(self: *CompileStep, linkage: CompileStep.Linkage) !void { const include_path = b.pathJoin(&.{ root, "installed", triplet, "include" }); errdefer allocator.free(include_path); - try self.include_dirs.append(IncludeDir{ .raw_path = include_path }); + try self.main_module.include_dirs.append(Module.IncludeDir{ .raw_path = include_path }); const lib_path = b.pathJoin(&.{ root, "installed", triplet, "lib" }); - try self.lib_paths.append(.{ .path = lib_path }); + try self.main_module.lib_paths.append(.{ .path = lib_path }); self.vcpkg_bin_path = b.pathJoin(&.{ root, "installed", triplet, "bin" }); }, @@ -1136,16 +615,6 @@ pub fn setExecCmd(self: *CompileStep, args: []const ?[]const u8) void { self.exec_cmd_args = duped_args; } -fn linkLibraryOrObject(self: *CompileStep, other: *CompileStep) void { - self.step.dependOn(&other.step); - self.link_objects.append(.{ .other_step = other }) catch @panic("OOM"); - self.include_dirs.append(.{ .other_step = other }) catch @panic("OOM"); - - for (other.installed_headers.items) |install_step| { - self.step.dependOn(install_step); - } -} - fn appendModuleArgs( cs: *CompileStep, zig_args: *ArrayList([]const u8), @@ -1161,21 +630,15 @@ fn appendModuleArgs( name: []const u8, mod: *Module, }).init(b.allocator); - { - var it = cs.modules.iterator(); - while (it.next()) |kv| { - // While we're traversing the root dependencies, let's make sure that no module names - // have colons in them, since the CLI forbids it. We handle this for transitive - // dependencies further down. - if (std.mem.indexOfScalar(u8, kv.key_ptr.*, ':') != null) { - @panic("Module names cannot contain colons"); - } - try to_name.append(.{ - .name = kv.key_ptr.*, - .mod = kv.value_ptr.*, - }); - } - } + // Make sure that the main module name does not have colons in it, since the CLI forbids it. + // We handle this for transitive dependencies further down. + if (std.mem.indexOfScalar(u8, cs.name, ':') != null) { + @panic("Module names cannot contain colons"); + } + try to_name.append(.{ + .name = cs.name, + .mod = cs.main_module, + }); while (to_name.popOrNull()) |dep| { if (mod_names.contains(dep.mod)) continue; @@ -1225,12 +688,9 @@ fn appendModuleArgs( } } - // Lastly, output the root dependencies - const deps_str = try constructDepString(b.allocator, mod_names, cs.modules); - if (deps_str.len > 0) { - try zig_args.append("--deps"); - try zig_args.append(deps_str); - } + // Lastly, output the main module dependency + try zig_args.append("--deps"); + try zig_args.append(cs.name); } fn constructDepString( @@ -2001,24 +1461,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } -fn isLibCLibrary(name: []const u8) bool { - const libc_libraries = [_][]const u8{ "c", "m", "dl", "rt", "pthread" }; - for (libc_libraries) |libc_lib_name| { - if (mem.eql(u8, name, libc_lib_name)) - return true; - } - return false; -} - -fn isLibCppLibrary(name: []const u8) bool { - const libcpp_libraries = [_][]const u8{ "c++", "stdc++" }; - for (libcpp_libraries) |libcpp_lib_name| { - if (mem.eql(u8, name, libcpp_lib_name)) - return true; - } - return false; -} - /// Returned slice must be freed by the caller. fn findVcpkgRoot(allocator: Allocator) !?[]const u8 { const appdata_path = try fs.getAppDataDir(allocator, "vcpkg"); diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index ca4f98ddd8ef..288dd521cd4b 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -4,17 +4,183 @@ builder: *Build, /// This could either be a generated file, in which case the module /// contains exactly one file, or it could be a path to the root source /// file of directory of files which constitute the module. -source_file: FileSource, +source_file: ?FileSource, dependencies: std.StringArrayHashMap(*Module), -include_dirs: std.ArrayListUnmanaged(CompileStep.IncludeDir) = .{}, -lib_paths: std.ArrayListUnmanaged([]const u8) = .{}, -rpaths: std.ArrayListUnmanaged([]const u8) = .{}, -framework_dirs: std.ArrayListUnmanaged([]const u8) = .{}, -system_libs: std.ArrayListUnmanaged(CompileStep.SystemLib) = .{}, -libs: std.ArrayListUnmanaged(*CompileStep) = .{}, -config_headers: std.ArrayListUnmanaged(*ConfigHeaderStep) = .{}, -installed_headers: std.ArrayListUnmanaged(InstalledHeader) = .{}, -frameworks: std.StringArrayHashMapUnmanaged(CompileStep.FrameworkLinkInfo) = .{}, +link_objects: ArrayList(LinkObject), +installed_headers: ArrayList(*Step), +frameworks: StringHashMap(FrameworkLinkInfo), +include_dirs: ArrayList(IncludeDir), +lib_paths: ArrayList(FileSource), +rpaths: ArrayList(FileSource), +framework_dirs: ArrayList(FileSource), +c_macros: ArrayList([]const u8), +c_std: std.Build.CStd, + +is_linking_libc: bool, +is_linking_libcpp: bool, +linker_script: ?FileSource = null, + +/// List of symbols forced as undefined in the symbol table +/// thus forcing their resolution by the linker. +/// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. +force_undefined_symbols: std.StringHashMap(void), + +pub fn create(owner: *Build, options: CreateModuleOptions) *Module { + const mod = owner.allocator.create(Module) catch @panic("OOM"); + const arena = owner.allocator; + mod.* = .{ + .builder = owner, + .source_file = options.source_file, + .dependencies = moduleDependenciesToArrayHashMap(arena, options.dependencies), + .link_objects = ArrayList(LinkObject).init(arena), + .installed_headers = ArrayList(*Step).init(arena), + .frameworks = StringHashMap(FrameworkLinkInfo).init(arena), + .include_dirs = ArrayList(IncludeDir).init(arena), + .lib_path = ArrayList(FileSource).init(arena), + .rpaths = ArrayList(FileSource).init(arena), + .framework_dirs = ArrayList(FileSource).init(arena), + .c_macros = ArrayList([]const u8).init(arena), + .c_std = std.Build.CStd.C99, + .is_linking_libc = false, + .is_linking_libcpp = false, + .linker_script = null, + }; + if (options.c_source_files) |c_files| { + mod.addCSourceFiles(c_files.files, c_files.flags); + } + + return mod; +} + +fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { + var result = std.StringArrayHashMap(*Module).init(arena); + for (deps) |dep| { + result.put(dep.name, dep.module) catch @panic("OOM"); + } + return result; +} + +pub fn addOptions(m: *Module, name: []const u8, options: *OptionsStep) void { + m.dependencies.put(m.builder.dupe(name), options.addModule(name)) catch @panic("OOM"); +} + +pub const LinkObject = union(enum) { + static_path: FileSource, + other_step: *CompileStep, + system_lib: SystemLib, + assembly_file: FileSource, + c_source_file: *CSourceFile, + c_source_files: *CSourceFiles, +}; + +pub fn linkLibC(self: *CompileStep) void { + self.is_linking_libc = true; +} + +pub fn linkLibCpp(self: *CompileStep) void { + self.is_linking_libcpp = true; +} + +/// This one has no integration with anything, it just puts -lname on the command line. +/// Prefer to use `linkSystemLibrary` instead. +pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { + const b = m.builder; + m.link_objects.append(.{ + .system_lib = .{ + .name = b.dupe(name), + .needed = false, + .weak = false, + .use_pkg_config = .no, + }, + }) catch @panic("OOM"); +} + +/// This one has no integration with anything, it just puts -needed-lname on the command line. +/// Prefer to use `linkSystemLibraryNeeded` instead. +pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { + const b = m.builder; + m.link_objects.append(.{ + .system_lib = .{ + .name = b.dupe(name), + .needed = true, + .weak = false, + .use_pkg_config = .no, + }, + }) catch @panic("OOM"); +} + +/// Darwin-only. This one has no integration with anything, it just puts -weak-lname on the +/// command line. Prefer to use `linkSystemLibraryWeak` instead. +pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { + const b = m.builder; + m.link_objects.append(.{ + .system_lib = .{ + .name = b.dupe(name), + .needed = false, + .weak = true, + .use_pkg_config = .no, + }, + }) catch @panic("OOM"); +} + +/// This links against a system library, exclusively using pkg-config to find the library. +/// Prefer to use `linkSystemLibrary` instead. +pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { + const b = m.builder; + m.link_objects.append(.{ + .system_lib = .{ + .name = b.dupe(lib_name), + .needed = false, + .weak = false, + .use_pkg_config = .force, + }, + }) catch @panic("OOM"); +} + +/// This links against a system library, exclusively using pkg-config to find the library. +/// Prefer to use `linkSystemLibraryNeeded` instead. +pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) void { + const b = m.builder; + m.link_objects.append(.{ + .system_lib = .{ + .name = b.dupe(lib_name), + .needed = true, + .weak = false, + .use_pkg_config = .force, + }, + }) catch @panic("OOM"); +} + +/// Handy when you have many C/C++ source files and want them all to have the same flags. +pub fn addCSourceFiles(m: *Module, files: []const []const u8, flags: []const []const u8) void { + const b = m.builder; + const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM"); + + const files_copy = b.dupeStrings(files); + const flags_copy = b.dupeStrings(flags); + + c_source_files.* = .{ + .files = .{ .path = files_copy }, + .flags = flags_copy, + }; + m.link_objects.append(.{ .c_source_files = c_source_files }) catch @panic("OOM"); +} + +pub fn addCSourceFile(m: *Module, file: []const u8, flags: []const []const u8) void { + m.addCSourceFileSource(.{ + .args = flags, + .source = .{ .path = file }, + }); +} + +pub fn addCSourceFileSource(m: *Module, source: CSourceFile) void { + const b = m.builder; + const c_source_file = b.allocator.create(CSourceFile) catch @panic("OOM"); + c_source_file.* = source.dupe(b); + m.link_objects.append(.{ .c_source_file = c_source_file }) catch @panic("OOM"); + // source.source.addStepDependencies(&self.step); + // TODO: Add step dependencies to consumers of the module +} const InstalledHeader = union(enum) { header: struct { @@ -29,193 +195,342 @@ const InstalledHeader = union(enum) { lib_step: *CompileStep, }; -pub fn addIncludePath(m: *Module, path: []const u8) void { - m.include_dirs.append( - m.builder.allocator, - CompileStep.IncludeDir{ .raw_path = m.builder.dupe(path) }, - ) catch @panic("OOM"); +pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { + const b = m.builder; + const install_file = b.addInstallHeaderFile(src_path, dest_rel_path); + b.getInstallStep().dependOn(&install_file.step); + m.installed_headers.append(&install_file.step) catch @panic("OOM"); } -pub fn addSystemIncludePath(m: *Module, path: []const u8) void { - m.include_dirs.append( - m.builder.allocator, - CompileStep.IncludeDir{ .raw_path_system = m.builder.dupe(path) }, - ) catch @panic("OOM"); -} +pub const InstallConfigHeaderOptions = struct { + install_dir: InstallDir = .header, + dest_rel_path: ?[]const u8 = null, +}; -pub fn addRPath(m: *Module, path: []const u8) void { - m.rpaths.append(m.builder.allocator, m.builder.dupe(path)) catch @panic("OOM"); +pub fn installConfigHeader( + m: *Module, + config_header: *ConfigHeaderStep, + options: InstallConfigHeaderOptions, +) void { + const dest_rel_path = options.dest_rel_path orelse config_header.include_path; + const b = m.builder; + const install_file = b.addInstallFileWithDir( + .{ .generated = &config_header.output_file }, + options.install_dir, + dest_rel_path, + ); + install_file.step.dependOn(&config_header.step); + b.getInstallStep().dependOn(&install_file.step); + m.installed_headers.append(&install_file.step) catch @panic("OOM"); } -pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { - m.framework_dirs.append(m.builder.allocator, m.builder.dupe(dir_path)) catch @panic("OOM"); +pub fn installHeadersDirectory( + m: *Module, + src_dir_path: []const u8, + dest_rel_path: []const u8, +) void { + return installHeadersDirectoryOptions(m, .{ + .source_dir = src_dir_path, + .install_dir = .header, + .install_subdir = dest_rel_path, + }); } -pub fn addLibraryPath(m: *Module, library_path: []const u8) void { - m.lib_paths.append(m.builder.allocator, m.builder.dupe(library_path)) catch @panic("OOM"); +pub fn installHeadersDirectoryOptions( + m: *Module, + options: std.Build.InstallDirStep.Options, +) void { + const b = m.builder; + const install_dir = b.addInstallDirectory(options); + b.getInstallStep().dependOn(&install_dir.step); + m.installed_headers.append(&install_dir.step) catch @panic("OOM"); } -pub fn addConfigHeader(m: *Module, config_header: *Build.ConfigHeaderStep) void { - m.config_headers.append(m.builder.allocator, config_header) catch @panic("OOM"); +pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { + assert(l.kind == .lib); + const b = m.builder; + const install_step = b.getInstallStep(); + // Copy each element from installed_headers, modifying the builder + // to be the new parent's builder. + for (l.main_module.installed_headers.items) |step| { + const step_copy = switch (step.id) { + inline .install_file, .install_dir => |id| blk: { + const T = id.Type(); + const ptr = b.allocator.create(T) catch @panic("OOM"); + ptr.* = step.cast(T).?.*; + ptr.dest_builder = b; + break :blk &ptr.step; + }, + else => unreachable, + }; + m.installed_headers.append(step_copy) catch @panic("OOM"); + install_step.dependOn(step_copy); + } + m.installed_headers.appendSlice(l.main_module.installed_headers.items) catch @panic("OOM"); } -pub fn linkLibC(m: *Module) void { - m.linkSystemLibrary("c"); +/// If the value is omitted, it is set to 1. +/// `name` and `value` need not live longer than the function call. +pub fn defineCMacro(self: *CompileStep, name: []const u8, value: ?[]const u8) void { + const b = self.step.owner; + const macro = std.Build.constructCMacro(b.allocator, name, value); + self.c_macros.append(macro) catch @panic("OOM"); } -pub fn linkLibCpp(m: *Module) void { - m.linkSystemLibrary("c++"); +/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1. +pub fn defineCMacroRaw(self: *CompileStep, name_and_value: []const u8) void { + const b = self.step.owner; + self.c_macros.append(b.dupe(name_and_value)) catch @panic("OOM"); } +pub const SystemLib = struct { + name: []const u8, + needed: bool, + weak: bool, + use_pkg_config: enum { + /// Don't use pkg-config, just pass -lfoo where foo is name. + no, + /// Try to get information on how to link the library from pkg-config. + /// If that fails, fall back to passing -lfoo where foo is name. + yes, + /// Try to get information on how to link the library from pkg-config. + /// If that fails, error out. + force, + }, +}; + pub fn linkLibrary(m: *Module, lib: *CompileStep) void { - m.libs.append(m.builder.allocator, lib) catch @panic("OOM"); + assert(lib.kind == .lib); + m.linkLibraryOrObject(lib); } -pub fn linkSystemLibrary(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = false, - .use_pkg_config = .yes, +fn linkLibraryOrObject(m: *Module, other: *CompileStep) void { + m.link_objects.append(.{ .other_step = other }) catch @panic("OOM"); + m.include_dirs.append(.{ .other_step = other }) catch @panic("OOM"); + + // TODO: Need to depend on installed_headers steps + // for (other.main_module.installed_headers.items) |install_step| { + // self.step.dependOn(install_step); + // } +} + +pub fn addAssemblyFile(m: *Module, path: []const u8) void { + const b = m.builder; + m.link_objects.append(.{ + .assembly_file = .{ .path = b.dupe(path) }, }) catch @panic("OOM"); } +pub fn addAssemblyFileSource(m: *Module, source: FileSource) void { + const b = m.builder; + const source_duped = source.dupe(b); + m.link_objects.append(.{ .assembly_file = source_duped }) catch @panic("OOM"); + // source_duped.addStepDependencies(&self.step); + // TODO +} + +pub fn addObjectFile(m: *Module, source_file: []const u8) void { + m.addObjectFileSource(.{ .path = source_file }); +} + +pub fn addObjectFileSource(m: *Module, source: FileSource) void { + const b = m.builder; + m.link_objects.append(.{ .static_path = source.dupe(b) }) catch @panic("OOM"); + // source.addStepDependencies(&self.step); + // TODO +} + +pub fn addObject(m: *Module, obj: *CompileStep) void { + assert(obj.kind == .obj); + m.linkLibraryOrObject(obj); +} + +pub fn linkSystemLibrary(m: *Module, name: []const u8) void { + m.linkSystemLibraryInner(name, .{}); +} + pub fn linkSystemLibraryNeeded(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = true, - .weak = false, - .use_pkg_config = .yes, - }) catch @panic("OOM"); + m.linkSystemLibraryInner(name, .{ .needed = true }); } pub fn linkSystemLibraryWeak(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = true, - .use_pkg_config = .yes, - }) catch @panic("OOM"); + m.linkSystemLibraryInner(name, .{ .weak = true }); } -pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = false, - .use_pkg_config = .no, - }) catch @panic("OOM"); -} +fn linkSystemLibraryInner(m: *Module, name: []const u8, opts: struct { + needed: bool = false, + weak: bool = false, +}) void { + const b = m.builder; + if (isLibCLibrary(name)) { + m.linkLibC(); + return; + } + if (isLibCppLibrary(name)) { + m.linkLibCpp(); + return; + } -pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = true, - .weak = false, - .use_pkg_config = .no, + m.link_objects.append(.{ + .system_lib = .{ + .name = b.dupe(name), + .needed = opts.needed, + .weak = opts.weak, + .use_pkg_config = .yes, + }, }) catch @panic("OOM"); } -pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(name), - .needed = false, - .weak = true, - .use_pkg_config = .no, - }) catch @panic("OOM"); +fn isLibCLibrary(name: []const u8) bool { + const libc_libraries = [_][]const u8{ "c", "m", "dl", "rt", "pthread" }; + for (libc_libraries) |libc_lib_name| { + if (mem.eql(u8, name, libc_lib_name)) + return true; + } + return false; } -pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(lib_name), - .needed = false, - .weak = false, - .use_pkg_config = .force, - }) catch @panic("OOM"); +fn isLibCppLibrary(name: []const u8) bool { + const libcpp_libraries = [_][]const u8{ "c++", "stdc++" }; + for (libcpp_libraries) |libcpp_lib_name| { + if (mem.eql(u8, name, libcpp_lib_name)) + return true; + } + return false; } -pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) void { - m.system_libs.append(m.builder.allocator, .{ - .name = m.builder.dupe(lib_name), - .needed = true, - .weak = false, - .use_pkg_config = .force, - }) catch @panic("OOM"); +/// Returns whether the module depends on a particular system library. +pub fn dependsOnSystemLibrary(m: Module, name: []const u8) bool { + if (isLibCLibrary(name)) { + return m.is_linking_libc; + } + if (isLibCppLibrary(name)) { + return m.is_linking_libcpp; + } + for (m.link_objects.items) |link_object| { + switch (link_object) { + .system_lib => |lib| if (mem.eql(u8, lib.name, name)) return true, + else => continue, + } + } + return false; } +pub const FrameworkLinkInfo = struct { + needed: bool = false, + weak: bool = false, +}; + pub fn linkFramework(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{}) catch @panic("OOM"); + const b = m.builder; + m.frameworks.put(b.dupe(framework_name), .{}) catch @panic("OOM"); } pub fn linkFrameworkNeeded(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ + const b = m.builder; + m.frameworks.put(b.dupe(framework_name), .{ .needed = true, }) catch @panic("OOM"); } pub fn linkFrameworkWeak(m: *Module, framework_name: []const u8) void { - m.frameworks.put(m.builder.allocator, m.builder.dupe(framework_name), .{ + const b = m.builder; + m.frameworks.put(b.dupe(framework_name), .{ .weak = true, }) catch @panic("OOM"); } -pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { - m.installed_headers.append(m.builder.allocator, .{ - .header = .{ - .source_path = m.builder.dupe(src_path), - .dest_rel_path = dest_rel_path, - }, - }) catch @panic("OOM"); +pub const IncludeDir = union(enum) { + raw_path: []const u8, + raw_path_system: []const u8, + other_step: *CompileStep, + config_header_step: *ConfigHeaderStep, +}; + +pub fn addSystemIncludePath(m: *Module, path: []const u8) void { + const b = m.builder; + m.include_dirs.append(IncludeDir{ .raw_path_system = b.dupe(path) }) catch @panic("OOM"); } -pub fn installConfigHeader( - m: *Module, - config_header: *ConfigHeaderStep, - options: CompileStep.InstallConfigHeaderOptions, -) void { - m.installed_headers.append(m.builder.allocator, .{ - .config_header = .{ - .config_header = config_header, - .options = options, - }, - }) catch @panic("OOM"); +pub fn addIncludePath(m: *Module, path: []const u8) void { + const b = m.builder; + m.include_dirs.append(IncludeDir{ .raw_path = b.dupe(path) }) catch @panic("OOM"); } -pub fn installHeadersDirectory( - m: *Module, - src_dir_path: []const u8, - dest_rel_path: []const u8, -) void { - return m.installHeadersDirectoryOptions(.{ - .source_dir = m.builder.dupe(src_dir_path), - .install_dir = .header, - .install_subdir = dest_rel_path, - }); +pub fn addConfigHeader(m: *Module, config_header: *ConfigHeaderStep) void { + // m.step.dependOn(&config_header.step); + // TODO + m.include_dirs.append(.{ .config_header_step = config_header }) catch @panic("OOM"); } -pub fn installHeadersDirectoryOptions( - m: *Module, - options: InstallDirStep.Options, -) void { - m.installed_headers.append(m.builder.allocator, .{ - .header_dir_step = options, - }) catch @panic("OOM"); +pub fn addLibraryPath(m: *Module, path: []const u8) void { + const b = m.builder; + m.lib_paths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); } -pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { - m.installed_headers.append(m.builder.allocator, .{ - .lib_step = l, - }) catch @panic("OOM"); +pub fn addLibraryPathDirectorySource(m: *Module, directory_source: FileSource) void { + m.lib_paths.append(directory_source) catch @panic("OOM"); + // directory_source.addStepDependencies(&self.step); + // TODO } -pub fn addOptions(m: *Module, name: []const u8, options: *OptionsStep) void { - m.dependencies.put(m.builder.dupe(name), options.addModule(name)) catch @panic("OOM"); +pub fn addRPath(m: *Module, path: []const u8) void { + const b = m.builder; + m.rpaths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); +} + +pub fn addRPathDirectorySource(m: *Module, directory_source: FileSource) void { + m.rpaths.append(directory_source) catch @panic("OOM"); + // directory_source.addStepDependencies(&m.step); + // TODO +} + +pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { + const b = m.builder; + m.framework_dirs.append(.{ .path = b.dupe(dir_path) }) catch @panic("OOM"); +} + +pub fn addFrameworkPathDirectorySource(m: *Module, directory_source: FileSource) void { + m.framework_dirs.append(directory_source) catch @panic("OOM"); + // directory_source.addStepDependencies(&self.step); + // TODO +} + +pub fn forceUndefinedSymbol(m: *Module, symbol_name: []const u8) void { + const b = m.builder; + m.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM"); +} + +pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { + const b = m.builder; + m.linker_script = source.dupe(b); + // source.addStepDependencies(&self.step); + // TODO } const std = @import("std"); const Build = std.Build; +const Step = Build.Step; const FileSource = Build.FileSource; const CompileStep = Build.CompileStep; const ConfigHeaderStep = Build.ConfigHeaderStep; const OptionsStep = Build.OptionsStep; const InstallDirStep = Build.InstallDirStep; +const InstallDir = Build.InstallDir; +const CSourceFile = Build.CSourceFile; +const CSourceFiles = Build.CSourceFiles; +const ModuleDependency = Build.ModuleDependency; +const CreateModuleOptions = Build.CreateModuleOptions; + +const ArrayList = std.ArrayList; +const StringHashMap = std.StringHashMap; +const Allocator = std.mem.Allocator; + +const assert = std.debug.assert; +const mem = std.mem; +const fs = std.fs; + +pub const addSystemIncludeDir = @compileError("deprecated; use addSystemIncludePath"); +pub const addIncludeDir = @compileError("deprecated; use addIncludePath"); +pub const addLibPath = @compileError("deprecated, use addLibraryPath"); +pub const addFrameworkDir = @compileError("deprecated, use addFrameworkPath"); From 40218656b7e192daf843190dcda0037907af0a33 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sun, 23 Apr 2023 00:02:58 +0000 Subject: [PATCH 16/29] wip: temp fixes to compile errors in CompileStep and RunStep These changes will not last, as I don't think they'll work with deeper dependency trees, but for now they'll do. --- lib/std/Build.zig | 15 ++++++----- lib/std/Build/CompileStep.zig | 47 ++++++++++++++++++----------------- lib/std/Build/Module.zig | 30 +++++++++++----------- lib/std/Build/RunStep.zig | 6 ++--- 4 files changed, 52 insertions(+), 46 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 9e43fddf3db3..dc0799787044 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -449,7 +449,7 @@ pub fn addOptions(self: *Build) *OptionsStep { pub const ExecutableOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + main_module_options: CreateModuleOptions, version: ?std.builtin.Version = null, target: CrossTarget = .{}, optimize: std.builtin.Mode = .Debug, @@ -462,9 +462,10 @@ pub const ExecutableOptions = struct { }; pub fn addExecutable(b: *Build, options: ExecutableOptions) *CompileStep { + const main_module = b.createModule(options.main_module_options); return CompileStep.create(b, .{ .name = options.name, - .root_source_file = options.root_source_file, + .main_module = main_module, .version = options.version, .target = options.target, .optimize = options.optimize, @@ -507,7 +508,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *CompileStep { pub const SharedLibraryOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + main_module_options: CreateModuleOptions, version: ?std.builtin.Version = null, target: CrossTarget, optimize: std.builtin.Mode, @@ -519,9 +520,10 @@ pub const SharedLibraryOptions = struct { }; pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *CompileStep { + const main_module = b.createModule(options.main_module_options); return CompileStep.create(b, .{ .name = options.name, - .root_source_file = options.root_source_file, + .main_module = main_module, .kind = .lib, .linkage = .dynamic, .version = options.version, @@ -537,7 +539,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *CompileStep { pub const StaticLibraryOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + main_module_options: CreateModuleOptions, target: CrossTarget, optimize: std.builtin.Mode, version: ?std.builtin.Version = null, @@ -549,9 +551,10 @@ pub const StaticLibraryOptions = struct { }; pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *CompileStep { + const main_module = b.createModule(options.main_module_options); return CompileStep.create(b, .{ .name = options.name, - .root_source_file = options.root_source_file, + .main_module = main_module, .kind = .lib, .linkage = .static, .version = options.version, diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index b72df3eb223c..6a1619d4fd8b 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -88,7 +88,6 @@ wasi_exec_model: ?std.builtin.WasiExecModel = null, /// Symbols to be exported when compiling to wasm export_symbol_names: []const []const u8 = &.{}, -root_src: ?FileSource, out_h_filename: []const u8, out_lib_filename: []const u8, out_pdb_filename: []const u8, @@ -682,7 +681,9 @@ fn appendModuleArgs( const name = kv.value_ptr.*; const deps_str = try constructDepString(b.allocator, mod_names, mod.dependencies); - const src = mod.builder.pathFromRoot(mod.source_file.getPath(mod.builder)); + // TODO: source_file may not always exits, for example if a module consists only of C source files. + // How do we represent this case in the CLI + const src = mod.builder.pathFromRoot(mod.source_file.?.getPath(mod.builder)); try zig_args.append("--mod"); try zig_args.append(try std.fmt.allocPrint(b.allocator, "{s}:{s}:{s}", .{ name, deps_str, src })); } @@ -720,7 +721,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { const b = step.owner; const self = @fieldParentPtr(CompileStep, "step", step); - if (self.root_src == null and self.link_objects.items.len == 0) { + if (self.main_module.source_file == null and self.main_module.link_objects.items.len == 0) { return step.fail("the linker needs one or more objects to link", .{}); } @@ -754,7 +755,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } { - var it = self.force_undefined_symbols.keyIterator(); + var it = self.main_module.force_undefined_symbols.keyIterator(); while (it.next()) |symbol_name| { try zig_args.append("--force_undefined"); try zig_args.append(symbol_name.*); @@ -766,7 +767,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append(try std.fmt.allocPrint(b.allocator, "{}", .{stack_size})); } - if (self.root_src) |root_src| try zig_args.append(root_src.getPath(b)); + if (self.main_module.source_file) |root_src| try zig_args.append(root_src.getPath(b)); // We will add link objects from transitive dependencies, but we want to keep // all link objects in the same order provided. @@ -775,13 +776,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { .link_objects = ArrayList(LinkObject).init(b.allocator), .seen_system_libs = StringHashMap(void).init(b.allocator), .seen_steps = std.AutoHashMap(*const Step, void).init(b.allocator), - .is_linking_libcpp = self.is_linking_libcpp, - .is_linking_libc = self.is_linking_libc, - .frameworks = &self.frameworks, + .is_linking_libcpp = self.main_module.is_linking_libcpp, + .is_linking_libc = self.main_module.is_linking_libc, + .frameworks = &self.main_module.frameworks, }; try transitive_deps.seen_steps.put(&self.step, {}); - try transitive_deps.add(self.link_objects.items); + try transitive_deps.add(self.main_module.link_objects.items); var prev_has_extra_flags = false; @@ -1144,7 +1145,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } - if (self.linker_script) |linker_script| { + if (self.main_module.linker_script) |linker_script| { try zig_args.append("--script"); try zig_args.append(linker_script.getPath(b)); } @@ -1169,7 +1170,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try self.appendModuleArgs(&zig_args); - for (self.include_dirs.items) |include_dir| { + for (self.main_module.include_dirs.items) |include_dir| { switch (include_dir) { .raw_path => |include_path| { try zig_args.append("-I"); @@ -1204,7 +1205,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append("-isystem"); try zig_args.append(fs.path.dirname(h_path).?); } - if (other.installed_headers.items.len > 0) { + if (other.main_module.installed_headers.items.len > 0) { try zig_args.append("-I"); try zig_args.append(b.pathJoin(&.{ other.step.owner.install_prefix, "include", @@ -1219,19 +1220,19 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } - for (self.c_macros.items) |c_macro| { + for (self.main_module.c_macros.items) |c_macro| { try zig_args.append("-D"); try zig_args.append(c_macro); } - try zig_args.ensureUnusedCapacity(2 * self.lib_paths.items.len); - for (self.lib_paths.items) |lib_path| { + try zig_args.ensureUnusedCapacity(2 * self.main_module.lib_paths.items.len); + for (self.main_module.lib_paths.items) |lib_path| { zig_args.appendAssumeCapacity("-L"); zig_args.appendAssumeCapacity(lib_path.getPath2(b, step)); } - try zig_args.ensureUnusedCapacity(2 * self.rpaths.items.len); - for (self.rpaths.items) |rpath| { + try zig_args.ensureUnusedCapacity(2 * self.main_module.rpaths.items.len); + for (self.main_module.rpaths.items) |rpath| { zig_args.appendAssumeCapacity("-rpath"); if (self.target_info.target.isDarwin()) switch (rpath) { @@ -1252,7 +1253,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { zig_args.appendAssumeCapacity(rpath.getPath2(b, step)); } - for (self.framework_dirs.items) |directory_source| { + for (self.main_module.framework_dirs.items) |directory_source| { if (b.sysroot != null) { try zig_args.append("-iframeworkwithsysroot"); } else { @@ -1264,7 +1265,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } { - var it = self.frameworks.iterator(); + var it = self.main_module.frameworks.iterator(); while (it.next()) |entry| { const name = entry.key_ptr.*; const info = entry.value_ptr.*; @@ -1576,19 +1577,19 @@ const TransitiveDeps = struct { fn addInner(td: *TransitiveDeps, other: *CompileStep, dyn: bool) !void { // Inherit dependency on libc and libc++ - td.is_linking_libcpp = td.is_linking_libcpp or other.is_linking_libcpp; - td.is_linking_libc = td.is_linking_libc or other.is_linking_libc; + td.is_linking_libcpp = td.is_linking_libcpp or other.main_module.is_linking_libcpp; + td.is_linking_libc = td.is_linking_libc or other.main_module.is_linking_libc; // Inherit dependencies on darwin frameworks if (!dyn) { - var it = other.frameworks.iterator(); + var it = other.main_module.frameworks.iterator(); while (it.next()) |framework| { try td.frameworks.put(framework.key_ptr.*, framework.value_ptr.*); } } // Inherit dependencies on system libraries and static libraries. - for (other.link_objects.items) |other_link_object| { + for (other.main_module.link_objects.items) |other_link_object| { switch (other_link_object) { .system_lib => |system_lib| { if ((try td.seen_system_libs.fetchPut(system_lib.name, {})) != null) diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 288dd521cd4b..b9c35e9c515e 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -5,7 +5,7 @@ builder: *Build, /// contains exactly one file, or it could be a path to the root source /// file of directory of files which constitute the module. source_file: ?FileSource, -dependencies: std.StringArrayHashMap(*Module), +dependencies: StringArrayHashMap(*Module), link_objects: ArrayList(LinkObject), installed_headers: ArrayList(*Step), frameworks: StringHashMap(FrameworkLinkInfo), @@ -23,7 +23,7 @@ linker_script: ?FileSource = null, /// List of symbols forced as undefined in the symbol table /// thus forcing their resolution by the linker. /// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. -force_undefined_symbols: std.StringHashMap(void), +force_undefined_symbols: StringHashMap(void), pub fn create(owner: *Build, options: CreateModuleOptions) *Module { const mod = owner.allocator.create(Module) catch @panic("OOM"); @@ -36,9 +36,10 @@ pub fn create(owner: *Build, options: CreateModuleOptions) *Module { .installed_headers = ArrayList(*Step).init(arena), .frameworks = StringHashMap(FrameworkLinkInfo).init(arena), .include_dirs = ArrayList(IncludeDir).init(arena), - .lib_path = ArrayList(FileSource).init(arena), + .lib_paths = ArrayList(FileSource).init(arena), .rpaths = ArrayList(FileSource).init(arena), .framework_dirs = ArrayList(FileSource).init(arena), + .force_undefined_symbols = StringHashMap(void).init(arena), .c_macros = ArrayList([]const u8).init(arena), .c_std = std.Build.CStd.C99, .is_linking_libc = false, @@ -73,12 +74,12 @@ pub const LinkObject = union(enum) { c_source_files: *CSourceFiles, }; -pub fn linkLibC(self: *CompileStep) void { - self.is_linking_libc = true; +pub fn linkLibC(m: *Module) void { + m.is_linking_libc = true; } -pub fn linkLibCpp(self: *CompileStep) void { - self.is_linking_libcpp = true; +pub fn linkLibCpp(m: *Module) void { + m.is_linking_libcpp = true; } /// This one has no integration with anything, it just puts -lname on the command line. @@ -160,7 +161,7 @@ pub fn addCSourceFiles(m: *Module, files: []const []const u8, flags: []const []c const flags_copy = b.dupeStrings(flags); c_source_files.* = .{ - .files = .{ .path = files_copy }, + .files = files_copy, .flags = flags_copy, }; m.link_objects.append(.{ .c_source_files = c_source_files }) catch @panic("OOM"); @@ -271,16 +272,16 @@ pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { /// If the value is omitted, it is set to 1. /// `name` and `value` need not live longer than the function call. -pub fn defineCMacro(self: *CompileStep, name: []const u8, value: ?[]const u8) void { - const b = self.step.owner; +pub fn defineCMacro(m: *Module, name: []const u8, value: ?[]const u8) void { + const b = m.builder; const macro = std.Build.constructCMacro(b.allocator, name, value); - self.c_macros.append(macro) catch @panic("OOM"); + m.c_macros.append(macro) catch @panic("OOM"); } /// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1. -pub fn defineCMacroRaw(self: *CompileStep, name_and_value: []const u8) void { - const b = self.step.owner; - self.c_macros.append(b.dupe(name_and_value)) catch @panic("OOM"); +pub fn defineCMacroRaw(m: *Module, name_and_value: []const u8) void { + const b = m.builder; + m.c_macros.append(b.dupe(name_and_value)) catch @panic("OOM"); } pub const SystemLib = struct { @@ -524,6 +525,7 @@ const CreateModuleOptions = Build.CreateModuleOptions; const ArrayList = std.ArrayList; const StringHashMap = std.StringHashMap; +const StringArrayHashMap = std.StringArrayHashMap; const Allocator = std.mem.Allocator; const assert = std.debug.assert; diff --git a/lib/std/Build/RunStep.zig b/lib/std/Build/RunStep.zig index 435c7369d039..c0194090d3a6 100644 --- a/lib/std/Build/RunStep.zig +++ b/lib/std/Build/RunStep.zig @@ -579,10 +579,10 @@ fn runCommand( else => break :interpret, } - const need_cross_glibc = exe.target.isGnuLibC() and exe.is_linking_libc; + const need_cross_glibc = exe.target.isGnuLibC() and exe.main_module.is_linking_libc; switch (b.host.getExternalExecutor(exe.target_info, .{ .qemu_fixes_dl = need_cross_glibc and b.glibc_runtimes_dir != null, - .link_libc = exe.is_linking_libc, + .link_libc = exe.main_module.is_linking_libc, })) { .native, .rosetta => { if (allow_skip) return error.MakeSkipped; @@ -1177,7 +1177,7 @@ fn evalGeneric(self: *RunStep, child: *std.process.Child) !StdIoResult { fn addPathForDynLibs(self: *RunStep, artifact: *CompileStep) void { const b = self.step.owner; - for (artifact.link_objects.items) |link_object| { + for (artifact.main_module.link_objects.items) |link_object| { switch (link_object) { .other_step => |other| { if (other.target.isWindows() and other.isDynamicLibrary()) { From b68313c7231069b65379c4af170cc8334eb8ead1 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sun, 23 Apr 2023 23:36:04 +0000 Subject: [PATCH 17/29] Make Module a Step. Adding a `Step` field to Build/Module.zig allow us to more easily track dependencies. CompileStep can now depend on main_module.step instead of all of the dependencies of main_module. --- lib/std/Build/Module.zig | 41 ++++++++++++++++++++-------------------- lib/std/Build/Step.zig | 2 ++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index b9c35e9c515e..6e184aad10a9 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -1,6 +1,7 @@ const Module = @This(); builder: *Build, +step: Step, /// This could either be a generated file, in which case the module /// contains exactly one file, or it could be a path to the root source /// file of directory of files which constitute the module. @@ -30,6 +31,11 @@ pub fn create(owner: *Build, options: CreateModuleOptions) *Module { const arena = owner.allocator; mod.* = .{ .builder = owner, + .step = Step.init(.{ + .id = .module, + .name = "module", // TODO: Better name + .owner = owner, + }), .source_file = options.source_file, .dependencies = moduleDependenciesToArrayHashMap(arena, options.dependencies), .link_objects = ArrayList(LinkObject).init(arena), @@ -50,6 +56,10 @@ pub fn create(owner: *Build, options: CreateModuleOptions) *Module { mod.addCSourceFiles(c_files.files, c_files.flags); } + for (options.dependencies) |dep| { + mod.step.dependOn(&dep.module.step); + } + return mod; } @@ -179,8 +189,7 @@ pub fn addCSourceFileSource(m: *Module, source: CSourceFile) void { const c_source_file = b.allocator.create(CSourceFile) catch @panic("OOM"); c_source_file.* = source.dupe(b); m.link_objects.append(.{ .c_source_file = c_source_file }) catch @panic("OOM"); - // source.source.addStepDependencies(&self.step); - // TODO: Add step dependencies to consumers of the module + source.source.addStepDependencies(&m.step); } const InstalledHeader = union(enum) { @@ -309,10 +318,9 @@ fn linkLibraryOrObject(m: *Module, other: *CompileStep) void { m.link_objects.append(.{ .other_step = other }) catch @panic("OOM"); m.include_dirs.append(.{ .other_step = other }) catch @panic("OOM"); - // TODO: Need to depend on installed_headers steps - // for (other.main_module.installed_headers.items) |install_step| { - // self.step.dependOn(install_step); - // } + for (other.main_module.installed_headers.items) |install_step| { + m.step.dependOn(install_step); + } } pub fn addAssemblyFile(m: *Module, path: []const u8) void { @@ -326,8 +334,7 @@ pub fn addAssemblyFileSource(m: *Module, source: FileSource) void { const b = m.builder; const source_duped = source.dupe(b); m.link_objects.append(.{ .assembly_file = source_duped }) catch @panic("OOM"); - // source_duped.addStepDependencies(&self.step); - // TODO + source_duped.addStepDependencies(&m.step); } pub fn addObjectFile(m: *Module, source_file: []const u8) void { @@ -337,8 +344,7 @@ pub fn addObjectFile(m: *Module, source_file: []const u8) void { pub fn addObjectFileSource(m: *Module, source: FileSource) void { const b = m.builder; m.link_objects.append(.{ .static_path = source.dupe(b) }) catch @panic("OOM"); - // source.addStepDependencies(&self.step); - // TODO + source.addStepDependencies(&m.step); } pub fn addObject(m: *Module, obj: *CompileStep) void { @@ -459,8 +465,7 @@ pub fn addIncludePath(m: *Module, path: []const u8) void { } pub fn addConfigHeader(m: *Module, config_header: *ConfigHeaderStep) void { - // m.step.dependOn(&config_header.step); - // TODO + m.step.dependOn(&config_header.step); m.include_dirs.append(.{ .config_header_step = config_header }) catch @panic("OOM"); } @@ -471,8 +476,7 @@ pub fn addLibraryPath(m: *Module, path: []const u8) void { pub fn addLibraryPathDirectorySource(m: *Module, directory_source: FileSource) void { m.lib_paths.append(directory_source) catch @panic("OOM"); - // directory_source.addStepDependencies(&self.step); - // TODO + directory_source.addStepDependencies(&m.step); } pub fn addRPath(m: *Module, path: []const u8) void { @@ -482,8 +486,7 @@ pub fn addRPath(m: *Module, path: []const u8) void { pub fn addRPathDirectorySource(m: *Module, directory_source: FileSource) void { m.rpaths.append(directory_source) catch @panic("OOM"); - // directory_source.addStepDependencies(&m.step); - // TODO + directory_source.addStepDependencies(&m.step); } pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { @@ -493,8 +496,7 @@ pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { pub fn addFrameworkPathDirectorySource(m: *Module, directory_source: FileSource) void { m.framework_dirs.append(directory_source) catch @panic("OOM"); - // directory_source.addStepDependencies(&self.step); - // TODO + directory_source.addStepDependencies(&m.step); } pub fn forceUndefinedSymbol(m: *Module, symbol_name: []const u8) void { @@ -505,8 +507,7 @@ pub fn forceUndefinedSymbol(m: *Module, symbol_name: []const u8) void { pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { const b = m.builder; m.linker_script = source.dupe(b); - // source.addStepDependencies(&self.step); - // TODO + source.addStepDependencies(&m.step); } const std = @import("std"); diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index 0c05a64b1c67..c5a98137e023 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -89,6 +89,7 @@ pub const Id = enum { config_header, objcopy, options, + module, custom, pub fn Type(comptime id: Id) type { @@ -108,6 +109,7 @@ pub const Id = enum { .config_header => Build.ConfigHeaderStep, .objcopy => Build.ObjCopyStep, .options => Build.OptionsStep, + .module => Build.Module, .custom => @compileError("no type available for custom step"), }; } From bcc551fea67a53c38d4729f020fec1c817053bef Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Mon, 24 Apr 2023 22:44:20 +0000 Subject: [PATCH 18/29] Make CompileStep properly depend on main_module step --- lib/std/Build/CompileStep.zig | 2 +- lib/std/Build/Module.zig | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 6a1619d4fd8b..654e2d829b49 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -371,7 +371,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { } } - if (options.main_module.source_file) |rs| rs.addStepDependencies(&self.step); + self.step.dependOn(&self.main_module.step); return self; } diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 6e184aad10a9..60a798de7e27 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -27,8 +27,8 @@ linker_script: ?FileSource = null, force_undefined_symbols: StringHashMap(void), pub fn create(owner: *Build, options: CreateModuleOptions) *Module { - const mod = owner.allocator.create(Module) catch @panic("OOM"); const arena = owner.allocator; + const mod = owner.allocator.create(Module) catch @panic("OOM"); mod.* = .{ .builder = owner, .step = Step.init(.{ @@ -52,6 +52,11 @@ pub fn create(owner: *Build, options: CreateModuleOptions) *Module { .is_linking_libcpp = false, .linker_script = null, }; + + if (options.source_file) |rs| { + rs.addStepDependencies(&mod.step); + } + if (options.c_source_files) |c_files| { mod.addCSourceFiles(c_files.files, c_files.flags); } @@ -73,6 +78,7 @@ fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDepend pub fn addOptions(m: *Module, name: []const u8, options: *OptionsStep) void { m.dependencies.put(m.builder.dupe(name), options.addModule(name)) catch @panic("OOM"); + m.step.dependOn(&options.step); } pub const LinkObject = union(enum) { From bd81db497f732e9f2c3d5b7d174806721c593b54 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Mon, 24 Apr 2023 23:52:03 +0000 Subject: [PATCH 19/29] Add Module.appendArgs --- lib/std/Build/CompileStep.zig | 42 ++++++-------- lib/std/Build/Module.zig | 101 ++++++++++++++++++++++++---------- 2 files changed, 87 insertions(+), 56 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 654e2d829b49..84322fd3eda1 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -614,11 +614,11 @@ pub fn setExecCmd(self: *CompileStep, args: []const ?[]const u8) void { self.exec_cmd_args = duped_args; } -fn appendModuleArgs( - cs: *CompileStep, +pub fn appendModuleArgs( + self: *CompileStep, zig_args: *ArrayList([]const u8), -) error{OutOfMemory}!void { - const b = cs.step.owner; +) error{ OutOfMemory, MakeFailed }!void { + const b = self.step.owner; // First, traverse the whole dependency graph and give every module a unique name, ideally one // named after what it's called somewhere in the graph. It will help here to have both a mapping // from module to name and a set of all the currently-used names. @@ -631,12 +631,12 @@ fn appendModuleArgs( }).init(b.allocator); // Make sure that the main module name does not have colons in it, since the CLI forbids it. // We handle this for transitive dependencies further down. - if (std.mem.indexOfScalar(u8, cs.name, ':') != null) { + if (std.mem.indexOfScalar(u8, self.name, ':') != null) { @panic("Module names cannot contain colons"); } try to_name.append(.{ - .name = cs.name, - .mod = cs.main_module, + .name = self.name, + .mod = self.main_module, }); while (to_name.popOrNull()) |dep| { @@ -670,6 +670,9 @@ fn appendModuleArgs( } } + try zig_args.append("--main-mod"); + try zig_args.append(self.name); + // Since the module names given to the CLI are based off of the exposed names, we already know // that none of the CLI names have colons in them, so there's no need to check that explicitly. @@ -680,25 +683,21 @@ fn appendModuleArgs( const mod = kv.key_ptr.*; const name = kv.value_ptr.*; + try mod.appendArgs(name, zig_args); + const deps_str = try constructDepString(b.allocator, mod_names, mod.dependencies); - // TODO: source_file may not always exits, for example if a module consists only of C source files. - // How do we represent this case in the CLI - const src = mod.builder.pathFromRoot(mod.source_file.?.getPath(mod.builder)); - try zig_args.append("--mod"); - try zig_args.append(try std.fmt.allocPrint(b.allocator, "{s}:{s}:{s}", .{ name, deps_str, src })); + if (deps_str.len > 0) { + try zig_args.append(try std.fmt.allocPrint(b.allocator, "--deps {s}", .{deps_str})); + } } } - - // Lastly, output the main module dependency - try zig_args.append("--deps"); - try zig_args.append(cs.name); } fn constructDepString( allocator: std.mem.Allocator, mod_names: std.AutoHashMap(*Module, []const u8), deps: std.StringArrayHashMap(*Module), -) ![]const u8 { +) error{OutOfMemory}![]const u8 { var deps_str = std.ArrayList(u8).init(allocator); var it = deps.iterator(); while (it.next()) |kv| { @@ -721,10 +720,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { const b = step.owner; const self = @fieldParentPtr(CompileStep, "step", step); - if (self.main_module.source_file == null and self.main_module.link_objects.items.len == 0) { - return step.fail("the linker needs one or more objects to link", .{}); - } - var zig_args = ArrayList([]const u8).init(b.allocator); defer zig_args.deinit(); @@ -767,8 +762,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append(try std.fmt.allocPrint(b.allocator, "{}", .{stack_size})); } - if (self.main_module.source_file) |root_src| try zig_args.append(root_src.getPath(b)); - // We will add link objects from transitive dependencies, but we want to keep // all link objects in the same order provided. // This array is used to keep self.link_objects immutable. @@ -1026,9 +1019,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append("--global-cache-dir"); try zig_args.append(b.global_cache_root.path orelse "."); - try zig_args.append("--name"); - try zig_args.append(self.name); - if (self.linkage) |some| switch (some) { .dynamic => try zig_args.append("-dynamic"), .static => try zig_args.append("-static"), diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 60a798de7e27..eab9ae368b0e 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -1,6 +1,5 @@ const Module = @This(); -builder: *Build, step: Step, /// This could either be a generated file, in which case the module /// contains exactly one file, or it could be a path to the root source @@ -30,7 +29,6 @@ pub fn create(owner: *Build, options: CreateModuleOptions) *Module { const arena = owner.allocator; const mod = owner.allocator.create(Module) catch @panic("OOM"); mod.* = .{ - .builder = owner, .step = Step.init(.{ .id = .module, .name = "module", // TODO: Better name @@ -68,6 +66,49 @@ pub fn create(owner: *Build, options: CreateModuleOptions) *Module { return mod; } +pub fn appendArgs( + m: *Module, + name: []const u8, + args: *ArrayList([]const u8), +) error{ OutOfMemory, MakeFailed }!void { + try args.append("--mod"); + try args.append(name); + + var num_source_files: usize = 0; + + if (m.source_file) |rs| { + try args.append(rs.getPath2(m.step.owner, &m.step)); + num_source_files += 1; + } + + for (m.link_objects.items) |obj| { + switch (obj) { + .c_source_file => |c| { + try args.append(c.source.getPath2(m.step.owner, &m.step)); + num_source_files +|= 1; + }, + .c_source_files => |c_files| { + for (c_files.files) |file_path| { + try args.append(file_path); + num_source_files +|= 1; + } + }, + // TODO: Should ASM files be included here? + .assembly_file => |asm_file| { + try args.append(asm_file.getPath2(m.step.owner, &m.step)); + num_source_files +|= 1; + }, + else => {}, + } + } + + if (num_source_files == 0) { + return m.step.fail("Module '{s}' must have at least one source file", .{name}); + } + + // TODO: Module specific args +} + fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { var result = std.StringArrayHashMap(*Module).init(arena); for (deps) |dep| { @@ -77,7 +118,7 @@ fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDepend } pub fn addOptions(m: *Module, name: []const u8, options: *OptionsStep) void { - m.dependencies.put(m.builder.dupe(name), options.addModule(name)) catch @panic("OOM"); + m.dependencies.put(m.step.owner.dupe(name), options.addModule(name)) catch @panic("OOM"); m.step.dependOn(&options.step); } @@ -101,7 +142,7 @@ pub fn linkLibCpp(m: *Module) void { /// This one has no integration with anything, it just puts -lname on the command line. /// Prefer to use `linkSystemLibrary` instead. pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.link_objects.append(.{ .system_lib = .{ .name = b.dupe(name), @@ -115,7 +156,7 @@ pub fn linkSystemLibraryName(m: *Module, name: []const u8) void { /// This one has no integration with anything, it just puts -needed-lname on the command line. /// Prefer to use `linkSystemLibraryNeeded` instead. pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.link_objects.append(.{ .system_lib = .{ .name = b.dupe(name), @@ -129,7 +170,7 @@ pub fn linkSystemLibraryNeededName(m: *Module, name: []const u8) void { /// Darwin-only. This one has no integration with anything, it just puts -weak-lname on the /// command line. Prefer to use `linkSystemLibraryWeak` instead. pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.link_objects.append(.{ .system_lib = .{ .name = b.dupe(name), @@ -143,7 +184,7 @@ pub fn linkSystemLibraryWeakName(m: *Module, name: []const u8) void { /// This links against a system library, exclusively using pkg-config to find the library. /// Prefer to use `linkSystemLibrary` instead. pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.link_objects.append(.{ .system_lib = .{ .name = b.dupe(lib_name), @@ -157,7 +198,7 @@ pub fn linkSystemLibraryPkgConfigOnly(m: *Module, lib_name: []const u8) void { /// This links against a system library, exclusively using pkg-config to find the library. /// Prefer to use `linkSystemLibraryNeeded` instead. pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.link_objects.append(.{ .system_lib = .{ .name = b.dupe(lib_name), @@ -170,7 +211,7 @@ pub fn linkSystemLibraryNeededPkgConfigOnly(m: *Module, lib_name: []const u8) vo /// Handy when you have many C/C++ source files and want them all to have the same flags. pub fn addCSourceFiles(m: *Module, files: []const []const u8, flags: []const []const u8) void { - const b = m.builder; + const b = m.step.owner; const c_source_files = b.allocator.create(CSourceFiles) catch @panic("OOM"); const files_copy = b.dupeStrings(files); @@ -191,7 +232,7 @@ pub fn addCSourceFile(m: *Module, file: []const u8, flags: []const []const u8) v } pub fn addCSourceFileSource(m: *Module, source: CSourceFile) void { - const b = m.builder; + const b = m.step.owner; const c_source_file = b.allocator.create(CSourceFile) catch @panic("OOM"); c_source_file.* = source.dupe(b); m.link_objects.append(.{ .c_source_file = c_source_file }) catch @panic("OOM"); @@ -212,7 +253,7 @@ const InstalledHeader = union(enum) { }; pub fn installHeader(m: *Module, src_path: []const u8, dest_rel_path: []const u8) void { - const b = m.builder; + const b = m.step.owner; const install_file = b.addInstallHeaderFile(src_path, dest_rel_path); b.getInstallStep().dependOn(&install_file.step); m.installed_headers.append(&install_file.step) catch @panic("OOM"); @@ -229,7 +270,7 @@ pub fn installConfigHeader( options: InstallConfigHeaderOptions, ) void { const dest_rel_path = options.dest_rel_path orelse config_header.include_path; - const b = m.builder; + const b = m.step.owner; const install_file = b.addInstallFileWithDir( .{ .generated = &config_header.output_file }, options.install_dir, @@ -256,7 +297,7 @@ pub fn installHeadersDirectoryOptions( m: *Module, options: std.Build.InstallDirStep.Options, ) void { - const b = m.builder; + const b = m.step.owner; const install_dir = b.addInstallDirectory(options); b.getInstallStep().dependOn(&install_dir.step); m.installed_headers.append(&install_dir.step) catch @panic("OOM"); @@ -264,7 +305,7 @@ pub fn installHeadersDirectoryOptions( pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { assert(l.kind == .lib); - const b = m.builder; + const b = m.step.owner; const install_step = b.getInstallStep(); // Copy each element from installed_headers, modifying the builder // to be the new parent's builder. @@ -288,14 +329,14 @@ pub fn installLibraryHeaders(m: *Module, l: *CompileStep) void { /// If the value is omitted, it is set to 1. /// `name` and `value` need not live longer than the function call. pub fn defineCMacro(m: *Module, name: []const u8, value: ?[]const u8) void { - const b = m.builder; + const b = m.step.owner; const macro = std.Build.constructCMacro(b.allocator, name, value); m.c_macros.append(macro) catch @panic("OOM"); } /// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1. pub fn defineCMacroRaw(m: *Module, name_and_value: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.c_macros.append(b.dupe(name_and_value)) catch @panic("OOM"); } @@ -330,14 +371,14 @@ fn linkLibraryOrObject(m: *Module, other: *CompileStep) void { } pub fn addAssemblyFile(m: *Module, path: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.link_objects.append(.{ .assembly_file = .{ .path = b.dupe(path) }, }) catch @panic("OOM"); } pub fn addAssemblyFileSource(m: *Module, source: FileSource) void { - const b = m.builder; + const b = m.step.owner; const source_duped = source.dupe(b); m.link_objects.append(.{ .assembly_file = source_duped }) catch @panic("OOM"); source_duped.addStepDependencies(&m.step); @@ -348,7 +389,7 @@ pub fn addObjectFile(m: *Module, source_file: []const u8) void { } pub fn addObjectFileSource(m: *Module, source: FileSource) void { - const b = m.builder; + const b = m.step.owner; m.link_objects.append(.{ .static_path = source.dupe(b) }) catch @panic("OOM"); source.addStepDependencies(&m.step); } @@ -374,7 +415,7 @@ fn linkSystemLibraryInner(m: *Module, name: []const u8, opts: struct { needed: bool = false, weak: bool = false, }) void { - const b = m.builder; + const b = m.step.owner; if (isLibCLibrary(name)) { m.linkLibC(); return; @@ -435,19 +476,19 @@ pub const FrameworkLinkInfo = struct { }; pub fn linkFramework(m: *Module, framework_name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.frameworks.put(b.dupe(framework_name), .{}) catch @panic("OOM"); } pub fn linkFrameworkNeeded(m: *Module, framework_name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.frameworks.put(b.dupe(framework_name), .{ .needed = true, }) catch @panic("OOM"); } pub fn linkFrameworkWeak(m: *Module, framework_name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.frameworks.put(b.dupe(framework_name), .{ .weak = true, }) catch @panic("OOM"); @@ -461,12 +502,12 @@ pub const IncludeDir = union(enum) { }; pub fn addSystemIncludePath(m: *Module, path: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.include_dirs.append(IncludeDir{ .raw_path_system = b.dupe(path) }) catch @panic("OOM"); } pub fn addIncludePath(m: *Module, path: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.include_dirs.append(IncludeDir{ .raw_path = b.dupe(path) }) catch @panic("OOM"); } @@ -476,7 +517,7 @@ pub fn addConfigHeader(m: *Module, config_header: *ConfigHeaderStep) void { } pub fn addLibraryPath(m: *Module, path: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.lib_paths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); } @@ -486,7 +527,7 @@ pub fn addLibraryPathDirectorySource(m: *Module, directory_source: FileSource) v } pub fn addRPath(m: *Module, path: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.rpaths.append(.{ .path = b.dupe(path) }) catch @panic("OOM"); } @@ -496,7 +537,7 @@ pub fn addRPathDirectorySource(m: *Module, directory_source: FileSource) void { } pub fn addFrameworkPath(m: *Module, dir_path: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.framework_dirs.append(.{ .path = b.dupe(dir_path) }) catch @panic("OOM"); } @@ -506,12 +547,12 @@ pub fn addFrameworkPathDirectorySource(m: *Module, directory_source: FileSource) } pub fn forceUndefinedSymbol(m: *Module, symbol_name: []const u8) void { - const b = m.builder; + const b = m.step.owner; m.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM"); } pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { - const b = m.builder; + const b = m.step.owner; m.linker_script = source.dupe(b); source.addStepDependencies(&m.step); } From 66cc806b43399b8395994d2e70a01e337725d38e Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Tue, 25 Apr 2023 03:15:29 +0000 Subject: [PATCH 20/29] Move transitive dep traversal to Module.zig --- lib/std/Build/CompileStep.zig | 204 +----------------------------- lib/std/Build/Module.zig | 230 ++++++++++++++++++++++++++++++++-- 2 files changed, 224 insertions(+), 210 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 84322fd3eda1..415e427b7fb1 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -418,7 +418,7 @@ pub fn producesPdbFile(self: *CompileStep) bool { /// Run pkg-config for the given library name and parse the output, returning the arguments /// that should be passed to zig to link the given library. -fn runPkgConfig(self: *CompileStep, lib_name: []const u8) ![]const []const u8 { +pub fn runPkgConfig(self: *CompileStep, lib_name: []const u8) ![]const []const u8 { const b = self.step.owner; const pkg_name = match: { // First we have to map the library name to pkg config name. Unfortunately, @@ -617,7 +617,7 @@ pub fn setExecCmd(self: *CompileStep, args: []const ?[]const u8) void { pub fn appendModuleArgs( self: *CompileStep, zig_args: *ArrayList([]const u8), -) error{ OutOfMemory, MakeFailed }!void { +) !void { const b = self.step.owner; // First, traverse the whole dependency graph and give every module a unique name, ideally one // named after what it's called somewhere in the graph. It will help here to have both a mapping @@ -683,7 +683,7 @@ pub fn appendModuleArgs( const mod = kv.key_ptr.*; const name = kv.value_ptr.*; - try mod.appendArgs(name, zig_args); + try mod.appendArgs(self, name, zig_args); const deps_str = try constructDepString(b.allocator, mod_names, mod.dependencies); if (deps_str.len > 0) { @@ -762,144 +762,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append(try std.fmt.allocPrint(b.allocator, "{}", .{stack_size})); } - // We will add link objects from transitive dependencies, but we want to keep - // all link objects in the same order provided. - // This array is used to keep self.link_objects immutable. - var transitive_deps: TransitiveDeps = .{ - .link_objects = ArrayList(LinkObject).init(b.allocator), - .seen_system_libs = StringHashMap(void).init(b.allocator), - .seen_steps = std.AutoHashMap(*const Step, void).init(b.allocator), - .is_linking_libcpp = self.main_module.is_linking_libcpp, - .is_linking_libc = self.main_module.is_linking_libc, - .frameworks = &self.main_module.frameworks, - }; - - try transitive_deps.seen_steps.put(&self.step, {}); - try transitive_deps.add(self.main_module.link_objects.items); - - var prev_has_extra_flags = false; - - for (transitive_deps.link_objects.items) |link_object| { - switch (link_object) { - .static_path => |static_path| try zig_args.append(static_path.getPath(b)), - - .other_step => |other| switch (other.kind) { - .exe => @panic("Cannot link with an executable build artifact"), - .@"test" => @panic("Cannot link with a test"), - .obj => { - try zig_args.append(other.getOutputSource().getPath(b)); - }, - .lib => l: { - if (self.isStaticLibrary() and other.isStaticLibrary()) { - // Avoid putting a static library inside a static library. - break :l; - } - - const full_path_lib = other.getOutputLibSource().getPath(b); - try zig_args.append(full_path_lib); - - if (other.linkage == Linkage.dynamic and !self.target.isWindows()) { - if (fs.path.dirname(full_path_lib)) |dirname| { - try zig_args.append("-rpath"); - try zig_args.append(dirname); - } - } - }, - }, - - .system_lib => |system_lib| { - const prefix: []const u8 = prefix: { - if (system_lib.needed) break :prefix "-needed-l"; - if (system_lib.weak) break :prefix "-weak-l"; - break :prefix "-l"; - }; - switch (system_lib.use_pkg_config) { - .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })), - .yes, .force => { - if (self.runPkgConfig(system_lib.name)) |args| { - try zig_args.appendSlice(args); - } else |err| switch (err) { - error.PkgConfigInvalidOutput, - error.PkgConfigCrashed, - error.PkgConfigFailed, - error.PkgConfigNotInstalled, - error.PackageNotFound, - => switch (system_lib.use_pkg_config) { - .yes => { - // pkg-config failed, so fall back to linking the library - // by name directly. - try zig_args.append(b.fmt("{s}{s}", .{ - prefix, - system_lib.name, - })); - }, - .force => { - panic("pkg-config failed for library {s}", .{system_lib.name}); - }, - .no => unreachable, - }, - - else => |e| return e, - } - }, - } - }, - - .assembly_file => |asm_file| { - if (prev_has_extra_flags) { - try zig_args.append("-extra-cflags"); - try zig_args.append("--"); - prev_has_extra_flags = false; - } - try zig_args.append(asm_file.getPath(b)); - }, - - .c_source_file => |c_source_file| { - if (c_source_file.args.len == 0) { - if (prev_has_extra_flags) { - try zig_args.append("-cflags"); - try zig_args.append("--"); - prev_has_extra_flags = false; - } - } else { - try zig_args.append("-cflags"); - for (c_source_file.args) |arg| { - try zig_args.append(arg); - } - try zig_args.append("--"); - } - try zig_args.append(c_source_file.source.getPath(b)); - }, - - .c_source_files => |c_source_files| { - if (c_source_files.flags.len == 0) { - if (prev_has_extra_flags) { - try zig_args.append("-cflags"); - try zig_args.append("--"); - prev_has_extra_flags = false; - } - } else { - try zig_args.append("-cflags"); - for (c_source_files.flags) |flag| { - try zig_args.append(flag); - } - try zig_args.append("--"); - } - for (c_source_files.files) |file| { - try zig_args.append(b.pathFromRoot(file)); - } - }, - } - } - - if (transitive_deps.is_linking_libcpp) { - try zig_args.append("-lc++"); - } - - if (transitive_deps.is_linking_libc) { - try zig_args.append("-lc"); - } - if (self.image_base) |image_base| { try zig_args.append("--image-base"); try zig_args.append(b.fmt("0x{x}", .{image_base})); @@ -1545,66 +1407,6 @@ fn addFlag(args: *ArrayList([]const u8), comptime name: []const u8, opt: ?bool) } } -const TransitiveDeps = struct { - link_objects: ArrayList(LinkObject), - seen_system_libs: StringHashMap(void), - seen_steps: std.AutoHashMap(*const Step, void), - is_linking_libcpp: bool, - is_linking_libc: bool, - frameworks: *StringHashMap(FrameworkLinkInfo), - - fn add(td: *TransitiveDeps, link_objects: []const LinkObject) !void { - try td.link_objects.ensureUnusedCapacity(link_objects.len); - - for (link_objects) |link_object| { - try td.link_objects.append(link_object); - switch (link_object) { - .other_step => |other| try addInner(td, other, other.isDynamicLibrary()), - else => {}, - } - } - } - - fn addInner(td: *TransitiveDeps, other: *CompileStep, dyn: bool) !void { - // Inherit dependency on libc and libc++ - td.is_linking_libcpp = td.is_linking_libcpp or other.main_module.is_linking_libcpp; - td.is_linking_libc = td.is_linking_libc or other.main_module.is_linking_libc; - - // Inherit dependencies on darwin frameworks - if (!dyn) { - var it = other.main_module.frameworks.iterator(); - while (it.next()) |framework| { - try td.frameworks.put(framework.key_ptr.*, framework.value_ptr.*); - } - } - - // Inherit dependencies on system libraries and static libraries. - for (other.main_module.link_objects.items) |other_link_object| { - switch (other_link_object) { - .system_lib => |system_lib| { - if ((try td.seen_system_libs.fetchPut(system_lib.name, {})) != null) - continue; - - if (dyn) - continue; - - try td.link_objects.append(other_link_object); - }, - .other_step => |inner_other| { - if ((try td.seen_steps.fetchPut(&inner_other.step, {})) != null) - continue; - - if (!dyn) - try td.link_objects.append(other_link_object); - - try addInner(td, inner_other, dyn or inner_other.isDynamicLibrary()); - }, - else => continue, - } - } - } -}; - fn checkCompileErrors(self: *CompileStep) !void { // Clear this field so that it does not get printed by the build runner. const actual_eb = self.step.result_error_bundle; diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index eab9ae368b0e..c0abe39691c6 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -68,34 +68,35 @@ pub fn create(owner: *Build, options: CreateModuleOptions) *Module { pub fn appendArgs( m: *Module, + compile_step: *CompileStep, name: []const u8, - args: *ArrayList([]const u8), -) error{ OutOfMemory, MakeFailed }!void { - try args.append("--mod"); - try args.append(name); + zig_args: *ArrayList([]const u8), +) !void { + try zig_args.append("--mod"); + try zig_args.append(name); var num_source_files: usize = 0; if (m.source_file) |rs| { - try args.append(rs.getPath2(m.step.owner, &m.step)); + try zig_args.append(rs.getPath2(m.step.owner, &m.step)); num_source_files += 1; } for (m.link_objects.items) |obj| { switch (obj) { .c_source_file => |c| { - try args.append(c.source.getPath2(m.step.owner, &m.step)); + try zig_args.append(c.source.getPath2(m.step.owner, &m.step)); num_source_files +|= 1; }, .c_source_files => |c_files| { for (c_files.files) |file_path| { - try args.append(file_path); + try zig_args.append(file_path); num_source_files +|= 1; } }, // TODO: Should ASM files be included here? .assembly_file => |asm_file| { - try args.append(asm_file.getPath2(m.step.owner, &m.step)); + try zig_args.append(asm_file.getPath2(m.step.owner, &m.step)); num_source_files +|= 1; }, else => {}, @@ -106,7 +107,153 @@ pub fn appendArgs( return m.step.fail("Module '{s}' must have at least one source file", .{name}); } - // TODO: Module specific args + try zig_args.append("--args"); + + // We will add link objects from transitive dependencies, but we want to keep + // all link objects in the same order provided. + // This array is used to keep m.link_objects immutable. + const b = m.step.owner; + var transitive_deps: TransitiveDeps = .{ + .link_objects = ArrayList(LinkObject).init(b.allocator), + .seen_system_libs = StringHashMap(void).init(b.allocator), + .seen_steps = std.AutoHashMap(*const Step, void).init(b.allocator), + .is_linking_libcpp = m.is_linking_libcpp, + .is_linking_libc = m.is_linking_libc, + .frameworks = &m.frameworks, + }; + + try transitive_deps.seen_steps.put(&m.step, {}); + try transitive_deps.add(m); + + { + var it = m.dependencies.iterator(); + while (it.next()) |kv| { + try transitive_deps.add(kv.value_ptr.*); + } + } + + var prev_has_extra_flags = false; + + for (transitive_deps.link_objects.items) |link_object| { + switch (link_object) { + .static_path => |static_path| try zig_args.append(static_path.getPath(b)), + + .other_step => |other| switch (other.kind) { + .exe => @panic("Cannot link with an executable build artifact"), + .@"test" => @panic("Cannot link with a test"), + .obj => { + try zig_args.append(other.getOutputSource().getPath(b)); + }, + .lib => l: { + if (compile_step.isStaticLibrary() and other.isStaticLibrary()) { + // Avoid putting a static library inside a static library. + break :l; + } + + const full_path_lib = other.getOutputLibSource().getPath(b); + try zig_args.append(full_path_lib); + + if (other.linkage == Linkage.dynamic and !compile_step.target.isWindows()) { + if (fs.path.dirname(full_path_lib)) |dirname| { + try zig_args.append("-rpath"); + try zig_args.append(dirname); + } + } + }, + }, + + .system_lib => |system_lib| { + const prefix: []const u8 = prefix: { + if (system_lib.needed) break :prefix "-needed-l"; + if (system_lib.weak) break :prefix "-weak-l"; + break :prefix "-l"; + }; + switch (system_lib.use_pkg_config) { + .no => try zig_args.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })), + .yes, .force => { + if (compile_step.runPkgConfig(system_lib.name)) |args| { + try zig_args.appendSlice(args); + } else |err| switch (err) { + error.PkgConfigInvalidOutput, + error.PkgConfigCrashed, + error.PkgConfigFailed, + error.PkgConfigNotInstalled, + error.PackageNotFound, + => switch (system_lib.use_pkg_config) { + .yes => { + // pkg-config failed, so fall back to linking the library + // by name directly. + try zig_args.append(b.fmt("{s}{s}", .{ + prefix, + system_lib.name, + })); + }, + .force => { + panic("pkg-config failed for library {s}", .{system_lib.name}); + }, + .no => unreachable, + }, + + else => |e| return e, + } + }, + } + }, + + .assembly_file => |asm_file| { + if (prev_has_extra_flags) { + try zig_args.append("-extra-cflags"); + try zig_args.append("--"); + prev_has_extra_flags = false; + } + try zig_args.append(asm_file.getPath(b)); + }, + + .c_source_file => |c_source_file| { + if (c_source_file.args.len == 0) { + if (prev_has_extra_flags) { + try zig_args.append("-cflags"); + try zig_args.append("--"); + prev_has_extra_flags = false; + } + } else { + try zig_args.append("-cflags"); + for (c_source_file.args) |arg| { + try zig_args.append(arg); + } + try zig_args.append("--"); + } + try zig_args.append(c_source_file.source.getPath(b)); + }, + + .c_source_files => |c_source_files| { + if (c_source_files.flags.len == 0) { + if (prev_has_extra_flags) { + try zig_args.append("-cflags"); + try zig_args.append("--"); + prev_has_extra_flags = false; + } + } else { + try zig_args.append("-cflags"); + for (c_source_files.flags) |flag| { + try zig_args.append(flag); + } + try zig_args.append("--"); + } + for (c_source_files.files) |file| { + try zig_args.append(b.pathFromRoot(file)); + } + }, + } + } + + if (transitive_deps.is_linking_libcpp) { + try zig_args.append("-lc++"); + } + + if (transitive_deps.is_linking_libc) { + try zig_args.append("-lc"); + } } fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { @@ -557,6 +704,69 @@ pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { source.addStepDependencies(&m.step); } +const TransitiveDeps = struct { + link_objects: ArrayList(LinkObject), + seen_system_libs: StringHashMap(void), + seen_steps: std.AutoHashMap(*const Step, void), + is_linking_libcpp: bool, + is_linking_libc: bool, + frameworks: *StringHashMap(FrameworkLinkInfo), + + fn add(td: *TransitiveDeps, module: *Module) !void { + td.is_linking_libcpp = td.is_linking_libcpp or module.is_linking_libcpp; + td.is_linking_libc = td.is_linking_libc or module.is_linking_libc; + + try td.link_objects.ensureUnusedCapacity(module.link_objects.items.len); + + for (module.link_objects.items) |link_object| { + try td.link_objects.append(link_object); + switch (link_object) { + .other_step => |other| try addInner(td, other.main_module, other.isDynamicLibrary()), + else => {}, + } + } + } + + fn addInner(td: *TransitiveDeps, other: *Module, dyn: bool) !void { + // Inherit dependency on libc and libc++ + td.is_linking_libcpp = td.is_linking_libcpp or other.is_linking_libcpp; + td.is_linking_libc = td.is_linking_libc or other.is_linking_libc; + + // Inherit dependencies on darwin frameworks + if (!dyn) { + var it = other.frameworks.iterator(); + while (it.next()) |framework| { + try td.frameworks.put(framework.key_ptr.*, framework.value_ptr.*); + } + } + + // Inherit dependencies on system libraries and static libraries. + for (other.link_objects.items) |other_link_object| { + switch (other_link_object) { + .system_lib => |system_lib| { + if ((try td.seen_system_libs.fetchPut(system_lib.name, {})) != null) + continue; + + if (dyn) + continue; + + try td.link_objects.append(other_link_object); + }, + .other_step => |inner_other| { + if ((try td.seen_steps.fetchPut(&inner_other.step, {})) != null) + continue; + + if (!dyn) + try td.link_objects.append(other_link_object); + + try addInner(td, inner_other.main_module, dyn or inner_other.isDynamicLibrary()); + }, + else => continue, + } + } + } +}; + const std = @import("std"); const Build = std.Build; const Step = Build.Step; @@ -570,6 +780,7 @@ const CSourceFile = Build.CSourceFile; const CSourceFiles = Build.CSourceFiles; const ModuleDependency = Build.ModuleDependency; const CreateModuleOptions = Build.CreateModuleOptions; +const Linkage = CompileStep.Linkage; const ArrayList = std.ArrayList; const StringHashMap = std.StringHashMap; @@ -579,6 +790,7 @@ const Allocator = std.mem.Allocator; const assert = std.debug.assert; const mem = std.mem; const fs = std.fs; +const panic = std.debug.panic; pub const addSystemIncludeDir = @compileError("deprecated; use addSystemIncludePath"); pub const addIncludeDir = @compileError("deprecated; use addIncludePath"); From 383885a69d695693e80a9831b4ee4a4cec93b775 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Tue, 25 Apr 2023 23:29:21 +0000 Subject: [PATCH 21/29] Add recursive module include dirs --- lib/std/Build.zig | 8 +-- lib/std/Build/CompileStep.zig | 50 ------------------ lib/std/Build/Module.zig | 99 +++++++++++++++++++++++++++++------ 3 files changed, 87 insertions(+), 70 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index dc0799787044..d4b1aae41dfb 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -462,7 +462,7 @@ pub const ExecutableOptions = struct { }; pub fn addExecutable(b: *Build, options: ExecutableOptions) *CompileStep { - const main_module = b.createModule(options.main_module_options); + const main_module = Module.create(b, options.name, options.main_module_options); return CompileStep.create(b, .{ .name = options.name, .main_module = main_module, @@ -520,7 +520,7 @@ pub const SharedLibraryOptions = struct { }; pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *CompileStep { - const main_module = b.createModule(options.main_module_options); + const main_module = Module.create(b, options.name, options.main_module_options); return CompileStep.create(b, .{ .name = options.name, .main_module = main_module, @@ -625,7 +625,7 @@ pub fn addAssembly(b: *Build, options: AssemblyOptions) *CompileStep { /// it available to other packages which depend on this one. /// `createModule` can be used instead to create a private module. pub fn addModule(b: *Build, name: []const u8, options: CreateModuleOptions) *Module { - const module = b.createModule(options); + const module = Module.create(b, name, options); b.modules.put(b.dupe(name), module) catch @panic("OOM"); return module; } @@ -662,7 +662,7 @@ pub const CreateModuleOptions = struct { /// but not exposed to other packages depending on this one. /// `addModule` can be used instead to create a public module. pub fn createModule(b: *Build, options: CreateModuleOptions) *Module { - return Module.create(b, options); + return Module.create(b, null, options); } /// Initializes a RunStep with argv, which must at least have the path to the diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 415e427b7fb1..d0afb506b410 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -1022,56 +1022,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try self.appendModuleArgs(&zig_args); - for (self.main_module.include_dirs.items) |include_dir| { - switch (include_dir) { - .raw_path => |include_path| { - try zig_args.append("-I"); - try zig_args.append(b.pathFromRoot(include_path)); - }, - .raw_path_system => |include_path| { - if (b.sysroot != null) { - try zig_args.append("-iwithsysroot"); - } else { - try zig_args.append("-isystem"); - } - - const resolved_include_path = b.pathFromRoot(include_path); - - const common_include_path = if (builtin.os.tag == .windows and b.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: { - // We need to check for disk designator and strip it out from dir path so - // that zig/clang can concat resolved_include_path with sysroot. - const disk_designator = fs.path.diskDesignatorWindows(resolved_include_path); - - if (mem.indexOf(u8, resolved_include_path, disk_designator)) |where| { - break :blk resolved_include_path[where + disk_designator.len ..]; - } - - break :blk resolved_include_path; - } else resolved_include_path; - - try zig_args.append(common_include_path); - }, - .other_step => |other| { - if (other.emit_h) { - const h_path = other.getOutputHSource().getPath(b); - try zig_args.append("-isystem"); - try zig_args.append(fs.path.dirname(h_path).?); - } - if (other.main_module.installed_headers.items.len > 0) { - try zig_args.append("-I"); - try zig_args.append(b.pathJoin(&.{ - other.step.owner.install_prefix, "include", - })); - } - }, - .config_header_step => |config_header| { - const full_file_path = config_header.output_file.path.?; - const header_dir_path = full_file_path[0 .. full_file_path.len - config_header.include_path.len]; - try zig_args.appendSlice(&.{ "-I", header_dir_path }); - }, - } - } - for (self.main_module.c_macros.items) |c_macro| { try zig_args.append("-D"); try zig_args.append(c_macro); diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index c0abe39691c6..6f11537abe00 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -25,13 +25,16 @@ linker_script: ?FileSource = null, /// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. force_undefined_symbols: StringHashMap(void), -pub fn create(owner: *Build, options: CreateModuleOptions) *Module { +pub fn create(owner: *Build, name: ?[]const u8, options: CreateModuleOptions) *Module { const arena = owner.allocator; const mod = owner.allocator.create(Module) catch @panic("OOM"); mod.* = .{ .step = Step.init(.{ .id = .module, - .name = "module", // TODO: Better name + .name = if (name) |n| + std.fmt.allocPrint(arena, "module {s}", .{n}) catch @panic("OOM") + else + "module", .owner = owner, }), .source_file = options.source_file, @@ -84,19 +87,19 @@ pub fn appendArgs( for (m.link_objects.items) |obj| { switch (obj) { - .c_source_file => |c| { - try zig_args.append(c.source.getPath2(m.step.owner, &m.step)); + .c_source_file => |_| { + // try zig_args.append(c.source.getPath2(m.step.owner, &m.step)); num_source_files +|= 1; }, .c_source_files => |c_files| { - for (c_files.files) |file_path| { - try zig_args.append(file_path); + for (c_files.files) |_| { + // try zig_args.append(file_path); num_source_files +|= 1; } }, // TODO: Should ASM files be included here? - .assembly_file => |asm_file| { - try zig_args.append(asm_file.getPath2(m.step.owner, &m.step)); + .assembly_file => |_| { + // try zig_args.append(asm_file.getPath2(m.step.owner, &m.step)); num_source_files +|= 1; }, else => {}, @@ -115,6 +118,7 @@ pub fn appendArgs( const b = m.step.owner; var transitive_deps: TransitiveDeps = .{ .link_objects = ArrayList(LinkObject).init(b.allocator), + .include_dirs = ArrayList(IncludeDir).init(b.allocator), .seen_system_libs = StringHashMap(void).init(b.allocator), .seen_steps = std.AutoHashMap(*const Step, void).init(b.allocator), .is_linking_libcpp = m.is_linking_libcpp, @@ -125,13 +129,6 @@ pub fn appendArgs( try transitive_deps.seen_steps.put(&m.step, {}); try transitive_deps.add(m); - { - var it = m.dependencies.iterator(); - while (it.next()) |kv| { - try transitive_deps.add(kv.value_ptr.*); - } - } - var prev_has_extra_flags = false; for (transitive_deps.link_objects.items) |link_object| { @@ -254,6 +251,56 @@ pub fn appendArgs( if (transitive_deps.is_linking_libc) { try zig_args.append("-lc"); } + + for (transitive_deps.include_dirs.items) |include_dir| { + switch (include_dir) { + .raw_path => |include_path| { + try zig_args.append("-I"); + try zig_args.append(b.pathFromRoot(include_path)); + }, + .raw_path_system => |include_path| { + if (b.sysroot != null) { + try zig_args.append("-iwithsysroot"); + } else { + try zig_args.append("-isystem"); + } + + const resolved_include_path = b.pathFromRoot(include_path); + + const common_include_path = if (builtin.os.tag == .windows and b.sysroot != null and fs.path.isAbsolute(resolved_include_path)) blk: { + // We need to check for disk designator and strip it out from dir path so + // that zig/clang can concat resolved_include_path with sysroot. + const disk_designator = fs.path.diskDesignatorWindows(resolved_include_path); + + if (mem.indexOf(u8, resolved_include_path, disk_designator)) |where| { + break :blk resolved_include_path[where + disk_designator.len ..]; + } + + break :blk resolved_include_path; + } else resolved_include_path; + + try zig_args.append(common_include_path); + }, + .other_step => |other| { + if (other.emit_h) { + const h_path = other.getOutputHSource().getPath(b); + try zig_args.append("-isystem"); + try zig_args.append(fs.path.dirname(h_path).?); + } + if (other.main_module.installed_headers.items.len > 0) { + try zig_args.append("-I"); + try zig_args.append(b.pathJoin(&.{ + other.step.owner.install_prefix, "include", + })); + } + }, + .config_header_step => |config_header| { + const full_file_path = config_header.output_file.path.?; + const header_dir_path = full_file_path[0 .. full_file_path.len - config_header.include_path.len]; + try zig_args.appendSlice(&.{ "-I", header_dir_path }); + }, + } + } } fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { @@ -706,6 +753,7 @@ pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { const TransitiveDeps = struct { link_objects: ArrayList(LinkObject), + include_dirs: ArrayList(IncludeDir), seen_system_libs: StringHashMap(void), seen_steps: std.AutoHashMap(*const Step, void), is_linking_libcpp: bool, @@ -721,10 +769,21 @@ const TransitiveDeps = struct { for (module.link_objects.items) |link_object| { try td.link_objects.append(link_object); switch (link_object) { - .other_step => |other| try addInner(td, other.main_module, other.isDynamicLibrary()), + .other_step => |other| try td.addInner(other.main_module, other.isDynamicLibrary()), else => {}, } } + + for (module.include_dirs.items) |include_dir| { + try td.include_dirs.append(include_dir); + } + + { + var it = module.dependencies.iterator(); + while (it.next()) |kv| { + try td.add(kv.value_ptr.*); + } + } } fn addInner(td: *TransitiveDeps, other: *Module, dyn: bool) !void { @@ -732,6 +791,13 @@ const TransitiveDeps = struct { td.is_linking_libcpp = td.is_linking_libcpp or other.is_linking_libcpp; td.is_linking_libc = td.is_linking_libc or other.is_linking_libc; + { + var it = other.dependencies.iterator(); + while (it.next()) |kv| { + try td.addInner(kv.value_ptr.*, dyn); + } + } + // Inherit dependencies on darwin frameworks if (!dyn) { var it = other.frameworks.iterator(); @@ -791,6 +857,7 @@ const assert = std.debug.assert; const mem = std.mem; const fs = std.fs; const panic = std.debug.panic; +const builtin = @import("builtin"); pub const addSystemIncludeDir = @compileError("deprecated; use addSystemIncludePath"); pub const addIncludeDir = @compileError("deprecated; use addIncludePath"); From a5151d4a47f0205576bfe85f1d45a84afd109184 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Wed, 26 Apr 2023 22:21:21 +0000 Subject: [PATCH 22/29] Move forceUndefinedSymbol back to CompileStep This may need to move back to Moulde eventually, but the goal is the handle the easy + common cases first --- lib/std/Build/CompileStep.zig | 11 +++++++++++ lib/std/Build/Module.zig | 11 ----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index d0afb506b410..975debc8983f 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -189,6 +189,11 @@ subsystem: ?std.Target.SubSystem = null, entry_symbol_name: ?[]const u8 = null, +/// List of symbols forced as undefined in the symbol table +/// thus forcing their resolution by the linker. +/// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. +force_undefined_symbols: StringHashMap(void), + /// Overrides the default stack size stack_size: ?u64 = null, @@ -328,6 +333,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { .rdynamic = false, .override_dest_dir = null, .installed_path = null, + .force_undefined_symbols = StringHashMap(void).init(owner.allocator), .output_path_source = GeneratedFile{ .step = &self.step }, .output_lib_path_source = GeneratedFile{ .step = &self.step }, @@ -401,6 +407,11 @@ pub fn checkObject(self: *CompileStep) *CheckObjectStep { return CheckObjectStep.create(self.step.owner, self.getOutputSource(), self.target_info.target.ofmt); } +pub fn forceUndefinedSymbol(self: *CompileStep, symbol_name: []const u8) void { + const b = self.owner; + self.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM"); +} + pub fn isDynamicLibrary(self: *CompileStep) bool { return self.kind == .lib and self.linkage == Linkage.dynamic; } diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 6f11537abe00..f5eb01b82376 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -20,11 +20,6 @@ is_linking_libc: bool, is_linking_libcpp: bool, linker_script: ?FileSource = null, -/// List of symbols forced as undefined in the symbol table -/// thus forcing their resolution by the linker. -/// Corresponds to `-u ` for ELF/MachO and `/include:` for COFF/PE. -force_undefined_symbols: StringHashMap(void), - pub fn create(owner: *Build, name: ?[]const u8, options: CreateModuleOptions) *Module { const arena = owner.allocator; const mod = owner.allocator.create(Module) catch @panic("OOM"); @@ -46,7 +41,6 @@ pub fn create(owner: *Build, name: ?[]const u8, options: CreateModuleOptions) *M .lib_paths = ArrayList(FileSource).init(arena), .rpaths = ArrayList(FileSource).init(arena), .framework_dirs = ArrayList(FileSource).init(arena), - .force_undefined_symbols = StringHashMap(void).init(arena), .c_macros = ArrayList([]const u8).init(arena), .c_std = std.Build.CStd.C99, .is_linking_libc = false, @@ -740,11 +734,6 @@ pub fn addFrameworkPathDirectorySource(m: *Module, directory_source: FileSource) directory_source.addStepDependencies(&m.step); } -pub fn forceUndefinedSymbol(m: *Module, symbol_name: []const u8) void { - const b = m.step.owner; - m.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM"); -} - pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { const b = m.step.owner; m.linker_script = source.dupe(b); From f4177f4421076719dc25fc1059eeb52e07dba680 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Wed, 26 Apr 2023 23:01:23 +0000 Subject: [PATCH 23/29] Add framework_dirs, frameworks, c_macros, and rpaths --- lib/std/Build/CompileStep.zig | 78 ++++---------------- lib/std/Build/Module.zig | 129 ++++++++++++++++++++-------------- 2 files changed, 92 insertions(+), 115 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 975debc8983f..94bfa078caae 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -694,11 +694,23 @@ pub fn appendModuleArgs( const mod = kv.key_ptr.*; const name = kv.value_ptr.*; - try mod.appendArgs(self, name, zig_args); + try zig_args.append("--mod"); + try zig_args.append(name); + + if (mod.source_file) |rs| { + try zig_args.append(rs.getPath2(b, &self.step)); + } + + const args_str = try mod.constructArgsString(self); + if (args_str.len > 0) { + try zig_args.append("--args"); + try zig_args.appendSlice(args_str); + } const deps_str = try constructDepString(b.allocator, mod_names, mod.dependencies); if (deps_str.len > 0) { - try zig_args.append(try std.fmt.allocPrint(b.allocator, "--deps {s}", .{deps_str})); + try zig_args.append("--deps"); + try zig_args.append(deps_str); } } } @@ -761,7 +773,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } { - var it = self.main_module.force_undefined_symbols.keyIterator(); + var it = self.force_undefined_symbols.keyIterator(); while (it.next()) |symbol_name| { try zig_args.append("--force_undefined"); try zig_args.append(symbol_name.*); @@ -1033,66 +1045,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try self.appendModuleArgs(&zig_args); - for (self.main_module.c_macros.items) |c_macro| { - try zig_args.append("-D"); - try zig_args.append(c_macro); - } - - try zig_args.ensureUnusedCapacity(2 * self.main_module.lib_paths.items.len); - for (self.main_module.lib_paths.items) |lib_path| { - zig_args.appendAssumeCapacity("-L"); - zig_args.appendAssumeCapacity(lib_path.getPath2(b, step)); - } - - try zig_args.ensureUnusedCapacity(2 * self.main_module.rpaths.items.len); - for (self.main_module.rpaths.items) |rpath| { - zig_args.appendAssumeCapacity("-rpath"); - - if (self.target_info.target.isDarwin()) switch (rpath) { - .path => |path| { - // On Darwin, we should not try to expand special runtime paths such as - // * @executable_path - // * @loader_path - if (mem.startsWith(u8, path, "@executable_path") or - mem.startsWith(u8, path, "@loader_path")) - { - zig_args.appendAssumeCapacity(path); - continue; - } - }, - .generated => {}, - }; - - zig_args.appendAssumeCapacity(rpath.getPath2(b, step)); - } - - for (self.main_module.framework_dirs.items) |directory_source| { - if (b.sysroot != null) { - try zig_args.append("-iframeworkwithsysroot"); - } else { - try zig_args.append("-iframework"); - } - try zig_args.append(directory_source.getPath2(b, step)); - try zig_args.append("-F"); - try zig_args.append(directory_source.getPath2(b, step)); - } - - { - var it = self.main_module.frameworks.iterator(); - while (it.next()) |entry| { - const name = entry.key_ptr.*; - const info = entry.value_ptr.*; - if (info.needed) { - try zig_args.append("-needed_framework"); - } else if (info.weak) { - try zig_args.append("-weak_framework"); - } else { - try zig_args.append("-framework"); - } - try zig_args.append(name); - } - } - if (b.sysroot) |sysroot| { try zig_args.appendSlice(&[_][]const u8{ "--sysroot", sysroot }); } diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index f5eb01b82376..9b3355d312a9 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -26,6 +26,7 @@ pub fn create(owner: *Build, name: ?[]const u8, options: CreateModuleOptions) *M mod.* = .{ .step = Step.init(.{ .id = .module, + .makeFn = make, .name = if (name) |n| std.fmt.allocPrint(arena, "module {s}", .{n}) catch @panic("OOM") else @@ -63,56 +64,97 @@ pub fn create(owner: *Build, name: ?[]const u8, options: CreateModuleOptions) *M return mod; } -pub fn appendArgs( +pub fn make(step: *Step, prog_node: *std.Progress.Node) !void { + // Make is a no-op, we only check that the module has at least one source file. + const m = @fieldParentPtr(Module, "step", step); + if (m.source_file != null) return; + + for (m.link_objects.items) |obj| { + switch (obj) { + .c_source_file => return, + .c_source_files => |files| { + if (files.files.len > 0) return; + }, + .assembly_file => return, + else => {}, + } + } + + return m.step.fail("{s} must have at least one source file", .{prog_node.name}); +} + +pub fn constructArgsString( m: *Module, compile_step: *CompileStep, - name: []const u8, - zig_args: *ArrayList([]const u8), -) !void { - try zig_args.append("--mod"); - try zig_args.append(name); +) ![]const []const u8 { + const b = m.step.owner; + var zig_args = ArrayList([]const u8).init(b.allocator); - var num_source_files: usize = 0; + for (m.c_macros.items) |c_macro| { + try zig_args.append("-D"); + try zig_args.append(c_macro); + } - if (m.source_file) |rs| { - try zig_args.append(rs.getPath2(m.step.owner, &m.step)); - num_source_files += 1; + try zig_args.ensureUnusedCapacity(2 * m.lib_paths.items.len); + for (m.lib_paths.items) |lib_path| { + zig_args.appendAssumeCapacity("-L"); + zig_args.appendAssumeCapacity(lib_path.getPath2(b, &m.step)); } - for (m.link_objects.items) |obj| { - switch (obj) { - .c_source_file => |_| { - // try zig_args.append(c.source.getPath2(m.step.owner, &m.step)); - num_source_files +|= 1; - }, - .c_source_files => |c_files| { - for (c_files.files) |_| { - // try zig_args.append(file_path); - num_source_files +|= 1; + try zig_args.ensureUnusedCapacity(2 * m.rpaths.items.len); + for (m.rpaths.items) |rpath| { + zig_args.appendAssumeCapacity("-rpath"); + + if (compile_step.target_info.target.isDarwin()) switch (rpath) { + .path => |path| { + // On Darwin, we should not try to expand special runtime paths such as + // * @executable_path + // * @loader_path + if (mem.startsWith(u8, path, "@executable_path") or + mem.startsWith(u8, path, "@loader_path")) + { + zig_args.appendAssumeCapacity(path); + continue; } }, - // TODO: Should ASM files be included here? - .assembly_file => |_| { - // try zig_args.append(asm_file.getPath2(m.step.owner, &m.step)); - num_source_files +|= 1; - }, - else => {}, - } + .generated => {}, + }; + + zig_args.appendAssumeCapacity(rpath.getPath2(b, &m.step)); } - if (num_source_files == 0) { - return m.step.fail("Module '{s}' must have at least one source file", .{name}); + for (m.framework_dirs.items) |directory_source| { + if (b.sysroot != null) { + try zig_args.append("-iframeworkwithsysroot"); + } else { + try zig_args.append("-iframework"); + } + try zig_args.append(directory_source.getPath2(b, &m.step)); + try zig_args.append("-F"); + try zig_args.append(directory_source.getPath2(b, &m.step)); } - try zig_args.append("--args"); + { + var it = m.frameworks.iterator(); + while (it.next()) |entry| { + const name = entry.key_ptr.*; + const info = entry.value_ptr.*; + if (info.needed) { + try zig_args.append("-needed_framework"); + } else if (info.weak) { + try zig_args.append("-weak_framework"); + } else { + try zig_args.append("-framework"); + } + try zig_args.append(name); + } + } // We will add link objects from transitive dependencies, but we want to keep // all link objects in the same order provided. // This array is used to keep m.link_objects immutable. - const b = m.step.owner; var transitive_deps: TransitiveDeps = .{ .link_objects = ArrayList(LinkObject).init(b.allocator), - .include_dirs = ArrayList(IncludeDir).init(b.allocator), .seen_system_libs = StringHashMap(void).init(b.allocator), .seen_steps = std.AutoHashMap(*const Step, void).init(b.allocator), .is_linking_libcpp = m.is_linking_libcpp, @@ -246,7 +288,7 @@ pub fn appendArgs( try zig_args.append("-lc"); } - for (transitive_deps.include_dirs.items) |include_dir| { + for (m.include_dirs.items) |include_dir| { switch (include_dir) { .raw_path => |include_path| { try zig_args.append("-I"); @@ -295,6 +337,8 @@ pub fn appendArgs( }, } } + + return zig_args.toOwnedSlice(); } fn moduleDependenciesToArrayHashMap(arena: Allocator, deps: []const ModuleDependency) std.StringArrayHashMap(*Module) { @@ -742,7 +786,6 @@ pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { const TransitiveDeps = struct { link_objects: ArrayList(LinkObject), - include_dirs: ArrayList(IncludeDir), seen_system_libs: StringHashMap(void), seen_steps: std.AutoHashMap(*const Step, void), is_linking_libcpp: bool, @@ -762,17 +805,6 @@ const TransitiveDeps = struct { else => {}, } } - - for (module.include_dirs.items) |include_dir| { - try td.include_dirs.append(include_dir); - } - - { - var it = module.dependencies.iterator(); - while (it.next()) |kv| { - try td.add(kv.value_ptr.*); - } - } } fn addInner(td: *TransitiveDeps, other: *Module, dyn: bool) !void { @@ -780,13 +812,6 @@ const TransitiveDeps = struct { td.is_linking_libcpp = td.is_linking_libcpp or other.is_linking_libcpp; td.is_linking_libc = td.is_linking_libc or other.is_linking_libc; - { - var it = other.dependencies.iterator(); - while (it.next()) |kv| { - try td.addInner(kv.value_ptr.*, dyn); - } - } - // Inherit dependencies on darwin frameworks if (!dyn) { var it = other.frameworks.iterator(); From 125b6bffb568c7e366dac46fd605f230176ad81b Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Wed, 26 Apr 2023 23:05:44 +0000 Subject: [PATCH 24/29] Add doc comment --- lib/std/Build/Module.zig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 9b3355d312a9..09d45948ea1b 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -83,6 +83,9 @@ pub fn make(step: *Step, prog_node: *std.Progress.Node) !void { return m.step.fail("{s} must have at least one source file", .{prog_node.name}); } +/// Constructs a string containing all module-specific build arguments needed to +/// build for the provided `CompileStep`. The list of arguments does *not* contain +/// arguments needed for dependency modules. pub fn constructArgsString( m: *Module, compile_step: *CompileStep, From 119bf1ca47d468de78e8a8f2252b0260cbca7c92 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Wed, 26 Apr 2023 23:10:06 +0000 Subject: [PATCH 25/29] Move linker_script and c_std back to CompileStep In the interest of keeping things simple, these can stay in CompileStep for now. They may need to be moved to Module later --- lib/std/Build/CompileStep.zig | 11 ++++++++++- lib/std/Build/Module.zig | 10 ---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 94bfa078caae..89f4545b9208 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -36,6 +36,7 @@ name: []const u8, target: CrossTarget, target_info: NativeTargetInfo, optimize: std.builtin.Mode, +linker_script: ?FileSource = null, version_script: ?[]const u8 = null, out_filename: []const u8, linkage: ?Linkage = null, @@ -77,6 +78,7 @@ initial_memory: ?u64 = null, max_memory: ?u64 = null, shared_memory: bool = false, global_base: ?u64 = null, +c_std: std.Build.CStd, zig_lib_dir: ?[]const u8, main_pkg_path: ?[]const u8, exec_cmd_args: ?[]const ?[]const u8, @@ -322,6 +324,7 @@ pub fn create(owner: *std.Build, options: Options) *CompileStep { .out_pdb_filename = owner.fmt("{s}.pdb", .{name}), .major_only_filename = null, .name_only_filename = null, + .c_std = std.Build.CStd.C99, .zig_lib_dir = null, .main_pkg_path = null, .exec_cmd_args = null, @@ -407,6 +410,12 @@ pub fn checkObject(self: *CompileStep) *CheckObjectStep { return CheckObjectStep.create(self.step.owner, self.getOutputSource(), self.target_info.target.ofmt); } +pub fn setLinkerScriptPath(self: *CompileStep, source: FileSource) void { + const b = self.step.owner; + self.linker_script = source.dupe(b); + source.addStepDependencies(&self.step); +} + pub fn forceUndefinedSymbol(self: *CompileStep, symbol_name: []const u8) void { const b = self.owner; self.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM"); @@ -1020,7 +1029,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { } } - if (self.main_module.linker_script) |linker_script| { + if (self.linker_script) |linker_script| { try zig_args.append("--script"); try zig_args.append(linker_script.getPath(b)); } diff --git a/lib/std/Build/Module.zig b/lib/std/Build/Module.zig index 09d45948ea1b..655138339033 100644 --- a/lib/std/Build/Module.zig +++ b/lib/std/Build/Module.zig @@ -14,11 +14,9 @@ lib_paths: ArrayList(FileSource), rpaths: ArrayList(FileSource), framework_dirs: ArrayList(FileSource), c_macros: ArrayList([]const u8), -c_std: std.Build.CStd, is_linking_libc: bool, is_linking_libcpp: bool, -linker_script: ?FileSource = null, pub fn create(owner: *Build, name: ?[]const u8, options: CreateModuleOptions) *Module { const arena = owner.allocator; @@ -43,10 +41,8 @@ pub fn create(owner: *Build, name: ?[]const u8, options: CreateModuleOptions) *M .rpaths = ArrayList(FileSource).init(arena), .framework_dirs = ArrayList(FileSource).init(arena), .c_macros = ArrayList([]const u8).init(arena), - .c_std = std.Build.CStd.C99, .is_linking_libc = false, .is_linking_libcpp = false, - .linker_script = null, }; if (options.source_file) |rs| { @@ -781,12 +777,6 @@ pub fn addFrameworkPathDirectorySource(m: *Module, directory_source: FileSource) directory_source.addStepDependencies(&m.step); } -pub fn setLinkerScriptPath(m: *Module, source: FileSource) void { - const b = m.step.owner; - m.linker_script = source.dupe(b); - source.addStepDependencies(&m.step); -} - const TransitiveDeps = struct { link_objects: ArrayList(LinkObject), seen_system_libs: StringHashMap(void), From b6bc9c802c9d6197ac1d216861611faf07bfd8cf Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sat, 29 Apr 2023 22:26:04 +0000 Subject: [PATCH 26/29] Add --root-source and --args-end flags to make CLI parsing easier. --- lib/std/Build/CompileStep.zig | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 89f4545b9208..84f6add4a169 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -707,20 +707,22 @@ pub fn appendModuleArgs( try zig_args.append(name); if (mod.source_file) |rs| { + try zig_args.append("--root-source"); try zig_args.append(rs.getPath2(b, &self.step)); } - const args_str = try mod.constructArgsString(self); - if (args_str.len > 0) { - try zig_args.append("--args"); - try zig_args.appendSlice(args_str); - } - const deps_str = try constructDepString(b.allocator, mod_names, mod.dependencies); if (deps_str.len > 0) { try zig_args.append("--deps"); try zig_args.append(deps_str); } + + const args_str = try mod.constructArgsString(self); + if (args_str.len > 0) { + try zig_args.append("--args"); + try zig_args.appendSlice(args_str); + try zig_args.append("--args-end"); + } } } } From 771f41554d2277959e23c39c62d5fd2bc7c8bf3c Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sat, 29 Apr 2023 22:30:33 +0000 Subject: [PATCH 27/29] Make addExecutable, add*Library take existing Modules --- lib/std/Build.zig | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index d4b1aae41dfb..a956a5fc43a0 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -449,7 +449,7 @@ pub fn addOptions(self: *Build) *OptionsStep { pub const ExecutableOptions = struct { name: []const u8, - main_module_options: CreateModuleOptions, + main_module: *Module, version: ?std.builtin.Version = null, target: CrossTarget = .{}, optimize: std.builtin.Mode = .Debug, @@ -462,10 +462,9 @@ pub const ExecutableOptions = struct { }; pub fn addExecutable(b: *Build, options: ExecutableOptions) *CompileStep { - const main_module = Module.create(b, options.name, options.main_module_options); return CompileStep.create(b, .{ .name = options.name, - .main_module = main_module, + .main_module = options.main_module, .version = options.version, .target = options.target, .optimize = options.optimize, @@ -508,7 +507,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *CompileStep { pub const SharedLibraryOptions = struct { name: []const u8, - main_module_options: CreateModuleOptions, + main_module: *Module, version: ?std.builtin.Version = null, target: CrossTarget, optimize: std.builtin.Mode, @@ -520,10 +519,9 @@ pub const SharedLibraryOptions = struct { }; pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *CompileStep { - const main_module = Module.create(b, options.name, options.main_module_options); return CompileStep.create(b, .{ .name = options.name, - .main_module = main_module, + .main_module = options.main_module, .kind = .lib, .linkage = .dynamic, .version = options.version, @@ -539,7 +537,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *CompileStep { pub const StaticLibraryOptions = struct { name: []const u8, - main_module_options: CreateModuleOptions, + main_module: *Module, target: CrossTarget, optimize: std.builtin.Mode, version: ?std.builtin.Version = null, @@ -551,10 +549,9 @@ pub const StaticLibraryOptions = struct { }; pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *CompileStep { - const main_module = b.createModule(options.main_module_options); return CompileStep.create(b, .{ .name = options.name, - .main_module = main_module, + .main_module = options.main_module, .kind = .lib, .linkage = .static, .version = options.version, From c8d48c962ab6ecd316ed535ac8f28dfb675fc2cb Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sun, 30 Apr 2023 00:49:34 +0000 Subject: [PATCH 28/29] Update tests for new build API --- build.zig | 16 +- lib/std/Build.zig | 8 +- lib/std/Build/CompileStep.zig | 190 +++++++++++++++++- lib/std/Build/TranslateCStep.zig | 5 +- .../add_overflow_in_function_evaluation.zig | 4 +- .../addition_with_non_numbers.zig | 6 +- .../address_of_number_literal.zig | 8 +- .../alignment_of_enum_field_specified.zig | 5 +- .../array_access_of_non_array.zig | 4 +- .../array_concatenation_with_wrong_type.zig | 4 +- .../array_mult_with_number_type.zig | 2 +- .../assign_inline_fn_to_non-comptime_var.zig | 2 +- .../assign_null_to_non-optional_pointer.zig | 4 +- .../assign_through_constant_pointer.zig | 4 +- .../assign_through_constant_slice.zig | 4 +- .../assign_to_constant_field.zig | 4 +- ...c_function_pointer_passed_to_asyncCall.zig | 2 +- ...bad_implicit_casting_of_anyframe_types.zig | 2 +- ...own_function_called_with_async_keyword.zig | 2 +- test/cases/compile_errors/bad_import.zig | 4 +- .../compile_errors/bad_usage_of_call.zig | 3 +- .../binary_not_on_number_literal.zig | 4 +- .../compile_errors/bogus_compile_var.zig | 4 +- .../bogus_method_call_on_slice.zig | 4 +- .../branch_on_undefined_value.zig | 4 +- ...function_with_naked_calling_convention.zig | 2 +- ...ction_passing_array_instead_of_pointer.zig | 4 +- .../cases/compile_errors/cast_unreachable.zig | 4 +- ..._bit_offset_pointer_to_regular_pointer.zig | 4 +- .../colliding_invalid_top_level_functions.zig | 4 +- ...traceback_of_references_that_caused_it.zig | 4 +- ..._tagged_enum_doesnt_crash_the_compiler.zig | 6 +- test/cases/compile_errors/compile_log.zig | 13 +- ...nt_warning_deduplication_in_generic_fn.zig | 4 +- .../compile_time_division_by_zero.zig | 4 +- .../comptime_if_inside_runtime_for.zig | 10 +- ...de_comptime_function_has_compile_error.zig | 4 +- .../container_init_with_non-type.zig | 4 +- ...trol_flow_uses_comptime_var_at_runtime.zig | 2 +- .../compile_errors/dereference_an_array.zig | 4 +- .../compile_errors/direct_struct_loop.zig | 8 +- ...ted_pointer_to_null-terminated_pointer.zig | 2 +- .../cases/compile_errors/division_by_zero.zig | 16 +- .../duplicate-unused_labels.zig | 16 +- .../duplicate_error_value_in_error_set.zig | 2 +- ...icate_field_in_struct_value_expression.zig | 8 +- .../embedFile_with_bogus_file.zig | 8 +- .../empty_switch_on_an_integer.zig | 2 +- ...eclarations_unavailable_for_reify_type.zig | 5 +- .../error_not_handled_in_switch.zig | 6 +- ...for_function_parameter_incompatibility.zig | 8 +- ..._known_at_comptime_violates_error_sets.zig | 4 +- ...xport_function_with_comptime_parameter.zig | 2 +- .../export_with_empty_name_string.zig | 2 +- .../extern_function_pointer_mismatch.zig | 18 +- ...xtern_function_with_comptime_parameter.zig | 12 +- ...mpatible_but_inferred_integer_tag_type.zig | 30 +-- .../extern_union_field_missing_type.zig | 2 +- .../extern_union_given_enum_tag_type.zig | 2 +- ...comptime_field_ptr_not_based_on_struct.zig | 5 +- ...ldParentPtr-comptime_wrong_field_index.zig | 5 +- test/cases/compile_errors/for.zig | 10 +- .../compile_errors/for_extra_capture.zig | 5 +- .../function_alignment_non_power_of_2.zig | 4 +- ...nction_call_assigned_to_incorrect_type.zig | 2 +- .../function_parameter_is_opaque.zig | 8 +- ...h_non-extern_non-packed_enum_parameter.zig | 4 +- ...non-extern_non-packed_struct_parameter.zig | 4 +- ..._non-extern_non-packed_union_parameter.zig | 4 +- ...nction_call_assigned_to_incorrect_type.zig | 2 +- ..._instance_with_non-constant_expression.zig | 8 +- ...ailure_in_generic_function_return_type.zig | 1 - ...obal_variable_alignment_non_power_of_2.zig | 4 +- ...nitializer_must_be_constant_expression.zig | 4 +- .../ignored_assert-err-ok_return_value.zig | 4 +- .../ignored_comptime_statement_value.zig | 4 +- .../ignored_deferred_function_call.zig | 4 +- .../ignored_deferred_statement_value.zig | 4 +- .../compile_errors/ignored_return_value.zig | 4 +- .../illegal_comparison_of_types.zig | 8 +- ...licit_cast_from_array_to_mutable_slice.zig | 4 +- ...mplicit_cast_of_error_set_not_a_subset.zig | 4 +- ...mplicitly_increasing_pointer_alignment.zig | 2 +- .../implicitly_increasing_slice_alignment.zig | 2 +- .../import_outside_package_path.zig | 2 +- .../compile_errors/incorrect_return_type.zig | 30 +-- .../increase_pointer_alignment_in_ptrCast.zig | 2 +- .../compile_errors/indirect_struct_loop.zig | 16 +- .../inferred_array_size_invalid_here.zig | 2 +- ...nferring_error_set_of_function_pointer.zig | 2 +- .../compile_errors/integer_overflow_error.zig | 6 +- .../compile_errors/invalid_builtin_fn.zig | 5 +- .../compile_errors/invalid_capture_type.zig | 4 +- ...valid_comparison_for_function_pointers.zig | 4 +- .../invalid_field_access_in_comptime.zig | 5 +- ...valid_field_in_struct_value_expression.zig | 9 +- ...invalid_optional_type_in_extern_struct.zig | 4 +- .../invalid_pointer_with_reify_type.zig | 2 +- .../invalid_shift_amount_error.zig | 6 +- test/cases/compile_errors/invalid_type.zig | 4 +- .../invalid_variadic_function.zig | 8 +- ..._3818_bitcast_from_parray-slice_to_u16.zig | 4 +- .../local_variable_redeclaration.zig | 2 +- .../local_variable_redeclares_parameter.zig | 6 +- .../local_variable_shadowing_global.zig | 2 +- .../main_function_with_bogus_args_type.zig | 4 +- ..._const_in_slice_with_nested_array_type.zig | 2 +- .../compile_errors/missing_else_clause.zig | 8 +- ...ssing_field_in_struct_value_expression.zig | 8 +- .../missing_main_fn_in_executable.zig | 2 - .../compile_errors/missing_param_name.zig | 4 +- ...elled_type_with_pointer_only_reference.zig | 6 +- .../mul_overflow_in_function_evaluation.zig | 5 +- .../multiple_function_definitions.zig | 4 +- ...gation_overflow_in_function_evaluation.zig | 4 +- test/cases/compile_errors/nested_vectors.zig | 1 - .../noalias_on_non_pointer_param.zig | 20 +- ...-comptime-parameter-used-as-array-size.zig | 3 +- ...h_struct_return_value_outside_function.zig | 6 +- ...ion_in_struct_literal_outside_function.zig | 6 +- ...of_things_that_require_const_variables.zig | 28 +-- ...xhaustive_enum_marker_assigned_a_value.zig | 5 +- ..._loop_on_a_type_that_requires_comptime.zig | 4 +- .../non_constant_expression_in_array_size.zig | 8 +- .../offsetOf-bad_field_name.zig | 5 +- .../compile_errors/offsetOf-non_struct.zig | 5 +- .../old_fn_ptr_in_extern_context.zig | 2 +- .../overflow_in_enum_value_allocation.zig | 4 +- .../packed_union_given_enum_tag_type.zig | 2 +- ...cked_union_with_automatic_layout_field.zig | 2 +- .../panic_called_at_compile_time.zig | 4 +- .../parameter_redeclaration.zig | 5 +- .../pass_const_ptr_to_mutable_ptr_fn.zig | 12 +- ...sing_an_under-aligned_function_pointer.zig | 4 +- .../compile_errors/pointer_to_noreturn.zig | 4 +- ...e_operator_in_switch_used_on_error_set.zig | 6 +- .../reassign_to_array_parameter.zig | 4 +- .../reassign_to_struct_parameter.zig | 4 +- .../compile_errors/redefinition_of_enums.zig | 4 +- .../redefinition_of_global_variables.zig | 4 +- .../compile_errors/redefinition_of_struct.zig | 8 +- .../reference_to_const_data.zig | 6 +- .../reify_type.Fn_with_is_generic_true.zig | 4 +- ...th_is_var_args_true_and_non-C_callconv.zig | 4 +- .../reify_type.Fn_with_return_type_null.zig | 4 +- .../reify_type_union_payload_is_undefined.zig | 4 +- .../return_from_defer_expression.zig | 6 +- .../return_invalid_type_from_test.zig | 6 +- ...ime_assignment_to_comptime_struct_type.zig | 2 +- ...time_assignment_to_comptime_union_type.zig | 2 +- .../runtime_to_comptime_num.zig | 6 +- ...oes_not_allow_negative_rhs_at_comptime.zig | 6 +- ...f_referential_struct_requires_comptime.zig | 1 - .../setAlignStack_in_inline_function.zig | 3 +- ...e_passed_as_array_init_type_with_elems.zig | 2 +- .../slice_sentinel_mismatch-2.zig | 4 +- .../slice_used_as_extern_fn_param.zig | 2 +- ...pecify_enum_tag_type_that_is_too_small.zig | 2 +- .../specify_non-integer_enum_tag_type.zig | 2 +- .../compile_errors/src_fields_runtime.zig | 5 +- ...n_where_return_type_is_self-referenced.zig | 6 +- ...fier_at_start_of_asm_output_constraint.zig | 6 +- .../std.fmt_error_for_unused_arguments.zig | 2 +- .../struct_type_mismatch_in_arg.zig | 6 +- ...eclarations_unavailable_for_reify_type.zig | 4 +- .../struct_with_invalid_field.zig | 13 +- .../sub_overflow_in_function_evaluation.zig | 4 +- .../suspend_inside_suspend_block.zig | 3 +- ...expression-duplicate_enumeration_prong.zig | 4 +- ...te_enumeration_prong_when_else_present.zig | 4 +- ...duplicate_or_overlapping_integer_value.zig | 12 +- .../switch_expression-duplicate_type.zig | 4 +- ...expression-duplicate_type_struct_alias.zig | 4 +- ...h_expression-missing_enumeration_prong.zig | 4 +- ...pression-non_exhaustive_integer_prongs.zig | 4 +- ...on-switch_on_pointer_type_with_no_else.zig | 4 +- ...expression-unreachable_else_prong_bool.zig | 4 +- ...expression-unreachable_else_prong_enum.zig | 6 +- ...ession-unreachable_else_prong_range_i8.zig | 4 +- ...ession-unreachable_else_prong_range_u8.zig | 4 +- ...h_expression-unreachable_else_prong_u1.zig | 4 +- ...h_expression-unreachable_else_prong_u2.zig | 4 +- .../switch_on_union_with_no_attached_enum.zig | 2 +- ...hing_with_exhaustive_enum_has___prong_.zig | 2 +- .../switching_with_non-exhaustive_enums.zig | 2 +- ...n_invalid_value_of_non-exhaustive_enum.zig | 2 +- ...d_on_union_with_no_associated_enum_tag.zig | 2 +- .../top_level_decl_dependency_loop.zig | 4 +- ...in_function_with_non_error_return_type.zig | 2 +- .../compile_errors/tuple_init_edge_cases.zig | 36 ++-- .../type_checking_function_pointers.zig | 4 +- .../compile_errors/undeclared_identifier.zig | 5 +- .../union_auto-enum_value_already_taken.zig | 2 +- .../union_enum_field_does_not_match_enum.zig | 2 +- .../compile_errors/unreachable_parameter.zig | 8 +- .../unreachable_with_return.zig | 8 +- .../while_expected_bool_got_error_union.zig | 4 +- .../while_expected_bool_got_optional.zig | 4 +- .../while_expected_error_union_got_bool.zig | 10 +- ...hile_expected_error_union_got_optional.zig | 10 +- .../while_expected_optional_got_bool.zig | 8 +- ...hile_expected_optional_got_error_union.zig | 8 +- .../write_to_const_global_variable.zig | 6 +- .../compile_errors/wrong_function_type.zig | 18 +- .../wrong_number_of_arguments.zig | 6 +- ...number_of_arguments_for_method_fn_call.zig | 10 +- .../wrong_size_to_an_array_literal.zig | 2 +- .../wrong_types_given_to_export.zig | 4 +- test/cases/error_in_nested_declaration.zig | 2 +- test/cases/safety/@alignCast misaligned.zig | 2 +- ...tCast error not present in destination.zig | 4 +- ...Int cannot fit - negative out of range.zig | 2 +- ...oInt cannot fit - negative to unsigned.zig | 2 +- ...Int cannot fit - positive out of range.zig | 2 +- test/cases/safety/bad union field access.zig | 2 +- ...ast []u8 to bigger slice of wrong size.zig | 2 +- ...ror return trace across suspend points.zig | 3 +- .../exact division failure - vectors.zig | 4 +- test/cases/safety/for_len_mismatch_three.zig | 1 - .../integer division by zero - vectors.zig | 4 +- test/cases/unused_labels.3.zig | 4 +- test/cases/variable_shadowing.3.zig | 3 +- test/cases/variable_shadowing.4.zig | 3 +- test/cases/variable_shadowing.5.zig | 3 +- test/cases/variable_shadowing.6.zig | 4 +- test/cases/x86_64-linux/inline_assembly.2.zig | 2 +- test/cases/x86_64-linux/inline_assembly.3.zig | 2 +- test/link/bss/build.zig | 5 +- test/link/common_symbols/build.zig | 11 +- test/link/common_symbols_alignment/build.zig | 13 +- test/link/glibc_compat/build.zig | 5 +- .../interdependent_static_c_libs/build.zig | 21 +- test/link/macho/bugs/13056/build.zig | 14 +- test/link/macho/bugs/13457/build.zig | 5 +- test/link/macho/dead_strip/build.zig | 8 +- test/link/macho/dead_strip_dylibs/build.zig | 1 + test/link/macho/dylib/build.zig | 16 +- test/link/macho/empty/build.zig | 9 +- test/link/macho/entry/build.zig | 8 +- test/link/macho/entry_in_archive/build.zig | 10 +- test/link/macho/entry_in_dylib/build.zig | 16 +- test/link/macho/headerpad/build.zig | 1 + test/link/macho/linksection/build.zig | 4 +- test/link/macho/needed_framework/build.zig | 8 +- test/link/macho/needed_library/build.zig | 16 +- test/link/macho/objc/build.zig | 9 +- test/link/macho/objcpp/build.zig | 9 +- test/link/macho/pagezero/build.zig | 16 +- test/link/macho/search_strategy/build.zig | 3 + test/link/macho/stack_size/build.zig | 2 + test/link/macho/strict_validation/build.zig | 5 +- test/link/macho/tls/build.zig | 7 +- test/link/macho/unwind_info/build.zig | 1 + test/link/macho/weak_framework/build.zig | 1 + test/link/macho/weak_library/build.zig | 4 + test/link/wasm/archive/build.zig | 5 +- test/link/wasm/basic-features/build.zig | 5 +- test/link/wasm/bss/build.zig | 10 +- test/link/wasm/export-data/build.zig | 5 +- test/link/wasm/export/build.zig | 15 +- test/link/wasm/extern-mangle/build.zig | 5 +- test/link/wasm/extern/build.zig | 7 +- test/link/wasm/function-table/build.zig | 15 +- test/link/wasm/infer-features/build.zig | 12 +- test/link/wasm/producers/build.zig | 5 +- test/link/wasm/segments/build.zig | 5 +- test/link/wasm/stack_pointer/build.zig | 5 +- test/link/wasm/type/build.zig | 5 +- test/src/Cases.zig | 30 ++- test/src/CompareOutput.zig | 9 +- test/src/StackTrace.zig | 5 +- test/standalone.zig | 24 --- test/standalone/c_compiler/build.zig | 2 + test/standalone/dep_diamond/build.zig | 22 +- .../dep_mutually_recursive/build.zig | 5 +- test/standalone/dep_recursive/build.zig | 8 +- test/standalone/dep_shared_builtin/build.zig | 13 +- test/standalone/dep_triangle/build.zig | 18 +- .../standalone/embed_generated_file/build.zig | 17 +- test/standalone/empty_env/build.zig | 4 +- test/standalone/extern/build.zig | 8 +- test/standalone/global_linkage/build.zig | 12 +- test/standalone/install_raw_hex/build.zig | 4 +- test/standalone/issue_11595/build.zig | 4 +- test/standalone/issue_12706/build.zig | 4 +- test/standalone/issue_13030/build.zig | 4 +- test/standalone/issue_13970/build.zig | 12 +- test/standalone/issue_339/build.zig | 4 +- test/standalone/issue_5825/build.zig | 5 +- test/standalone/issue_794/build.zig | 4 +- test/standalone/issue_8550/build.zig | 5 +- .../standalone/load_dynamic_library/build.zig | 8 +- test/standalone/main_pkg_path/build.zig | 4 +- test/standalone/mix_c_files/build.zig | 4 +- test/standalone/mix_o_files/build.zig | 6 +- .../module_add_include_path/build.zig | 23 --- .../module_add_include_path/include/header.h | 8 - .../module_add_include_path/test.zig | 6 - .../module_add_include_path/test_module.zig | 3 - test/standalone/module_add_options/build.zig | 30 --- test/standalone/module_add_options/test.zig | 5 - .../module_add_options/test_module.zig | 6 - .../module_install_header/build.zig | 24 --- .../module_install_header/include/header.h | 8 - .../standalone/module_install_header/test.zig | 1 - .../module_install_header/test_module.zig | 0 .../standalone/module_link_libc_cpp/build.zig | 26 --- test/standalone/module_link_libc_cpp/test.zig | 9 - .../module_link_libc_cpp/test_module.zig | 3 - test/standalone/module_link_library/build.zig | 34 ---- test/standalone/module_link_library/lib/lib.c | 5 - test/standalone/module_link_library/lib/lib.h | 4 - test/standalone/module_link_library/test.zig | 6 - .../module_link_library/test_module.zig | 3 - test/standalone/pie/build.zig | 4 +- test/standalone/pkg_import/build.zig | 11 +- test/standalone/shared_library/build.zig | 6 +- test/standalone/static_c_lib/build.zig | 5 +- test/standalone/strip_empty_loop/build.zig | 4 +- .../test_runner_module_imports/build.zig | 15 +- test/standalone/test_runner_path/build.zig | 5 +- test/standalone/use_alias/build.zig | 4 +- test/tests.zig | 40 ++-- 323 files changed, 1436 insertions(+), 804 deletions(-) delete mode 100644 test/standalone/module_add_include_path/build.zig delete mode 100644 test/standalone/module_add_include_path/include/header.h delete mode 100644 test/standalone/module_add_include_path/test.zig delete mode 100644 test/standalone/module_add_include_path/test_module.zig delete mode 100644 test/standalone/module_add_options/build.zig delete mode 100644 test/standalone/module_add_options/test.zig delete mode 100644 test/standalone/module_add_options/test_module.zig delete mode 100644 test/standalone/module_install_header/build.zig delete mode 100644 test/standalone/module_install_header/include/header.h delete mode 100644 test/standalone/module_install_header/test.zig delete mode 100644 test/standalone/module_install_header/test_module.zig delete mode 100644 test/standalone/module_link_libc_cpp/build.zig delete mode 100644 test/standalone/module_link_libc_cpp/test.zig delete mode 100644 test/standalone/module_link_libc_cpp/test_module.zig delete mode 100644 test/standalone/module_link_library/build.zig delete mode 100644 test/standalone/module_link_library/lib/lib.c delete mode 100644 test/standalone/module_link_library/lib/lib.h delete mode 100644 test/standalone/module_link_library/test.zig delete mode 100644 test/standalone/module_link_library/test_module.zig diff --git a/build.zig b/build.zig index 6cb3e35488cb..493d4e100391 100644 --- a/build.zig +++ b/build.zig @@ -35,9 +35,13 @@ pub fn build(b: *std.Build) !void { const skip_install_lib_files = b.option(bool, "no-lib", "skip copying of lib/ files and langref to installation prefix. Useful for development") orelse deprecated_skip_install_lib_files; const skip_install_langref = b.option(bool, "no-langref", "skip copying of langref to the installation prefix") orelse skip_install_lib_files; + const docgen_module = b.createModule(.{ + .source_file = .{ .path = "doc/docgen.zig" }, + }); + const docgen_exe = b.addExecutable(.{ .name = "docgen", - .root_source_file = .{ .path = "doc/docgen.zig" }, + .main_module = docgen_module, .target = .{}, .optimize = .Debug, }); @@ -64,9 +68,12 @@ pub fn build(b: *std.Build) !void { legacy_write_to_cache.addCopyFileToSource(langref_file, "zig-cache/langref.html"); docs_step.dependOn(&legacy_write_to_cache.step); + const check_case_mod = b.createModule(.{ + .source_file = .{ .path = "test/src/Cases.zig" }, + }); const check_case_exe = b.addExecutable(.{ .name = "check-case", - .root_source_file = .{ .path = "test/src/Cases.zig" }, + .main_module = check_case_mod, .optimize = optimize, }); check_case_exe.main_pkg_path = "."; @@ -537,9 +544,12 @@ fn addCompilerStep( optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget, ) *std.Build.CompileStep { + const exe_mod = b.createModule(.{ + .source_file = .{ .path = "src/main.zig" }, + }); const exe = b.addExecutable(.{ .name = "zig", - .root_source_file = .{ .path = "src/main.zig" }, + .main_module = exe_mod, .target = target, .optimize = optimize, }); diff --git a/lib/std/Build.zig b/lib/std/Build.zig index a956a5fc43a0..71ea1cd6aa2e 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -480,7 +480,7 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *CompileStep { pub const ObjectOptions = struct { name: []const u8, - root_source_file: ?FileSource = null, + main_module: *Module, target: CrossTarget, optimize: std.builtin.Mode, max_rss: usize = 0, @@ -493,7 +493,7 @@ pub const ObjectOptions = struct { pub fn addObject(b: *Build, options: ObjectOptions) *CompileStep { return CompileStep.create(b, .{ .name = options.name, - .root_source_file = options.root_source_file, + .main_module = options.main_module, .target = options.target, .optimize = options.optimize, .kind = .obj, @@ -567,7 +567,7 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *CompileStep { pub const TestOptions = struct { name: []const u8 = "test", - root_source_file: FileSource, + main_module: *Module, target: CrossTarget = .{}, optimize: std.builtin.Mode = .Debug, version: ?std.builtin.Version = null, @@ -584,7 +584,7 @@ pub fn addTest(b: *Build, options: TestOptions) *CompileStep { return CompileStep.create(b, .{ .name = options.name, .kind = .@"test", - .root_source_file = options.root_source_file, + .main_module = options.main_module, .target = options.target, .optimize = options.optimize, .max_rss = options.max_rss, diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index 84f6add4a169..d599008fffeb 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -24,7 +24,9 @@ const ObjCopyStep = std.Build.ObjCopyStep; const CheckObjectStep = std.Build.CheckObjectStep; const RunStep = std.Build.RunStep; const OptionsStep = std.Build.OptionsStep; +const InstallConfigHeaderOptions = Module.InstallConfigHeaderOptions; const ConfigHeaderStep = std.Build.ConfigHeaderStep; +const CSourceFile = std.Build.CSourceFile; const LinkObject = Module.LinkObject; const FrameworkLinkInfo = Module.FrameworkLinkInfo; const CompileStep = @This(); @@ -417,7 +419,7 @@ pub fn setLinkerScriptPath(self: *CompileStep, source: FileSource) void { } pub fn forceUndefinedSymbol(self: *CompileStep, symbol_name: []const u8) void { - const b = self.owner; + const b = self.step.owner; self.force_undefined_symbols.put(b.dupe(symbol_name), {}) catch @panic("OOM"); } @@ -634,6 +636,192 @@ pub fn setExecCmd(self: *CompileStep, args: []const ?[]const u8) void { self.exec_cmd_args = duped_args; } +pub fn addOptions(self: *CompileStep, name: []const u8, options: *OptionsStep) void { + self.main_module.addOptions(name, options); +} + +pub fn linkLibC(self: *CompileStep) void { + self.main_module.linkLibC(); +} + +pub fn linkLibCpp(self: *CompileStep) void { + self.main_module.linkLibCpp(); +} + +/// This one has no integration with anything, it just puts -lname on the command line. +/// Prefer to use `linkSystemLibrary` instead. +pub fn linkSystemLibraryName(self: *CompileStep, name: []const u8) void { + self.main_module.linkSystemLibraryName(name); +} + +/// This one has no integration with anything, it just puts -needed-lname on the command line. +/// Prefer to use `linkSystemLibraryNeeded` instead. +pub fn linkSystemLibraryNeededName(self: *CompileStep, name: []const u8) void { + self.main_module.linkSystemLibraryNeededName(name); +} + +/// Darwin-only. This one has no integration with anything, it just puts -weak-lname on the +/// command line. Prefer to use `linkSystemLibraryWeak` instead. +pub fn linkSystemLibraryWeakName(self: *CompileStep, name: []const u8) void { + self.main_module.linkSystemLibraryWeakName(name); +} + +/// This links against a system library, exclusively using pkg-config to find the library. +/// Prefer to use `linkSystemLibrary` instead. +pub fn linkSystemLibraryPkgConfigOnly(self: *CompileStep, lib_name: []const u8) void { + self.main_module.linkSystemLibraryPkgConfigOnly(lib_name); +} + +/// This links against a system library, exclusively using pkg-config to find the library. +/// Prefer to use `linkSystemLibraryNeeded` instead. +pub fn linkSystemLibraryNeededPkgConfigOnly(self: *CompileStep, lib_name: []const u8) void { + self.main_module.linkSystemLibraryNeededPkgConfigOnly(lib_name); +} + +/// Handy when you have many C/C++ source files and want them all to have the same flags. +pub fn addCSourceFiles(self: *CompileStep, files: []const []const u8, flags: []const []const u8) void { + self.main_module.addCSourceFiles(files, flags); +} + +pub fn addCSourceFile(self: *CompileStep, file: []const u8, flags: []const []const u8) void { + self.main_module.addCSourceFile(file, flags); +} + +pub fn addCSourceFileSource(self: *CompileStep, source: CSourceFile) void { + self.main_module.addCSourceFileSource(source); +} + +pub fn installHeader(self: *CompileStep, src_path: []const u8, dest_rel_path: []const u8) void { + self.main_module.installHeader(src_path, dest_rel_path); +} + +pub fn installConfigHeader( + self: *CompileStep, + config_header: *ConfigHeaderStep, + options: InstallConfigHeaderOptions, +) void { + self.main_module.installConfigHeader(config_header, options); +} + +pub fn installHeadersDirectory( + self: *CompileStep, + src_dir_path: []const u8, + dest_rel_path: []const u8, +) void { + self.main_module.installHeadersDirectory(src_dir_path, dest_rel_path); +} + +pub fn installHeadersDirectoryOptions( + self: *CompileStep, + options: std.Build.InstallDirStep.Options, +) void { + self.main_module.installHeadersDirectoryOptions(options); +} + +pub fn installLibraryHeaders(self: *CompileStep, l: *CompileStep) void { + self.main_module.installLibraryHeaders(l); +} + +/// If the value is omitted, it is set to 1. +/// `name` and `value` need not live longer than the function call. +pub fn defineCMacro(self: *CompileStep, name: []const u8, value: ?[]const u8) void { + self.main_module.defineCMacro(name, value); +} + +/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1. +pub fn defineCMacroRaw(self: *CompileStep, name_and_value: []const u8) void { + self.main_module.defineCMacroRaw(name_and_value); +} + +pub fn linkLibrary(self: *CompileStep, lib: *CompileStep) void { + self.main_module.linkLibrary(lib); +} + +pub fn addAssemblyFile(self: *CompileStep, path: []const u8) void { + self.main_module.addAssemblyFile(path); +} + +pub fn addAssemblyFileSource(self: *CompileStep, source: FileSource) void { + self.main_module.addAssemblyFileSource(source); +} + +pub fn addObjectFile(self: *CompileStep, source_file: []const u8) void { + self.main_module.addObjectFile(source_file); +} + +pub fn addObjectFileSource(self: *CompileStep, source: FileSource) void { + self.main_module.addObjectFileSource(source); +} + +pub fn addObject(self: *CompileStep, obj: *CompileStep) void { + self.main_module.addObject(obj); +} + +pub fn linkSystemLibrary(self: *CompileStep, name: []const u8) void { + self.main_module.linkSystemLibrary(name); +} + +pub fn linkSystemLibraryNeeded(self: *CompileStep, name: []const u8) void { + self.main_module.linkSystemLibraryNeeded(name); +} + +pub fn linkSystemLibraryWeak(self: *CompileStep, name: []const u8) void { + self.main_module.linkSystemLibraryWeak(name); +} + +/// Returns whether the module depends on a particular system library. +pub fn dependsOnSystemLibrary(self: CompileStep, name: []const u8) bool { + self.main_module.dependsOnSystemLibrary(name); +} + +pub fn linkFramework(self: *CompileStep, framework_name: []const u8) void { + self.main_module.linkFramework(framework_name); +} + +pub fn linkFrameworkNeeded(self: *CompileStep, framework_name: []const u8) void { + self.main_module.linkFrameworkNeeded(framework_name); +} + +pub fn linkFrameworkWeak(self: *CompileStep, framework_name: []const u8) void { + self.main_module.linkFrameworkWeak(framework_name); +} + +pub fn addSystemIncludePath(self: *CompileStep, path: []const u8) void { + self.main_module.addSystemIncludePath(path); +} + +pub fn addIncludePath(self: *CompileStep, path: []const u8) void { + self.main_module.addIncludePath(path); +} + +pub fn addConfigHeader(self: *CompileStep, config_header: *ConfigHeaderStep) void { + self.main_module.addConfigHeader(config_header); +} + +pub fn addLibraryPath(self: *CompileStep, path: []const u8) void { + self.main_module.addLibraryPath(path); +} + +pub fn addLibraryPathDirectorySource(self: *CompileStep, directory_source: FileSource) void { + self.main_module.addLibraryPathDirectorySource(directory_source); +} + +pub fn addRPath(self: *CompileStep, path: []const u8) void { + self.main_module.addRPath(path); +} + +pub fn addRPathDirectorySource(self: *CompileStep, directory_source: FileSource) void { + self.main_module.addRPathDirectorySource(directory_source); +} + +pub fn addFrameworkPath(self: *CompileStep, dir_path: []const u8) void { + self.main_module.addFrameworkPath(dir_path); +} + +pub fn addFrameworkPathDirectorySource(self: *CompileStep, directory_source: FileSource) void { + self.main_module.addFrameworkPathDirectorySource(directory_source); +} + pub fn appendModuleArgs( self: *CompileStep, zig_args: *ArrayList([]const u8), diff --git a/lib/std/Build/TranslateCStep.zig b/lib/std/Build/TranslateCStep.zig index f2dc23d95074..81637cd9062c 100644 --- a/lib/std/Build/TranslateCStep.zig +++ b/lib/std/Build/TranslateCStep.zig @@ -57,8 +57,11 @@ pub const AddExecutableOptions = struct { /// Creates a step to build an executable from the translated source. pub fn addExecutable(self: *TranslateCStep, options: AddExecutableOptions) *CompileStep { + const output_module = self.step.owner.createModule(.{ + .source_file = .{ .generated = &self.output_file }, + }); return self.step.owner.addExecutable(.{ - .root_source_file = .{ .generated = &self.output_file }, + .main_module = output_module, .name = options.name orelse "translated_c", .version = options.version, .target = options.target orelse self.target, diff --git a/test/cases/compile_errors/add_overflow_in_function_evaluation.zig b/test/cases/compile_errors/add_overflow_in_function_evaluation.zig index 2f8a4678d365..ef1a01671161 100644 --- a/test/cases/compile_errors/add_overflow_in_function_evaluation.zig +++ b/test/cases/compile_errors/add_overflow_in_function_evaluation.zig @@ -3,7 +3,9 @@ fn add(a: u16, b: u16) u16 { return a + b; } -export fn entry() usize { return @sizeOf(@TypeOf(y)); } +export fn entry() usize { + return @sizeOf(@TypeOf(y)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/addition_with_non_numbers.zig b/test/cases/compile_errors/addition_with_non_numbers.zig index 3687df914f25..dd139c171db9 100644 --- a/test/cases/compile_errors/addition_with_non_numbers.zig +++ b/test/cases/compile_errors/addition_with_non_numbers.zig @@ -1,9 +1,11 @@ const Foo = struct { field: i32, }; -const x = Foo {.field = 1} + Foo {.field = 2}; +const x = Foo{ .field = 1 } + Foo{ .field = 2 }; -export fn entry() usize { return @sizeOf(@TypeOf(x)); } +export fn entry() usize { + return @sizeOf(@TypeOf(x)); +} // error // backend=llvm diff --git a/test/cases/compile_errors/address_of_number_literal.zig b/test/cases/compile_errors/address_of_number_literal.zig index c6b41a149558..9d3fe312a749 100644 --- a/test/cases/compile_errors/address_of_number_literal.zig +++ b/test/cases/compile_errors/address_of_number_literal.zig @@ -1,7 +1,11 @@ const x = 3; const y = &x; -fn foo() *const i32 { return y; } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +fn foo() *const i32 { + return y; +} +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/alignment_of_enum_field_specified.zig b/test/cases/compile_errors/alignment_of_enum_field_specified.zig index 14ffa6f499a2..056a64ab35e8 100644 --- a/test/cases/compile_errors/alignment_of_enum_field_specified.zig +++ b/test/cases/compile_errors/alignment_of_enum_field_specified.zig @@ -1,7 +1,4 @@ -const Number = enum { - a, - b align(i32), -}; +const Number = enum { a, b align(i32) }; export fn entry1() void { var x: Number = undefined; _ = x; diff --git a/test/cases/compile_errors/array_access_of_non_array.zig b/test/cases/compile_errors/array_access_of_non_array.zig index 06fa1569e660..9f9155c45a33 100644 --- a/test/cases/compile_errors/array_access_of_non_array.zig +++ b/test/cases/compile_errors/array_access_of_non_array.zig @@ -1,9 +1,9 @@ export fn f() void { - var bad : bool = undefined; + var bad: bool = undefined; bad[0] = bad[0]; } export fn g() void { - var bad : bool = undefined; + var bad: bool = undefined; _ = bad[0]; } diff --git a/test/cases/compile_errors/array_concatenation_with_wrong_type.zig b/test/cases/compile_errors/array_concatenation_with_wrong_type.zig index 6f2648f74b2e..5c634eceb098 100644 --- a/test/cases/compile_errors/array_concatenation_with_wrong_type.zig +++ b/test/cases/compile_errors/array_concatenation_with_wrong_type.zig @@ -2,7 +2,9 @@ const src = "aoeu"; const derp: usize = 1234; const a = derp ++ "foo"; -export fn entry() usize { return @sizeOf(@TypeOf(a)); } +export fn entry() usize { + return @sizeOf(@TypeOf(a)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/array_mult_with_number_type.zig b/test/cases/compile_errors/array_mult_with_number_type.zig index d3ec870f4e86..bd47c3b56f7b 100644 --- a/test/cases/compile_errors/array_mult_with_number_type.zig +++ b/test/cases/compile_errors/array_mult_with_number_type.zig @@ -7,4 +7,4 @@ export fn entry(base: f32, exponent: f32) f32 { // target=native // // :2:12: error: expected indexable; found 'f32' -// :2:17: note: this operator multiplies arrays; use std.math.pow for exponentiation \ No newline at end of file +// :2:17: note: this operator multiplies arrays; use std.math.pow for exponentiation diff --git a/test/cases/compile_errors/assign_inline_fn_to_non-comptime_var.zig b/test/cases/compile_errors/assign_inline_fn_to_non-comptime_var.zig index 857123eae55d..b16cb8f66e63 100644 --- a/test/cases/compile_errors/assign_inline_fn_to_non-comptime_var.zig +++ b/test/cases/compile_errors/assign_inline_fn_to_non-comptime_var.zig @@ -2,7 +2,7 @@ export fn entry() void { var a = &b; _ = a; } -fn b() callconv(.Inline) void { } +inline fn b() void {} // error // backend=stage2 diff --git a/test/cases/compile_errors/assign_null_to_non-optional_pointer.zig b/test/cases/compile_errors/assign_null_to_non-optional_pointer.zig index ca36dbfbfbdf..cc410e62289a 100644 --- a/test/cases/compile_errors/assign_null_to_non-optional_pointer.zig +++ b/test/cases/compile_errors/assign_null_to_non-optional_pointer.zig @@ -1,6 +1,8 @@ const a: *u8 = null; -export fn entry() usize { return @sizeOf(@TypeOf(a)); } +export fn entry() usize { + return @sizeOf(@TypeOf(a)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/assign_through_constant_pointer.zig b/test/cases/compile_errors/assign_through_constant_pointer.zig index 674d0875aa6a..3e8f72d5eea6 100644 --- a/test/cases/compile_errors/assign_through_constant_pointer.zig +++ b/test/cases/compile_errors/assign_through_constant_pointer.zig @@ -1,6 +1,6 @@ export fn f() void { - var cstr = "Hat"; - cstr[0] = 'W'; + var cstr = "Hat"; + cstr[0] = 'W'; } // error diff --git a/test/cases/compile_errors/assign_through_constant_slice.zig b/test/cases/compile_errors/assign_through_constant_slice.zig index 08910b624838..83102d302d42 100644 --- a/test/cases/compile_errors/assign_through_constant_slice.zig +++ b/test/cases/compile_errors/assign_through_constant_slice.zig @@ -1,6 +1,6 @@ export fn f() void { - var cstr: []const u8 = "Hat"; - cstr[0] = 'W'; + var cstr: []const u8 = "Hat"; + cstr[0] = 'W'; } // error diff --git a/test/cases/compile_errors/assign_to_constant_field.zig b/test/cases/compile_errors/assign_to_constant_field.zig index 33f08344c4ca..c64ee9ad291e 100644 --- a/test/cases/compile_errors/assign_to_constant_field.zig +++ b/test/cases/compile_errors/assign_to_constant_field.zig @@ -2,7 +2,9 @@ const Foo = struct { field: i32, }; export fn derp() void { - const f = Foo {.field = 1234,}; + const f = Foo{ + .field = 1234, + }; f.field = 0; } diff --git a/test/cases/compile_errors/async/non_async_function_pointer_passed_to_asyncCall.zig b/test/cases/compile_errors/async/non_async_function_pointer_passed_to_asyncCall.zig index d8ab4087e0a3..82cbb4469aaf 100644 --- a/test/cases/compile_errors/async/non_async_function_pointer_passed_to_asyncCall.zig +++ b/test/cases/compile_errors/async/non_async_function_pointer_passed_to_asyncCall.zig @@ -3,7 +3,7 @@ export fn entry() void { var bytes: [100]u8 align(16) = undefined; _ = @asyncCall(&bytes, {}, ptr, .{}); } -fn afunc() void { } +fn afunc() void {} // error // backend=stage1 diff --git a/test/cases/compile_errors/async/prevent_bad_implicit_casting_of_anyframe_types.zig b/test/cases/compile_errors/async/prevent_bad_implicit_casting_of_anyframe_types.zig index 066bf1c10787..bdf7bec45816 100644 --- a/test/cases/compile_errors/async/prevent_bad_implicit_casting_of_anyframe_types.zig +++ b/test/cases/compile_errors/async/prevent_bad_implicit_casting_of_anyframe_types.zig @@ -21,4 +21,4 @@ fn func() void {} // // :3:28: error: expected type 'anyframe->i32', found 'anyframe' // :8:28: error: expected type 'anyframe->i32', found 'i32' -// tmp.zig:13:29: error: expected type 'anyframe->i32', found '*@Frame(func)' \ No newline at end of file +// tmp.zig:13:29: error: expected type 'anyframe->i32', found '*@Frame(func)' diff --git a/test/cases/compile_errors/async/runtime-known_function_called_with_async_keyword.zig b/test/cases/compile_errors/async/runtime-known_function_called_with_async_keyword.zig index c66d0f9cbb55..bfc22cca255d 100644 --- a/test/cases/compile_errors/async/runtime-known_function_called_with_async_keyword.zig +++ b/test/cases/compile_errors/async/runtime-known_function_called_with_async_keyword.zig @@ -3,7 +3,7 @@ export fn entry() void { _ = async ptr(); } -fn afunc() callconv(.Async) void { } +fn afunc() callconv(.Async) void {} // error // backend=stage1 diff --git a/test/cases/compile_errors/bad_import.zig b/test/cases/compile_errors/bad_import.zig index e624d7104c68..521331e345e5 100644 --- a/test/cases/compile_errors/bad_import.zig +++ b/test/cases/compile_errors/bad_import.zig @@ -1,4 +1,6 @@ -const bogus = @import("bogus-does-not-exist.zig",); +const bogus = @import( + "bogus-does-not-exist.zig", +); // error // backend=stage2 diff --git a/test/cases/compile_errors/bad_usage_of_call.zig b/test/cases/compile_errors/bad_usage_of_call.zig index c0b632bef660..2350b19ce353 100644 --- a/test/cases/compile_errors/bad_usage_of_call.zig +++ b/test/cases/compile_errors/bad_usage_of_call.zig @@ -19,7 +19,7 @@ pub export fn entry() void { @call(.always_inline, call_me, .{}); } fn foo() void {} -fn bar() callconv(.Inline) void {} +inline fn bar() void {} fn baz1() void {} fn baz2() void {} @@ -33,4 +33,3 @@ fn baz2() void {} // :11:5: error: no-inline call of inline function // :15:26: error: modifier 'compile_time' requires a comptime-known function // :19:27: error: modifier 'always_inline' requires a comptime-known function - diff --git a/test/cases/compile_errors/binary_not_on_number_literal.zig b/test/cases/compile_errors/binary_not_on_number_literal.zig index cb57ca9ee151..455b79130a59 100644 --- a/test/cases/compile_errors/binary_not_on_number_literal.zig +++ b/test/cases/compile_errors/binary_not_on_number_literal.zig @@ -2,7 +2,9 @@ const TINY_QUANTUM_SHIFT = 4; const TINY_QUANTUM_SIZE = 1 << TINY_QUANTUM_SHIFT; var block_aligned_stuff: usize = (4 + TINY_QUANTUM_SIZE) & ~(TINY_QUANTUM_SIZE - 1); -export fn entry() usize { return @sizeOf(@TypeOf(block_aligned_stuff)); } +export fn entry() usize { + return @sizeOf(@TypeOf(block_aligned_stuff)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/bogus_compile_var.zig b/test/cases/compile_errors/bogus_compile_var.zig index be222e539349..4780423caecf 100644 --- a/test/cases/compile_errors/bogus_compile_var.zig +++ b/test/cases/compile_errors/bogus_compile_var.zig @@ -1,5 +1,7 @@ const x = @import("builtin").bogus; -export fn entry() usize { return @sizeOf(@TypeOf(x)); } +export fn entry() usize { + return @sizeOf(@TypeOf(x)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/bogus_method_call_on_slice.zig b/test/cases/compile_errors/bogus_method_call_on_slice.zig index ed18f43f48bf..76d0d90644f0 100644 --- a/test/cases/compile_errors/bogus_method_call_on_slice.zig +++ b/test/cases/compile_errors/bogus_method_call_on_slice.zig @@ -2,7 +2,9 @@ var self = "aoeu"; fn f(m: []const u8) void { m.copy(u8, self[0..], m); } -export fn entry() usize { return @sizeOf(@TypeOf(&f)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&f)); +} pub export fn entry1() void { .{}.bar(); } diff --git a/test/cases/compile_errors/branch_on_undefined_value.zig b/test/cases/compile_errors/branch_on_undefined_value.zig index a0a3be83cfa4..fc30fe7d3c93 100644 --- a/test/cases/compile_errors/branch_on_undefined_value.zig +++ b/test/cases/compile_errors/branch_on_undefined_value.zig @@ -1,6 +1,8 @@ const x = if (undefined) true else false; -export fn entry() usize { return @sizeOf(@TypeOf(x)); } +export fn entry() usize { + return @sizeOf(@TypeOf(x)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/calling_function_with_naked_calling_convention.zig b/test/cases/compile_errors/calling_function_with_naked_calling_convention.zig index 54bf58542542..2bd0364d4f8e 100644 --- a/test/cases/compile_errors/calling_function_with_naked_calling_convention.zig +++ b/test/cases/compile_errors/calling_function_with_naked_calling_convention.zig @@ -1,7 +1,7 @@ export fn entry() void { foo(); } -fn foo() callconv(.Naked) void { } +fn foo() callconv(.Naked) void {} // error // backend=llvm diff --git a/test/cases/compile_errors/calling_var_args_extern_function_passing_array_instead_of_pointer.zig b/test/cases/compile_errors/calling_var_args_extern_function_passing_array_instead_of_pointer.zig index c9e2e2e5eb82..3b9e1ca8eb64 100644 --- a/test/cases/compile_errors/calling_var_args_extern_function_passing_array_instead_of_pointer.zig +++ b/test/cases/compile_errors/calling_var_args_extern_function_passing_array_instead_of_pointer.zig @@ -1,5 +1,7 @@ export fn entry() void { - foo("hello".*,); + foo( + "hello".*, + ); } pub extern fn foo(format: *const u8, ...) void; diff --git a/test/cases/compile_errors/cast_unreachable.zig b/test/cases/compile_errors/cast_unreachable.zig index cf2331ff7e5f..39da655d7fe1 100644 --- a/test/cases/compile_errors/cast_unreachable.zig +++ b/test/cases/compile_errors/cast_unreachable.zig @@ -1,7 +1,9 @@ fn f() i32 { return @as(i32, return 1); } -export fn entry() void { _ = f(); } +export fn entry() void { + _ = f(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/casting_bit_offset_pointer_to_regular_pointer.zig b/test/cases/compile_errors/casting_bit_offset_pointer_to_regular_pointer.zig index f9df19802ade..8cf62c8ec0d8 100644 --- a/test/cases/compile_errors/casting_bit_offset_pointer_to_regular_pointer.zig +++ b/test/cases/compile_errors/casting_bit_offset_pointer_to_regular_pointer.zig @@ -12,7 +12,9 @@ fn bar(x: *const u3) u3 { return x.*; } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/colliding_invalid_top_level_functions.zig b/test/cases/compile_errors/colliding_invalid_top_level_functions.zig index ee0711088d92..8a2adb2f0778 100644 --- a/test/cases/compile_errors/colliding_invalid_top_level_functions.zig +++ b/test/cases/compile_errors/colliding_invalid_top_level_functions.zig @@ -1,6 +1,8 @@ fn func() bogus {} fn func() bogus {} -export fn entry() usize { return @sizeOf(@TypeOf(func)); } +export fn entry() usize { + return @sizeOf(@TypeOf(func)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/compileError_shows_traceback_of_references_that_caused_it.zig b/test/cases/compile_errors/compileError_shows_traceback_of_references_that_caused_it.zig index 092cbbd40034..d5206671fba8 100644 --- a/test/cases/compile_errors/compileError_shows_traceback_of_references_that_caused_it.zig +++ b/test/cases/compile_errors/compileError_shows_traceback_of_references_that_caused_it.zig @@ -1,4 +1,6 @@ -const foo = @compileError("aoeu",); +const foo = @compileError( + "aoeu", +); const bar = baz + foo; const baz = 1; diff --git a/test/cases/compile_errors/compileLog_of_tagged_enum_doesnt_crash_the_compiler.zig b/test/cases/compile_errors/compileLog_of_tagged_enum_doesnt_crash_the_compiler.zig index 55676f923064..9f4515d9b44c 100644 --- a/test/cases/compile_errors/compileLog_of_tagged_enum_doesnt_crash_the_compiler.zig +++ b/test/cases/compile_errors/compileLog_of_tagged_enum_doesnt_crash_the_compiler.zig @@ -1,13 +1,11 @@ -const Bar = union(enum(u32)) { - X: i32 = 1 -}; +const Bar = union(enum(u32)) { X: i32 = 1 }; fn testCompileLog(x: Bar) void { @compileLog(x); } pub export fn entry() void { - comptime testCompileLog(Bar{.X = 123}); + comptime testCompileLog(Bar{ .X = 123 }); } // error diff --git a/test/cases/compile_errors/compile_log.zig b/test/cases/compile_errors/compile_log.zig index e1ea460dc352..35e0639cede0 100644 --- a/test/cases/compile_errors/compile_log.zig +++ b/test/cases/compile_errors/compile_log.zig @@ -1,10 +1,17 @@ export fn foo() void { - comptime bar(12, "hi",); + comptime bar( + 12, + "hi", + ); } fn bar(a: i32, b: []const u8) void { - @compileLog("begin",); + @compileLog( + "begin", + ); @compileLog("a", a, "b", b); - @compileLog("end",); + @compileLog( + "end", + ); } export fn baz() void { const S = struct { a: u32 }; diff --git a/test/cases/compile_errors/compile_log_statement_warning_deduplication_in_generic_fn.zig b/test/cases/compile_errors/compile_log_statement_warning_deduplication_in_generic_fn.zig index 4b31d9924a5c..6f1ebcf889b7 100644 --- a/test/cases/compile_errors/compile_log_statement_warning_deduplication_in_generic_fn.zig +++ b/test/cases/compile_errors/compile_log_statement_warning_deduplication_in_generic_fn.zig @@ -4,7 +4,9 @@ export fn entry() void { } fn inner(comptime n: usize) void { comptime var i = 0; - inline while (i < n) : (i += 1) { @compileLog("!@#$"); } + inline while (i < n) : (i += 1) { + @compileLog("!@#$"); + } } // error diff --git a/test/cases/compile_errors/compile_time_division_by_zero.zig b/test/cases/compile_errors/compile_time_division_by_zero.zig index 281ccf28a1d4..8954ace9ab1e 100644 --- a/test/cases/compile_errors/compile_time_division_by_zero.zig +++ b/test/cases/compile_errors/compile_time_division_by_zero.zig @@ -3,7 +3,9 @@ fn foo(x: u32) u32 { return 1 / x; } -export fn entry() usize { return @sizeOf(@TypeOf(y)); } +export fn entry() usize { + return @sizeOf(@TypeOf(y)); +} // error // backend=llvm diff --git a/test/cases/compile_errors/comptime_if_inside_runtime_for.zig b/test/cases/compile_errors/comptime_if_inside_runtime_for.zig index 6200776d1892..1902058fc525 100644 --- a/test/cases/compile_errors/comptime_if_inside_runtime_for.zig +++ b/test/cases/compile_errors/comptime_if_inside_runtime_for.zig @@ -1,9 +1,9 @@ export fn entry() void { - var x: u32 = 0; - for(0..1, 1..2) |_, _| { - var y = x + if(x == 0) 1 else 0; - _ = y; - } + var x: u32 = 0; + for (0..1, 1..2) |_, _| { + var y = x + if (x == 0) 1 else 0; + _ = y; + } } // error diff --git a/test/cases/compile_errors/constant_inside_comptime_function_has_compile_error.zig b/test/cases/compile_errors/constant_inside_comptime_function_has_compile_error.zig index 2b67390b05b2..1ce744d6d7e0 100644 --- a/test/cases/compile_errors/constant_inside_comptime_function_has_compile_error.zig +++ b/test/cases/compile_errors/constant_inside_comptime_function_has_compile_error.zig @@ -1,7 +1,9 @@ const ContextAllocator = MemoryPool(usize); pub fn MemoryPool(comptime T: type) type { - const free_list_t = @compileError("aoeu",); + const free_list_t = @compileError( + "aoeu", + ); _ = T; return struct { diff --git a/test/cases/compile_errors/container_init_with_non-type.zig b/test/cases/compile_errors/container_init_with_non-type.zig index aa62be6dc5a0..7bdf07a1cefe 100644 --- a/test/cases/compile_errors/container_init_with_non-type.zig +++ b/test/cases/compile_errors/container_init_with_non-type.zig @@ -1,7 +1,9 @@ const zero: i32 = 0; const a = zero{1}; -export fn entry() usize { return @sizeOf(@TypeOf(a)); } +export fn entry() usize { + return @sizeOf(@TypeOf(a)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/control_flow_uses_comptime_var_at_runtime.zig b/test/cases/compile_errors/control_flow_uses_comptime_var_at_runtime.zig index a8058e8c758d..5283239af6a7 100644 --- a/test/cases/compile_errors/control_flow_uses_comptime_var_at_runtime.zig +++ b/test/cases/compile_errors/control_flow_uses_comptime_var_at_runtime.zig @@ -5,7 +5,7 @@ export fn foo() void { } } -fn bar() void { } +fn bar() void {} export fn baz() void { comptime var idx: u32 = 0; while (idx < 1) { diff --git a/test/cases/compile_errors/dereference_an_array.zig b/test/cases/compile_errors/dereference_an_array.zig index f5aabf081c5a..27e4d81e5550 100644 --- a/test/cases/compile_errors/dereference_an_array.zig +++ b/test/cases/compile_errors/dereference_an_array.zig @@ -5,7 +5,9 @@ pub fn pass(in: []u8) []u8 { return out.*[0..1]; } -export fn entry() usize { return @sizeOf(@TypeOf(&pass)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&pass)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/direct_struct_loop.zig b/test/cases/compile_errors/direct_struct_loop.zig index 0abc1a4f73b0..6009471fc011 100644 --- a/test/cases/compile_errors/direct_struct_loop.zig +++ b/test/cases/compile_errors/direct_struct_loop.zig @@ -1,5 +1,9 @@ -const A = struct { a : A, }; -export fn entry() usize { return @sizeOf(A); } +const A = struct { + a: A, +}; +export fn entry() usize { + return @sizeOf(A); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/disallow_coercion_from_non-null-terminated_pointer_to_null-terminated_pointer.zig b/test/cases/compile_errors/disallow_coercion_from_non-null-terminated_pointer_to_null-terminated_pointer.zig index 3670def4eec8..45fa4c14f5e8 100644 --- a/test/cases/compile_errors/disallow_coercion_from_non-null-terminated_pointer_to_null-terminated_pointer.zig +++ b/test/cases/compile_errors/disallow_coercion_from_non-null-terminated_pointer_to_null-terminated_pointer.zig @@ -1,6 +1,6 @@ extern fn puts(s: [*:0]const u8) c_int; pub export fn entry() void { - const no_zero_array = [_]u8{'h', 'e', 'l', 'l', 'o'}; + const no_zero_array = [_]u8{ 'h', 'e', 'l', 'l', 'o' }; const no_zero_ptr: [*]const u8 = &no_zero_array; _ = puts(no_zero_ptr); } diff --git a/test/cases/compile_errors/division_by_zero.zig b/test/cases/compile_errors/division_by_zero.zig index 2e2f7e2be2a7..3019554fb83a 100644 --- a/test/cases/compile_errors/division_by_zero.zig +++ b/test/cases/compile_errors/division_by_zero.zig @@ -3,10 +3,18 @@ const lit_float_x = 1.0 / 0.0; const int_x = @as(u32, 1) / @as(u32, 0); const float_x = @as(f32, 1.0) / @as(f32, 0.0); -export fn entry1() usize { return @sizeOf(@TypeOf(lit_int_x)); } -export fn entry2() usize { return @sizeOf(@TypeOf(lit_float_x)); } -export fn entry3() usize { return @sizeOf(@TypeOf(int_x)); } -export fn entry4() usize { return @sizeOf(@TypeOf(float_x)); } // no error on purpose +export fn entry1() usize { + return @sizeOf(@TypeOf(lit_int_x)); +} +export fn entry2() usize { + return @sizeOf(@TypeOf(lit_float_x)); +} +export fn entry3() usize { + return @sizeOf(@TypeOf(int_x)); +} +export fn entry4() usize { + return @sizeOf(@TypeOf(float_x)); +} // no error on purpose // error // backend=stage2 diff --git a/test/cases/compile_errors/duplicate-unused_labels.zig b/test/cases/compile_errors/duplicate-unused_labels.zig index 4bfc6c596012..8fd220b4bc17 100644 --- a/test/cases/compile_errors/duplicate-unused_labels.zig +++ b/test/cases/compile_errors/duplicate-unused_labels.zig @@ -1,20 +1,26 @@ comptime { - blk: { blk: while (false) {} } + blk: { + blk: while (false) {} + } } comptime { - blk: while (false) { blk: for (@as([0]void, undefined)) |_| {} } + blk: while (false) { + blk: for (@as([0]void, undefined)) |_| {} + } } comptime { - blk: for (@as([0]void, undefined)) |_| { blk: {} } + blk: for (@as([0]void, undefined)) |_| { + blk: {} + } } comptime { blk: {} } comptime { - blk: while(false) {} + blk: while (false) {} } comptime { - blk: for(@as([0]void, undefined)) |_| {} + blk: for (@as([0]void, undefined)) |_| {} } // error diff --git a/test/cases/compile_errors/duplicate_error_value_in_error_set.zig b/test/cases/compile_errors/duplicate_error_value_in_error_set.zig index 5e9cddb975d8..927537d952ce 100644 --- a/test/cases/compile_errors/duplicate_error_value_in_error_set.zig +++ b/test/cases/compile_errors/duplicate_error_value_in_error_set.zig @@ -1,4 +1,4 @@ -const Foo = error { +const Foo = error{ Bar, Bar, }; diff --git a/test/cases/compile_errors/duplicate_field_in_struct_value_expression.zig b/test/cases/compile_errors/duplicate_field_in_struct_value_expression.zig index fa5bc6fb4e31..eda001c086cb 100644 --- a/test/cases/compile_errors/duplicate_field_in_struct_value_expression.zig +++ b/test/cases/compile_errors/duplicate_field_in_struct_value_expression.zig @@ -1,10 +1,10 @@ const A = struct { - x : i32, - y : i32, - z : i32, + x: i32, + y: i32, + z: i32, }; export fn f() void { - const a = A { + const a = A{ .z = 1, .y = 2, .x = 3, diff --git a/test/cases/compile_errors/embedFile_with_bogus_file.zig b/test/cases/compile_errors/embedFile_with_bogus_file.zig index fa05d1ed4909..232f9721cc83 100644 --- a/test/cases/compile_errors/embedFile_with_bogus_file.zig +++ b/test/cases/compile_errors/embedFile_with_bogus_file.zig @@ -1,6 +1,10 @@ -const resource = @embedFile("bogus.txt",); +const resource = @embedFile( + "bogus.txt", +); -export fn entry() usize { return @sizeOf(@TypeOf(resource)); } +export fn entry() usize { + return @sizeOf(@TypeOf(resource)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/empty_switch_on_an_integer.zig b/test/cases/compile_errors/empty_switch_on_an_integer.zig index de4d7e9c652d..6ba69f8d3e87 100644 --- a/test/cases/compile_errors/empty_switch_on_an_integer.zig +++ b/test/cases/compile_errors/empty_switch_on_an_integer.zig @@ -1,6 +1,6 @@ export fn entry() void { var x: u32 = 0; - switch(x) {} + switch (x) {} } // error diff --git a/test/cases/compile_errors/enum_with_declarations_unavailable_for_reify_type.zig b/test/cases/compile_errors/enum_with_declarations_unavailable_for_reify_type.zig index f23718c2ca57..fb55a733e522 100644 --- a/test/cases/compile_errors/enum_with_declarations_unavailable_for_reify_type.zig +++ b/test/cases/compile_errors/enum_with_declarations_unavailable_for_reify_type.zig @@ -1,5 +1,8 @@ export fn entry() void { - _ = @Type(@typeInfo(enum { foo, const bar = 1; })); + _ = @Type(@typeInfo(enum { + foo, + const bar = 1; + })); } // error diff --git a/test/cases/compile_errors/error_not_handled_in_switch.zig b/test/cases/compile_errors/error_not_handled_in_switch.zig index 8f0d26a4a92b..04f122b11d24 100644 --- a/test/cases/compile_errors/error_not_handled_in_switch.zig +++ b/test/cases/compile_errors/error_not_handled_in_switch.zig @@ -5,9 +5,9 @@ export fn entry() void { } fn foo(x: i32) !void { switch (x) { - 0 ... 10 => return error.Foo, - 11 ... 20 => return error.Bar, - 21 ... 30 => return error.Baz, + 0...10 => return error.Foo, + 11...20 => return error.Bar, + 21...30 => return error.Baz, else => {}, } } diff --git a/test/cases/compile_errors/error_note_for_function_parameter_incompatibility.zig b/test/cases/compile_errors/error_note_for_function_parameter_incompatibility.zig index 76543697a42d..3fb9cc61bdf9 100644 --- a/test/cases/compile_errors/error_note_for_function_parameter_incompatibility.zig +++ b/test/cases/compile_errors/error_note_for_function_parameter_incompatibility.zig @@ -1,5 +1,9 @@ -fn do_the_thing(func: *const fn (arg: i32) void) void { _ = func; } -fn bar(arg: bool) void { _ = arg; } +fn do_the_thing(func: *const fn (arg: i32) void) void { + _ = func; +} +fn bar(arg: bool) void { + _ = arg; +} export fn entry() void { do_the_thing(bar); } diff --git a/test/cases/compile_errors/explicit_error_set_cast_known_at_comptime_violates_error_sets.zig b/test/cases/compile_errors/explicit_error_set_cast_known_at_comptime_violates_error_sets.zig index 22fc965769e5..1d029f09dbe2 100644 --- a/test/cases/compile_errors/explicit_error_set_cast_known_at_comptime_violates_error_sets.zig +++ b/test/cases/compile_errors/explicit_error_set_cast_known_at_comptime_violates_error_sets.zig @@ -1,5 +1,5 @@ -const Set1 = error {A, B}; -const Set2 = error {A, C}; +const Set1 = error{ A, B }; +const Set2 = error{ A, C }; comptime { var x = Set1.B; var y = @errSetCast(Set2, x); diff --git a/test/cases/compile_errors/export_function_with_comptime_parameter.zig b/test/cases/compile_errors/export_function_with_comptime_parameter.zig index 4491a98e9cb0..8d5dbef1c36c 100644 --- a/test/cases/compile_errors/export_function_with_comptime_parameter.zig +++ b/test/cases/compile_errors/export_function_with_comptime_parameter.zig @@ -1,4 +1,4 @@ -export fn foo(comptime x: anytype, y: i32) i32{ +export fn foo(comptime x: anytype, y: i32) i32 { return x + y; } diff --git a/test/cases/compile_errors/export_with_empty_name_string.zig b/test/cases/compile_errors/export_with_empty_name_string.zig index f199c2632c05..9f3b215bc216 100644 --- a/test/cases/compile_errors/export_with_empty_name_string.zig +++ b/test/cases/compile_errors/export_with_empty_name_string.zig @@ -1,4 +1,4 @@ -pub export fn entry() void { } +pub export fn entry() void {} comptime { @export(entry, .{ .name = "" }); } diff --git a/test/cases/compile_errors/extern_function_pointer_mismatch.zig b/test/cases/compile_errors/extern_function_pointer_mismatch.zig index f10a3dbdb3b9..383784f8f294 100644 --- a/test/cases/compile_errors/extern_function_pointer_mismatch.zig +++ b/test/cases/compile_errors/extern_function_pointer_mismatch.zig @@ -1,9 +1,17 @@ -const fns = [_](fn(i32)i32) { a, b, c }; -pub fn a(x: i32) i32 {return x + 0;} -pub fn b(x: i32) i32 {return x + 1;} -export fn c(x: i32) i32 {return x + 2;} +const fns = [_](fn (i32) i32){ a, b, c }; +pub fn a(x: i32) i32 { + return x + 0; +} +pub fn b(x: i32) i32 { + return x + 1; +} +export fn c(x: i32) i32 { + return x + 2; +} -export fn entry() usize { return @sizeOf(@TypeOf(fns)); } +export fn entry() usize { + return @sizeOf(@TypeOf(fns)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/extern_function_with_comptime_parameter.zig b/test/cases/compile_errors/extern_function_with_comptime_parameter.zig index 58f15f7fab53..2b27082503f5 100644 --- a/test/cases/compile_errors/extern_function_with_comptime_parameter.zig +++ b/test/cases/compile_errors/extern_function_with_comptime_parameter.zig @@ -4,9 +4,15 @@ fn f() i32 { } pub extern fn entry1(b: u32, comptime a: [2]u8, c: i32) void; pub extern fn entry2(b: u32, noalias a: anytype, i43) void; -comptime { _ = f; } -comptime { _ = entry1; } -comptime { _ = entry2; } +comptime { + _ = f; +} +comptime { + _ = entry1; +} +comptime { + _ = entry2; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/extern_struct_with_extern-compatible_but_inferred_integer_tag_type.zig b/test/cases/compile_errors/extern_struct_with_extern-compatible_but_inferred_integer_tag_type.zig index 6484e301a9cc..897d226c4d08 100644 --- a/test/cases/compile_errors/extern_struct_with_extern-compatible_but_inferred_integer_tag_type.zig +++ b/test/cases/compile_errors/extern_struct_with_extern-compatible_but_inferred_integer_tag_type.zig @@ -1,32 +1,4 @@ -pub const E = enum { -@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", -@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23", -@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34", -@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42",@"43",@"44",@"45", -@"46",@"47",@"48",@"49",@"50",@"51",@"52",@"53",@"54",@"55",@"56", -@"57",@"58",@"59",@"60",@"61",@"62",@"63",@"64",@"65",@"66",@"67", -@"68",@"69",@"70",@"71",@"72",@"73",@"74",@"75",@"76",@"77",@"78", -@"79",@"80",@"81",@"82",@"83",@"84",@"85",@"86",@"87",@"88",@"89", -@"90",@"91",@"92",@"93",@"94",@"95",@"96",@"97",@"98",@"99",@"100", -@"101",@"102",@"103",@"104",@"105",@"106",@"107",@"108",@"109", -@"110",@"111",@"112",@"113",@"114",@"115",@"116",@"117",@"118", -@"119",@"120",@"121",@"122",@"123",@"124",@"125",@"126",@"127", -@"128",@"129",@"130",@"131",@"132",@"133",@"134",@"135",@"136", -@"137",@"138",@"139",@"140",@"141",@"142",@"143",@"144",@"145", -@"146",@"147",@"148",@"149",@"150",@"151",@"152",@"153",@"154", -@"155",@"156",@"157",@"158",@"159",@"160",@"161",@"162",@"163", -@"164",@"165",@"166",@"167",@"168",@"169",@"170",@"171",@"172", -@"173",@"174",@"175",@"176",@"177",@"178",@"179",@"180",@"181", -@"182",@"183",@"184",@"185",@"186",@"187",@"188",@"189",@"190", -@"191",@"192",@"193",@"194",@"195",@"196",@"197",@"198",@"199", -@"200",@"201",@"202",@"203",@"204",@"205",@"206",@"207",@"208", -@"209",@"210",@"211",@"212",@"213",@"214",@"215",@"216",@"217", -@"218",@"219",@"220",@"221",@"222",@"223",@"224",@"225",@"226", -@"227",@"228",@"229",@"230",@"231",@"232",@"233",@"234",@"235", -@"236",@"237",@"238",@"239",@"240",@"241",@"242",@"243",@"244", -@"245",@"246",@"247",@"248",@"249",@"250",@"251",@"252",@"253", -@"254",@"255", @"256" -}; +pub const E = enum { @"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39", @"40", @"41", @"42", @"43", @"44", @"45", @"46", @"47", @"48", @"49", @"50", @"51", @"52", @"53", @"54", @"55", @"56", @"57", @"58", @"59", @"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"69", @"70", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79", @"80", @"81", @"82", @"83", @"84", @"85", @"86", @"87", @"88", @"89", @"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99", @"100", @"101", @"102", @"103", @"104", @"105", @"106", @"107", @"108", @"109", @"110", @"111", @"112", @"113", @"114", @"115", @"116", @"117", @"118", @"119", @"120", @"121", @"122", @"123", @"124", @"125", @"126", @"127", @"128", @"129", @"130", @"131", @"132", @"133", @"134", @"135", @"136", @"137", @"138", @"139", @"140", @"141", @"142", @"143", @"144", @"145", @"146", @"147", @"148", @"149", @"150", @"151", @"152", @"153", @"154", @"155", @"156", @"157", @"158", @"159", @"160", @"161", @"162", @"163", @"164", @"165", @"166", @"167", @"168", @"169", @"170", @"171", @"172", @"173", @"174", @"175", @"176", @"177", @"178", @"179", @"180", @"181", @"182", @"183", @"184", @"185", @"186", @"187", @"188", @"189", @"190", @"191", @"192", @"193", @"194", @"195", @"196", @"197", @"198", @"199", @"200", @"201", @"202", @"203", @"204", @"205", @"206", @"207", @"208", @"209", @"210", @"211", @"212", @"213", @"214", @"215", @"216", @"217", @"218", @"219", @"220", @"221", @"222", @"223", @"224", @"225", @"226", @"227", @"228", @"229", @"230", @"231", @"232", @"233", @"234", @"235", @"236", @"237", @"238", @"239", @"240", @"241", @"242", @"243", @"244", @"245", @"246", @"247", @"248", @"249", @"250", @"251", @"252", @"253", @"254", @"255", @"256" }; pub const S = extern struct { e: E, }; diff --git a/test/cases/compile_errors/extern_union_field_missing_type.zig b/test/cases/compile_errors/extern_union_field_missing_type.zig index 6890e65714c4..fde58f69e5e2 100644 --- a/test/cases/compile_errors/extern_union_field_missing_type.zig +++ b/test/cases/compile_errors/extern_union_field_missing_type.zig @@ -2,7 +2,7 @@ const Letter = extern union { A, }; export fn entry() void { - var a = Letter { .A = {} }; + var a = Letter{ .A = {} }; _ = a; } diff --git a/test/cases/compile_errors/extern_union_given_enum_tag_type.zig b/test/cases/compile_errors/extern_union_given_enum_tag_type.zig index 6a691eb2e2f5..4aa0e623c796 100644 --- a/test/cases/compile_errors/extern_union_given_enum_tag_type.zig +++ b/test/cases/compile_errors/extern_union_given_enum_tag_type.zig @@ -9,7 +9,7 @@ const Payload = extern union(Letter) { C: bool, }; export fn entry() void { - var a = Payload { .A = 1234 }; + var a = Payload{ .A = 1234 }; _ = a; } diff --git a/test/cases/compile_errors/fieldParentPtr-comptime_field_ptr_not_based_on_struct.zig b/test/cases/compile_errors/fieldParentPtr-comptime_field_ptr_not_based_on_struct.zig index 7f57268f0645..ea21ad6095d7 100644 --- a/test/cases/compile_errors/fieldParentPtr-comptime_field_ptr_not_based_on_struct.zig +++ b/test/cases/compile_errors/fieldParentPtr-comptime_field_ptr_not_based_on_struct.zig @@ -2,7 +2,10 @@ const Foo = struct { a: i32, b: i32, }; -const foo = Foo { .a = 1, .b = 2, }; +const foo = Foo{ + .a = 1, + .b = 2, +}; comptime { const field_ptr = @intToPtr(*i32, 0x1234); diff --git a/test/cases/compile_errors/fieldParentPtr-comptime_wrong_field_index.zig b/test/cases/compile_errors/fieldParentPtr-comptime_wrong_field_index.zig index a73409aea336..d912ed99436c 100644 --- a/test/cases/compile_errors/fieldParentPtr-comptime_wrong_field_index.zig +++ b/test/cases/compile_errors/fieldParentPtr-comptime_wrong_field_index.zig @@ -2,7 +2,10 @@ const Foo = struct { a: i32, b: i32, }; -const foo = Foo { .a = 1, .b = 2, }; +const foo = Foo{ + .a = 1, + .b = 2, +}; comptime { const another_foo_ptr = @fieldParentPtr(Foo, "b", &foo.a); diff --git a/test/cases/compile_errors/for.zig b/test/cases/compile_errors/for.zig index 5bd3aa0c646f..c9f995487802 100644 --- a/test/cases/compile_errors/for.zig +++ b/test/cases/compile_errors/for.zig @@ -1,13 +1,15 @@ export fn a() void { for (0..10, 10..21) |i, j| { - _ = i; _ = j; + _ = i; + _ = j; } } export fn b() void { const s1 = "hello"; const s2 = true; for (s1, s2) |i, j| { - _ = i; _ = j; + _ = i; + _ = j; } } export fn c() void { @@ -20,7 +22,9 @@ export fn d() void { const x: [*]const u8 = "hello"; const y: [*]const u8 = "world"; for (x, 0.., y) |x1, x2, x3| { - _ = x1; _ = x2; _ = x3; + _ = x1; + _ = x2; + _ = x3; } } diff --git a/test/cases/compile_errors/for_extra_capture.zig b/test/cases/compile_errors/for_extra_capture.zig index a137b57d5152..33267d4c4b9a 100644 --- a/test/cases/compile_errors/for_extra_capture.zig +++ b/test/cases/compile_errors/for_extra_capture.zig @@ -1,6 +1,7 @@ export fn b() void { - for (0..10) |i, j| { - _ = i; _ = j; + for (0..10, 0..) |i, j| { + _ = i; + _ = j; } } diff --git a/test/cases/compile_errors/function_alignment_non_power_of_2.zig b/test/cases/compile_errors/function_alignment_non_power_of_2.zig index 11d6768dfd0c..e40ca022d2c1 100644 --- a/test/cases/compile_errors/function_alignment_non_power_of_2.zig +++ b/test/cases/compile_errors/function_alignment_non_power_of_2.zig @@ -1,5 +1,7 @@ extern fn foo() align(3) void; -export fn entry() void { return foo(); } +export fn entry() void { + return foo(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/function_call_assigned_to_incorrect_type.zig b/test/cases/compile_errors/function_call_assigned_to_incorrect_type.zig index fcbfabe2978a..1060987b9a19 100644 --- a/test/cases/compile_errors/function_call_assigned_to_incorrect_type.zig +++ b/test/cases/compile_errors/function_call_assigned_to_incorrect_type.zig @@ -3,7 +3,7 @@ export fn entry() void { arr = concat(); } fn concat() [16]f32 { - return [1]f32{0}**16; + return [1]f32{0} ** 16; } // error diff --git a/test/cases/compile_errors/function_parameter_is_opaque.zig b/test/cases/compile_errors/function_parameter_is_opaque.zig index 57c89bd7f47f..4f5d7f94fc29 100644 --- a/test/cases/compile_errors/function_parameter_is_opaque.zig +++ b/test/cases/compile_errors/function_parameter_is_opaque.zig @@ -9,12 +9,16 @@ export fn entry2() void { _ = someFuncPtr; } -fn foo(p: FooType) void {_ = p;} +fn foo(p: FooType) void { + _ = p; +} export fn entry3() void { _ = foo; } -fn bar(p: @TypeOf(null)) void {_ = p;} +fn bar(p: @TypeOf(null)) void { + _ = p; +} export fn entry4() void { _ = bar; } diff --git a/test/cases/compile_errors/function_with_non-extern_non-packed_enum_parameter.zig b/test/cases/compile_errors/function_with_non-extern_non-packed_enum_parameter.zig index 572378d09322..3945c5b0f01f 100644 --- a/test/cases/compile_errors/function_with_non-extern_non-packed_enum_parameter.zig +++ b/test/cases/compile_errors/function_with_non-extern_non-packed_enum_parameter.zig @@ -1,5 +1,7 @@ const Foo = enum { A, B, C }; -export fn entry(foo: Foo) void { _ = foo; } +export fn entry(foo: Foo) void { + _ = foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig b/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig index 55ee27764112..137037f9e747 100644 --- a/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig +++ b/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig @@ -3,7 +3,9 @@ const Foo = struct { B: f32, C: bool, }; -export fn entry(foo: Foo) void { _ = foo; } +export fn entry(foo: Foo) void { + _ = foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig b/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig index f848392c9092..d651329f72f8 100644 --- a/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig +++ b/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig @@ -3,7 +3,9 @@ const Foo = union { B: f32, C: bool, }; -export fn entry(foo: Foo) void { _ = foo; } +export fn entry(foo: Foo) void { + _ = foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/generic_function_call_assigned_to_incorrect_type.zig b/test/cases/compile_errors/generic_function_call_assigned_to_incorrect_type.zig index a2e303670d6d..4ff284128284 100644 --- a/test/cases/compile_errors/generic_function_call_assigned_to_incorrect_type.zig +++ b/test/cases/compile_errors/generic_function_call_assigned_to_incorrect_type.zig @@ -2,7 +2,7 @@ pub export fn entry() void { var res: []i32 = undefined; res = myAlloc(i32); } -fn myAlloc(comptime arg: type) anyerror!arg{ +fn myAlloc(comptime arg: type) anyerror!arg { unreachable; } diff --git a/test/cases/compile_errors/generic_function_instance_with_non-constant_expression.zig b/test/cases/compile_errors/generic_function_instance_with_non-constant_expression.zig index 1317c4376a83..7ec82ee46daa 100644 --- a/test/cases/compile_errors/generic_function_instance_with_non-constant_expression.zig +++ b/test/cases/compile_errors/generic_function_instance_with_non-constant_expression.zig @@ -1,9 +1,13 @@ -fn foo(comptime x: i32, y: i32) i32 { return x + y; } +fn foo(comptime x: i32, y: i32) i32 { + return x + y; +} fn test1(a: i32, b: i32) i32 { return foo(a, b); } -export fn entry() usize { return @sizeOf(@TypeOf(&test1)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&test1)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/generic_instantiation_failure_in_generic_function_return_type.zig b/test/cases/compile_errors/generic_instantiation_failure_in_generic_function_return_type.zig index 3146c3860403..c730f80e6f42 100644 --- a/test/cases/compile_errors/generic_instantiation_failure_in_generic_function_return_type.zig +++ b/test/cases/compile_errors/generic_instantiation_failure_in_generic_function_return_type.zig @@ -6,7 +6,6 @@ pub export fn entry() void { } fn sliceAsBytes(slice: anytype) std.meta.trait.isPtrTo(.Array)(@TypeOf(slice)) {} - // error // backend=llvm // target=native diff --git a/test/cases/compile_errors/global_variable_alignment_non_power_of_2.zig b/test/cases/compile_errors/global_variable_alignment_non_power_of_2.zig index b88d8aaf701c..5110e41334b8 100644 --- a/test/cases/compile_errors/global_variable_alignment_non_power_of_2.zig +++ b/test/cases/compile_errors/global_variable_alignment_non_power_of_2.zig @@ -1,5 +1,7 @@ const some_data: [100]u8 align(3) = undefined; -export fn entry() usize { return @sizeOf(@TypeOf(some_data)); } +export fn entry() usize { + return @sizeOf(@TypeOf(some_data)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig b/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig index e2694343e825..a87f62839660 100644 --- a/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig +++ b/test/cases/compile_errors/global_variable_initializer_must_be_constant_expression.zig @@ -1,6 +1,8 @@ extern fn foo() i32; const x = foo(); -export fn entry() i32 { return x; } +export fn entry() i32 { + return x; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/ignored_assert-err-ok_return_value.zig b/test/cases/compile_errors/ignored_assert-err-ok_return_value.zig index 39657badd479..1257636622da 100644 --- a/test/cases/compile_errors/ignored_assert-err-ok_return_value.zig +++ b/test/cases/compile_errors/ignored_assert-err-ok_return_value.zig @@ -1,7 +1,9 @@ export fn foo() void { bar() catch unreachable; } -fn bar() anyerror!i32 { return 0; } +fn bar() anyerror!i32 { + return 0; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/ignored_comptime_statement_value.zig b/test/cases/compile_errors/ignored_comptime_statement_value.zig index fc6cdfdd28fe..99b43dea0016 100644 --- a/test/cases/compile_errors/ignored_comptime_statement_value.zig +++ b/test/cases/compile_errors/ignored_comptime_statement_value.zig @@ -1,5 +1,7 @@ export fn foo() void { - comptime {1;} + comptime { + 1; + } } // error diff --git a/test/cases/compile_errors/ignored_deferred_function_call.zig b/test/cases/compile_errors/ignored_deferred_function_call.zig index b318baa16cb5..9537255d3304 100644 --- a/test/cases/compile_errors/ignored_deferred_function_call.zig +++ b/test/cases/compile_errors/ignored_deferred_function_call.zig @@ -1,7 +1,9 @@ export fn foo() void { defer bar(); } -fn bar() anyerror!i32 { return 0; } +fn bar() anyerror!i32 { + return 0; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/ignored_deferred_statement_value.zig b/test/cases/compile_errors/ignored_deferred_statement_value.zig index 9a270497afc9..1319414e5974 100644 --- a/test/cases/compile_errors/ignored_deferred_statement_value.zig +++ b/test/cases/compile_errors/ignored_deferred_statement_value.zig @@ -1,5 +1,7 @@ export fn foo() void { - defer {1;} + defer { + 1; + } } // error diff --git a/test/cases/compile_errors/ignored_return_value.zig b/test/cases/compile_errors/ignored_return_value.zig index 57f859e3d375..08424c4fe9fc 100644 --- a/test/cases/compile_errors/ignored_return_value.zig +++ b/test/cases/compile_errors/ignored_return_value.zig @@ -1,7 +1,9 @@ export fn foo() void { bar(); } -fn bar() i32 { return 0; } +fn bar() i32 { + return 0; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/illegal_comparison_of_types.zig b/test/cases/compile_errors/illegal_comparison_of_types.zig index 69d7a28fa44f..5720aa05cc87 100644 --- a/test/cases/compile_errors/illegal_comparison_of_types.zig +++ b/test/cases/compile_errors/illegal_comparison_of_types.zig @@ -9,8 +9,12 @@ fn bad_eql_2(a: *const EnumWithData, b: *const EnumWithData) bool { return a.* == b.*; } -export fn entry1() usize { return @sizeOf(@TypeOf(&bad_eql_1)); } -export fn entry2() usize { return @sizeOf(@TypeOf(&bad_eql_2)); } +export fn entry1() usize { + return @sizeOf(@TypeOf(&bad_eql_1)); +} +export fn entry2() usize { + return @sizeOf(@TypeOf(&bad_eql_2)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/implicit_cast_from_array_to_mutable_slice.zig b/test/cases/compile_errors/implicit_cast_from_array_to_mutable_slice.zig index e7ae5d72770a..74e52f7a0b03 100644 --- a/test/cases/compile_errors/implicit_cast_from_array_to_mutable_slice.zig +++ b/test/cases/compile_errors/implicit_cast_from_array_to_mutable_slice.zig @@ -1,5 +1,7 @@ var global_array: [10]i32 = undefined; -fn foo(param: []i32) void {_ = param;} +fn foo(param: []i32) void { + _ = param; +} export fn entry() void { foo(global_array); } diff --git a/test/cases/compile_errors/implicit_cast_of_error_set_not_a_subset.zig b/test/cases/compile_errors/implicit_cast_of_error_set_not_a_subset.zig index 0a182343b99a..2874fdf1d19d 100644 --- a/test/cases/compile_errors/implicit_cast_of_error_set_not_a_subset.zig +++ b/test/cases/compile_errors/implicit_cast_of_error_set_not_a_subset.zig @@ -1,5 +1,5 @@ -const Set1 = error{A, B}; -const Set2 = error{A, C}; +const Set1 = error{ A, B }; +const Set2 = error{ A, C }; export fn entry() void { foo(Set1.B); } diff --git a/test/cases/compile_errors/implicitly_increasing_pointer_alignment.zig b/test/cases/compile_errors/implicitly_increasing_pointer_alignment.zig index 13adba1b9140..fcecfa861117 100644 --- a/test/cases/compile_errors/implicitly_increasing_pointer_alignment.zig +++ b/test/cases/compile_errors/implicitly_increasing_pointer_alignment.zig @@ -4,7 +4,7 @@ const Foo = packed struct { }; export fn entry() void { - var foo = Foo { .a = 1, .b = 10 }; + var foo = Foo{ .a = 1, .b = 10 }; bar(&foo.b); } diff --git a/test/cases/compile_errors/implicitly_increasing_slice_alignment.zig b/test/cases/compile_errors/implicitly_increasing_slice_alignment.zig index 84ec6464f4ad..171936097dc9 100644 --- a/test/cases/compile_errors/implicitly_increasing_slice_alignment.zig +++ b/test/cases/compile_errors/implicitly_increasing_slice_alignment.zig @@ -4,7 +4,7 @@ const Foo = packed struct { }; export fn entry() void { - var foo = Foo { .a = 1, .b = 10 }; + var foo = Foo{ .a = 1, .b = 10 }; foo.b += 1; bar(@as(*[1]u32, &foo.b)[0..]); } diff --git a/test/cases/compile_errors/import_outside_package_path.zig b/test/cases/compile_errors/import_outside_package_path.zig index 0c0df594193e..34044e3b0fba 100644 --- a/test/cases/compile_errors/import_outside_package_path.zig +++ b/test/cases/compile_errors/import_outside_package_path.zig @@ -1,4 +1,4 @@ -comptime{ +comptime { _ = @import("../a.zig"); } diff --git a/test/cases/compile_errors/incorrect_return_type.zig b/test/cases/compile_errors/incorrect_return_type.zig index 57cf54a02347..f0fafed4c705 100644 --- a/test/cases/compile_errors/incorrect_return_type.zig +++ b/test/cases/compile_errors/incorrect_return_type.zig @@ -1,18 +1,18 @@ - pub export fn entry() void{ - _ = foo(); - } - const A = struct { - a: u32, - }; - fn foo() A { - return bar(); - } - const B = struct { - a: u32, - }; - fn bar() B { - unreachable; - } +pub export fn entry() void { + _ = foo(); +} +const A = struct { + a: u32, +}; +fn foo() A { + return bar(); +} +const B = struct { + a: u32, +}; +fn bar() B { + unreachable; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/increase_pointer_alignment_in_ptrCast.zig b/test/cases/compile_errors/increase_pointer_alignment_in_ptrCast.zig index 242454e859c7..8d7e14acae89 100644 --- a/test/cases/compile_errors/increase_pointer_alignment_in_ptrCast.zig +++ b/test/cases/compile_errors/increase_pointer_alignment_in_ptrCast.zig @@ -1,5 +1,5 @@ export fn entry() u32 { - var bytes: [4]u8 = [_]u8{0x01, 0x02, 0x03, 0x04}; + var bytes: [4]u8 = [_]u8{ 0x01, 0x02, 0x03, 0x04 }; const ptr = @ptrCast(*u32, &bytes[0]); return ptr.*; } diff --git a/test/cases/compile_errors/indirect_struct_loop.zig b/test/cases/compile_errors/indirect_struct_loop.zig index dca2b9c3f65b..a1bb6ee41b4b 100644 --- a/test/cases/compile_errors/indirect_struct_loop.zig +++ b/test/cases/compile_errors/indirect_struct_loop.zig @@ -1,7 +1,15 @@ -const A = struct { b : B, }; -const B = struct { c : C, }; -const C = struct { a : A, }; -export fn entry() usize { return @sizeOf(A); } +const A = struct { + b: B, +}; +const B = struct { + c: C, +}; +const C = struct { + a: A, +}; +export fn entry() usize { + return @sizeOf(A); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/inferred_array_size_invalid_here.zig b/test/cases/compile_errors/inferred_array_size_invalid_here.zig index 084057dc8e6d..0791540ed2d5 100644 --- a/test/cases/compile_errors/inferred_array_size_invalid_here.zig +++ b/test/cases/compile_errors/inferred_array_size_invalid_here.zig @@ -4,7 +4,7 @@ export fn entry() void { } export fn entry2() void { const S = struct { a: *const [_]u8 }; - var a = .{ S{} }; + var a = .{S{}}; _ = a; } diff --git a/test/cases/compile_errors/inferring_error_set_of_function_pointer.zig b/test/cases/compile_errors/inferring_error_set_of_function_pointer.zig index 862e33906a6b..396ca9e834fb 100644 --- a/test/cases/compile_errors/inferring_error_set_of_function_pointer.zig +++ b/test/cases/compile_errors/inferring_error_set_of_function_pointer.zig @@ -1,5 +1,5 @@ comptime { - const z: ?fn()!void = null; + const z: ?fn () !void = null; } // error diff --git a/test/cases/compile_errors/integer_overflow_error.zig b/test/cases/compile_errors/integer_overflow_error.zig index aa4725b7e7ad..0720ede7c601 100644 --- a/test/cases/compile_errors/integer_overflow_error.zig +++ b/test/cases/compile_errors/integer_overflow_error.zig @@ -1,5 +1,7 @@ -const x : u8 = 300; -export fn entry() usize { return @sizeOf(@TypeOf(x)); } +const x: u8 = 300; +export fn entry() usize { + return @sizeOf(@TypeOf(x)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/invalid_builtin_fn.zig b/test/cases/compile_errors/invalid_builtin_fn.zig index 5b7b832177bf..3297525fd981 100644 --- a/test/cases/compile_errors/invalid_builtin_fn.zig +++ b/test/cases/compile_errors/invalid_builtin_fn.zig @@ -1,6 +1,7 @@ -fn f() @bogus(foo) { +fn f() @bogus(foo) {} +export fn entry() void { + _ = f(); } -export fn entry() void { _ = f(); } // error // backend=stage2 diff --git a/test/cases/compile_errors/invalid_capture_type.zig b/test/cases/compile_errors/invalid_capture_type.zig index 3813021c951b..fc97f3420952 100644 --- a/test/cases/compile_errors/invalid_capture_type.zig +++ b/test/cases/compile_errors/invalid_capture_type.zig @@ -1,5 +1,7 @@ export fn f1() void { - if (true) |x| { _ = x; } + if (true) |x| { + _ = x; + } } export fn f2() void { if (@as(usize, 5)) |_| {} diff --git a/test/cases/compile_errors/invalid_comparison_for_function_pointers.zig b/test/cases/compile_errors/invalid_comparison_for_function_pointers.zig index cd63c70259b3..3c76da2e38c0 100644 --- a/test/cases/compile_errors/invalid_comparison_for_function_pointers.zig +++ b/test/cases/compile_errors/invalid_comparison_for_function_pointers.zig @@ -1,7 +1,9 @@ fn foo() void {} const invalid = foo > foo; -export fn entry() usize { return @sizeOf(@TypeOf(invalid)); } +export fn entry() usize { + return @sizeOf(@TypeOf(invalid)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/invalid_field_access_in_comptime.zig b/test/cases/compile_errors/invalid_field_access_in_comptime.zig index 672c2b74c90b..ebcc525b2a13 100644 --- a/test/cases/compile_errors/invalid_field_access_in_comptime.zig +++ b/test/cases/compile_errors/invalid_field_access_in_comptime.zig @@ -1,4 +1,7 @@ -comptime { var x = doesnt_exist.whatever; _ = x; } +comptime { + var x = doesnt_exist.whatever; + _ = x; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/invalid_field_in_struct_value_expression.zig b/test/cases/compile_errors/invalid_field_in_struct_value_expression.zig index 97f440da3b3f..f1cd96d8e7cc 100644 --- a/test/cases/compile_errors/invalid_field_in_struct_value_expression.zig +++ b/test/cases/compile_errors/invalid_field_in_struct_value_expression.zig @@ -1,10 +1,10 @@ const A = struct { - x : i32, - y : i32, - z : i32, + x: i32, + y: i32, + z: i32, }; export fn f() void { - const a = A { + const a = A{ .z = 4, .y = 2, .foo = 42, @@ -21,7 +21,6 @@ pub export fn entry() void { dump(.{ .field_1 = 123, .field_3 = 456 }); } - // error // backend=stage2 // target=native diff --git a/test/cases/compile_errors/invalid_optional_type_in_extern_struct.zig b/test/cases/compile_errors/invalid_optional_type_in_extern_struct.zig index 10e140d881ff..c7d7b4233abe 100644 --- a/test/cases/compile_errors/invalid_optional_type_in_extern_struct.zig +++ b/test/cases/compile_errors/invalid_optional_type_in_extern_struct.zig @@ -1,7 +1,9 @@ const stroo = extern struct { moo: ?[*c]u8, }; -export fn testf(fluff: *stroo) void { _ = fluff; } +export fn testf(fluff: *stroo) void { + _ = fluff; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/invalid_pointer_with_reify_type.zig b/test/cases/compile_errors/invalid_pointer_with_reify_type.zig index cac9e3289440..dce09234d459 100644 --- a/test/cases/compile_errors/invalid_pointer_with_reify_type.zig +++ b/test/cases/compile_errors/invalid_pointer_with_reify_type.zig @@ -8,7 +8,7 @@ export fn entry() void { .child = u8, .is_allowzero = false, .sentinel = &@as(u8, 0), - }}); + } }); } // error diff --git a/test/cases/compile_errors/invalid_shift_amount_error.zig b/test/cases/compile_errors/invalid_shift_amount_error.zig index 49852b5b7d03..9ea4c55d2981 100644 --- a/test/cases/compile_errors/invalid_shift_amount_error.zig +++ b/test/cases/compile_errors/invalid_shift_amount_error.zig @@ -1,8 +1,10 @@ -const x : u8 = 2; +const x: u8 = 2; fn f() u16 { return x << 8; } -export fn entry() u16 { return f(); } +export fn entry() u16 { + return f(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/invalid_type.zig b/test/cases/compile_errors/invalid_type.zig index 902d3652e7a3..a7c7d8109422 100644 --- a/test/cases/compile_errors/invalid_type.zig +++ b/test/cases/compile_errors/invalid_type.zig @@ -1,5 +1,7 @@ fn a() bogus {} -export fn entry() void { _ = a(); } +export fn entry() void { + _ = a(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/invalid_variadic_function.zig b/test/cases/compile_errors/invalid_variadic_function.zig index 997db9fee8fa..7652cb329a08 100644 --- a/test/cases/compile_errors/invalid_variadic_function.zig +++ b/test/cases/compile_errors/invalid_variadic_function.zig @@ -1,8 +1,12 @@ fn foo(...) void {} fn bar(a: anytype, ...) callconv(a) void {} -comptime { _ = foo; } -comptime { _ = bar; } +comptime { + _ = foo; +} +comptime { + _ = bar; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig b/test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig index 874f015ffb93..92cd406d1c94 100644 --- a/test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig +++ b/test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig @@ -1,10 +1,10 @@ export fn foo1() void { - var bytes = [_]u8{1, 2}; + var bytes = [_]u8{ 1, 2 }; const word: u16 = @bitCast(u16, bytes[0..]); _ = word; } export fn foo2() void { - var bytes: []const u8 = &[_]u8{1, 2}; + var bytes: []const u8 = &[_]u8{ 1, 2 }; const word: u16 = @bitCast(u16, bytes); _ = word; } diff --git a/test/cases/compile_errors/local_variable_redeclaration.zig b/test/cases/compile_errors/local_variable_redeclaration.zig index a0861ada490d..5b81cd5fbc20 100644 --- a/test/cases/compile_errors/local_variable_redeclaration.zig +++ b/test/cases/compile_errors/local_variable_redeclaration.zig @@ -1,5 +1,5 @@ export fn f() void { - const a : i32 = 0; + const a: i32 = 0; var a = 0; } diff --git a/test/cases/compile_errors/local_variable_redeclares_parameter.zig b/test/cases/compile_errors/local_variable_redeclares_parameter.zig index 6e523c2c8f6d..f49b7f137e8d 100644 --- a/test/cases/compile_errors/local_variable_redeclares_parameter.zig +++ b/test/cases/compile_errors/local_variable_redeclares_parameter.zig @@ -1,7 +1,9 @@ -fn f(a : i32) void { +fn f(a: i32) void { const a = 0; } -export fn entry() void { f(1); } +export fn entry() void { + f(1); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/local_variable_shadowing_global.zig b/test/cases/compile_errors/local_variable_shadowing_global.zig index 91df6a7c3d81..e3f221d0c560 100644 --- a/test/cases/compile_errors/local_variable_shadowing_global.zig +++ b/test/cases/compile_errors/local_variable_shadowing_global.zig @@ -2,7 +2,7 @@ const Foo = struct {}; const Bar = struct {}; export fn entry() void { - var Bar : i32 = undefined; + var Bar: i32 = undefined; _ = Bar; } diff --git a/test/cases/compile_errors/main_function_with_bogus_args_type.zig b/test/cases/compile_errors/main_function_with_bogus_args_type.zig index dd02e1af34ac..f0322e0484f8 100644 --- a/test/cases/compile_errors/main_function_with_bogus_args_type.zig +++ b/test/cases/compile_errors/main_function_with_bogus_args_type.zig @@ -1,4 +1,6 @@ -pub fn main(args: [][]bogus) !void {_ = args;} +pub fn main(args: [][]bogus) !void { + _ = args; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/missing_const_in_slice_with_nested_array_type.zig b/test/cases/compile_errors/missing_const_in_slice_with_nested_array_type.zig index 1ef986935b73..2f596db1ed7e 100644 --- a/test/cases/compile_errors/missing_const_in_slice_with_nested_array_type.zig +++ b/test/cases/compile_errors/missing_const_in_slice_with_nested_array_type.zig @@ -2,7 +2,7 @@ const Geo3DTex2D = struct { vertices: [][2]f32 }; pub fn getGeo3DTex2D() Geo3DTex2D { return Geo3DTex2D{ .vertices = [_][2]f32{ - [_]f32{ -0.5, -0.5}, + [_]f32{ -0.5, -0.5 }, }, }; } diff --git a/test/cases/compile_errors/missing_else_clause.zig b/test/cases/compile_errors/missing_else_clause.zig index e96363b9cdc4..508a06ab585b 100644 --- a/test/cases/compile_errors/missing_else_clause.zig +++ b/test/cases/compile_errors/missing_else_clause.zig @@ -1,9 +1,13 @@ fn f(b: bool) void { - const x : i32 = if (b) h: { break :h 1; }; + const x: i32 = if (b) h: { + break :h 1; + }; _ = x; } fn g(b: bool) void { - const y = if (b) h: { break :h @as(i32, 1); }; + const y = if (b) h: { + break :h @as(i32, 1); + }; _ = y; } fn h() void { diff --git a/test/cases/compile_errors/missing_field_in_struct_value_expression.zig b/test/cases/compile_errors/missing_field_in_struct_value_expression.zig index 600540d1e043..3d19cb9db155 100644 --- a/test/cases/compile_errors/missing_field_in_struct_value_expression.zig +++ b/test/cases/compile_errors/missing_field_in_struct_value_expression.zig @@ -1,12 +1,12 @@ const A = struct { - x : i32, - y : i32, - z : i32, + x: i32, + y: i32, + z: i32, }; export fn f() void { // we want the error on the '{' not the 'A' because // the A could be a complicated expression - const a = A { + const a = A{ .z = 4, .y = 2, }; diff --git a/test/cases/compile_errors/missing_main_fn_in_executable.zig b/test/cases/compile_errors/missing_main_fn_in_executable.zig index 3c1ae631ac40..9f243356b779 100644 --- a/test/cases/compile_errors/missing_main_fn_in_executable.zig +++ b/test/cases/compile_errors/missing_main_fn_in_executable.zig @@ -1,5 +1,3 @@ - - // error // backend=llvm // target=x86_64-linux diff --git a/test/cases/compile_errors/missing_param_name.zig b/test/cases/compile_errors/missing_param_name.zig index 88da902ea2d2..1f679ea1d825 100644 --- a/test/cases/compile_errors/missing_param_name.zig +++ b/test/cases/compile_errors/missing_param_name.zig @@ -1,5 +1,7 @@ fn f(i32) void {} -export fn entry() usize { return @sizeOf(@TypeOf(f)); } +export fn entry() usize { + return @sizeOf(@TypeOf(f)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/misspelled_type_with_pointer_only_reference.zig b/test/cases/compile_errors/misspelled_type_with_pointer_only_reference.zig index ef8fce6c8086..ca8adade0198 100644 --- a/test/cases/compile_errors/misspelled_type_with_pointer_only_reference.zig +++ b/test/cases/compile_errors/misspelled_type_with_pointer_only_reference.zig @@ -24,11 +24,13 @@ pub const JsonNode = struct { fn foo() void { var jll: JasonList = undefined; jll.init(1234); - var jd = JsonNode {.kind = JsonType.JSONArray , .jobject = JsonOA.JSONArray {jll} }; + var jd = JsonNode{ .kind = JsonType.JSONArray, .jobject = JsonOA.JSONArray{jll} }; _ = jd; } -export fn entry() usize { return @sizeOf(@TypeOf(foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/mul_overflow_in_function_evaluation.zig b/test/cases/compile_errors/mul_overflow_in_function_evaluation.zig index 6be57f770eb8..c484df754083 100644 --- a/test/cases/compile_errors/mul_overflow_in_function_evaluation.zig +++ b/test/cases/compile_errors/mul_overflow_in_function_evaluation.zig @@ -3,7 +3,9 @@ fn mul(a: u16, b: u16) u16 { return a * b; } -export fn entry() usize { return @sizeOf(@TypeOf(&y)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&y)); +} // error // backend=stage2 @@ -11,4 +13,3 @@ export fn entry() usize { return @sizeOf(@TypeOf(&y)); } // // :3:14: error: overflow of integer type 'u16' with value '1800000' // :1:14: note: called from here - diff --git a/test/cases/compile_errors/multiple_function_definitions.zig b/test/cases/compile_errors/multiple_function_definitions.zig index d07eaee257e1..134daaeaa4f6 100644 --- a/test/cases/compile_errors/multiple_function_definitions.zig +++ b/test/cases/compile_errors/multiple_function_definitions.zig @@ -1,6 +1,8 @@ fn a() void {} fn a() void {} -export fn entry() void { a(); } +export fn entry() void { + a(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/negation_overflow_in_function_evaluation.zig b/test/cases/compile_errors/negation_overflow_in_function_evaluation.zig index abd8549fd20d..208f76100519 100644 --- a/test/cases/compile_errors/negation_overflow_in_function_evaluation.zig +++ b/test/cases/compile_errors/negation_overflow_in_function_evaluation.zig @@ -3,7 +3,9 @@ fn neg(x: i8) i8 { return -x; } -export fn entry() usize { return @sizeOf(@TypeOf(&y)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&y)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/nested_vectors.zig b/test/cases/compile_errors/nested_vectors.zig index 9482c2300522..5a9b3756583e 100644 --- a/test/cases/compile_errors/nested_vectors.zig +++ b/test/cases/compile_errors/nested_vectors.zig @@ -10,4 +10,3 @@ export fn entry() void { // target=native // // :3:16: error: expected integer, float, bool, or pointer for the vector element type; found '@Vector(4, u8)' - diff --git a/test/cases/compile_errors/noalias_on_non_pointer_param.zig b/test/cases/compile_errors/noalias_on_non_pointer_param.zig index 806808820f4c..e58170e2434c 100644 --- a/test/cases/compile_errors/noalias_on_non_pointer_param.zig +++ b/test/cases/compile_errors/noalias_on_non_pointer_param.zig @@ -1,11 +1,19 @@ -fn f(noalias x: i32) void { _ = x; } -export fn entry() void { f(1234); } +fn f(noalias x: i32) void { + _ = x; +} +export fn entry() void { + f(1234); +} -fn generic(comptime T: type, noalias _: [*]T, noalias _: [*]const T, _: usize) void {} -comptime { _ = generic; } +fn generic(comptime T: type, noalias _: [*]T, noalias _: [*]const T, _: usize) void {} +comptime { + _ = generic; +} -fn slice(noalias _: []u8) void {} -comptime { _ = slice; } +fn slice(noalias _: []u8) void {} +comptime { + _ = slice; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/non-comptime-parameter-used-as-array-size.zig b/test/cases/compile_errors/non-comptime-parameter-used-as-array-size.zig index de5a3830ebc5..f5eba9ee62a0 100644 --- a/test/cases/compile_errors/non-comptime-parameter-used-as-array-size.zig +++ b/test/cases/compile_errors/non-comptime-parameter-used-as-array-size.zig @@ -5,8 +5,7 @@ export fn entry() void { _ = llamas2; } -fn makeLlamas(count: usize) [count]u8 { -} +fn makeLlamas(count: usize) [count]u8 {} // error // target=native diff --git a/test/cases/compile_errors/non-const_expression_function_call_with_struct_return_value_outside_function.zig b/test/cases/compile_errors/non-const_expression_function_call_with_struct_return_value_outside_function.zig index 4acd0afb815a..d9bd0dd2b223 100644 --- a/test/cases/compile_errors/non-const_expression_function_call_with_struct_return_value_outside_function.zig +++ b/test/cases/compile_errors/non-const_expression_function_call_with_struct_return_value_outside_function.zig @@ -4,11 +4,13 @@ const Foo = struct { const a = get_it(); fn get_it() Foo { global_side_effect = true; - return Foo {.x = 13}; + return Foo{ .x = 13 }; } var global_side_effect = false; -export fn entry() usize { return @sizeOf(@TypeOf(a)); } +export fn entry() usize { + return @sizeOf(@TypeOf(a)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig b/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig index 2e0043c5ecec..c6d4e04fc1f7 100644 --- a/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig +++ b/test/cases/compile_errors/non-const_expression_in_struct_literal_outside_function.zig @@ -1,10 +1,12 @@ const Foo = struct { x: i32, }; -const a = Foo {.x = get_it()}; +const a = Foo{ .x = get_it() }; extern fn get_it() i32; -export fn entry() usize { return @sizeOf(@TypeOf(a)); } +export fn entry() usize { + return @sizeOf(@TypeOf(a)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig b/test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig index cf65131a1f1e..0b2e8585d9ef 100644 --- a/test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig +++ b/test/cases/compile_errors/non-const_variables_of_things_that_require_const_variables.zig @@ -1,30 +1,30 @@ export fn entry1() void { - var m2 = &2; - _ = m2; + var m2 = &2; + _ = m2; } export fn entry2() void { - var a = undefined; - _ = a; + var a = undefined; + _ = a; } export fn entry3() void { - var b = 1; - _ = b; + var b = 1; + _ = b; } export fn entry4() void { - var c = 1.0; - _ = c; + var c = 1.0; + _ = c; } export fn entry5() void { - var d = null; - _ = d; + var d = null; + _ = d; } export fn entry6(opaque_: *Opaque) void { - var e = opaque_.*; - _ = e; + var e = opaque_.*; + _ = e; } export fn entry7() void { - var f = i32; - _ = f; + var f = i32; + _ = f; } const Opaque = opaque {}; diff --git a/test/cases/compile_errors/non-exhaustive_enum_marker_assigned_a_value.zig b/test/cases/compile_errors/non-exhaustive_enum_marker_assigned_a_value.zig index c9ebb6af7142..831845722bfd 100644 --- a/test/cases/compile_errors/non-exhaustive_enum_marker_assigned_a_value.zig +++ b/test/cases/compile_errors/non-exhaustive_enum_marker_assigned_a_value.zig @@ -8,7 +8,10 @@ const B = enum { b, _, }; -comptime { _ = A; _ = B; } +comptime { + _ = A; + _ = B; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/non-inline_for_loop_on_a_type_that_requires_comptime.zig b/test/cases/compile_errors/non-inline_for_loop_on_a_type_that_requires_comptime.zig index ce72f912b87e..2bb3f84cb9d2 100644 --- a/test/cases/compile_errors/non-inline_for_loop_on_a_type_that_requires_comptime.zig +++ b/test/cases/compile_errors/non-inline_for_loop_on_a_type_that_requires_comptime.zig @@ -4,7 +4,9 @@ const Foo = struct { }; export fn entry() void { const xx: [2]Foo = .{ .{ .name = "", .T = u8 }, .{ .name = "", .T = u8 } }; - for (xx) |f| { _ = f;} + for (xx) |f| { + _ = f; + } } // error diff --git a/test/cases/compile_errors/non_constant_expression_in_array_size.zig b/test/cases/compile_errors/non_constant_expression_in_array_size.zig index 07facfa0f2c8..74d60ad3944d 100644 --- a/test/cases/compile_errors/non_constant_expression_in_array_size.zig +++ b/test/cases/compile_errors/non_constant_expression_in_array_size.zig @@ -2,9 +2,13 @@ const Foo = struct { y: [get()]u8, }; var global_var: usize = 1; -fn get() usize { return global_var; } +fn get() usize { + return global_var; +} -export fn entry() usize { return @offsetOf(Foo, "y"); } +export fn entry() usize { + return @offsetOf(Foo, "y"); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/offsetOf-bad_field_name.zig b/test/cases/compile_errors/offsetOf-bad_field_name.zig index eb04da3c6816..2c0fbe08b4ce 100644 --- a/test/cases/compile_errors/offsetOf-bad_field_name.zig +++ b/test/cases/compile_errors/offsetOf-bad_field_name.zig @@ -2,7 +2,10 @@ const Foo = struct { derp: i32, }; export fn foo() usize { - return @offsetOf(Foo, "a",); + return @offsetOf( + Foo, + "a", + ); } // error diff --git a/test/cases/compile_errors/offsetOf-non_struct.zig b/test/cases/compile_errors/offsetOf-non_struct.zig index 45e9cf9518fb..2a2705f15df3 100644 --- a/test/cases/compile_errors/offsetOf-non_struct.zig +++ b/test/cases/compile_errors/offsetOf-non_struct.zig @@ -1,6 +1,9 @@ const Foo = i32; export fn foo() usize { - return @offsetOf(Foo, "a",); + return @offsetOf( + Foo, + "a", + ); } // error diff --git a/test/cases/compile_errors/old_fn_ptr_in_extern_context.zig b/test/cases/compile_errors/old_fn_ptr_in_extern_context.zig index 4f957a827cc7..7983ed8ec88b 100644 --- a/test/cases/compile_errors/old_fn_ptr_in_extern_context.zig +++ b/test/cases/compile_errors/old_fn_ptr_in_extern_context.zig @@ -5,7 +5,7 @@ comptime { _ = @sizeOf(S) == 1; } comptime { - _ = [*c][4]fn() callconv(.C) void; + _ = [*c][4]fn () callconv(.C) void; } // error diff --git a/test/cases/compile_errors/overflow_in_enum_value_allocation.zig b/test/cases/compile_errors/overflow_in_enum_value_allocation.zig index 2a5b55e86da1..821ac6c2563e 100644 --- a/test/cases/compile_errors/overflow_in_enum_value_allocation.zig +++ b/test/cases/compile_errors/overflow_in_enum_value_allocation.zig @@ -3,8 +3,8 @@ const Moo = enum(u8) { Over, }; pub export fn entry() void { - var y = Moo.Last; - _ = y; + var y = Moo.Last; + _ = y; } // error diff --git a/test/cases/compile_errors/packed_union_given_enum_tag_type.zig b/test/cases/compile_errors/packed_union_given_enum_tag_type.zig index 03aaef0d8cd5..2e69afd0a9ec 100644 --- a/test/cases/compile_errors/packed_union_given_enum_tag_type.zig +++ b/test/cases/compile_errors/packed_union_given_enum_tag_type.zig @@ -9,7 +9,7 @@ const Payload = packed union(Letter) { C: bool, }; export fn entry() void { - var a = Payload { .A = 1234 }; + var a = Payload{ .A = 1234 }; _ = a; } diff --git a/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig b/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig index 0db9d83dfbc7..26d224de8594 100644 --- a/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig +++ b/test/cases/compile_errors/packed_union_with_automatic_layout_field.zig @@ -7,7 +7,7 @@ const Payload = packed union { B: bool, }; export fn entry() void { - var a = Payload { .B = true }; + var a = Payload{ .B = true }; _ = a; } diff --git a/test/cases/compile_errors/panic_called_at_compile_time.zig b/test/cases/compile_errors/panic_called_at_compile_time.zig index 220161930bc4..8198cd8e5d31 100644 --- a/test/cases/compile_errors/panic_called_at_compile_time.zig +++ b/test/cases/compile_errors/panic_called_at_compile_time.zig @@ -1,6 +1,8 @@ export fn entry() void { comptime { - @panic("aoeu",); + @panic( + "aoeu", + ); } } diff --git a/test/cases/compile_errors/parameter_redeclaration.zig b/test/cases/compile_errors/parameter_redeclaration.zig index 89c7c4bd2aa8..1c6ec1a4393e 100644 --- a/test/cases/compile_errors/parameter_redeclaration.zig +++ b/test/cases/compile_errors/parameter_redeclaration.zig @@ -1,6 +1,7 @@ -fn f(a : i32, a : i32) void { +fn f(a: i32, a: i32) void {} +export fn entry() void { + f(1, 2); } -export fn entry() void { f(1, 2); } // error // backend=stage2 diff --git a/test/cases/compile_errors/pass_const_ptr_to_mutable_ptr_fn.zig b/test/cases/compile_errors/pass_const_ptr_to_mutable_ptr_fn.zig index fd24b58f5563..ce39eba4f34a 100644 --- a/test/cases/compile_errors/pass_const_ptr_to_mutable_ptr_fn.zig +++ b/test/cases/compile_errors/pass_const_ptr_to_mutable_ptr_fn.zig @@ -1,14 +1,20 @@ fn foo() bool { - const a = @as([]const u8, "a",); + const a = @as( + []const u8, + "a", + ); const b = &a; return ptrEql(b, b); } fn ptrEql(a: *[]const u8, b: *[]const u8) bool { - _ = a; _ = b; + _ = a; + _ = b; return true; } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/passing_an_under-aligned_function_pointer.zig b/test/cases/compile_errors/passing_an_under-aligned_function_pointer.zig index 3e3500e71fc7..6d3e2e871d14 100644 --- a/test/cases/compile_errors/passing_an_under-aligned_function_pointer.zig +++ b/test/cases/compile_errors/passing_an_under-aligned_function_pointer.zig @@ -4,7 +4,9 @@ export fn entry() void { fn testImplicitlyDecreaseFnAlign(ptr: *const fn () align(8) i32, answer: i32) void { if (ptr() != answer) unreachable; } -fn alignedSmall() align(4) i32 { return 1234; } +fn alignedSmall() align(4) i32 { + return 1234; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/pointer_to_noreturn.zig b/test/cases/compile_errors/pointer_to_noreturn.zig index 0891fd36997f..5d757f631b46 100644 --- a/test/cases/compile_errors/pointer_to_noreturn.zig +++ b/test/cases/compile_errors/pointer_to_noreturn.zig @@ -1,5 +1,7 @@ fn a() *noreturn {} -export fn entry() void { _ = a(); } +export fn entry() void { + _ = a(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/range_operator_in_switch_used_on_error_set.zig b/test/cases/compile_errors/range_operator_in_switch_used_on_error_set.zig index 859197929c6f..697582228fe4 100644 --- a/test/cases/compile_errors/range_operator_in_switch_used_on_error_set.zig +++ b/test/cases/compile_errors/range_operator_in_switch_used_on_error_set.zig @@ -1,13 +1,13 @@ export fn entry() void { foo(452) catch |err| switch (err) { - error.Foo ... error.Bar => {}, + error.Foo...error.Bar => {}, else => {}, }; } fn foo(x: i32) !void { switch (x) { - 0 ... 10 => return error.Foo, - 11 ... 20 => return error.Bar, + 0...10 => return error.Foo, + 11...20 => return error.Bar, else => {}, } } diff --git a/test/cases/compile_errors/reassign_to_array_parameter.zig b/test/cases/compile_errors/reassign_to_array_parameter.zig index 492732997065..380fd6215436 100644 --- a/test/cases/compile_errors/reassign_to_array_parameter.zig +++ b/test/cases/compile_errors/reassign_to_array_parameter.zig @@ -1,8 +1,8 @@ fn reassign(a: [3]f32) void { - a = [3]f32{4, 5, 6}; + a = [3]f32{ 4, 5, 6 }; } export fn entry() void { - reassign(.{1, 2, 3}); + reassign(.{ 1, 2, 3 }); } // error diff --git a/test/cases/compile_errors/reassign_to_struct_parameter.zig b/test/cases/compile_errors/reassign_to_struct_parameter.zig index 963448f8fe0e..560de215b532 100644 --- a/test/cases/compile_errors/reassign_to_struct_parameter.zig +++ b/test/cases/compile_errors/reassign_to_struct_parameter.zig @@ -2,10 +2,10 @@ const S = struct { x: u32, }; fn reassign(s: S) void { - s = S{.x = 2}; + s = S{ .x = 2 }; } export fn entry() void { - reassign(S{.x = 3}); + reassign(S{ .x = 3 }); } // error diff --git a/test/cases/compile_errors/redefinition_of_enums.zig b/test/cases/compile_errors/redefinition_of_enums.zig index 641211872e12..34d5efe8df86 100644 --- a/test/cases/compile_errors/redefinition_of_enums.zig +++ b/test/cases/compile_errors/redefinition_of_enums.zig @@ -1,5 +1,5 @@ -const A = enum {x}; -const A = enum {x}; +const A = enum { x }; +const A = enum { x }; // error // backend=stage2 diff --git a/test/cases/compile_errors/redefinition_of_global_variables.zig b/test/cases/compile_errors/redefinition_of_global_variables.zig index ed0d6f3ed95b..6f4ed225f791 100644 --- a/test/cases/compile_errors/redefinition_of_global_variables.zig +++ b/test/cases/compile_errors/redefinition_of_global_variables.zig @@ -1,5 +1,5 @@ -var a : i32 = 1; -var a : i32 = 2; +var a: i32 = 1; +var a: i32 = 2; // error // backend=stage2 diff --git a/test/cases/compile_errors/redefinition_of_struct.zig b/test/cases/compile_errors/redefinition_of_struct.zig index dc6d4abeebea..fa541d6ec9b2 100644 --- a/test/cases/compile_errors/redefinition_of_struct.zig +++ b/test/cases/compile_errors/redefinition_of_struct.zig @@ -1,5 +1,9 @@ -const A = struct { x : i32, }; -const A = struct { y : i32, }; +const A = struct { + x: i32, +}; +const A = struct { + y: i32, +}; // error // backend=stage2 diff --git a/test/cases/compile_errors/reference_to_const_data.zig b/test/cases/compile_errors/reference_to_const_data.zig index cbc0fe131cfa..e773cdb4a088 100644 --- a/test/cases/compile_errors/reference_to_const_data.zig +++ b/test/cases/compile_errors/reference_to_const_data.zig @@ -1,5 +1,5 @@ export fn foo() void { - var ptr = &[_]u8{0,0,0,0}; + var ptr = &[_]u8{ 0, 0, 0, 0 }; ptr[1] = 2; } export fn bar() void { @@ -11,11 +11,11 @@ export fn baz() void { ptr.* = false; } export fn qux() void { - const S = struct{ + const S = struct { x: usize, y: usize, }; - var ptr = &S{.x=1,.y=2}; + var ptr = &S{ .x = 1, .y = 2 }; ptr.x = 2; } export fn quux() void { diff --git a/test/cases/compile_errors/reify_type.Fn_with_is_generic_true.zig b/test/cases/compile_errors/reify_type.Fn_with_is_generic_true.zig index abdccdf36df6..bb2c4bbe6282 100644 --- a/test/cases/compile_errors/reify_type.Fn_with_is_generic_true.zig +++ b/test/cases/compile_errors/reify_type.Fn_with_is_generic_true.zig @@ -8,7 +8,9 @@ const Foo = @Type(.{ .params = &.{}, }, }); -comptime { _ = Foo; } +comptime { + _ = Foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/reify_type.Fn_with_is_var_args_true_and_non-C_callconv.zig b/test/cases/compile_errors/reify_type.Fn_with_is_var_args_true_and_non-C_callconv.zig index f3542d583a13..a341435b36ba 100644 --- a/test/cases/compile_errors/reify_type.Fn_with_is_var_args_true_and_non-C_callconv.zig +++ b/test/cases/compile_errors/reify_type.Fn_with_is_var_args_true_and_non-C_callconv.zig @@ -8,7 +8,9 @@ const Foo = @Type(.{ .params = &.{}, }, }); -comptime { _ = Foo; } +comptime { + _ = Foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/reify_type.Fn_with_return_type_null.zig b/test/cases/compile_errors/reify_type.Fn_with_return_type_null.zig index 49335ab69336..d348a0c908a0 100644 --- a/test/cases/compile_errors/reify_type.Fn_with_return_type_null.zig +++ b/test/cases/compile_errors/reify_type.Fn_with_return_type_null.zig @@ -8,7 +8,9 @@ const Foo = @Type(.{ .params = &.{}, }, }); -comptime { _ = Foo; } +comptime { + _ = Foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/reify_type_union_payload_is_undefined.zig b/test/cases/compile_errors/reify_type_union_payload_is_undefined.zig index 410bb9265811..886d443a00c0 100644 --- a/test/cases/compile_errors/reify_type_union_payload_is_undefined.zig +++ b/test/cases/compile_errors/reify_type_union_payload_is_undefined.zig @@ -1,7 +1,9 @@ const Foo = @Type(.{ .Struct = undefined, }); -comptime { _ = Foo; } +comptime { + _ = Foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/return_from_defer_expression.zig b/test/cases/compile_errors/return_from_defer_expression.zig index 12aa08a11cdc..28d1b0c6c6c4 100644 --- a/test/cases/compile_errors/return_from_defer_expression.zig +++ b/test/cases/compile_errors/return_from_defer_expression.zig @@ -6,13 +6,15 @@ pub fn testTrickyDefer() !void { const a = maybeInt() orelse return; } -fn canFail() anyerror!void { } +fn canFail() anyerror!void {} pub fn maybeInt() ?i32 { return 0; } -export fn entry() usize { return @sizeOf(@TypeOf(testTrickyDefer)); } +export fn entry() usize { + return @sizeOf(@TypeOf(testTrickyDefer)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/return_invalid_type_from_test.zig b/test/cases/compile_errors/return_invalid_type_from_test.zig index a954bd7ee569..d70bab390a0c 100644 --- a/test/cases/compile_errors/return_invalid_type_from_test.zig +++ b/test/cases/compile_errors/return_invalid_type_from_test.zig @@ -1,8 +1,10 @@ -test "example" { return 1; } +test "example" { + return 1; +} // error // backend=stage2 // target=native // is_test=1 // -// :1:25: error: expected type '@typeInfo(@typeInfo(@TypeOf(tmp.test.example)).Fn.return_type.?).ErrorUnion.error_set!void', found 'comptime_int' \ No newline at end of file +// :1:25: error: expected type '@typeInfo(@typeInfo(@TypeOf(tmp.test.example)).Fn.return_type.?).ErrorUnion.error_set!void', found 'comptime_int' diff --git a/test/cases/compile_errors/runtime_assignment_to_comptime_struct_type.zig b/test/cases/compile_errors/runtime_assignment_to_comptime_struct_type.zig index 5949031fb926..67a46a7226a0 100644 --- a/test/cases/compile_errors/runtime_assignment_to_comptime_struct_type.zig +++ b/test/cases/compile_errors/runtime_assignment_to_comptime_struct_type.zig @@ -4,7 +4,7 @@ const Foo = struct { }; export fn f() void { var x: u8 = 0; - const foo = Foo { .Bar = x, .Baz = u8 }; + const foo = Foo{ .Bar = x, .Baz = u8 }; _ = foo; } diff --git a/test/cases/compile_errors/runtime_assignment_to_comptime_union_type.zig b/test/cases/compile_errors/runtime_assignment_to_comptime_union_type.zig index 04e2a97e7aa0..940b1e0e8d08 100644 --- a/test/cases/compile_errors/runtime_assignment_to_comptime_union_type.zig +++ b/test/cases/compile_errors/runtime_assignment_to_comptime_union_type.zig @@ -4,7 +4,7 @@ const Foo = union { }; export fn f() void { var x: u8 = 0; - const foo = Foo { .Bar = x }; + const foo = Foo{ .Bar = x }; _ = foo; } diff --git a/test/cases/compile_errors/runtime_to_comptime_num.zig b/test/cases/compile_errors/runtime_to_comptime_num.zig index 972adb59bb40..8d4a3fb999f5 100644 --- a/test/cases/compile_errors/runtime_to_comptime_num.zig +++ b/test/cases/compile_errors/runtime_to_comptime_num.zig @@ -2,16 +2,16 @@ pub export fn entry() void { var a: u32 = 0; _ = @as(comptime_int, a); } -pub export fn entry2() void{ +pub export fn entry2() void { var a: u32 = 0; _ = @as(comptime_float, a); } -pub export fn entry3() void{ +pub export fn entry3() void { comptime var aa: comptime_float = 0.0; var a: f32 = 4; aa = a; } -pub export fn entry4() void{ +pub export fn entry4() void { comptime var aa: comptime_int = 0.0; var a: f32 = 4; aa = a; diff --git a/test/cases/compile_errors/saturating_shl_assign_does_not_allow_negative_rhs_at_comptime.zig b/test/cases/compile_errors/saturating_shl_assign_does_not_allow_negative_rhs_at_comptime.zig index c8e95e3969e5..8bf071d75b52 100644 --- a/test/cases/compile_errors/saturating_shl_assign_does_not_allow_negative_rhs_at_comptime.zig +++ b/test/cases/compile_errors/saturating_shl_assign_does_not_allow_negative_rhs_at_comptime.zig @@ -1,8 +1,8 @@ export fn a() void { comptime { - var x = @as(i32, 1); - x <<|= @as(i32, -2); - } + var x = @as(i32, 1); + x <<|= @as(i32, -2); + } } // error diff --git a/test/cases/compile_errors/self_referential_struct_requires_comptime.zig b/test/cases/compile_errors/self_referential_struct_requires_comptime.zig index 3ce75710267c..661a12df97c8 100644 --- a/test/cases/compile_errors/self_referential_struct_requires_comptime.zig +++ b/test/cases/compile_errors/self_referential_struct_requires_comptime.zig @@ -7,7 +7,6 @@ pub export fn entry() void { _ = s; } - // error // backend=stage2 // target=native diff --git a/test/cases/compile_errors/setAlignStack_in_inline_function.zig b/test/cases/compile_errors/setAlignStack_in_inline_function.zig index 62bbb3865c9c..a84424e368c0 100644 --- a/test/cases/compile_errors/setAlignStack_in_inline_function.zig +++ b/test/cases/compile_errors/setAlignStack_in_inline_function.zig @@ -1,7 +1,7 @@ export fn entry() void { foo(); } -fn foo() callconv(.Inline) void { +inline fn foo() void { @setAlignStack(16); } @@ -12,7 +12,6 @@ fn bar() void { @setAlignStack(16); } - // error // backend=stage2 // target=native diff --git a/test/cases/compile_errors/slice_passed_as_array_init_type_with_elems.zig b/test/cases/compile_errors/slice_passed_as_array_init_type_with_elems.zig index 7af505e20d34..91dc5f512899 100644 --- a/test/cases/compile_errors/slice_passed_as_array_init_type_with_elems.zig +++ b/test/cases/compile_errors/slice_passed_as_array_init_type_with_elems.zig @@ -1,5 +1,5 @@ export fn entry() void { - const x = []u8{1, 2}; + const x = []u8{ 1, 2 }; _ = x; } diff --git a/test/cases/compile_errors/slice_sentinel_mismatch-2.zig b/test/cases/compile_errors/slice_sentinel_mismatch-2.zig index 3cc5ac4c3914..424cd8936359 100644 --- a/test/cases/compile_errors/slice_sentinel_mismatch-2.zig +++ b/test/cases/compile_errors/slice_sentinel_mismatch-2.zig @@ -2,7 +2,9 @@ fn foo() [:0]u8 { var x: []u8 = undefined; return x; } -comptime { _ = foo; } +comptime { + _ = foo; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/slice_used_as_extern_fn_param.zig b/test/cases/compile_errors/slice_used_as_extern_fn_param.zig index 8391c3e21bfa..e6b065cd623f 100644 --- a/test/cases/compile_errors/slice_used_as_extern_fn_param.zig +++ b/test/cases/compile_errors/slice_used_as_extern_fn_param.zig @@ -1,4 +1,4 @@ -extern fn Text(str: []const u8, num: i32) callconv(.C) void; +extern fn Text(str: []const u8, num: i32) callconv(.C) void; export fn entry() void { _ = Text; } diff --git a/test/cases/compile_errors/specify_enum_tag_type_that_is_too_small.zig b/test/cases/compile_errors/specify_enum_tag_type_that_is_too_small.zig index d878bec18b0d..3024f1dee841 100644 --- a/test/cases/compile_errors/specify_enum_tag_type_that_is_too_small.zig +++ b/test/cases/compile_errors/specify_enum_tag_type_that_is_too_small.zig @@ -1,4 +1,4 @@ -const Small = enum (u2) { +const Small = enum(u2) { One, Two, Three, diff --git a/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig b/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig index f2ff3e2cd142..0bd2839e08fe 100644 --- a/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig +++ b/test/cases/compile_errors/specify_non-integer_enum_tag_type.zig @@ -1,4 +1,4 @@ -const Small = enum (f32) { +const Small = enum(f32) { One, Two, Three, diff --git a/test/cases/compile_errors/src_fields_runtime.zig b/test/cases/compile_errors/src_fields_runtime.zig index 0bdb5af81cbe..a9828311233a 100644 --- a/test/cases/compile_errors/src_fields_runtime.zig +++ b/test/cases/compile_errors/src_fields_runtime.zig @@ -4,7 +4,10 @@ pub export fn entry1() void { comptime var b: []const u8 = s.fn_name; comptime var c: u32 = s.column; comptime var d: u32 = s.line; - _ = a; _ = b; _ = c; _ = d; + _ = a; + _ = b; + _ = c; + _ = d; } // error diff --git a/test/cases/compile_errors/stage1/obj/generic_function_where_return_type_is_self-referenced.zig b/test/cases/compile_errors/stage1/obj/generic_function_where_return_type_is_self-referenced.zig index c8d31ec8dfac..75f43ed914b9 100644 --- a/test/cases/compile_errors/stage1/obj/generic_function_where_return_type_is_self-referenced.zig +++ b/test/cases/compile_errors/stage1/obj/generic_function_where_return_type_is_self-referenced.zig @@ -1,10 +1,8 @@ fn Foo(comptime T: type) Foo(T) { - return struct{ x: T }; + return struct { x: T }; } export fn entry() void { - const t = Foo(u32) { - .x = 1 - }; + const t = Foo(u32){ .x = 1 }; _ = t; } diff --git a/test/cases/compile_errors/stage1/obj/unsupported_modifier_at_start_of_asm_output_constraint.zig b/test/cases/compile_errors/stage1/obj/unsupported_modifier_at_start_of_asm_output_constraint.zig index 7c70fc509528..5bcf30547b5f 100644 --- a/test/cases/compile_errors/stage1/obj/unsupported_modifier_at_start_of_asm_output_constraint.zig +++ b/test/cases/compile_errors/stage1/obj/unsupported_modifier_at_start_of_asm_output_constraint.zig @@ -1,6 +1,10 @@ export fn foo() void { var bar: u32 = 3; - asm volatile ("" : [baz]"+r"(bar) : : ""); + asm volatile ("" + : [baz] "+r" (bar), + : + : "" + ); } // error diff --git a/test/cases/compile_errors/std.fmt_error_for_unused_arguments.zig b/test/cases/compile_errors/std.fmt_error_for_unused_arguments.zig index a20f2798bc8d..6661fe54eba8 100644 --- a/test/cases/compile_errors/std.fmt_error_for_unused_arguments.zig +++ b/test/cases/compile_errors/std.fmt_error_for_unused_arguments.zig @@ -1,5 +1,5 @@ export fn entry() void { - @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}); + @import("std").debug.print("{d} {d} {d} {d} {d}", .{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }); } // error diff --git a/test/cases/compile_errors/struct_type_mismatch_in_arg.zig b/test/cases/compile_errors/struct_type_mismatch_in_arg.zig index d051966c523d..fe5a764e81be 100644 --- a/test/cases/compile_errors/struct_type_mismatch_in_arg.zig +++ b/test/cases/compile_errors/struct_type_mismatch_in_arg.zig @@ -1,11 +1,11 @@ const Foo = struct { i: i32 }; const Bar = struct { j: i32 }; -pub fn helper(_: Foo, _: Bar) void { } +pub fn helper(_: Foo, _: Bar) void {} comptime { - helper(Bar { .j = 10 }, Bar { .j = 10 }); - helper(Bar { .i = 10 }, Bar { .j = 10 }); + helper(Bar{ .j = 10 }, Bar{ .j = 10 }); + helper(Bar{ .i = 10 }, Bar{ .j = 10 }); } // error diff --git a/test/cases/compile_errors/struct_with_declarations_unavailable_for_reify_type.zig b/test/cases/compile_errors/struct_with_declarations_unavailable_for_reify_type.zig index 81864160dcc1..f0a24632283e 100644 --- a/test/cases/compile_errors/struct_with_declarations_unavailable_for_reify_type.zig +++ b/test/cases/compile_errors/struct_with_declarations_unavailable_for_reify_type.zig @@ -1,5 +1,7 @@ export fn entry() void { - _ = @Type(@typeInfo(struct { const foo = 1; })); + _ = @Type(@typeInfo(struct { + const foo = 1; + })); } // error diff --git a/test/cases/compile_errors/struct_with_invalid_field.zig b/test/cases/compile_errors/struct_with_invalid_field.zig index aa24b029bb73..479eeeac8fd0 100644 --- a/test/cases/compile_errors/struct_with_invalid_field.zig +++ b/test/cases/compile_errors/struct_with_invalid_field.zig @@ -1,9 +1,16 @@ -const std = @import("std",); +const std = @import( + "std", +); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; const HeaderWeight = enum { - H1, H2, H3, H4, H5, H6, + H1, + H2, + H3, + H4, + H5, + H6, }; const MdText = ArrayList(u8); @@ -16,7 +23,7 @@ const MdNode = union(enum) { }; export fn entry() void { - const a = MdNode.Header { + const a = MdNode.Header{ .text = MdText.init(std.testing.allocator), .weight = HeaderWeight.H1, }; diff --git a/test/cases/compile_errors/sub_overflow_in_function_evaluation.zig b/test/cases/compile_errors/sub_overflow_in_function_evaluation.zig index c87f99837309..651ef34bdcd6 100644 --- a/test/cases/compile_errors/sub_overflow_in_function_evaluation.zig +++ b/test/cases/compile_errors/sub_overflow_in_function_evaluation.zig @@ -3,7 +3,9 @@ fn sub(a: u16, b: u16) u16 { return a - b; } -export fn entry() usize { return @sizeOf(@TypeOf(&y)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&y)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/suspend_inside_suspend_block.zig b/test/cases/compile_errors/suspend_inside_suspend_block.zig index 80436bd07f7f..29a7968e0baa 100644 --- a/test/cases/compile_errors/suspend_inside_suspend_block.zig +++ b/test/cases/compile_errors/suspend_inside_suspend_block.zig @@ -3,8 +3,7 @@ export fn entry() void { } fn foo() void { suspend { - suspend { - } + suspend {} } } diff --git a/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong.zig b/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong.zig index 7011f0a2d5a0..58f98a55f57c 100644 --- a/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong.zig +++ b/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong.zig @@ -14,7 +14,9 @@ fn f(n: Number) i32 { } } -export fn entry() usize { return @sizeOf(@TypeOf(&f)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&f)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong_when_else_present.zig b/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong_when_else_present.zig index 89f195937576..045760e287c2 100644 --- a/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong_when_else_present.zig +++ b/test/cases/compile_errors/switch_expression-duplicate_enumeration_prong_when_else_present.zig @@ -15,7 +15,9 @@ fn f(n: Number) i32 { } } -export fn entry() usize { return @sizeOf(@TypeOf(&f)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&f)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-duplicate_or_overlapping_integer_value.zig b/test/cases/compile_errors/switch_expression-duplicate_or_overlapping_integer_value.zig index 60e361a47afe..7d9980d53e1c 100644 --- a/test/cases/compile_errors/switch_expression-duplicate_or_overlapping_integer_value.zig +++ b/test/cases/compile_errors/switch_expression-duplicate_or_overlapping_integer_value.zig @@ -1,12 +1,14 @@ fn foo(x: u8) u8 { return switch (x) { - 0 ... 100 => @as(u8, 0), - 101 ... 200 => 1, - 201, 203 ... 207 => 2, - 206 ... 255 => 3, + 0...100 => @as(u8, 0), + 101...200 => 1, + 201, 203...207 => 2, + 206...255 => 3, }; } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-duplicate_type.zig b/test/cases/compile_errors/switch_expression-duplicate_type.zig index 59c9b0657a26..44f745364c39 100644 --- a/test/cases/compile_errors/switch_expression-duplicate_type.zig +++ b/test/cases/compile_errors/switch_expression-duplicate_type.zig @@ -7,7 +7,9 @@ fn foo(comptime T: type, x: T) u8 { else => 3, }; } -export fn entry() usize { return @sizeOf(@TypeOf(foo(u32, 0))); } +export fn entry() usize { + return @sizeOf(@TypeOf(foo(u32, 0))); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-duplicate_type_struct_alias.zig b/test/cases/compile_errors/switch_expression-duplicate_type_struct_alias.zig index 797d2bd50d30..4420e575c95f 100644 --- a/test/cases/compile_errors/switch_expression-duplicate_type_struct_alias.zig +++ b/test/cases/compile_errors/switch_expression-duplicate_type_struct_alias.zig @@ -11,7 +11,9 @@ fn foo(comptime T: type, x: T) u8 { else => 3, }; } -export fn entry() usize { return @sizeOf(@TypeOf(foo(u32, 0))); } +export fn entry() usize { + return @sizeOf(@TypeOf(foo(u32, 0))); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-missing_enumeration_prong.zig b/test/cases/compile_errors/switch_expression-missing_enumeration_prong.zig index 1075b837de7f..ca80d420ff93 100644 --- a/test/cases/compile_errors/switch_expression-missing_enumeration_prong.zig +++ b/test/cases/compile_errors/switch_expression-missing_enumeration_prong.zig @@ -12,7 +12,9 @@ fn f(n: Number) i32 { } } -export fn entry() usize { return @sizeOf(@TypeOf(&f)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&f)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-non_exhaustive_integer_prongs.zig b/test/cases/compile_errors/switch_expression-non_exhaustive_integer_prongs.zig index 6770b5a0555c..83097f273391 100644 --- a/test/cases/compile_errors/switch_expression-non_exhaustive_integer_prongs.zig +++ b/test/cases/compile_errors/switch_expression-non_exhaustive_integer_prongs.zig @@ -3,7 +3,9 @@ fn foo(x: u8) void { 0 => {}, } } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-switch_on_pointer_type_with_no_else.zig b/test/cases/compile_errors/switch_expression-switch_on_pointer_type_with_no_else.zig index bbad58c74c84..32491170ebbd 100644 --- a/test/cases/compile_errors/switch_expression-switch_on_pointer_type_with_no_else.zig +++ b/test/cases/compile_errors/switch_expression-switch_on_pointer_type_with_no_else.zig @@ -4,7 +4,9 @@ fn foo(x: *u8) void { } } var y: u8 = 100; -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-unreachable_else_prong_bool.zig b/test/cases/compile_errors/switch_expression-unreachable_else_prong_bool.zig index 5dfc839c13f6..859f97fa8af5 100644 --- a/test/cases/compile_errors/switch_expression-unreachable_else_prong_bool.zig +++ b/test/cases/compile_errors/switch_expression-unreachable_else_prong_bool.zig @@ -5,7 +5,9 @@ fn foo(x: bool) void { else => {}, } } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-unreachable_else_prong_enum.zig b/test/cases/compile_errors/switch_expression-unreachable_else_prong_enum.zig index cc837b8b5e1f..e7bb8d392f0d 100644 --- a/test/cases/compile_errors/switch_expression-unreachable_else_prong_enum.zig +++ b/test/cases/compile_errors/switch_expression-unreachable_else_prong_enum.zig @@ -1,4 +1,4 @@ -const TestEnum = enum{ T1, T2 }; +const TestEnum = enum { T1, T2 }; fn err(x: u8) TestEnum { switch (x) { @@ -15,7 +15,9 @@ fn foo(x: u8) void { } } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=llvm diff --git a/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_i8.zig b/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_i8.zig index bcb84eed122d..861b0ceaeece 100644 --- a/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_i8.zig +++ b/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_i8.zig @@ -8,7 +8,9 @@ fn foo(x: i8) void { else => {}, } } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_u8.zig b/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_u8.zig index 2230434ea065..16fdea91ea17 100644 --- a/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_u8.zig +++ b/test/cases/compile_errors/switch_expression-unreachable_else_prong_range_u8.zig @@ -8,7 +8,9 @@ fn foo(x: u8) void { else => {}, } } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-unreachable_else_prong_u1.zig b/test/cases/compile_errors/switch_expression-unreachable_else_prong_u1.zig index 58458474a4fc..3d7988454e27 100644 --- a/test/cases/compile_errors/switch_expression-unreachable_else_prong_u1.zig +++ b/test/cases/compile_errors/switch_expression-unreachable_else_prong_u1.zig @@ -5,7 +5,9 @@ fn foo(x: u1) void { else => {}, } } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_expression-unreachable_else_prong_u2.zig b/test/cases/compile_errors/switch_expression-unreachable_else_prong_u2.zig index 9e337cb2007c..dd8f315dcdb1 100644 --- a/test/cases/compile_errors/switch_expression-unreachable_else_prong_u2.zig +++ b/test/cases/compile_errors/switch_expression-unreachable_else_prong_u2.zig @@ -7,7 +7,9 @@ fn foo(x: u2) void { else => {}, } } -export fn entry() usize { return @sizeOf(@TypeOf(&foo)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&foo)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/switch_on_union_with_no_attached_enum.zig b/test/cases/compile_errors/switch_on_union_with_no_attached_enum.zig index 4d8742d32e1a..bf5c97eaa61c 100644 --- a/test/cases/compile_errors/switch_on_union_with_no_attached_enum.zig +++ b/test/cases/compile_errors/switch_on_union_with_no_attached_enum.zig @@ -4,7 +4,7 @@ const Payload = union { C: bool, }; export fn entry() void { - const a = Payload { .A = 1234 }; + const a = Payload{ .A = 1234 }; foo(&a); } fn foo(a: *const Payload) void { diff --git a/test/cases/compile_errors/switching_with_exhaustive_enum_has___prong_.zig b/test/cases/compile_errors/switching_with_exhaustive_enum_has___prong_.zig index 3dad0be9df20..e3d09bc13333 100644 --- a/test/cases/compile_errors/switching_with_exhaustive_enum_has___prong_.zig +++ b/test/cases/compile_errors/switching_with_exhaustive_enum_has___prong_.zig @@ -1,4 +1,4 @@ -const E = enum{ +const E = enum { a, b, }; diff --git a/test/cases/compile_errors/switching_with_non-exhaustive_enums.zig b/test/cases/compile_errors/switching_with_non-exhaustive_enums.zig index 435d409dd48e..5678102fc6c5 100644 --- a/test/cases/compile_errors/switching_with_non-exhaustive_enums.zig +++ b/test/cases/compile_errors/switching_with_non-exhaustive_enums.zig @@ -22,7 +22,7 @@ pub export fn entry2() void { } } pub export fn entry3() void { - var u = U{.a = 2}; + var u = U{ .a = 2 }; switch (u) { // error: `_` prong not allowed when switching on tagged union .a => {}, .b => {}, diff --git a/test/cases/compile_errors/tagName_on_invalid_value_of_non-exhaustive_enum.zig b/test/cases/compile_errors/tagName_on_invalid_value_of_non-exhaustive_enum.zig index 1de0d1c14501..1abd36accfd6 100644 --- a/test/cases/compile_errors/tagName_on_invalid_value_of_non-exhaustive_enum.zig +++ b/test/cases/compile_errors/tagName_on_invalid_value_of_non-exhaustive_enum.zig @@ -1,5 +1,5 @@ test "enum" { - const E = enum(u8) {A, B, _}; + const E = enum(u8) { A, B, _ }; _ = @tagName(@intToEnum(E, 5)); } diff --git a/test/cases/compile_errors/tagName_used_on_union_with_no_associated_enum_tag.zig b/test/cases/compile_errors/tagName_used_on_union_with_no_associated_enum_tag.zig index cbe0f642a5fb..28eaa9bad815 100644 --- a/test/cases/compile_errors/tagName_used_on_union_with_no_associated_enum_tag.zig +++ b/test/cases/compile_errors/tagName_used_on_union_with_no_associated_enum_tag.zig @@ -3,7 +3,7 @@ const FloatInt = extern union { Int: i32, }; export fn entry() void { - var fi = FloatInt{.Float = 123.45}; + var fi = FloatInt{ .Float = 123.45 }; var tagName = @tagName(fi); _ = tagName; } diff --git a/test/cases/compile_errors/top_level_decl_dependency_loop.zig b/test/cases/compile_errors/top_level_decl_dependency_loop.zig index 8ba3d98ea28f..5657ca52fe3c 100644 --- a/test/cases/compile_errors/top_level_decl_dependency_loop.zig +++ b/test/cases/compile_errors/top_level_decl_dependency_loop.zig @@ -1,5 +1,5 @@ -const a : @TypeOf(b) = 0; -const b : @TypeOf(a) = 0; +const a: @TypeOf(b) = 0; +const b: @TypeOf(a) = 0; export fn entry() void { const c = a + b; _ = c; diff --git a/test/cases/compile_errors/try_in_function_with_non_error_return_type.zig b/test/cases/compile_errors/try_in_function_with_non_error_return_type.zig index 44d8b9ee56ee..96b098237034 100644 --- a/test/cases/compile_errors/try_in_function_with_non_error_return_type.zig +++ b/test/cases/compile_errors/try_in_function_with_non_error_return_type.zig @@ -1,7 +1,7 @@ export fn f() void { try something(); } -fn something() anyerror!void { } +fn something() anyerror!void {} // error // backend=stage2 diff --git a/test/cases/compile_errors/tuple_init_edge_cases.zig b/test/cases/compile_errors/tuple_init_edge_cases.zig index 32b52cdc1f0f..fe6f5c926131 100644 --- a/test/cases/compile_errors/tuple_init_edge_cases.zig +++ b/test/cases/compile_errors/tuple_init_edge_cases.zig @@ -1,37 +1,49 @@ pub export fn entry1() void { const T = @TypeOf(.{ 123, 3 }); - var b = T{ .@"1" = 3 }; _ = b; - var c = T{ 123, 3 }; _ = c; - var d = T{}; _ = d; + var b = T{ .@"1" = 3 }; + _ = b; + var c = T{ 123, 3 }; + _ = c; + var d = T{}; + _ = d; } pub export fn entry2() void { var a: u32 = 2; const T = @TypeOf(.{ 123, a }); - var b = T{ .@"1" = 3 }; _ = b; - var c = T{ 123, 3 }; _ = c; - var d = T{}; _ = d; + var b = T{ .@"1" = 3 }; + _ = b; + var c = T{ 123, 3 }; + _ = c; + var d = T{}; + _ = d; } pub export fn entry3() void { var a: u32 = 2; const T = @TypeOf(.{ 123, a }); - var b = T{ .@"0" = 123 }; _ = b; + var b = T{ .@"0" = 123 }; + _ = b; } comptime { var a: u32 = 2; const T = @TypeOf(.{ 123, a }); - var b = T{ .@"0" = 123 }; _ = b; - var c = T{ 123, 2 }; _ = c; - var d = T{}; _ = d; + var b = T{ .@"0" = 123 }; + _ = b; + var c = T{ 123, 2 }; + _ = c; + var d = T{}; + _ = d; } pub export fn entry4() void { var a: u32 = 2; const T = @TypeOf(.{ 123, a }); - var b = T{ 123, 4, 5 }; _ = b; + var b = T{ 123, 4, 5 }; + _ = b; } pub export fn entry5() void { var a: u32 = 2; const T = @TypeOf(.{ 123, a }); - var b = T{ .@"0" = 123, .@"2" = 123, .@"1" = 123 }; _ = b; + var b = T{ .@"0" = 123, .@"2" = 123, .@"1" = 123 }; + _ = b; } // error diff --git a/test/cases/compile_errors/type_checking_function_pointers.zig b/test/cases/compile_errors/type_checking_function_pointers.zig index f11330917061..dd980558a8be 100644 --- a/test/cases/compile_errors/type_checking_function_pointers.zig +++ b/test/cases/compile_errors/type_checking_function_pointers.zig @@ -1,7 +1,9 @@ fn a(b: *const fn (*const u8) void) void { _ = b; } -fn c(d: u8) void {_ = d;} +fn c(d: u8) void { + _ = d; +} export fn entry() void { a(c); } diff --git a/test/cases/compile_errors/undeclared_identifier.zig b/test/cases/compile_errors/undeclared_identifier.zig index 7edc0d2896bf..c7eb94e370d7 100644 --- a/test/cases/compile_errors/undeclared_identifier.zig +++ b/test/cases/compile_errors/undeclared_identifier.zig @@ -1,7 +1,6 @@ export fn a() void { - return - b + - c; + return b + + c; } // error diff --git a/test/cases/compile_errors/union_auto-enum_value_already_taken.zig b/test/cases/compile_errors/union_auto-enum_value_already_taken.zig index 36b52c96b9bd..da2fefbad5a3 100644 --- a/test/cases/compile_errors/union_auto-enum_value_already_taken.zig +++ b/test/cases/compile_errors/union_auto-enum_value_already_taken.zig @@ -6,7 +6,7 @@ const MultipleChoice = union(enum(u32)) { E = 60, }; export fn entry() void { - var x = MultipleChoice { .C = {} }; + var x = MultipleChoice{ .C = {} }; _ = x; } diff --git a/test/cases/compile_errors/union_enum_field_does_not_match_enum.zig b/test/cases/compile_errors/union_enum_field_does_not_match_enum.zig index 3c1e2d53ef7a..6b70c0eaf4b4 100644 --- a/test/cases/compile_errors/union_enum_field_does_not_match_enum.zig +++ b/test/cases/compile_errors/union_enum_field_does_not_match_enum.zig @@ -10,7 +10,7 @@ const Payload = union(Letter) { D: bool, }; export fn entry() void { - var a = Payload {.A = 1234}; + var a = Payload{ .A = 1234 }; _ = a; } diff --git a/test/cases/compile_errors/unreachable_parameter.zig b/test/cases/compile_errors/unreachable_parameter.zig index 31649744321b..f00b24e07acf 100644 --- a/test/cases/compile_errors/unreachable_parameter.zig +++ b/test/cases/compile_errors/unreachable_parameter.zig @@ -1,5 +1,9 @@ -fn f(a: noreturn) void { _ = a; } -export fn entry() void { f(); } +fn f(a: noreturn) void { + _ = a; +} +export fn entry() void { + f(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/unreachable_with_return.zig b/test/cases/compile_errors/unreachable_with_return.zig index 5293734c8200..10b5e56ce78c 100644 --- a/test/cases/compile_errors/unreachable_with_return.zig +++ b/test/cases/compile_errors/unreachable_with_return.zig @@ -1,5 +1,9 @@ -fn a() noreturn {return;} -export fn entry() void { a(); } +fn a() noreturn { + return; +} +export fn entry() void { + a(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/while_expected_bool_got_error_union.zig b/test/cases/compile_errors/while_expected_bool_got_error_union.zig index ecf3dd0736fa..07b441188371 100644 --- a/test/cases/compile_errors/while_expected_bool_got_error_union.zig +++ b/test/cases/compile_errors/while_expected_bool_got_error_union.zig @@ -1,7 +1,9 @@ export fn foo() void { while (bar()) {} } -fn bar() anyerror!i32 { return 1; } +fn bar() anyerror!i32 { + return 1; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/while_expected_bool_got_optional.zig b/test/cases/compile_errors/while_expected_bool_got_optional.zig index 3db4b7ae74e4..2a2af57ef328 100644 --- a/test/cases/compile_errors/while_expected_bool_got_optional.zig +++ b/test/cases/compile_errors/while_expected_bool_got_optional.zig @@ -1,7 +1,9 @@ export fn foo() void { while (bar()) {} } -fn bar() ?i32 { return 1; } +fn bar() ?i32 { + return 1; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/while_expected_error_union_got_bool.zig b/test/cases/compile_errors/while_expected_error_union_got_bool.zig index f7960437ec55..6d31d2055baa 100644 --- a/test/cases/compile_errors/while_expected_error_union_got_bool.zig +++ b/test/cases/compile_errors/while_expected_error_union_got_bool.zig @@ -1,7 +1,13 @@ export fn foo() void { - while (bar()) |x| {_ = x;} else |err| {_ = err;} + while (bar()) |x| { + _ = x; + } else |err| { + _ = err; + } +} +fn bar() bool { + return true; } -fn bar() bool { return true; } // error // backend=stage2 diff --git a/test/cases/compile_errors/while_expected_error_union_got_optional.zig b/test/cases/compile_errors/while_expected_error_union_got_optional.zig index 5cabd76fce3f..6ebf96dc4c7f 100644 --- a/test/cases/compile_errors/while_expected_error_union_got_optional.zig +++ b/test/cases/compile_errors/while_expected_error_union_got_optional.zig @@ -1,7 +1,13 @@ export fn foo() void { - while (bar()) |x| {_ = x;} else |err| {_ = err;} + while (bar()) |x| { + _ = x; + } else |err| { + _ = err; + } +} +fn bar() ?i32 { + return 1; } -fn bar() ?i32 { return 1; } // error // backend=stage2 diff --git a/test/cases/compile_errors/while_expected_optional_got_bool.zig b/test/cases/compile_errors/while_expected_optional_got_bool.zig index 22b8c1e58c73..5ced72b49266 100644 --- a/test/cases/compile_errors/while_expected_optional_got_bool.zig +++ b/test/cases/compile_errors/while_expected_optional_got_bool.zig @@ -1,7 +1,11 @@ export fn foo() void { - while (bar()) |x| {_ = x;} + while (bar()) |x| { + _ = x; + } +} +fn bar() bool { + return true; } -fn bar() bool { return true; } // error // backend=stage2 diff --git a/test/cases/compile_errors/while_expected_optional_got_error_union.zig b/test/cases/compile_errors/while_expected_optional_got_error_union.zig index 38a8a0dd2038..7bde2c866d29 100644 --- a/test/cases/compile_errors/while_expected_optional_got_error_union.zig +++ b/test/cases/compile_errors/while_expected_optional_got_error_union.zig @@ -1,7 +1,11 @@ export fn foo() void { - while (bar()) |x| {_ = x;} + while (bar()) |x| { + _ = x; + } +} +fn bar() anyerror!i32 { + return 1; } -fn bar() anyerror!i32 { return 1; } // error // backend=stage2 diff --git a/test/cases/compile_errors/write_to_const_global_variable.zig b/test/cases/compile_errors/write_to_const_global_variable.zig index 5ffd3a305d06..075854c45462 100644 --- a/test/cases/compile_errors/write_to_const_global_variable.zig +++ b/test/cases/compile_errors/write_to_const_global_variable.zig @@ -1,8 +1,10 @@ -const x : i32 = 99; +const x: i32 = 99; fn f() void { x = 1; } -export fn entry() void { f(); } +export fn entry() void { + f(); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/wrong_function_type.zig b/test/cases/compile_errors/wrong_function_type.zig index 867bdccc1a6e..6dfa9bd70232 100644 --- a/test/cases/compile_errors/wrong_function_type.zig +++ b/test/cases/compile_errors/wrong_function_type.zig @@ -1,8 +1,16 @@ -const fns = [_]fn() void { a, b, c }; -fn a() i32 {return 0;} -fn b() i32 {return 1;} -fn c() i32 {return 2;} -export fn entry() usize { return @sizeOf(@TypeOf(fns)); } +const fns = [_]fn () void{ a, b, c }; +fn a() i32 { + return 0; +} +fn b() i32 { + return 1; +} +fn c() i32 { + return 2; +} +export fn entry() usize { + return @sizeOf(@TypeOf(fns)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/wrong_number_of_arguments.zig b/test/cases/compile_errors/wrong_number_of_arguments.zig index 05d761de18ca..b83a7a4a3706 100644 --- a/test/cases/compile_errors/wrong_number_of_arguments.zig +++ b/test/cases/compile_errors/wrong_number_of_arguments.zig @@ -1,7 +1,11 @@ export fn a() void { c(1); } -fn c(d: i32, e: i32, f: i32) void { _ = d; _ = e; _ = f; } +fn c(d: i32, e: i32, f: i32) void { + _ = d; + _ = e; + _ = f; +} // error // backend=stage2 diff --git a/test/cases/compile_errors/wrong_number_of_arguments_for_method_fn_call.zig b/test/cases/compile_errors/wrong_number_of_arguments_for_method_fn_call.zig index da6a7be4fa45..ecbd2d02e36a 100644 --- a/test/cases/compile_errors/wrong_number_of_arguments_for_method_fn_call.zig +++ b/test/cases/compile_errors/wrong_number_of_arguments_for_method_fn_call.zig @@ -1,11 +1,15 @@ const Foo = struct { - fn method(self: *const Foo, a: i32) void {_ = self; _ = a;} + fn method(self: *const Foo, a: i32) void { + _ = self; + _ = a; + } }; fn f(foo: *const Foo) void { - foo.method(1, 2); } -export fn entry() usize { return @sizeOf(@TypeOf(&f)); } +export fn entry() usize { + return @sizeOf(@TypeOf(&f)); +} // error // backend=stage2 diff --git a/test/cases/compile_errors/wrong_size_to_an_array_literal.zig b/test/cases/compile_errors/wrong_size_to_an_array_literal.zig index a38d8d4d8587..a58d6d984605 100644 --- a/test/cases/compile_errors/wrong_size_to_an_array_literal.zig +++ b/test/cases/compile_errors/wrong_size_to_an_array_literal.zig @@ -1,5 +1,5 @@ comptime { - const array = [2]u8{1, 2, 3}; + const array = [2]u8{ 1, 2, 3 }; _ = array; } diff --git a/test/cases/compile_errors/wrong_types_given_to_export.zig b/test/cases/compile_errors/wrong_types_given_to_export.zig index 2ae55b4a6317..a5618ebb29b3 100644 --- a/test/cases/compile_errors/wrong_types_given_to_export.zig +++ b/test/cases/compile_errors/wrong_types_given_to_export.zig @@ -1,6 +1,6 @@ -fn entry() callconv(.C) void { } +fn entry() callconv(.C) void {} comptime { - @export(entry, .{.name = "entry", .linkage = @as(u32, 1234) }); + @export(entry, .{ .name = "entry", .linkage = @as(u32, 1234) }); } // error diff --git a/test/cases/error_in_nested_declaration.zig b/test/cases/error_in_nested_declaration.zig index 3fff746909a5..710b821e6552 100644 --- a/test/cases/error_in_nested_declaration.zig +++ b/test/cases/error_in_nested_declaration.zig @@ -5,7 +5,7 @@ const S = struct { pub fn str(_: @This(), extra: []u32) []i32 { return @bitCast([]i32, extra); } - }, + }, }; pub export fn entry() void { diff --git a/test/cases/safety/@alignCast misaligned.zig b/test/cases/safety/@alignCast misaligned.zig index aed805cf66bf..538e0ecdf69a 100644 --- a/test/cases/safety/@alignCast misaligned.zig +++ b/test/cases/safety/@alignCast misaligned.zig @@ -9,7 +9,7 @@ pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usi } pub fn main() !void { - var array align(4) = [_]u32{0x11111111, 0x11111111}; + var array align(4) = [_]u32{ 0x11111111, 0x11111111 }; const bytes = std.mem.sliceAsBytes(array[0..]); if (foo(bytes) != 0x11111111) return error.Wrong; return error.TestFailed; diff --git a/test/cases/safety/@errSetCast error not present in destination.zig b/test/cases/safety/@errSetCast error not present in destination.zig index 2fd0a912e660..372bd80aa59a 100644 --- a/test/cases/safety/@errSetCast error not present in destination.zig +++ b/test/cases/safety/@errSetCast error not present in destination.zig @@ -7,8 +7,8 @@ pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usi } std.process.exit(1); } -const Set1 = error{A, B}; -const Set2 = error{A, C}; +const Set1 = error{ A, B }; +const Set2 = error{ A, C }; pub fn main() !void { foo(Set1.B) catch {}; return error.TestFailed; diff --git a/test/cases/safety/@floatToInt cannot fit - negative out of range.zig b/test/cases/safety/@floatToInt cannot fit - negative out of range.zig index fa7e628f4a02..691acd34c298 100644 --- a/test/cases/safety/@floatToInt cannot fit - negative out of range.zig +++ b/test/cases/safety/@floatToInt cannot fit - negative out of range.zig @@ -14,7 +14,7 @@ pub fn main() !void { fn bar(a: f32) i8 { return @floatToInt(i8, a); } -fn baz(_: i8) void { } +fn baz(_: i8) void {} // run // backend=llvm // target=native diff --git a/test/cases/safety/@floatToInt cannot fit - negative to unsigned.zig b/test/cases/safety/@floatToInt cannot fit - negative to unsigned.zig index 5bab7710b13a..f74dedb87c9d 100644 --- a/test/cases/safety/@floatToInt cannot fit - negative to unsigned.zig +++ b/test/cases/safety/@floatToInt cannot fit - negative to unsigned.zig @@ -14,7 +14,7 @@ pub fn main() !void { fn bar(a: f32) u8 { return @floatToInt(u8, a); } -fn baz(_: u8) void { } +fn baz(_: u8) void {} // run // backend=llvm // target=native diff --git a/test/cases/safety/@floatToInt cannot fit - positive out of range.zig b/test/cases/safety/@floatToInt cannot fit - positive out of range.zig index fe931e875153..d4cc7cd2a769 100644 --- a/test/cases/safety/@floatToInt cannot fit - positive out of range.zig +++ b/test/cases/safety/@floatToInt cannot fit - positive out of range.zig @@ -14,7 +14,7 @@ pub fn main() !void { fn bar(a: f32) u8 { return @floatToInt(u8, a); } -fn baz(_: u8) void { } +fn baz(_: u8) void {} // run // backend=llvm // target=native diff --git a/test/cases/safety/bad union field access.zig b/test/cases/safety/bad union field access.zig index fbe7718c6658..c6706d41764d 100644 --- a/test/cases/safety/bad union field access.zig +++ b/test/cases/safety/bad union field access.zig @@ -14,7 +14,7 @@ const Foo = union { }; pub fn main() !void { - var f = Foo { .int = 42 }; + var f = Foo{ .int = 42 }; bar(&f); return error.TestFailed; } diff --git a/test/cases/safety/cast []u8 to bigger slice of wrong size.zig b/test/cases/safety/cast []u8 to bigger slice of wrong size.zig index 93d18178a84f..a72b2fcca2ca 100644 --- a/test/cases/safety/cast []u8 to bigger slice of wrong size.zig +++ b/test/cases/safety/cast []u8 to bigger slice of wrong size.zig @@ -9,7 +9,7 @@ pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usi } pub fn main() !void { - const x = widenSlice(&[_]u8{1, 2, 3, 4, 5}); + const x = widenSlice(&[_]u8{ 1, 2, 3, 4, 5 }); if (x.len == 0) return error.Whatever; return error.TestFailed; } diff --git a/test/cases/safety/error return trace across suspend points.zig b/test/cases/safety/error return trace across suspend points.zig index 28c53cb6d144..2c31aacc9d2f 100644 --- a/test/cases/safety/error return trace across suspend points.zig +++ b/test/cases/safety/error return trace across suspend points.zig @@ -1,6 +1,5 @@ const std = @import("std"); - pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn { _ = message; _ = stack_trace; @@ -36,4 +35,4 @@ fn printTrace(p: anyframe->anyerror!void) void { } // run // backend=stage1 -// target=native \ No newline at end of file +// target=native diff --git a/test/cases/safety/exact division failure - vectors.zig b/test/cases/safety/exact division failure - vectors.zig index 3c515cbdf118..cc8c01db9e3b 100644 --- a/test/cases/safety/exact division failure - vectors.zig +++ b/test/cases/safety/exact division failure - vectors.zig @@ -9,8 +9,8 @@ pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usi } pub fn main() !void { - var a: @Vector(4, i32) = [4]i32{111, 222, 333, 444}; - var b: @Vector(4, i32) = [4]i32{111, 222, 333, 441}; + var a: @Vector(4, i32) = [4]i32{ 111, 222, 333, 444 }; + var b: @Vector(4, i32) = [4]i32{ 111, 222, 333, 441 }; const x = divExact(a, b); _ = x; return error.TestFailed; diff --git a/test/cases/safety/for_len_mismatch_three.zig b/test/cases/safety/for_len_mismatch_three.zig index 95bc24426994..0cfcddbfeecc 100644 --- a/test/cases/safety/for_len_mismatch_three.zig +++ b/test/cases/safety/for_len_mismatch_three.zig @@ -21,4 +21,3 @@ pub fn main() !void { // run // backend=llvm // target=native - diff --git a/test/cases/safety/integer division by zero - vectors.zig b/test/cases/safety/integer division by zero - vectors.zig index ed7ac7777b4b..a376b66fdb85 100644 --- a/test/cases/safety/integer division by zero - vectors.zig +++ b/test/cases/safety/integer division by zero - vectors.zig @@ -8,8 +8,8 @@ pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace, _: ?usi std.process.exit(1); } pub fn main() !void { - var a: @Vector(4, i32) = [4]i32{111, 222, 333, 444}; - var b: @Vector(4, i32) = [4]i32{111, 0, 333, 444}; + var a: @Vector(4, i32) = [4]i32{ 111, 222, 333, 444 }; + var b: @Vector(4, i32) = [4]i32{ 111, 0, 333, 444 }; const x = div0(a, b); _ = x; return error.TestFailed; diff --git a/test/cases/unused_labels.3.zig b/test/cases/unused_labels.3.zig index e4f3ac05a404..36c9aa2319b0 100644 --- a/test/cases/unused_labels.3.zig +++ b/test/cases/unused_labels.3.zig @@ -1,5 +1,7 @@ comptime { - blk: {blk: {}} + blk: { + blk: {} + } } // error diff --git a/test/cases/variable_shadowing.3.zig b/test/cases/variable_shadowing.3.zig index 3d899e72cc86..af16bccc1478 100644 --- a/test/cases/variable_shadowing.3.zig +++ b/test/cases/variable_shadowing.3.zig @@ -1,7 +1,6 @@ pub fn main() void { var i = 0; - for ("n", 0..) |_, i| { - } + for ("n", 0..) |_, i| {} } // error diff --git a/test/cases/variable_shadowing.4.zig b/test/cases/variable_shadowing.4.zig index ab9f93d17d4a..3653c2457de7 100644 --- a/test/cases/variable_shadowing.4.zig +++ b/test/cases/variable_shadowing.4.zig @@ -1,7 +1,6 @@ pub fn main() void { var i = 0; - for ("n") |i| { - } + for ("n") |i| {} } // error diff --git a/test/cases/variable_shadowing.5.zig b/test/cases/variable_shadowing.5.zig index 1a5676190ada..78247e68feaa 100644 --- a/test/cases/variable_shadowing.5.zig +++ b/test/cases/variable_shadowing.5.zig @@ -1,7 +1,6 @@ pub fn main() void { var i = 0; - while ("n") |i| { - } + while ("n") |i| {} } // error diff --git a/test/cases/variable_shadowing.6.zig b/test/cases/variable_shadowing.6.zig index 894a398b61de..a3ae7bc34687 100644 --- a/test/cases/variable_shadowing.6.zig +++ b/test/cases/variable_shadowing.6.zig @@ -2,9 +2,7 @@ pub fn main() void { var i = 0; while ("n") |bruh| { _ = bruh; - } else |i| { - - } + } else |i| {} } // error diff --git a/test/cases/x86_64-linux/inline_assembly.2.zig b/test/cases/x86_64-linux/inline_assembly.2.zig index 1695265ab4eb..7958d0df71ea 100644 --- a/test/cases/x86_64-linux/inline_assembly.2.zig +++ b/test/cases/x86_64-linux/inline_assembly.2.zig @@ -2,7 +2,7 @@ pub fn main() void { var bruh: u32 = 1; asm ("" : - : [bruh] "{rax}" (4) + : [bruh] "{rax}" (4), : "memory" ); } diff --git a/test/cases/x86_64-linux/inline_assembly.3.zig b/test/cases/x86_64-linux/inline_assembly.3.zig index 765eaeb97eb2..a6f57e832a74 100644 --- a/test/cases/x86_64-linux/inline_assembly.3.zig +++ b/test/cases/x86_64-linux/inline_assembly.3.zig @@ -2,7 +2,7 @@ pub fn main() void {} comptime { asm ("" : - : [bruh] "{rax}" (4) + : [bruh] "{rax}" (4), : "memory" ); } diff --git a/test/link/bss/build.zig b/test/link/bss/build.zig index 3c7964fae1ef..d9cfeda96dd1 100644 --- a/test/link/bss/build.zig +++ b/test/link/bss/build.zig @@ -4,9 +4,12 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test"); b.default_step = test_step; + const mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const exe = b.addExecutable(.{ .name = "bss", - .root_source_file = .{ .path = "main.zig" }, + .main_module = mod, .optimize = .Debug, }); diff --git a/test/link/common_symbols/build.zig b/test/link/common_symbols/build.zig index a2104e1ac27b..9287f864f18f 100644 --- a/test/link/common_symbols/build.zig +++ b/test/link/common_symbols/build.zig @@ -11,15 +11,22 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_a_mod = b.createModule(.{ .c_source_files = .{ + .files = &.{ "c.c", "a.c", "b.c" }, + .flags = &.{"-fcommon"}, + } }); const lib_a = b.addStaticLibrary(.{ .name = "a", + .main_module = lib_a_mod, .optimize = optimize, .target = .{}, }); - lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"}); + const test_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const test_exe = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = test_mod, .optimize = optimize, }); test_exe.linkLibrary(lib_a); diff --git a/test/link/common_symbols_alignment/build.zig b/test/link/common_symbols_alignment/build.zig index 4cb253dc05e0..552bdb9ff643 100644 --- a/test/link/common_symbols_alignment/build.zig +++ b/test/link/common_symbols_alignment/build.zig @@ -11,15 +11,24 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_a_module = b.createModule(.{ + .c_source_files = .{ + .files = &.{"a.c"}, + .flags = &.{"-fcommon"}, + }, + }); const lib_a = b.addStaticLibrary(.{ .name = "a", + .main_module = lib_a_module, .optimize = optimize, .target = .{}, }); - lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"}); + const test_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const test_exe = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = test_mod, .optimize = optimize, }); test_exe.linkLibrary(lib_a); diff --git a/test/link/glibc_compat/build.zig b/test/link/glibc_compat/build.zig index 8d850cf350cc..f27f8ac59d6e 100644 --- a/test/link/glibc_compat/build.zig +++ b/test/link/glibc_compat/build.zig @@ -5,9 +5,12 @@ pub fn build(b: *std.Build) void { b.default_step = test_step; inline for (.{ "aarch64-linux-gnu.2.27", "aarch64-linux-gnu.2.34" }) |t| { + const mod = b.createModule(.{ + .source_file = .{ .path = "main.c" }, + }); const exe = b.addExecutable(.{ .name = t, - .root_source_file = .{ .path = "main.c" }, + .main_module = mod, .target = std.zig.CrossTarget.parse( .{ .arch_os_abi = t }, ) catch unreachable, diff --git a/test/link/interdependent_static_c_libs/build.zig b/test/link/interdependent_static_c_libs/build.zig index 2eb8fa27b4e7..5129feabdce4 100644 --- a/test/link/interdependent_static_c_libs/build.zig +++ b/test/link/interdependent_static_c_libs/build.zig @@ -11,24 +11,39 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_a_module = b.createModule(.{ + .c_source_files = .{ + .files = &.{"a.c"}, + .flags = &.{}, + }, + }); const lib_a = b.addStaticLibrary(.{ .name = "a", + .main_module = lib_a_module, .optimize = optimize, .target = .{}, }); - lib_a.addCSourceFile("a.c", &[_][]const u8{}); lib_a.addIncludePath("."); + const lib_b_module = b.createModule(.{ + .c_source_files = .{ + .files = &.{"b.c"}, + .flags = &.{}, + }, + }); const lib_b = b.addStaticLibrary(.{ .name = "b", + .main_module = lib_b_module, .optimize = optimize, .target = .{}, }); - lib_b.addCSourceFile("b.c", &[_][]const u8{}); lib_b.addIncludePath("."); + const test_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const test_exe = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = test_mod, .optimize = optimize, }); test_exe.linkLibrary(lib_a); diff --git a/test/link/macho/bugs/13056/build.zig b/test/link/macho/bugs/13056/build.zig index 1ac50a61c62d..10ed56e8d66a 100644 --- a/test/link/macho/bugs/13056/build.zig +++ b/test/link/macho/bugs/13056/build.zig @@ -19,16 +19,22 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const sdk = std.zig.system.darwin.getDarwinSDK(b.allocator, target_info.target) orelse @panic("macOS SDK is required to run the test"); + const exe_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"test.cpp"}, + .flags = &.{ + "-nostdlib++", + "-nostdinc++", + }, + }, + }); const exe = b.addExecutable(.{ .name = "test", + .main_module = exe_mod, .optimize = optimize, }); exe.addSystemIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include" }) catch unreachable); exe.addIncludePath(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/include/c++/v1" }) catch unreachable); - exe.addCSourceFile("test.cpp", &.{ - "-nostdlib++", - "-nostdinc++", - }); exe.addObjectFile(std.fs.path.join(b.allocator, &.{ sdk.path, "/usr/lib/libc++.tbd" }) catch unreachable); const run_cmd = b.addRunArtifact(exe); diff --git a/test/link/macho/bugs/13457/build.zig b/test/link/macho/bugs/13457/build.zig index 89096bba387d..9dc8d3578000 100644 --- a/test/link/macho/bugs/13457/build.zig +++ b/test/link/macho/bugs/13457/build.zig @@ -15,9 +15,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = .{ .path = "main.zig" }, + .main_module = mod, .optimize = optimize, .target = target, }); diff --git a/test/link/macho/dead_strip/build.zig b/test/link/macho/dead_strip/build.zig index 4c739b3d8c6c..5fe8858e2c23 100644 --- a/test/link/macho/dead_strip/build.zig +++ b/test/link/macho/dead_strip/build.zig @@ -43,12 +43,18 @@ fn createScenario( target: std.zig.CrossTarget, name: []const u8, ) *std.Build.CompileStep { + const mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = name, + .main_module = mod, .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); exe.linkLibC(); return exe; } diff --git a/test/link/macho/dead_strip_dylibs/build.zig b/test/link/macho/dead_strip_dylibs/build.zig index 47e53f853e26..b6debe52498a 100644 --- a/test/link/macho/dead_strip_dylibs/build.zig +++ b/test/link/macho/dead_strip_dylibs/build.zig @@ -49,6 +49,7 @@ fn createScenario( ) *std.Build.CompileStep { const exe = b.addExecutable(.{ .name = name, + .main_module = b.createModule(.{}), .optimize = optimize, }); exe.addCSourceFile("main.c", &[0][]const u8{}); diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index a4085b51e620..d7aa78ba497b 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -15,13 +15,19 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const dylib_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"a.c"}, + .flags = &.{}, + }, + }); const dylib = b.addSharedLibrary(.{ .name = "a", .version = .{ .major = 1, .minor = 0 }, + .main_module = dylib_mod, .optimize = optimize, .target = target, }); - dylib.addCSourceFile("a.c", &.{}); dylib.linkLibC(); const check_dylib = dylib.checkObject(); @@ -33,12 +39,18 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize test_step.dependOn(&check_dylib.step); + const exe_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "main", + .main_module = exe_mod, .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); exe.linkSystemLibrary("a"); exe.addLibraryPathDirectorySource(dylib.getOutputDirectorySource()); exe.addRPathDirectorySource(dylib.getOutputDirectorySource()); diff --git a/test/link/macho/empty/build.zig b/test/link/macho/empty/build.zig index 9933746d533a..6fb65c92aee5 100644 --- a/test/link/macho/empty/build.zig +++ b/test/link/macho/empty/build.zig @@ -15,13 +15,18 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{ "main.c", "empty.c" }, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "test", + .main_module = mod, .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); - exe.addCSourceFile("empty.c", &[0][]const u8{}); exe.linkLibC(); const run_cmd = b.addRunArtifact(exe); diff --git a/test/link/macho/entry/build.zig b/test/link/macho/entry/build.zig index e983bc9391cf..64fa86d56cdb 100644 --- a/test/link/macho/entry/build.zig +++ b/test/link/macho/entry/build.zig @@ -13,12 +13,18 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "main", + .main_module = mod, .optimize = optimize, .target = .{ .os_tag = .macos }, }); - exe.addCSourceFile("main.c", &.{}); exe.linkLibC(); exe.entry_symbol_name = "_non_main"; diff --git a/test/link/macho/entry_in_archive/build.zig b/test/link/macho/entry_in_archive/build.zig index f4b04bd162aa..f1fb6bf01005 100644 --- a/test/link/macho/entry_in_archive/build.zig +++ b/test/link/macho/entry_in_archive/build.zig @@ -13,16 +13,24 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const lib = b.addStaticLibrary(.{ .name = "main", + .main_module = lib_mod, .optimize = optimize, .target = .{ .os_tag = .macos }, }); - lib.addCSourceFile("main.c", &.{}); lib.linkLibC(); + const exe_mod = b.createModule(.{}); const exe = b.addExecutable(.{ .name = "main", + .main_module = exe_mod, .optimize = optimize, .target = .{ .os_tag = .macos }, }); diff --git a/test/link/macho/entry_in_dylib/build.zig b/test/link/macho/entry_in_dylib/build.zig index 135661872dbb..ddd6623fa90c 100644 --- a/test/link/macho/entry_in_dylib/build.zig +++ b/test/link/macho/entry_in_dylib/build.zig @@ -13,21 +13,33 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"bootstrap.c"}, + .flags = &.{}, + }, + }); const lib = b.addSharedLibrary(.{ .name = "bootstrap", + .main_module = lib_mod, .optimize = optimize, .target = .{ .os_tag = .macos }, }); - lib.addCSourceFile("bootstrap.c", &.{}); lib.linkLibC(); lib.linker_allow_shlib_undefined = true; + const exe_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "main", + .main_module = exe_mod, .optimize = optimize, .target = .{ .os_tag = .macos }, }); - exe.addCSourceFile("main.c", &.{}); exe.linkLibrary(lib); exe.linkLibC(); exe.entry_symbol_name = "_bootstrap"; diff --git a/test/link/macho/headerpad/build.zig b/test/link/macho/headerpad/build.zig index 22cfcc90eca2..6686b038be4c 100644 --- a/test/link/macho/headerpad/build.zig +++ b/test/link/macho/headerpad/build.zig @@ -107,6 +107,7 @@ fn simpleExe( ) *std.Build.CompileStep { const exe = b.addExecutable(.{ .name = name, + .main_module = b.createModule(.{}), .optimize = optimize, }); exe.addCSourceFile("main.c", &.{}); diff --git a/test/link/macho/linksection/build.zig b/test/link/macho/linksection/build.zig index b8b3a59f35ba..e5814b8bfaa6 100644 --- a/test/link/macho/linksection/build.zig +++ b/test/link/macho/linksection/build.zig @@ -17,7 +17,9 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const obj = b.addObject(.{ .name = "test", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, .target = target, }); diff --git a/test/link/macho/needed_framework/build.zig b/test/link/macho/needed_framework/build.zig index fc80a44f734b..b13704c826d9 100644 --- a/test/link/macho/needed_framework/build.zig +++ b/test/link/macho/needed_framework/build.zig @@ -16,11 +16,17 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { // -dead_strip_dylibs // -needed_framework Cocoa + const exe_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "test", + .main_module = exe_mod, .optimize = optimize, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); exe.linkLibC(); exe.linkFrameworkNeeded("Cocoa"); exe.dead_strip_dylibs = true; diff --git a/test/link/macho/needed_library/build.zig b/test/link/macho/needed_library/build.zig index cb9ea38d4bcc..08a90a75a833 100644 --- a/test/link/macho/needed_library/build.zig +++ b/test/link/macho/needed_library/build.zig @@ -15,23 +15,35 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const dylib_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"a.c"}, + .flags = &.{}, + }, + }); const dylib = b.addSharedLibrary(.{ .name = "a", + .main_module = dylib_mod, .version = .{ .major = 1, .minor = 0 }, .optimize = optimize, .target = target, }); - dylib.addCSourceFile("a.c", &.{}); dylib.linkLibC(); // -dead_strip_dylibs // -needed-la + const exe_mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "test", + .main_module = exe_mod, .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &[0][]const u8{}); exe.linkLibC(); exe.linkSystemLibraryNeeded("a"); exe.addLibraryPathDirectorySource(dylib.getOutputDirectorySource()); diff --git a/test/link/macho/objc/build.zig b/test/link/macho/objc/build.zig index 4cd12f786fe1..290be5d87686 100644 --- a/test/link/macho/objc/build.zig +++ b/test/link/macho/objc/build.zig @@ -14,13 +14,18 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{ "Foo.m", "test.m" }, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "test", + .main_module = mod, .optimize = optimize, }); exe.addIncludePath("."); - exe.addCSourceFile("Foo.m", &[0][]const u8{}); - exe.addCSourceFile("test.m", &[0][]const u8{}); exe.linkLibC(); // TODO when we figure out how to ship framework stubs for cross-compilation, // populate paths to the sysroot here. diff --git a/test/link/macho/objcpp/build.zig b/test/link/macho/objcpp/build.zig index aebf0e17fd93..8483744d6955 100644 --- a/test/link/macho/objcpp/build.zig +++ b/test/link/macho/objcpp/build.zig @@ -14,14 +14,19 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{ "Foo.mm", "test.mm" }, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "test", + .main_module = mod, .optimize = optimize, }); b.default_step.dependOn(&exe.step); exe.addIncludePath("."); - exe.addCSourceFile("Foo.mm", &[0][]const u8{}); - exe.addCSourceFile("test.mm", &[0][]const u8{}); exe.linkLibCpp(); // TODO when we figure out how to ship framework stubs for cross-compilation, // populate paths to the sysroot here. diff --git a/test/link/macho/pagezero/build.zig b/test/link/macho/pagezero/build.zig index b467df2b20b7..fa548ef6aed1 100644 --- a/test/link/macho/pagezero/build.zig +++ b/test/link/macho/pagezero/build.zig @@ -10,12 +10,18 @@ pub fn build(b: *std.Build) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; { + const mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "pagezero", + .main_module = mod, .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); exe.linkLibC(); exe.pagezero_size = 0x4000; @@ -32,12 +38,18 @@ pub fn build(b: *std.Build) void { } { + const mod = b.createModule(.{ + .c_source_files = .{ + .files = &.{"main.c"}, + .flags = &.{}, + }, + }); const exe = b.addExecutable(.{ .name = "no_pagezero", + .main_module = mod, .optimize = optimize, .target = target, }); - exe.addCSourceFile("main.c", &.{}); exe.linkLibC(); exe.pagezero_size = 0; diff --git a/test/link/macho/search_strategy/build.zig b/test/link/macho/search_strategy/build.zig index 4777629c8b9a..cdcb2c3effcc 100644 --- a/test/link/macho/search_strategy/build.zig +++ b/test/link/macho/search_strategy/build.zig @@ -49,6 +49,7 @@ fn createScenario( ) *std.Build.CompileStep { const static = b.addStaticLibrary(.{ .name = name, + .main_module = b.createModule(.{}), .optimize = optimize, .target = target, }); @@ -60,6 +61,7 @@ fn createScenario( const dylib = b.addSharedLibrary(.{ .name = name, + .main_module = b.createModule(.{}), .version = .{ .major = 1, .minor = 0 }, .optimize = optimize, .target = target, @@ -72,6 +74,7 @@ fn createScenario( const exe = b.addExecutable(.{ .name = name, + .main_module = b.createModule(.{}), .optimize = optimize, .target = target, }); diff --git a/test/link/macho/stack_size/build.zig b/test/link/macho/stack_size/build.zig index c7d308d004e4..b245e1e0f11c 100644 --- a/test/link/macho/stack_size/build.zig +++ b/test/link/macho/stack_size/build.zig @@ -15,8 +15,10 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const mod = b.createModule(.{}); const exe = b.addExecutable(.{ .name = "main", + .main_module = mod, .optimize = optimize, .target = target, }); diff --git a/test/link/macho/strict_validation/build.zig b/test/link/macho/strict_validation/build.zig index 34a0cd73fc1c..facce5a65b59 100644 --- a/test/link/macho/strict_validation/build.zig +++ b/test/link/macho/strict_validation/build.zig @@ -16,9 +16,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const exe = b.addExecutable(.{ .name = "main", - .root_source_file = .{ .path = "main.zig" }, + .main_module = mod, .optimize = optimize, .target = target, }); diff --git a/test/link/macho/tls/build.zig b/test/link/macho/tls/build.zig index f155f514f881..23cb4beb9be7 100644 --- a/test/link/macho/tls/build.zig +++ b/test/link/macho/tls/build.zig @@ -15,8 +15,10 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const lib_mod = b.createModule(.{}); const lib = b.addSharedLibrary(.{ .name = "a", + .main_module = lib_mod, .version = .{ .major = 1, .minor = 0 }, .optimize = optimize, .target = target, @@ -24,8 +26,11 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize lib.addCSourceFile("a.c", &.{}); lib.linkLibC(); + const test_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const test_exe = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = test_mod, .optimize = optimize, .target = target, }); diff --git a/test/link/macho/unwind_info/build.zig b/test/link/macho/unwind_info/build.zig index 4ace2a4e96d3..6e3ba8e1169b 100644 --- a/test/link/macho/unwind_info/build.zig +++ b/test/link/macho/unwind_info/build.zig @@ -68,6 +68,7 @@ fn createScenario( ) *std.Build.CompileStep { const exe = b.addExecutable(.{ .name = name, + .main_module = b.createModule(.{}), .optimize = optimize, .target = target, }); diff --git a/test/link/macho/weak_framework/build.zig b/test/link/macho/weak_framework/build.zig index 5dfd421243ff..3c7e8b664d93 100644 --- a/test/link/macho/weak_framework/build.zig +++ b/test/link/macho/weak_framework/build.zig @@ -16,6 +16,7 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const exe = b.addExecutable(.{ .name = "test", + .main_module = b.createModule(.{}), .optimize = optimize, }); exe.addCSourceFile("main.c", &[0][]const u8{}); diff --git a/test/link/macho/weak_library/build.zig b/test/link/macho/weak_library/build.zig index 88d72ad48775..5c3292a1dccc 100644 --- a/test/link/macho/weak_library/build.zig +++ b/test/link/macho/weak_library/build.zig @@ -15,8 +15,10 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const target: std.zig.CrossTarget = .{ .os_tag = .macos }; + const dylib_mod = b.createModule(.{}); const dylib = b.addSharedLibrary(.{ .name = "a", + .main_module = dylib_mod, .version = .{ .major = 1, .minor = 0, .patch = 0 }, .target = target, .optimize = optimize, @@ -25,8 +27,10 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize dylib.linkLibC(); b.installArtifact(dylib); + const exe_mod = b.createModule(.{}); const exe = b.addExecutable(.{ .name = "test", + .main_module = exe_mod, .target = target, .optimize = optimize, }); diff --git a/test/link/wasm/archive/build.zig b/test/link/wasm/archive/build.zig index 120134af196e..0e67fc493b71 100644 --- a/test/link/wasm/archive/build.zig +++ b/test/link/wasm/archive/build.zig @@ -15,9 +15,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { // The code in question will pull-in compiler-rt, // and therefore link with its archive file. + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "main", - .root_source_file = .{ .path = "main.zig" }, + .main_module = lib_mod, .optimize = optimize, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, }); diff --git a/test/link/wasm/basic-features/build.zig b/test/link/wasm/basic-features/build.zig index be709a698fa2..f3c14c10afe8 100644 --- a/test/link/wasm/basic-features/build.zig +++ b/test/link/wasm/basic-features/build.zig @@ -4,9 +4,12 @@ pub const requires_stage2 = true; pub fn build(b: *std.Build) void { // Library with explicitly set cpu features + const mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "main.zig" }, + .main_module = mod, .optimize = .Debug, .target = .{ .cpu_arch = .wasm32, diff --git a/test/link/wasm/bss/build.zig b/test/link/wasm/bss/build.zig index 4a26e78a1200..652e5c7554a5 100644 --- a/test/link/wasm/bss/build.zig +++ b/test/link/wasm/bss/build.zig @@ -14,9 +14,12 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.OptimizeMode, is_safe: bool) void { { + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = lib_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize_mode, }); @@ -57,9 +60,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize_mode: std.builtin.Opt // verify zero'd declaration is stored in bss for all optimization modes. { + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "lib2.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib2.zig" }, + .main_module = lib_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize_mode, }); diff --git a/test/link/wasm/export-data/build.zig b/test/link/wasm/export-data/build.zig index 38b8c3e19e82..135bb3f50e79 100644 --- a/test/link/wasm/export-data/build.zig +++ b/test/link/wasm/export-data/build.zig @@ -9,9 +9,12 @@ pub fn build(b: *std.Build) void { return; } + const mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = mod, .optimize = .ReleaseSafe, // to make the output deterministic in address positions .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, }); diff --git a/test/link/wasm/export/build.zig b/test/link/wasm/export/build.zig index 794201dbf6c9..d68d2bc64474 100644 --- a/test/link/wasm/export/build.zig +++ b/test/link/wasm/export/build.zig @@ -13,18 +13,24 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const no_export_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const no_export = b.addSharedLibrary(.{ .name = "no-export", - .root_source_file = .{ .path = "main.zig" }, + .main_module = no_export_mod, .optimize = optimize, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, }); no_export.use_llvm = false; no_export.use_lld = false; + const dynamic_export_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const dynamic_export = b.addSharedLibrary(.{ .name = "dynamic", - .root_source_file = .{ .path = "main.zig" }, + .main_module = dynamic_export_mod, .optimize = optimize, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, }); @@ -32,9 +38,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize dynamic_export.use_llvm = false; dynamic_export.use_lld = false; + const force_export_mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const force_export = b.addSharedLibrary(.{ .name = "force", - .root_source_file = .{ .path = "main.zig" }, + .main_module = force_export_mod, .optimize = optimize, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, }); diff --git a/test/link/wasm/extern-mangle/build.zig b/test/link/wasm/extern-mangle/build.zig index 6c292acbab6e..fd9291ad1be2 100644 --- a/test/link/wasm/extern-mangle/build.zig +++ b/test/link/wasm/extern-mangle/build.zig @@ -11,9 +11,12 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = lib_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); diff --git a/test/link/wasm/extern/build.zig b/test/link/wasm/extern/build.zig index 55562143c2f0..472b171ee287 100644 --- a/test/link/wasm/extern/build.zig +++ b/test/link/wasm/extern/build.zig @@ -13,13 +13,16 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const exe_mod = b.createModule(.{ .source_file = .{ .path = "main.zig" }, .c_source_files = .{ + .files = &.{"foo.c"}, + .flags = &.{}, + } }); const exe = b.addExecutable(.{ .name = "extern", - .root_source_file = .{ .path = "main.zig" }, + .main_module = exe_mod, .optimize = optimize, .target = .{ .cpu_arch = .wasm32, .os_tag = .wasi }, }); - exe.addCSourceFile("foo.c", &.{}); exe.use_llvm = false; exe.use_lld = false; diff --git a/test/link/wasm/function-table/build.zig b/test/link/wasm/function-table/build.zig index 4ce62947277b..9cb10bbcc0b3 100644 --- a/test/link/wasm/function-table/build.zig +++ b/test/link/wasm/function-table/build.zig @@ -13,9 +13,12 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const import_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const import_table = b.addSharedLibrary(.{ .name = "import_table", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = import_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); @@ -23,9 +26,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize import_table.use_lld = false; import_table.import_table = true; + const export_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const export_table = b.addSharedLibrary(.{ .name = "export_table", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = export_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); @@ -33,9 +39,12 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize export_table.use_lld = false; export_table.export_table = true; + const regular_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const regular_table = b.addSharedLibrary(.{ .name = "regular_table", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = regular_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); diff --git a/test/link/wasm/infer-features/build.zig b/test/link/wasm/infer-features/build.zig index 00fb48651b03..6c9a1049bb6f 100644 --- a/test/link/wasm/infer-features/build.zig +++ b/test/link/wasm/infer-features/build.zig @@ -4,8 +4,14 @@ pub const requires_stage2 = true; pub fn build(b: *std.Build) void { // Wasm Object file which we will use to infer the features from + const c_obj_mod = b.createModule(.{ .c_source_files = .{ + .files = &.{"foo.c"}, + .flags = &.{}, + } }); + const c_obj = b.addObject(.{ .name = "c_obj", + .main_module = c_obj_mod, .optimize = .Debug, .target = .{ .cpu_arch = .wasm32, @@ -13,13 +19,15 @@ pub fn build(b: *std.Build) void { .os_tag = .freestanding, }, }); - c_obj.addCSourceFile("foo.c", &.{}); // Wasm library that doesn't have any features specified. This will // infer its featureset from other linked object files. + const mod = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "main.zig" }, + .main_module = mod, .optimize = .Debug, .target = .{ .cpu_arch = .wasm32, diff --git a/test/link/wasm/producers/build.zig b/test/link/wasm/producers/build.zig index 439a66cfa51c..94dd6f4a8d90 100644 --- a/test/link/wasm/producers/build.zig +++ b/test/link/wasm/producers/build.zig @@ -14,9 +14,12 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = lib_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); diff --git a/test/link/wasm/segments/build.zig b/test/link/wasm/segments/build.zig index 59fab9bb84ca..5c433d144fbc 100644 --- a/test/link/wasm/segments/build.zig +++ b/test/link/wasm/segments/build.zig @@ -13,9 +13,12 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = lib_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); diff --git a/test/link/wasm/stack_pointer/build.zig b/test/link/wasm/stack_pointer/build.zig index bf643c1e3a39..e5b7514a8b31 100644 --- a/test/link/wasm/stack_pointer/build.zig +++ b/test/link/wasm/stack_pointer/build.zig @@ -13,9 +13,12 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = lib_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); diff --git a/test/link/wasm/type/build.zig b/test/link/wasm/type/build.zig index 7a779f6c21ff..f05654d81de5 100644 --- a/test/link/wasm/type/build.zig +++ b/test/link/wasm/type/build.zig @@ -13,9 +13,12 @@ pub fn build(b: *std.Build) void { } fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "lib.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "lib", - .root_source_file = .{ .path = "lib.zig" }, + .main_module = lib_mod, .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding }, .optimize = optimize, }); diff --git a/test/src/Cases.zig b/test/src/Cases.zig index b2c8ff555e99..2596fc8d6b85 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -484,28 +484,44 @@ pub fn lowerToBuildSteps( for (update.files.items) |file| { writefiles.add(file.path, file.src); } + + var deps = std.ArrayList(std.Build.ModuleDependency).init(b.allocator); + deps.ensureUnusedCapacity(case.deps.items.len) catch @panic("OOM"); + for (case.deps.items) |dep| { + deps.appendAssumeCapacity(.{ + .name = dep.name, + .module = b.createModule(.{ + .source_file = writefiles.getFileSource(dep.path).?, + }), + }); + } + const root_source_file = writefiles.getFileSource(update.files.items[0].path).?; + const main_mod = b.createModule(.{ + .source_file = root_source_file, + .dependencies = deps.items, + }); const artifact = if (case.is_test) b.addTest(.{ - .root_source_file = root_source_file, + .main_module = main_mod, .name = case.name, .target = case.target, .optimize = case.optimize_mode, }) else switch (case.output_mode) { .Obj => b.addObject(.{ - .root_source_file = root_source_file, + .main_module = main_mod, .name = case.name, .target = case.target, .optimize = case.optimize_mode, }), .Lib => b.addStaticLibrary(.{ - .root_source_file = root_source_file, + .main_module = main_mod, .name = case.name, .target = case.target, .optimize = case.optimize_mode, }), .Exe => b.addExecutable(.{ - .root_source_file = root_source_file, + .main_module = main_mod, .name = case.name, .target = case.target, .optimize = case.optimize_mode, @@ -525,12 +541,6 @@ pub fn lowerToBuildSteps( }, } - for (case.deps.items) |dep| { - artifact.addAnonymousModule(dep.name, .{ - .source_file = writefiles.getFileSource(dep.path).?, - }); - } - switch (update.case) { .Compile => { parent_step.dependOn(&artifact.step); diff --git a/test/src/CompareOutput.zig b/test/src/CompareOutput.zig index fb89082defc9..bdab5d00875e 100644 --- a/test/src/CompareOutput.zig +++ b/test/src/CompareOutput.zig @@ -96,6 +96,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { const exe = b.addExecutable(.{ .name = "test", + .main_module = b.createModule(.{}), .target = .{}, .optimize = .Debug, }); @@ -120,7 +121,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { const basename = case.sources.items[0].filename; const exe = b.addExecutable(.{ .name = "test", - .root_source_file = write_src.getFileSource(basename).?, + .main_module = b.createModule(.{ + .source_file = write_src.getFileSource(basename).?, + }), .optimize = optimize, .target = .{}, }); @@ -147,7 +150,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void { const basename = case.sources.items[0].filename; const exe = b.addExecutable(.{ .name = "test", - .root_source_file = write_src.getFileSource(basename).?, + .main_module = b.createModule(.{ + .source_file = write_src.getFileSource(basename).?, + }), .target = .{}, .optimize = .Debug, }); diff --git a/test/src/StackTrace.zig b/test/src/StackTrace.zig index c32720a210b3..61cb9b1d955b 100644 --- a/test/src/StackTrace.zig +++ b/test/src/StackTrace.zig @@ -74,9 +74,12 @@ fn addExpect( const src_basename = "source.zig"; const write_src = b.addWriteFile(src_basename, source); + const mod = b.createModule(.{ + .source_file = write_src.getFileSource(src_basename).?, + }); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = write_src.getFileSource(src_basename).?, + .main_module = mod, .optimize = optimize_mode, .target = .{}, }); diff --git a/test/standalone.zig b/test/standalone.zig index d203e095d030..4ebe9a26d6a8 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -218,30 +218,6 @@ pub const build_cases = [_]BuildCase{ // .build_root = "test/standalone/options", // .import = @import("standalone/options/build.zig"), //}, - .{ - .build_root = "test/standalone/module_add_include_path", - .import = @import("standalone/module_add_include_path/build.zig"), - }, - .{ - .build_root = "test/standalone/module_link_library", - .import = @import("standalone/module_link_library/build.zig"), - }, - .{ - .build_root = "test/standalone/module_install_header", - .import = @import("standalone/module_install_header/build.zig"), - }, - .{ - .build_root = "test/standalone/module_link_libc_cpp", - .import = @import("standalone/module_link_libc_cpp/build.zig"), - }, - .{ - .build_root = "test/standalone/module_add_options", - .import = @import("standalone/module_add_options/build.zig"), - }, - .{ - .build_root = "test/standalone/strip_empty_loop", - .import = @import("standalone/strip_empty_loop/build.zig"), - }, }; const std = @import("std"); diff --git a/test/standalone/c_compiler/build.zig b/test/standalone/c_compiler/build.zig index 6c5f2b4db6b9..216d1a003cbe 100644 --- a/test/standalone/c_compiler/build.zig +++ b/test/standalone/c_compiler/build.zig @@ -16,6 +16,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const exe_c = b.addExecutable(.{ .name = "test_c", + .main_module = b.createModule(.{}), .optimize = optimize, .target = target, }); @@ -24,6 +25,7 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize const exe_cpp = b.addExecutable(.{ .name = "test_cpp", + .main_module = b.createModule(.{}), .optimize = optimize, .target = target, }); diff --git a/test/standalone/dep_diamond/build.zig b/test/standalone/dep_diamond/build.zig index 6c6d9b6d1a25..2d5a4820e005 100644 --- a/test/standalone/dep_diamond/build.zig +++ b/test/standalone/dep_diamond/build.zig @@ -9,21 +9,27 @@ pub fn build(b: *std.Build) void { const shared = b.createModule(.{ .source_file = .{ .path = "shared.zig" }, }); - - const exe = b.addExecutable(.{ - .name = "test", - .root_source_file = .{ .path = "test.zig" }, - .optimize = optimize, - }); - exe.addAnonymousModule("foo", .{ + const foo = b.createModule(.{ .source_file = .{ .path = "foo.zig" }, .dependencies = &.{.{ .name = "shared", .module = shared }}, }); - exe.addAnonymousModule("bar", .{ + const bar = b.createModule(.{ .source_file = .{ .path = "bar.zig" }, .dependencies = &.{.{ .name = "shared", .module = shared }}, }); + const exe = b.addExecutable(.{ + .name = "test", + .main_module = b.createModule(.{ + .source_file = .{ .path = "test.zig" }, + .dependencies = &.{ + .{ .name = "foo", .module = foo }, + .{ .name = "bar", .module = bar }, + }, + }), + .optimize = optimize, + }); + const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); } diff --git a/test/standalone/dep_mutually_recursive/build.zig b/test/standalone/dep_mutually_recursive/build.zig index e70e77540182..138e3cbc270c 100644 --- a/test/standalone/dep_mutually_recursive/build.zig +++ b/test/standalone/dep_mutually_recursive/build.zig @@ -17,10 +17,11 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "test", - .root_source_file = .{ .path = "test.zig" }, + .main_module = b.createModule(.{ .source_file = .{ .path = "test.zig" }, .dependencies = &.{ + .{ .name = "foo", .module = foo }, + } }), .optimize = optimize, }); - exe.addModule("foo", foo); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/dep_recursive/build.zig b/test/standalone/dep_recursive/build.zig index 2e2cf4ea4d4f..86e79615422e 100644 --- a/test/standalone/dep_recursive/build.zig +++ b/test/standalone/dep_recursive/build.zig @@ -13,10 +13,14 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "test", - .root_source_file = .{ .path = "test.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "test.zig" }, + .dependencies = &.{ + .{ .name = "foo", .module = foo }, + }, + }), .optimize = optimize, }); - exe.addModule("foo", foo); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/dep_shared_builtin/build.zig b/test/standalone/dep_shared_builtin/build.zig index 175118d33192..6f4882ec3093 100644 --- a/test/standalone/dep_shared_builtin/build.zig +++ b/test/standalone/dep_shared_builtin/build.zig @@ -6,14 +6,19 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; + const foo = b.createModule(.{ + .source_file = .{ .path = "foo.zig" }, + }); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = .{ .path = "test.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "test.zig" }, + .dependencies = &.{ + .{ .name = "foo", .module = foo }, + }, + }), .optimize = optimize, }); - exe.addAnonymousModule("foo", .{ - .source_file = .{ .path = "foo.zig" }, - }); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/dep_triangle/build.zig b/test/standalone/dep_triangle/build.zig index 63eed997ceb6..e213a29e108c 100644 --- a/test/standalone/dep_triangle/build.zig +++ b/test/standalone/dep_triangle/build.zig @@ -10,16 +10,22 @@ pub fn build(b: *std.Build) void { .source_file = .{ .path = "shared.zig" }, }); + const foo_mod = b.createModule(.{ + .source_file = .{ .path = "foo.zig" }, + .dependencies = &.{.{ .name = "shared", .module = shared }}, + }); + const exe = b.addExecutable(.{ .name = "test", - .root_source_file = .{ .path = "test.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "test.zig" }, + .dependencies = &.{ + .{ .name = "foo", .module = foo_mod }, + .{ .name = "shared", .module = shared }, + }, + }), .optimize = optimize, }); - exe.addAnonymousModule("foo", .{ - .source_file = .{ .path = "foo.zig" }, - .dependencies = &.{.{ .name = "shared", .module = shared }}, - }); - exe.addModule("shared", shared); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/embed_generated_file/build.zig b/test/standalone/embed_generated_file/build.zig index af1ae0a00f70..168b2a8e0539 100644 --- a/test/standalone/embed_generated_file/build.zig +++ b/test/standalone/embed_generated_file/build.zig @@ -6,7 +6,9 @@ pub fn build(b: *std.Build) void { const bootloader = b.addExecutable(.{ .name = "bootloader", - .root_source_file = .{ .path = "bootloader.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "bootloader.zig" }, + }), .target = .{ .cpu_arch = .x86, .os_tag = .freestanding, @@ -14,13 +16,18 @@ pub fn build(b: *std.Build) void { .optimize = .ReleaseSmall, }); + const bootloader_mod = b.createModule(.{ + .source_file = bootloader.getOutputSource(), + }); const exe = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + .dependencies = &.{ + .{ .name = "bootloader.elf", .module = bootloader_mod }, + }, + }), .optimize = .Debug, }); - exe.addAnonymousModule("bootloader.elf", .{ - .source_file = bootloader.getOutputSource(), - }); test_step.dependOn(&exe.step); } diff --git a/test/standalone/empty_env/build.zig b/test/standalone/empty_env/build.zig index 27ec75be22df..d6cc2106590b 100644 --- a/test/standalone/empty_env/build.zig +++ b/test/standalone/empty_env/build.zig @@ -14,7 +14,9 @@ pub fn build(b: *std.Build) void { const main = b.addExecutable(.{ .name = "main", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, }); diff --git a/test/standalone/extern/build.zig b/test/standalone/extern/build.zig index 153380e91ded..2915a92a428f 100644 --- a/test/standalone/extern/build.zig +++ b/test/standalone/extern/build.zig @@ -5,12 +5,16 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "exports", - .root_source_file = .{ .path = "exports.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "export.zig" }, + }), .target = .{}, .optimize = optimize, }); const main = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, }); main.addObject(obj); diff --git a/test/standalone/global_linkage/build.zig b/test/standalone/global_linkage/build.zig index d560027265cb..846519031ad7 100644 --- a/test/standalone/global_linkage/build.zig +++ b/test/standalone/global_linkage/build.zig @@ -9,20 +9,26 @@ pub fn build(b: *std.Build) void { const obj1 = b.addStaticLibrary(.{ .name = "obj1", - .root_source_file = .{ .path = "obj1.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "obj1.zig" }, + }), .optimize = optimize, .target = target, }); const obj2 = b.addStaticLibrary(.{ .name = "obj2", - .root_source_file = .{ .path = "obj2.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "obj2.zig" }, + }), .optimize = optimize, .target = target, }); const main = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, }); main.linkLibrary(obj1); diff --git a/test/standalone/install_raw_hex/build.zig b/test/standalone/install_raw_hex/build.zig index b34bb013788e..71afd00fe5b1 100644 --- a/test/standalone/install_raw_hex/build.zig +++ b/test/standalone/install_raw_hex/build.zig @@ -17,7 +17,9 @@ pub fn build(b: *std.Build) void { const elf = b.addExecutable(.{ .name = "zig-nrf52-blink.elf", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .target = target, .optimize = optimize, }); diff --git a/test/standalone/issue_11595/build.zig b/test/standalone/issue_11595/build.zig index b03d3c66cedc..cdd10aac606f 100644 --- a/test/standalone/issue_11595/build.zig +++ b/test/standalone/issue_11595/build.zig @@ -15,7 +15,9 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "zigtest", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .target = target, .optimize = optimize, }); diff --git a/test/standalone/issue_12706/build.zig b/test/standalone/issue_12706/build.zig index 04eb826b44d8..49bc852a387f 100644 --- a/test/standalone/issue_12706/build.zig +++ b/test/standalone/issue_12706/build.zig @@ -11,7 +11,9 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "main", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, .target = target, }); diff --git a/test/standalone/issue_13030/build.zig b/test/standalone/issue_13030/build.zig index e31863fee2fb..d9f37807ff0f 100644 --- a/test/standalone/issue_13030/build.zig +++ b/test/standalone/issue_13030/build.zig @@ -15,7 +15,9 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const obj = b.addObject(.{ .name = "main", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, .target = .{}, }); diff --git a/test/standalone/issue_13970/build.zig b/test/standalone/issue_13970/build.zig index cc70144596df..69822e32b80b 100644 --- a/test/standalone/issue_13970/build.zig +++ b/test/standalone/issue_13970/build.zig @@ -5,15 +5,21 @@ pub fn build(b: *std.Build) void { b.default_step = test_step; const test1 = b.addTest(.{ - .root_source_file = .{ .path = "test_root/empty.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "test_root/empty.zig" }, + }), .test_runner = "src/main.zig", }); const test2 = b.addTest(.{ - .root_source_file = .{ .path = "src/empty.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "src/empty.zig" }, + }), .test_runner = "src/main.zig", }); const test3 = b.addTest(.{ - .root_source_file = .{ .path = "empty.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "empty.zig" }, + }), .test_runner = "src/main.zig", }); diff --git a/test/standalone/issue_339/build.zig b/test/standalone/issue_339/build.zig index f4215dbb8bc2..1a1547a11fb0 100644 --- a/test/standalone/issue_339/build.zig +++ b/test/standalone/issue_339/build.zig @@ -9,7 +9,9 @@ pub fn build(b: *std.Build) void { const obj = b.addObject(.{ .name = "test", - .root_source_file = .{ .path = "test.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "test.zig" }, + }), .target = target, .optimize = optimize, }); diff --git a/test/standalone/issue_5825/build.zig b/test/standalone/issue_5825/build.zig index e8e8d4877230..9f2b9bad57dd 100644 --- a/test/standalone/issue_5825/build.zig +++ b/test/standalone/issue_5825/build.zig @@ -12,13 +12,16 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; const obj = b.addObject(.{ .name = "issue_5825", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, .target = target, }); const exe = b.addExecutable(.{ .name = "issue_5825", + .main_module = b.createModule(.{}), .optimize = optimize, .target = target, }); diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig index 8527f4af2c77..5bf841add166 100644 --- a/test/standalone/issue_794/build.zig +++ b/test/standalone/issue_794/build.zig @@ -5,7 +5,9 @@ pub fn build(b: *std.Build) void { b.default_step = test_step; const test_artifact = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), }); test_artifact.addIncludePath("a_directory"); diff --git a/test/standalone/issue_8550/build.zig b/test/standalone/issue_8550/build.zig index 32bb3e10eb89..ee0f27b23116 100644 --- a/test/standalone/issue_8550/build.zig +++ b/test/standalone/issue_8550/build.zig @@ -13,9 +13,12 @@ pub fn build(b: *std.Build) !void { }, }; + const mod = b.createModule(.{ + .source_file = .{ .path = "./main.zig" }, + }); const kernel = b.addExecutable(.{ .name = "kernel", - .root_source_file = .{ .path = "./main.zig" }, + .main_module = mod, .optimize = optimize, .target = target, }); diff --git a/test/standalone/load_dynamic_library/build.zig b/test/standalone/load_dynamic_library/build.zig index 6dec8de7ae76..2195cef559d1 100644 --- a/test/standalone/load_dynamic_library/build.zig +++ b/test/standalone/load_dynamic_library/build.zig @@ -12,7 +12,9 @@ pub fn build(b: *std.Build) void { const lib = b.addSharedLibrary(.{ .name = "add", - .root_source_file = .{ .path = "add.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "add.zig" }, + }), .version = .{ .major = 1, .minor = 0 }, .optimize = optimize, .target = target, @@ -20,7 +22,9 @@ pub fn build(b: *std.Build) void { const main = b.addExecutable(.{ .name = "main", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, .target = target, }); diff --git a/test/standalone/main_pkg_path/build.zig b/test/standalone/main_pkg_path/build.zig index f3eacce767b8..068aa68c6d72 100644 --- a/test/standalone/main_pkg_path/build.zig +++ b/test/standalone/main_pkg_path/build.zig @@ -5,7 +5,9 @@ pub fn build(b: *std.Build) void { b.default_step = test_step; const test_exe = b.addTest(.{ - .root_source_file = .{ .path = "a/test.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "a/test.zig" }, + }), }); test_exe.setMainPkgPath("."); diff --git a/test/standalone/mix_c_files/build.zig b/test/standalone/mix_c_files/build.zig index 39a0da27ad4c..48fb6ea12f4e 100644 --- a/test/standalone/mix_c_files/build.zig +++ b/test/standalone/mix_c_files/build.zig @@ -18,7 +18,9 @@ pub fn build(b: *std.Build) void { fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void { const exe = b.addExecutable(.{ .name = "test", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, }); exe.addCSourceFile("test.c", &[_][]const u8{"-std=c11"}); diff --git a/test/standalone/mix_o_files/build.zig b/test/standalone/mix_o_files/build.zig index 2d9323ff00de..56bfcfb76adc 100644 --- a/test/standalone/mix_o_files/build.zig +++ b/test/standalone/mix_o_files/build.zig @@ -7,15 +7,19 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; const target: std.zig.CrossTarget = .{}; + const obj_mod = b.createModule(.{ + .source_file = .{ .path = "base64.zig" }, + }); const obj = b.addObject(.{ .name = "base64", - .root_source_file = .{ .path = "base64.zig" }, + .main_module = obj_mod, .optimize = optimize, .target = target, }); const exe = b.addExecutable(.{ .name = "test", + .main_module = b.createModule(.{}), .optimize = optimize, .target = target, }); diff --git a/test/standalone/module_add_include_path/build.zig b/test/standalone/module_add_include_path/build.zig deleted file mode 100644 index b3244abe16c0..000000000000 --- a/test/standalone/module_add_include_path/build.zig +++ /dev/null @@ -1,23 +0,0 @@ -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const optimize = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ - .name = "test", - .root_source_file = .{ .path = "test.zig" }, - .optimize = optimize, - }); - var module = b.addModule("test_module", .{ - .source_file = .{ - .path = "test_module.zig", - }, - .dependencies = &.{}, - }); - module.addIncludePath("include"); - - exe.addModule("test_module", module); - - const run = b.addRunArtifact(exe); - b.default_step = &run.step; -} diff --git a/test/standalone/module_add_include_path/include/header.h b/test/standalone/module_add_include_path/include/header.h deleted file mode 100644 index 48b916111ff6..000000000000 --- a/test/standalone/module_add_include_path/include/header.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef HEADER_H -#define HEADER_H - -int add(int a, int b) { - return a + b; -} - -#endif \ No newline at end of file diff --git a/test/standalone/module_add_include_path/test.zig b/test/standalone/module_add_include_path/test.zig deleted file mode 100644 index c0ae9efebe72..000000000000 --- a/test/standalone/module_add_include_path/test.zig +++ /dev/null @@ -1,6 +0,0 @@ -const test_module = @import("test_module"); -const assert = @import("std").debug.assert; - -pub fn main() void { - assert(test_module.header.add(1, 2) == 3); -} diff --git a/test/standalone/module_add_include_path/test_module.zig b/test/standalone/module_add_include_path/test_module.zig deleted file mode 100644 index 2793e36289d7..000000000000 --- a/test/standalone/module_add_include_path/test_module.zig +++ /dev/null @@ -1,3 +0,0 @@ -pub const header = @cImport({ - @cInclude("header.h"); -}); diff --git a/test/standalone/module_add_options/build.zig b/test/standalone/module_add_options/build.zig deleted file mode 100644 index f8d09865dc2a..000000000000 --- a/test/standalone/module_add_options/build.zig +++ /dev/null @@ -1,30 +0,0 @@ -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const optimize = b.standardOptimizeOption(.{}); - const test_opt = b.option(bool, "option", "test option") orelse true; - const opts = b.addOptions(); - opts.addOption(bool, "option", test_opt); - - const exe = b.addExecutable(.{ - .name = "test", - .root_source_file = .{ .path = "test.zig" }, - .optimize = optimize, - }); - - var module = b.addModule("test_module", .{ - .source_file = .{ - .path = "test_module.zig", - }, - .dependencies = &.{}, - }); - module.addOptions("test_options", opts); - - exe.addModule("test_module", module); - - const run = b.addRunArtifact(exe); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(&run.step); - b.default_step = test_step; -} diff --git a/test/standalone/module_add_options/test.zig b/test/standalone/module_add_options/test.zig deleted file mode 100644 index 395d90619db1..000000000000 --- a/test/standalone/module_add_options/test.zig +++ /dev/null @@ -1,5 +0,0 @@ -const test_module = @import("test_module"); - -pub fn main() void { - test_module.hasOption(); -} diff --git a/test/standalone/module_add_options/test_module.zig b/test/standalone/module_add_options/test_module.zig deleted file mode 100644 index 6f15f16d30cd..000000000000 --- a/test/standalone/module_add_options/test_module.zig +++ /dev/null @@ -1,6 +0,0 @@ -const options = @import("test_options"); -const std = @import("std"); - -pub fn hasOption() void { - std.debug.assert(options.option); -} diff --git a/test/standalone/module_install_header/build.zig b/test/standalone/module_install_header/build.zig deleted file mode 100644 index 994cc707e4db..000000000000 --- a/test/standalone/module_install_header/build.zig +++ /dev/null @@ -1,24 +0,0 @@ -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const optimize = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ - .name = "test", - .root_source_file = .{ .path = "test.zig" }, - .optimize = optimize, - }); - var module = b.addModule("test_module", .{ - .source_file = .{ - .path = "test_module.zig", - }, - .dependencies = &.{}, - }); - module.installHeader("include/header.h", "header.h"); - - exe.addModule("test_module", module); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(b.getInstallStep()); - b.default_step = test_step; -} diff --git a/test/standalone/module_install_header/include/header.h b/test/standalone/module_install_header/include/header.h deleted file mode 100644 index 48b916111ff6..000000000000 --- a/test/standalone/module_install_header/include/header.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef HEADER_H -#define HEADER_H - -int add(int a, int b) { - return a + b; -} - -#endif \ No newline at end of file diff --git a/test/standalone/module_install_header/test.zig b/test/standalone/module_install_header/test.zig deleted file mode 100644 index 902b554db075..000000000000 --- a/test/standalone/module_install_header/test.zig +++ /dev/null @@ -1 +0,0 @@ -pub fn main() void {} diff --git a/test/standalone/module_install_header/test_module.zig b/test/standalone/module_install_header/test_module.zig deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/test/standalone/module_link_libc_cpp/build.zig b/test/standalone/module_link_libc_cpp/build.zig deleted file mode 100644 index 6f1da04e54e0..000000000000 --- a/test/standalone/module_link_libc_cpp/build.zig +++ /dev/null @@ -1,26 +0,0 @@ -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const optimize = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ - .name = "test", - .root_source_file = .{ .path = "test.zig" }, - .optimize = optimize, - }); - - var module = b.addModule("test_module", .{ - .source_file = .{ - .path = "test_module.zig", - }, - .dependencies = &.{}, - }); - module.linkLibC(); - module.linkLibCpp(); - - exe.addModule("test_module", module); - - const run = b.addRunArtifact(exe); - - b.default_step = &run.step; -} diff --git a/test/standalone/module_link_libc_cpp/test.zig b/test/standalone/module_link_libc_cpp/test.zig deleted file mode 100644 index 404f880bd1f6..000000000000 --- a/test/standalone/module_link_libc_cpp/test.zig +++ /dev/null @@ -1,9 +0,0 @@ -const test_module = @import("test_module"); -const assert = @import("std").debug.assert; - -extern fn fabs(num: f32) f32; - -pub fn main() void { - assert(test_module.c.abs(-1) == 1); - assert(fabs(fabs(-1.0) - 1.0) < 1e-7); -} diff --git a/test/standalone/module_link_libc_cpp/test_module.zig b/test/standalone/module_link_libc_cpp/test_module.zig deleted file mode 100644 index bb053b4bdfba..000000000000 --- a/test/standalone/module_link_libc_cpp/test_module.zig +++ /dev/null @@ -1,3 +0,0 @@ -pub const c = @cImport({ - @cInclude("stdlib.h"); -}); diff --git a/test/standalone/module_link_library/build.zig b/test/standalone/module_link_library/build.zig deleted file mode 100644 index 1e2cae8b67a2..000000000000 --- a/test/standalone/module_link_library/build.zig +++ /dev/null @@ -1,34 +0,0 @@ -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const optimize = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ - .name = "test", - .root_source_file = .{ .path = "test.zig" }, - .optimize = optimize, - }); - const lib = b.addStaticLibrary(.{ - .name = "lib", - .target = .{}, - .optimize = optimize, - }); - lib.addIncludePath("lib"); - lib.addCSourceFile("lib/lib.c", &.{""}); - - var module = b.addModule("test_module", .{ - .source_file = .{ - .path = "test_module.zig", - }, - .dependencies = &.{}, - }); - module.addIncludePath("lib"); - module.linkLibrary(lib); - - exe.addModule("test_module", module); - - const run = b.addRunArtifact(exe); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(&run.step); -} diff --git a/test/standalone/module_link_library/lib/lib.c b/test/standalone/module_link_library/lib/lib.c deleted file mode 100644 index 109cb04f3ff9..000000000000 --- a/test/standalone/module_link_library/lib/lib.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int add(int a, int b) { - return a + b; -} \ No newline at end of file diff --git a/test/standalone/module_link_library/lib/lib.h b/test/standalone/module_link_library/lib/lib.h deleted file mode 100644 index c3b18495cc6a..000000000000 --- a/test/standalone/module_link_library/lib/lib.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef LIB_H -#define LIB_H -int add(int a, int b); -#endif \ No newline at end of file diff --git a/test/standalone/module_link_library/test.zig b/test/standalone/module_link_library/test.zig deleted file mode 100644 index c0ae9efebe72..000000000000 --- a/test/standalone/module_link_library/test.zig +++ /dev/null @@ -1,6 +0,0 @@ -const test_module = @import("test_module"); -const assert = @import("std").debug.assert; - -pub fn main() void { - assert(test_module.header.add(1, 2) == 3); -} diff --git a/test/standalone/module_link_library/test_module.zig b/test/standalone/module_link_library/test_module.zig deleted file mode 100644 index 7b0bf6143121..000000000000 --- a/test/standalone/module_link_library/test_module.zig +++ /dev/null @@ -1,3 +0,0 @@ -pub const header = @cImport({ - @cInclude("lib.h"); -}); diff --git a/test/standalone/pie/build.zig b/test/standalone/pie/build.zig index 1efcd5c26e87..f624d18b46ae 100644 --- a/test/standalone/pie/build.zig +++ b/test/standalone/pie/build.zig @@ -11,7 +11,9 @@ pub fn build(b: *std.Build) void { }; const main = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, .target = target, }); diff --git a/test/standalone/pkg_import/build.zig b/test/standalone/pkg_import/build.zig index 30f4b2ede49b..548e6a8aacc7 100644 --- a/test/standalone/pkg_import/build.zig +++ b/test/standalone/pkg_import/build.zig @@ -6,12 +6,19 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; + const my_pkg = b.createModule(.{ + .source_file = .{ .path = "pkg.zig" }, + }); const exe = b.addExecutable(.{ .name = "test", - .root_source_file = .{ .path = "test.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "test.zig" }, + .dependencies = &.{ + .{ .name = "my_pkg", .module = my_pkg }, + }, + }), .optimize = optimize, }); - exe.addAnonymousModule("my_pkg", .{ .source_file = .{ .path = "pkg.zig" } }); const run = b.addRunArtifact(exe); test_step.dependOn(&run.step); diff --git a/test/standalone/shared_library/build.zig b/test/standalone/shared_library/build.zig index 377bf8186264..bd5aa501ee1d 100644 --- a/test/standalone/shared_library/build.zig +++ b/test/standalone/shared_library/build.zig @@ -6,9 +6,12 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; const target: std.zig.CrossTarget = .{}; + const lib_mod = b.createModule(.{ + .source_file = .{ .path = "mathtest.zig" }, + }); const lib = b.addSharedLibrary(.{ .name = "mathtest", - .root_source_file = .{ .path = "mathtest.zig" }, + .main_module = lib_mod, .version = .{ .major = 1, .minor = 0 }, .target = target, .optimize = optimize, @@ -16,6 +19,7 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "test", + .main_module = b.createModule(.{}), .target = target, .optimize = optimize, }); diff --git a/test/standalone/static_c_lib/build.zig b/test/standalone/static_c_lib/build.zig index 3feae705fcdb..f1ad7f30dcd4 100644 --- a/test/standalone/static_c_lib/build.zig +++ b/test/standalone/static_c_lib/build.zig @@ -8,6 +8,7 @@ pub fn build(b: *std.Build) void { const foo = b.addStaticLibrary(.{ .name = "foo", + .main_module = b.createModule(.{}), .optimize = optimize, .target = .{}, }); @@ -15,7 +16,9 @@ pub fn build(b: *std.Build) void { foo.addIncludePath("."); const test_exe = b.addTest(.{ - .root_source_file = .{ .path = "foo.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "foo.zig" }, + }), .optimize = optimize, }); test_exe.linkLibrary(foo); diff --git a/test/standalone/strip_empty_loop/build.zig b/test/standalone/strip_empty_loop/build.zig index 73c70d661627..52be6f6a5a51 100644 --- a/test/standalone/strip_empty_loop/build.zig +++ b/test/standalone/strip_empty_loop/build.zig @@ -9,7 +9,9 @@ pub fn build(b: *std.Build) void { const main = b.addExecutable(.{ .name = "main", - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, .target = target, }); diff --git a/test/standalone/test_runner_module_imports/build.zig b/test/standalone/test_runner_module_imports/build.zig index f27771889827..d3eaa90c16c9 100644 --- a/test/standalone/test_runner_module_imports/build.zig +++ b/test/standalone/test_runner_module_imports/build.zig @@ -1,18 +1,21 @@ const std = @import("std"); pub fn build(b: *std.Build) void { - const t = b.addTest(.{ - .root_source_file = .{ .path = "src/main.zig" }, - .test_runner = "test_runner/main.zig", - }); - const module1 = b.createModule(.{ .source_file = .{ .path = "module1/main.zig" } }); const module2 = b.createModule(.{ .source_file = .{ .path = "module2/main.zig" }, .dependencies = &.{.{ .name = "module1", .module = module1 }}, }); - t.addModule("module2", module2); + const t = b.addTest(.{ + .main_module = b.createModule(.{ + .source_file = .{ .path = "src/main.zig" }, + .dependencies = &.{ + .{ .name = "module2", .module = module2 }, + }, + }), + .test_runner = "test_runner/main.zig", + }); const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&b.addRunArtifact(t).step); diff --git a/test/standalone/test_runner_path/build.zig b/test/standalone/test_runner_path/build.zig index 352a18efe058..6a64f547beb3 100644 --- a/test/standalone/test_runner_path/build.zig +++ b/test/standalone/test_runner_path/build.zig @@ -6,8 +6,11 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test the program"); b.default_step = test_step; + const mod = b.createModule(.{ + .source_file = .{ .path = "test.zig" }, + }); const test_exe = b.addTest(.{ - .root_source_file = .{ .path = "test.zig" }, + .main_module = mod, }); test_exe.test_runner = "test_runner.zig"; diff --git a/test/standalone/use_alias/build.zig b/test/standalone/use_alias/build.zig index 6fa0a1f3a5b6..fbb0ceeb475f 100644 --- a/test/standalone/use_alias/build.zig +++ b/test/standalone/use_alias/build.zig @@ -7,7 +7,9 @@ pub fn build(b: *std.Build) void { const optimize: std.builtin.OptimizeMode = .Debug; const main = b.addTest(.{ - .root_source_file = .{ .path = "main.zig" }, + .main_module = b.createModule(.{ + .source_file = .{ .path = "main.zig" }, + }), .optimize = optimize, }); main.addIncludePath("."); diff --git a/test/tests.zig b/test/tests.zig index 4b16ac50c78b..ac6056769ecb 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -540,9 +540,12 @@ pub fn addStackTraceTests( test_filter: ?[]const u8, optimize_modes: []const OptimizeMode, ) *Step { + const check_mod = b.createModule(.{ + .source_file = .{ .path = "test/src/check-stack-trace.zig" }, + }); const check_exe = b.addExecutable(.{ .name = "check-stack-trace", - .root_source_file = .{ .path = "test/src/check-stack-trace.zig" }, + .main_module = check_mod, .target = .{}, .optimize = .Debug, }); @@ -579,10 +582,14 @@ pub fn addStandaloneTests( if (os_tag != builtin.os.tag) continue; } + const mod = b.createModule(.{ + .source_file = .{ .path = case.src_path }, + }); + if (case.is_exe) { const exe = b.addExecutable(.{ .name = std.fs.path.stem(case.src_path), - .root_source_file = .{ .path = case.src_path }, + .main_module = mod, .optimize = optimize, .target = case.target, }); @@ -594,7 +601,7 @@ pub fn addStandaloneTests( if (case.is_test) { const exe = b.addTest(.{ .name = std.fs.path.stem(case.src_path), - .root_source_file = .{ .path = case.src_path }, + .main_module = mod, .optimize = optimize, .target = case.target, }); @@ -983,8 +990,11 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { else options.max_rss; + const these_tests_mod = b.createModule(.{ + .source_file = .{ .path = options.root_src }, + }); const these_tests = b.addTest(.{ - .root_source_file = .{ .path = options.root_src }, + .main_module = these_tests_mod, .optimize = test_target.optimize_mode, .target = test_target.target, .max_rss = max_rss, @@ -1020,13 +1030,8 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { var altered_target = test_target.target; altered_target.ofmt = null; - const compile_c = b.addExecutable(.{ - .name = qualified_name, - .link_libc = test_target.link_libc, - .target = altered_target, - }); - compile_c.overrideZigLibDir("lib"); - compile_c.addCSourceFileSource(.{ + const compile_c_mod = b.createModule(.{}); + compile_c_mod.addCSourceFileSource(.{ .source = these_tests.getOutputSource(), .args = &.{ // TODO output -std=c89 compatible C code @@ -1043,6 +1048,14 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step { "-Wno-absolute-value", }, }); + + const compile_c = b.addExecutable(.{ + .name = qualified_name, + .main_module = compile_c_mod, + .link_libc = test_target.link_libc, + .target = altered_target, + }); + compile_c.overrideZigLibDir("lib"); compile_c.addIncludePath("lib"); // for zig.h if (test_target.link_libc == false and test_target.target.getOsTag() == .windows) { compile_c.subsystem = .Console; @@ -1085,8 +1098,11 @@ pub fn addCAbiTests(b: *std.Build, skip_non_native: bool, skip_release: bool) *S const triple_prefix = c_abi_target.zigTriple(b.allocator) catch @panic("OOM"); + const test_mod = b.createModule(.{ + .source_file = .{ .path = "test/c_abi/main.zig" }, + }); const test_step = b.addTest(.{ - .root_source_file = .{ .path = "test/c_abi/main.zig" }, + .main_module = test_mod, .optimize = optimize_mode, .target = c_abi_target, .name = b.fmt("test-c-abi-{s}-{s}", .{ From e0a5f7aaf900b8c7ab00accc2c17fa49ff720e25 Mon Sep 17 00:00:00 2001 From: Adam Goertz Date: Sun, 30 Apr 2023 01:18:50 +0000 Subject: [PATCH 29/29] Parse module-specific args on CLI Bootstrap now builds, but stage3 fails because we're not yet passing the main_module correctly, so the compilation does not have a root source file yet. --- src/main.zig | 322 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 188 insertions(+), 134 deletions(-) diff --git a/src/main.zig b/src/main.zig index 6a83791ca38f..80558f0541d3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -701,6 +701,186 @@ const ArgsIterator = struct { return it.args[it.i]; } }; +const ModuleArgs = struct { + mod: *Package, + deps_str: []const u8, // still in CLI arg format +}; + +fn parseModuleArgs( + gpa: Allocator, + arena: Allocator, + args_iter: *ArgsIterator, + modules: *std.StringArrayHashMap(ModuleArgs), +) !void { + var root_src_orig: ?[]const u8 = null; + var deps_str: []const u8 = ""; + + var clang_argv = std.ArrayList([]const u8).init(gpa); + defer clang_argv.deinit(); + + var cssan = ClangSearchSanitizer.init(gpa, &clang_argv); + defer cssan.map.deinit(); + + var system_libs = std.StringArrayHashMap(Compilation.SystemLib).init(gpa); + defer system_libs.deinit(); + + var extra_cflags = std.ArrayList([]const u8).init(gpa); + defer extra_cflags.deinit(); + + var lib_dirs = std.ArrayList([]const u8).init(gpa); + defer lib_dirs.deinit(); + + var rpath_list = std.ArrayList([]const u8).init(gpa); + defer rpath_list.deinit(); + + var c_source_files = std.ArrayList(Compilation.CSourceFile).init(gpa); + defer c_source_files.deinit(); + + var link_objects = std.ArrayList(Compilation.LinkObject).init(gpa); + defer link_objects.deinit(); + + var framework_dirs = std.ArrayList([]const u8).init(gpa); + defer framework_dirs.deinit(); + + var frameworks: std.StringArrayHashMapUnmanaged(Compilation.SystemLib) = .{}; + + const mod_name = args_iter.nextOrFatal(); + + var file_ext: ?Compilation.FileExt = null; + while (args_iter.next()) |mod_arg| { + if (std.mem.eql(u8, mod_arg, "--root-source")) { + root_src_orig = args_iter.nextOrFatal(); + } else if (std.mem.eql(u8, mod_arg, "--args")) { + while (args_iter.next()) |arg| { + if (mem.startsWith(u8, arg, "-")) { + if (mem.eql(u8, arg, "-cflags")) { + extra_cflags.shrinkRetainingCapacity(0); + while (true) { + const next_arg = args_iter.next() orelse { + fatal("expected -- after -cflags", .{}); + }; + if (mem.eql(u8, next_arg, "--")) break; + try extra_cflags.append(next_arg); + } + } else if (mem.eql(u8, arg, "-rpath")) { + try rpath_list.append(args_iter.nextOrFatal()); + } else if (mem.eql(u8, arg, "--library-directory") or mem.eql(u8, arg, "-L")) { + try lib_dirs.append(args_iter.nextOrFatal()); + } else if (mem.eql(u8, arg, "-F")) { + try framework_dirs.append(args_iter.nextOrFatal()); + } else if (mem.eql(u8, arg, "-framework")) { + try frameworks.put(gpa, args_iter.nextOrFatal(), .{}); + } else if (mem.eql(u8, arg, "-weak_framework")) { + try frameworks.put(gpa, args_iter.nextOrFatal(), .{ .weak = true }); + } else if (mem.eql(u8, arg, "-needed_framework")) { + try frameworks.put(gpa, args_iter.nextOrFatal(), .{ .needed = true }); + } else if (mem.eql(u8, arg, "--library") or mem.eql(u8, arg, "-l")) { + // We don't know whether this library is part of libc or libc++ until + // we resolve the target, so we simply append to the list for now. + try system_libs.put(args_iter.nextOrFatal(), .{}); + } else if (mem.eql(u8, arg, "--needed-library") or + mem.eql(u8, arg, "-needed-l") or + mem.eql(u8, arg, "-needed_library")) + { + const next_arg = args_iter.nextOrFatal(); + try system_libs.put(next_arg, .{ .needed = true }); + } else if (mem.eql(u8, arg, "-weak_library") or mem.eql(u8, arg, "-weak-l")) { + try system_libs.put(args_iter.nextOrFatal(), .{ .weak = true }); + } else if (mem.eql(u8, arg, "-D")) { + try clang_argv.append(arg); + try clang_argv.append(args_iter.nextOrFatal()); + } else if (mem.eql(u8, arg, "-I")) { + try cssan.addIncludePath(.I, arg, args_iter.nextOrFatal(), false); + } else if (mem.eql(u8, arg, "-isystem") or mem.eql(u8, arg, "-iwithsysroot")) { + try cssan.addIncludePath(.isystem, arg, args_iter.nextOrFatal(), false); + } else if (mem.eql(u8, arg, "-idirafter")) { + try cssan.addIncludePath(.idirafter, arg, args_iter.nextOrFatal(), false); + } else if (mem.eql(u8, arg, "-iframework") or mem.eql(u8, arg, "-iframeworkwithsysroot")) { + try cssan.addIncludePath(.iframework, arg, args_iter.nextOrFatal(), false); + } else if (mem.startsWith(u8, arg, "-L")) { + try lib_dirs.append(arg[2..]); + } else if (mem.startsWith(u8, arg, "-F")) { + try framework_dirs.append(arg[2..]); + } else if (mem.startsWith(u8, arg, "-l")) { + // We don't know whether this library is part of libc or libc++ until + // we resolve the target, so we simply append to the list for now. + try system_libs.put(arg["-l".len..], .{}); + } else if (mem.startsWith(u8, arg, "-needed-l")) { + try system_libs.put(arg["-needed-l".len..], .{ .needed = true }); + } else if (mem.startsWith(u8, arg, "-weak-l")) { + try system_libs.put(arg["-weak-l".len..], .{ .weak = true }); + } else if (mem.startsWith(u8, arg, "-D")) { + try clang_argv.append(arg); + } else if (mem.startsWith(u8, arg, "-I")) { + try cssan.addIncludePath(.I, arg, arg[2..], true); + } else if (mem.eql(u8, arg, "-x")) { + const lang = args_iter.nextOrFatal(); + if (mem.eql(u8, lang, "none")) { + file_ext = null; + } else if (Compilation.LangToExt.get(lang)) |got_ext| { + file_ext = got_ext; + } else { + fatal("language not recognized: '{s}'", .{lang}); + } + } else if (mem.eql(u8, arg, "--args-end")) { + break; + } else { + fatal("unrecognized parameter: '{s}'", .{arg}); + } + } else switch (file_ext orelse + Compilation.classifyFileExt(arg)) { + .object, .static_library, .shared_library => try link_objects.append(.{ .path = arg }), + .assembly, .assembly_with_cpp, .c, .cpp, .h, .ll, .bc, .m, .mm, .cu => { + try c_source_files.append(.{ + .src_path = arg, + .extra_flags = try arena.dupe([]const u8, extra_cflags.items), + // duped when parsing the args. + .ext = file_ext, + }); + }, + .zig => { + fatal("zig root source file should be specified as --root-source {s}", .{arg}); + }, + .def, .unknown => { + fatal("unrecognized file extension of parameter '{s}'", .{arg}); + }, + } + } + } else if (std.mem.eql(u8, mod_arg, "--deps")) { + deps_str = args_iter.nextOrFatal(); + } + } + + const root_src = if (root_src_orig) |rs| + try introspect.resolvePath(arena, rs) + else + // TODO: Is this the right path to choose here? + try introspect.resolvePath(arena, "."); + + for ([_][]const u8{ "std", "root", "builtin" }) |name| { + if (mem.eql(u8, mod_name, name)) { + fatal("unable to add module '{s}' -> '{s}': conflicts with builtin module", .{ mod_name, root_src }); + } + } + + var mod_it = modules.iterator(); + while (mod_it.next()) |kv| { + if (std.mem.eql(u8, mod_name, kv.key_ptr.*)) { + fatal("unable to add module '{s}' -> '{s}': already exists as '{s}'", .{ mod_name, root_src, kv.value_ptr.mod.root_src_path }); + } + } + + try modules.ensureUnusedCapacity(1); + modules.putAssumeCapacity(mod_name, .{ + .mod = try Package.create( + gpa, + fs.path.dirname(root_src), + fs.path.basename(root_src), + // TODO: Add parsed module arguments + ), + .deps_str = deps_str, + }); +} fn buildOutputType( gpa: Allocator, @@ -710,6 +890,7 @@ fn buildOutputType( ) !void { var color: Color = .auto; var optimize_mode: std.builtin.Mode = .Debug; + var main_module: ?[]const u8 = null; var provided_name: ?[]const u8 = null; var link_mode: ?std.builtin.LinkMode = null; var dll_export_fns: ?bool = null; @@ -904,10 +1085,7 @@ fn buildOutputType( // Contains every module specified via --mod. The dependencies are added // after argument parsing is completed. We use a StringArrayHashMap to make // error output consistent. - var modules = std.StringArrayHashMap(struct { - mod: *Package, - deps_str: []const u8, // still in CLI arg format - }).init(gpa); + var modules = std.StringArrayHashMap(ModuleArgs).init(gpa); defer { var it = modules.iterator(); while (it.next()) |kv| kv.value_ptr.mod.destroy(gpa); @@ -949,7 +1127,6 @@ fn buildOutputType( var cssan = ClangSearchSanitizer.init(gpa, &clang_argv); defer cssan.map.deinit(); - var file_ext: ?Compilation.FileExt = null; args_loop: while (args_iter.next()) |arg| { if (mem.startsWith(u8, arg, "@")) { // This is a "compiler response file". We must parse the file and treat its @@ -972,54 +1149,9 @@ fn buildOutputType( fatal("unexpected end-of-parameter mark: --", .{}); } } else if (mem.eql(u8, arg, "--mod")) { - const info = args_iter.nextOrFatal(); - var info_it = mem.split(u8, info, ":"); - const mod_name = info_it.next() orelse fatal("expected non-empty argument after {s}", .{arg}); - const deps_str = info_it.next() orelse fatal("expected 'name:deps:path' after {s}", .{arg}); - const root_src_orig = info_it.rest(); - if (root_src_orig.len == 0) fatal("expected 'name:deps:path' after {s}", .{arg}); - if (mod_name.len == 0) fatal("empty name for module at '{s}'", .{root_src_orig}); - - const root_src = try introspect.resolvePath(arena, root_src_orig); - - for ([_][]const u8{ "std", "root", "builtin" }) |name| { - if (mem.eql(u8, mod_name, name)) { - fatal("unable to add module '{s}' -> '{s}': conflicts with builtin module", .{ mod_name, root_src }); - } - } - - var mod_it = modules.iterator(); - while (mod_it.next()) |kv| { - if (std.mem.eql(u8, mod_name, kv.key_ptr.*)) { - fatal("unable to add module '{s}' -> '{s}': already exists as '{s}'", .{ mod_name, root_src, kv.value_ptr.mod.root_src_path }); - } - } - - try modules.ensureUnusedCapacity(1); - modules.put(mod_name, .{ - .mod = try Package.create( - gpa, - fs.path.dirname(root_src), - fs.path.basename(root_src), - ), - .deps_str = deps_str, - }) catch unreachable; - } else if (mem.eql(u8, arg, "--deps")) { - if (root_deps_str != null) { - fatal("only one --deps argument is allowed", .{}); - } - root_deps_str = args_iter.nextOrFatal(); + try parseModuleArgs(gpa, arena, &args_iter, &modules); } else if (mem.eql(u8, arg, "--main-pkg-path")) { main_pkg_path = args_iter.nextOrFatal(); - } else if (mem.eql(u8, arg, "-cflags")) { - extra_cflags.shrinkRetainingCapacity(0); - while (true) { - const next_arg = args_iter.next() orelse { - fatal("expected -- after -cflags", .{}); - }; - if (mem.eql(u8, next_arg, "--")) break; - try extra_cflags.append(next_arg); - } } else if (mem.eql(u8, arg, "--color")) { const next_arg = args_iter.next() orelse { fatal("expected [auto|on|off] after --color", .{}); @@ -1045,22 +1177,11 @@ fn buildOutputType( image_base_override = std.fmt.parseUnsigned(u64, next_arg, 0) catch |err| { fatal("unable to parse image base override '{s}': {s}", .{ next_arg, @errorName(err) }); }; - } else if (mem.eql(u8, arg, "--name")) { - provided_name = args_iter.nextOrFatal(); - if (!mem.eql(u8, provided_name.?, fs.path.basename(provided_name.?))) - fatal("invalid package name '{s}': cannot contain folder separators", .{provided_name.?}); - } else if (mem.eql(u8, arg, "-rpath")) { - try rpath_list.append(args_iter.nextOrFatal()); - } else if (mem.eql(u8, arg, "--library-directory") or mem.eql(u8, arg, "-L")) { - try lib_dirs.append(args_iter.nextOrFatal()); - } else if (mem.eql(u8, arg, "-F")) { - try framework_dirs.append(args_iter.nextOrFatal()); - } else if (mem.eql(u8, arg, "-framework")) { - try frameworks.put(gpa, args_iter.nextOrFatal(), .{}); - } else if (mem.eql(u8, arg, "-weak_framework")) { - try frameworks.put(gpa, args_iter.nextOrFatal(), .{ .weak = true }); - } else if (mem.eql(u8, arg, "-needed_framework")) { - try frameworks.put(gpa, args_iter.nextOrFatal(), .{ .needed = true }); + } else if (mem.eql(u8, arg, "--main-mod")) { + main_module = args_iter.nextOrFatal(); + provided_name = main_module; + if (!mem.eql(u8, main_module.?, fs.path.basename(main_module.?))) + fatal("invalid module name '{s}': cannot contain folder separators", .{main_module.?}); } else if (mem.eql(u8, arg, "-install_name")) { install_name = args_iter.nextOrFatal(); } else if (mem.startsWith(u8, arg, "--compress-debug-sections=")) { @@ -1094,29 +1215,6 @@ fn buildOutputType( linker_script = args_iter.nextOrFatal(); } else if (mem.eql(u8, arg, "--version-script")) { version_script = args_iter.nextOrFatal(); - } else if (mem.eql(u8, arg, "--library") or mem.eql(u8, arg, "-l")) { - // We don't know whether this library is part of libc or libc++ until - // we resolve the target, so we simply append to the list for now. - try system_libs.put(args_iter.nextOrFatal(), .{}); - } else if (mem.eql(u8, arg, "--needed-library") or - mem.eql(u8, arg, "-needed-l") or - mem.eql(u8, arg, "-needed_library")) - { - const next_arg = args_iter.nextOrFatal(); - try system_libs.put(next_arg, .{ .needed = true }); - } else if (mem.eql(u8, arg, "-weak_library") or mem.eql(u8, arg, "-weak-l")) { - try system_libs.put(args_iter.nextOrFatal(), .{ .weak = true }); - } else if (mem.eql(u8, arg, "-D")) { - try clang_argv.append(arg); - try clang_argv.append(args_iter.nextOrFatal()); - } else if (mem.eql(u8, arg, "-I")) { - try cssan.addIncludePath(.I, arg, args_iter.nextOrFatal(), false); - } else if (mem.eql(u8, arg, "-isystem") or mem.eql(u8, arg, "-iwithsysroot")) { - try cssan.addIncludePath(.isystem, arg, args_iter.nextOrFatal(), false); - } else if (mem.eql(u8, arg, "-idirafter")) { - try cssan.addIncludePath(.idirafter, arg, args_iter.nextOrFatal(), false); - } else if (mem.eql(u8, arg, "-iframework") or mem.eql(u8, arg, "-iframeworkwithsysroot")) { - try cssan.addIncludePath(.iframework, arg, args_iter.nextOrFatal(), false); } else if (mem.eql(u8, arg, "--version")) { const next_arg = args_iter.nextOrFatal(); version = std.builtin.Version.parse(next_arg) catch |err| { @@ -1467,31 +1565,6 @@ fn buildOutputType( verbose_llvm_cpu_features = true; } else if (mem.startsWith(u8, arg, "-T")) { linker_script = arg[2..]; - } else if (mem.startsWith(u8, arg, "-L")) { - try lib_dirs.append(arg[2..]); - } else if (mem.startsWith(u8, arg, "-F")) { - try framework_dirs.append(arg[2..]); - } else if (mem.startsWith(u8, arg, "-l")) { - // We don't know whether this library is part of libc or libc++ until - // we resolve the target, so we simply append to the list for now. - try system_libs.put(arg["-l".len..], .{}); - } else if (mem.startsWith(u8, arg, "-needed-l")) { - try system_libs.put(arg["-needed-l".len..], .{ .needed = true }); - } else if (mem.startsWith(u8, arg, "-weak-l")) { - try system_libs.put(arg["-weak-l".len..], .{ .weak = true }); - } else if (mem.startsWith(u8, arg, "-D")) { - try clang_argv.append(arg); - } else if (mem.startsWith(u8, arg, "-I")) { - try cssan.addIncludePath(.I, arg, arg[2..], true); - } else if (mem.eql(u8, arg, "-x")) { - const lang = args_iter.nextOrFatal(); - if (mem.eql(u8, lang, "none")) { - file_ext = null; - } else if (Compilation.LangToExt.get(lang)) |got_ext| { - file_ext = got_ext; - } else { - fatal("language not recognized: '{s}'", .{lang}); - } } else if (mem.startsWith(u8, arg, "-mexec-model=")) { wasi_exec_model = std.meta.stringToEnum(std.builtin.WasiExecModel, arg["-mexec-model=".len..]) orelse { fatal("expected [command|reactor] for -mexec-mode=[value], found '{s}'", .{arg["-mexec-model=".len..]}); @@ -1499,25 +1572,6 @@ fn buildOutputType( } else { fatal("unrecognized parameter: '{s}'", .{arg}); } - } else switch (file_ext orelse - Compilation.classifyFileExt(arg)) { - .object, .static_library, .shared_library => try link_objects.append(.{ .path = arg }), - .assembly, .assembly_with_cpp, .c, .cpp, .h, .ll, .bc, .m, .mm, .cu => { - try c_source_files.append(.{ - .src_path = arg, - .extra_flags = try arena.dupe([]const u8, extra_cflags.items), - // duped when parsing the args. - .ext = file_ext, - }); - }, - .zig => { - if (root_src_file) |other| { - fatal("found another zig file '{s}' after root source file '{s}'", .{ arg, other }); - } else root_src_file = arg; - }, - .def, .unknown => { - fatal("unrecognized file extension of parameter '{s}'", .{arg}); - }, } } if (optimize_mode_string) |s| {