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 Jun 3, 2024
1 parent 51cb479 commit 07c85c6
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
13 changes: 13 additions & 0 deletions install-wrappers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ 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 target_os in $TARGET_OSES; do
templ=
[ $target_os = mingw32 ] && templ=wrappers/llvm-mingw_toolchainfile_arch.cmake.in
[ $target_os = mingw32uwp ] && templ=wrappers/llvm-mingw_toolchainfile_arch_uwp.cmake.in
[ -n "$templ" ] || continue

for arch in $ARCHS; do
sed "s,@ARCH@,${arch},g" $templ >"$PREFIX/$toolchainfile_path/${arch}-w64-${target_os}_toolchainfile.cmake"
done
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)
set(CMAKE_SYSTEM_NAME Windows)
endif()
if(NOT CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()

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

set(ENV{PKG_CONFIG_SYSROOT_DIR} "${CMAKE_SYSROOT}")
if($ENV{PKG_CONFIG_LIBDIR})
set(ENV{PKG_CONFIG_LIBDIR} "${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig:$ENV{PKG_CONFIG_LIBDIR}")
else()
set(ENV{PKG_CONFIG_LIBDIR} "${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig")
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)

# search tools in prefix, then in system search paths
# note that the toolchain will be sourced multiple times and stuff like
# CMAKE_C_COMPILER might be cached - use temp variables with find_program
set(_linker lld)
find_program(_clang_exe clang HINTS "${_prefix}bin" NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CACHE )
find_program(_clangp_exe clang++ HINTS "${_prefix}bin" NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CACHE)
find_program(_llvm_rc llvm-rc HINTS "${_prefix}bin" NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CACHE)
find_program(_ld_linker ld.${_linker} HINTS "${_prefix}bin" NO_CMAKE_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CACHE)

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20")
cmake_path(GET _clang_exe PARENT_PATH _clang_path)
else()
get_filename_component(_clang_path "${_clang_exe}" DIRECTORY)
endif()

# test if the system compiler is used
if(_clang_path STREQUAL "${_prefix}bin")
set(CMAKE_ASM_COMPILER "${_clang_exe}")
set(CMAKE_C_COMPILER "${_clang_exe}")
set(CMAKE_CXX_COMPILER "${_clangp_exe}")
else()
file(GLOB _respath "${_prefix}lib/clang/*")

set(CMAKE_ASM_COMPILER "${_clang_exe}" "-resource-dir=${_respath}")
set(CMAKE_C_COMPILER "${_clang_exe}" "-resource-dir=${_respath}")
set(CMAKE_CXX_COMPILER "${_clangp_exe}" "-resource-dir=${_respath}")
endif()

set(CMAKE_RC_COMPILER "${_llvm_rc}")
set(CMAKE_LINKER "${_ld_linker}")

unset(_clang_path)
unset(_prefix)
unset(_clang_exe)
unset(_clangp_exe)
unset(_llvm_rc)
unset(_ld_linker)

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()

set(CMAKE_EXE_LINKER_FLAGS_INIT "--start-no-unused-arguments -stdlib=libc++ -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)
3 changes: 3 additions & 0 deletions wrappers/llvm-mingw_toolchainfile_arch.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(CMAKE_SYSTEM_PROCESSOR @ARCH@)

include("${CMAKE_CURRENT_LIST_DIR}/llvm-mingw_toolchainfile.cmake")
15 changes: 15 additions & 0 deletions wrappers/llvm-mingw_toolchainfile_arch_uwp.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# WindowsStore an UWP app - doesn't seem to have any effect outside of Visual Studio generators
# https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-windows-10-universal-applications
set(CMAKE_SYSTEM_NAME WindowsStore)
set(CMAKE_SYSTEM_VERSION 10)
set(CMAKE_SYSTEM_PROCESSOR @ARCH@)

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

set(CMAKE_C_FLAGS_INIT "-D_WIN32_WINNT=0x0A00 -DWINVER=0x0A00 -DWINAPI_FAMILY=WINAPI_FAMILY_APP -DUNICODE -D_UCRT")
set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} ${CMAKE_C_FLAGS_INIT}")
set(CMAKE_ASM_FLAGS_INIT "${CMAKE_C_FLAGS_INIT}")

# Cant extend the content written in CMake's Modules/Platform/Windows-GNU.cmake. Replace it
set(CMAKE_C_STANDARD_LIBRARIES "-lwindowsapp -lucrtapp -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32" CACHE STRING "Libraries linked by default with all C applications" FORCE)
set(CMAKE_CXX_STANDARD_LIBRARIES "-lwindowsapp -lucrtapp -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32" CACHE STRING "Libraries linked by default with all C++ applications" FORCE)

0 comments on commit 07c85c6

Please sign in to comment.