From 93eb4a7bd17f0da02a5ea71426bed5c2056e9ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90o=C3=A0n=20Tr=E1=BA=A7n=20C=C3=B4ng=20Danh?= Date: Sat, 31 Jul 2021 14:17:35 +0700 Subject: Ninja: Revert accidental LINK_WHAT_YOU_USE impl for static libraries In commit d7159f9591, (Ninja: Fix LINK_WHAT_YOU_USE link flag placement, 2021-06-24), we fixed usage of -Wl,--no-as-needed to override -Wl,--as-needed from LDFLAGS. However, that change also passes -Wl,--no-as-needed to `ar`, which is incorrect flag for `ar`. Let's check the target type before passing down the flags. Fixes: #22487 --- Source/cmNinjaNormalTargetGenerator.cxx | 5 ++++- Tests/RunCMake/LinkWhatYouUse/C.cmake | 1 + Tests/RunCMake/LinkWhatYouUse/CXX.cmake | 1 + Tests/RunCMake/LinkWhatYouUse/foo.c | 4 ++++ Tests/RunCMake/LinkWhatYouUse/foo.cxx | 4 ++++ 5 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Tests/RunCMake/LinkWhatYouUse/foo.c create mode 100644 Tests/RunCMake/LinkWhatYouUse/foo.cxx diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index 5a4c652..61cfa11 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/Source/cmNinjaNormalTargetGenerator.cxx @@ -1156,7 +1156,10 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement( this->AddModuleDefinitionFlag(linkLineComputer.get(), vars["LINK_FLAGS"], config); - if (gt->GetPropertyAsBool("LINK_WHAT_YOU_USE")) { + if (gt->GetPropertyAsBool("LINK_WHAT_YOU_USE") && + (gt->GetType() == cmStateEnums::TargetType::EXECUTABLE || + gt->GetType() == cmStateEnums::TargetType::SHARED_LIBRARY || + gt->GetType() == cmStateEnums::TargetType::MODULE_LIBRARY)) { vars["LINK_FLAGS"] += " -Wl,--no-as-needed"; } vars["LINK_FLAGS"] = globalGen->EncodeLiteral(vars["LINK_FLAGS"]); diff --git a/Tests/RunCMake/LinkWhatYouUse/C.cmake b/Tests/RunCMake/LinkWhatYouUse/C.cmake index 4c3f428..f00db30 100644 --- a/Tests/RunCMake/LinkWhatYouUse/C.cmake +++ b/Tests/RunCMake/LinkWhatYouUse/C.cmake @@ -2,3 +2,4 @@ enable_language(C) set(CMAKE_LINK_WHAT_YOU_USE TRUE) add_executable(main main.c) target_link_libraries(main m) +add_library(foo STATIC foo.c) diff --git a/Tests/RunCMake/LinkWhatYouUse/CXX.cmake b/Tests/RunCMake/LinkWhatYouUse/CXX.cmake index 9555832..5762e94 100644 --- a/Tests/RunCMake/LinkWhatYouUse/CXX.cmake +++ b/Tests/RunCMake/LinkWhatYouUse/CXX.cmake @@ -2,3 +2,4 @@ enable_language(CXX) set(CMAKE_LINK_WHAT_YOU_USE TRUE) add_executable(main main.cxx) target_link_libraries(main m) +add_library(foo STATIC foo.cxx) diff --git a/Tests/RunCMake/LinkWhatYouUse/foo.c b/Tests/RunCMake/LinkWhatYouUse/foo.c new file mode 100644 index 0000000..c83d856 --- /dev/null +++ b/Tests/RunCMake/LinkWhatYouUse/foo.c @@ -0,0 +1,4 @@ +int foo(void) +{ + return 0; +} diff --git a/Tests/RunCMake/LinkWhatYouUse/foo.cxx b/Tests/RunCMake/LinkWhatYouUse/foo.cxx new file mode 100644 index 0000000..e05eb7e --- /dev/null +++ b/Tests/RunCMake/LinkWhatYouUse/foo.cxx @@ -0,0 +1,4 @@ +int foo() +{ + return 0; +} -- cgit v0.12