Cross compiling Chromium for Windows

How to keep coding in your Linux/Mac box while building for Windows

Posted by Henrique Ferreiro on Tue, Jan 22, 2019

Since October 2017, Chromium for Windows can be built from Linux machines. This works by automatically downloading the Windows SDK and using clang-cl in the same way it is used on Windows. Unfortunately, the announcement at chromium-dev mentioned that this would only work for people working at Google, as they were not allowed to redistribute the Windows SDK, and asked for help in implementing the missing bits.

Our Chromium work at Igalia requires sometimes to use Windows machines to build or test Windows specific code, so we were really eager to get this working. After some hacking, we contributed depot_tools support for using a copy of the Windows SDK from a local zip file at depot_tools@c5a26a7. As mentioned in the documentation, the SDK can be packaged very easily by running the following script from a depot_tools checkout on a Windows box:

$ python win_toolchain/package_from_installed.py 2017 -w 10.0.17134.0
Building file list for VS 2017 Windows 10.0.17134.0...
Wrote to out.zip.
Extracting to determine hash...
Hashing...
Calculating hash of toolchain in vs_files. Please wait...
Renamed out.zip to 5c1e774993a7d6cb7ead97c408780169a12e2a3b.zip.

Then, after copying the zip file to the Linux or Mac host, gclient needs to be setup to build Chromium for Windows by adding target_os = ['win'] to .gclient and it needs to be told where the SDK package is. In order to use a local file, DEPOT_TOOLS_WIN_TOOLCHAIN_BASE_URL needs to point to the local directory where 5c1e774993a7d6cb7ead97c408780169a12e2a3b.zip was saved. Finally, gclient is going to look for a file whose basename matches a hardcoded hash in src/build/vs_toolchain.py#338, but it can be changed by setting GYP_MSVS_HASH_<toolchain hash>=<local hash>. For this particular case:

$ export DEPOT_TOOLS_WIN_TOOLCHAIN_BASE_URL=~/Downloads
$ export GYP_MSVS_HASH_3bc0ec615cf20ee342f3bc29bc991b5ad66d8d2c=5c1e774993a7d6cb7ead97c408780169a12e2a3b

Instead of grepping for toolchain_hash in vs_toolchain.py, a more convenient way to find its value is setting DEPOT_TOOLS_WIN_TOOLCHAIN_BASE_URL alone and syncing:

$ export DEPOT_TOOLS_WIN_TOOLCHAIN_BASE_URL=~/Downloads
$ gclient sync
...
Running hooks:  14% (11/76) win_toolchain
________ running '/usr/bin/python src/build/vs_toolchain.py update --force' in '/home/hferreiro/git/chromium'
Windows toolchain out of date or doesn't exist, updating (Pro)...
  current_hashes:
  desired_hash: 3bc0ec615cf20ee342f3bc29bc991b5ad66d8d2c
...

Finally, target_os = "win" needs to be added to the args.gn file, and then ninja can be run normally.

Enjoy.