77 Commits

Author SHA1 Message Date
Andrew Kelley
105db13536 std.Build: implement --host-target, --host-cpu, --host-dynamic-linker
This also makes a long-overdue change of extracting common state from
Build into a shared Graph object.

Getting the semantics right for these flags turned out to be quite
tricky. In the end it works like this:
* The override only happens when the target is fully native, with no
  additional query parameters, such as versions or CPU features added.
* The override affects the resolved Target but leaves the original Query
  unmodified.
* The "is native?" detection logic operates on the original, unmodified
  query. This makes it possible to provide invalid host target
  information, causing confusing errors to occur. Don't do that.

There are some minor breaking changes to std.Build API such as the fact
that `b.zig_exe` is now moved to `b.graph.zig_exe`, as well as a handful
of other similar flags.
2024-02-02 20:43:01 -07:00
Jakub Konka
92deebcd66 cli+build: handle -ObjC flag and route it to MachO linker 2024-02-02 22:00:16 +01:00
Ian Johnson
27d2d8e81f Apply cflags from pkg-config to all dependent modules
Closes #18628

This commit splits the arguments obtained from pkg-config into two
groups, cflags and libs, and consistently applies the cflags to each
individual module linking the library while applying the libs only once
for each compilation.
2024-01-29 14:08:41 -08:00
Andrew Kelley
2dea375450 std.Build.Compile: handle modules sans root source files
Uses the new `-M[name][=src]` CLI syntax to omit the source when the
module does not have a zig root source file.

Only some kinds of link objects imply that this should happen.
2024-01-19 22:28:07 -07:00
Jakub Konka
a8b9f0cf22 std/Build/Step/Compile: do not propagate deps of complex addObject step 2024-01-14 23:07:21 +01:00
Carl Åstholm
0f9345ea9a Expose -f(no-)formatted-panics to the build system (again) 2024-01-13 22:07:34 -08:00
dhash
9bb6430318 Add support for --(no-)undefined-version
Co-authored-by: Motiejus Jakštys <motiejus@jakstys.lt>
Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
Co-authored-by: Samuel Cantero <scanterog@gmail.com>
Co-authored-by: Giorgos Georgiou <giorgos.georgiou@datadoghq.com>
Co-authored-by: Carl Åstholm <carl@astholm.se>
2024-01-09 17:24:11 +01:00
Andrew Kelley
60094cc3fc
Merge pull request #18491 from ziglang/fix-mod-link-obj-path
std.Build.Step.Compile: fix link object paths
2024-01-09 01:11:15 -08:00
Andrew Kelley
cf5a5dc8b5 std.Build.Step.Compile: fix link object paths
They were being resolved relative to the wrong owner.

closes #18460
2024-01-08 18:24:14 -07:00
Carl Åstholm
9c8c9b7cc8 Build: Use LazyPath for version scripts 2024-01-09 01:47:54 +02:00
Andrew Kelley
60958d135f std.Build.Step.Compile: fix passing wrong Builder to getPath
This resulted in an error building a transitive module dependency.
2024-01-02 18:22:11 -07:00
Andrew Kelley
eae6d45cde CLI: introduce global -I args for C include paths
This isn't technically needed since per-module -I args can suffice, but
this can produce very long CLI invocations when several --mod args are
combined with --search-prefix args since the -I args have to be repeated
for each module.

This is a partial revert of ecbe8bbf2df2ed4d473efbc32e0b6d7091fba76f.
2024-01-01 19:49:08 -07:00
Andrew Kelley
8fa4496909 std.Build.Step.Compile: fix search prefix lowering
Instead of adding -I and -L flags at the end of the CLI, they need to be
added for every module, after the module-specific flags.
2024-01-01 19:49:08 -07:00
Andrew Kelley
1ef4df9044 std.Build.Step.Compile: support modules with different args
Now that the CLI supports per-module settings, the build system takes
advantage of this when lowering a compilation to the command line.
2024-01-01 17:51:21 -07:00
Andrew Kelley
ca77c3bacc std.Build.Step.Compile: restore previous step name
Print using the target query rather than the resolved target. This makes
it print "native" when compiling for the native target.
2024-01-01 17:51:20 -07:00
Andrew Kelley
b92e30ff0b std.Build.ResolvedTarget: rename target field to result
This change is seemingly insignificant but I actually agonized over this
for three days. Some other things I considered:

* (status quo in master branch) make Compile step creation functions
  accept a Target.Query and delete the ResolvedTarget struct.
  - downside: redundantly resolve target queries many times
* same as before but additionally add a hash map to cache target query
  resolutions.
  - downside: now there is a hash map that doesn't actually need to
    exist, just to make the API more ergonomic.
* add is_native_os and is_native_abi fields to std.Target and use it
  directly as the result of resolving a target query.
  - downside: they really don't belong there. They would be available
    as comptime booleans via `@import("builtin")` but they should not
    be exposed that way.

With this change the downsides are:
* the option name of addExecutable and friends is `target` instead of
  `resolved_target` matching the type name.
  - upside: this does not break compatibility with existing build
    scripts
* you likely end up seeing `target.result.cpu.arch` rather than
  `target.cpu.arch`.
  - upside: this is an improvement over `target.target.cpu.arch` which
    it was before this commit.
  - downside: `b.host.target` is now `b.host.result`.
2024-01-01 17:51:18 -07:00
Andrew Kelley
dbdb87502d std.Target: add DynamicLinker 2024-01-01 17:51:18 -07:00
Andrew Kelley
3179f58c41 rename std.zig.CrossTarget to std.Target.Query 2024-01-01 17:51:18 -07:00
Andrew Kelley
04480f72d8 fix linker test regressions
Caused by problems with transitive dependencies
2024-01-01 17:51:18 -07:00
Andrew Kelley
142471fcc4 zig build system: change target, compilation, and module APIs
Introduce the concept of "target query" and "resolved target". A target
query is what the user specifies, with some things left to default. A
resolved target has the default things discovered and populated.
In the future, std.zig.CrossTarget will be rename to std.Target.Query.
Introduces `std.Build.resolveTargetQuery` to get from one to the other.

The concept of `main_mod_path` is gone, no longer supported. You have to
put the root source file at the module root now.

* remove deprecated API
* update build.zig for the breaking API changes in this branch
* move std.Build.Step.Compile.BuildId to std.zig.BuildId
* add more options to std.Build.ExecutableOptions, std.Build.ObjectOptions,
  std.Build.SharedLibraryOptions, std.Build.StaticLibraryOptions, and
  std.Build.TestOptions.
* remove `std.Build.constructCMacro`. There is no use for this API.
* deprecate `std.Build.Step.Compile.defineCMacro`. Instead,
  `std.Build.Module.addCMacro` is provided.
  - remove `std.Build.Step.Compile.defineCMacroRaw`.
* deprecate `std.Build.Step.Compile.linkFrameworkNeeded`
  - use `std.Build.Module.linkFramework`
* deprecate `std.Build.Step.Compile.linkFrameworkWeak`
  - use `std.Build.Module.linkFramework`
* move more logic into `std.Build.Module`
* allow `target` and `optimize` to be `null` when creating a Module.
  Along with other fields, those unspecified options will be inherited
  from parent `Module` when inserted into an import table.
* the `target` field of `addExecutable` is now required. pass `b.host`
  to get the host target.
2024-01-01 17:51:18 -07:00
Andrew Kelley
579f572cf2 zig build system: remove vcpkg integration
Instead of vcpkg, users are encouraged to use the Zig package manager to
fulfill dependencies on Windows.
2024-01-01 17:51:18 -07:00
Andrew Kelley
c20ad51c62 introduce std.Build.Module and extract some logic into it
This moves many settings from `std.Build.Step.Compile` and into
`std.Build.Module`, and then makes them transitive.

In other words, it adds support for exposing Zig modules in packages,
which are configured in various ways, such as depending on other link
objects, include paths, or even a different optimization mode.

Now, transitive dependencies will be included in the compilation, so you
can, for example, make a Zig module depend on some C source code, and
expose that Zig module in a package.

Currently, the compiler frontend autogenerates only one
`@import("builtin")` module for the entire compilation, however, a
future enhancement will be to make it honor the differences in modules,
so that modules can be compiled with different optimization modes, code
model, valgrind integration, or even target CPU feature set.

closes #14719
2024-01-01 17:51:18 -07:00
Carl Åstholm
779b8e2598 Expose -f(no-)formatted-panics to the build system 2023-12-16 04:08:05 +02:00
xdBronch
5de0636577 expose to build system 2023-11-13 14:57:51 +02:00
Luuk de Gram
c893f83715
cli: consolidate entry point flags 2023-11-03 12:48:53 +01:00
Luuk de Gram
938f9dea37
update linker tests
This updates all linker tests to include `no_entry` as well as changes
all tests to executable so they do not need to be updated later when
the in-house WebAssembly linker supports dynamic libraries.
2023-11-03 12:48:53 +01:00
Luuk de Gram
1cb7a01b25
wasm-linker: implement -fno-entry flag
This adds support for the `-fno-entry` and `-fentry` flags respectively, for
zig build-{exe/lib} and the build system. For `zig cc` we use the `--no-entry`
flag to be compatible with clang and existing tooling.

In `start.zig` we now make the main function optional when the target is
WebAssembly, as to allow for the build-exe command in combination with
`-fno-entry`.

When the execution model is set, and is set to 'reactor', we now verify
when an entry name is given it matches what is expected. When no entry
point is given, we set it to `_initialize` by default. This means the user
will also be met with an error when they use the reactor model, but did
not provide the correct function.
2023-11-03 12:48:52 +01:00
Jakub Konka
bc081901dc Step.Compile: differentiate between fuzzy and exact matches for compile errors 2023-10-25 11:40:16 +02:00
Jakub Konka
89563f07e3 build: match and ignore /?/ in expected compile errors 2023-10-24 22:08:36 +02:00
Jan Philipp Hafer
fd2239bde9 child_process + Build: rename exec to run + all related code
Justification: exec, execv etc are unix concepts and portable version
should be called differently.

Do no touch non-Zig code. Adjust error names as well, if associated.
Closes #5853.
2023-10-22 14:47:20 -04:00
Andrew Kelley
a361f37b1c
Merge pull request #17608 from squeek502/resinator-fixes
resinator: Fix `INCLUDE` var handling and sync with upstream
2023-10-20 03:49:14 -04:00
dweiller
db18b562ac std.Build: do not assume custom test runners support client-server mode 2023-10-20 02:39:17 -04:00
Ryan Liptak
8ec04b567e Error if an .rc file uses the 'preprocess only' or 'no preprocess' flags 2023-10-18 18:30:32 -07:00
Ryan Liptak
5f15acc463 Add preliminary support for Windows .manifest files
An embedded manifest file is really just XML data embedded as a RT_MANIFEST resource (ID = 24). Typically, the Windows-only 'Manifest Tool' (`mt.exe`) is used to embed manifest files, and `mt.exe` also seems to perform some transformation of the manifest data before embedding, but in testing it doesn't seem like the transformations are necessary to get the intended result.

So, to handle embedding manifest files, Zig now takes the following approach:

- Generate a .rc file with the contents `1 24 "path-to-manifest.manifest"`
- Compile that generated .rc file into a .res file
- Link the .res file into the final binary

This effectively achieves the same thing as `mt.exe` minus the validation/transformations of the XML data that it performs.

How this is used:

On the command line:
```
zig build-exe main.zig main.manifest
```
(on the command line, specifying a .manifest file when the target object format is not COFF is an error)

or in build.zig:

```
const exe = b.addExecutable(.{
    .name = "manifest-test",
    .root_source_file = .{ .path = "main.zig" },
    .target = target,
    .optimize = optimize,
    .win32_manifest = .{ .path = "main.manifest" },
});
```
(in build.zig, the manifest file is ignored if the target object format is not COFF)

Note: Currently, only one manifest file can be specified per compilation. This is because the ID of the manifest resource is currently always 1. Specifying multiple manifests could be supported if a way for the user to specify an ID for each manifest is added (manifest IDs must be a u16).

Closes #17406

options
2023-10-15 13:33:16 -07:00
radar roark
244233da2a remove redundant use of pathFromRoot 2023-10-13 07:20:45 -07:00
Krzysztof Wolicki
7abf9b3a83
Step.Compile: add options struct for addCSourceFiles (#17420)
Closes #17410
2023-10-10 14:29:26 -04:00
Andrew Kelley
d0bcc390e8 get zig fetch working with the new system
* start renaming "package" to "module" (see #14307)
  - build system gains `main_mod_path` and `main_pkg_path` is still
    there but it is deprecated.
* eliminate the object-oriented memory management style of what was
  previously `*Package`. Now it is `*Package.Module` and all pointers
  point to externally managed memory.
* fixes to get the new Fetch.zig code working. The previous commit was
  work-in-progress. There are still two commented out code paths, the
  one that leads to `Compilation.create` and the one for `zig build`
  that fetches the entire dependency tree and creates the required
  modules for the build runner.
2023-10-08 16:54:31 -07:00
Jakub Konka
8b4e3b6aee comp: add support for -fdata-sections 2023-10-04 11:21:56 -07:00
Carl Åstholm
412d863ba5 std.Build: expose -idirafter to the build system 2023-10-02 16:22:07 -07:00
LinuxUserGD
ceaae42e90 Add '--compress-debug-sections=zstd' 2023-09-26 14:18:01 -07:00
antlilja
8eff0a0a66 Support non zig dependencies
Dependencies no longer require a build.zig file.

Adds path function to Dependency struct which
returns a LazyPath into a dependency.
2023-09-24 02:47:21 +01:00
Ryan Liptak
0168ed7bf1 rc compilation: Use MSVC includes if present, fallback to mingw
The include directories used when preprocessing .rc files are now separate from the target, and by default will use the system MSVC include paths if the MSVC + Windows SDK are present, otherwise it will fall back to the MinGW includes distributed with Zig. This default behavior can be overridden by the `-rcincludes` option (possible values: any (the default), msvc, gnu, or none).

This behavior is useful because Windows resource files may `#include` files that only exist with in the MSVC include dirs (e.g. in `<MSVC install directory>/atlmfc/include` which can contain other .rc files, images, icons, cursors, etc). So, by defaulting to the `any` behavior (MSVC if present, MinGW fallback), users will by default get behavior that is most-likely-to-work.

It also should be okay that the include directories used when compiling .rc files differ from the include directories used when compiling the main binary, since the .res format is not dependent on anything ABI-related. The only relevant differences would be things like `#define` constants being different values in the MinGW headers vs the MSVC headers, but any such differences would likely be a MinGW bug.
2023-09-17 03:09:58 -07:00
Ryan Liptak
a94d830a48 addWin32ResourceFile: Ignore the resource file if the target object format is not coff 2023-09-17 03:09:45 -07:00
Ryan Liptak
2a56fe1175 Add a .rc -> .res compiler to the Zig compiler 2023-09-17 03:09:45 -07:00
Jakub Konka
71cc2e6759 build: merge FrameworkDir into IncludeDir 2023-08-18 08:08:24 +02:00
Jakub Konka
23b4a2b8e1 build: disambiguate system framework path (-iframework) from framework path (-F) 2023-08-18 08:00:40 +02:00
Jakub Konka
6a4b29a58f build: remove spurious -iframework flag; use getPath2 for framework path resolution 2023-08-18 08:00:40 +02:00
Jakub Konka
5e945f813c build: do not emit -iwithsysroot/-iframeworkwithsysroot implicitly
Prior to this change, we would unconditionally emit any system include path/framework
path as `-iwithsysroot`/`-iframeworkwithsysroot` if the sysroot was
set which can lead to unexpected build failures. Now, calls to
`b.addSystemIncludePath` and `b.addFrameworkPath` will always emit
search paths as `-isystem`/`-iframework`. As a result, it is now up to
the user to correctly concat the search paths with the sysroot when
and where desired.

If there is a need for emitting `-iwithsysroot`/`-iframeworkwithsysroot`
I would advise adding explicit hooks such as `addSystemIncludePathWithSysroot`
and `addFrameworkPathWithSysroot`.
2023-08-18 08:00:40 +02:00
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
aef8bcf776 std.Build.Step.Compile: fine-grained system lib search control
For each library you can specify the preferred mode and search strategy.

The old way of setting global state is eliminated.
2023-08-03 09:52:15 -07:00