> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Build on Linux for Windows

> Guide for cross-compiling ClickHouse from Linux for Windows systems

This is for the case when you have a Linux machine and want to use it to build ClickHouse
binaries that run on Windows. There is no native Windows build - the Windows binaries are
always cross-compiled, as they are for macOS and FreeBSD.

:::note
The Windows port is **not finished**. CI links `clickhouse.exe` on every pull request to
`master` (not on backport pull requests to release branches) and then executes it under Wine
(see [CI](#ci)) - `clickhouse-local` over a trivial query, which is as far as the port has
been run. Nothing has been run on a real Windows machine, and no test suite
runs against it. [Remaining work](#remaining-work) is an inventory of what is left, by
subsystem.
:::

The cross-build for Windows is based on the [Build instructions](https://clickhouse.com/docs/resources/develop-contribute/build/build.md),
follow them first.

## How it works

The target triple is `x86_64-w64-windows-gnu`, that is, mingw-w64 rather than MSVC:

* The output is an ordinary native PE executable. There is no emulation layer of the Cygwin
  kind involved, and no extra runtime DLL to ship: the binaries call the Windows API
  directly.
* Only the Win32 API headers and the CRT import libraries come from mingw-w64. The compiler,
  the linker and the whole C++ runtime are ours - Clang, LLD, and the libc++, libc++abi,
  LLVM libunwind and compiler-rt in `contrib/llvm-project`, exactly as on every other
  platform.
* mingw-w64 is packaged by every Linux distribution and is freely redistributable, so unlike
  an MSVC-targeted build this needs nothing extracted from a Windows installation and no
  Microsoft licence.

Two details are worth knowing when reading the code:

* **Windows is LLP64.** `long` is 32 bits wide, unlike on every other platform ClickHouse
  supports, where it is 64. Code that assumes `sizeof(long) == 8` - `__builtin_clzl` on a
  `size_t`, an `L`-suffixed constant that needs 64 bits, a `long` typedef for a pointer -
  is silently wrong. Several such bugs were found while getting this far.
* **Thread stacks come from the PE header.** Windows has no `RLIMIT_STACK` equivalent, and
  its default of 1 MiB is too small for our recursive-descent parser, so the link sets an
  8 MiB reserve. That covers every thread, not just the main one, which is why - unlike on
  macOS, see `src/Common/ThreadStackSize.h` - no per-thread override is needed.

## Install the cross-compilation toolset

```bash theme={null}
sudo apt-get install mingw-w64
```

This installs the headers and import libraries under `/usr/x86_64-w64-mingw32`, which is
where the toolchain file looks for them. Point `-DMINGW_SYSROOT=...` at a different sysroot
if yours lives elsewhere. Nothing else from the package is used - in particular not its GCC.

mingw-w64 12 or newer is required: older releases (such as the 11 in Ubuntu 24.04) lack the
`_l`-suffixed per-locale CRT functions (`_iswctype_l`, ...) that the libc++ locale support
calls, in both the headers and `libmsvcrt.a`. If your distribution's package is older,
install the `mingw-w64-common` and `mingw-w64-x86-64-dev` packages from a newer release the
way `ci/docker/binary-builder/Dockerfile` does - they are data-only packages that install
cleanly anywhere - and point `-DMINGW_SYSROOT` at them if not at the default location.

## Build ClickHouse

```bash theme={null}
cd ClickHouse
cmake -S . -B build-windows -G Ninja \
    -DCMAKE_TOOLCHAIN_FILE=$PWD/cmake/windows/toolchain-x86_64.cmake \
    -DENABLE_LIBRARIES=OFF \
    -DENABLE_TESTS=OFF \
    -DENABLE_UTILS=OFF \
    -DENABLE_CLICKHOUSE_ALL=OFF
ninja -C build-windows clickhouse
```

`ENABLE_LIBRARIES=OFF` is what keeps the optional third-party libraries - Kafka, HDFS, the
cloud storage SDKs and so on - out of the build. None of them has been looked at for
Windows, and the client and local modes do not need them.

The resulting binaries are in the PE format and cannot be run on Linux. To test them without
a Windows machine, use Wine - but note that an `x86_64` PE binary needs an `x86_64` host, or
an emulator such as FEX on ARM:

```bash theme={null}
wine build-windows/programs/clickhouse.exe local --query "SELECT 1"
```

## Current state

What builds for Windows today:

* the C++ runtime: libc++, libc++abi, LLVM libunwind (driving SEH on `x86_64`) and the
  compiler-rt builtins;
* 117 third-party libraries, among them OpenSSL, Boost, zlib-ng, zstd, LZ4, re2, Abseil,
  cctz, c-ares, replxx and Snappy;
* all of Poco - Foundation, Net, XML, JSON, Util and NetSSL;
* every library under `src`, and `programs`, which link into a native PE `clickhouse.exe`.

`clickhouse-windows-ported`, defined in `cmake/windows/ported_targets.cmake`, is the first two
of those. It is built alongside `clickhouse` rather than being subsumed by it, because it is
derived from the contrib list rather than from what the binary happens to link, so a
third-party library that nothing depends on yet stays covered.

The target is derived from the contrib list rather than being a hand-written copy of it, so a
newly added third-party library is covered automatically. If yours does not build for Windows
and nothing in the client or local modes needs it, add it to `WINDOWS_UNPORTED_TARGETS` in
that file with a note - do not silently drop the coverage.

Not built for Windows, and why:

| Component | Reason |
| - | - |
| `libuv` | Needs its `src/win/` sources selected. Only reached through nats-io, Cassandra and AMQP-CPP. |
| `libxml2` | Wants `iconv.h`, and its `off_t` use assumes LP64. Only reached through libhdfs3 and the Azure SDK. |
| `libarchive` | Its `config.h` in `contrib/libarchive-cmake` describes a POSIX host. Until one exists for Windows, `clickhouse-local` there cannot read archives. |
| `jemalloc` | Builds, but its `pages_map` cannot honour our `MADV_DONTNEED`-based purging. A client does not need a custom allocator. |
| Keeper, the JIT, the DWARF parser, `libfiu`, `liburing`, `numactl`, Rust | Server-side, Linux-specific, or no Windows target in the CI image. See `cmake/target.cmake`. |
| `Poco::Net::NetworkInterface`, `MulticastSocket` | Upstream's Windows implementation needs members our Poco fork removed along with the rest of its Windows support. Nothing in ClickHouse uses either. |

## Remaining work

What stands between a binary that links and one that works.

**Run more of it.** CI executes `clickhouse.exe local --query "SELECT 1"` under Wine on every
pull request - the startup path and one pipeline - plus the focused checks in
`utils/windows-selftest`. Everything past that is still unexecuted: the client against a real
server, `clickhouse-local` over actual files, any of the test suites, and the binary on a real
Windows machine rather than on Wine, which stubs enough of the platform that some behaviour
cannot be checked there at all (see [CI](#ci)). Note for a development host that is
not `x86_64`: an `x86_64` PE needs an `x86_64` host, or an emulator such as FEX, which Wine on
ARM does not ship.

**Debug info.** The build passes `-g0` on Windows. A PE image cannot exceed 4 GiB, the DWARF
for this binary is several times that on its own, and PE has no `.gnu_debuglink` equivalent to
carry it in a separate file. A crash therefore symbolizes to a module and an offset, not to a
file and a line. Splitting the debug info out needs either a PDB writer or an out-of-band
format of our own.

**No `Epoll` backend.** `src/Common/Epoll.*` is Linux-only, so everything that polls sockets
through it - the parts of the client that wait on more than one connection at a time - does
not work yet. Windows has `WSAPoll` and I/O completion ports; the wake-up path already has a
Windows implementation in `WakeupFd`, built on a loopback socket pair because a Windows pipe
cannot be waited on alongside sockets.

**Subsystems that report `NOT_IMPLEMENTED`,** each with the reason recorded at its site: local
syslog, archive reading (`libarchive` needs a hand-written Windows `config.h`), conditional
writes to the local object storage (they need `flock` on a directory and sub-second
modification times), and the web terminal.

**`clickhouse-disks` paths on non-local disks.** `DiskWithPath` joins and normalizes its virtual
paths through `std::filesystem`, which on Windows also treats `\` as a separator. An object
whose name contains a backslash on an object-storage disk therefore cannot be navigated to or
completed by `clickhouse-disks.exe`. The fix is to do the path handling of `clickhouse-disks`
in string space, with `std::filesystem` only for the `local` disk.

**The `server` mode is rejected up front.** `clickhouse server` prints that the server is not
supported on Windows and exits: its startup goes through `BaseDaemon`, which needs POSIX
signals and `fork`. The `clickhouse-server` alias is not installed. `clickhouse-server-lib`
itself is still built and linked, because `clickhouse-local` needs it.

**No symlink aliases.** On Linux the install tree offers `clickhouse-client`, `clickhouse-local`
and the rest as extensionless symlinks to `clickhouse`; next to `clickhouse.exe` such a symlink
cannot resolve, so none are generated or installed on Windows. The mode is chosen the multi-call
way - `clickhouse.exe client`, `clickhouse.exe local` - or by copying the binary to a name such
as `clickhouse-local.exe`: the dispatcher reads the mode from `argv[0]` with the directory and
the case-insensitive `.exe` suffix stripped.

**Compiled out, all server-side or POSIX-only:** the sampling query profiler and the signal
handlers (Windows reports faults through SEH instead), `ThreadFuzzer`, the `fork`-based
watchdog, `ShellCommand` and everything built on it, the pseudo-terminal features, and the
`su`/`docker-init`/`install` tools. Anything that needs one of these on Windows needs a
Windows design first, not a stub.

**`std::filesystem::path` conversions.** `base/base/pathToString.h` explains the problem and
is used at every site that needed it so far, but the codebase keeps relying on the implicit
`path`/`std::string` conversion that only exists on POSIX, so newly written code reintroduces
the errors. UTF-8 is the answer - `path::string()` is not, it goes through the active code
page.

**A POSIX call and its Windows counterpart may not accept the same descriptors.** The first
time the binary was executed rather than merely linked, it threw before printing anything:
`getTerminalSize` read the console size from standard input, because `ioctl(TIOCGWINSZ)`
answers on any descriptor of the terminal. Its Windows counterpart does not - the size lives on
the console's *screen buffer*, which only an output handle has - so it failed with
`ERROR_INVALID_HANDLE` by construction. Nor is `_isatty` a test for "is a console": it is true
for every character device, `NUL` and the console input handle included, so the only usable
predicate was the console call itself succeeding. Expect more of this shape: it compiles, it
mirrors the POSIX code, and it is wrong.

## CI

The `Build (amd_windows)` job builds `clickhouse`, `clickhouse-windows-ported` and
`clickhouse-windows-selftest`. It is
defined by `BuildTypes.AMD_WINDOWS` in `ci/defs/defs.py`, its cmake flags in
`ci/jobs/build_clickhouse.py`, and it runs in the `clickhouse/binary-builder` image, which
installs `mingw-w64`. The job publishes no artifact: `clickhouse.exe` is not something to hand
to anyone yet.

The job is part of the `PR` workflow only. The `BackportPR` workflow does not run it, so a
cherry-pick to a release branch can break the Windows build without a CI signal.

The job then **runs** what it built, under Wine: `clickhouse.exe --version` and
`clickhouse.exe local --query "SELECT 1"`. That is why this cross-build, alone among the ones
here, runs on an `amd64` runner - an `x86_64` PE needs an `x86_64` host, and the ARM build of
Wine ships no `x86` emulator - and why `binary-builder` installs `wine64` on `amd64`.
`clickhouse-local` answering a trivial query covers the whole startup path: configuration, the
temporary data directory, the thread pools and one query pipeline. Everything the port
implements for Windows - the console queries, the socket and file-mapping wrappers,
`setThreadName`, the loopback `WakeupFd` - was written against the documentation and had never
been executed before this step existed, so this is where those assumptions are checked.

Answering a query does not reach all of it, and the cross-build configures with
`ENABLE_TESTS=0`, so behaviour that is on no path a `SELECT` takes has nowhere else to be
checked. `utils/windows-selftest` is that place: a small executable, linking only `_poco_net`
and `common`, that the same step runs right after `clickhouse.exe`. It covers the emulation of
`MSG_DONTWAIT`, which Winsock has no equivalent for, and the memory limit a job object imposes,
which is what a Windows container caps a process with. Add a check there for anything the port
implements that a query does not exercise.

The step also runs `utils/windows-selftest/backup-non-ascii-names.sh`, which makes
`clickhouse.exe local` back a table up to a `Disk` and to a `File` destination whose names are
not ASCII, restores both, and then looks for the backups on the host under their UTF-8 names.
The last part is the point: a name that enters `std::filesystem::path` through the active code
page instead of `pathFromString` is still restored correctly, because both directions are
mangled the same way, but the backup lands in a directory with a different name.

The C runtime's `main` decodes the command line through the active code page, so `main` in
`programs/main.cpp` rebuilds `argv` from `GetCommandLineW` as UTF-8 before anything reads it.
The step checks that by passing a non-ASCII `--query` and comparing the `hex` of the string the
query received. It runs Wine under a UTF-8 locale, without which Wine itself would decode the
Unix command line through a narrower code page before the program started.

One limit of running on Wine rather than on Windows is worth knowing before writing such a
check: Wine stubs whole interfaces while reporting success. `QueryInformationJobObject` for
`JobObjectExtendedLimitInformation` zeroes the caller's struct and returns true (see
`NtQueryInformationJobObject` in Wine's `dlls/ntdll/unix/sync.c`), so a limit set with
`SetInformationJobObject` cannot be read back and the clamp in `getMemoryAmountOrZero` cannot
be observed end to end there. The self-test therefore exercises the decision - `windowsJobObjectMemoryLimit`,
which is declared separately for this reason - against the values the operating system would
supply, and separately asserts that whatever the job does report, the amount stays within it.
