project_options icon indicating copy to clipboard operation
project_options copied to clipboard

Add toolchain support

Open abeimler opened this issue 1 year ago • 2 comments

Add function to setup -DCMAKE_TOOLCHAIN_FILE/-DVCPKG_TOOLCHAIN_FILE and include() toolchain via -DOPT_TARGET_TRIPLET:STRING=x64-mingw-dynamic option.

Example:

project_options(
   TARGET_TRIPLET "x64-mingw-dynamic"
)

or with cmake arguments:

# for custom toolchains
cmake ... -DOPT_TOOLCHAIN_FILE:STRING="..." -DOPT_TARGET_TRIPLET:STRING="..."
## setup vcpkg and more

# use project_options toolchains
cmake ... -DOPT_TARGET_TRIPLET:STRING="..."
## setup vcpkg and more

# use vcpkg arguments
cmake ... -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE="..." -DVCPKG_TARGET_TRIPLET="..."
## read vcpkg triplet and set OPT_TARGET_TRIPLET etc.
  • setup variables for cmake/vcpkg: VCPKG_HOST_TRIPLET, VCPKG_TRIPLET and DVCPKG_CHAINLOAD_TOOLCHAIN_FILE - https://github.com/microsoft/vcpkg/blob/master/docs/users/triplets.md
  • Possible Triplets for -DOPT_TARGET_TRIPLET:
    • x64-mingw-dynamic, x64-mingw-static - for Windows builds
    • x86-mingw-dynamic, x86-mingw-static - for Windows builds (32-Bit) (optional?)
    • arm64-linux, arm-linux or arm64-windows, arm-windows - for ARM builds
    • wasm32-emscripten - for WebASM/Emscripten (optional)
    • more (optional)
  • setup cmake-toolchain
    • cmake toolchain files included (batteries included)
    • use pre-defined cmake toolchain files
      • examples from - https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html
      • https://github.com/staticlibs/cmake/tree/master/toolchains
      • write own toolchains - https://github.com/aminya/Toolchain, https://github.com/abeimler/simple-cppbuilder/tree/main/cmake
      • https://github.com/ruslo/polly (outdated)
      • https://github.com/onqtam/awesome-cmake#toolchains=
      • https://github.com/dockcross/dockcross
  • checks for ENV variables and paths, print some helpful messages if something is missing (optional)
    • For example, when building for Android you need to set -DANDROID_NDK and -DANDROID_ABI
    • Maybe add some (default) extra properties: like for example emscript, set_target_properties(${target} PROPERTIES LINK_FLAGS "-s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s USE_PTHREADS=1 --bind")

Notes

  • Cross compiler must setup by user
  • Maybe addition arguments must be pass for cross-compiler root path or other paths like Android NDK, emscripten SDK (emsdk), ...

Upvote & Fund

  • I am using Polar.sh so you can upvote and help fund this issue. The funding is received once the issue is completed & confirmed by you.

  • Thank you in advance for helping prioritize & fund our backlog.

Fund with Polar

abeimler avatar Jul 21 '22 16:07 abeimler

I have taken the automated approach to the toolchains; the user should not need to specify the toolchains manually. We detect the proper toolchain based on the compiler settings and the target system.

For example, for the msvc toolchain: https://github.com/aminya/project_options/blob/370900da1d9df08146c1690a2a2c978122979716/src/VCEnvironment.cmake#L58-L64

aminya avatar Jul 21 '22 17:07 aminya

We already detect MinGW and apply some fixes for it for VCPKG. You can use the same function to detect MinGW and include its toolchain.

https://github.com/aminya/project_options/blob/370900da1d9df08146c1690a2a2c978122979716/src/MinGW.cmake#L4

aminya avatar Oct 19 '22 06:10 aminya