xref: /aosp_15_r20/external/bazelbuild-rules_go/windows.rst (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1*9bb1b549SSpandan DasUsing rules_go on Windows
2*9bb1b549SSpandan Das=========================
3*9bb1b549SSpandan Das
4*9bb1b549SSpandan Das.. _--incompatible_enable_cc_toolchain_resolution: https://github.com/bazelbuild/bazel/issues/7260
5*9bb1b549SSpandan Das.. _Installing Bazel on Windows: https://docs.bazel.build/versions/master/install-windows.html
6*9bb1b549SSpandan Das
7*9bb1b549SSpandan DasThis document provides a list of instructions for setting up Bazel to build
8*9bb1b549SSpandan DasGo code on a Windows computer.
9*9bb1b549SSpandan Das
10*9bb1b549SSpandan DasMost of the difficulty here is installing a compatible C/C++ toolchain. Cgo
11*9bb1b549SSpandan Dasonly works with GCC and clang toolchains, so MSVC cannot be used. This is a
12*9bb1b549SSpandan DasGo limitation, not a Bazel or rules_go problem: cgo determines types of
13*9bb1b549SSpandan Dasdefinitions by parsing error messages that GCC emits when compiling generated
14*9bb1b549SSpandan Dasfiles.
15*9bb1b549SSpandan Das
16*9bb1b549SSpandan DasSee also `Installing Bazel on Windows`_, the official instructions for
17*9bb1b549SSpandan Dasinstalling Bazel.
18*9bb1b549SSpandan Das
19*9bb1b549SSpandan DasInstall and configure dependencies
20*9bb1b549SSpandan Das----------------------------------
21*9bb1b549SSpandan Das
22*9bb1b549SSpandan Das* Install msys2 from https://www.msys2.org/. This is needed to provide a bash
23*9bb1b549SSpandan Das  environment for Bazel.
24*9bb1b549SSpandan Das
25*9bb1b549SSpandan Das  * Follow the installation directions to the end, including
26*9bb1b549SSpandan Das    running ``pacman -Syu`` and ``pacman -Su`` in the msys2 shell.
27*9bb1b549SSpandan Das
28*9bb1b549SSpandan Das* Install additional msys2 tools.
29*9bb1b549SSpandan Das
30*9bb1b549SSpandan Das  * Run ``pacman -S mingw-w64-x86_64-gcc``. GCC is needed if you plan to build
31*9bb1b549SSpandan Das    any cgo code. MSVC will not work with cgo. This is a Go limitation, not a
32*9bb1b549SSpandan Das    Bazel limitation. cgo determines types of definitions by compiling specially
33*9bb1b549SSpandan Das    crafted C files and parsing error messages. GCC or clang are specifically
34*9bb1b549SSpandan Das    needed for this.
35*9bb1b549SSpandan Das  * Run ``pacman -S patch``. ``patch`` is needed by ``git_repository`` and
36*9bb1b549SSpandan Das    ``http_archive`` dependencies declared by rules_go. We use it to add
37*9bb1b549SSpandan Das    and modify build files.
38*9bb1b549SSpandan Das
39*9bb1b549SSpandan Das* Add ``C:\msys64\usr\bin`` to ``PATH`` in order to locate ``patch`` and
40*9bb1b549SSpandan Das  other DLLs.
41*9bb1b549SSpandan Das* Add ``C:\msys64\mingw64\bin`` to ``PATH`` in order to locate mingw DLLs.
42*9bb1b549SSpandan Das  ``protoc`` and other host binaries will not run without these.
43*9bb1b549SSpandan Das* Set the environment variable ``BAZEL_SH`` to ``C:\msys64\usr\bin\bash.exe``.
44*9bb1b549SSpandan Das  Bazel needs this to run shell scripts.
45*9bb1b549SSpandan Das* Set the environment variable ``CC`` to ``C:\msys64\mingw64\bin\gcc.exe``.
46*9bb1b549SSpandan Das  Bazel uses this to configure the C/C++ toolchain.
47*9bb1b549SSpandan Das* Install the MSVC++ redistributable from
48*9bb1b549SSpandan Das  https://www.microsoft.com/en-us/download/details.aspx?id=48145.
49*9bb1b549SSpandan Das  Bazel itself depends on this.
50*9bb1b549SSpandan Das* Install Git from https://git-scm.com/download/win. The Git install should
51*9bb1b549SSpandan Das  add the installed directory to your ``PATH`` automatically.
52*9bb1b549SSpandan Das
53*9bb1b549SSpandan DasInstall bazel
54*9bb1b549SSpandan Das-------------
55*9bb1b549SSpandan Das
56*9bb1b549SSpandan Das* Download Bazel from https://github.com/bazelbuild/bazel/releases.
57*9bb1b549SSpandan Das* Move the binary to ``%APPDATA%\bin\bazel.exe``.
58*9bb1b549SSpandan Das* Add that directory to ``PATH``.
59*9bb1b549SSpandan Das* Confirm ``bazel version`` works.
60*9bb1b549SSpandan Das
61*9bb1b549SSpandan DasConfirm C/C++ works
62*9bb1b549SSpandan Das-------------------
63*9bb1b549SSpandan Das
64*9bb1b549SSpandan DasCreate a workspace with a simple ``cc_binary`` target.
65*9bb1b549SSpandan Das
66*9bb1b549SSpandan Das.. code::
67*9bb1b549SSpandan Das
68*9bb1b549SSpandan Das    -- WORKSPACE --
69*9bb1b549SSpandan Das
70*9bb1b549SSpandan Das    -- BUILD.bazel --
71*9bb1b549SSpandan Das    cc_binary(
72*9bb1b549SSpandan Das        name = "hello",
73*9bb1b549SSpandan Das        srcs = ["hello.c"],
74*9bb1b549SSpandan Das    )
75*9bb1b549SSpandan Das
76*9bb1b549SSpandan Das    -- hello.c --
77*9bb1b549SSpandan Das    #include <stdio.h>
78*9bb1b549SSpandan Das
79*9bb1b549SSpandan Das    int main() {
80*9bb1b549SSpandan Das      printf("hello\n");
81*9bb1b549SSpandan Das      return 0;
82*9bb1b549SSpandan Das    }
83*9bb1b549SSpandan Das
84*9bb1b549SSpandan DasTo build with MinGW, run the command below. Add the ``-s`` flag to print
85*9bb1b549SSpandan Dascommands executed by Bazel to confirm MinGW is actually used.
86*9bb1b549SSpandan Das
87*9bb1b549SSpandan Das.. code::
88*9bb1b549SSpandan Das
89*9bb1b549SSpandan Das    bazel build --cpu=x64_windows --compiler=mingw-gcc //:hello
90*9bb1b549SSpandan Das
91*9bb1b549SSpandan DasFuture versions of Bazel will select a C/C++ toolchain using the same platform
92*9bb1b549SSpandan Dasand toolchain system used by other rules. This will be the default after the
93*9bb1b549SSpandan Das`--incompatible_enable_cc_toolchain_resolution`_ flag is flipped. To ensure
94*9bb1b549SSpandan Dasthat the MinGW toolchain is registered, either build against a ``platform``
95*9bb1b549SSpandan Dastarget with the ``@bazel_tools//tools/cpp:mingw`` constraint such as
96*9bb1b549SSpandan Das``@io_bazel_rules_go//go/toolchain:windows_amd64_cgo``, or define your own
97*9bb1b549SSpandan Dastarget explicitly, as below:
98*9bb1b549SSpandan Das
99*9bb1b549SSpandan Das.. code::
100*9bb1b549SSpandan Das
101*9bb1b549SSpandan Das    platform(
102*9bb1b549SSpandan Das        name = "windows_amd64_mingw",
103*9bb1b549SSpandan Das        constraint_values = [
104*9bb1b549SSpandan Das            "@bazel_tools//tools/cpp:mingw",
105*9bb1b549SSpandan Das            "@platforms//cpu:x86_64",
106*9bb1b549SSpandan Das            "@platforms//os:windows",
107*9bb1b549SSpandan Das        ],
108*9bb1b549SSpandan Das    )
109*9bb1b549SSpandan Das
110*9bb1b549SSpandan DasYou can build with the command below. This also ensures the MinGW toolchain is
111*9bb1b549SSpandan Dasregistered (it is not, by default).
112*9bb1b549SSpandan Das
113*9bb1b549SSpandan Das.. code::
114*9bb1b549SSpandan Das
115*9bb1b549SSpandan Das    bazel build --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows_mingw --host_platform=//:windows_amd64_mingw --platforms=//:windows_amd64_mingw --incompatible_enable_cc_toolchain_resolution //:hello
116*9bb1b549SSpandan Das
117*9bb1b549SSpandan DasYou may want to add these flags to a ``.bazelrc`` file in your project root
118*9bb1b549SSpandan Dasdirectory or in your home directory.
119*9bb1b549SSpandan Das
120*9bb1b549SSpandan Das.. code::
121*9bb1b549SSpandan Das
122*9bb1b549SSpandan Das    build --cpu=x64_windows
123*9bb1b549SSpandan Das    build --compiler=mingw-gcc
124*9bb1b549SSpandan Das    build --extra_toolchains=@local_config_cc//:cc-toolchain-x64_windows_mingw
125*9bb1b549SSpandan Das    build --host_platform=//:windows_amd64_mingw
126*9bb1b549SSpandan Das    build --platforms=//:windows_amd64_mingw
127*9bb1b549SSpandan Das    build --incompatible_enable_cc_toolchain_resolution
128*9bb1b549SSpandan Das
129*9bb1b549SSpandan DasConfirm Go works
130*9bb1b549SSpandan Das----------------
131*9bb1b549SSpandan Das
132*9bb1b549SSpandan Das* Copy boilerplate from rules_go.
133*9bb1b549SSpandan Das* Confirm that you can run a pure Go "hello world" binary with
134*9bb1b549SSpandan Das  ``bazel run //:target``
135*9bb1b549SSpandan Das* Confirm you can run a cgo binary with the same set of flags and platforms
136*9bb1b549SSpandan Das  used to build a C target above.
137