Why does ZLS not work with my project? #1996
-
Hi, I'm currently struggling with making ZLS work with my project and I'd like to get some external opinions. First of all, my project: I'm currently working on developing a HAL for different platforms, very similar to MicroZig. Before someone says it, I know I'm kinda reinventing the wheel and they've already done it and etc, but I'm doing it because I want to learn about the language, its build system, etc. The project is called UniMicro and the most important is this:
pub fn init(b: *std.Build, options: UniMicroOptions) *std.Build.Step.InstallFile {
// Determine the architecture to be used.
const target = b.resolveTargetQuery(options.target_platform.cpu.target);
// Compile the ELF
const elf = b.addExecutable(.{
.name = b.fmt("{s}.elf", .{options.name}),
.root_source_file = .{ .cwd_relative = root() ++ "/main.zig" },
.target = target,
.optimize = options.optimization_level,
.single_threaded = options.target_platform.single_threaded,
.linkage = .static,
});
// Set the entrypoint to the unimicro_main function. It prepares everything
// and then calls the user's main function.
elf.entry = .{ .symbol_name = "unimicro_main" };
// Module with the HAL to be imported by the user.
const hal_module = b.createModule(.{
.root_source_file = .{
.cwd_relative = b.fmt("{s}/hal/{s}/{s}/hal.zig", .{
root(),
options.target_platform.manufacturer,
options.target_platform.name,
}),
},
.target = target,
.optimize = options.optimization_level,
.single_threaded = options.target_platform.single_threaded,
});
// Create an app module containing the user's code.
const app_module = b.createModule(.{
.root_source_file = options.main_file,
.target = target,
.optimize = options.optimization_level,
.single_threaded = options.target_platform.single_threaded,
.imports = &.{
.{ .name = "UniMicro", .module = hal_module },
},
});
// Add the app module as an import on the main.zig file.
elf.root_module.addImport("app", app_module);
// Create a module containing the functions startup and hang, which are
// processor specific and, threrefore, come from the cpus/ directory.
const cpu_module = b.createModule(.{
.root_source_file = .{ .cwd_relative = b.fmt("{s}/cpus/{s}/{s}.zig", .{
root(),
options.target_platform.cpu.manufacturer,
options.target_platform.cpu.name,
}) },
.target = target,
.optimize = options.optimization_level,
.single_threaded = options.target_platform.single_threaded,
});
// Add the cpu module as an import on the main.zig file.
elf.root_module.addImport("cpu", cpu_module);
// Create the executable that generates the linker script.
const linker_gen = b.addExecutable(.{
.name = "linker_generator",
.root_source_file = .{ .cwd_relative = root() ++ "/utils/linker_generator.zig" },
.target = b.graph.host,
.optimize = std.builtin.OptimizeMode.ReleaseFast,
});
// Add an import "UniMicro" to the linker_generator executable.
linker_gen.root_module.addImport("UniMicro", b.createModule(.{
.root_source_file = .{ .cwd_relative = root() ++ "/build.zig" },
}));
// Stringify the args to be passed to the linker_generator.
const args_str = std.json.stringifyAlloc(b.allocator, options.target_platform, .{}) catch @panic("Out of Memory!");
// Run the linker_generator.
const linker_gen_run = b.addRunArtifact(linker_gen);
linker_gen_run.addArg(args_str);
// Set the linker script of the ELF to be that generated by the
// linker generator executable.
elf.setLinkerScript(linker_gen_run.addOutputFileArg("memory.ld"));
// Copy elf to output dir
const copy_elf = b.addInstallArtifact(elf, .{});
b.default_step.dependOn(©_elf.step);
// Make a bin out of elf
const bin = b.addObjCopy(elf.getEmittedBin(), .{
.format = .bin,
});
bin.step.dependOn(©_elf.step);
// Copy bin to the output directory
const copy_bin = b.addInstallBinFile(bin.getOutput(), b.fmt("{s}.bin", .{options.name}));
b.default_step.dependOn(©_bin.step);
return copy_bin;
}
pub const UniMicroOptions = struct {
name: []const u8,
main_file: std.Build.LazyPath,
optimization_level: std.builtin.OptimizeMode,
target_platform: Chip,
};
pub const Chip = struct {
name: []const u8,
manufacturer: []const u8,
cpu: Cpu,
memory_sections: []const MemorySection,
interrupts: []const []const u8,
single_threaded: bool = false,
};
pub const Cpu = struct {
name: []const u8,
manufacturer: []const u8,
target: std.Target.Query,
}; The contents of the the other files are not really essential. Then I create a separate zig project, run pub fn build(b: *std.Build) void {
const optimization_level = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSafe });
var install_step = UniMicro.init(b, .{
.name = "test",
.main_file = .{ .cwd_relative = "src/main.zig" },
.optimization_level = optimization_level,
.target_platform = UniMicro.supported_chips.stmicro.stm32f411cc,
});
// Some other configuration...
} The build system works flawlessly, giving me a So, here goes my big issue: ZLS on the project that uses UniMicro does not present me any autocomplete or any help at all. Is it some configuration I'm missing? Is my project's structure just not compatible with ZLS? Does ZLS not support modules fetched with the Zig package manager? This issue is really a deal breaker for me and is making me put this project aside 😞 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Is your problem that something like this doesn't provide completions? const UniMicro = @import("UniMicro");
pub fn main() !void {
UniMicro.modules.<cursor>
} Try changing pub const modules = struct {
pub const gpio = @import("gpio.zig");
pub const rcc = @import("rcc.zig").rcc;
pub const interrupts = @import("interrupts.zig");
}; Otherwise I did not encounter any problems when I looked into how ZLS inspects your project. If the previous step didn't help, could you do the following:
I believe that you trying to say that something that uses |
Beta Was this translation helpful? Give feedback.
Probably getting unlucky with the
name
overlapCould you post the output of (run in the root folder of your test project):
zig build --build-runner /home/lucas/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig