Skip to content

Commit c84c62c

Browse files
committed
[CMake] Fix LLVM build non-determinism on RHEL
On RHEL, the OS tooling (ar, ranlib) is not deterministic by default. Therefore, we cannot get bit-for-bit identical builds. The goal of this patch is that it adds the flags required to force determinism. Differential Revision: https://reviews.llvm.org/D64817
1 parent 29f6f9b commit c84c62c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ if(APPLE)
135135
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
136136
endif()
137137

138+
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
139+
# RHEL7 has ar and ranlib being non-deterministic by default. The D flag forces determinism,
140+
# however only GNU version of ar and ranlib (2.27) have this option.
141+
# RHEL DTS7 is also affected by this, which uses GNU binutils 2.28
142+
execute_process(COMMAND ${CMAKE_AR} rD t.a
143+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} RESULT_VARIABLE AR_RESULT OUTPUT_VARIABLE RANLIB_OUTPUT)
144+
if(${AR_RESULT} EQUAL 0)
145+
execute_process(COMMAND ${CMAKE_RANLIB} -D t.a
146+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} RESULT_VARIABLE RANLIB_RESULT OUTPUT_VARIABLE RANLIB_OUTPUT)
147+
if(${RANLIB_RESULT} EQUAL 0)
148+
set(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
149+
set(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
150+
set(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
151+
152+
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Dqc <TARGET> <LINK_FLAGS> <OBJECTS>")
153+
set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> Dq <TARGET> <LINK_FLAGS> <OBJECTS>")
154+
set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -D <TARGET>")
155+
endif()
156+
file(REMOVE ${CMAKE_BINARY_DIR}/t.a)
157+
endif()
158+
endif()
159+
138160
if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
139161
if(NOT LLVM_BUILD_32_BITS)
140162
if (CMAKE_CXX_COMPILER_ID MATCHES "XL")

0 commit comments

Comments
 (0)