Currently cc_binary()/cc_library() cannot consume RC/RES files. One need to have possibility to specify it in srcs/deps or separate attributes.
https://docs.bazel.build/versions/master/be/c-cpp.html#cc_binary_args
https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library_args
Permitted srcs file types:
C and C++ source files: .c, .cc, .cpp, .cxx, .c++, .C
C and C++ header files: .h, .hh, .hpp, .hxx, .inc
Assembler with C preprocessor: .S
Archive: .a, .pic.a
"Always link" library: .lo, .pic.lo
Shared library, versioned or unversioned: .so, .so.version
Object file: .o, .pic.o
ERROR: C:/users/chebotarev_v/documents/codegen/main/BUILD:21:12: in srcs attribute of cc_binary rule //main:hello-world: file '//main:some_res.rc' is misplaced here (expected .cc, .cpp, .cxx, .c++, .C, .c, .h, .hh, .hpp, .ipp, .hxx, .inc, .S, .s, .asm, .a, .lib, .pic.a, .lo, .pic.lo, .so, .dylib, .dll, .o or .pic.o)
ERROR: C:/users/chebotarev_v/documents/codegen/main/BUILD:21:12: in srcs attribute of cc_binary rule //main:hello-world: '//main:some_res.rc' does not produce any cc_binary srcs files (expected .cc, .cpp, .cxx, .c++, .C, .c, .h, .hh, .hpp, .ipp, .hxx, .inc, .S, .s, .asm, .a, .lib, .pic.a, .lo, .pic.lo, .so, .dylib, .dll, .o or .pic.o)
ERROR: Analysis of target '//main:hello-world' failed; build aborted: Analysis of target '//main:hello-world' failed; build aborted
I tried to compile RC into RES manually (using res.exe utility) and specify it in linkopts, but it looks like it does not work (RES is generating successfully by custom genrule):
ERROR: C:/users/chebotarev_v/documents/codegen/main/BUILD:23:16: in linkopts attribute of cc_binary rule //main:hello-world: could not resolve label 'some_res.res'
ERROR: Analysis of target '//main:hello-world' failed; build aborted: Analysis of target '//main:hello-world' failed; build aborted
Specifying RES in deps fails because it has wrong extension:
ERROR: C:/users/chebotarev_v/documents/codegen/main/BUILD:22:12: in deps attribute of cc_binary rule //main:hello-world: file '//main:some_res.res' is misplaced here (expected .ld, .lds or .ldscript)
ERROR: Analysis of target '//main:hello-world' failed; build aborted: Analysis of target '//main:hello-world' failed; build aborted
https://docs.bazel.build/versions/master/be/c-cpp.html#cc_binary_args
List of labels; optional
The list of other libraries to be linked in to the binary target.
These can be cc_library, cc_inc_library, or objc_library targets.
I guess both RES files in deps (or in additional res attribute) and RC files in srcs should be supported by bazel.
It is necessary to build binaries with resources in Windows.
Nothing.
Currently we have no plan to work on compiling RC into RES in native C++ rule. But there is a workaround, you can compile through a genrule and them add the resource file(.res) as an object file(.o) in srcs.
We use this way to build bazel.exe with an icon, see https://github.com/bazelbuild/bazel/commit/3d21578895bc6d1fb153fec3cea9810396abf78e
/cc @laszlocsomor
Actually, we no longer use that genrule, we just link in a prebuilt object file.
Starting from Bazel 0.10.0 we can use the cc_import rule to import the file cleanly.
The reason I ditched the genrule was that we had no reliable way to find the resource compiler, and I didn't want the success of building Bazel depend on such an unimportant detail as whether we can build its icon.
Good news, I exported find_vc_path and find_msvc_tool skylark functions from Bazel, so people can use them to find VC tools and configure their tool in a repository rule, including the resource compiler.
See https://github.com/bazelbuild/bazel/commit/ff12a22fde96a28d6ad1279b60a421163e772aa6
cc_import currently doesn't support .res file in srcs, but I guess we can add it.
Sorry, just noticed rc.exe is under C:\Program Files (x86)\Windows Kits\8.1\bin\x64, so find_msvc_tool doesn't work..
I would like to mention that the pipeline is slightly different - first, we create RES from RC by res.exe, then OBJ from RES by cvtres.exe (which is located near the linker) and finally link OBJ to the binary.
Can you advise how to find path to SDK for selected toolchain in order to generate RES properly?
We are not aware of a reliable way to detect where res.exe resides. AFAIK it's part of the Windows SDK, not of VC or VS, and we haven't found a good way to detect where the SDK is installed.
I mean rc.exe, not res.exe. Or did you mean res.exe? I'm not aware of that tool, nor of cvtres.exe. How do you install those?
Yeah, I've really meant rc.exe. It produces res file which can be directly consumed by a linker or it can be compiled into object file using cvtres.exe and then consumed by a linker.
Typically, it would be okay assuming rc.exe is at "%WindowsSdkDir%"\bin\{x86|x64}\rc.exe (after filling environment variables):
https://github.com/bazelbuild/bazel/blob/master/tools/cpp/windows_cc_configure.bzl#L324
Basically, it would be awesome if user rule could access WindowsSdkDir environment variable which was found by configure_windows_toolchain() in order to run whatever it wants for given target architecture.
@excitoon, sounds like a fair requirement.
As this was the top search result when I was trying to find how to build in an icon into my executable, here are some cross references to issues/PRs:
Most helpful comment
Basically, it would be awesome if user rule could access
WindowsSdkDirenvironment variable which was found byconfigure_windows_toolchain()in order to run whatever it wants for given target architecture.