Skip to content

Commit

Permalink
Resolve tools to include runfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jsharpe committed Jan 25, 2021
1 parent 4d4acaa commit 7c8a534
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
12 changes: 12 additions & 0 deletions test/cmake_text_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ def _merge_flag_values_no_toolchain_file_test(ctx):
"ws",
"linux",
"cmake",
"make",
"ninja",
tools,
flags,
"test_rule",
Expand Down Expand Up @@ -290,6 +292,8 @@ def _create_min_cmake_script_no_toolchain_file_test(ctx):
"ws",
"linux",
"cmake",
"make",
"ninja",
tools,
flags,
"test_rule",
Expand Down Expand Up @@ -334,6 +338,8 @@ def _create_min_cmake_script_wipe_toolchain_test(ctx):
"ws",
"linux",
"cmake",
"make",
"ninja",
tools,
flags,
"test_rule",
Expand Down Expand Up @@ -374,6 +380,8 @@ def _create_min_cmake_script_toolchain_file_test(ctx):
"ws",
"linux",
"cmake",
"make",
"ninja",
tools,
flags,
"test_rule",
Expand Down Expand Up @@ -440,6 +448,8 @@ def _create_cmake_script_no_toolchain_file_test(ctx):
"ws",
"linux",
"cmake",
"make",
"ninja",
tools,
flags,
"test_rule",
Expand Down Expand Up @@ -493,6 +503,8 @@ def _create_cmake_script_toolchain_file_test(ctx):
"ws",
"osx",
"cmake",
"make",
"ninja",
tools,
flags,
"test_rule",
Expand Down
2 changes: 2 additions & 0 deletions tools/build_defs/cmake.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def _create_configure_script(configureParameters):
# as default, pass execution OS as target OS
target_os = os_name(ctx),
cmake_path = configureParameters.attrs.cmake_path,
make_path = configureParameters.attrs.make_path,
ninja_path = configureParameters.attrs.ninja_path,
tools = tools,
flags = flags,
install_prefix = "$$INSTALL_PREFIX$$",
Expand Down
8 changes: 7 additions & 1 deletion tools/build_defs/cmake_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def create_cmake_script(
workspace_name,
target_os,
cmake_path,
make_path,
ninja_path,
tools,
flags,
install_prefix,
Expand All @@ -25,7 +27,7 @@ def create_cmake_script(
cmake_path: The path to the cmake executable
tools: cc_toolchain tools (CxxToolsInfo)
flags: cc_toolchain flags (CxxFlagsInfo)
install_prefix: value ot pass to CMAKE_INSTALL_PREFIX
install_prefix: value to pass to CMAKE_INSTALL_PREFIX
root: sources root relative to the $EXT_BUILD_ROOT
no_toolchain_file: if False, CMake toolchain file will be generated, otherwise not
user_cache: dictionary with user's values of cache initializers
Expand Down Expand Up @@ -73,6 +75,10 @@ def create_cmake_script(
if not params.cache.get("CMAKE_RANLIB"):
params.cache.update({"CMAKE_RANLIB": ""})

if len(make_path.split('/')) != 1:
params.commands.append("export PATH=$PATH:$(dirname {})".format(make_path))
if len(ninja_path.split('/')) != 1:
params.commands.append("export PATH=$PATH:$(dirname {})".format(ninja_path))
set_env_vars = " ".join([key + "=\"" + params.env[key] + "\"" for key in params.env])
str_cmake_cache_entries = " ".join(["-D" + key + "=\"" + params.cache[key] + "\"" for key in params.cache])
cmake_call = " ".join([
Expand Down
50 changes: 18 additions & 32 deletions tools/build_defs/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
),
mandatory = False,
allow_files = True,
cfg = "host",
default = [],
),
"postfix_script": attr.string(
Expand Down Expand Up @@ -191,7 +192,10 @@ def create_attrs(attr_struct, configure_name, create_configure_script, **kwargs)
attrs["create_configure_script"] = create_configure_script

for arg in kwargs:
attrs[arg] = kwargs[arg]
if hasattr(attrs, arg):
attrs[arg] += kwargs[arg]
else:
attrs[arg] = kwargs[arg]
return struct(**attrs)

# buildifier: disable=name-conventions
Expand Down Expand Up @@ -285,7 +289,7 @@ def cc_external_rule_impl(ctx, attrs):
"""
lib_name = attrs.lib_name or ctx.attr.name

inputs = _define_inputs(attrs)
inputs = _define_inputs(ctx, attrs)
outputs = _define_outputs(ctx, attrs, lib_name)
out_cc_info = _define_out_cc_info(ctx, attrs, inputs, outputs)

Expand Down Expand Up @@ -358,14 +362,17 @@ def cc_external_rule_impl(ctx, attrs):
if "requires-network" in ctx.attr.tags:
execution_requirements = {"requires-network": ""}

inputs_from_tools, manifests_from_tools = ctx.resolve_tools(tools = attrs.tools_deps)

ctx.actions.run_shell(
mnemonic = "Cc" + attrs.configure_name.capitalize() + "MakeRule",
inputs = depset(inputs.declared_inputs, transitive = [cc_toolchain.all_files]),
inputs = depset(inputs.declared_inputs),
outputs = rule_outputs + [
empty.file,
wrapped_outputs.log_file,
],
tools = depset([wrapped_outputs.script_file] + ctx.files.data),
input_manifests = manifests_from_tools,
tools = depset([wrapped_outputs.script_file] + ctx.files.data, transitive=[inputs.tools_files, inputs_from_tools, cc_toolchain.all_files]),
# We should take the default PATH passed by Bazel, not that from cc_toolchain
# for Windows, because the PATH under msys2 is different and that is which we need
# for shell commands
Expand Down Expand Up @@ -518,25 +525,15 @@ def _correct_path_variable(env):
env["PATH"] = "$PATH:" + value
return env

def _depset(item):
if item == None:
return depset()
return depset([item])

def _list(item):
if item:
return [item]
return []

def _copy_deps_and_tools(files):
lines = []
lines += _symlink_contents_to_dir("lib", files.libs)
lines += _symlink_contents_to_dir("include", files.headers + files.include_dirs)

if files.tools_files:
lines.append("##mkdirs## $$EXT_BUILD_DEPS$$/bin")
for tool in files.tools_files:
lines.append("##symlink_to_dir## $$EXT_BUILD_ROOT$$/{} $$EXT_BUILD_DEPS$$/bin/".format(tool))
for tool in files.tools_files.to_list():
lines.append("##symlink_to_dir## $$EXT_BUILD_ROOT$$/{} $$EXT_BUILD_DEPS$$/".format(tool))

for ext_dir in files.ext_build_dirs:
lines.append("##symlink_to_dir## $$EXT_BUILD_ROOT$$/{} $$EXT_BUILD_DEPS$$".format(_file_path(ext_dir)))
Expand Down Expand Up @@ -651,7 +648,7 @@ InputFiles = provider(
),
)

def _define_inputs(attrs):
def _define_inputs(ctx, attrs):
cc_infos = []

bazel_headers = []
Expand Down Expand Up @@ -680,31 +677,20 @@ def _define_inputs(attrs):
# but filter out repeating directories
ext_build_dirs = uniq_list_keep_order(ext_build_dirs)

tools_roots = []
tools_files = []
input_files = []
for tool in attrs.tools_deps:
tool_root = detect_root(tool)
tools_roots.append(tool_root)
for file_list in tool.files.to_list():
tools_files += _list(file_list)

for tool in attrs.additional_tools:
for file_list in tool.files.to_list():
tools_files += _list(file_list)

for input in attrs.additional_inputs:
for file_list in input.files.to_list():
input_files += _list(file_list)
input_files += list(file_list)

# These variables are needed for correct C/C++ providers constraction,
tools_files, manifests_from_tools = ctx.resolve_tools(tools = ctx.attr.tools_deps + attrs.additional_tools)
# These variables are needed for correct C/C++ providers construction,
# they should contain all libraries and include directories.
cc_info_merged = cc_common.merge_cc_infos(cc_infos = cc_infos)
return InputFiles(
headers = bazel_headers,
include_dirs = bazel_system_includes,
libs = bazel_libs,
tools_files = tools_roots,
tools_files = tools_files,
deps_compilation_info = cc_info_merged.compilation_context,
deps_linking_info = cc_info_merged.linking_context,
ext_build_dirs = ext_build_dirs,
Expand Down
9 changes: 8 additions & 1 deletion tools/build_defs/native_tools/tool_access.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ def get_make_data(ctx):
def _access_and_expect_label_copied(toolchain_type_, ctx, tool_name):
tool_data = access_tool(toolchain_type_, ctx, tool_name)
if tool_data.target:
# This could be made more efficient by changing the
# toolchain to provide the executable as a target
cmd_file = tool_data
for file in tool_data.target.files.to_list():
if file.path.endswith("/"+tool_data.path):
cmd_file = file
break
return struct(
deps = [tool_data.target],
# as the tool will be copied into tools directory
path = "$EXT_BUILD_DEPS/bin/{}".format(tool_data.path),
path = "$EXT_BUILD_ROOT/{}".format(cmd_file.path)
)
else:
return struct(
Expand Down

0 comments on commit 7c8a534

Please sign in to comment.