]> git.ipfire.org Git - thirdparty/zstd.git/commit
[cmake] Fix -z noexecstack portability
authorNick Terrell <terrelln@fb.com>
Fri, 20 Dec 2024 17:40:32 +0000 (09:40 -0800)
committerNick Terrell <nickrterrell@gmail.com>
Fri, 20 Dec 2024 23:06:23 +0000 (15:06 -0800)
commitf0937b83d9a32cb2b59f99bbc4db717ae6e83c9b
treefaff281df8d222e9428d2a2adaf5d0f4fa5cda0f
parent5a7f5c745cfb7135f27002efc79fee1804d21d44
[cmake] Fix -z noexecstack portability

Summary:
Issue reported by @ryandesign and @MarcusCalhoun-Lopez.

CMake doesn't support spaces in flags. This caused older versions of gcc to
ignore the unknown flag "-z noexecstack" on MacOS since it was interpreted as a
single flag, not two separate flags. Then, during compilation it was treated as
"-z" "noexecstack", which was correctly forwarded to the linker. But the MacOS
linker does not support `-z noexecstack` so compilation failed.

The fix is to use `-Wl,-z,noexecstack`. This is never misinterpreted by a
compiler. However, not all compilers support this syntax to forward flags to the
linker. To fix this issue, we check if all the relevant `noexecstack` flags have
been successfully set, and if they haven't we disable assembly.

See also PR#4056 and PR#4061. I decided to go a different route because this is
simpler. It might not successfully set these flags on some compilers, but in that
case it also disables assembly, so they aren't required.

Test Plan:
```
mkdir build-cmake
cmake ../build/cmake/CMakeLists.txt
make -j
```

See that the linker flag is successfully detected & that assembly is enabled.

Run the same commands on MacOS which doesn't support `-Wl,-z,noexecstack` and see
that everything compiles and that `LD_FLAG_WL_Z_NOEXECSTACK` and
`ZSTD_HAS_NOEXECSTACK` are both false.
build/cmake/CMakeModules/AddZstdCompilationFlags.cmake
build/cmake/lib/CMakeLists.txt