Skip to content

Commit

Permalink
Fix cross compiling for android on macOS
Browse files Browse the repository at this point in the history
The way rules_foreign_cc builds cross compilation targets isn't exactly
what cmake expects. Because of this when targeting android on macOS,
some macOS specific linker flags are added to cmake's test compile
invocations, leading to failures. This works around this issue by
setting some more variables to trick cmake into only using the expected
settings.
  • Loading branch information
keith committed Jan 6, 2023
1 parent f1ac5b4 commit 215bae4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions foreign_cc/cmake.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ load("//foreign_cc/private:transitions.bzl", "foreign_cc_rule_variant")
load(
"//foreign_cc/private/framework:platform.bzl",
"os_name",
"target_os_name",
)
load(
"//toolchains/native_tools:tool_access.bzl",
Expand Down Expand Up @@ -251,6 +252,7 @@ def _create_configure_script(configureParameters):

configure_script = create_cmake_script(
workspace_name = ctx.workspace_name,
target_os = target_os_name(ctx),
generator = attrs.generator,
cmake_path = attrs.cmake_path,
tools = tools,
Expand Down
10 changes: 10 additions & 0 deletions foreign_cc/private/cmake_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def _escape_dquote_bash_crosstool(text):

def create_cmake_script(
workspace_name,
target_os,
generator,
cmake_path,
tools,
Expand All @@ -37,6 +38,7 @@ def create_cmake_script(
Args:
workspace_name: current workspace name
target_os: The target OS for the build
generator: The generator target for cmake to use
cmake_path: The path to the cmake executable
tools: cc_toolchain tools (CxxToolsInfo)
Expand Down Expand Up @@ -91,6 +93,14 @@ def create_cmake_script(
if not params.cache.get("CMAKE_RANLIB"):
params.cache.update({"CMAKE_RANLIB": ""})

# Avoid cmake passing wrong linker flags when targeting android on macOS
# https://github.com/bazelbuild/rules_foreign_cc/issues/289
if target_os == "android":
params.cache.update({
"ANDROID": "YES",
"CMAKE_SYSTEM_NAME": "Linux",
})

set_env_vars = [
"export {}=\"{}\"".format(key, _escape_dquote_bash(params.env[key]))
for key in params.env
Expand Down
1 change: 1 addition & 0 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
cfg = "exec",
default = Label("@rules_foreign_cc//foreign_cc/private/framework:platform_info"),
),
"_android_constraint": attr.label(default = Label("@platforms//os:android")),
}

# A list of common fragments required by rules using this framework
Expand Down
19 changes: 19 additions & 0 deletions foreign_cc/private/framework/platform.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,22 @@ def os_name(ctx):
return "unknown"

return platform_info[ForeignCcPlatformInfo].os

def target_os_name(ctx):
"""A helper function for getting the target operating system name based on the constraints
Args:
ctx (ctx): The current rule's context object
Returns:
str: The string of the current platform
"""

android_constraint = ctx.attr._android_constraint[platform_common.ConstraintValueInfo]
operating_systems = ["android"]
for os in operating_systems:
constraint = getattr(ctx.attr, "_{}_constraint".format(os))
if constraint and ctx.target_platform_has_constraint(constraint[platform_common.ConstraintValueInfo]):
return os

return "unknown"

0 comments on commit 215bae4

Please sign in to comment.