Specify platform for iOS simulator in bazel and compile C++ code


I am learning Bazel and now I want to cross-compile some C++ code for both Mac and iOS. As a first step I want to compile my C++ code for iOS simulator using Bazel and my custom made rules.

I currently have this in tutorial3/toolchain/BUILD.bazel:

load("//tutorial3/toolchain:toolchain.bzl","cxx_toolchain")

platform(
    name = "iphone_simulator",
    constraint_values = [
        "@platforms//os:ios",
        "@platforms//cpu:x86_64",
    ],
)

toolchain_type(
    name = "cxx_toolchain_type",
    visibility = ["//visibility:public"],
)

cxx_toolchain(
    name = "mac_compiler_toolchain",
    compiler = "clang++",
    archiver = "ar",
)

cxx_toolchain(
    name = "ios_sim_compiler_toolchain",
    compiler = "xcrun -sdk iphonesimulator clang++ -arch x86_64",
    archiver = "xcrun -sdk iphonesimulator ar",
)

toolchain(
    name = "mac_toolchain",
    exec_compatible_with = [
        "@platforms//os:osx"
    ],
    target_compatible_with = [
        "@platforms//os:osx",
    ],
    toolchain = ":mac_compiler_toolchain",
    toolchain_type = ":cxx_toolchain_type",
)

toolchain(
    name = "iphone_simulator_toolchain",
    target_compatible_with = [
        ":iphone_simulator"
    ],
    toolchain = ":ios_sim_compiler_toolchain",
    toolchain_type = ":cxx_toolchain_type",
)

When I run this command

bazel build tutorial3/lib:func2 --subcommands --toolchain_resolution_debug='//tutorial3/toolchain:cxx_toolchain_type' --platforms=//tutorial3/toolchain:iphone_simulator

I get this error:

ERROR: /Users/erikbylow/Code/Git/BazelTutorial/tutorial3/lib/BUILD.bazel:15:19: While resolving toolchains for target //tutorial3/lib:func2 (790fdfb): No matching toolchains found for types //tutorial3/toolchain:cxx_toolchain_type.
To debug, rerun with --toolchain_resolution_debug='//tutorial3/toolchain:cxx_toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
ERROR: Analysis of target '//tutorial3/lib:func2' failed; build aborted
INFO: Elapsed time: 0.078s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

I have managed to build the object files successfully with xcrun -sdk iphonesimulator clang++ -arch x86_64 -c tutorial3/lib/func1.cpp -o file1.o. So I hope I can just update the rules a bit and pass the correct arguments from the BUILD file depending on the platform.

I am quite new to this so this might be quite wrong.

At the moment it stops already before building has began so that needs to be solved first.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img