Commit Graph

420 Commits

Author SHA1 Message Date
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
Ben Boeckel
716dfd3b1e cmFindCommon: track debug state as an object
This also means that the `DebugState` instances do not need to ask if
debug mode is enabled: if they exist, it is enabled.
2025-04-29 20:10:43 +02:00
Ben Boeckel
bd5cb1f8e6 cmFindCommon: use DebugModeEnabled to query for debug mode
This simplifies changing how debug mode is represented.
2025-04-29 20:10:43 +02:00
Matthew Woehlke
de5fa2ae53 find_package: Improve support for CPS multiple inclusion
Keep track of CPS files we have imported in CMake's state, and use this
(instead of the prior, temporary work-around that used `<name>_CONFIG`)
as a guard for trying to import more than once from the same file. This
has two advantages; first, it is robust against finding the same package
name in different locations in alternating searches, and second, it
allows us to load additional appendices associated with a root package
that has already been loaded.

Fixes: #26731
2025-04-03 14:40:39 -04:00
Brad King
be1f4dde2e Merge topic 'find_package-cps-multiple-inclusion'
b498611902 find_package: Add minimal support for CPS multiple inclusion

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10581
2025-04-03 14:06:53 -04:00
Matthew Woehlke
b498611902 find_package: Add minimal support for CPS multiple inclusion
Do not try to load a CPS file if we've already loaded the same file.
This only works if the current `find_package` call selects the same file
as the previous most recent call, and if the user has not meddled with
the `<name>_CONFIG` variable.

Issue: #26731
2025-04-02 13:45:36 -04:00
Brad King
ff5712fbfc Merge topic 'find_package-fix-nested-calls'
37823b366f find_package: Restore component requirements in nested calls

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !10576
2025-04-02 10:23:28 -04:00
Matthew Woehlke
37823b366f find_package: Restore component requirements in nested calls
Fix logic to populate required and optional components from CMake
variables when `find_package` is called in a nested context.

This was broken in commit e2a6416622 (find_package: Refactor in support
of recursion, 2024-11-29, v4.0.0-rc1~356^2), which promoted the
component sets from locals (in cmFindPackageCommand::InitialPass) to
member variables. Previously, in a nested context, these sets were
simply not filled, and we relied on the variables indicating component
requirement to already be set. When logic was added to properly fill the
sets (which is needed for CPS), it blindly dumped all components into
the required set, without actually checking whether the context had
marked the components as required or optional.

Fixes: #26824
2025-04-02 10:16:40 -04:00
Brad King
2b8b17106c FindCABLE: Add policy to remove this module
CABLE has not been maintained in a long time.
2025-03-27 09:48:49 -04:00
Brad King
d84881dce6 Merge topic 'fix-cps-cmake-deps'
c3d279841b find_package: CPS component requirements != CMake components

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10427
2025-03-06 07:53:27 -05:00
Matthew Woehlke
c3d279841b find_package: CPS component requirements != CMake components
Modify how CMake handles required components of a CPS transitive
dependency to not pass them as COMPONENTS if a CMake-script package is
found as the resolved dependency. This is necessary as many CMake-script
package description files do not treat component requests as target
requests (which, in CPS-land, they effectively are), but do implement
logic to mark themselves 'not found' if requested components are
missing. As a result, passing in the required targets as required
components is likely to cause the dependency to be spuriously not found
if it is only available via a CMake-script package configuration file.

Fix this by introducing a new 'required targets' concept, and by passing
CPS component requirements as both required targets and optional
components. The latter serves as a hint for packages that might provide
only a subset of themselves. The former is used to post-validate a
CMake-script package, or is folded on-the-fly into required components
when considering CPS packages.

Note that this functionality is not exposed to the user at this time,
and is only used when resolving transitive dependencies for a CPS
package.
2025-03-05 14:47:49 -05:00
Kitware Robot
1772622772 LICENSE: Replace references to Copyright.txt with LICENSE.rst
```
git grep -lz 'Copyright.txt or https://cmake.org/licensing ' |
  while IFS= read -r -d $'\0' f ; do
    sed -i '/Copyright.txt or https:\/\/cmake.org\/licensing / {
              s/Copyright.txt/LICENSE.rst/
            }' "$f" ; done
```
2025-03-03 10:43:35 -05:00
Kitware Robot
de273b2e11 LICENSE: Replace references to Copyright.txt with LICENSE.rst
```
git grep -lz 'Copyright.txt or https://cmake.org/licensing ' |
  while IFS= read -r -d $'\0' f ; do
    sed -i '/Copyright.txt or https:\/\/cmake.org\/licensing / {
              s/Copyright.txt/LICENSE.rst/
            }' "$f" ; done
```
2025-03-03 09:56:07 -05:00
Brad King
75da788360 Merge topic 'find-package-required'
857a039d66 find_*: Add variable to default calls to REQUIRED

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10364
2025-02-28 11:15:23 -05:00
Martin Duffy
857a039d66 find_*: Add variable to default calls to REQUIRED
This adds a `CMAKE_FIND_REQUIRED` variable which causes `find_package`,
`find_path`, `find_file`, `find_library` and `find_program` to be
considered `REQUIRED` by default.

It also introduces an `OPTIONAL` keyword to those commands, allowing
them to ignore the value of this variable.

Issue: #26576
2025-02-27 10:51:07 -05:00
Hongxu Xu
0e2b87244c find_package: Fix CMAKE_FIND_PACKAGE_SORT_DIRECTION documented default
Previously the documentation said the default was `DEC`,
but the implementation has always defaulted to `ASC`.

Fixes: #22728
2025-02-24 15:03:34 -05:00
Brad King
2123244746 FindGCCXML: Add policy to remove this module
GCC-XML has been superseded by CastXML for a long time.

Closes: #26687
2025-02-17 11:35:25 -05:00
Brad King
a0aa37180c Merge topic 'find-cps-version'
3e6466eb16 find_package: Honor version requests when finding CPS packages
7a0e698384 find_package: Fix CPS version parsing
35a7ed125b find_package: Fix reporting of rejected CPS files' version

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10297
2025-02-13 09:33:06 -05:00
Brad King
3f2386db2b Merge topic 'find-cps-version' into release-4.0
3e6466eb16 find_package: Honor version requests when finding CPS packages
7a0e698384 find_package: Fix CPS version parsing
35a7ed125b find_package: Fix reporting of rejected CPS files' version

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !10297
2025-02-13 09:33:04 -05:00
Matthew Woehlke
3e6466eb16 find_package: Honor version requests when finding CPS packages
Teach find_package to check a CPS package's version (when provided)
against a version request given to the find_package invocation.
2025-02-12 11:36:41 -05:00
Matthew Woehlke
7a0e698384 find_package: Fix CPS version parsing
Update cmPackageInfoReader's version parsing to more fully conform to
the specification and to reject non-conforming version strings. Start
adding framework to support version schemas other than "simple". Fix how
cmFindPackageCommand extracts version parts to not fail if more than
four parts are present.
2025-02-12 11:35:54 -05:00
Matthew Woehlke
caff0d62c6 cmFindPackageCommand: Simplify cmStrCat
Replace an unnecessary single-character string_view in cmStrCat with a
literal character.
2025-02-11 12:29:06 -05:00
Matthew Woehlke
35a7ed125b find_package: Fix reporting of rejected CPS files' version
The logic to extract the version of a CPS file into the location used to
record files that were considered but rejected was happening too late,
resulting in rejected files unnecessarily reporting their version as
"unknown". Fix this by filling the variable sooner.
2025-02-07 11:57:54 -05:00
Matthew Woehlke
894f256a12 find_package: Find CPS components
Implement finding components of CPS packages. Specifically, reject any
candidate packages that don't provide all required components, and
ignore appendices that don't provide requested (required or optional)
components. This applies to both top-level searches and also searching
for package dependencies.
2025-02-01 06:44:43 -05:00
Matthew Woehlke
ac20cd43c0 find_package: Fix reporting of transitive dependency errors
Fix find_package to set an error message when trying to find
dependencies of a CPS package fails. This fixes the command previously
reporting "find_package unknown error" for such failures.
2025-02-01 06:20:14 -05:00