Skip to content

Commit

Permalink
wrappers: add CMake toolchain files
Browse files Browse the repository at this point in the history
Those toolchain files greatly simplify configuring a project
to use the llvm-mingw toolchain.
  • Loading branch information
nolange committed Aug 11, 2024
1 parent 462aee2 commit 79ce480
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions install-wrappers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ if [ -n "$EXEEXT" ]; then
else
CTW_SUFFIX=.sh
fi
toolchainfile_path=share/cmake; toolchainfile_rev=../..
mkdir -p "$PREFIX/$toolchainfile_path"
sed "s,@RELPATH@,$toolchainfile_rev,g" wrappers/llvm-mingw_toolchainfile.cmake.in >"$PREFIX/$toolchainfile_path/llvm-mingw_toolchainfile.cmake"
for arch in $ARCHS; do
sed "s,@ARCH@,${arch},g" wrappers/llvm-mingw_toolchainfile_arch.cmake.in >"$PREFIX/$toolchainfile_path/${arch}-w64-mingw32_toolchainfile.cmake"
done
cd "$PREFIX/bin"
for arch in $ARCHS; do
for target_os in $TARGET_OSES; do
Expand Down
84 changes: 84 additions & 0 deletions wrappers/llvm-mingw_toolchainfile.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
set(_prefix "${CMAKE_CURRENT_LIST_DIR}/@RELPATH@/")
cmake_path(ABSOLUTE_PATH _prefix NORMALIZE)
else()
get_filename_component(_prefix "${CMAKE_CURRENT_LIST_DIR}/@RELPATH@/" ABSOLUTE)
endif()

if(NOT CMAKE_SYSTEM_NAME AND NOT CMAKE_HOST_WIN32)
set(CMAKE_SYSTEM_NAME Windows)
endif()
if(NOT CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
if(CMAKE_HOST_WIN32)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
set(CMAKE_SYSTEM_PROCESSOR aarch64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^ARM")
set(CMAKE_SYSTEM_PROCESSOR armv7)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^[Xx]86$")
set(CMAKE_SYSTEM_PROCESSOR i686)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "^[Xx]64$")
set(CMAKE_SYSTEM_PROCESSOR x86_64)
endif()
endif()
endif()

set(CMAKE_SYSROOT "${_prefix}${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32")

if(NOT "$ENV{PKG_CONFIG_LIBDIR}" STREQUAL "")
set(_respath "${CMAKE_SYSROOT}/lib/pkgconfig:${CMAKE_SYSROOT}/share/pkgconfig:$ENV{PKG_CONFIG_LIBDIR}")
else()
set(_respath "${CMAKE_SYSROOT}/lib/pkgconfig:${CMAKE_SYSROOT}/share/pkgconfig")
endif()

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
cmake_path(CONVERT _respath TO_NATIVE_PATH_LIST _respath)
set(ENV{PKG_CONFIG_LIBDIR} "${_respath}")
cmake_path(NATIVE_PATH CMAKE_SYSROOT _respath)
set(ENV{PKG_CONFIG_SYSROOT_DIR} "${_respath}")
else()
file(TO_NATIVE_PATH "${_respath}" _respath)
set(ENV{PKG_CONFIG_LIBDIR} "${_respath}")
file(TO_NATIVE_PATH "${CMAKE_SYSROOT}" _respath)
set(ENV{PKG_CONFIG_SYSROOT_DIR} "${_respath}")
endif()

# set these before find_program!
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

set(_linker lld)
set(_exesuff)
if(CMAKE_HOST_WIN32)
set(_exesuff .exe)
endif()
set(CMAKE_ASM_COMPILER "${_prefix}bin/clang${_exesuff}")
set(CMAKE_C_COMPILER "${_prefix}bin/clang${_exesuff}")
set(CMAKE_CXX_COMPILER "${_prefix}bin/clang++${_exesuff}")
set(CMAKE_RC_COMPILER "${_prefix}bin/llvm-rc${_exesuff}")
set(CMAKE_LINKER "${_prefix}bin/ld.${_linker}${_exesuff}")
unset(_exesuff)

unset(_prefix)
unset(_respath)

set(CMAKE_C_COMPILER_TARGET ${CMAKE_SYSTEM_PROCESSOR}-w64-mingw32)
set(CMAKE_CXX_COMPILER_TARGET ${CMAKE_C_COMPILER_TARGET})
set(CMAKE_ASM_COMPILER_TARGET ${CMAKE_C_COMPILER_TARGET})

set(CMAKE_CXX_FLAGS_INIT "-stdlib=libc++")

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.29")
string(TOUPPER ${_linker} CMAKE_LINKER_TYPE)
set(_linker)
else()
set(_linker " -fuse-ld=${_linker}")
endif()

# CMAKE_CXX_FLAGS is automatically added now, but might be needed to add -stdlib=libc++ for really old CMake Versions
set(CMAKE_EXE_LINKER_FLAGS_INIT "--start-no-unused-arguments -rtlib=compiler-rt -unwindlib=libunwind --end-no-unused-arguments${_linker}")
set(CMAKE_MODULE_LINKER_FLAGS_INIT "${CMAKE_EXE_LINKER_FLAGS_INIT}")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "${CMAKE_EXE_LINKER_FLAGS_INIT}")
unset(_linker)
7 changes: 7 additions & 0 deletions wrappers/llvm-mingw_toolchainfile_arch.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if(NOT CMAKE_SYSTEM_NAME)
set(CMAKE_SYSTEM_NAME Windows)
endif()

set(CMAKE_SYSTEM_PROCESSOR @ARCH@)

include("${CMAKE_CURRENT_LIST_DIR}/llvm-mingw_toolchainfile.cmake")

0 comments on commit 79ce480

Please sign in to comment.