toolchain files should be able to add information specific to try_compile
I am working with embedded projects, particularly with newlib. For a working executable you typically provide some function this library expects, just trying to compile a program will give you errors of missing symbols. see #21957 for an example of the output.
However, it comes with a bare "no system" library that allows compilation, with gcc + newlib the argument -specs=nosys.specs
will allow executable's to be build.
But you dont want this switch globally enabled, it should just be used by try_compile.
What would be needed is some variable that can be set in the toolchain file,
instructing how try_compile can build an executable, like CMAKE_TRY_COMPILE_EXE_LINKER_FLAGS
, potentially CMAKE_TRY_COMPILE_<LANG>_FLAGS
aswell.
Example toolchain file, remember that the tests will now work, but the executables might be broken.
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
set(CMAKE_TOOLCHAIN_REMOVE_MPATH ${CMAKE_CURRENT_LIST_DIR})
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_SYSTEM_VERSION )
# heres the magic
set(CMAKE_EXE_LINKER_FLAGS_INIT "-specs=nosys.specs")
# and this is not needed anymore
# set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_INSTALL_SO_NO_EXE 0)
# Generic paths and search settings
set(CMAKE_PROGRAM_PATH "${PACKAGE_PREFIX_DIR}/bin")
set(CMAKE_SYSROOT "${PACKAGE_PREFIX_DIR}/arm-none-eabi")
set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(ENV{PKG_CONFIG_PATH} "")
set(ENV{PKG_CONFIG_LIBDIR} "${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} "${CMAKE_SYSROOT}")
# Compiler binaries
set(CMAKE_C_COMPILER "${PACKAGE_PREFIX_DIR}/bin/arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "${PACKAGE_PREFIX_DIR}/bin/arm-none-eabi-g++")
set(CMAKE_Fortran_COMPILER "${PACKAGE_PREFIX_DIR}/bin/arm-none-eabi-gfortran")
set(CMAKE_OBJC_COMPILER "${PACKAGE_PREFIX_DIR}/bin/arm-none-eabi-gobjc")
set(CMAKE_OBJCXX_COMPILER "${PACKAGE_PREFIX_DIR}/bin/arm-none-eabi-gobjc++")