`-static-pie` is incompatible with `CheckPIESupported`

$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.30)
project(TestStaticPIE LANGUAGES CXX)

include(CheckPIESupported)
check_pie_supported(OUTPUT_VARIABLE output LANGUAGES CXX)
if(CMAKE_CXX_LINK_PIE_SUPPORTED)
  set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
add_executable(test main.cpp)
$ cat main.cpp 
#include <iostream>

int main() {
  std::cout << "Test.\n";
}

After running

$ LDFLAGS="-static-pie" cmake -B build
$ cmake --build build

the resulting executable is not linked statically.

The support of PIE is done through dedicated CMake private variables which are used if the check_pie_supported() command is successful. So, it is not required nor recommended specifying directly PIE options.

Now, the possibility to link statically is not part of the current mechanism. So the link command will have both PIE link options -pie and -static-pie and I suspect that -pie option override the -static-pie one.

Please open an issue to document this current limitation.

https://gitlab.kitware.com/cmake/cmake/-/issues/26757