Skip to content

Add CompileStep APIs to Build.Module. #14731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a6ec559
Add some CompileStep APIs to Build.Module.
AdamGoertz Feb 26, 2023
44ffc09
Add more CompileStep APIs.
AdamGoertz Feb 27, 2023
32c5e6c
Add some tests for module API.
AdamGoertz Feb 27, 2023
2a9f18a
Merge branch 'master' into add-module-apis
AdamGoertz Feb 27, 2023
71f91cc
Add linkFramework
AdamGoertz Feb 27, 2023
5422b25
Add more CompileStep APIs
AdamGoertz Feb 27, 2023
95cdee3
Merge branch 'master' into add-module-apis
AdamGoertz Mar 5, 2023
8b21303
Merge branch 'master' into add-module-apis
AdamGoertz Mar 5, 2023
caa56f8
Merge remote-tracking branch 'upstream/master' into add-module-apis
AdamGoertz Mar 5, 2023
50879a4
Merge branch 'add-module-apis' of github.com:AdamGoertz/zig into add-…
AdamGoertz Mar 5, 2023
a668957
Fix linkLibC & linkLibCpp
AdamGoertz Mar 15, 2023
321685c
Merge branch 'master' into add-module-apis
AdamGoertz Mar 15, 2023
5cdf5a6
Add test for Module.linkLibC, Module.linkLibCpp
AdamGoertz Mar 15, 2023
c00e0a9
Merge branch 'master' into add-module-apis
AdamGoertz Mar 15, 2023
c736475
Merge branch 'master' into add-module-apis
AdamGoertz Mar 24, 2023
c4883be
Merge branch 'add-module-apis' of github.com:AdamGoertz/zig into add-…
AdamGoertz Mar 24, 2023
8b4053d
Fix formatting
AdamGoertz Mar 24, 2023
2f61e41
Ensure lib is installed before building exe
AdamGoertz Mar 25, 2023
3a34eb6
Make relative paths relative to module build root
AdamGoertz Mar 25, 2023
5ffcba2
#14979 Add addOptions to Module
AdamGoertz Apr 1, 2023
390113c
Merge remote-tracking branch 'upstream/master' into add-module-apis
AdamGoertz Apr 22, 2023
b6e9d85
Fix simple requested changes
AdamGoertz Apr 22, 2023
e0f80a9
Remove unnecessary usages of pathFromRoot
AdamGoertz Apr 22, 2023
3383c6f
Move Module to Build/Module.zig
AdamGoertz Apr 22, 2023
d6e471a
WIP: Begin moving CompileStep APIs to Module
AdamGoertz Apr 22, 2023
4021865
wip: temp fixes to compile errors in CompileStep and RunStep
AdamGoertz Apr 23, 2023
b68313c
Make Module a Step.
AdamGoertz Apr 23, 2023
bcc551f
Make CompileStep properly depend on main_module step
AdamGoertz Apr 24, 2023
bd81db4
Add Module.appendArgs
AdamGoertz Apr 24, 2023
66cc806
Move transitive dep traversal to Module.zig
AdamGoertz Apr 25, 2023
383885a
Add recursive module include dirs
AdamGoertz Apr 25, 2023
a5151d4
Move forceUndefinedSymbol back to CompileStep
AdamGoertz Apr 26, 2023
f4177f4
Add framework_dirs, frameworks, c_macros, and rpaths
AdamGoertz Apr 26, 2023
125b6bf
Add doc comment
AdamGoertz Apr 26, 2023
119bf1c
Move linker_script and c_std back to CompileStep
AdamGoertz Apr 26, 2023
b6bc9c8
Add --root-source and --args-end flags to make CLI parsing easier.
AdamGoertz Apr 29, 2023
771f415
Make addExecutable, add*Library take existing Modules
AdamGoertz Apr 29, 2023
c8d48c9
Update tests for new build API
AdamGoertz Apr 30, 2023
e0a5f7a
Parse module-specific args on CLI
AdamGoertz Apr 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 13 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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 = ".";
Expand Down Expand Up @@ -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,
});
Expand Down
68 changes: 32 additions & 36 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -448,7 +449,7 @@ pub fn addOptions(self: *Build) *OptionsStep {

pub const ExecutableOptions = struct {
name: []const u8,
root_source_file: ?FileSource = null,
main_module: *Module,
version: ?std.builtin.Version = null,
target: CrossTarget = .{},
optimize: std.builtin.Mode = .Debug,
Expand All @@ -463,7 +464,7 @@ pub const ExecutableOptions = struct {
pub fn addExecutable(b: *Build, options: ExecutableOptions) *CompileStep {
return CompileStep.create(b, .{
.name = options.name,
.root_source_file = options.root_source_file,
.main_module = options.main_module,
.version = options.version,
.target = options.target,
.optimize = options.optimize,
Expand All @@ -479,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,
Expand All @@ -492,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,
Expand All @@ -506,7 +507,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *CompileStep {

pub const SharedLibraryOptions = struct {
name: []const u8,
root_source_file: ?FileSource = null,
main_module: *Module,
version: ?std.builtin.Version = null,
target: CrossTarget,
optimize: std.builtin.Mode,
Expand All @@ -520,7 +521,7 @@ pub const SharedLibraryOptions = struct {
pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *CompileStep {
return CompileStep.create(b, .{
.name = options.name,
.root_source_file = options.root_source_file,
.main_module = options.main_module,
.kind = .lib,
.linkage = .dynamic,
.version = options.version,
Expand All @@ -536,7 +537,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *CompileStep {

pub const StaticLibraryOptions = struct {
name: []const u8,
root_source_file: ?FileSource = null,
main_module: *Module,
target: CrossTarget,
optimize: std.builtin.Mode,
version: ?std.builtin.Version = null,
Expand All @@ -550,7 +551,7 @@ pub const StaticLibraryOptions = struct {
pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *CompileStep {
return CompileStep.create(b, .{
.name = options.name,
.root_source_file = options.root_source_file,
.main_module = options.main_module,
.kind = .lib,
.linkage = .static,
.version = options.version,
Expand All @@ -566,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,
Expand All @@ -583,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,
Expand Down Expand Up @@ -621,40 +622,44 @@ 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;
}

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 = &.{},
};

/// This function creates a private module, to be used by the current package,
/// 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;
}

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;
return Module.create(b, null, options);
}

/// Initializes a RunStep with argv, which must at least have the path to the
Expand Down Expand Up @@ -1548,15 +1553,6 @@ pub fn runBuild(b: *Build, build_zig: anytype) anyerror!void {
}
}

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),
};

/// 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 {
Expand Down
Loading