generate_build_zig
Generates a modern build.zig file with best practices for executable or library projects, supporting custom Zig version and dependencies.
Instructions
Generate a modern build.zig file with best practices
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectName | No | Name of the project | my-project |
| projectType | No | Type of project to generate | executable |
| zigVersion | No | Target Zig version | 0.15.2 |
| dependencies | No | List of dependencies to include |
Implementation Reference
- src/index.ts:1103-1162 (handler)Handler method `generateBuildZig` on the ZigServer class that processes tool arguments, builds a ZigBuildConfig, calls ZigBuildSystemHelper.generateBuildZig(), and returns a markdown-formatted response with usage instructions.
private async generateBuildZig(args: Record<string, any>): Promise<string> { Logger.debug('Generating build.zig file'); const config: Partial<ZigBuildConfig> = { zigVersion: args.zigVersion || '0.15.2', buildMode: args.optimizationLevel || 'ReleaseSafe', dependencies: {}, buildSteps: [], }; // Add dependencies if provided if (Array.isArray(args.dependencies)) { for (const dep of args.dependencies) { config.dependencies![dep] = `dependency("${dep}")`; } } const buildZigContent = ZigBuildSystemHelper.generateBuildZig(config); return ` # Generated build.zig \`\`\`zig ${buildZigContent} \`\`\` ## Usage Instructions: 1. **Build the project:** \`\`\`bash zig build \`\`\` 2. **Run the application:** \`\`\`bash zig build run \`\`\` 3. **Run tests:** \`\`\`bash zig build test \`\`\` 4. **Build for different targets:** \`\`\`bash zig build -Dtarget=x86_64-windows-gnu zig build -Dtarget=aarch64-linux-gnu \`\`\` 5. **Different optimization modes:** \`\`\`bash zig build -Doptimize=Debug zig build -Doptimize=ReleaseFast \`\`\` ## Next Steps: - Customize the build script for your specific needs - Add additional build steps or dependencies as required - Consider using build.zig.zon for dependency management `.trim(); - src/zig-build.ts:22-101 (helper)Static helper method `ZigBuildSystemHelper.generateBuildZig()` that generates the actual build.zig file content with modern Zig 0.15.2+ patterns including executable creation, dependency management, run/test/docs steps.
static generateBuildZig(config: Partial<ZigBuildConfig>): string { const { zigVersion = '0.15.2', buildMode: _buildMode = 'ReleaseSafe', targetTriple: _targetTriple, dependencies = {}, buildSteps: _buildSteps = [], } = config; return `//! Build script for Zig project //! Zig version: ${zigVersion} or later //! Modern build system with enhanced module support const std = @import("std"); pub fn build(b: *std.Build) void { // Standard target options allow the person running \`zig build\` to choose // what target to build for. Here we do not override the defaults, which // means any target is allowed, and the default is native. const target = b.standardTargetOptions(.{}); // Standard optimization options allow the person running \`zig build\` to select // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const optimize = b.standardOptimizeOption(.{}); // Create the main executable const exe = b.addExecutable(.{ .name = "main", .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); // Add dependencies using modern module system ${Object.entries(dependencies) .map( ([name, _path]) => ` const ${name}_dep = b.dependency("${name}", .{ .target = target, .optimize = optimize, }); exe.root_module.addImport("${name}", ${name}_dep.module("${name}"));` ) .join('\n')} // Install the executable b.installArtifact(exe); // Create a run step const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); // Forward arguments to the run step if (b.args) |args| { run_cmd.addArgs(args); } const run_step = b.step("run", "Run the application"); run_step.dependOn(&run_cmd.step); // Create test step const unit_tests = b.addTest(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); const run_unit_tests = b.addRunArtifact(unit_tests); const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_unit_tests.step); // Add documentation generation step const docs_step = b.step("docs", "Generate documentation"); const docs_install = b.addInstallDirectory(.{ .source_dir = exe.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); docs_step.dependOn(&docs_install.step); }`; - src/index.ts:258-285 (schema)Input schema definition for the generate_build_zig tool with properties: projectName, projectType, zigVersion, dependencies.
inputSchema: { type: 'object', properties: { projectName: { type: 'string', description: 'Name of the project', default: 'my-project', }, projectType: { type: 'string', enum: ['executable', 'library', 'both'], description: 'Type of project to generate', default: 'executable', }, zigVersion: { type: 'string', description: 'Target Zig version', default: '0.15.2', }, dependencies: { type: 'array', items: { type: 'string' }, description: 'List of dependencies to include', default: [], }, }, required: [], }, - src/index.ts:255-256 (registration)Tool registration within the ListToolsRequestSchema handler – defines name 'generate_build_zig' and its description.
{ name: 'generate_build_zig', - src/index.ts:385-393 (registration)Tool dispatch within the CallToolRequestSchema switch-case – routes 'generate_build_zig' to await this.generateBuildZig(args).
case 'generate_build_zig': return { content: [ { type: 'text', text: await this.generateBuildZig(args || {}), }, ], };