Skip to content

Commit 893ce57

Browse files
committed
docs: add some documentation on Windows SDK search
Add some documentation on the flags and the process by which clang identifies the headers and libraries for the Windows environment. It should identify the flags and their interactions as well as the order in which the various sources of information are consulted. Differential Revision: https://reviews.llvm.org/D146165 Reviewed By: hans, mstorjo
1 parent acf6a32 commit 893ce57

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

clang/docs/UsersManual.rst

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,3 +4487,126 @@ If the user is using the static CRT (``/MT``), then different runtimes are used
44874487
to produce DLLs and EXEs. To link a DLL, pass
44884488
``clang_rt.asan_dll_thunk-x86_64.lib``. To link an EXE, pass
44894489
``-wholearchive:clang_rt.asan-x86_64.lib``.
4490+
4491+
Windows System Headers and Library Lookup
4492+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4493+
4494+
clang-cl uses a set of different approaches to locate the right system libraries
4495+
to link against when building code. The Windows environment uses libraries from
4496+
three distinct sources:
4497+
4498+
1. Windows SDK
4499+
2. UCRT (Universal C Runtime)
4500+
3. Visual C++ Tools (VCRuntime)
4501+
4502+
The Windows SDK provides the import libraries and headers required to build
4503+
programs against the Windows system packages. Underlying the Windows SDK is the
4504+
UCRT, the universal C runtime.
4505+
4506+
This difference is best illustrated by the various headers that one would find
4507+
in the different categories. The WinSDK would contain headers such as
4508+
`WinSock2.h` which is part of the Windows API surface, providing the Windows
4509+
socketing interfaces for networking. UCRT provides the C library headers,
4510+
including e.g. `stdio.h`. Finally, the Visual C++ tools provides the underlying
4511+
Visual C++ Runtime headers such as `stdint.h` or `crtdefs.h`.
4512+
4513+
There are various controls that allow the user control over where clang-cl will
4514+
locate these headers. The default behaviour for the Windows SDK and UCRT is as
4515+
follows:
4516+
4517+
1. Consult the command line.
4518+
4519+
Anything the user specifies is always given precedence. The following
4520+
extensions are part of the clang-cl toolset:
4521+
4522+
- `/winsysroot:`
4523+
4524+
The `/winsysroot:` is used as an equivalent to `-sysroot` on Unix
4525+
environments. It allows the control of an alternate location to be treated
4526+
as a system root. When specified, it will be used as the root where the
4527+
`Windows Kits` is located.
4528+
4529+
- `/winsdkversion:`
4530+
- `/winsdkdir:`
4531+
4532+
If `/winsysroot:` is not specified, the `/winsdkdir:` argument is consulted
4533+
as a location to identify where the Windows SDK is located. Contrary to
4534+
`/winsysroot:`, `/winsdkdir:` is expected to be the complete path rather
4535+
than a root to locate `Windows Kits`.
4536+
4537+
The `/winsdkversion:` flag allows the user to specify a version identifier
4538+
for the SDK to prefer. When this is specified, no additional validation is
4539+
performed and this version is preferred. If the version is not specified,
4540+
the highest detected version number will be used.
4541+
4542+
2. Consult the environment.
4543+
4544+
TODO: This is not yet implemented.
4545+
4546+
This will consult the environment variables:
4547+
4548+
- `WindowsSdkDir`
4549+
- `UCRTVersion`
4550+
4551+
3. Fallback to the registry.
4552+
4553+
If no arguments are used to indicate where the SDK is present, and the
4554+
compiler is running on Windows, the registry is consulted to locate the
4555+
installation.
4556+
4557+
The Visual C++ Toolset has a slightly more elaborate mechanism for detection.
4558+
4559+
1. Consult the command line.
4560+
4561+
- `/winsysroot:`
4562+
4563+
The `/winsysroot:` is used as an equivalent to `-sysroot` on Unix
4564+
environments. It allows the control of an alternate location to be treated
4565+
as a system root. When specified, it will be used as the root where the
4566+
`VC` directory is located.
4567+
4568+
- `/vctoolsdir:`
4569+
- `/vctoolsversion:`
4570+
4571+
If `/winsysroot:` is not specified, the `/vctoolsdir:` argument is consulted
4572+
as a location to identify where the Visual C++ Tools are located. If
4573+
`/vctoolsversion:` is specified, that version is preferred, otherwise, the
4574+
highest version detected is used.
4575+
4576+
2. Consult the environment.
4577+
4578+
- `/external:[VARIABLE]`
4579+
4580+
This specifies a user identified environment variable which is treated as
4581+
a path delimiter (`;`) separated list of paths to map into `-imsvc`
4582+
arguments which are treated as `-isystem`.
4583+
4584+
- `INCLUDE` and `EXTERNAL_INCLUDE`
4585+
4586+
The path delimiter (`;`) separated list of paths will be mapped to
4587+
`-imsvc` arguments which are treated as `-isystem`.
4588+
4589+
- `LIB` (indirectly)
4590+
4591+
The linker `link.exe` or `lld-link.exe` will honour the environment
4592+
variable `LIB` which is a path delimiter (`;`) set of paths to consult for
4593+
the import libraries to use when linking the final target.
4594+
4595+
The following environment variables will be consulted and used to form paths
4596+
to validate and load content from as appropriate:
4597+
4598+
- `VCToolsInstallDir`
4599+
- `VCINSTALLDIR`
4600+
- `Path`
4601+
4602+
3. Consult `ISetupConfiguration` [Windows Only]
4603+
4604+
Assuming that the toolchain is built with `USE_MSVC_SETUP_API` defined and
4605+
is running on Windows, the Visual Studio COM interface `ISetupConfiguration`
4606+
will be used to locate the installation of the MSVC toolset.
4607+
4608+
4. Fallback to the registry [DEPRECATED]
4609+
4610+
The registry information is used to help locate the installation as a final
4611+
fallback. This is only possible for pre-VS2017 installations and is
4612+
considered deprecated.

0 commit comments

Comments
 (0)