Skip to content

Investigate making CMake cross compilation not require host tools #4617

Closed

Description

@ScottTodd

Background

#4584 removed our custom CMake cross compilation code, which was implicitly compiling host tools (iree-translate, generate_cc_embed_data, iree-tblgen, etc.) used for the target build. Now a host build must be performed explicitly, or a binary distribution must be used.

One unfortunate consequence of this is that runtime-only builds for the Java bindings (and likely TFLite bindings too) still require at least the flatcc cli tool for compiling our schema flatbuffer files using the flatbuffer_c_library build helpers.

In general, trying to build all required tools and artifacts for a given user application in a single build step seems like a fool's errand, particularly when the application requires cross compilation. On the other end of the spectrum, requiring users to precisely build different parts of a project using different build systems (CMake host -> CMake target -> Gradle) will just lead to difficulty integrating.

flatcc

While we can build the CLI program for the target by adjusting these settings:
https://github.com/google/iree/blob/a66ff82b03006a4fdd646075eaceb4240eaf909c/build_tools/third_party/flatcc/CMakeLists.txt#L58-L62
https://github.com/google/iree/blob/a66ff82b03006a4fdd646075eaceb4240eaf909c/build_tools/cmake/iree_copts.cmake#L379-L383
https://github.com/google/iree/blob/a66ff82b03006a4fdd646075eaceb4240eaf909c/build_tools/cmake/flatbuffer_c_library.cmake#L98
we can't (at least as far as I'm aware) invoke the target program (e.g. an Android/Linux binary) on the host (e.g. a Windows machine) during a build step.

Solutions

I think for third party tools we could use FIND_PROGRAM() (if installed) and/or vcpkg to help with finding host tools and not building them ourselves. For in-tree tools like generate_cc_embed_data I think our only options are:

  1. Our own binary distributions
  2. An explicit host build (CMake invocation + path setup)
  3. An implicit host build (the magic we had before)
  4. Check in generated files where possible (like with this tablegen'd file)

Hopefully this mostly affects IREE developers only, since in the case of Android we can publish AAR files and users can skip all this CMake stuff :P. Maybe that's the answer to this? We could write a script to invoke CMake twice and generate an AAR using https://docs.gradle.org/current/userguide/command_line_interface.html then use that in any of our Android samples.

cc @KoolJBlack , @not-jenni

Activity

  1. added
    infrastructure
    platform/android 🤖
    on Jan 27, 2021
  2. stellaraccident commented on Jan 27, 2021

    @stellaraccident
    Collaborator

    I'm reworking that now and will just enable building the compiler for the runtime package, but it's pretty non-ideal.

  3. benvanik commented on Jan 27, 2021

    @benvanik
    Collaborator

    I think it's fine to require two build invocations - one for host and one for target - when building the runtime. The invocation that builds flatcc should take literally seconds at most and be done once, and as you say is something we can automate with vcpkg to hide it from most users. For a majority of people cross-compiling this is the kind of thing they need to do already (especially if they are using protobuf/flatbuffers/etc from source like we are, or pngcrush, or crunch, etc etc).

    Falling back to PATH copies is a bit risky as there's no guarantee it'll match versions and won't work for something we build ourselves (like the embed data stuff), but flatcc isn't changing much so it's probably fine so long as it is a shared path with the vcpkg (not a big conditional cmake block that is only exercised in that one case).

  4. benvanik commented on Jan 27, 2021

    @benvanik
    Collaborator

    (the alternative is that we check in binaries, which is the extreme of non-ideal - needing to invoke your compiler for each platform is something that comes with the territory of cross-compiling)

  5. benvanik commented on Jan 27, 2021

    @benvanik
    Collaborator

    I'm reworking that now and will just enable building the compiler for the runtime package, but it's pretty non-ideal.

    To clarify, the "flatbuffer compiler" not the "iree compiler" correct? We should literally only be issuing a cmake build for the --target flatcc_cli.

  6. stellaraccident commented on Jan 27, 2021

    @stellaraccident
    Collaborator

    I'm reworking that now and will just enable building the compiler for the runtime package, but it's pretty non-ideal.

    To clarify, the "flatbuffer compiler" not the "iree compiler" correct? We should literally only be issuing a cmake build for the --target flatcc_cli.

    No - I think it is more broken than that. See the log: https://pipelines.actions.githubusercontent.com/SaXEyjchYvnWH9Rq3qeZEPLXtO1Hv1A8b0nd0VWR5igNiKNAhf/_apis/pipelines/1/runs/42323/signedlogcontent/9?urlExpires=2021-01-27T00%3A13%3A25.9225710Z&urlSigningMethod=HMACV1&urlSignature=wko37FpEfWFKj6bkDFYUGzckVMJC4bkXi%2BiQrh5Nd1A%3D

    Some stuff getting hauled in and failing CMake configure:

    2021-01-26T22:26:20.9066420Z CMake Error at build_tools/cmake/iree_bytecode_module.cmake:71 (add_custom_command):
    2021-01-26T22:26:20.9067312Z   Error evaluating generator expression:
    2021-01-26T22:26:20.9067746Z 
    2021-01-26T22:26:20.9068359Z     $<TARGET_FILE:iree-translate>
    2021-01-26T22:26:20.9068737Z 
    2021-01-26T22:26:20.9069328Z   No target "iree-translate"
    

    With these options:

          f"-DCMAKE_BUILD_TYPE=Release",
          # TODO: Should be able to build runtime without compiler if resolved:
          # https://github.com/google/iree/issues/4617
          f"-DIREE_BUILD_COMPILER=OFF",
          f"-DIREE_BUILD_PYTHON_BINDINGS=ON",
          f"-DIREE_BUILD_SAMPLES=OFF",
    
  7. ScottTodd commented on Jan 27, 2021

    @ScottTodd
    MemberAuthor

    This is what we settled on as a workaround for right now in building the Java bindings

    https://github.com/not-jenni/iree/blob/937ad5a9601becbd9dac3b6c0a7c6d3b2798feaf/experimental/bindings/java/build.gradle#L26-L42 (on #4608)

            externalNativeBuild {
                cmake {
                    arguments '-DIREE_BUILD_EXPERIMENTAL_JAVA_BINDINGS=ON',
                            ...
                            // Run the commands at
                            // https://google.github.io/iree/get-started/getting-started-android-cmake#configure-and-build
                            // and update this flag to point to iree-build-host/install/.
                            '-DIREE_HOST_BINARY_ROOT='
    

    That's messy for developers and basically a non-starter for users :p

  8. benvanik commented on Jan 27, 2021

    @benvanik
    Collaborator

    ok, sounds like there are a bunch of issues - but the end goal should be that if a user is building the runtime from source the only host targets they need to build are flatcc_cli and the embed data binary (and maybe iree-dump-module & co, which are runtime data tools). We can have an install target for that so there's just one cmake build to do, and we can have vcpkg do that automatically when installing iree[runtime-tools] or whatnot.

  9. stellaraccident commented on Jan 27, 2021

    @stellaraccident
    Collaborator

    I "fixed" my problem by building with -DIREE_BUILD_TESTS=OFF. Which then exposed another problem in that we have non-test depends on benchmark. Required a small patch to the top-level to configure benchmark regardless.

  10. benvanik commented on Jan 27, 2021

    @benvanik
    Collaborator

    It may be useful cleanup to split out IREE_BUILD_COMPILER_TESTS and IREE_BUILD_RUNTIME_TESTS - lit tests in the former, iree-check-module/etc in the former.

  11. stellaraccident commented on Jan 27, 2021

    @stellaraccident
    Collaborator

    It may be useful cleanup to split out IREE_BUILD_COMPILER_TESTS and IREE_BUILD_RUNTIME_TESTS - lit tests in the former, iree-check-module/etc in the former.

    Yeah, next on my list.

  12. ScottTodd commented on Jan 27, 2021

    @ScottTodd
    MemberAuthor

    I think two things got conflated here.

    IREE's runtime (including tests), when built for any platform, should not require compiler tools like iree-translate. #4627 should fix that.
    IREE's runtime, when built for any platform, may still require host tools like flatcc's CLI and generate_cc_embed_data.

  13. ScottTodd commented on Apr 27, 2022

    @ScottTodd
    MemberAuthor

    I think this issue is stale/obsolete now. Host tools like flatcc and generate_c_embed_data can still be required for runtime builds, and we have a reasonable separation between compiler and runtime tools/builds now. We also have a few functional examples of building for weirder platforms that are relatively stable (Android, RISC-V/bare-metal, Web/Emscripten).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    infrastructureplatform/android 🤖

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions