Diagnostics: Warn about non-target directives

Introduce `CMD_STRICT` to act as a parent for warnings about allowed but
disrecommended usage. Introduce a warning that discourages use of
non-target directives (which modify the build environment for the entire
[sub]tree). Add notes to the official documentation recommending use of
target-specific alternatives. This has been accepted wisdom for quite
some time, but only some of the affected commands made any mention of
this in the documentation.
This commit is contained in:
Matthew Woehlke
2026-07-28 11:38:20 -04:00
parent 42da7a30a9
commit 00959286b9
30 changed files with 512 additions and 48 deletions

View File

@@ -27,7 +27,5 @@ language syntax may require escapes to specify some values).
.. |command_name| replace:: ``add_compile_definitions``
.. include:: include/GENEX_NOTE.rst
See Also
^^^^^^^^
* The command :command:`target_compile_definitions` adds target-specific definitions.
.. |target_command_name| replace:: :command:`target_compile_definitions`
.. include:: include/NON_TARGET_NOTE.rst

View File

@@ -16,6 +16,9 @@ directory and below.
These options are not used when linking.
See the :command:`add_link_options` command for that.
.. |target_command_name| replace:: :command:`target_compile_options`
.. include:: include/NON_TARGET_NOTE.rst
Arguments
^^^^^^^^^

View File

@@ -16,9 +16,15 @@ flags, but it is intended to add preprocessor definitions.
This command has been superseded by alternatives:
* Use :command:`add_compile_definitions` to add preprocessor definitions.
* Use :command:`include_directories` to add include directories.
* Use :command:`add_compile_options` to add other options.
* Use :command:`add_compile_definitions`
or :command:`target_compile_definitions`
to add preprocessor definitions.
* Use :command:`add_compile_options`
or :command:`target_compile_options`
to add other options.
* Use :command:`include_directories`
or :command:`target_include_directories`
to add include directories.
Flags beginning in ``-D`` or ``/D`` that look like preprocessor definitions are
automatically added to the :prop_dir:`COMPILE_DEFINITIONS` directory

View File

@@ -26,6 +26,9 @@ exist to add libraries (:command:`target_link_libraries` or
.. |command_name| replace:: ``add_link_options``
.. include:: include/GENEX_NOTE.rst
.. |target_command_name| replace:: :command:`target_link_options`
.. include:: include/NON_TARGET_NOTE.rst
.. include:: include/DEVICE_LINK_OPTIONS.rst
.. include:: include/OPTIONS_SHELL.rst

View File

@@ -0,0 +1,2 @@
.. note::
.. include:: include/NON_TARGET_NOTE_BODY.rst

View File

@@ -0,0 +1,6 @@
Use of the |command_name| command for new development is discouraged.
Unencapsulated alterations to the build environment can have unintentionally
broad effect, potentially resulting in unnecessary build dependencies.
Additionally, the |command_name| command cannot be used to express interface
usage requirements. It is recommended that new development use the
|target_command_name| command instead.

View File

@@ -32,11 +32,8 @@ considered in dependency calculations - see compiler docs.
.. |command_name| replace:: ``include_directories``
.. include:: include/GENEX_NOTE.rst
.. note::
Prefer the :command:`target_include_directories` command to add include
directories to individual targets and optionally propagate/export them
to dependents.
.. |target_command_name| replace:: :command:`target_include_directories`
.. include:: include/NON_TARGET_NOTE.rst
See Also
^^^^^^^^

View File

@@ -13,7 +13,10 @@ the current directory or below by commands such as :command:`add_executable`
or :command:`add_library`. See the :command:`target_link_libraries` command
for meaning of arguments.
.. |command_name| replace:: ``link_libraries``
.. |target_command_name| replace:: :command:`target_link_libraries`
.. note::
The :command:`target_link_libraries` command should be preferred whenever
possible. Library dependencies are chained automatically, so directory-wide
.. include:: include/NON_TARGET_NOTE_BODY.rst
Library dependencies are chained automatically, so directory-wide
specification of link libraries is rarely needed.

View File

@@ -5,7 +5,7 @@ CMD_INSTALL_ABSOLUTE_DESTINATION
.. diagnostic::
:default: ignore
:parent: CMD_AUTHOR
:parent: CMD_STRICT
Warn when an :command:`install` command specifies an absolute ``DESTINATION``
path. Absolute destinations are typically undesirable because they prevent

View File

@@ -0,0 +1,15 @@
CMD_NON_TARGET_DIRECTIVE
------------------------
.. versionadded:: 4.5
.. diagnostic::
:default: ignore
:parent: CMD_STRICT
Warn about use of non-target directives which alter the build environment.
Unencapsulated alterations to the build environment can have unintentionally
broad effect, potentially resulting in unnecessary build dependencies.
Additionally, such commands cannot be used to express interface usage
requirements. It is recommended that new development use target-specific
commands instead.

View File

@@ -0,0 +1,14 @@
CMD_STRICT
----------
.. versionadded:: 4.5
.. diagnostic::
:default: ignore
:parent: CMD_AUTHOR
Control diagnostics related to allowed but disrecommended usage. This is
the parent of the following diagnostics:
* :diagnostic:`CMD_INSTALL_ABSOLUTE_DESTINATION`
* :diagnostic:`CMD_NON_TARGET_DIRECTIVE`

View File

@@ -90,6 +90,8 @@ The following categories are defined:
/diagnostic/CMD_DEPRECATED
/diagnostic/CMD_EXPERIMENTAL
/diagnostic/CMD_INSTALL_ABSOLUTE_DESTINATION
/diagnostic/CMD_NON_TARGET_DIRECTIVE
/diagnostic/CMD_POLICY
/diagnostic/CMD_STRICT
/diagnostic/CMD_UNINITIALIZED
/diagnostic/CMD_UNUSED_CLI

View File

@@ -487,7 +487,8 @@ they were added and a summary of the new features and changes is given below.
:preset:`configurePresets.errors`.
* The ``installAbsoluteDestination`` field was added to
:preset:`configurePresets.warnings` and :preset:`configurePresets.errors`.
:preset:`configurePresets.warnings` and
:preset:`configurePresets.errors`.
* Changes to `Macro Expansion`_
@@ -501,6 +502,15 @@ they were added and a summary of the new features and changes is given below.
* The :preset:`testPresets.execution.testPassthroughArguments` field was
added to forward arguments to test executables.
``13``
.. versionadded:: 4.5
* Changes to `Configure Presets <Configure Preset_>`_:
* The ``strict`` and ``nonTargetDirective`` fields were added to
:preset:`configurePresets.warnings` and
:preset:`configurePresets.errors`.
Schema
======

View File

@@ -50,6 +50,15 @@
:cmake-option:`-Wno-error=install-absolute-destination` on the command line.
This may not be set to ``true`` if ``warnings.installAbsoluteDestination`` is set to ``false``.
.. _`CMakePresets.configurePresets.errors.nonTargetDirective`:
``nonTargetDirective``
.. presets-versionadded:: 13
An optional boolean. Equivalent to passing :cmake-option:`-Werror=non-target-directive` or
:cmake-option:`-Wno-error=non-target-directive` on the command line.
This may not be set to ``true`` if ``warnings.nonTargetDirective`` is set to ``false``.
.. _`CMakePresets.configurePresets.errors.policy`:
``policy``
@@ -59,6 +68,15 @@
:cmake-option:`-Wno-error=policy` on the command line.
This may not be set to ``true`` if ``warnings.policy`` is set to ``false``.
.. _`CMakePresets.configurePresets.errors.strict`:
``strict``
.. presets-versionadded:: 13
An optional boolean. Equivalent to passing :cmake-option:`-Werror=strict` or
:cmake-option:`-Wno-error=strict` on the command line.
This may not be set to ``true`` if ``warnings.strict`` is set to ``false``.
.. _`CMakePresets.configurePresets.errors.uninitialized`:
``uninitialized``

View File

@@ -420,7 +420,51 @@
"$ref": "#/definitions/buildPresets@v10.."
},
"testPresets": {
"$ref": "#/definitions/testPresets@v12"
"$ref": "#/definitions/testPresets@v12.."
},
"packagePresets": {
"$ref": "#/definitions/packagePresets@v10.."
},
"workflowPresets": {
"$ref": "#/definitions/workflowPresets@v10.."
},
"$comment": {
"$ref": "#/definitions/$comment@v10.."
}
},
"required": [
"version"
],
"additionalProperties": false
},
{
"description": "The presets specify the generator and the build directory, and optionally an array of variables and other arguments to pass to CMake.",
"properties": {
"$schema": {
"$ref": "#/definitions/$schema@v8.."
},
"version": {
"type": "integer",
"description": "A required integer representing the version of the JSON schema.",
"const": 13
},
"cmakeMinimumRequired": {
"$ref": "#/definitions/cmakeMinimumRequired@v10.."
},
"include": {
"$ref": "#/definitions/include@v4.."
},
"vendor": {
"$ref": "#/definitions/vendor@v1.."
},
"configurePresets": {
"$ref": "#/definitions/configurePresets@v13"
},
"buildPresets": {
"$ref": "#/definitions/buildPresets@v10.."
},
"testPresets": {
"$ref": "#/definitions/testPresets@v12.."
},
"packagePresets": {
"$ref": "#/definitions/packagePresets@v10.."
@@ -893,6 +937,86 @@
]
}
},
"configurePresets@v13": {
"type": "array",
"description": "An optional array of configure preset objects.",
"items": {
"type": "object",
"description": "A configure preset object.",
"unevaluatedProperties": false,
"properties": {
"name": {
"$ref": "#/definitions/configurePresets.name@v1.."
},
"hidden": {
"$ref": "#/definitions/configurePresets.hidden@v1.."
},
"inherits": {
"$ref": "#/definitions/configurePresets.inherits@v1.."
},
"condition": {
"$ref": "#/definitions/configurePresets.condition@v10.."
},
"vendor": {
"$ref": "#/definitions/configurePresets.vendor@v1.."
},
"displayName": {
"$ref": "#/definitions/configurePresets.displayName@v1.."
},
"description": {
"$ref": "#/definitions/configurePresets.description@v1.."
},
"generator": {
"$ref": "#/definitions/configurePresets.generator@v3.."
},
"architecture": {
"$ref": "#/definitions/configurePresets.architecture@v10.."
},
"toolset": {
"$ref": "#/definitions/configurePresets.toolset@v10.."
},
"toolchainFile": {
"$ref": "#/definitions/configurePresets.toolchainFile@v3.."
},
"graphviz": {
"$ref": "#/definitions/configurePresets.graphviz@v10.."
},
"binaryDir": {
"$ref": "#/definitions/configurePresets.binaryDir@v3.."
},
"installDir": {
"$ref": "#/definitions/configurePresets.installDir@v3.."
},
"cmakeExecutable": {
"$ref": "#/definitions/configurePresets.cmakeExecutable@v1.."
},
"cacheVariables": {
"$ref": "#/definitions/configurePresets.cacheVariables@v10.."
},
"environment": {
"$ref": "#/definitions/configurePresets.environment@v1.."
},
"warnings": {
"$ref": "#/definitions/configurePresets.warnings@v13"
},
"errors": {
"$ref": "#/definitions/configurePresets.errors@v13"
},
"debug": {
"$ref": "#/definitions/configurePresets.debug@v10.."
},
"trace": {
"$ref": "#/definitions/configurePresets.trace@v10.."
},
"$comment": {
"$ref": "#/definitions/$comment@v10.."
}
},
"required": [
"name"
]
}
},
"configurePresets.name@v1..": {
"type": "string",
"minLength": 1,
@@ -1281,19 +1405,19 @@
"unevaluatedProperties": false,
"properties": {
"author": {
"$ref": "#/definitions/configurePresets.warnings.author@v12"
"$ref": "#/definitions/configurePresets.warnings.author@v12.."
},
"deprecated": {
"$ref": "#/definitions/configurePresets.warnings.deprecated@v1.."
},
"experimental": {
"$ref": "#/definitions/configurePresets.warnings.experimental@v12"
"$ref": "#/definitions/configurePresets.warnings.experimental@v12.."
},
"installAbsoluteDestination": {
"$ref": "#/definitions/configurePresets.warnings.installAbsoluteDestination@v12"
"$ref": "#/definitions/configurePresets.warnings.installAbsoluteDestination@v12.."
},
"policy": {
"$ref": "#/definitions/configurePresets.warnings.policy@v12"
"$ref": "#/definitions/configurePresets.warnings.policy@v12.."
},
"uninitialized": {
"$ref": "#/definitions/configurePresets.warnings.uninitialized@v1.."
@@ -1309,7 +1433,47 @@
}
}
},
"configurePresets.warnings.author@v12": {
"configurePresets.warnings@v13": {
"type": "object",
"description": "An optional object specifying the warnings to enable.",
"unevaluatedProperties": false,
"properties": {
"author": {
"$ref": "#/definitions/configurePresets.warnings.author@v12.."
},
"deprecated": {
"$ref": "#/definitions/configurePresets.warnings.deprecated@v1.."
},
"experimental": {
"$ref": "#/definitions/configurePresets.warnings.experimental@v12.."
},
"installAbsoluteDestination": {
"$ref": "#/definitions/configurePresets.warnings.installAbsoluteDestination@v12.."
},
"nonTargetDirective": {
"$ref": "#/definitions/configurePresets.warnings.nonTargetDirective@v13"
},
"policy": {
"$ref": "#/definitions/configurePresets.warnings.policy@v12.."
},
"strict": {
"$ref": "#/definitions/configurePresets.warnings.strict@v13"
},
"uninitialized": {
"$ref": "#/definitions/configurePresets.warnings.uninitialized@v1.."
},
"unusedCli": {
"$ref": "#/definitions/configurePresets.warnings.unusedCli@v1.."
},
"systemVars": {
"$ref": "#/definitions/configurePresets.warnings.systemVars@v1.."
},
"$comment": {
"$ref": "#/definitions/$comment@v10.."
}
}
},
"configurePresets.warnings.author@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Wauthor or -Wno-author on the command line. This may not be set to false if errors.author is set to true."
},
@@ -1321,18 +1485,26 @@
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Wdev or -Wno-dev on the command line. This may not be set to false if errors.dev is set to true."
},
"configurePresets.warnings.experimental@v12": {
"configurePresets.warnings.experimental@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Wexperimental or -Wno-experimental on the command line. This may not be set to false if errors.experimental is set to true."
},
"configurePresets.warnings.installAbsoluteDestination@v12": {
"configurePresets.warnings.installAbsoluteDestination@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Winstall-absolute-destination or -Wno-install-absolute-destination on the command line. This may not be set to false if errors.installAbsoluteDestination is set to true."
},
"configurePresets.warnings.policy@v12": {
"configurePresets.warnings.nonTargetDirective@v13": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Wnon-target-directive or -Wno-non-target-directive on the command line. This may not be set to false if errors.nonTargetDirective is set to true."
},
"configurePresets.warnings.policy@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Wpolicy or -Wno-policy on the command line. This may not be set to false if errors.policy is set to true."
},
"configurePresets.warnings.strict@v13": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Wstrict or -Wno-strict on the command line. This may not be set to false if errors.strict is set to true."
},
"configurePresets.warnings.uninitialized@v1..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Wuninitialized or -Wno-uninitialized on the command line. This may not be set to false if errors.uninitialized is set to true."
@@ -1380,32 +1552,69 @@
"unevaluatedProperties": false,
"properties": {
"author": {
"$ref": "#/definitions/configurePresets.errors.author@v12"
"$ref": "#/definitions/configurePresets.errors.author@v12.."
},
"deprecated": {
"$ref": "#/definitions/configurePresets.errors.deprecated@v1.."
},
"experimental": {
"$ref": "#/definitions/configurePresets.errors.experimental@v12"
"$ref": "#/definitions/configurePresets.errors.experimental@v12.."
},
"installAbsoluteDestination": {
"$ref": "#/definitions/configurePresets.errors.installAbsoluteDestination@v12"
"$ref": "#/definitions/configurePresets.errors.installAbsoluteDestination@v12.."
},
"policy": {
"$ref": "#/definitions/configurePresets.errors.policy@v12"
"$ref": "#/definitions/configurePresets.errors.policy@v12.."
},
"uninitialized": {
"$ref": "#/definitions/configurePresets.errors.uninitialized@v12"
"$ref": "#/definitions/configurePresets.errors.uninitialized@v12.."
},
"unusedCli": {
"$ref": "#/definitions/configurePresets.errors.unusedCli@v12"
"$ref": "#/definitions/configurePresets.errors.unusedCli@v12.."
},
"$comment": {
"$ref": "#/definitions/$comment@v10.."
}
}
},
"configurePresets.errors.author@v12": {
"configurePresets.errors@v13": {
"type": "object",
"description": "An optional object specifying the errors to enable.",
"unevaluatedProperties": false,
"properties": {
"author": {
"$ref": "#/definitions/configurePresets.errors.author@v12.."
},
"deprecated": {
"$ref": "#/definitions/configurePresets.errors.deprecated@v1.."
},
"experimental": {
"$ref": "#/definitions/configurePresets.errors.experimental@v12.."
},
"installAbsoluteDestination": {
"$ref": "#/definitions/configurePresets.errors.installAbsoluteDestination@v12.."
},
"nonTargetDirective": {
"$ref": "#/definitions/configurePresets.errors.nonTargetDirective@v13"
},
"policy": {
"$ref": "#/definitions/configurePresets.errors.policy@v12.."
},
"strict": {
"$ref": "#/definitions/configurePresets.errors.strict@v13"
},
"uninitialized": {
"$ref": "#/definitions/configurePresets.errors.uninitialized@v12.."
},
"unusedCli": {
"$ref": "#/definitions/configurePresets.errors.unusedCli@v12.."
},
"$comment": {
"$ref": "#/definitions/$comment@v10.."
}
}
},
"configurePresets.errors.author@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=author or -Wno-error=author on the command line. This may not be set to true if warnings.author is set to false."
},
@@ -1417,23 +1626,31 @@
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=dev or -Wno-error=dev on the command line. This may not be set to true if warnings.dev is set to false."
},
"configurePresets.errors.experimental@v12": {
"configurePresets.errors.experimental@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=experimental or -Wno-error=experimental on the command line. This may not be set to true if warnings.experimental is set to false."
},
"configurePresets.errors.installAbsoluteDestination@v12": {
"configurePresets.errors.installAbsoluteDestination@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=install-absolute-destination or -Wno-error=install-absolute-destination on the command line. This may not be set to true if warnings.installAbsoluteDestination is set to false."
},
"configurePresets.errors.policy@v12": {
"configurePresets.errors.nonTargetDirective@v13": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=non-target-directive or -Wno-error=non-target-directive on the command line. This may not be set to true if warnings.nonTargetDirective is set to false."
},
"configurePresets.errors.policy@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=policy or -Wno-error=policy on the command line. This may not be set to true if warnings.policy is set to false."
},
"configurePresets.errors.uninitialized@v12": {
"configurePresets.errors.strict@v13": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=strict or -Wno-error=strict on the command line. This may not be set to true if warnings.strict is set to false."
},
"configurePresets.errors.uninitialized@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=uninitialized or -Wno-error=uninitialized on the command line. This may not be set to true if warnings.uninitialized is set to false."
},
"configurePresets.errors.unusedCli@v12": {
"configurePresets.errors.unusedCli@v12..": {
"type": "boolean",
"description": "An optional boolean. Equivalent to passing -Werror=unused-cli or -Wno-error=unused-cli on the command line. This may not be set to true if warnings.unusedCli is set to false."
},
@@ -2315,7 +2532,7 @@
]
}
},
"testPresets@v12": {
"testPresets@v12..": {
"type": "array",
"description": "An optional array of test preset objects. Used to specify arguments to ctest.",
"items": {
@@ -2366,7 +2583,7 @@
"$ref": "#/definitions/testPresets.filter@v10.."
},
"execution": {
"$ref": "#/definitions/testPresets.execution@v12"
"$ref": "#/definitions/testPresets.execution@v12.."
},
"$comment": {
"$ref": "#/definitions/$comment@v10.."
@@ -3088,7 +3305,7 @@
}
}
},
"testPresets.execution@v12": {
"testPresets.execution@v12..": {
"type": "object",
"description": "An optional object specifying options for test execution.",
"unevaluatedProperties": false,
@@ -3127,7 +3344,7 @@
"$ref": "#/definitions/testPresets.execution.noTestsAction@v2.."
},
"testPassthroughArguments": {
"$ref": "#/definitions/testPresets.execution.testPassthroughArguments@v12"
"$ref": "#/definitions/testPresets.execution.testPassthroughArguments@v12.."
},
"$comment": {
"$ref": "#/definitions/$comment@v10.."
@@ -3249,7 +3466,7 @@
],
"description": "An optional string specifying the behavior if no tests are found. Must be one of the following values: \"default\" (equivalent to not passing any value on the command line), \"error\" (equivalent to passing --no-tests=error on the command line), or \"ignore\" (equivalent to passing --no-tests-ignore on the command line)."
},
"testPresets.execution.testPassthroughArguments@v12": {
"testPresets.execution.testPassthroughArguments@v12..": {
"type": "array",
"description": "An optional array of strings. Each element is forwarded as an argument to every test executable. Equivalent to passing arguments after -- on the ctest command line.",
"items": {

View File

@@ -1,5 +1,5 @@
---
version: 12
version: 13
types:
- id: condition
type: variant

View File

@@ -50,6 +50,15 @@
:option:`-Wno-install-absolute-destination <cmake -Wno->` on the command line.
This may not be set to ``false`` if ``errors.installAbsoluteDestination`` is set to ``true``.
.. _`CMakePresets.configurePresets.warnings.nonTargetDirective`:
``nonTargetDirective``
.. presets-versionadded:: 13
An optional boolean. Equivalent to passing :option:`-Wnon-target-directive <cmake -W>` or
:option:`-Wno-non-target-directive <cmake -Wno->` on the command line.
This may not be set to ``false`` if ``errors.nonTargetDirective`` is set to ``true``.
.. _`CMakePresets.configurePresets.warnings.policy`:
``policy``
@@ -59,6 +68,15 @@
:option:`-Wno-policy <cmake -Wno->` on the command line.
This may not be set to ``false`` if ``errors.policy`` is set to ``true``.
.. _`CMakePresets.configurePresets.warnings.strict`:
``strict``
.. presets-versionadded:: 13
An optional boolean. Equivalent to passing :option:`-Wstrict <cmake -W>` or
:option:`-Wno-strict <cmake -Wno->` on the command line.
This may not be set to ``false`` if ``errors.strict`` is set to ``true``.
.. _`CMakePresets.configurePresets.warnings.uninitialized`:
``uninitialized``

View File

@@ -0,0 +1,4 @@
presets-v13
-----------
* :manual:`cmake-presets(7)` files now support schema version 13.

View File

@@ -0,0 +1,20 @@
warn-strict
-----------
* The :diagnostic:`CMD_NON_TARGET_DIRECTIVE` diagnostic category was added to
diagnose commands that affect the directory scope when comparable
target-specific commands exist. The use of such directives is disrecommended.
This diagnostic may be controlled with the
:option:`-Wnon-target-directive <cmake -W>` command-line option,
the ``nonTargetDirective`` field in a
:manual:`CMake Presets <cmake-presets(7)>`
:preset:`configurePresets.warnings` object,
or the :command:`cmake_diagnostic` command.
* The :diagnostic:`CMD_STRICT` diagnostic category was added to control
diagnostics regarding allowed but disrecommended usage as a group.
This diagnostic may be controlled with the :option:`-Wstrict <cmake -W>`
command-line option, the ``strict`` field in a
:manual:`CMake Presets <cmake-presets(7)>`
:preset:`configurePresets.warnings` object,
or the :command:`cmake_diagnostic` command.

View File

@@ -2,6 +2,7 @@
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmAddCompileDefinitionsCommand.h"
#include "cmDiagnostics.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
@@ -9,8 +10,14 @@ bool cmAddCompileDefinitionsCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
mf.IssueDiagnostic(cmDiagnostics::CMD_NON_TARGET_DIRECTIVE,
"Use of non-target directives is not recommended. "
"Consider target_compile_definitions instead.");
for (std::string const& i : args) {
mf.AddCompileDefinition(i);
}
return true;
}

View File

@@ -2,6 +2,7 @@
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmAddCompileOptionsCommand.h"
#include "cmDiagnostics.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
@@ -9,8 +10,14 @@ bool cmAddCompileOptionsCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
mf.IssueDiagnostic(cmDiagnostics::CMD_NON_TARGET_DIRECTIVE,
"Use of non-target directives is not recommended. "
"Consider target_compile_options instead.");
for (std::string const& i : args) {
mf.AddCompileOption(i);
}
return true;
}

View File

@@ -2,6 +2,7 @@
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmAddDefinitionsCommand.h"
#include "cmDiagnostics.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
@@ -9,8 +10,16 @@ bool cmAddDefinitionsCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
mf.IssueDiagnostic(
cmDiagnostics::CMD_NON_TARGET_DIRECTIVE,
"Use of non-target directives is not recommended. "
"Consider target_compile_definitions, target_compile_options, or "
"target_include_directories instead.");
for (std::string const& i : args) {
mf.AddDefineFlag(i);
}
return true;
}

View File

@@ -2,6 +2,7 @@
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmAddLinkOptionsCommand.h"
#include "cmDiagnostics.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
@@ -9,8 +10,14 @@ bool cmAddLinkOptionsCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
mf.IssueDiagnostic(cmDiagnostics::CMD_NON_TARGET_DIRECTIVE,
"Use of non-target directives is not recommended. "
"Consider target_link_options instead.");
for (std::string const& i : args) {
mf.AddLinkOption(i);
}
return true;
}

View File

@@ -29,7 +29,9 @@
SELECT(ACTION, Warn, CMD_NONE, CMD_AUTHOR, 12) \
SELECT(ACTION, Warn, CMD_AUTHOR, CMD_DEPRECATED, 1) \
SELECT(ACTION, Warn, CMD_AUTHOR, CMD_EXPERIMENTAL, 12) \
SELECT(ACTION, Ignore, CMD_AUTHOR, CMD_INSTALL_ABSOLUTE_DESTINATION, 12) \
SELECT(ACTION, Ignore, CMD_AUTHOR, CMD_STRICT, 13) \
SELECT(ACTION, Ignore, CMD_STRICT, CMD_INSTALL_ABSOLUTE_DESTINATION, 12) \
SELECT(ACTION, Ignore, CMD_STRICT, CMD_NON_TARGET_DIRECTIVE, 13) \
SELECT(ACTION, Warn, CMD_AUTHOR, CMD_POLICY, 12) \
SELECT(ACTION, Ignore, CMD_NONE, CMD_UNINITIALIZED, 1) \
SELECT(ACTION, Warn, CMD_NONE, CMD_UNUSED_CLI, 1)

View File

@@ -8,6 +8,7 @@
#include <cmext/algorithm>
#include "cmDiagnostics.h"
#include "cmExecutionStatus.h"
#include "cmGeneratorExpression.h"
#include "cmMakefile.h"
@@ -22,12 +23,16 @@ static void NormalizeInclude(cmMakefile& mf, std::string& inc);
bool cmIncludeDirectoryCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
mf.IssueDiagnostic(cmDiagnostics::CMD_NON_TARGET_DIRECTIVE,
"Use of non-target directives is not recommended. "
"Consider target_include_directories instead.");
if (args.empty()) {
return true;
}
cmMakefile& mf = status.GetMakefile();
auto i = args.begin();
bool before = mf.IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE");

View File

@@ -2,6 +2,7 @@
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmLinkDirectoriesCommand.h"
#include "cmDiagnostics.h"
#include "cmExecutionStatus.h"
#include "cmGeneratorExpression.h"
#include "cmList.h"
@@ -15,11 +16,16 @@ static void AddLinkDir(cmMakefile& mf, std::string const& dir,
bool cmLinkDirectoriesCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
mf.IssueDiagnostic(cmDiagnostics::CMD_NON_TARGET_DIRECTIVE,
"Use of non-target directives is not recommended. "
"Consider target_link_directories instead.");
if (args.empty()) {
return true;
}
cmMakefile& mf = status.GetMakefile();
bool before = mf.IsOn("CMAKE_LINK_DIRECTORIES_BEFORE");
auto i = args.cbegin();

View File

@@ -2,16 +2,23 @@
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmLinkLibrariesCommand.h"
#include "cmDiagnostics.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
bool cmLinkLibrariesCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
cmMakefile& mf = status.GetMakefile();
mf.IssueDiagnostic(cmDiagnostics::CMD_NON_TARGET_DIRECTIVE,
"Use of non-target directives is not recommended. "
"Consider target_link_libraries instead.");
if (args.empty()) {
return true;
}
cmMakefile& mf = status.GetMakefile();
// add libraries, note that there is an optional prefix
// of debug and optimized than can be used
for (auto i = args.begin(); i != args.end(); ++i) {

View File

@@ -0,0 +1,56 @@
CMake Warning \(non-target-directive\) at NonTargetDirectives\.cmake:[0-9]+ \(add_definitions\):
Use of non-target directives is not recommended\. Consider
target_compile_definitions, target_compile_options, or
target_include_directories instead\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
This warning is for project developers\. Use -Wno-author or
-Wno-non-target-directive to suppress it\.
CMake Warning \(non-target-directive\) at NonTargetDirectives\.cmake:[0-9]+ \(add_compile_definitions\):
Use of non-target directives is not recommended\. Consider
target_compile_definitions instead\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
This warning is for project developers\. Use -Wno-author or
-Wno-non-target-directive to suppress it\.
CMake Warning \(non-target-directive\) at NonTargetDirectives\.cmake:[0-9]+ \(add_compile_options\):
Use of non-target directives is not recommended\. Consider
target_compile_options instead\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
This warning is for project developers\. Use -Wno-author or
-Wno-non-target-directive to suppress it\.
CMake Warning \(non-target-directive\) at NonTargetDirectives\.cmake:[0-9]+ \(add_link_options\):
Use of non-target directives is not recommended\. Consider
target_link_options instead\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
This warning is for project developers\. Use -Wno-author or
-Wno-non-target-directive to suppress it\.
CMake Warning \(non-target-directive\) at NonTargetDirectives\.cmake:[0-9]+ \(include_directories\):
Use of non-target directives is not recommended\. Consider
target_include_directories instead\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
This warning is for project developers\. Use -Wno-author or
-Wno-non-target-directive to suppress it\.
CMake Warning \(non-target-directive\) at NonTargetDirectives\.cmake:[0-9]+ \(link_directories\):
Use of non-target directives is not recommended\. Consider
target_link_directories instead\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
This warning is for project developers\. Use -Wno-author or
-Wno-non-target-directive to suppress it\.
CMake Warning \(non-target-directive\) at NonTargetDirectives\.cmake:[0-9]+ \(link_libraries\):
Use of non-target directives is not recommended\. Consider
target_link_libraries instead\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
This warning is for project developers\. Use -Wno-author or
-Wno-non-target-directive to suppress it\.

View File

@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 4.4)
project(NonTargetDirectives)
# Test diagnostics for non-target directives. Note that, because we don't
# need to actually compile anything, it doesn't matter if we use options,
# directories, or libraries that don't exist.
add_definitions(-DFOO)
add_compile_definitions(BAR)
add_compile_options(-fPIC)
add_link_options(-Wl,--no-undefined)
include_directories(${CMAKE_SOURCE_DIR}/include)
link_directories(${CMAKE_SOURCE_DIR}/lib)
link_libraries(/usr/lib/libm.so)

View File

@@ -29,3 +29,6 @@ block()
set(RunCMake_TEST_NO_CLEAN 1)
run_persist_test(Persist2 -Wauthor)
endblock()
run_cmake_with_options(NonTargetDirectives -Wstrict)
run_cmake_with_options(NonTargetDirectives -Wnon-target-directive)