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/cmake.bzl

169 lines
5.8 KiB
Python
Raw Normal View History

""" Defines the rule for building external library with CMake
"""
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "os_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(
"//tools/build_defs:cc_toolchain_util.bzl",
"get_flags_info",
"get_tools_info",
"is_debug_mode",
)
load(
"//tools/build_defs:detect_root.bzl",
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
"detect_root",
)
load(
"//tools/build_defs:framework.bzl",
"CC_EXTERNAL_RULE_ATTRIBUTES",
"cc_external_rule_impl",
"create_attrs",
)
load(
"//tools/build_defs/native_tools:tool_access.bzl",
"get_cmake_data",
"get_make_data",
2021-01-22 14:17:24 +00:00
"get_ninja_data",
)
load(":cmake_script.bzl", "create_cmake_script")
def _cmake_external(ctx):
cmake_data = get_cmake_data(ctx)
make_data = get_make_data(ctx)
tools_deps = ctx.attr.tools_deps + cmake_data.deps + make_data.deps
ninja_data = get_ninja_data(ctx)
make_commands = ctx.attr.make_commands
if _uses_ninja(ctx.attr.make_commands):
tools_deps += ninja_data.deps
make_commands = [command.replace("ninja", ninja_data.path) for command in make_commands]
attrs = create_attrs(
ctx.attr,
configure_name = "CMake",
create_configure_script = _create_configure_script,
postfix_script = "##copy_dir_contents_to_dir## $$BUILD_TMPDIR$$/$$INSTALL_PREFIX$$ $$INSTALLDIR$$\n" + ctx.attr.postfix_script,
tools_deps = tools_deps,
cmake_path = cmake_data.path,
ninja_path = ninja_data.path,
make_path = make_data.path,
make_commands = make_commands,
)
return cc_external_rule_impl(ctx, attrs)
def _uses_ninja(make_commands):
for command in make_commands:
(before, separator, after) = command.partition(" ")
if before == "ninja":
return True
return False
def _create_configure_script(configureParameters):
ctx = configureParameters.ctx
inputs = configureParameters.inputs
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
root = detect_root(ctx.attr.lib_source)
if len(ctx.attr.working_directory) > 0:
root = root + "/" + ctx.attr.working_directory
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
tools = get_tools_info(ctx)
# CMake will replace <TARGET> with the actual output file
flags = get_flags_info(ctx, "<TARGET>")
no_toolchain_file = ctx.attr.cache_entries.get("CMAKE_TOOLCHAIN_FILE") or not ctx.attr.generate_crosstool_file
define_install_prefix = "export INSTALL_PREFIX=\"" + _get_install_prefix(ctx) + "\"\n"
configure_script = create_cmake_script(
workspace_name = ctx.workspace_name,
# as default, pass execution OS as target OS
target_os = os_name(ctx),
cmake_path = configureParameters.attrs.cmake_path,
tools = tools,
flags = flags,
install_prefix = "$$INSTALL_PREFIX$$",
root = root,
no_toolchain_file = no_toolchain_file,
user_cache = dict(ctx.attr.cache_entries),
user_env = dict(ctx.attr.env_vars),
options = ctx.attr.cmake_options,
include_dirs = inputs.include_dirs,
is_debug_mode = is_debug_mode(ctx),
)
return define_install_prefix + configure_script
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
def _get_install_prefix(ctx):
if ctx.attr.install_prefix:
return ctx.attr.install_prefix
if ctx.attr.lib_name:
return ctx.attr.lib_name
return ctx.attr.name
def _attrs():
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
attrs = dict(CC_EXTERNAL_RULE_ATTRIBUTES)
attrs.update({
"cache_entries": attr.string_dict(
doc = (
"CMake cache entries to initialize (they will be passed with -Dkey=value) " +
"Values, defined by the toolchain, will be joined with the values, passed here. " +
"(Toolchain values come first)"
),
mandatory = False,
default = {},
),
"cmake_options": attr.string_list(
doc = "Other CMake options",
mandatory = False,
default = [],
),
"env_vars": attr.string_dict(
doc = (
"CMake environment variable values to join with toolchain-defined. " +
"For example, additional CXXFLAGS."
),
mandatory = False,
default = {},
),
"generate_crosstool_file": attr.bool(
doc = (
"When True, CMake crosstool file will be generated from the toolchain values, " +
"provided cache-entries and env_vars (some values will still be passed as -Dkey=value " +
"and environment variables). " +
"If CMAKE_TOOLCHAIN_FILE cache entry is passed, specified crosstool file will be used " +
"When using this option, it makes sense to specify CMAKE_SYSTEM_NAME in the " +
"cache_entries - the rule makes only a poor guess about the target system, " +
"it is better to specify it manually."
),
mandatory = False,
default = False,
),
"install_prefix": attr.string(
doc = "Relative install prefix to be passed to CMake in -DCMAKE_INSTALL_PREFIX",
mandatory = False,
),
"working_directory": attr.string(
doc = (
"Working directory, with the main CMakeLists.txt " +
"(otherwise, the top directory of the lib_source label files is used.)"
),
mandatory = False,
default = "",
),
})
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
return attrs
cmake_external = rule(
doc = "Rule for building external library with CMake.",
attrs = _attrs(),
fragments = ["cpp"],
output_to_genfiles = True,
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
implementation = _cmake_external,
toolchains = [
"@rules_foreign_cc//tools/build_defs:cmake_toolchain",
"@rules_foreign_cc//tools/build_defs:ninja_toolchain",
"@rules_foreign_cc//tools/build_defs:make_toolchain",
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],
)