Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions examples/2024-01-15-zig-build-explained/all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
echo "Run Entry..."
DIR="$(dirname "$(realpath "$0")")"
for ex in `ls -d */`;do
echo "Run ${ex}..."
cd ${DIR}/${ex}
if [ -f part.sh ]; then
./part.sh
fi
done
27 changes: 27 additions & 0 deletions examples/2024-01-15-zig-build-explained/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "test",
.root_source_file = .{ .path = "main.zig" },
.target = target,
.optimize = optimize,
});
// b.installArtifact(exe);
const cmd = b.addSystemCommand(&.{
"./all.sh",
// "-outfile=lines.c",
// "lines.l",
});

exe.step.dependOn(&cmd.step);
b.installArtifact(exe);
// const run_cmd = b.addRunArtifact(exe);
// run_cmd.step.dependOn(b.getInstallStep());
// if (b.args) |args| {
// run_cmd.addArgs(args);
// }
// const run_step = b.step("run", "Run the app");
// run_step.dependOn(&run_cmd.step);
}
5 changes: 5 additions & 0 deletions examples/2024-01-15-zig-build-explained/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("{s}\n", .{""});
}
28 changes: 28 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.1/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "test",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const unit_tests = b.addTest(.{
.root_source_file = .{ .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);
}
10 changes: 10 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.1/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
5 changes: 5 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.2/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const named_step = b.step("step-name", "This is what is shown in help");
_ = named_step;
}
14 changes: 14 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.3/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "test",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

const compile_step = b.step("compile", "Compiles src/main.zig");
compile_step.dependOn(&exe.step);
}
10 changes: 10 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.3/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
10 changes: 10 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.4/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const exe = b.addExecutable(.{
.name = "fresh",
.root_source_file = .{ .path = "src/main.zig" },
.optimize = .ReleaseSafe,
});
const compile_step = b.step("compile", "Compiles src/main.zig");
compile_step.dependOn(&exe.step);
}
10 changes: 10 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.4/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
13 changes: 13 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.6/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "fresh",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
}
10 changes: 10 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.6/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
15 changes: 15 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.7/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "fresh",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
10 changes: 10 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.7/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
18 changes: 18 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.8/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "fresh",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
15 changes: 15 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/1.8/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const std = @import("std");

pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
std.debug.print("args from builds:", .{});
for (std.os.argv) |arg| {
std.debug.print("{s},", .{arg});
}
std.debug.print("\n", .{});
try bw.flush(); // don't forget to flush!
}
8 changes: 8 additions & 0 deletions examples/2024-01-15-zig-build-explained/part1/part.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
echo "Run Part..."
DIR="$(dirname "$(realpath "$0")")"
for ex in `ls -d */`;do
echo "Run example ${ex}..."
cd ${DIR}/${ex}
zig build --summary none
done
28 changes: 28 additions & 0 deletions examples/2024-01-15-zig-build-explained/part2/2.1/buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdlib.h>
#include <string.h>

void * buffer_create(size_t len)
{
return malloc(len);
}

void buffer_destroy(void * ptr)
{
free(ptr);
}

void buffer_write(void * buf, size_t offset, void * data, size_t len)
{
memcpy(
(char*)buf + offset,
data,
len);
}

void buffer_read(void * buf, size_t offset, void * data, size_t len)
{
memcpy(
data,
(char*)buf + offset,
len);
}
24 changes: 24 additions & 0 deletions examples/2024-01-15-zig-build-explained/part2/2.1/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "example",
// 这块调试了很久。最后的结论是根本不要写
// .root_source_file = .{ .path = undefined },
.target = target,
.optimize = optimize,
});
// 这块调试了很久。API变了不会写,着了很久的文档和看了很久的代码
exe.addCSourceFile(.{ .file = std.build.LazyPath.relative("main.c"), .flags = &.{} });
exe.addCSourceFile(.{ .file = std.build.LazyPath.relative("buffer.c"), .flags = &.{} });
//exe.linkLibC();
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args); //
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
22 changes: 22 additions & 0 deletions examples/2024-01-15-zig-build-explained/part2/2.1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>

void * buffer_create(size_t len);
void buffer_destroy(void * ptr);
void buffer_write(void * buf, size_t offset, void * data, size_t len);
void buffer_read(void * buf, size_t offset, void * data, size_t len);

int main()
{
void * buf = buffer_create(8);
buffer_write(buf, 0, "World", 6);

{
char str[8];
buffer_read(buf, 0, str, 6);
printf("Hello, %s!\n", str);
}

buffer_destroy(buf);

return 0;
}
24 changes: 24 additions & 0 deletions examples/2024-01-15-zig-build-explained/part2/2.10/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "example",
.target = target,
.optimize = optimize,
});
exe.addCSourceFile(.{
.file = std.build.LazyPath.relative("main.m"),
.flags = &.{},
});
exe.linkFramework("Foundation");
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
15 changes: 15 additions & 0 deletions examples/2024-01-15-zig-build-explained/part2/2.10/main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#import "Foundation/Foundation.h"

@interface SampleClass : NSObject
- (void)sampleMethod;
@end
@implementation SampleClass
- (void)sampleMethod {
NSLog(@"Hello World in Objective-C!\n");
}
@end
int main() {
SampleClass *sampleClass = [[SampleClass alloc]init];
[sampleClass sampleMethod];
return 0;
}
28 changes: 28 additions & 0 deletions examples/2024-01-15-zig-build-explained/part2/2.11/buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdlib.h>
#include <string.h>

void * buffer_create(size_t len)
{
return malloc(len);
}

void buffer_destroy(void * ptr)
{
free(ptr);
}

void buffer_write(void * buf, size_t offset, void * data, size_t len)
{
memcpy(
(char*)buf + offset,
data,
len);
}

void buffer_read(void * buf, size_t offset, void * data, size_t len)
{
memcpy(
data,
(char*)buf + offset,
len);
}
Loading