Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig build system: cache build artifacts and detect cache invalidation #330

Closed
5 tasks
andrewrk opened this issue Apr 18, 2017 · 4 comments
Closed
5 tasks
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Milestone

Comments

@andrewrk
Copy link
Member

migrated from #204

  • in zig itself, cache objects by creating a sha256 hash of all the inputs that goes into it, such as the list of source files used with their mtimes, the compile variable values set, the modification time of the zig binary itself as well as the recursive modification time of the dynamic libraries the zig binary depends on. Verify that builtin.o, compiler_rt.o, test_runner.o, and all objects made with zig only have to be created when zig is recompiled or when their sources are changed.
    • @embedFile should add the target to the hash
    • c_import should detect when a file is looked at by libclang and add that to the hash
    • various command line arguments may need to go into the hash
  • in zig itself, cache executables built with a similar strategy. Additionally hash the list of dynamically linked libraries
  • in zig itself, keep cached objects in ~/.zig/cache/. Let's have a file ~/.zig/config which has an option where you can set the maximum size of the LRU cache, with some nice default like 100.
  • cache build.zig -> ./build executable using this strategy
  • for compiling c objects, use the feature of c compilers that computes dependencies to get a list of files that are dependencies, and then avoid unnecessarily recompiling C objects. Also put into the hash environment variables, other build options, as well as the modification date of the detected compiler, and its recursive dynamic libraries. You should be able to modify the build.zig file and then, provided that the same options were chosen for compiling a given C object, the step does not needlessly be repeated.
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Apr 18, 2017
@andrewrk andrewrk added this to the 0.2.0 milestone Apr 18, 2017
@dumblob
Copy link

dumblob commented Apr 18, 2017

Just a note to the ~/.zig/ directory. I would strongly recommend following XDG [1] [2] variables and only if they're not set create and use this directory. On my system it looks e.g. like this:

$ env | grep XDG
XDG_PUBLICSHARE_DIR=/home/my_user/
XDG_CONFIG_HOME=/home/my_user/.config/
XDG_DOCUMENTS_DIR=/home/my_user/doc/
XDG_VTNR=1
XDG_DOWNLOAD_DIR=/home/my_user/
XDG_SESSION_ID=c1
XDG_VIDEOS_DIR=/home/my_user/video/
XDG_TEMPLATES_DIR=/home/my_user/
XDG_MUSIC_DIR=/home/my_user/audio/
XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/home/my_user/.local/share/
XDG_PICTURES_DIR=/home/my_user/pics/
XDG_SEAT=seat0
XDG_CACHE_HOME=/home/my_user/.cache/
XDG_RUNTIME_DIR=/run/user/1000
XDG_DATA_HOME=/home/my_user/.local/share/
XDG_CONFIG_DIRS=/etc/xdg/
XDG_DESKTOP_DIR=/home/my_user/

@andrewrk
Copy link
Member Author

Good call, using the XDG directories.

@andrewrk
Copy link
Member Author

andrewrk commented May 2, 2017

See the updated cache explanation here: #298 (comment)

This was referenced Sep 6, 2017
@andrewrk andrewrk modified the milestones: 0.2.0, 0.3.0 Feb 28, 2018
@andrewrk andrewrk modified the milestones: 0.3.0, 0.4.0 Aug 10, 2018
@andrewrk andrewrk modified the milestones: 0.4.0, 0.5.0 Feb 19, 2019
andrewrk added a commit that referenced this issue Mar 9, 2019
 * in Zig build scripts, getOutputPath() is no longer a valid function
   to call, unless setOutputDir() was used, or within a custom make()
   function. Instead there is more convenient API to use which takes
   advantage of the caching system. Search this commit diff for
   `exe.run()` for an example.
 * Zig build by default enables caching. All build artifacts will go
   into zig-cache. If you want to access build artifacts in a convenient
   location, it is recommended to add an `install` step. Otherwise
   you can use the `run()` API mentioned above to execute programs
   directly from their location in the cache. Closes #330.
   `addSystemCommand` is available for programs not built with Zig
   build.
 * Please note that Zig does no cache evicting yet. You may have to
   manually delete zig-cache directories periodically to keep disk
   usage down. It's planned for this to be a simple Least Recently
   Used eviction system eventually.
 * `--output`, `--output-lib`, and `--output-h` are removed. Instead,
   use `--output-dir` which defaults to the current working directory.
   Or take advantage of `--cache on`, which will print the main output
   path to stdout, and the other artifacts will be in the same directory
   with predictable file names. `--disable-gen-h` is available when
   one wants to prevent .h file generation.
 * `@cImport` is always independently cached now. Closes #2015.
   It always writes the generated Zig code to disk which makes debug
   info and compile errors better. No more "TODO: remember C source
   location to display here"
 * Fix .d file parsing. (Fixes the MacOS CI failure)
 * Zig no longer creates "temporary files" other than inside a
   zig-cache directory.

This breaks the CLI API that Godbolt uses. The suggested new invocation
can be found in this commit diff, in the changes to `test/cli.zig`.
@andrewrk andrewrk modified the milestones: 0.5.0, 0.4.0 Apr 8, 2019
@Earnestly
Copy link

Earnestly commented Aug 8, 2019

Just to add a quick note here, zig doesn't quite follow XDG Base Directory Specification (XBDS) and the commenter who dumped a load of non-XBDS environments probably didn't realise it.

Instead of checking the XDG_DATA_HOME environment, zig hardcodes HOME/.local/share. This is the required fall back if XDG_DATA_HOME is not set (and not an absolute path).

Lots of information about the XBDS here: https://wiki.archlinux.org/index.php/XDG_Base_Directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase.
Projects
None yet
Development

No branches or pull requests

3 participants