From efa0a3958bd24fa1db689e0d76e976687471da87 Mon Sep 17 00:00:00 2001 From: Ondra Voves Date: Sat, 21 Dec 2024 21:04:00 +0100 Subject: [PATCH] Initial commit --- .ci/build.sh | 11 +++ .github/workflows/test.yaml | 113 ++++++++++++++++++++++++++ .gitignore | 39 +++++++++ .gitmodules | 3 + .version | 1 + .vscode/extensions.json | 8 ++ .zigversion | 1 + README.md | 31 +++++++ build.zig | 140 ++++++++++++++++++++++++++++++++ build.zig.zon | 17 ++++ content/.gitkeep | 0 externals/cetech1 | 1 + modules/minimal/build.zig | 35 ++++++++ modules/minimal/build.zig.zon | 11 +++ modules/minimal/src/private.zig | 69 ++++++++++++++++ tools/generate_vscode.zig | 30 +++++++ 16 files changed, 510 insertions(+) create mode 100755 .ci/build.sh create mode 100644 .github/workflows/test.yaml create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .version create mode 100644 .vscode/extensions.json create mode 100644 .zigversion create mode 100644 README.md create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 content/.gitkeep create mode 160000 externals/cetech1 create mode 100644 modules/minimal/build.zig create mode 100644 modules/minimal/build.zig.zon create mode 100644 modules/minimal/src/private.zig create mode 100644 tools/generate_vscode.zig diff --git a/.ci/build.sh b/.ci/build.sh new file mode 100755 index 0000000..cb00ee5 --- /dev/null +++ b/.ci/build.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -x +set -e + +git -C externals/cetech1/ submodule update --init + +zig build init +zig build -Dwith_tracy=false + +ls -Rhan zig-out/ diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..0bcd50d --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,113 @@ +name: Test + +permissions: + checks: write + +on: + pull_request: + push: + +concurrency: + group: ${{ github.head_ref || github.run_id }}-${{ github.actor }} + cancel-in-progress: true + +jobs: + validation: + name: Validation + runs-on: ubuntu-latest + timeout-minutes: 1 + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Read .zig-version + id: zigversion + uses: juliangruber/read-file-action@v1 + with: + path: ./.zigversion + - name: Install Zig + uses: mlugg/setup-zig@v1 + with: + version: ${{ steps.zigversion.outputs.content }} + + - name: Lint + run: zig fmt --check . --exclude externals/ + + x86-64-linux: + needs: ["validation"] + name: x86-64 Linux + runs-on: linux-latest + timeout-minutes: 20 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: true + + - name: Prepare + uses: awalsh128/cache-apt-pkgs-action@latest + with: + packages: libdbus-1-dev + version: 1.0 + + - name: Read .zig-version + id: zigversion + uses: juliangruber/read-file-action@v1 + with: + path: ./.zigversion + - name: Install Zig + uses: mlugg/setup-zig@v1 + with: + version: ${{ steps.zigversion.outputs.content }} + + - name: Build + run: .ci/build.sh + + x86-64-macos: + needs: ["validation"] + name: x86-64 Macos + runs-on: macos-latest + timeout-minutes: 20 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: true + + - name: Read .zig-version + id: zigversion + uses: juliangruber/read-file-action@v1 + with: + path: ./.zigversion + - name: Install Zig + uses: mlugg/setup-zig@v1 + with: + version: ${{ steps.zigversion.outputs.content }} + + - name: Build + run: .ci/build.sh + + x86-64-windows: + needs: ["validation"] + name: x86-64 Windows + runs-on: windows-latest + timeout-minutes: 20 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: true + + - name: Read .zig-version + id: zigversion + uses: juliangruber/read-file-action@v1 + with: + path: ./.zigversion + - name: Install Zig + uses: mlugg/setup-zig@v1 + with: + version: ${{ steps.zigversion.outputs.content }} + + - name: Build + shell: bash + run: .ci/build.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8bdc240 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +# Build +/build + +# ZIG +*zig-cache +*zig-out + +# VSCode + +# MacOS +*.DS_Store +.DS_Store +*.dylib + +# Xcode +*.pbxuser +*.mode1v3 +*.mode2v3 +*.perspectivev3 +*.xcuserstate +project.xcworkspace/ +xcuserdata/ + +kcov-output/ + +imgui.ini + +_static.zig + +# VSCode +.vscode/launch.json +.vscode/settings.json + +# temp folder for tests +/fixtures/tmp + +# folder for generated tmp data, cache and debug purpose files +.ct_temp + diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ca1bc05 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "externals/cetech1"] + path = externals/cetech1 + url = https://github.com/cyberegoorg/cetech1.git diff --git a/.version b/.version new file mode 100644 index 0000000..ffad661 --- /dev/null +++ b/.version @@ -0,0 +1 @@ +0.1.0-a1 \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..689fb33 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,8 @@ +{ + "recommendations": [ + "ziglang.vscode-zig", // ZIG support. + "vadimcn.vscode-lldb", // Debugging support. + "Gruntfuggly.todo-tree", // Todo viewer. + "kdheepak.d2-markdown-preview" // D2 preview + ] +} \ No newline at end of file diff --git a/.zigversion b/.zigversion new file mode 100644 index 0000000..ba55757 --- /dev/null +++ b/.zigversion @@ -0,0 +1 @@ +2024.10.0-mach \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..27b31b8 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# CETech1 minimal + +> [!IMPORTANT] +> Work in progressssssssssssss + +## Getting started + +1. Create repository from this template. + +2. [Get ZIG/ZLS](https://cyberegoorg.github.io/cetech1/getting-started.html#get-zig-zls) + +3. Init + + ```sh + git -C externals/cetech1/ submodule update --init + zig build init + ``` + +4. Build + + ```sh + zig build + ``` + +5. Create CETech1 project in `content` dir + + ```sh + zig-out/bin/cetech1 --asset-root content/ + ``` + +6. Have fun diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..c7d03c7 --- /dev/null +++ b/build.zig @@ -0,0 +1,140 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +const min_zig_version = std.SemanticVersion.parse("0.14.0-dev.1911") catch @panic("Where is .zigversion?"); +const version = std.SemanticVersion.parse(@embedFile(".version")) catch @panic("Where is .version?"); + +const cetech1_build = @import("cetech1"); + +const enabled_cetech_modules = cetech1_build.core_modules ++ cetech1_build.editor_modules; + +pub const modules = [_][]const u8{ + // Minimal + "minimal", +}; + +pub fn build(b: *std.Build) !void { + try ensureZigVersion(); + + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + // + // OPTIONS + // + + const options = .{ + .externals_optimize = b.option(std.builtin.OptimizeMode, "externals_optimize", "Optimize for externals libs") orelse .ReleaseFast, + .dynamic_modules = b.option(bool, "dynamic_modules", "build all modules in dynamic mode.") orelse true, + //.static_modules = b.option(bool, "static_modules", "build all modules in static mode.") orelse false, + + // Tracy options + .enable_tracy = b.option(bool, "with_tracy", "build with tracy.") orelse true, + .tracy_on_demand = b.option(bool, "tracy_on_demand", "build tracy with TRACY_ON_DEMAND") orelse true, + }; + + const options_step = b.addOptions(); + options_step.addOption(std.SemanticVersion, "version", version); + + // add build args + inline for (std.meta.fields(@TypeOf(options))) |field| { + options_step.addOption(field.type, field.name, @field(options, field.name)); + } + const options_module = options_step.createModule(); + _ = options_module; // autofix + + // + // Extrnals + // + + // Cetech1 + const cetech1 = b.dependency( + "cetech1", + .{ + .target = target, + .optimize = optimize, + // .optimize = options.externals_optimize, + + .with_tracy = options.enable_tracy, + .tracy_on_demand = options.tracy_on_demand, + + //.static_modules = options.static_modules, + .dynamic_modules = options.dynamic_modules, + }, + ); + + // + // TOOLS + // + + const generate_vscode_tool = b.addExecutable(.{ + .name = "generate_vscode", + .root_source_file = b.path("tools/generate_vscode.zig"), + .target = target, + }); + generate_vscode_tool.root_module.addAnonymousImport("generate_vscode", .{ + .root_source_file = b.path("externals/cetech1/src/tools/generate_vscode.zig"), + }); + + // + // Init repository step + // + const init_step = b.step("init", "init repository"); + cetech1_build.initStep(b, init_step, "externals/cetech1/"); + + // + // Gen vscode + // + const vscode_step = b.step("vscode", "init/update vscode configs"); + const gen_vscode = b.addRunArtifact(generate_vscode_tool); + gen_vscode.addDirectoryArg(b.path(".vscode/")); + vscode_step.dependOn(&gen_vscode.step); + + b.installArtifact(cetech1.artifact("shaderc")); + + if (options.dynamic_modules) { + b.installArtifact(cetech1.artifact("cetech1")); + + var buff: [256:0]u8 = undefined; + for (enabled_cetech_modules) |m| { + const artifact_name = try std.fmt.bufPrintZ(&buff, "ct_{s}", .{m}); + const art = cetech1.artifact(artifact_name); + const step = b.addInstallArtifact(art, .{}); + b.default_step.dependOn(&step.step); + } + + for (modules) |m| { + const artifact_name = try std.fmt.bufPrintZ(&buff, "ct_{s}", .{m}); + const art = b.dependency(m, .{ + .target = target, + .optimize = optimize, + }).artifact(artifact_name); + + const step = b.addInstallArtifact(art, .{}); + b.default_step.dependOn(&step.step); + } + } +} + +fn ensureZigVersion() !void { + var installed_ver = builtin.zig_version; + installed_ver.build = null; + + if (installed_ver.order(min_zig_version) == .lt) { + std.log.err("\n" ++ + \\--------------------------------------------------------------------------- + \\ + \\Installed Zig compiler version is too old. + \\ + \\Min. required version: {any} + \\Installed version: {any} + \\ + \\Please install newer version and try again. + \\zig/get_zig.sh + \\ + \\--------------------------------------------------------------------------- + \\ + , .{ min_zig_version, installed_ver }); + return error.ZigIsTooOld; + } +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..ba9b243 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,17 @@ +.{ + .name = "cetech1-minimal", + .version = "0.1.0", + .paths = .{ + "build.zig", + "build.zig.zon", + "README.md", + }, + .dependencies = .{ + .cetech1 = .{ .path = "externals/cetech1" }, + + // + // Modules + // + .minimal = .{ .path = "modules/minimal" }, + }, +} diff --git a/content/.gitkeep b/content/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/externals/cetech1 b/externals/cetech1 new file mode 160000 index 0000000..ecece3b --- /dev/null +++ b/externals/cetech1 @@ -0,0 +1 @@ +Subproject commit ecece3b9d21dbc67c9f20658de4ea76046c179ef diff --git a/modules/minimal/build.zig b/modules/minimal/build.zig new file mode 100644 index 0000000..2e221aa --- /dev/null +++ b/modules/minimal/build.zig @@ -0,0 +1,35 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +const version: std.SemanticVersion = .{ .major = 0, .minor = 1, .patch = 0 }; + +pub fn build(b: *std.Build) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const cetech1 = b.dependency("cetech1", .{}); + const cetech1_module = cetech1.module("cetech1"); + + const lib = b.addSharedLibrary(.{ + .name = "ct_minimal", + .version = version, + .root_source_file = b.path("src/private.zig"), + .target = target, + .optimize = optimize, + }); + + const slib = b.addStaticLibrary(.{ + .name = "ct_minimal_static", + .version = version, + .root_source_file = b.path("src/private.zig"), + .target = target, + .optimize = optimize, + }); + + inline for (.{ lib, slib }) |l| { + l.root_module.addImport("cetech1", cetech1_module); + + l.addIncludePath(b.path("includes")); + b.installArtifact(l); + } +} diff --git a/modules/minimal/build.zig.zon b/modules/minimal/build.zig.zon new file mode 100644 index 0000000..0c50209 --- /dev/null +++ b/modules/minimal/build.zig.zon @@ -0,0 +1,11 @@ +.{ + .name = "foo", + .version = "0.1.0", + .paths = .{ + "build.zig", + "build.zig.zon", + }, + .dependencies = .{ + .cetech1 = .{ .path = "../../externals/cetech1/public/" }, + }, +} diff --git a/modules/minimal/src/private.zig b/modules/minimal/src/private.zig new file mode 100644 index 0000000..928ddb9 --- /dev/null +++ b/modules/minimal/src/private.zig @@ -0,0 +1,69 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const cetech1 = @import("cetech1"); +const strid = cetech1.strid; +const cdb = cetech1.cdb; + +const module_name = .minimal; + +// Need for logging from std. +pub const std_options: std.Options = .{ + .logFn = cetech1.log.zigLogFnGen(&_log), +}; +// Log for module +const log = std.log.scoped(module_name); + +// Basic cetech "import". +var _allocator: Allocator = undefined; +var _log: *const cetech1.log.LogAPI = undefined; +var _cdb: *const cdb.CdbAPI = undefined; +var _coreui: *const cetech1.coreui.CoreUIApi = undefined; +var _kernel: *const cetech1.kernel.KernelApi = undefined; +var _tmpalloc: *const cetech1.tempalloc.TempAllocApi = undefined; + +// Play with this constants to enable some features +const spam_log = false; +const do_tasks = false; +const do_cdb = false; + +// Global state that can surive hot-reload +const G = struct {}; +var _g: *G = undefined; + +// Register all cdb stuff in this method + +// Create types, register api, interfaces etc... +pub fn load_module_zig(apidb: *const cetech1.apidb.ApiDbAPI, allocator: Allocator, log_api: *const cetech1.log.LogAPI, load: bool, reload: bool) anyerror!bool { + // basic + _allocator = allocator; + _log = log_api; + _cdb = apidb.getZigApi(module_name, cdb.CdbAPI).?; + _coreui = apidb.getZigApi(module_name, cetech1.coreui.CoreUIApi).?; + _kernel = apidb.getZigApi(module_name, cetech1.kernel.KernelApi).?; + _tmpalloc = apidb.getZigApi(module_name, cetech1.tempalloc.TempAllocApi).?; + + // create global variable that can survive reload + _g = try apidb.globalVar(G, module_name, "_g", .{}); + + if (load) { + log.info("LOAD", .{}); + return true; + } + + if (!load) { + log.info("UNLOAD", .{}); + return true; + } + + if (reload) { + log.info("WITH RELOAD", .{}); + } + + return true; +} + +// This is only one fce that cetech1 need to load/unload/reload module. +pub export fn ct_load_module_minimal(apidb: *const cetech1.apidb.ApiDbAPI, allocator: *const std.mem.Allocator, load: bool, reload: bool) callconv(.C) bool { + return cetech1.modules.loadModuleZigHelper(load_module_zig, module_name, apidb, allocator, load, reload); +} diff --git a/tools/generate_vscode.zig b/tools/generate_vscode.zig new file mode 100644 index 0000000..20b18cf --- /dev/null +++ b/tools/generate_vscode.zig @@ -0,0 +1,30 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +const generate_vscode = @import("generate_vscode"); +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allcator = gpa.allocator(); + + const args = try std.process.argsAlloc(allcator); + defer std.process.argsFree(allcator, args); + + if (args.len != 2) fatal("wrong number of arguments {d}", .{args.len}); + + const vscode_path = args[1]; + + var vscode_dir = try std.fs.openDirAbsolute(vscode_path, .{}); + defer vscode_dir.close(); + + try generate_vscode.createLauchJson(allcator, vscode_dir, .{ + .configurations = &.{}, + }); + + try generate_vscode.createOrUpdateSettingsJson(allcator, vscode_dir, true); +} + +fn fatal(comptime format: []const u8, args: anytype) noreturn { + std.debug.print(format, args); + std.process.exit(1); +}