2
0
Fork 0
mirror of https://github.com/bazel-contrib/rules_foreign_cc synced 2024-12-04 08:02:31 +00:00
rules_foreign_cc/tools/build_defs/cc_toolchain_util.bzl

393 lines
14 KiB
Python
Raw Normal View History

use tools paths and flags from the toolchain (#7) * take tools paths and flags from the toolchain * correct toolchain data extraction, correct cmake flags and environment * framework script: export variables intended to be used in config script config script can create child processes which could use this variables * cmake: prepend relative [to the execroot] paths with "$EXT_BUILD_ROOT/" unfortunately, CMake does not understand relative paths (sometimes it runs tests for the passed compiler and for that concatenates the passed relative path to some tmp directory created for test) we replace only paths starting with external/ and <top-package-name>/, so we are quite fine to not make a mistake with replacing some not related to paths text; the targets for replacement take very different form (judging by examining toolchain definitions), for instance "-Lexternal/something" * test data for cross compilation for android and with --crosstool_top data files taken from bazel examples and bazel test data; NB one of the interesting things is that we need to specify tools dependency on android sdk and ndk, so that cmake_external shell script have access to these tools * remove cross compilation example with CROSSTOOL for now * add very simple CMake library (test data) * rename target to indicate it is not a test, but example * adjust android example: rename the target to indicate it is not a test but example, add android ndk sources as additional tool for cmake target, (otherwise it is not available in sandbox) use more simple cmake-built library * corrections to the framework & cmake: - correct search for the root directory of the filegroup (when it is not under external) - it was a mistake to specify shared library linker, specify CMAKE_AR instead - unfortunately, I have not discovered how to modify CMake behaviour to pass the custom static flags (rcsD) before "-qc <target>" to the ar linker; option from documentation does not work. To be investigated. For now, do not pass the static cxx linker options, taken from Bazel toolchain.
2018-08-06 13:23:18 +00:00
""" Defines create_linking_info, which wraps passed libraries into CcLinkingInfo
"""
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
use tools paths and flags from the toolchain (#7) * take tools paths and flags from the toolchain * correct toolchain data extraction, correct cmake flags and environment * framework script: export variables intended to be used in config script config script can create child processes which could use this variables * cmake: prepend relative [to the execroot] paths with "$EXT_BUILD_ROOT/" unfortunately, CMake does not understand relative paths (sometimes it runs tests for the passed compiler and for that concatenates the passed relative path to some tmp directory created for test) we replace only paths starting with external/ and <top-package-name>/, so we are quite fine to not make a mistake with replacing some not related to paths text; the targets for replacement take very different form (judging by examining toolchain definitions), for instance "-Lexternal/something" * test data for cross compilation for android and with --crosstool_top data files taken from bazel examples and bazel test data; NB one of the interesting things is that we need to specify tools dependency on android sdk and ndk, so that cmake_external shell script have access to these tools * remove cross compilation example with CROSSTOOL for now * add very simple CMake library (test data) * rename target to indicate it is not a test, but example * adjust android example: rename the target to indicate it is not a test but example, add android ndk sources as additional tool for cmake target, (otherwise it is not available in sandbox) use more simple cmake-built library * corrections to the framework & cmake: - correct search for the root directory of the filegroup (when it is not under external) - it was a mistake to specify shared library linker, specify CMAKE_AR instead - unfortunately, I have not discovered how to modify CMake behaviour to pass the custom static flags (rcsD) before "-qc <target>" to the ar linker; option from documentation does not work. To be investigated. For now, do not pass the static cxx linker options, taken from Bazel toolchain.
2018-08-06 13:23:18 +00:00
load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
"ASSEMBLE_ACTION_NAME",
"CPP_COMPILE_ACTION_NAME",
"CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME",
"CPP_LINK_EXECUTABLE_ACTION_NAME",
"CPP_LINK_STATIC_LIBRARY_ACTION_NAME",
"C_COMPILE_ACTION_NAME",
use tools paths and flags from the toolchain (#7) * take tools paths and flags from the toolchain * correct toolchain data extraction, correct cmake flags and environment * framework script: export variables intended to be used in config script config script can create child processes which could use this variables * cmake: prepend relative [to the execroot] paths with "$EXT_BUILD_ROOT/" unfortunately, CMake does not understand relative paths (sometimes it runs tests for the passed compiler and for that concatenates the passed relative path to some tmp directory created for test) we replace only paths starting with external/ and <top-package-name>/, so we are quite fine to not make a mistake with replacing some not related to paths text; the targets for replacement take very different form (judging by examining toolchain definitions), for instance "-Lexternal/something" * test data for cross compilation for android and with --crosstool_top data files taken from bazel examples and bazel test data; NB one of the interesting things is that we need to specify tools dependency on android sdk and ndk, so that cmake_external shell script have access to these tools * remove cross compilation example with CROSSTOOL for now * add very simple CMake library (test data) * rename target to indicate it is not a test, but example * adjust android example: rename the target to indicate it is not a test but example, add android ndk sources as additional tool for cmake target, (otherwise it is not available in sandbox) use more simple cmake-built library * corrections to the framework & cmake: - correct search for the root directory of the filegroup (when it is not under external) - it was a mistake to specify shared library linker, specify CMAKE_AR instead - unfortunately, I have not discovered how to modify CMake behaviour to pass the custom static flags (rcsD) before "-qc <target>" to the ar linker; option from documentation does not work. To be investigated. For now, do not pass the static cxx linker options, taken from Bazel toolchain.
2018-08-06 13:23:18 +00:00
)
load("@bazel_skylib//lib:collections.bzl", "collections")
use tools paths and flags from the toolchain (#7) * take tools paths and flags from the toolchain * correct toolchain data extraction, correct cmake flags and environment * framework script: export variables intended to be used in config script config script can create child processes which could use this variables * cmake: prepend relative [to the execroot] paths with "$EXT_BUILD_ROOT/" unfortunately, CMake does not understand relative paths (sometimes it runs tests for the passed compiler and for that concatenates the passed relative path to some tmp directory created for test) we replace only paths starting with external/ and <top-package-name>/, so we are quite fine to not make a mistake with replacing some not related to paths text; the targets for replacement take very different form (judging by examining toolchain definitions), for instance "-Lexternal/something" * test data for cross compilation for android and with --crosstool_top data files taken from bazel examples and bazel test data; NB one of the interesting things is that we need to specify tools dependency on android sdk and ndk, so that cmake_external shell script have access to these tools * remove cross compilation example with CROSSTOOL for now * add very simple CMake library (test data) * rename target to indicate it is not a test, but example * adjust android example: rename the target to indicate it is not a test but example, add android ndk sources as additional tool for cmake target, (otherwise it is not available in sandbox) use more simple cmake-built library * corrections to the framework & cmake: - correct search for the root directory of the filegroup (when it is not under external) - it was a mistake to specify shared library linker, specify CMAKE_AR instead - unfortunately, I have not discovered how to modify CMake behaviour to pass the custom static flags (rcsD) before "-qc <target>" to the ar linker; option from documentation does not work. To be investigated. For now, do not pass the static cxx linker options, taken from Bazel toolchain.
2018-08-06 13:23:18 +00:00
LibrariesToLinkInfo = provider(
doc = "Libraries to be wrapped into CcLinkingInfo",
fields = dict(
static_libraries = "Static library files, optional",
shared_libraries = "Shared library files, optional",
interface_libraries = "Interface library files, optional",
),
)
CxxToolsInfo = provider(
doc = "Paths to the C/C++ tools, taken from the toolchain",
fields = dict(
cc = "C compiler",
cxx = "C++ compiler",
cxx_linker_static = "C++ linker to link static library",
cxx_linker_executable = "C++ linker to link executable",
),
)
CxxFlagsInfo = provider(
doc = "Flags for the C/C++ tools, taken from the toolchain",
fields = dict(
cc = "C compiler flags",
cxx = "C++ compiler flags",
cxx_linker_shared = "C++ linker flags when linking shared library",
cxx_linker_static = "C++ linker flags when linking static library",
cxx_linker_executable = "C++ linker flags when linking executable",
assemble = "Assemble flags",
),
)
def _to_list(element):
if element == None:
return []
else:
return [element]
def _to_depset(element):
if element == None:
return depset()
return depset(element)
def _create_libraries_to_link(ctx, files):
libs = []
static_map = _files_map(_filter(files.static_libraries or [], _is_position_independent, True))
pic_static_map = _files_map(_filter(files.static_libraries or [], _is_position_independent, False))
shared_map = _files_map(files.shared_libraries or [])
interface_map = _files_map(files.interface_libraries or [])
names = collections.uniq(static_map.keys() + pic_static_map.keys() + shared_map.keys() + interface_map.keys())
cc_toolchain = find_cpp_toolchain(ctx)
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
for name_ in names:
libs += [cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
static_library = static_map.get(name_),
pic_static_library = pic_static_map.get(name_),
dynamic_library = shared_map.get(name_),
interface_library = interface_map.get(name_),
alwayslink = ctx.attr.alwayslink,
)]
return libs
def _is_position_independent(file):
return file.basename.endswith(".pic.a")
def _filter(list_, predicate, inverse):
result = []
for elem in list_:
check = predicate(elem)
if not inverse and check or inverse and not check:
result += [elem]
return result
def _files_map(files_list):
by_names_map = {}
for file_ in files_list:
name_ = _file_name_no_ext(file_.basename)
value = by_names_map.get(name_)
if value:
fail("Can not have libraries with the same name in the same category")
by_names_map[name_] = file_
return by_names_map
def _build_cc_link_params(
ctx,
user_link_flags,
static_libraries,
dynamic_libraries,
runtime_artifacts):
static_shared = None
static_no_shared = None
if static_libraries != None and len(static_libraries) > 0:
static_shared = cc_common.create_cc_link_params(
ctx = ctx,
user_link_flags = user_link_flags,
libraries_to_link = _to_depset(static_libraries),
)
static_no_shared = cc_common.create_cc_link_params(
ctx = ctx,
libraries_to_link = _to_depset(static_libraries),
)
else:
static_shared = cc_common.create_cc_link_params(
ctx = ctx,
user_link_flags = user_link_flags,
libraries_to_link = _to_depset(dynamic_libraries),
dynamic_libraries_for_runtime = _to_depset(runtime_artifacts),
)
static_no_shared = cc_common.create_cc_link_params(
ctx = ctx,
libraries_to_link = _to_depset(dynamic_libraries),
dynamic_libraries_for_runtime = _to_depset(runtime_artifacts),
)
no_static_shared = None
no_static_no_shared = None
if dynamic_libraries != None and len(dynamic_libraries) > 0:
no_static_shared = cc_common.create_cc_link_params(
ctx = ctx,
user_link_flags = user_link_flags,
libraries_to_link = _to_depset(dynamic_libraries),
dynamic_libraries_for_runtime = _to_depset(runtime_artifacts),
)
no_static_no_shared = cc_common.create_cc_link_params(
ctx = ctx,
libraries_to_link = _to_depset(dynamic_libraries),
dynamic_libraries_for_runtime = _to_depset(runtime_artifacts),
)
else:
no_static_shared = cc_common.create_cc_link_params(
ctx = ctx,
user_link_flags = user_link_flags,
libraries_to_link = _to_depset(static_libraries),
)
no_static_no_shared = cc_common.create_cc_link_params(
ctx = ctx,
libraries_to_link = _to_depset(static_libraries),
)
return {
"static_mode_params_for_dynamic_library": static_shared,
"static_mode_params_for_executable": static_no_shared,
"dynamic_mode_params_for_dynamic_library": no_static_shared,
"dynamic_mode_params_for_executable": no_static_no_shared,
}
def targets_windows(ctx, cc_toolchain):
""" Returns true if build is targeting Windows
Args:
ctx - rule context
cc_toolchain - optional - Cc toolchain
"""
toolchain = cc_toolchain if cc_toolchain else find_cpp_toolchain(ctx)
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
return cc_common.is_enabled(
feature_configuration = feature_configuration,
feature_name = "targets_windows",
)
def create_linking_info(ctx, user_link_flags, files):
""" Creates CcLinkingInfo for the passed user link options and libraries.
Args:
ctx - rule context
user_link_flags - (list of strings) link optins, provided by user
files - (LibrariesToLink) provider with the library files
"""
return cc_common.create_linking_context(
user_link_flags = user_link_flags,
libraries_to_link = _create_libraries_to_link(ctx, files),
)
def get_env_vars(ctx):
cc_toolchain = find_cpp_toolchain(ctx)
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
copts = ctx.attr.copts if hasattr(ctx.attr, "copts") else []
vars = dict()
for action_name in [C_COMPILE_ACTION_NAME, CPP_LINK_STATIC_LIBRARY_ACTION_NAME, CPP_LINK_EXECUTABLE_ACTION_NAME]:
vars.update(cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = action_name,
variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
user_compile_flags = copts,
),
))
return vars
def is_debug_mode(ctx):
# see workspace_definitions.bzl
return str(True) == ctx.attr._is_debug[config_common.FeatureFlagInfo].value
def get_tools_info(ctx):
""" Takes information about tools paths from cc_toolchain, returns CxxToolsInfo
Args:
ctx - rule context
"""
cc_toolchain = find_cpp_toolchain(ctx)
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
return CxxToolsInfo(
cc = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = C_COMPILE_ACTION_NAME,
),
cxx = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = CPP_COMPILE_ACTION_NAME,
),
cxx_linker_static = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
),
cxx_linker_executable = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME,
),
)
def get_flags_info(ctx):
""" Takes information about flags from cc_toolchain, returns CxxFlagsInfo
Args:
ctx - rule context
"""
cc_toolchain_ = find_cpp_toolchain(ctx)
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain_,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
copts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.conlyopts) or []
cxxopts = (ctx.fragments.cpp.copts + ctx.fragments.cpp.cxxopts) or []
linkopts = ctx.fragments.cpp.linkopts or []
flags = CxxFlagsInfo(
cc = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = C_COMPILE_ACTION_NAME,
variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain_,
),
),
cxx = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_COMPILE_ACTION_NAME,
variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain_,
add_legacy_cxx_options = True,
),
),
cxx_linker_shared = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_LINK_DYNAMIC_LIBRARY_ACTION_NAME,
variables = cc_common.create_link_variables(
cc_toolchain = cc_toolchain_,
feature_configuration = feature_configuration,
is_using_linker = True,
is_linking_dynamic_library = True,
),
),
cxx_linker_static = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
variables = cc_common.create_link_variables(
cc_toolchain = cc_toolchain_,
feature_configuration = feature_configuration,
is_using_linker = False,
is_linking_dynamic_library = False,
),
),
cxx_linker_executable = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_LINK_EXECUTABLE_ACTION_NAME,
variables = cc_common.create_link_variables(
cc_toolchain = cc_toolchain_,
feature_configuration = feature_configuration,
is_using_linker = True,
is_linking_dynamic_library = False,
),
),
assemble = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = ASSEMBLE_ACTION_NAME,
variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain_,
),
),
)
return CxxFlagsInfo(
cc = _add_if_needed(flags.cc, copts),
cxx = _add_if_needed(flags.cxx, cxxopts),
cxx_linker_shared = _add_if_needed(flags.cxx_linker_shared, linkopts),
cxx_linker_static = flags.cxx_linker_static,
cxx_linker_executable = _add_if_needed(flags.cxx_linker_executable, linkopts),
assemble = _add_if_needed(flags.assemble, copts),
)
def _add_if_needed(arr, add_arr):
filtered = []
for to_add in add_arr:
found = False
for existing in arr:
if existing == to_add:
found = True
if not found:
filtered += [to_add]
return arr + filtered
def absolutize_path_in_str(workspace_name, root_str, text, force = False):
""" Replaces relative paths in [the middle of] 'text', prepending them with 'root_str'.
If there is nothing to replace, returns the 'text'.
We only will replace relative paths starting with either 'external/' or '<top-package-name>/',
because we only want to point with absolute paths to external repositories or inside our
current workspace. (And also to limit the possibility of error with such not exact replacing.)
Args:
workspace_name - workspace name
text - the text to do replacement in
root_str - the text to prepend to the found relative path
"""
new_text = _prefix(text, "external/", root_str)
if new_text == text:
new_text = _prefix(text, workspace_name + "/", root_str)
# absolutize relative by adding our working directory
# this works because we ru on windows under msys now
if force and new_text == text and not text.startswith("/"):
new_text = root_str + "/" + text
return new_text
def _prefix(text, from_str, prefix):
text = text.replace('"', '\\"')
(before, middle, after) = text.partition(from_str)
if not middle or before.endswith("/"):
return text
return before + prefix + middle + after
def _file_name_no_ext(basename):
(before, separator, after) = basename.partition(".")
return before