# Compilation: always import ubsan if a ZCU exists · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/2d4574aafbf95f357ad6306b98d9c8ebebbafe2e)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 2d4574aafbf95f357ad6306b98d9c8ebebbafe2e

Requested commit: 2d4574aafbf95f357ad6306b98d9c8ebebbafe2e

Commit: 2d4574aafbf95f357ad6306b98d9c8ebebbafe2e

Tree: cbe4a1fb8e16545bfe49e542f8d8d1f02de9aa36

Author: David Rubin

Committer: David Rubin

## Message

```
Compilation: always import ubsan if a ZCU exists

Unlike `compiler-rt`, `ubsan` uses the standard library quite a lot.
Using a similar approach to how `compiler-rt` is handled today, where it's
compiled into its own object and then linked would be sub-optimal as we'd
be introducing a lot of code bloat.

This approach always "imports" `ubsan` if the ZCU, if it exists. If it doesn't
such as the case where we're compiling only C code, then we have no choice other
than to compile it down to an object and link. There's still a tiny optimization
we can do in that case, which is when compiling to a static library, there's no
need to construct an archive with a single object. We'd only go back and parse out
ubsan from the archive later in the pipeline. So we compile it to an object instead
and link that to the static library.

TLDR;
- `zig build-exe foo.c` -> build `libubsan.a` and links
- `zig build-obj foo.c` -> doesn't build anything, just emits references to ubsan runtime
- `zig build-lib foo.c -static` -> build `ubsan.o` and link it
- `zig build-exe foo.zig bar.c` -> import `ubsan-rt` into the ZCU
- `zig build-obj foo.zig bar.c` -> import `ubsan-rt` into the ZCU
- `zig build-lib foo.zig bar.c` -> import `ubsan-rt` into the ZCU

```

## Parents

- [a468929519157cf7a2070a0368357cb73ad80b61](https://git.cafe/gitcafe/zig/commit/a468929519157cf7a2070a0368357cb73ad80b61?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/2d4574aafbf95f357ad6306b98d9c8ebebbafe2e?format=markdown)
