Skip to content

When crosscompiling, build host tools externally. #5170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

alexreinking
Copy link
Member

@alexreinking alexreinking commented Aug 9, 2020

This PR updates CMake to build the host tools (currently binary2cpp and build_halide_h) externally with a given toolchain (specified by the new CMake variable Halide_HOST_TOOLCHAIN_FILE) when the main project is being cross-compiled.

There are some awkward contortions needed to avoid changing the project to an outright superbuild, and I don't see how we can support cross-OS builds (eg. build on Windows, deploy on Linux or vice-versa) without making that change.

We need separate directories for host build tools and end-user/generator build tools. I would vote to move the host tools into src/tools and keep /tools for user stuff. This makes it easier to organize the CMakeLists.txt.

attn: @shoaibkamil -- would appreciate your help testing this since you were the one interested in cross-compiling Halide.

Fixes #5040

@alexreinking
Copy link
Member Author

CMake failures are trunk failures. Make failures are because I haven't updated the Makefile.

@steven-johnson
Copy link
Contributor

Alternate thought: since we now require Python for the python bindings and some apps anyway, maybe it's time to consider requiring it for building in general; both binary2cpp and build_halide_h would be trivial to write in Python.

@alexreinking alexreinking added the build Issues related to building Halide and with CI label Aug 11, 2020
@alexreinking alexreinking self-assigned this Aug 11, 2020
@alexreinking alexreinking force-pushed the cmake/crosscompile branch 2 times, most recently from 927f7ab to 97fcbeb Compare August 11, 2020 09:27
Copy link
Contributor

@steven-johnson steven-johnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits.

##

if (CMAKE_CROSSCOMPILING)
# Note: using CMAKE_EXECUTABLE_SUFFIX breaks cross-OS builds involving Windows.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused by this comment -- it breaks the builds but we use it anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't break any existing scenarios, it just isn't a supported scenario without going full superbuild. I can make this clearer.

if (CMAKE_CROSSCOMPILING)
# Note: using CMAKE_EXECUTABLE_SUFFIX breaks cross-OS builds involving Windows.
include(ExternalProject)
ExternalProject_Add(host-tools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super-duper-nit: I find it hard to quickly read the keywords-vs-arg-values here; I wonder if it would be more obvious with indentation like:

    ExternalProject_Add(host-tools
                        BUILD_BYPRODUCTS
                              "<INSTALL_DIR>/bin/build_halide_h${CMAKE_EXECUTABLE_SUFFIX}"
                              "<INSTALL_DIR>/bin/binary2cpp${CMAKE_EXECUTABLE_SUFFIX}"
                        SOURCE_DIR 
                              ${CMAKE_CURRENT_LIST_DIR}/tools
                        INSTALL_DIR 
                              ${CMAKE_CURRENT_BINARY_DIR}/host-tools
                        CMAKE_ARGS
                              -D CMAKE_TOOLCHAIN_FILE=${Halide_HOST_TOOLCHAIN_FILE}
                              -D CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                              -D CMAKE_INSTALL_PREFIX=<INSTALL_DIR>
                        BUILD_ALWAYS 
                              YES)

Copy link
Member Author

@alexreinking alexreinking Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The formatter in CLion is not very smart and it tramples such indentation 😞 I could add some blank lines to make it a bit more readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know there existed an autoformatter for CMake. Does it exist anywhere other than CLion?

Copy link
Member Author

@alexreinking alexreinking Aug 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I'm aware of. There are two open-source CMake formatters I'm aware of. I've tried both. One, cmake-format does a bad job, the linter is even worse. The other, gersemi, is not very configurable, is in pre-alpha state and led to a huge diff.

set_target_properties(binary2cpp
PROPERTIES
IMPORTED_LOCATION "${INSTALL_DIR}/bin/binary2cpp${CMAKE_EXECUTABLE_SUFFIX}"
CROSSCOMPILING_EMULATOR "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we don't explicitly set CROSSCOMPILING_EMULATOR? Is empty not the default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure how CMake will treat it given that it's an imported target, but for non-imported executable targets, it wants CROSSCOMPILING_EMULATOR to be defined if the target is used in add_custom_command. So I'm being defensive here.

project(host-tools)

# Require standard C++14
set(CMAKE_CXX_STANDARD 14)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...don't we actually only require C++11?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC when we went through and looked at our list of supported compilers (GCC 5+, Clang 9+, MSVC 2017+), C++14 was the GCD among them.

@steven-johnson
Copy link
Contributor

Build looks clean. Ready to review/land?

@alexreinking
Copy link
Member Author

Build looks clean. Ready to review/land?

I wanted some input from @shoaibkamil to see if this works with his cross compiling set-up.

@shoaibkamil
Copy link
Contributor

I wanted some input from @shoaibkamil to see if this works with his cross compiling set-up.

Will give it a try soon and let you know if this works to create a Windows ARM64 Halide.lib when compiling on a Windows x64 machine.

@alexreinking
Copy link
Member Author

alexreinking commented Aug 13, 2020

Thanks Shoiab -- note that you should not set CMAKE_CROSSCOMPILING yourself when doing this. Just use the appropriate toolchain files via the CMAKE_TOOLCHAIN_FILE (for the target) and Halide_HOST_TOOLCHAIN_FILE (for the host) variables.

@steven-johnson
Copy link
Contributor

Is this PR still an active concern?

@shoaibkamil
Copy link
Contributor

Yes, this is still important. I'm in the middle of rebuilding my windows dev environment so haven't been able to test yet. Will do it ASAP

@steven-johnson
Copy link
Contributor

Will do it ASAP

Not urgent, just wanted to know the status since this has been dormant for a while

@alexreinking alexreinking modified the milestone: v12.0.0 Oct 19, 2020
@alexreinking alexreinking deleted the cmake/crosscompile branch June 3, 2021 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Issues related to building Halide and with CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support cross-compiling Halide
3 participants