-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Win32 resource in Bazel build
With this commit, Win32 resource and manifest start being embedded into the final executables even for Bazel build (#948). There must be no observable behavior change in GYP build. PiperOrigin-RevId: 679898803
- Loading branch information
1 parent
8f1db1e
commit cc9e050
Showing
7 changed files
with
384 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package( | ||
default_visibility = ["//visibility:public"], | ||
) | ||
|
||
exports_files(["bin/rc.exe"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# Copyright 2010-2021, Google Inc. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following disclaimer | ||
# in the documentation and/or other materials provided with the | ||
# distribution. | ||
# * Neither the name of Google Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
"""Repository rule for Windows SDK.""" | ||
|
||
_GET_WINSDK_INCLUDE_DIRS_TEMPLATE = """ | ||
def get_winsdk_include_dirs(): | ||
return [ | ||
r"{include_um}", | ||
r"{include_shared}", | ||
] | ||
""" | ||
|
||
def _get_path_env_var(repository_ctx, name): | ||
"""Returns a path from an environment variable with removing quotes.""" | ||
|
||
value = repository_ctx.getenv(name) | ||
if value == None: | ||
return None | ||
|
||
if value[0] == "\"" and len(value) > 1 and value[-1] != "\"": | ||
value = value[1:-1] | ||
if "/" in value: | ||
value = value.replace("/", "\\") | ||
if value[-1] == "\\": | ||
value = value.rstrip("\\") | ||
return value | ||
|
||
def _read_reg_key(repository_ctx, reg_binary, reg_key, reg_value): | ||
"""Reads a Win32 registory value.""" | ||
|
||
result = repository_ctx.execute([reg_binary, "query", reg_key, "/v", reg_value]) | ||
if result.stderr: | ||
return None | ||
for line in result.stdout.split("\n"): | ||
line = line.strip() | ||
if not line.startswith(reg_value): | ||
continue | ||
if line.find("REG_SZ") == -1: | ||
continue | ||
return line[line.find("REG_SZ") + len("REG_SZ"):].strip() | ||
return None | ||
|
||
def _find_winsdk_path_and_ver(repository_ctx): | ||
"""Find the Windows SDK path and its latest version.""" | ||
|
||
reg_binary = _get_path_env_var(repository_ctx, "SYSTEMROOT") + r"\system32\reg.exe" | ||
reg_roots = [ | ||
r"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node", | ||
r"HKEY_CURRENT_USER\SOFTWARE\Wow6432Node", | ||
r"HKEY_LOCAL_MACHINE\SOFTWARE", | ||
r"HKEY_CURRENT_USER\SOFTWARE", | ||
] | ||
|
||
for reg_root in reg_roots: | ||
# The following registry key has not been updated since Windows 10. | ||
# Hopefully we can keep using it for a while. | ||
reg_key = reg_root + r"\Microsoft\Microsoft SDKs\Windows\v10.0" | ||
winsdk_dir = _read_reg_key(repository_ctx, reg_binary, reg_key, "InstallationFolder") | ||
if not winsdk_dir: | ||
continue | ||
winsdk_ver_base = _read_reg_key(repository_ctx, reg_binary, reg_key, "ProductVersion") | ||
if not winsdk_ver_base: | ||
continue | ||
for rev in range(9, -1, -1): | ||
winsdk_ver = winsdk_ver_base + "." + str(rev) | ||
if repository_ctx.path(winsdk_dir + "/bin/" + winsdk_ver).exists: | ||
return (winsdk_dir, winsdk_ver) | ||
return (None, None) | ||
|
||
def _windows_sdk_impl(repo_ctx): | ||
is_windows = repo_ctx.os.name.lower().startswith("win") | ||
if not is_windows: | ||
repo_ctx.file("BUILD.bazel", "") | ||
repo_ctx.template( | ||
"windows_sdk_rules.bzl", | ||
repo_ctx.path(Label("@//bazel:windows_sdk_rules.noop.template.bzl")), | ||
executable = False, | ||
) | ||
return | ||
|
||
winsdk_dir, winsdk_ver = _find_winsdk_path_and_ver(repo_ctx) | ||
if winsdk_dir == None or winsdk_ver == None: | ||
repo_ctx.file("BUILD.bazel", "") | ||
repo_ctx.template( | ||
"windows_sdk_rules.bzl", | ||
repo_ctx.path(Label("@//bazel:windows_sdk_rules.noop.template.bzl")), | ||
executable = False, | ||
) | ||
return | ||
|
||
# Hopefully "x86" and "arm64" should just work, but we need to check later. | ||
arch = repo_ctx.os.arch | ||
if arch == "amd64" or arch == "x86_64": | ||
arch = "x64" | ||
|
||
winsdk_path = repo_ctx.path(winsdk_dir) | ||
repo_ctx.symlink(winsdk_path.get_child("bin/" + winsdk_ver + "/" + arch), "bin") | ||
repo_ctx.symlink(winsdk_path.get_child("include/" + winsdk_ver), "include") | ||
repo_ctx.symlink(winsdk_path.get_child("lib/" + winsdk_ver), "lib") | ||
repo_ctx.template( | ||
"BUILD.bazel", | ||
repo_ctx.path(Label("@//bazel:BUILD.winsdk.bazel")), | ||
executable = False, | ||
) | ||
|
||
get_winsdk_include_dirs = _GET_WINSDK_INCLUDE_DIRS_TEMPLATE.format( | ||
include_um = repo_ctx.path("include/um"), | ||
include_shared = repo_ctx.path("include/shared"), | ||
) | ||
repo_ctx.file("get_winsdk_include_dirs.bzl", get_winsdk_include_dirs) | ||
|
||
repo_ctx.template( | ||
"windows_sdk_rules.bzl", | ||
repo_ctx.path(Label("@//bazel:windows_sdk_rules.win32.template.bzl")), | ||
executable = False, | ||
) | ||
|
||
windows_sdk_repository = repository_rule( | ||
implementation = _windows_sdk_impl, | ||
configure = True, | ||
local = True, | ||
environ = ["SYSTEMROOT"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2010-2021, Google Inc. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following disclaimer | ||
# in the documentation and/or other materials provided with the | ||
# distribution. | ||
# * Neither the name of Google Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
"""A no-op implementation used on non-Windows platforms.""" | ||
|
||
def windows_resource(name, **kwargs): | ||
_ignore = name # @unused | ||
_ignore = kwargs # @unused | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# Copyright 2010-2021, Google Inc. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following disclaimer | ||
# in the documentation and/or other materials provided with the | ||
# distribution. | ||
# * Neither the name of Google Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
"""A reas implementation used on Windows platform.""" | ||
|
||
load("@bazel_skylib//lib:paths.bzl", "paths") | ||
|
||
# "get_winsdk_include_dirs.bzl" is dynamically generated by "windows_sdk_repository.bzl" | ||
load(":get_winsdk_include_dirs.bzl", "get_winsdk_include_dirs") | ||
|
||
def _build_win_rc(ctx, resource_name, rc_file, resources, defines, codepage): | ||
resource_file = ctx.actions.declare_file(resource_name) | ||
source_dir = ctx.label.package | ||
|
||
includes = [ | ||
rc_file.dirname, | ||
source_dir, | ||
ctx.var["BINDIR"], | ||
ctx.var["GENDIR"], | ||
] + get_winsdk_include_dirs() + ["./"] | ||
|
||
transitive_defines = [] | ||
for resource in ctx.attr.resources: | ||
if CcInfo in resource: | ||
transitive_defines += resource[CcInfo].compilation_context.defines.to_list() | ||
|
||
copts_defines = [ | ||
copt[2:] | ||
for copt in ctx.fragments.cpp.copts | ||
if copt.startswith(("-D", "/D")) | ||
] | ||
|
||
arch_defines = ctx.attr.arch_defines | ||
|
||
args = ctx.actions.args() | ||
args.add_all( | ||
copts_defines + transitive_defines + defines + ["RC_INVOKED"] + arch_defines, | ||
format_each = "/D%s", | ||
uniquify = True, | ||
) | ||
args.add_all(includes, format_each = "/I%s", uniquify = True) | ||
args.add(resource_file, format = "/fo%s") | ||
if codepage != 1200: # 1200 is UTF-16 | ||
args.add(codepage, format = "/C%s") | ||
args.add(rc_file) | ||
ctx.actions.run( | ||
inputs = [rc_file] + resources, | ||
outputs = [resource_file], | ||
executable = ctx.executable.rc_exe, | ||
arguments = [args], | ||
mnemonic = "WindowsRc", | ||
progress_message = "Compiling resources {0} to {1}".format( | ||
rc_file.basename, | ||
resource_file.basename, | ||
), | ||
toolchain = None, | ||
) | ||
|
||
return resource_file | ||
|
||
def _win32_res_rule_impl(ctx): | ||
rc_files = ctx.files.rc_files | ||
resources = ctx.files.resources | ||
manifests = ctx.files.manifests | ||
defines = ctx.attr.defines | ||
codepage = ctx.attr.codepage | ||
|
||
resource_files = [] | ||
for rc_file in rc_files: | ||
resource_files.append(_build_win_rc( | ||
ctx = ctx, | ||
resource_name = paths.replace_extension(rc_file.path, ".res"), | ||
resources = resources, | ||
rc_file = rc_file, | ||
defines = defines, | ||
codepage = codepage, | ||
)) | ||
resource_file_provider = DefaultInfo(files = depset(resource_files)) | ||
|
||
resource_link_flags = [res.path for res in resource_files] | ||
|
||
manifest_flags = [] | ||
if len(manifests) > 0: | ||
manifest_flags.append("/MANIFEST:EMBED") | ||
for manifest in manifests: | ||
manifest_flags.append("/MANIFESTINPUT:{}".format(manifest.path)) | ||
|
||
linker_input = cc_common.create_linker_input( | ||
owner = ctx.label, | ||
additional_inputs = depset(resource_files + manifests), | ||
user_link_flags = depset(resource_link_flags + manifest_flags), | ||
) | ||
linking_context = cc_common.create_linking_context( | ||
linker_inputs = depset([linker_input]), | ||
) | ||
return [ | ||
resource_file_provider, | ||
CcInfo(linking_context = linking_context), | ||
] | ||
|
||
_win32_res_rule = rule( | ||
_win32_res_rule_impl, | ||
attrs = { | ||
"rc_files": attr.label_list( | ||
allow_files = [".rc"], | ||
), | ||
"resources": attr.label_list( | ||
allow_files = True, | ||
), | ||
"manifests": attr.label_list( | ||
allow_files = [".manifest"], | ||
), | ||
"defines": attr.string_list( | ||
allow_empty = True, | ||
default = [], | ||
mandatory = False, | ||
), | ||
"codepage": attr.int( | ||
default = 1200, # UTF-16 | ||
mandatory = False, | ||
), | ||
"rc_exe": attr.label( | ||
executable = True, | ||
cfg = "exec", | ||
allow_single_file = [".exe"], | ||
default = Label(":bin/rc.exe"), | ||
), | ||
"arch_defines": attr.string_list( | ||
default = [], | ||
mandatory = False, | ||
), | ||
}, | ||
provides = [CcInfo], | ||
fragments = ["cpp"], | ||
) | ||
|
||
def windows_resource(name, **kwargs): | ||
_win32_res_rule( | ||
name = name, | ||
arch_defines = select({ | ||
"@platforms//cpu:arm64": ["_ARM64_"], | ||
"@platforms//cpu:x86_64": ["_AMD64_"], | ||
"@platforms//cpu:x86_32": ["_X86_"], | ||
"//conditions:default": [""], | ||
}), | ||
**kwargs | ||
) |
Oops, something went wrong.