Commit c1f5a44b authored by Marc Chevrier's avatar Marc Chevrier
Browse files

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
parent 8e28d263
The final set of compile options used for a target is constructed by
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
......
......@@ -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
add_link_options
----------------
Adds options to the link of targets.
::
add_link_options(<option> ...)
Adds options to the linker command line for targets in the current
directory and below that are added after this command is invoked.
See documentation of the :prop_dir:`directory <LINK_OPTIONS>` and
:prop_tgt:`target <LINK_OPTIONS>` ``LINK_OPTIONS`` properties.
This command can be used to add any options, but alternative command
exist to add libraries (:command:`target_link_libraries` or
:command:`link_libraries`).
Arguments to ``add_link_options`` may use "generator expressions" with
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:: OPTIONS_SHELL.txt
......@@ -39,4 +39,4 @@ with 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
......@@ -70,7 +70,8 @@ Each ``<item>`` may be:
Link flags specified here are inserted into the link command in the same
place as the link libraries. This might not be correct, depending on
the linker. Use the :prop_tgt:`LINK_FLAGS` target property to add link
the linker. Use the :prop_tgt:`LINK_OPTIONS` target property or
:command:`target_link_options` command to add link
flags explicitly. The flags will then be placed at the toolchain-defined
flag position in the link command.
......
target_link_options
-------------------
Add link options to a target.
::
target_link_options(<target> [BEFORE]
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
Specify link options to use when linking a given target. The
named ``<target>`` must have been created by a command such as
:command:`add_executable` or :command:`add_library` and must not be an
:ref:`ALIAS target <Alias Targets>`.
If ``BEFORE`` is specified, the content will be prepended to the property
instead of being appended.
This command can be used to add any options, but
alternative commands exist to add libraries
(:command:`target_link_libraries` and :command:`link_libraries`).
See documentation of the :prop_dir:`directory <LINK_OPTIONS>` and
:prop_tgt:`target <LINK_OPTIONS>` ``LINK_OPTIONS`` properties.
The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
specify the scope of the following arguments. ``PRIVATE`` and ``PUBLIC``
items will populate the :prop_tgt:`LINK_OPTIONS` property of
``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
:prop_tgt:`INTERFACE_LINK_OPTIONS` property of ``<target>``.
(:ref:`IMPORTED targets <Imported Targets>` only support ``INTERFACE`` items.)
The following arguments specify compile options. Repeated calls for the same
``<target>`` append items in the order called.
Arguments to ``target_link_options`` may use "generator expressions"
with 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:: OPTIONS_SHELL.txt
......@@ -78,6 +78,7 @@ These commands are available only in CMake projects.
/command/add_dependencies
/command/add_executable
/command/add_library
/command/add_link_options
/command/add_subdirectory
/command/add_test
/command/aux_source_directory
......@@ -111,6 +112,7 @@ These commands are available only in CMake projects.
/command/target_compile_options
/command/target_include_directories
/command/target_link_libraries
/command/target_link_options
/command/target_sources
/command/try_compile
/command/try_run
......
......@@ -77,6 +77,7 @@ Properties on Directories
/prop_dir/INTERPROCEDURAL_OPTIMIZATION
/prop_dir/LABELS
/prop_dir/LINK_DIRECTORIES
/prop_dir/LINK_OPTIONS
/prop_dir/LISTFILE_STACK
/prop_dir/MACROS
/prop_dir/PARENT_DIRECTORY
......@@ -225,6 +226,7 @@ Properties on Targets
/prop_tgt/INTERFACE_COMPILE_OPTIONS
/prop_tgt/INTERFACE_INCLUDE_DIRECTORIES
/prop_tgt/INTERFACE_LINK_LIBRARIES
/prop_tgt/INTERFACE_LINK_OPTIONS
/prop_tgt/INTERFACE_POSITION_INDEPENDENT_CODE
/prop_tgt/INTERFACE_SOURCES
/prop_tgt/INTERFACE_SYSTEM_INCLUDE_DIRECTORIES
......@@ -254,6 +256,7 @@ Properties on Targets
/prop_tgt/LINK_INTERFACE_MULTIPLICITY_CONFIG
/prop_tgt/LINK_INTERFACE_MULTIPLICITY
/prop_tgt/LINK_LIBRARIES
/prop_tgt/LINK_OPTIONS
/prop_tgt/LINK_SEARCH_END_STATIC
/prop_tgt/LINK_SEARCH_START_STATIC
/prop_tgt/LINK_WHAT_YOU_USE
......
LINK_OPTIONS
------------
List of options to use for the link step.
This property holds a :ref:`;-list <CMake Language Lists>` of options
given so far to the :command:`add_link_options` command.
This property is used to initialize the :prop_tgt:`LINK_OPTIONS` target
property when a target is created, which is used by the generators to set
the options for the compiler.
Contents of ``LINK_OPTIONS`` may use "generator expressions" with 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.
INTERFACE_LINK_OPTIONS
----------------------
.. |property_name| replace:: link options
.. |command_name| replace:: :command:`target_link_options`
.. |PROPERTY_INTERFACE_NAME| replace:: ``INTERFACE_LINK_OPTIONS``
.. |PROPERTY_LINK| replace:: :prop_tgt:`LINK_OPTIONS`
.. |PROPERTY_GENEX| replace:: ``$<TARGET_PROPERTY:foo,INTERFACE_LINK_OPTIONS>``
.. include:: INTERFACE_BUILD_PROPERTY.txt
......@@ -3,7 +3,11 @@ LINK_FLAGS
Additional flags to use when linking this target.
The LINK_FLAGS property can be used to add extra flags to the link
step of a target. :prop_tgt:`LINK_FLAGS_<CONFIG>` will add to the
The LINK_FLAGS property, managed as a string, can be used to add extra flags
to the link step of a target. :prop_tgt:`LINK_FLAGS_<CONFIG>` will add to the
configuration ``<CONFIG>``, for example, ``DEBUG``, ``RELEASE``,
``MINSIZEREL``, ``RELWITHDEBINFO``, ...
.. note::
This property has been superseded by :prop_tgt:`LINK_OPTIONS` property.
......@@ -4,3 +4,7 @@ LINK_FLAGS_<CONFIG>
Per-configuration linker flags for a target.
This is the configuration-specific version of LINK_FLAGS.
.. note::
This property has been superseded by :prop_tgt:`LINK_OPTIONS` property.
LINK_OPTIONS
------------
List of options to use when linking this target.
This property holds a :ref:`;-list <CMake Language Lists>` of options
specified so far for its target. Use the :command:`target_link_options`
command to append more options.
This property is initialized by the :prop_dir:`LINK_OPTIONS` directory
property when a target is created, and is used by the generators to set
the options for the compiler.
Contents of ``LINK_OPTIONS`` may use "generator expressions" with 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.
.. note::
This property must be used in preference to :prop_tgt:`LINK_FLAGS` property.
LINK_OPTIONS
------------
* CMake gained new capabilities to manage link step:
* :prop_dir:`LINK_OPTIONS` directory property
* :prop_tgt:`LINK_OPTIONS` and :prop_tgt:`INTERFACE_LINK_OPTIONS` target
properties
* :command:`add_link_options` command to add link options in the current
directory
* :command:`target_link_options` command to add link options to targets
......@@ -379,6 +379,8 @@ set(SRCS
cmAddCompileDefinitionsCommand.h
cmAddCompileOptionsCommand.cxx
cmAddCompileOptionsCommand.h
cmAddLinkOptionsCommand.cxx
cmAddLinkOptionsCommand.h
cmAddCustomCommandCommand.cxx
cmAddCustomCommandCommand.h
cmAddCustomTargetCommand.cxx
......@@ -574,6 +576,8 @@ set(SRCS
cmTargetCompileOptionsCommand.h
cmTargetIncludeDirectoriesCommand.cxx
cmTargetIncludeDirectoriesCommand.h
cmTargetLinkOptionsCommand.cxx
cmTargetLinkOptionsCommand.h
cmTargetLinkLibrariesCommand.cxx
cmTargetLinkLibrariesCommand.h
cmTargetPropCommandBase.cxx
......
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmAddLinkOptionsCommand.h"
#include "cmMakefile.h"
class cmExecutionStatus;
bool cmAddLinkOptionsCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
if (args.empty()) {
return true;
}
for (std::string const& i : args) {
this->Makefile->AddLinkOption(i);
}
return true;
}
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#ifndef cmAddLinkOptionsCommand_h
#define cmAddLinkOptionsCommand_h
#include "cmConfigure.h" // IWYU pragma: keep
#include <string>
#include <vector>
#include "cmCommand.h"
class cmExecutionStatus;
class cmAddLinkOptionsCommand : public cmCommand
{
public:
/**
* This is a virtual constructor for the command.
*/
cmCommand* Clone() override { return new cmAddLinkOptionsCommand; }
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) override;
};
#endif
......@@ -82,6 +82,7 @@
#if defined(CMAKE_BUILD_WITH_CMAKE)
# include "cmAddCompileOptionsCommand.h"
# include "cmAddLinkOptionsCommand.h"
# include "cmAuxSourceDirectoryCommand.h"
# include "cmBuildNameCommand.h"
# include "cmCMakeHostSystemInformationCommand.h"
......@@ -100,6 +101,7 @@
# include "cmRemoveDefinitionsCommand.h"
# include "cmSourceGroupCommand.h"
# include "cmSubdirDependsCommand.h"
# include "cmTargetLinkOptionsCommand.h"
# include "cmUseMangledMesaCommand.h"
# include "cmUtilitySourceCommand.h"
# include "cmVariableRequiresCommand.h"
......@@ -272,7 +274,10 @@ void GetProjectCommands(cmState* state)
state->AddBuiltinCommand("include_external_msproject",
new cmIncludeExternalMSProjectCommand);
state->AddBuiltinCommand("install_programs", new cmInstallProgramsCommand);
state->AddBuiltinCommand("add_link_options", new cmAddLinkOptionsCommand);
state->AddBuiltinCommand("link_libraries", new cmLinkLibrariesCommand);
state->AddBuiltinCommand("target_link_options",
new cmTargetLinkOptionsCommand);
state->AddBuiltinCommand("load_cache", new cmLoadCacheCommand);
state->AddBuiltinCommand("qt_wrap_cpp", new cmQTWrapCPPCommand);
state->AddBuiltinCommand("qt_wrap_ui", new cmQTWrapUICommand);
......
......@@ -7,6 +7,7 @@
#include <sstream>
#include <utility>
#include "cmAlgorithms.h"
#include "cmGeneratorExpression.h"
#include "cmGeneratorExpressionDAGChecker.h"
#include "cmGeneratorTarget.h"
......@@ -169,6 +170,11 @@ void cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
end = "\\\n";
}
os << "\n";
} else if (property.first == "INTERFACE_LINK_OPTIONS") {
os << "LOCAL_EXPORT_LDFLAGS := ";
std::vector<std::string> linkFlagsList;
cmSystemTools::ExpandListArgument(property.second, linkFlagsList);
os << cmJoin(linkFlagsList, " ") << "\n";
} else {
os << "# " << property.first << " " << (property.second) << "\n";
}
......
......@@ -95,6 +95,9 @@ bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
cmGeneratorExpression::BuildInterface,
properties, missingTargets);
this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gte,
cmGeneratorExpression::BuildInterface,
properties, missingTargets);
this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
properties);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment