From b1e189b4f463a21d98d84f3ddbd19ed47201e6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=22xq=22=20Quei=C3=9Fner?= Date: Wed, 24 Jan 2024 12:52:47 +0100 Subject: [PATCH] Adds step/option for printing the available targets, shortens target list. --- build/build.zig | 87 +++++++++++++++++++++++++++++---- examples/next-gen/build.zig | 12 ++--- examples/next-gen/build.zig.zon | 8 +-- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/build/build.zig b/build/build.zig index 8150551..272bd69 100644 --- a/build/build.zig +++ b/build/build.zig @@ -246,22 +246,66 @@ pub fn createBuildEnvironment(b: *std.Build, comptime info: EnvironmentInfo) *Bu .self_pkg_name = comptime info.self, .core_pkg_name = comptime info.core, .cpus = &core_module.cpus, + + .show_targets_step = std.Build.Step.init(.{ + .id = .custom, + .name = "Show MicroZig targets", + .owner = b, + .makeFn = BuildEnvironment.print_target_steps, + .first_ret_addr = null, + }), }; be.self = b.dependency(info.self, .{}); be.microzig_core = b.dependency(info.core, .{}); - inline for (be.board_support_packages, requested_bsps) |*bsp, def| { - bsp.* = BoardSupportPackage{ - .name = def.import_name, - .dep = b.dependency(def.import_name, .{}), - }; + // Fetch all available board support packages and targets: + { + var duplicate_package_names = std.StringArrayHashMap(void).init(be.host_build.allocator); + defer duplicate_package_names.deinit(); + + inline for (be.board_support_packages, requested_bsps) |*bsp, def| { + bsp.* = BoardSupportPackage{ + .name = def.import_name, + .dep = b.dependency(def.import_name, .{}), + }; + + for (def.bsp.targets) |tgt| { + const full_name = b.fmt("{s}#{s}", .{ tgt.id, def.import_name }); + + const old_value = be.targets.fetchPut(be.host_build.allocator, tgt.id, tgt.target) catch @panic("out of memory"); + be.targets.put(be.host_build.allocator, full_name, tgt.target) catch @panic("out of memory"); + + if (old_value != null) { + duplicate_package_names.put(tgt.id, {}) catch @panic("out of memory"); + } + } + } + + // Remove all non-unique packages from the list as they are ambigious: + for (duplicate_package_names.keys()) |key| { + _ = be.targets.orderedRemove(key); + } + + // reuse set to store names of unique long names + duplicate_package_names.clearRetainingCapacity(); + + // Find all elements where only a single non-hashed variant exists: + for (be.targets.keys()) |long_value| { + const index = std.mem.indexOfScalar(u8, long_value, '#') orelse continue; - for (def.bsp.targets) |tgt| { - const full_name = b.fmt("{s}#{s}", .{ tgt.id, def.import_name }); + const short_value = long_value[0..index]; - be.targets.put(be.host_build.allocator, tgt.id, tgt.target) catch @panic("out of memory"); - be.targets.put(be.host_build.allocator, full_name, tgt.target) catch @panic("out of memory"); + if (be.targets.get(short_value) != null) { + // If we have the short variant, we don't have a duplicate anymore, so + // let's drop the long variant: + duplicate_package_names.put(long_value, {}) catch @panic("out of memory"); + } + } + + // Drop all unnecessary long variants: + for (duplicate_package_names.keys()) |key| { + _ = be.targets.orderedRemove(key); } } @@ -288,11 +332,18 @@ pub const BuildEnvironment = struct { cpus: *const CpuArray, + show_targets_step: std.Build.Step, + /// Searches for a target called `name` and returns a pointer to the MicroZig Target if it exists. pub fn findTarget(env: *const BuildEnvironment, name: []const u8) ?*const Target { return env.targets.getPtr(name); } + /// Returns a slice to all available target names. + pub fn getTargetNames(env: *const BuildEnvironment) []const []const u8 { + return env.targets.keys(); + } + /// Returns the instance to the CPU descriptor for the given CPU model. pub fn getCpuDescriptor(env: *BuildEnvironment, model: CpuModel) *const Cpu { return if (model == .custom) @@ -301,6 +352,13 @@ pub const BuildEnvironment = struct { env.cpus.getPtrConst(model); } + /// Returns a build step that will print all available targets to this instance. + /// + /// Can be used to provide a list to the user or developer. + pub fn getShowTargetsStep(env: *BuildEnvironment) *std.Build.Step { + return &env.show_targets_step; + } + /// Declares a new MicroZig firmware file. pub fn addFirmware( /// The MicroZig instance that should be used to create the firmware. @@ -640,6 +698,17 @@ pub const BuildEnvironment = struct { return write.add("linker.ld", contents.items); } + + fn print_target_steps(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { + _ = prog_node; + + const env = @fieldParentPtr(BuildEnvironment, "show_targets_step", step); + + std.debug.print("Available MicroZig targets:\n", .{}); + for (env.targets.keys()) |listed_target_name| { + std.debug.print("* {s}\n", .{listed_target_name}); + } + } }; //////////////////////////////////////// diff --git a/examples/next-gen/build.zig b/examples/next-gen/build.zig index 0c3579e..2c500c3 100644 --- a/examples/next-gen/build.zig +++ b/examples/next-gen/build.zig @@ -2,18 +2,14 @@ const std = @import("std"); const MicroZig = @import("microzig"); pub fn build(b: *std.Build) void { - const microzig = MicroZig.createBuildEnvironment(b, .{ - .self = "microzig", // package name of the build package (optional) - .core = "microzig-core", // package name of the core package (optional) - }); + const microzig = MicroZig.createBuildEnvironment(b, .{}); + + const show_targets_step = b.step("show-targets", "Shows all available MicroZig targets"); + show_targets_step.dependOn(microzig.getShowTargetsStep()); const optimize = b.standardOptimizeOption(.{}); const target_name = b.option([]const u8, "target", "Select the target to build for.") orelse "board:mbed/lpc1768"; - for (microzig.targets.keys()) |listed_target_name| { - std.debug.print("- '{s}'\n", .{listed_target_name}); - } - const target = microzig.findTarget(target_name).?; const firmware = microzig.addFirmware(b, .{ diff --git a/examples/next-gen/build.zig.zon b/examples/next-gen/build.zig.zon index ce5dbe8..d587aec 100644 --- a/examples/next-gen/build.zig.zon +++ b/examples/next-gen/build.zig.zon @@ -4,19 +4,19 @@ .dependencies = .{ .microzig = .{ .url = "https://public.devspace.random-projects.net/microzig-build.tar.gz", - .hash = "12208fcae95a6d3bc80301bfbabe9f937cf299188f44bed100f61e39437d8fc4a49a", + .hash = "122001f3340f37dd4d80e0942407a503e6a429d7c92a05221a3e301b947782933bdb", }, .@"microzig-core" = .{ .url = "https://public.devspace.random-projects.net/microzig-core.tar.gz", - .hash = "122085d8c30906f461a3aecb54eb0dadb644c25724b0eb2d3fc89f1f4c3a8d411be2", + .hash = "1220460c2625fb17a075b17f720f275d814544e1f20a8f89e7e542ff3e0d697111bc", }, .@"microzig-bsp-nxp" = .{ .url = "https://public.devspace.random-projects.net/board-support/nxp/lpc.tar.gz", - .hash = "12201530a2d4d2751a5bc93720d3a3d6eab0349cc271004102bcc470beffb02c7e84", + .hash = "122005a181f1d51aaffd02748ab5d910597f328a12b37e68dc5a1993716d9888ccc5", }, .@"microzig-bsp-rp2040" = .{ .url = "https://public.devspace.random-projects.net/board-support/raspberrypi/rp2040.tar.gz", - .hash = "1220817e39ac34923bae65047afc344706c11e7dd054efee3ca31ff78adc78a4e0f6", + .hash = "122094dceb25e3664b96a6b482529fdc254582048d82fcec7126678e5e021b26af2c", }, }, } \ No newline at end of file