@imp/rules/c/toolchain

Module: @imp/rules/c/toolchain.

cc_toolchain_for_platform(by_os)

Wrap one provider for each OS into one union. Give the union to a rule toolchain option in place of a provider. The same target then uses gcc on Linux and MSVC on Windows, for example.

Each by_os value is a provider or a function without arguments that returns a provider. A function declares the provider only when the union selects it.

select(target_platform?) selects by target_platform.os only. The default is the host platform. The parameter keeps the target platform apart from the execution platform for a later cross-compilation feature. Each provider now resolves for the host only.

Source: rules/c/toolchain.luau:136.

clang_opt_flags(opt)

The optimization flags of opt for the gcc and clang flag vocabulary. gcc and zig share them. MSVC uses /O2 /DNDEBUG and /Od /Zi.

Source: rules/c/toolchain.luau:47.

import_lib_args(out_path, is_shared)

The -Wl,--out-implib argument that makes a MinGW-style link step write the import_lib_path file, or no argument. gcc and zig share it. MSVC writes the same file without a flag.

Source: rules/c/toolchain.luau:107.

import_lib_path(out_path, is_shared)

The import library that the link step of a Windows shared library writes beside the DLL, or nil. MSVC link.exe cannot link against a DLL. It needs the import library, <stem>.lib, the name that cl.exe /LD gives it. A Windows shared-library output path must end in lowercase .dll.

Source: rules/c/toolchain.luau:93.

is_cc_toolchain_union(toolchain)

True when toolchain is a cc_toolchain_for_platform union.

Source: rules/c/toolchain.luau:156.

rpath_origin_args(bundled)

The -Wl,-rpath,$ORIGIN argument of the link step of a bundled binary, or no argument. gcc and zig share it.

The loader expands $ORIGIN to the directory of the executable, where the binary rule copies the transitive shared libraries. The quotes are necessary: the link tokens go into an sh -c script, which would expand $ORIGIN to an empty string. Windows finds a DLL beside the executable without an rpath.

Source: rules/c/toolchain.luau:81.

select_cc_toolchain(toolchain, target_platform)

Resolve toolchain to a provider. A union selects with target_platform. A provider or nil is returned unchanged.

Source: rules/c/toolchain.luau:162.

shell_quote(value)

Quote value as one token of an sh -c script.

Each provider runs its compile, archive, and link commands through sh -c, also when the native compiler is cl.exe. Thus this quoting is the same for each toolchain. Only the content of a response file needs a quoting function for each toolchain, because the native tool parses it. See rsp_quote of each provider.

Source: rules/c/toolchain.luau:41.

soname_args(out_path, is_shared)

The -Wl,-soname argument of the link step of a shared library, or no argument for a static library or on Windows. gcc and zig share it.

Without it, ld copies the link-line path of the library into the DT_NEEDED entry of a consumer, for example build/c/<name>.so. The loader reads a DT_NEEDED value with a slash as a path relative to the working directory, and does not use its search paths. The bare filename keeps DT_NEEDED a name that a search path or an rpath can resolve.

Source: rules/c/toolchain.luau:66.