Merge topic 'fix-cps-arg-parsing' into release-4.2

41b6b07527 cmExportCommand: Fix PACKAGE_INFO argument parsing

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !11459
This commit is contained in:
Brad King
2025-12-02 20:19:19 +00:00
committed by Kitware Robot
12 changed files with 64 additions and 43 deletions

View File

@@ -250,22 +250,21 @@ static bool HandleExportMode(std::vector<std::string> const& args,
if (arguments.PackageInfo) {
if (arguments.PackageInfo->PackageName.empty()) {
if (!arguments.PackageInfo->Check(status, false)) {
return false;
}
} else {
if (!arguments.Filename.empty()) {
status.SetError("PACKAGE_INFO and FILE are mutually exclusive.");
return false;
}
if (!arguments.Namespace.empty()) {
status.SetError("PACKAGE_INFO and NAMESPACE are mutually exclusive.");
return false;
}
if (!arguments.PackageInfo->Check(status) ||
!arguments.PackageInfo->SetMetadataFromProject(status)) {
return false;
}
// TODO: Fix our use of the parser to enforce this.
status.SetError("PACKAGE_INFO missing required value.");
return false;
}
if (!arguments.Filename.empty()) {
status.SetError("PACKAGE_INFO and FILE are mutually exclusive.");
return false;
}
if (!arguments.Namespace.empty()) {
status.SetError("PACKAGE_INFO and NAMESPACE are mutually exclusive.");
return false;
}
if (!arguments.PackageInfo->Check(status) ||
!arguments.PackageInfo->SetMetadataFromProject(status)) {
return false;
}
}

View File

@@ -2184,6 +2184,12 @@ bool HandlePackageInfoMode(std::vector<std::string> const& args,
return false;
}
if (arguments.PackageName.empty()) {
// TODO: Fix our use of the parser to enforce this.
status.SetError(cmStrCat(args[0], " missing package name."));
return false;
}
if (exportName.empty()) {
status.SetError(cmStrCat(args[0], " missing EXPORT."));
return false;

View File

@@ -19,11 +19,6 @@ template void cmPackageInfoArguments::Bind<void>(cmArgumentParser<void>&,
namespace {
bool ArgWasSpecified(bool value)
{
return value;
}
bool ArgWasSpecified(std::string const& value)
{
return !value.empty();
@@ -52,26 +47,8 @@ bool ArgWasSpecified(std::vector<std::string> const& value)
} \
} while (false)
bool cmPackageInfoArguments::Check(cmExecutionStatus& status,
bool enable) const
bool cmPackageInfoArguments::Check(cmExecutionStatus& status) const
{
if (!enable) {
// Check if any options were given.
ENFORCE_REQUIRES("PACKAGE_INFO", this->LowerCase, "LOWER_CASE_FILE");
ENFORCE_REQUIRES("PACKAGE_INFO", this->Appendix, "APPENDIX");
ENFORCE_REQUIRES("PACKAGE_INFO", this->Version, "VERSION");
ENFORCE_REQUIRES("PACKAGE_INFO", this->License, "LICENSE");
ENFORCE_REQUIRES("PACKAGE_INFO", this->DefaultLicense, "DEFAULT_LICENSE");
ENFORCE_REQUIRES("PACKAGE_INFO", this->Description, "DESCRIPTION");
ENFORCE_REQUIRES("PACKAGE_INFO", this->Website, "HOMEPAGE_URL");
ENFORCE_REQUIRES("PACKAGE_INFO", this->DefaultTargets, "DEFAULT_TARGETS");
ENFORCE_REQUIRES("PACKAGE_INFO", this->DefaultConfigs,
"DEFAULT_CONFIGURATIONS");
ENFORCE_REQUIRES("PACKAGE_INFO", this->ProjectName, "PROJECT");
ENFORCE_REQUIRES("PACKAGE_INFO", this->NoProjectDefaults,
"NO_PROJECT_METADATA");
}
// Check for incompatible options.
if (!this->Appendix.empty()) {
ENFORCE_EXCLUSIVE("APPENDIX", this->Version, "VERSION");

View File

@@ -43,9 +43,8 @@ public:
std::string GetPackageDirName() const;
std::string GetPackageFileName() const;
/// Ensure that no conflicting options were specified. If \p enable is
/// \c false, forbid specifying any options whatsoever.
bool Check(cmExecutionStatus& status, bool enable = true) const;
/// Ensure that no conflicting options were specified.
bool Check(cmExecutionStatus& status) const;
/// Set metadata (not already specified) from either the specified project,
/// or from the project which matches the package name.

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,4 @@
CMake Error at BadArgs0\.cmake:3 \(export\):
export PACKAGE_INFO missing required value\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)

View File

@@ -0,0 +1,3 @@
add_library(foo INTERFACE)
install(TARGETS foo EXPORT foo DESTINATION .)
export(EXPORT foo PACKAGE_INFO)

View File

@@ -12,6 +12,7 @@ set(RunCMake_TEST_OPTIONS
)
# Test incorrect usage
run_cmake(BadArgs0)
run_cmake(BadArgs1)
run_cmake(BadArgs2)
run_cmake(BadArgs3)

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,22 @@
CMake Error at BadArgs0\.cmake:1 \(install\):
install PACKAGE_INFO missing package name\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
CMake Error at BadArgs0\.cmake:2 \(install\):
install PACKAGE_INFO missing EXPORT\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
CMake Error at BadArgs0\.cmake:3 \(install\):
install PACKAGE_INFO missing EXPORT\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)
CMake Error at BadArgs0\.cmake:7 \(install\):
install PACKAGE_INFO missing package name\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)

View File

@@ -0,0 +1,7 @@
install(PACKAGE_INFO)
install(PACKAGE_INFO test)
install(PACKAGE_INFO test EXPORT)
add_library(foo INTERFACE)
install(TARGETS foo EXPORT foo DESTINATION .)
install(PACKAGE_INFO EXPORT foo)

View File

@@ -25,6 +25,7 @@ function(run_cmake_install test)
endfunction()
# Test incorrect usage
run_cmake(BadArgs0)
run_cmake(BadArgs1)
run_cmake(BadArgs2)
run_cmake(BadName)