Skip to content
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

Add CompileStep APIs to Build.Module. #14731

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) = .{},
AdamGoertz marked this conversation as resolved.
Show resolved Hide resolved
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");
AdamGoertz marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn addLibraryPath(m: *Module, library_path: []const u8) void {
m.lib_paths.append(m.builder.dupe(library_path)) catch @panic("OOM");
AdamGoertz marked this conversation as resolved.
Show resolved Hide resolved
}

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");
AdamGoertz marked this conversation as resolved.
Show resolved Hide resolved
}
};

/// A file that is generated by a build step.
Expand Down
29 changes: 29 additions & 0 deletions lib/std/Build/CompileStep.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
AdamGoertz marked this conversation as resolved.
Show resolved Hide resolved

for (module.dependencies.values()) |dep| {
try cs.addRecursiveBuildDeps(dep, done);
}
Expand Down