#How to write a file into a lazypath folder

1 messages · Page 1 of 1 (latest)

timid sleet
#

One of my buildsteps creates a folder.
How can I write a file into it?
I know about B.addWriteFile but I cannot get it to work, not matter what I try

What would be a simplistic/minimalistic example of outputting a file to a folder created by the buildsystem?

hybrid lake
#

I don't know what your target is, but b.addWriteFile creates a folder in a .zig-cache subdirectory. Is this your target? For what do you need this folder?

timid sleet
#

I am literally stuck in the process of creating the file to begin with

#

Let alone its usage, I just cannot get it to be made

hybrid lake
#

Should the file be in a .zig-cache subdirectory or not?

timid sleet
#
  • I have some bytes inside a const
  • I want those bytes written to a file inside a LazyPath
  • #stuck, doesn't write because I don't understand the syntax
hybrid lake
#

build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const create_step = b.step("create", "Create a file");
    const create_file = b.addWriteFile("foo", "bar");
    create_step.dependOn(&create_file.step);
}

shell:

$ rm -r .zig-cache
$ find
.
./build.zig
./build.zig.zon
$ zig build create
$ find            
.
./build.zig
./build.zig.zon
./.zig-cache
./.zig-cache/tmp
./.zig-cache/o
./.zig-cache/o/4b0d52780f92421c4b769ed9fc2e0f3c
./.zig-cache/o/4b0d52780f92421c4b769ed9fc2e0f3c/dependencies.zig
./.zig-cache/o/e82da1b58fb770e07829ebe8bcd8d74d
./.zig-cache/o/e82da1b58fb770e07829ebe8bcd8d74d/build.o
./.zig-cache/o/e82da1b58fb770e07829ebe8bcd8d74d/build
./.zig-cache/o/8714c3b07cdb6eeb21c7ca5acda584a3
./.zig-cache/o/8714c3b07cdb6eeb21c7ca5acda584a3/foo
./.zig-cache/h
./.zig-cache/h/20bae35774714c1ccaa12c26f8003e89.txt
./.zig-cache/h/timestamp
./.zig-cache/h/3c7ad56d47468b04ca42ee6c19994cc0.txt
./.zig-cache/z
./.zig-cache/z/7828b67ad03a02f11d2bd164c5ee1d12
$ cat ./.zig-cache/o/8714c3b07cdb6eeb21c7ca5acda584a3/foo
bar%
timid sleet
#
  const patchWrite = B.addWriteFile(""++"zigcc.patch", internal.getPatch(B, "zigcc"));
  const patchFile  = patchWrite.addCopyFile(dir, "zigcc.patch");
  B.getInstallStep().dependOn(&patchWrite.step);
```Mine is definitely wrong, but don't really understand why
leaden surge
#

the source should be a file not a directory
thats probably the problem

#

you should use addCopyDirectory if its a directory

timid sleet
#

its not a directory

#

I want that file into the directory

#

.nim is the target LazyPath directory

#

it should result in whateverjumbledmess/.nim/zigcc.patch

hybrid lake
#

addWriteFile creates a new .zig-cache subdirectory. afaik the best solution is to use b.addWriteFiles() (note the plural of files) and copy everything from the original place to there

leaden surge
#

you cant add stuff to a cached directory
the path is the hash of its contents for a reason

#

if you do, it will inevitably break stuff

#

you could create an artifact that does the git clone, applies the diff, and writes the file
all in one exeutable

timid sleet
#

ok, can you propose an alternative to this simple bash script?

git clone URL folder
cp file.patch folder/file.patch
cd folder
git apply file.patch
./build.sh
```Because that's all I want to do
#

at this point I'm even considering writing the thing in bash, tbh

leaden surge
#

you could keep it a bash script sure, although ofc you could just write it with zig
but if you dont mind relying on a shell and coreutils its whatever

#

basically

cold ore
#

for one-time tasks that you are expected to run once after checking out the repo, you're probably better of using a shell script

timid sleet
#

well that's what I don't want to do. but Its been hours and 5sloc of bash are still stuck in the very first line only working

#

so it was a statement of frustation, not really of intent. I don't want to do it in bash

leaden surge
#

youd want to use addSystemCommand(&.{“/bin/sh”, “script.sh”}
but then script.sh should take an output directory at least so that the build system can cache it well

timid sleet
#

but then its not cross-platform, and back to square1

#

because if I'm even using build.zig is to make it cross-platform to begin with

leaden surge
#

you could just write it in zig, copying files is easy, calling system commands is easy
instead of using a shell script

#

and then its cross platform
still relies on a git system command being available but its whatever

timid sleet
#

git+zig is all I'm willing to depend on, yeah

timid sleet
#

I really don't understand how people don't understand the frustration on the buildsystem tools, when 5sloc of bash becomes this hell

#

imagine a new user facing that... like... facepalm

#

But that's a topic for another day I guess

leaden surge
#

I assume youre fine rewriting the shell script in zig so ill just show you what youd do after that:

const git_run_step = b.addRunArtifact(git_rool_thing);
git_run_step.addOutputDirectoryPath(“foo”);

thats all

leaden surge
cold ore
#

you're generally not expected to patch the source code you're building from using the zig build system

leaden surge
#

^

#

and youre generally not expected to use git directly instead of using the built inpackage manager

#

99.9% of people will just use the package manager to fetch the repo and not change it or vendor it and change it directly