From 8e28d2630a60475dad715162a1802d301ada35bd Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Thu, 26 Apr 2018 10:11:50 +0200 Subject: Makefile generator: link flags management refactoring --- Source/cmMakefileExecutableTargetGenerator.cxx | 14 ++------- Source/cmMakefileLibraryTargetGenerator.cxx | 42 +++++--------------------- Source/cmMakefileLibraryTargetGenerator.h | 3 +- Source/cmMakefileTargetGenerator.cxx | 11 +++++++ Source/cmMakefileTargetGenerator.h | 2 ++ 5 files changed, 24 insertions(+), 48 deletions(-) diff --git a/Source/cmMakefileExecutableTargetGenerator.cxx b/Source/cmMakefileExecutableTargetGenerator.cxx index 1e59f44..9ffffc2 100644 --- a/Source/cmMakefileExecutableTargetGenerator.cxx +++ b/Source/cmMakefileExecutableTargetGenerator.cxx @@ -154,12 +154,7 @@ void cmMakefileExecutableTargetGenerator::WriteDeviceExecutableRule( linkLanguage, this->ConfigName); // Add target-specific linker flags. - this->LocalGenerator->AppendFlags( - linkFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - std::string linkFlagsConfig = "LINK_FLAGS_"; - linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName); - this->LocalGenerator->AppendFlags( - linkFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig)); + this->GetTargetLinkFlags(linkFlags); // Construct a list of files associated with this executable that // may need to be cleaned. @@ -437,12 +432,7 @@ void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink) linkLanguage, this->ConfigName); // Add target-specific linker flags. - this->LocalGenerator->AppendFlags( - linkFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - std::string linkFlagsConfig = "LINK_FLAGS_"; - linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName); - this->LocalGenerator->AppendFlags( - linkFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig)); + this->GetTargetLinkFlags(linkFlags); { std::unique_ptr linkLineComputer( diff --git a/Source/cmMakefileLibraryTargetGenerator.cxx b/Source/cmMakefileLibraryTargetGenerator.cxx index c538992..8a08789 100644 --- a/Source/cmMakefileLibraryTargetGenerator.cxx +++ b/Source/cmMakefileLibraryTargetGenerator.cxx @@ -137,10 +137,7 @@ void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules() this->GeneratorTarget->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS"); if (hasCUDA && resolveDeviceSymbols) { std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY"; - std::string extraFlags; - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - this->WriteDeviceLibraryRules(linkRuleVar, extraFlags, false); + this->WriteDeviceLibraryRules(linkRuleVar, false); } std::string linkLanguage = @@ -173,10 +170,7 @@ void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink) cuda_lang) != closure->Languages.end()); if (hasCUDA) { std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY"; - std::string extraFlags; - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - this->WriteDeviceLibraryRules(linkRuleVar, extraFlags, relink); + this->WriteDeviceLibraryRules(linkRuleVar, relink); } } @@ -187,13 +181,7 @@ void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink) linkRuleVar += "_CREATE_SHARED_LIBRARY"; std::string extraFlags; - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - std::string linkFlagsConfig = "LINK_FLAGS_"; - linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName); - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig)); - + this->GetTargetLinkFlags(extraFlags); this->LocalGenerator->AddConfigVariableFlags( extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName); @@ -223,10 +211,7 @@ void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink) cuda_lang) != closure->Languages.end()); if (hasCUDA) { std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY"; - std::string extraFlags; - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - this->WriteDeviceLibraryRules(linkRuleVar, extraFlags, relink); + this->WriteDeviceLibraryRules(linkRuleVar, relink); } } @@ -237,12 +222,7 @@ void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink) linkRuleVar += "_CREATE_SHARED_MODULE"; std::string extraFlags; - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - std::string linkFlagsConfig = "LINK_FLAGS_"; - linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName); - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig)); + this->GetTargetLinkFlags(extraFlags); this->LocalGenerator->AddConfigVariableFlags( extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName); @@ -265,12 +245,7 @@ void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink) linkRuleVar += "_CREATE_MACOSX_FRAMEWORK"; std::string extraFlags; - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); - std::string linkFlagsConfig = "LINK_FLAGS_"; - linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName); - this->LocalGenerator->AppendFlags( - extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig)); + this->GetTargetLinkFlags(extraFlags); this->LocalGenerator->AddConfigVariableFlags( extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName); @@ -278,7 +253,7 @@ void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink) } void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules( - const std::string& linkRuleVar, const std::string& extraFlags, bool relink) + const std::string& linkRuleVar, bool relink) { #ifdef CMAKE_BUILD_WITH_CMAKE // TODO: Merge the methods that call this method to avoid @@ -296,7 +271,7 @@ void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules( // Create set of linking flags. std::string linkFlags; - this->LocalGenerator->AppendFlags(linkFlags, extraFlags); + this->GetTargetLinkFlags(linkFlags); // Get the name of the device object to generate. std::string const targetOutputReal = @@ -458,7 +433,6 @@ void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules( this->WriteTargetDriverRule(targetOutputReal, relink); #else static_cast(linkRuleVar); - static_cast(extraFlags); static_cast(relink); #endif } diff --git a/Source/cmMakefileLibraryTargetGenerator.h b/Source/cmMakefileLibraryTargetGenerator.h index 02fa029..35e4327 100644 --- a/Source/cmMakefileLibraryTargetGenerator.h +++ b/Source/cmMakefileLibraryTargetGenerator.h @@ -27,8 +27,7 @@ protected: void WriteSharedLibraryRules(bool relink); void WriteModuleLibraryRules(bool relink); - void WriteDeviceLibraryRules(const std::string& linkRule, - const std::string& extraFlags, bool relink); + void WriteDeviceLibraryRules(const std::string& linkRule, bool relink); void WriteLibraryRules(const std::string& linkRule, const std::string& extraFlags, bool relink); // MacOSX Framework support methods diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 1f65f08..8cbddd9 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -82,6 +82,17 @@ cmMakefileTargetGenerator* cmMakefileTargetGenerator::New( return result; } +void cmMakefileTargetGenerator::GetTargetLinkFlags(std::string& flags) +{ + this->LocalGenerator->AppendFlags( + flags, this->GeneratorTarget->GetProperty("LINK_FLAGS")); + + std::string linkFlagsConfig = "LINK_FLAGS_"; + linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName); + this->LocalGenerator->AppendFlags( + flags, this->GeneratorTarget->GetProperty(linkFlagsConfig)); +} + void cmMakefileTargetGenerator::CreateRuleFile() { // Create a directory for this target. diff --git a/Source/cmMakefileTargetGenerator.h b/Source/cmMakefileTargetGenerator.h index 7af3cf3..4e6849a 100644 --- a/Source/cmMakefileTargetGenerator.h +++ b/Source/cmMakefileTargetGenerator.h @@ -52,6 +52,8 @@ public: cmGeneratorTarget* GetGeneratorTarget() { return this->GeneratorTarget; } protected: + void GetTargetLinkFlags(std::string& flags); + // create the file and directory etc void CreateRuleFile(); -- cgit v0.12 From c1f5a44b28047cde74e2fb10e8d68e314272f699 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Tue, 24 Apr 2018 17:01:01 +0200 Subject: LINK_OPTIONS: Add new family of properties This family enable to manage link flags Three new properties: * directory property: LINK_OPTIONS * target properties: LINK_OPTIONS and INTERFACE_LINK_OPTIONS Two new commands * add_link_options(): to populate directory property * target_link_options(): to populate target properties Fixes: #16543 --- Help/command/COMPILE_OPTIONS_SHELL.txt | 9 --- Help/command/OPTIONS_SHELL.txt | 9 +++ Help/command/add_compile_options.rst | 2 +- Help/command/add_link_options.rst | 24 +++++++ Help/command/target_compile_options.rst | 2 +- Help/command/target_link_libraries.rst | 3 +- Help/command/target_link_options.rst | 40 +++++++++++ Help/manual/cmake-commands.7.rst | 2 + Help/manual/cmake-properties.7.rst | 3 + Help/prop_dir/LINK_OPTIONS.rst | 16 +++++ Help/prop_tgt/INTERFACE_LINK_OPTIONS.rst | 9 +++ Help/prop_tgt/LINK_FLAGS.rst | 8 ++- Help/prop_tgt/LINK_FLAGS_CONFIG.rst | 4 ++ Help/prop_tgt/LINK_OPTIONS.rst | 21 ++++++ Help/release/dev/LINK_OPTIONS.rst | 11 +++ Source/CMakeLists.txt | 4 ++ Source/cmAddLinkOptionsCommand.cxx | 20 ++++++ Source/cmAddLinkOptionsCommand.h | 31 +++++++++ Source/cmCommands.cxx | 5 ++ Source/cmExportBuildAndroidMKGenerator.cxx | 6 ++ Source/cmExportBuildFileGenerator.cxx | 3 + Source/cmExportInstallFileGenerator.cxx | 3 + Source/cmGeneratorExpressionDAGChecker.h | 3 +- Source/cmGeneratorTarget.cxx | 81 +++++++++++++++++++--- Source/cmGeneratorTarget.h | 6 ++ Source/cmGlobalXCodeGenerator.cxx | 4 ++ Source/cmLocalGenerator.cxx | 8 +++ Source/cmLocalVisualStudio7Generator.cxx | 7 ++ Source/cmMakefile.cxx | 15 ++++ Source/cmMakefile.h | 3 + Source/cmMakefileExecutableTargetGenerator.cxx | 4 +- Source/cmMakefileLibraryTargetGenerator.cxx | 8 +-- Source/cmMakefileTargetGenerator.cxx | 8 ++- Source/cmMakefileTargetGenerator.h | 2 +- Source/cmState.cxx | 2 + Source/cmStateDirectory.cxx | 52 ++++++++++++++ Source/cmStateDirectory.h | 7 ++ Source/cmStatePrivate.h | 4 ++ Source/cmStateSnapshot.cxx | 7 ++ Source/cmTarget.cxx | 77 ++++++++++++++++++-- Source/cmTarget.h | 6 ++ Source/cmTargetLinkOptionsCommand.cxx | 41 +++++++++++ Source/cmTargetLinkOptionsCommand.h | 41 +++++++++++ Source/cmVisualStudio10TargetGenerator.cxx | 5 ++ .../CMakeCommands/add_link_options/CMakeLists.txt | 20 ++++++ .../add_link_options/LinkOptionsExe.c | 4 ++ .../target_link_options/CMakeLists.txt | 19 +++++ .../target_link_options/LinkOptionsLib.c | 7 ++ Tests/CMakeLists.txt | 3 + Tests/ExportImport/Export/CMakeLists.txt | 10 +++ Tests/ExportImport/Import/A/CMakeLists.txt | 5 ++ .../ExportImport/Import/A/imp_testLinkOptions.cpp | 8 +++ Tests/RunCMake/AndroidMK/AndroidMK.cmake | 6 +- .../RunCMake/AndroidMK/expectedBuildAndroidMK.txt | 8 +++ .../AndroidMK/expectedInstallAndroidMK.txt | 8 +++ Tests/RunCMake/CMakeLists.txt | 2 + Tests/RunCMake/add_link_options/CMakeLists.txt | 5 ++ .../add_link_options/LINK_OPTIONS-exe-check.cmake | 7 ++ .../add_link_options/LINK_OPTIONS-exe-result.txt | 1 + .../add_link_options/LINK_OPTIONS-mod-check.cmake | 7 ++ .../add_link_options/LINK_OPTIONS-mod-result.txt | 1 + .../LINK_OPTIONS-shared-check.cmake | 7 ++ .../LINK_OPTIONS-shared-result.txt | 1 + Tests/RunCMake/add_link_options/LINK_OPTIONS.cmake | 17 +++++ Tests/RunCMake/add_link_options/LinkOptionsExe.c | 4 ++ Tests/RunCMake/add_link_options/LinkOptionsLib.c | 7 ++ Tests/RunCMake/add_link_options/RunCMakeTest.cmake | 28 ++++++++ .../RunCMake/set_property/LINK_OPTIONS-stdout.txt | 2 + Tests/RunCMake/set_property/LINK_OPTIONS.cmake | 3 + Tests/RunCMake/set_property/RunCMakeTest.cmake | 1 + Tests/RunCMake/target_link_options/CMakeLists.txt | 5 ++ .../LINK_OPTIONS-basic-check.cmake | 7 ++ .../LINK_OPTIONS-basic-result.txt | 1 + .../LINK_OPTIONS-exe-check.cmake | 7 ++ .../LINK_OPTIONS-exe-result.txt | 1 + .../LINK_OPTIONS-interface-check.cmake | 4 ++ .../LINK_OPTIONS-interface-result.txt | 1 + .../LINK_OPTIONS-mod-check.cmake | 7 ++ .../LINK_OPTIONS-mod-result.txt | 1 + .../LINK_OPTIONS-shared-check.cmake | 7 ++ .../LINK_OPTIONS-shared-result.txt | 1 + .../target_link_options/LINK_OPTIONS.cmake | 39 +++++++++++ .../RunCMake/target_link_options/LinkOptionsExe.c | 4 ++ .../RunCMake/target_link_options/LinkOptionsLib.c | 7 ++ .../target_link_options/RunCMakeTest.cmake | 62 +++++++++++++++++ 85 files changed, 929 insertions(+), 42 deletions(-) delete mode 100644 Help/command/COMPILE_OPTIONS_SHELL.txt create mode 100644 Help/command/OPTIONS_SHELL.txt create mode 100644 Help/command/add_link_options.rst create mode 100644 Help/command/target_link_options.rst create mode 100644 Help/prop_dir/LINK_OPTIONS.rst create mode 100644 Help/prop_tgt/INTERFACE_LINK_OPTIONS.rst create mode 100644 Help/prop_tgt/LINK_OPTIONS.rst create mode 100644 Help/release/dev/LINK_OPTIONS.rst create mode 100644 Source/cmAddLinkOptionsCommand.cxx create mode 100644 Source/cmAddLinkOptionsCommand.h create mode 100644 Source/cmTargetLinkOptionsCommand.cxx create mode 100644 Source/cmTargetLinkOptionsCommand.h create mode 100644 Tests/CMakeCommands/add_link_options/CMakeLists.txt create mode 100644 Tests/CMakeCommands/add_link_options/LinkOptionsExe.c create mode 100644 Tests/CMakeCommands/target_link_options/CMakeLists.txt create mode 100644 Tests/CMakeCommands/target_link_options/LinkOptionsLib.c create mode 100644 Tests/ExportImport/Import/A/imp_testLinkOptions.cpp create mode 100644 Tests/RunCMake/add_link_options/CMakeLists.txt create mode 100644 Tests/RunCMake/add_link_options/LINK_OPTIONS-exe-check.cmake create mode 100644 Tests/RunCMake/add_link_options/LINK_OPTIONS-exe-result.txt create mode 100644 Tests/RunCMake/add_link_options/LINK_OPTIONS-mod-check.cmake create mode 100644 Tests/RunCMake/add_link_options/LINK_OPTIONS-mod-result.txt create mode 100644 Tests/RunCMake/add_link_options/LINK_OPTIONS-shared-check.cmake create mode 100644 Tests/RunCMake/add_link_options/LINK_OPTIONS-shared-result.txt create mode 100644 Tests/RunCMake/add_link_options/LINK_OPTIONS.cmake create mode 100644 Tests/RunCMake/add_link_options/LinkOptionsExe.c create mode 100644 Tests/RunCMake/add_link_options/LinkOptionsLib.c create mode 100644 Tests/RunCMake/add_link_options/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/set_property/LINK_OPTIONS-stdout.txt create mode 100644 Tests/RunCMake/set_property/LINK_OPTIONS.cmake create mode 100644 Tests/RunCMake/target_link_options/CMakeLists.txt create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-basic-check.cmake create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-basic-result.txt create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-exe-check.cmake create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-exe-result.txt create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-interface-check.cmake create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-interface-result.txt create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-mod-check.cmake create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-mod-result.txt create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-shared-check.cmake create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS-shared-result.txt create mode 100644 Tests/RunCMake/target_link_options/LINK_OPTIONS.cmake create mode 100644 Tests/RunCMake/target_link_options/LinkOptionsExe.c create mode 100644 Tests/RunCMake/target_link_options/LinkOptionsLib.c create mode 100644 Tests/RunCMake/target_link_options/RunCMakeTest.cmake diff --git a/Help/command/COMPILE_OPTIONS_SHELL.txt b/Help/command/COMPILE_OPTIONS_SHELL.txt deleted file mode 100644 index a1316c8..0000000 --- a/Help/command/COMPILE_OPTIONS_SHELL.txt +++ /dev/null @@ -1,9 +0,0 @@ -The final set of compile options used for a target is constructed by -accumulating options from the current target and the usage requirements of -it dependencies. The set of options is de-duplicated to avoid repetition. -While beneficial for individual options, the de-duplication step can break -up option groups. For example, ``-D A -D B`` becomes ``-D A B``. One may -specify a group of options using shell-like quoting along with a ``SHELL:`` -prefix. The ``SHELL:`` prefix is dropped and the rest of the option string -is parsed using the :command:`separate_arguments` ``UNIX_COMMAND`` mode. -For example, ``"SHELL:-D A" "SHELL:-D B"`` becomes ``-D A -D B``. diff --git a/Help/command/OPTIONS_SHELL.txt b/Help/command/OPTIONS_SHELL.txt new file mode 100644 index 0000000..530c012 --- /dev/null +++ b/Help/command/OPTIONS_SHELL.txt @@ -0,0 +1,9 @@ +The final set of compile or link options used for a target is constructed by +accumulating options from the current target and the usage requirements of +it dependencies. The set of options is de-duplicated to avoid repetition. +While beneficial for individual options, the de-duplication step can break +up option groups. For example, ``-D A -D B`` becomes ``-D A B``. One may +specify a group of options using shell-like quoting along with a ``SHELL:`` +prefix. The ``SHELL:`` prefix is dropped and the rest of the option string +is parsed using the :command:`separate_arguments` ``UNIX_COMMAND`` mode. +For example, ``"SHELL:-D A" "SHELL:-D B"`` becomes ``-D A -D B``. diff --git a/Help/command/add_compile_options.rst b/Help/command/add_compile_options.rst index c445608..350a1c0 100644 --- a/Help/command/add_compile_options.rst +++ b/Help/command/add_compile_options.rst @@ -22,4 +22,4 @@ the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)` manual for available expressions. See the :manual:`cmake-buildsystem(7)` manual for more on defining buildsystem properties. -.. include:: COMPILE_OPTIONS_SHELL.txt +.. include:: OPTIONS_SHELL.txt diff --git a/Help/command/add_link_options.rst b/Help/command/add_link_options.rst new file mode 100644 index 0000000..c827d70 --- /dev/null +++ b/Help/command/add_link_options.rst @@ -0,0 +1,24 @@ +add_link_options +---------------- + +Adds options to the link of targets. + +:: + + add_link_options(