kassane / zig-esp-idf-sample

Run Zig on esp-idf (Xtensa/RISC-V)
Apache License 2.0
60 stars 4 forks source link

How can I make ZLS happy? #17

Closed JanBeelte closed 1 week ago

JanBeelte commented 2 weeks ago

I try to get the VSCode autocompletion to work with the idf import. Currently I am getting:

error: (store ): Failed to execute build runner to collect build configuration, command:
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/zig build --build-runner /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig
Error: /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig:85:35: error: missing struct field: zig_lib_directory
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/lib/std/Build.zig:113:19: note: struct declared here

error: (store ): Failed to load build configuration for file:///workspaces/open-sensor-platform/firmware/blink/build.zig (error: error.RunFailed)
error: (store ): Failed to execute build runner to collect build configuration, command:
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/zig build --build-runner /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig
Error: /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig:85:35: error: missing struct field: zig_lib_directory
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/lib/std/Build.zig:113:19: note: struct declared here

error: (store ): Failed to load build configuration for file:///workspaces/open-sensor-platform/firmware/blink/build.zig (error: error.RunFailed)

do I need any additional configuration to make ZLS happy? On https://github.com/kassane/zig-esp-idf-sample/issues/16 you mentioned something about idf EnvVar, I did not fully understand that part.

Thanks a lot for your help!

Best,

Jan

kassane commented 2 weeks ago

Hi, @JanBeelte

What is the zls version?

The zig-xtensa version is stuck at the latest version 0.14.0-dev with llvm18 (because of the espressif fork).

Aware of the previous comment. But it is also worth noting (a mistake on my part) that if you are using zig-upstream in the system PATH you may encounter a issue with zls invoking zig build to generate zig-cache for reading.

Why?

See esp32p4 cpu-target in build.zig (custom target exclusive to espressif fork) or remove esp32p4 target.

---- edit

do I need any additional configuration to make ZLS happy?

No.

JanBeelte commented 1 week ago

I used the latest release from: https://github.com/zigtools/zls which is 0.13.0 I was thinking about building 0.14.0 from source but I could not even find a tag for that.

Should I just use the latest commit on main branch? Will this be compatible with xtensa fork?

Another question: We are only interested in the RISC-V Variants (C2/C3 specifically), do we even have to use the espressif fork in that case?

kassane commented 1 week ago

I use zls or zigscient (zls fork) master.

I have now succeeded in re-enabling autocomplete in the editor and finding out what the problem is in this case. (may need clean $HOME/.cache/zls and zig-cache after change)

diff --git a/build.zig b/build.zig
index 8a24a1e..ddd1fc1 100644
--- a/build.zig
+++ b/build.zig
@@ -83,7 +83,7 @@ fn includeDeps(b: *std.Build, lib: *std.Build.Step.Compile) !void {
 }

 pub fn searched_idf_libs(b: *std.Build, lib: *std.Build.Step.Compile) !void {
-    var dir = try std.fs.cwd().openDir("../build", .{
+    var dir = try std.fs.cwd().openDir("./build", .{
         .iterate = true,
     });
     defer dir.close();
@@ -598,13 +598,6 @@ const riscv_targets = &[_]std.Target.Query{
         .cpu_features_add = std.Target.riscv.featureSet(&.{ .m, .a, .c, .zifencei, .zicsr }),
     },
     // esp32-p4 have .xesppie cpu-feature (espressif vendor extension)
-    .{
-        .cpu_arch = .riscv32,
-        .cpu_model = .{ .explicit = &std.Target.riscv.cpu.esp32p4 },
-        .os_tag = .freestanding,
-        .abi = .eabihf,
-        .cpu_features_sub = std.Target.riscv.featureSet(&.{ .zca, .zcb, .zcmt, .zcmp }),
-    },
 };
 const xtensa_targets = &[_]std.Target.Query{
     // need zig-fork (using espressif-llvm backend) to support this

Captura de imagem_20241119_103326

kassane commented 1 week ago

But this broken cmake build!! :face_exhaling:

-    var dir = try std.fs.cwd().openDir("../build", .{
+    var dir = try std.fs.cwd().openDir("./build", .{

edit

Why ../build path?

main/CMakelists.txt call zig build in rootpath. Zig get collect all obj files to join in static-lib (avoid undef. references) in build path.

JanBeelte commented 1 week ago

I use zls or zigscient (zls fork) master.

would it be an option to add a working zls release as a compiled package to https://github.com/kassane/zig-espressif-bootstrap/releases ? That way I could easily include it into my docker devcontainer :-)

Best,

Jan

kassane commented 1 week ago

would it be an option to add a working zls release as a compiled package to[...]

Please! Try this change with zls 0.13.0.

JanBeelte commented 1 week ago

Please! Try this change with zls 0.13.0.

So far no luck on my end. I tried the following steps:

  1. Apply changes to build.zig

  2. rm -r .zig-cache/

  3. Try auto complete (not working)

  4. zig build in root gives the following error:

    error: unable to check cache: stat file '../build/CMakeFiles/zig-sample-idf.elf.dir/project_elf_src_esp32c3.c.obj' failed: FileNotFound
    error: the following command failed with 1 compilation errors:
    /opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/zig build-lib @/workspaces/open-sensor-platform/firmware/blink/.zig-cache/args/4223c0265e3ed0c56e2371105880993db471b85add2f1349ee842df7955f69eb 
    Build Summary: 0/3 steps succeeded; 1 failed
    install transitive failure
    └─ install app_zig transitive failure
    └─ zig build-lib app_zig Debug xtensa-freestanding-none 1 errors
    error: the following build command failed with exit code 1:
    /workspaces/open-sensor-platform/firmware/blink/.zig-cache/o/16657f8492b8ccaa85bf37111e4f0cd4/build /opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/zig /opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/lib /workspaces/open-sensor-platform/firmware/blink /workspaces/open-sensor-platform/firmware/blink/.zig-cache /home/esp/.cache/zig --seed 0xf9bdf7db -Z68bd84308dded7e5
  5. idf.py build gives error as you pointed out. Should I change back to "../build" ?

  6. Auto-complete still not working

Did I miss anything in the process?

Thanks for the help!

kassane commented 1 week ago

idf.py build gives error as you pointed out. Should I change back to "../build" ?

Running cmakelists in rootpath will call main/cmakelists and will also automatically invoke build.zig.

Try just configuring idf.py.

1 - idf.py fullclean 2 - idf.py set-config esp32-c3 3 - open editor with build.zig change. 4 - test autocomplete (see const idf = @import(...) color change)

For idf.py build, need undo ./build to ../build path on build.zig.

JanBeelte commented 1 week ago

set-config could not be found, you probably meant set-target?

I followed all steps but still zls release 0.13.0 does not like me:

error: (store ): Failed to execute build runner to collect build configuration, command:
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/zig build --build-runner /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig
Error: /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig:85:35: error: missing struct field: zig_lib_directory
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/lib/std/Build.zig:113:19: note: struct declared here

error: (store ): Failed to load build configuration for file:///workspaces/open-sensor-platform/firmware/blink/build.zig (error: error.RunFailed)
error: (store ): Failed to execute build runner to collect build configuration, command:
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/zig build --build-runner /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig
Error: /home/esp/.cache/zls/build_runner/21872970afd69e48a0847077e5196711/build_runner.zig:85:35: error: missing struct field: zig_lib_directory
/opt/zig-relsafe-espressif-x86_64-linux-musl-baseline/lib/std/Build.zig:113:19: note: struct declared here

error: (store ): Failed to load build configuration for file:///workspaces/open-sensor-platform/firmware/blink/build.zig (error: error.RunFailed)
kassane commented 1 week ago

set-config could not be found, you probably meant set-target?

yes. sorry :sweat_smile:

I followed all steps but still zls release 0.13.0 does not like me:

Ok. need zls master!

I'll build it.

JanBeelte commented 1 week ago

Ok. need zls master!

I'll build it.

Thanks ☺️ looking forward to it!

kassane commented 1 week ago

Thanks ☺️ looking forward to it!

https://github.com/kassane/zig-espressif-bootstrap/releases/download/0.14.0-xtensa-dev/zls-x86_64-linux-musl-baseline_ea2c303b0.tar.xz

~/zls/zig-out/bin $ sha256sum zls-x86_64-linux-musl-baseline_ea2c303b0.tar.xz 
d74ce12d36d4ba88f2d3ea01e4cb4db0cbf70e2bc1b99a6d3381a8dd62d7ea82  zls-x86_64-linux-musl-baseline_ea2c303b0.tar.xz

zls commit: https://github.com/zigtools/zls/commit/ea2c303b0ddb08a0d364b8eb7959d685fe9e65c6 built using zig-xtensa (compatible semantic analyzer)

JanBeelte commented 1 week ago

It is working perfectly now, thank you very much 😊

kassane commented 1 week ago

It is working perfectly now, thank you very much 😊

Amazing!! Any questions and PR are welcome. I need help for improvements.

JanBeelte commented 1 week ago

Amazing!! Any questions and PR are welcome. I need help for improvements.

We are working on getting i2c up and running with sane defaults on c3. As soon as we have something working we can raise a PR 😃