444 Commits
Author SHA1 Message Date
Tyler Yankee f460a199e3 Source: Add and use CM_UNREACHABLE macro
In debug builds, we assert and abort to give more useful crashes for
development (i.e., instead of triggering UB). Otherwise, we get the
optimization benefits of `unreachable()` (or the older
platform-specific fallbacks per `cm/utility`).
2026-08-26 13:24:34 -04:00
Tyler Yankee 45a426a4e3 codespell: Fix some hyphenated spellings
These were surfaced by an updated `codespell` in the `spellcheck` CI
base image.
2026-08-21 09:43:48 -04:00
Matthew Woehlke f1d7bedc4b TargetTypes: Improve use of imported target scope type
Move `ImportedTargetScope` to `cmTargetTypes.h`, enabling it to be used
without dragging in all of `cmMakefile.h`. Use this in more places
instead of `bool global`.
2026-07-01 14:44:11 -04:00
Matthew Woehlke a5a9ec9113 TargetTypes: Extract target-related types to dedicated header
Move some target related enumeration types from `cmStateTypes.h` to a
new header, as these really aren't "state" types. Also, make
`TargetType` a strongly-typed enumeration.
2026-07-01 14:44:11 -04:00
Matthew Woehlke a7415d243a Diagnostics: Simplify issuing policy warnings
Add and use a couple helper functions for issuing policy warnings. This
improves consistency and allows some simplification of many call sites.
(One or two instances in particular are greatly simplified.)
2026-05-20 11:55:13 -04:00
Matthew Woehlke 028d3183ad Diagnostics: Add POLICY, EXPERIMENTAL
Add new diagnostic categories CMD_POLICY and CMD_EXPERIMENTAL, both as
children of CMD_AUTHOR. Use the former when issuing policy warnings. Use
the latter when issuing experimental feature warnings.
2026-05-15 09:57:23 -04:00
Matthew Woehlke 1309532390 Diagnostics: Use new methods
Find places that are currently relying on diagnostic-specific message
types to issue diagnostics and replace these with calls to the new
diagnostic methods.
2026-04-16 15:10:35 -04:00
Brad King e1c28e5011 Merge topic 'find_package-stack'
1b946dfbfe Merge branch 'backport-4.2-find_package-stack' into find_package-stack
e8ae3645db Merge branch 'backport-4.2-find_package-stack' into find_package-stack
aae02ee60a find_package: Share package information among copies of package stack
a789c21100 Merge branch 'backport-4.2-find_package-stack' into find_package-stack
9387988626 find_package: Save package information only after successfully loading it
561ece2407 cmFindPackageStack: Restore pure value semantics
e935ed22fb find_package: Share package information among copies of package stack
5aa649d5f6 find_package: Save package information only after successfully loading it
...

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !11887
2026-04-02 13:23:54 -04:00
Brad King 1b946dfbfe Merge branch 'backport-4.2-find_package-stack' into find_package-stack 2026-04-01 13:54:11 -04:00
Brad King aae02ee60a find_package: Share package information among copies of package stack
Since commit ae373e93fb (install(PACKAGE_INFO): Add version and location
to package dependencies, 2025-07-31, v4.2.0-rc1~340^2) we add package
information to the current package's stack entry as it is discovered.
However, a nested package can cause the current package's stack entry to
be replaced by a copy with a new ordering index due to commit c6e6861e63
(install(EXPORT): Export find_dependency() calls, 2023-11-07,
v3.29.0-rc1~439^2~1).  Depending on whether imported targets were
created by the current package before finding the nested package, the
`CurrentPackageInfo` pointer is either invalidated, or left pointing at
only one of multiple copies.

Fix this by sharing a single instance of package information with all
copies of the current package's stack entry.  Keep a mutable pointer
to the current package's information only while it is still pending.
This also avoids the need to expose mutation from the package stack.

Add a test that exposes the previously-invalidated pointer to dynamic
analysis tools.

Fixes: #27730
2026-04-01 13:53:07 -04:00
Brad King 9387988626 find_package: Save package information only after successfully loading it
If a package configuration file sets `<PackageName>_FOUND` to false,
the package is considered not found.  Do not save its package info.

Note that this exposes an existing pointer invalidation on nested
`find_package` calls, which will be fixed in following commits.

Issue: #27730
2026-04-01 13:51:11 -04:00
Brad King 561ece2407 cmFindPackageStack: Restore pure value semantics
A stack entry's storage may be shared by other copies, so mutation is
incompatible with value semantics.  We've migrated the motivating use
case to another approach.

Revert commit b3873b8272 (cmFindPackageStack: Allow controlled mutation,
2025-07-29, v4.2.0-rc1~438^2) and commit f2bdc2176f (cmStack: New,
mutable stack class, 2025-07-29, v4.2.0-rc1~438^2~1).  Record their
parent as a second parent of this commit so `git blame` can see the
original history of the restored content.
2026-04-01 13:47:24 -04:00
Brad King e935ed22fb find_package: Share package information among copies of package stack
Since commit ae373e93fb (install(PACKAGE_INFO): Add version and location
to package dependencies, 2025-07-31, v4.2.0-rc1~340^2) we add package
information to the current package's stack entry as it is discovered.
However, a nested package can cause the current package's stack entry to
be replaced by a copy with a new ordering index due to commit c6e6861e63
(install(EXPORT): Export find_dependency() calls, 2023-11-07,
v3.29.0-rc1~439^2~1).  Depending on whether imported targets were
created by the current package before finding the nested package, the
`CurrentPackageInfo` pointer is either invalidated, or left pointing at
only one of multiple copies.

Fix this by sharing a single instance of package information with all
copies of the current package's stack entry.  Keep a mutable pointer
to the current package's information only while it is still pending.
This also avoids the need to expose mutation from the package stack.

Add a test that exposes the previously-invalidated pointer to dynamic
analysis tools.

Fixes: #27730
2026-04-01 13:47:01 -04:00
Brad King 5aa649d5f6 find_package: Save package information only after successfully loading it
If a package configuration file sets `<PackageName>_FOUND` to false,
the package is considered not found.  Do not save its package info.

Note that this exposes an existing pointer invalidation on nested
`find_package` calls, which will be fixed in following commits.

Issue: #27730
2026-04-01 13:47:01 -04:00
Matthew Woehlke e74fbb3d44 Diagnostics: Implement diagnostic stack
Create a stack state for managing diagnostics. This will eventually
replace some of the global state used for diagnostics, as well as
providing a mechanism to add additional diagnostics.

For now, this is only the stack manipulation with a bare minimum of
interface logic (in particular, the `block` and `include` commands).
Nothing actually looks at the diagnostic state yet, and the primary user
interface does not yet exist (although there are some references to it
already).
2026-03-18 14:32:21 -04:00
Matthew Woehlke d80a38e524 cmMakefile: Use enumeration for policy scope
Create `cmPolicyScope` to describe whether `ReadDependentFile` creates a
policy scope, rather than a bare `bool`. Replace a local enumeration in
`cmFindPackageCommand` serving the same purpose with this new, global
one.
2026-03-13 16:44:09 -04:00
Alex Overchenko 70b35324ec Source: Reduce const char* to std::string conversions 2026-03-10 11:45:36 -04:00
Matthew Woehlke f5c515cbf2 CPS: Remove experimental gate (mostly)
Remove basic CPS import and export from 'experimental' status. Update
documentation and tests accordingly.

Note that mapped exports (CMAKE_EXPERIMENTAL_MAPPED_PACKAGE_INFO) are
still experimental.
2026-02-11 16:10:29 -05:00
John Franklin Rickard d932b6fb8d Prefer cmHasSuffix for specific extension checks 2026-02-06 12:27:23 -05:00
John Franklin Rickard e531e7420d Reduce string copies by using View function alternatives 2026-02-06 12:27:13 -05:00
Brad King da84370cff Merge topic 'sbom-export-generators'
8ec5a595cd cmSbom: Add documentation for `install(SBOM)` and `export(SBOM)`
83671f2d87 cmSbom: Add `export(SBOM)` build generators and unit tests
f2027a886b cmSbom: Add `install(SBOM)` generator and unit tests
dcadde3662 CPS: Refactor ForbidGeneratorExpressions into cmGeneratorExpression
bcb6f689a4 cmExportInstallFileGenerator: Allow overriding of CheckInterfaceDirs
6d6c37c90d cmPackageInfoArguments: Extract common values
8c6676b2ea cmFindPackage: Fix PACKAGE_VERSION not being set when rebuilding

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !11396
2025-12-18 10:13:32 -05:00
Taylor Sasser 8c6676b2ea cmFindPackage: Fix PACKAGE_VERSION not being set when rebuilding 2025-12-16 16:17:58 -05:00
Matthew Woehlke df94baa828 find_package: Support GLOBAL for CPS imports
Teach CPS import to respect `find_package(GLOBAL)`.

Fixes: #27370
2025-12-12 17:31:26 -05:00
AJIOB fe173b68f0 Source: Use cmStrCat in place of string addition 2025-12-09 11:06:59 -05:00
Matthew Woehlke fa4bed7844 cmPackageInfoReader: Don't crash if input is malformed
Check all instances of converting a JSON value to a string to ensure
that we check first if the value is convertible, in order to avoid an
exception being thrown, which crashes CMake. Modify some instances to
report when we encounter such invalid values. (Many instances, however,
just silently ignore invalid values.)

Fixes: #27350
2025-11-05 11:29:13 -05:00
Matthew Woehlke 6c2fc502b6 find_package: Report why a candidate was rejected
Improve how find_package reports the list of candidate package
configuration files that were considered but rejected to include a
reason for rejection. For CPS in particular, this allows the user to
tell if a CPS file was rejected due to a version mismatch, missing
required components, or because the file could not be read.

While we do not try to report more detail for why cmPackageInfoReader
rejected a file, the possible reasons are usually easy enough to
distinguish:

- The file is so malformed that we cannot read a JSON object.
- The schema version is not a version that CMake understands.
- The root object does not contain a string named "name".
- The root object does not contain an object named "components".
- Prefix resolution failed.

Three of these can only result from a file that fails schema validation.
2025-11-05 10:45:23 -05:00
Brad King e9beb26909 Merge topic 'pkg-root-var'
1a16e4fc69 Merge branch 'backport-pkg-root-var' into pkg-root-var
87a661a916 find_package: Fix regression on `<PackageName>_ROOT` relative to CWD
fc059e1978 Merge branch 'backport-pkg-root-var' into pkg-root-var
ab0a44a0a0 find_package: Fix regression on `<PackageName>_ROOT` relative to CWD
d029b828b2 find_package: Avoid repeating search for lower-case .cps file
fc4d238d7a Tests: Match RunCMake.find_package-CPS version candidates more precisely

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !11277
2025-10-03 11:12:07 -04:00
Robert Maynard 87a661a916 find_package: Fix regression on <PackageName>_ROOT relative to CWD
In commit 9d44a77454 (find_*: Explicitly normalize found paths as they
exist on disk, 2024-10-17, v4.0.0-rc1~597^2~1), we removed path
normalization from the internal `cmSearchPath::AddPathInternal` helper.
Most call sites were updated to normalize input paths first, but search
paths derived from `<PackageName>_ROOT` CMake variables are no longer
normalized.  Instead we normalize the path to the file found after
searching.

When `find_package` "config" mode considers a candidate CMake package
configuration file, normalize its path before loading the adjacent
package version file so that the latter is loaded by absolute path.
Otherwise `cmMakefile::ReadDependentFile` interprets a relative path
with respect to the current source directory rather than the current
working directory.

Fixes: #27279
2025-10-02 15:30:13 -04:00
Brad King fc059e1978 Merge branch 'backport-pkg-root-var' into pkg-root-var 2025-10-02 15:28:03 -04:00
Brad King d029b828b2 find_package: Avoid repeating search for lower-case .cps file 2025-10-02 15:24:47 -04:00
Brad King c5a1675436 cmVersionMacros: Adopt CMake_VERSION_ENCODE 2025-09-24 17:36:05 -04:00
Sergiu Deitsch 7040406f86 cmListFileLexer: Do not require null-terminated input 2025-09-12 18:28:55 +02:00
Matthew Woehlke c6a6d47233 find_package: CPS targets use CMP0200
Tweak CPS import to actually set CMP0200 NEW on targets imported from
CPS (as the documentation claims we do).
2025-09-02 11:40:18 -04:00
Taylor Sasser ae373e93fb install(PACKAGE_INFO): Add version and location to package dependencies
Refactor `cmFindPackageStack` to track additional metadata
about <package> found. This includes two new fields,
`Version` and `Location` which correspond to package version and path.
The remaining package information will be implemented in a later commit
2025-08-12 11:07:09 -04:00
Matthew Woehlke b3873b8272 cmFindPackageStack: Allow controlled mutation
As mentioned in the previous commit, we would like to record additional
information in the find-package stack, but we don't have the information
at the point a stack entry is created. This necessitates making the
stack mutable. However, in order to restrict mutation, do not directly
expose the mutable value, and instead arrange for it to be accessible
only via cmFindPackageStackRAII (renamed and extracted from cmMakefile).
This ensures that mutation can only happen while the stack is being
built.
2025-07-30 11:01:14 -04:00
Vito Gamberini f61768107e find_package: Implement UNWIND_INCLUDE
This implements a limited exception mechanism for find_package() via
the UNWIND_INCLUDE keyword.

When package discovery via find_package(UNWIND_INCLUDE) fails the
StateSnapshot is updated to an UNWINDING state. In this state further
calls to find_package() and include() are forbidden. While in the
UNWINDING state, the include() command immediately calls
SetReturnInvoked() whenever it is reached.

The UNWINDING state is reset when a parent call to find_package() is
reached.

Fixes: #26897
2025-07-13 16:08:19 -04:00
Ben Boeckel c42d82d569 find_*: support suppressing implicit transition events
When projects explicitly unset variables, the transition detection can
end up reporting events when they are not desired.

See: #24833
2025-06-11 20:09:32 +02:00
Ben Boeckel 0e53a67a7b cmFindPackage: fix over-reporting
Both track when a `_DIR` variable is already defined and suppress
implicit event reporting when a mode that has no reliable method for
such detection.
2025-06-10 20:01:12 +02:00
Ben Boeckel 8b3fc9578a cmFindPackageCommand: report found files to debugging infra 2025-06-09 12:24:32 -04:00
Ben Boeckel 928a74f684 cmFindCommon: always track configure log information
This does mean that debug state is always around, so instead gate the
non-configure log information behind a dedicated variable.
2025-06-09 12:24:31 -04:00
Brad King 6be0c6d26a Merge topic 'astgrep-cmstrcat'
557c44b93e cmStrCat: use character literals where possible
23779057fd cmStrCat: combine neighboring arguments where possible
483d13daf4 ast-grep: add a rule to turn strings into characters
61743471d9 ast-grep: add a rule to find adjacent string literals in cmStrCat calls

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10790
2025-05-22 08:47:15 -04:00
Brad King edb80c068b Merge topic 'find-package-event-providers'
3d60b0ba1d configure-log: fill in dependency provider information

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !10785
2025-05-20 09:13:36 -04:00
Ben Boeckel 557c44b93e cmStrCat: use character literals where possible
Found and replaced using the `cmstrcat-to-char-literal` rule for
`ast-grep`.
2025-05-15 19:12:04 +02:00
Ben Boeckel 3ac505f79d cmFindCommon: log search path variables
No pruning of variables which are not relevant is performed as it would
involve duplication of determining which paths to manage.
2025-05-15 12:24:58 +02:00
Ben Boeckel 3d60b0ba1d configure-log: fill in dependency provider information
This uses the special "path" of `dependency_provider::…` to indicate the
provider in use.
2025-05-14 22:12:46 +02:00
Ben Boeckel 8df9819839 find_package: generate find_package-v1 configure log events
Since `find_package` is far more complex than other `find_*` commands,
it gets its own event.

Closes: #24833
2025-05-09 16:46:43 +02:00
Brad King 52903a0ae4 Merge topic 'find-event-refactor'
fdccb8846c cmFindCommonDebugState: adopt event writing logic
716dfd3b1e cmFindCommon: track debug state as an object
c6d097135d cmFindBaseDebugState: factor out a base class
bd5cb1f8e6 cmFindCommon: use `DebugModeEnabled` to query for debug mode
76ea613f4c cmFindBaseDebugState: get the command name directly from the object

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !10713
2025-05-02 11:07:02 -04:00
Brad King 0556c1b455 Merge topic 'fix-find-performane-regression'
bb3a348def find_package: Fix performance regression in 4.0.0 release

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !10731
2025-05-01 15:50:28 -04:00
Matthew Woehlke bb3a348def find_package: Fix performance regression in 4.0.0 release
In commit e90f60f864 (find_package: Don't glob certain macOS paths,
2024-10-23, v4.0.0-rc1~579^2~1) we changed the name matching logic of
`find_package` to check if a possible match is a directory before
checking whether the name is a match.  In some situations, this results
in unnecessarily calling `stat` for a very large number of files, which
can be extremely slow on some systems (especially Windows).  Fix this by
making the check the last thing we do before accepting a possible match.

Fixes: #26817
2025-05-01 14:48:33 -04:00
Ben Boeckel fdccb8846c cmFindCommonDebugState: adopt event writing logic
This lays the groundwork for `find_package` also using the same
triggering logic.
2025-05-01 12:47:24 +02:00