Rust: Add support for position independent code

This commit is contained in:
Baudouin Feildel
2026-03-19 14:25:56 -04:00
committed by Brad King
parent c2a2bc368d
commit 942ddcdd36
2 changed files with 46 additions and 2 deletions
+40
View File
@@ -19,6 +19,46 @@ set(CMAKE_Rust_FLAGS_RELEASE_INIT "-O")
set(CMAKE_Rust_FLAGS_RELWITHDEBINFO_INIT "-O -g")
set(CMAKE_Rust_FLAGS_MINSIZEREL_INIT "-C opt-level=z")
block(
PROPAGATE
CMAKE_Rust_LINK_PIE_SUPPORTED
CMAKE_Rust_LINK_NO_PIE_SUPPORTED
CMAKE_Rust_COMPILE_OPTIONS_PIE
CMAKE_Rust_COMPILE_OPTIONS_PIC
CMAKE_Rust_LINK_OPTIONS_PIE
CMAKE_Rust_LINK_OPTIONS_NO_PIE
)
execute_process(
COMMAND "${CMAKE_Rust_COMPILER}" --print relocation-models
OUTPUT_VARIABLE RUSTC_OUTPUT
ERROR_VARIABLE RUSTC_ERROR
RESULT_VARIABLE RUSTC_EXITCODE
)
if(RUSTC_EXITCODE EQUAL "0")
string(REPLACE "\n" ";" RUSTC_OUTPUT_LINES "${RUSTC_OUTPUT}")
list(TRANSFORM RUSTC_OUTPUT_LINES STRIP)
if("pic" IN_LIST RUSTC_OUTPUT_LINES)
set(CMAKE_Rust_COMPILE_OPTIONS_PIC -C relocation-model=pic)
endif()
if("pie" IN_LIST RUSTC_OUTPUT_LINES)
set(CMAKE_Rust_LINK_PIE_SUPPORTED TRUE)
set(CMAKE_Rust_COMPILE_OPTIONS_PIE -C relocation-model=pie)
set(CMAKE_Rust_LINK_OPTIONS_PIE -C relocation-model=pie)
else()
set(CMAKE_Rust_LINK_PIE_SUPPORTED FALSE)
endif()
if("static" IN_LIST RUSTC_OUTPUT_LINES)
set(CMAKE_Rust_LINK_NO_PIE_SUPPORTED TRUE)
set(CMAKE_Rust_LINK_OPTIONS_NO_PIE -C relocation-model=static)
else()
set(CMAKE_Rust_LINK_NO_PIE_SUPPORTED FALSE)
endif()
else()
string(REPLACE "\n" "\n " RUSTC_ERROR " ${RUSTC_ERROR}")
message(FATAL_ERROR "Failed to check PIC/PIE support in rustc:\n${RUSTC_ERROR}")
endif()
endblock()
cmake_initialize_per_config_variable(CMAKE_Rust_FLAGS "Flags used by the Rust compiler")
if(NOT CMAKE_Rust_CREATE_STATIC_LIBRARY)
+6 -2
View File
@@ -3596,8 +3596,12 @@ void cmLocalGenerator::AppendPositionIndependentLinkerFlags(
}
char const* PICValue = target->GetLinkPIEProperty(config);
if (!PICValue) {
// POSITION_INDEPENDENT_CODE is not set
if (!PICValue && lang != "Rust") {
// POSITION_INDEPENDENT_CODE is not set, note that for Rust we do not
// return as the compiler tends to enable PIE all the time, which is the
// opposite of what C & C++ compilers do. So instead of letting the rust
// compiler decide on its own whether PIE should be enabled, we explicit
// set it.
return;
}