Commit Graph

164 Commits

Author SHA1 Message Date
Zachary Raineri
0461a64a93
change uses of std.builtin.Mode to OptimizeMode (#16745)
std.builtin.Mode is deprecated.
2023-08-09 14:39:34 -04:00
Andrew Kelley
25a9487caa std.Build.LazyPath: fix resolution of cwd_relative
The callsites of getPath rely on the result being absolute so that they
can pass the path to a child process with the cwd set to the build root.
2023-07-30 18:42:08 -07:00
Andrew Kelley
38840e2e58 build system: follow-up enhancements regarding LazyPath
* introduce LazyPath.cwd_relative variant and use it for --zig-lib-dir. closes #12685
* move overrideZigLibDir and setMainPkgPath to options fields set once
  and then never mutated.
* avoid introducing Build/util.zig
* use doc comments for deprecation notices so that they show up in
  generated documentation.
* introduce InstallArtifact.Options, accept it as a parameter to
  addInstallArtifact, and move override_dest_dir into it. Instead of
  configuring the installation via Compile step, configure the
  installation via the InstallArtifact step. In retrospect this is
  obvious.
* remove calls to pushInstalledFile in InstallArtifact. See #14943
* rewrite InstallArtifact to not incorrectly observe whether a Compile
  step has any generated outputs. InstallArtifact is meant to trigger
  output generation.
* fix child process evaluation code handling of `-fno-emit-bin`.
* don't store out_h_filename, out_ll_filename, etc., pointlessly. these
  are all just simple extensions appended to the root name.
* make emit_directory optional. It's possible to have nothing outputted,
  for example, if you're just type-checking.
* avoid passing -femit-foo/-fno-emit-foo when it is the default
* rename ConfigHeader.getTemplate to getOutput
* deprecate addOptionArtifact
* update the random number seed of Options step caching.
* avoid using `inline for` pointlessly
* avoid using `override_Dest_dir` pointlessly
* avoid emitting an executable pointlessly in test cases

Removes forceBuild and forceEmit. Let's consider these additions separately.
Nearly all of the usage sites were suspicious.
2023-07-30 11:19:32 -07:00
Felix (xq) Queißner
5c01818410 Introduces Compile.getEmittedX() functions, drops Compile.emit_X. Resolves #14971 2023-07-30 11:18:50 -07:00
Felix (xq) Queißner
ce95a3b153 Build.zig rename orgy (aka: #16353). Renames FileSource to LazyPath and removes functions that take literal paths instead of LazyPath. 2023-07-30 11:18:50 -07:00
Zachary Raineri
d82b359010 Use builtin inference over @as where possible 2023-07-24 10:23:51 -07:00
Ian Johnson
bbda053f9e Build: make InstallDirStep use a FileSource
Closes #16187
2023-06-26 15:59:53 -07:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
r00ster91
6e84f46990 std: replace builtin.Version with SemanticVersion 2023-06-17 13:17:34 -07:00
Andrew Kelley
629f0d23b5
Merge pull request #15579 from squeek502/mem-delimiters
Split `std.mem.split` and `tokenize` into `sequence`, `any`, and `scalar` versions
2023-06-03 13:51:02 -07:00
Mason Remaley
5744ceedb8
Fixes WriteFile.getFileSource failure on Windows (#15730) 2023-05-24 14:26:07 -07:00
Linus Groh
0f6fa3f20b std: Move std.debug.{TTY.Config,detectTTYConfig} to std.io.tty
Also get rid of the TTY wrapper struct, which was exlusively used as a
namespace - this is done by the tty.zig root struct now.

detectTTYConfig has been renamed to just detectConfig, which is enough
given the new namespace. Additionally, a doc comment had been added.
2023-05-24 10:15:02 +01:00
Linus Groh
39c2eee285 std.debug: Rename TTY.Color enum values to snake case 2023-05-24 10:15:02 +01:00
mlugg
f65e8c7862 Deduplicate uses of the same package across dependencies 2023-05-18 00:27:21 -07:00
Andrew Kelley
728ce2d7c1 tweaks to --build-id
* build.zig: the result of b.option() can be assigned directly in many
   cases thanks to the return type being an optional
 * std.Build: make the build system aware of the
   std.Build.Step.Compile.BuildId type when used as an option.
   - remove extraneous newlines in error logs
 * simplify caching logic
 * simplify hexstring parsing tests and use a doc test
 * simplify hashing logic. don't use an optional when the `none` tag
   already provides this meaning.
 * CLI: fix incorrect linker arg parsing
2023-05-16 20:39:01 -07:00
Ryan Liptak
815e53b147 Update all std.mem.tokenize calls to their appropriate function
Everywhere that can now use `tokenizeScalar` should get a nice little performance boost.
2023-05-13 13:45:04 -07:00
Veikka Tuominen
3f3b1a6808 std.Build: use Step.* instead of *Step
Follow up to 13eb7251d3
2023-05-03 20:55:29 -07:00
Nicolas Sterchele
13eb7251d3 build: rename std.Build.*Step to std.Build.Step.*
Follow-up actions from #14647

Fixes #14947
2023-05-03 08:39:24 +03:00
dweiller
1b432072b5 std.Build: detect and disallow top-level step name clashes 2023-04-30 16:34:34 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Andrew Kelley
4a233d1871 CI: more C backend test coverage
The CI now runs C backend tests in addition to compiling them. It uses
-std=c99 -pedantic -Werror in order to catch non-conformant C code.

This necessitated disabling a test case that caused a C compile error,
in addition to disabling a handful of warnings that are already being
triggered by Zig's C backend output for the behavior tests.

The upshot is that I was able to, very cleanly, integrate the C backend
tests into the build system, so that it communicates via the test runner
protocol along with all the other behavior tests.
2023-04-15 10:33:08 -07:00
Andrew Kelley
adc9b77d5f std.Build: add some more init options to CompileStep 2023-04-15 10:33:08 -07:00
Andrew Kelley
3c3cee2cfa fix build logic due to state mutations and break the API accordingly
* remove setName, setFilter, and setTestRunner. Please set these
   options directly when creating the CompileStep.
 * removed unused field
 * remove computeOutFileNames and inline the logic, making clear the
   goal of avoiding state mutations after the build step is created.
2023-04-11 08:42:14 -07:00
Andrew Kelley
8f481dfc3c fix std.Build.OptionsStep
* use the same hash function as the rest of the steps
 * fix race condition due to a macOS oddity.
 * fix race condition due to file truncation (rename into place instead)
 * integrate with marking Step.result_cached. check if the file already
   exists with fs.access before doing anything else.
 * use a directory so that the file basename can be "options.zig"
   instead of a hash digest.
 * better error reporting in case of file system failures.
2023-03-19 00:39:29 -04:00
Motiejus Jakštys
9f2aa3fbee Build.zig_exe: make it sentinel-aware
This is useful for tests that want to `execve` zig directly. The string
is already null-terminated, so this will just expose it as such,
removing an extra allocation from the test.

Will be used in #14462
2023-03-17 15:54:09 -04:00
Jacob Young
cfcd6698cd main: add debug option to dump unoptimized llvm ir 2023-03-17 01:57:14 -04:00
Andrew Kelley
ede5dcffea make the build runner and test runner talk to each other
std.Build.addTest creates a CompileStep as before, however, this kind of
step no longer actually runs the unit tests. Instead it only compiles
it, and one must additionally create a RunStep from the CompileStep in
order to actually run the tests.

RunStep gains integration with the default test runner, which now
supports the standard --listen=- argument in order to communicate over
stdin and stdout. It also reports test statistics; how many passed,
failed, and leaked, as well as directly associating the relevant stderr
with the particular test name that failed.

This separation of CompileStep and RunStep means that
`CompileStep.Kind.test_exe` is no longer needed, and therefore has been
removed in this commit.

 * build runner: show unit test statistics in build summary
 * added Step.writeManifest since many steps want to treat it as a
   warning and emit the same message if it fails.
 * RunStep: fixed error message that prints the failed command printing
   the original argv and not the adjusted argv in case an interpreter
   was used.
 * RunStep: fixed not passing the command line arguments to the
   interpreter.
 * move src/Server.zig to std.zig.Server so that the default test runner
   can use it.
 * the simpler test runner function which is used by work-in-progress
   backends now no longer prints to stderr, which is necessary in order
   for the build runner to not print the stderr as a warning message.
2023-03-15 10:48:14 -07:00
Andrew Kelley
f829f848dd std.Build.InstallFileStep: add missing step dependencies
in the creation function, which had to change from init() to create().
2023-03-15 10:48:14 -07:00
Andrew Kelley
59f5df3af9 std.Build: use Cache hash helper for package prefix dirs
Previously this code used SipHash(1, 3) directly; now that we have the
cache system available in the build system, borrow the same
implementation as is being used everywhere else.
2023-03-15 10:48:14 -07:00
Andrew Kelley
8510f9e2bc std.Build: add addAnonymousDependency
This is for bypassing the package manager and directly depending on
another package via the build system. For this to work the anonymous
package must be found on the file system relative to the current
package's build.zig.
2023-03-15 10:48:14 -07:00
Andrew Kelley
0b8736f5ed re-enable CLI tests
CLI tests are now ported over to the new std.Build API and thus work
properly with concurrency.

 * add `std.Build.addCheckFile` for creating a
   `std.Build.CheckFileStep`.
 * add `std.Build.makeTempPath`. This function is intended to be called
   in the `configure` phase only. It returns an absolute directory path,
   which is potentially going to be a source of API breakage in the
   future, so keep that in mind when using this function.
 * add `std.Build.CheckFileStep.setName`.
 * `std.Build.CheckFileStep`: better error message when reading the
   input file fails.
 * `std.Build.RunStep`: add a `has_side_effects` flag for when you need
   to override the autodetection.
 * `std.Build.RunStep`: add the ability to obtain a FileSource for the
   directory that contains the written files.
 * `std.Build.WriteFileStep`: add a way to write bytes to an arbitrary
   path - absolute or relative to the package root. Be careful with this
   because it updates source files. This should not be used as part of
   the normal build process, but as a utility occasionally run by a
   developer with intent to modify source files and then commit those
   changes to version control. A file added this way is not available
   with `getFileSource`.
2023-03-15 10:48:13 -07:00
Andrew Kelley
f51413d2cf zig build: add an OOM-prevention system
The problem is that one may execute too many subprocesses concurrently
that, together, exceed an RSS value that causes the OOM killer to kill
something problematic such as the window manager. Or worse, nothing, and
the system freezes.

This is a real world problem. For example when building LLVM a simple
`ninja install` will bring your system to its knees if you don't know
that you should add `-DLLVM_PARALLEL_LINK_JOBS=1`.

In particular: compiling the zig std lib tests takes about 2G each,
which at 16x at once (8 cores + hyperthreading) is using all 32GB of my
RAM, causing the OOM killer to kill my window manager

The idea here is that you can annotate steps that might use a high
amount of system resources with an upper bound. So for example I could
mark the std lib tests as having an upper bound peak RSS of 3 GiB.

Then the build system will do 2 things:

1. ulimit the child process, so that it will fail if it would exceed
   that memory limit.
2. Notice how much system RAM is available and avoid running too many
   concurrent jobs at once that would total more than that.

This implements (1) not with an operating system enforced limit, but by
checking the maxrss after a child process exits.

However it does implement (2) correctly.

The available memory used by the build system defaults to the total
system memory, regardless of whether it is used by other processes at
the time of spawning the build runner. This value can be overridden with
the new --maxrss flag to `zig build`. This mechanism will ensure that
the sum total of upper bound RSS memory of concurrent tasks will not
exceed this value.

This system makes it so that project maintainers can annotate
problematic subprocesses, avoiding bug reports from users, who can
blissfully execute `zig build` without worrying about the project's
internals.

Nobody's computer crashes, and the build system uses as much parallelism
as possible without risking OOM. Users do not need to unnecessarily
resort to -j1 when the build system can figure this out for them.
2023-03-15 10:48:13 -07:00
Andrew Kelley
a4c35a6245 std.Build: audit use of updateFile
* remove std.Build.updateFile. I noticed some people use it from
   build.zig (declare phase) when it is intended only for use in the
   make phase.
   - This also was incorrectly reporting errors with std.log.
 * std.Build.InstallArtifactStep
   - report better errors on failure
   - report whether the step was cached or not
 * std.Build.InstallDirStep: report better error on failure
 * std.Build.InstallFileStep: report better error on failure
2023-03-15 10:48:13 -07:00
Andrew Kelley
1e63573d35 std.build.CompileStep: eliminate std.log usage 2023-03-15 10:48:13 -07:00
Andrew Kelley
58edefc6d1 zig build: many enhancements related to parallel building
Rework std.Build.Step to have an `owner: *Build` field. This
simplified the implementation of installation steps, as well as provided
some much-needed common API for the new parallelized build system.

--verbose is now defined very concretely: it prints to stderr just
before spawning a child process.

Child process execution is updated to conform to the new
parallel-friendly make() function semantics.

DRY up the failWithCacheError handling code. It now integrates properly
with the step graph instead of incorrectly dumping to stderr and calling
process exit.

In the main CLI, fix `zig fmt` crash when there are no errors and stdin
is used.

Deleted steps:
 * EmulatableRunStep - this entire thing can be removed in favor of a
   flag added to std.Build.RunStep called `skip_foreign_checks`.
 * LogStep - this doesn't really fit with a multi-threaded build runner
   and is effectively superseded by the new build summary output.

build runner:
 * add -fsummary and -fno-summary to override the default behavior,
   which is to print a summary if any of the build steps fail.
 * print the dep prefix when emitting error messages for steps.

std.Build.FmtStep:
 * This step now supports exclude paths as well as a check flag.
 * The check flag decides between two modes, modify mode, and check
   mode. These can be used to update source files in place, or to fail
   the build, respectively.

Zig's own build.zig:
 * The `test-fmt` step will do all the `zig fmt` checking that we expect
   to be done. Since the `test` step depends on this one, we can simply
   remove the explicit call to `zig fmt` in the CI.
 * The new `fmt` step will actually perform `zig fmt` and update source
   files in place.

std.Build.RunStep:
 * expose max_stdio_size is a field (previously an unchangeable
   hard-coded value).
 * rework the API. Instead of configuring each stream independently,
   there is a `stdio` field where you can choose between
   `infer_from_args`, `inherit`, or `check`. These determine whether the
   RunStep is considered to have side-effects or not. The previous
   field, `condition` is gone.
 * when stdio mode is set to `check` there is a slice of any number of
   checks to make, which include things like exit code, stderr matching,
   or stdout matching.
 * remove the ill-defined `print` field.
 * when adding an output arg, it takes the opportunity to give itself a
   better name.
 * The flag `skip_foreign_checks` is added. If this is true, a RunStep
   which is configured to check the output of the executed binary will
   not fail the build if the binary cannot be executed due to being for
   a foreign binary to the host system which is running the build graph.
   Command-line arguments such as -fqemu and -fwasmtime may affect
   whether a binary is detected as foreign, as well as system
   configuration such as Rosetta (macOS) and binfmt_misc (Linux).
   - This makes EmulatableRunStep no longer needed.
 * Fix the child process handling to properly integrate with the new
   bulid API and to avoid deadlocks in stdout/stderr streams by polling
   if necessary.

std.Build.RemoveDirStep now uses the open build_root directory handle
instead of an absolute path.
2023-03-15 10:48:13 -07:00
Andrew Kelley
e895d58214 build system: give RunStep a better default step name 2023-03-15 10:48:13 -07:00
Andrew Kelley
8b2d872020 fix std.Build.TranslateCStep 2023-03-15 10:48:13 -07:00
Andrew Kelley
0e078790fe multiplex compiler progress messages into the build runner 2023-03-15 10:48:13 -07:00
Andrew Kelley
79440d2b47 std.Build.CompileStep: obtain the build output dir from protocol
Now building successfully works again.
2023-03-15 10:48:13 -07:00
Andrew Kelley
986a30e373 integrate the build runner and the compiler server
The compiler now provides a server protocol for an interactive session
with another process. The build runner uses this protocol to communicate
compilation errors semantically from zig compiler subprocesses to the
build runner.

The protocol is exposed via stdin/stdout, or on a network socket,
depending on whether the CLI flag `--listen=-` or e.g.
`--listen=127.0.0.1:1337` is used.

Additionally:

 * add the zig version string to the build runner cache prefix

 * remove --prominent-compile-errors CLI flag because it no longer does
   anything. Compilation errors are now unconditionally displayed at the
   bottom of the build summary output when using the terminal-based
   build runner.

 * Remove the color field from std.Build. The build steps are no longer
   supposed to interact with stderr directly. Instead they communicate
   semantically back to the build runner, which has its own logic about
   TTY configuration.

 * Use the cleanExit() pattern in the build runner.

 * Build steps can now use error.MakeFailed when they have already
   properly reported an error, or they can fail with any other error
   code in which case the build runner will create a simple message
   based on this error code.
2023-03-15 10:48:13 -07:00
Andrew Kelley
c5edd8b7f8 std.Build: better handling of stderr of child processes
With this commit, the build runner now communicates progress towards
completion of the step graph to the terminal, while also printing the
stderr of child processes as soon as possible, without clobbering each
other, and without clobbering the CLI progress output.
2023-03-15 10:48:12 -07:00
Andrew Kelley
8d38472293 std.Build: further enhance debug message for bad getPath()
Now it also shows the step stack trace of the step whose make function
is being run.
2023-03-15 10:48:12 -07:00
Andrew Kelley
02381c0372 std.Build: improve debugging of misconfigured steps
* Step.init() now takes an options struct
 * Step.init() now captures a small stack trace and stores it in the
   Step so that it can be accessed when printing user-friendly debugging
   information, including the lines of code that created the step in
   question.
2023-03-15 10:48:12 -07:00
Andrew Kelley
9580fbcf35 build system: capture stderr and report it later
Instead of dumping directly to stderr. This prevents processes running
simultaneously from racing their stderr against each other.

For now it only reports at the end, but an improvement would be to
report as soon as a failed step occurs.
2023-03-15 10:48:12 -07:00
Andrew Kelley
c6a895f667 extract some logic from std.Build to build_runner.zig 2023-03-15 10:48:12 -07:00
Andrew Kelley
653814f76b std.Build.addModule: return the created module 2023-03-04 05:39:14 -05:00
Andrew Kelley
26196be344 rename std.Build.InstallRawStep to ObjCopyStep
And make it not do any installation, only objcopying. We already have
install steps for doing installation.

This commit also makes ObjCopyStep properly integrate with caching.
2023-02-24 23:48:03 -05:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Andrew Kelley
a9e1cf3049
Merge pull request #14571 from ziglang/more-build-zig
std.Build.ConfigHeaderStep: support sentinel-terminated strings
2023-02-13 17:23:19 -05:00
AdamGoertz
c8dc00086e
Add -ferror-tracing and -fno-error-tracing compile options 2023-02-13 16:23:13 +02:00
Andrew Kelley
5c1f7288d9 std.Build: delete test that doesn't test anything 2023-02-13 06:42:26 -07:00
Andrew Kelley
3f8f63b132 std.Build: make cache_root and global_cache_root relative to cwd
This makes it so that when there is a tree of std.Build objects, only
one zig-cache is used (the top-level application) instead of polluting
package directories with zig-cache folders.
2023-02-13 06:42:26 -07:00
Andrew Kelley
d97042ad2e std.Build: start using the cache system with RunStep
* Use std.Build.Cache.Directory instead of a string for storing the
   cache roots and build roots.
 * Set up a std.Build.Cache in build_runner.zig and use it in
   std.Build.RunStep for avoiding redundant work.
2023-02-13 06:42:26 -07:00
Andrew Kelley
9cb52ca6ce move the cache system from compiler to std lib 2023-02-13 06:42:25 -07:00
Andrew Kelley
dad6039092 std.Build: support running build artifacts from packages
Deprecate CompileStep.run. The problem with this function is that it
does the RunStep with the same build.zig context as the CompileStep, but
this is not desirable when running an executable that is provided by a
dependency package. Instead, users should use `b.addRunArtifact`.
This has the additional benefit of conforming to the existing naming
conventions.

Additionally, support enum literals in config header options values.
2023-02-13 06:42:25 -07:00
Asherah Connor
c6344866f9 std.Build.addAssembly: add missing .kind 2023-02-11 14:24:13 +02:00
Andrew Kelley
b29e3fa2cd std.Build: enhancements to ConfigHeaderStep
Breaking API change to std.Build.addConfigHeader. It now uses an options
struct.

Introduce std.Build.CompileStep.installConfigHeader which also accepts
an options struct. This is used to add a generated config file into the
set of installed header files for a particular compilation artifact.

std.Build.ConfigHeaderStep now additionally supports a "blank" style
where a header is generated from scratch. It no longer exposes
`output_dir`. Instead it exposes a FileSource via `output_file`.
It now additionally accepts an `include_path` option which affects the
include path of CompileStep when using the `#include` directive, as well
as affecting the default installation subdirectory for header
installation purposes.

The hash used for the directory to store the generated config file now
includes the contents of the generated file. This fixes possible race
conditions when generating multiple header files simultaneously.

The values hash table is now an array hash map, to preserve order for
the "blank" use case.

I also took the opportunity to remove output_dir from TranslateCStep and
WriteFileStep. This is technically a breaking change, but it was always
naughty to access these fields.
2023-02-05 06:26:30 -07:00
Andrew Kelley
693b12f8e1 std.Build: support exposing and depending on zig modules
New API introduced: std.Build.addModule

This function exposes a zig module with the given name, which can be
used by packages that depend on this one via std.Build.Dependency.module.

std.Build.Pkg and related functionality is deleted. Every use case has a
straightforward upgrade path using the new Module struct.

std.Build.OptionsStep.getPackage is replaced by
std.Build.OptionsStep.createModule.

std.Build.CompileStep.addPackagePath is replaced by
std.Build.CompileStep.addAnonymousModule.

This partially addresses #14307 by renaming some of the instances of
"package" to "module".

Closes #14278
2023-02-04 01:55:39 -05:00
Andrew Kelley
81c27c74bc use build.zig.zon instead of build.zig.ini for the manifest file
* improve error message when build manifest file is missing
 * update std.zig.Ast to support ZON
 * Compilation.AllErrors.Message: make the notes field a const slice
 * move build manifest parsing logic into src/Manifest.zig and add more
   checks, and make the checks integrate into the standard error
   reporting code so that reported errors look sexy

closes #14290
2023-02-03 00:06:11 -07:00
Andrew Kelley
90e48d4b34 std.Build: avoid use of catch unreachable
Usage of `catch unreachable` in build scripts is completely harmless
because build scripts are always run in Debug mode, however, it sets a
poor example for beginners to learn from.
2023-01-31 15:09:35 -07:00
Andrew Kelley
13a9616540 std.Build: add deprecated declarations
These declarations are now aliases of their new APIs and marked as
deprecated via doc comments:
 * std.build.Builder
 * std.build
 * std.Build.LibExeObjStep
2023-01-31 15:09:35 -07:00
Andrew Kelley
16cdd1297e rename std.Build.LibExeObjStep to std.Build.CompileStep
This matches the nomenclature internally: a Compilation is the main type
that represents a single invokation of the compiler.
2023-01-31 15:09:35 -07:00
Andrew Kelley
5129fae4e8 std.Build: accept host Target in create()
And only detect native target in LibExeObjStep once, in create().
2023-01-31 15:09:35 -07:00
Andrew Kelley
36e2d992dd combine std.build and std.build.Builder into std.Build
I've been wanting to do this for along time.
2023-01-31 15:09:35 -07:00