Skip to content

Fix CMake compilation with -Wp,-D_FORTIFY_SOURCE=2 flags #12842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2025

Conversation

aale24
Copy link
Contributor

@aale24 aale24 commented Aug 1, 2025

Handle both -D_FORTIFY_SOURCE=2 and -Wp,-D_FORTIFY_SOURCE=2 variants when removing FORTIFY_SOURCE flags for unit test compilation with -O0.

What does this PR do?

Fixes a CMake compilation error that occurs when building GDAL unit tests on systems using hardened compiler flags with the -Wp,-D_FORTIFY_SOURCE=2 format.

The existing code in autotest/cpp/CMakeLists.txt only removes the -D_FORTIFY_SOURCE=2 variant when compiling unit tests with -O0 optimization, but doesn't handle the -Wp,-D_FORTIFY_SOURCE=2 variant used by modern hardened build environments. This leaves dangling -Wp, prefixes that cause compilation to fail with "too many filenames" errors.

The fix adds string replacement for both variants to ensure clean flag removal.

What are related issues/pull requests?

Fixes #12840

Tasklist

  • AI (Copilot or something similar) supported my development of this PR
  • Make sure code is correctly formatted (cf pre-commit configuration)
  • Add test case(s)
  • Add documentation
  • Updated Python API documentation (swig/include/python/docs/)
  • Review
  • Adjust for comments
  • All CI builds and checks have passed

Environment

Provide environment details, if relevant:

  • OS: Amazon Linux 2023
  • Compiler: GCC with hardened flags (-Wp,-D_FORTIFY_SOURCE=2)

Testing

cmake .. -DCMAKE_CXX_FLAGS="-Wp,-D_FORTIFY_SOURCE=2 -O2"
cmake --build . --target gdal_unit_test --verbose

gdal-fixed-with-Wp.log

cmake .. -DCMAKE_CXX_FLAGS="-D_FORTIFY_SOURCE=2 -O2"
-D_FORTIFY_SOURCE=2 -O2"
cmake --build . --target gdal_unit_test --verbose

gdal-fixed-without-Wp.log

cmake .. 
-D_FORTIFY_SOURCE=2 -O2"
cmake --build . --target gdal_unit_test --verbose

gdal-fixed-no-flags.log

Handle both -D_FORTIFY_SOURCE=2 and -Wp,-D_FORTIFY_SOURCE=2 variants
when removing FORTIFY_SOURCE flags for unit test compilation with -O0.
@rouault rouault added the backport release/3.11 Backport to release/3.11 branch label Aug 1, 2025
@rouault rouault merged commit 4a23a43 into OSGeo:master Aug 1, 2025
38 checks passed
@aale24 aale24 deleted the fix-cmake-fortify-source-wp-prefix branch August 1, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport release/3.11 Backport to release/3.11 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CMake unit test build fails with "too many filenames" error when FORTIFY_SOURCE uses -Wp, prefix
2 participants