Skip to content

Commit 2cab814

Browse files
Ensure lto toolchain consistency by forcing cmake to use llvm-ar
1 parent f2f35ce commit 2cab814

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ include(GNUInstallDirs)
4242
include(FortranLib)
4343
include(AddPatchedSource)
4444

45+
# Use llvm-ar for IntelLLVM Fortran compiler
46+
include(UseLlvmArchive)
47+
4548
# Common compiler flags and definitions
4649
if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
4750
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fbacktrace -fconvert=big-endian -ffree-line-length-none -ffixed-line-length-none")

cmake/UseLlvmArchive.cmake

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Use llvm-ar with `rcs` so we dont need to setup a separate ranlib.
2+
3+
# Identify compiler is IntelLLVM
4+
# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html
5+
if (NOT (CMAKE_Fortran_COMPILER_ID MATCHES "IntelLLVM"))
6+
return()
7+
endif()
8+
9+
# set a temp variable to hold the Fortran compiler path
10+
set(_TMP_VAR "")
11+
if (CMAKE_Fortran_COMPILER)
12+
set(_TMP_VAR "${CMAKE_Fortran_COMPILER}")
13+
endif()
14+
15+
# try to discover full paths to llvm-ar
16+
# https://cmake.org/cmake/help/latest/command/execute_process.html
17+
if (_TMP_VAR)
18+
execute_process(
19+
COMMAND "${_TMP_VAR}" -print-prog-name=llvm-ar
20+
OUTPUT_VARIABLE LLVM_AR_PATH
21+
OUTPUT_STRIP_TRAILING_WHITESPACE
22+
)
23+
endif()
24+
25+
# enforce llvm-ar
26+
set(CMAKE_AR "${LLVM_AR_PATH}" CACHE FILEPATH "" FORCE)
27+
28+
# write index table via `s` flag, which disables separate ranlib step.
29+
# https://github.com/Kitware/CMake/blob/1c7fe4dc0b47de90bec6918b33f0aca47d688887/Modules/CMakeCInformation.cmake#L152-L162
30+
# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_ARCHIVE_FINISH.html
31+
#
32+
set(CMAKE_Fortran_ARCHIVE_CREATE "<CMAKE_AR> rcs <TARGET> <OBJECTS>")
33+
set(CMAKE_Fortran_ARCHIVE_FINISH "")
34+
35+
unset(_TMP_VAR)

0 commit comments

Comments
 (0)