From 215b9148eb0841224496e55f993b9ad7da55101e Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Wed, 31 Aug 2022 09:41:42 -0400 Subject: CMakePresets.json: Fix formatting of --list-presets=all Only print an extra newline after a section if that section was actually printed. --- Source/cmCMakePresetsGraph.cxx | 37 ++++++++++++++++------ Source/cmCMakePresetsGraph.h | 17 +++++++--- .../CMakePresets/ListAllPresetsNoBuild-stdout.txt | 8 +++++ .../CMakePresets/ListAllPresetsNoBuild.json.in | 14 ++++++++ .../CMakePresets/ListAllPresetsNoTest-stdout.txt | 8 +++++ .../CMakePresets/ListAllPresetsNoTest.json.in | 14 ++++++++ Tests/RunCMake/CMakePresets/RunCMakeTest.cmake | 6 ++++ 7 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild-stdout.txt create mode 100644 Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild.json.in create mode 100644 Tests/RunCMake/CMakePresets/ListAllPresetsNoTest-stdout.txt create mode 100644 Tests/RunCMake/CMakePresets/ListAllPresetsNoTest.json.in diff --git a/Source/cmCMakePresetsGraph.cxx b/Source/cmCMakePresetsGraph.cxx index dba79d5..c64c633 100644 --- a/Source/cmCMakePresetsGraph.cxx +++ b/Source/cmCMakePresetsGraph.cxx @@ -1067,6 +1067,16 @@ void cmCMakePresetsGraph::ClearPresets() this->Files.clear(); } +void cmCMakePresetsGraph::printPrecedingNewline(PrintPrecedingNewline* newline) +{ + if (newline) { + if (*newline == PrintPrecedingNewline::True) { + std::cout << std::endl; + } + *newline = PrintPrecedingNewline::True; + } +} + void cmCMakePresetsGraph::PrintPresets( const std::vector& presets) { @@ -1095,13 +1105,16 @@ void cmCMakePresetsGraph::PrintPresets( } } -void cmCMakePresetsGraph::PrintConfigurePresetList() const +void cmCMakePresetsGraph::PrintConfigurePresetList( + PrintPrecedingNewline* newline) const { - PrintConfigurePresetList([](const ConfigurePreset&) { return true; }); + PrintConfigurePresetList([](const ConfigurePreset&) { return true; }, + newline); } void cmCMakePresetsGraph::PrintConfigurePresetList( - const std::function& filter) const + const std::function& filter, + PrintPrecedingNewline* newline) const { std::vector presets; for (auto const& p : this->ConfigurePresetOrder) { @@ -1114,12 +1127,14 @@ void cmCMakePresetsGraph::PrintConfigurePresetList( } if (!presets.empty()) { + printPrecedingNewline(newline); std::cout << "Available configure presets:\n\n"; cmCMakePresetsGraph::PrintPresets(presets); } } -void cmCMakePresetsGraph::PrintBuildPresetList() const +void cmCMakePresetsGraph::PrintBuildPresetList( + PrintPrecedingNewline* newline) const { std::vector presets; for (auto const& p : this->BuildPresetOrder) { @@ -1132,12 +1147,14 @@ void cmCMakePresetsGraph::PrintBuildPresetList() const } if (!presets.empty()) { + printPrecedingNewline(newline); std::cout << "Available build presets:\n\n"; cmCMakePresetsGraph::PrintPresets(presets); } } -void cmCMakePresetsGraph::PrintTestPresetList() const +void cmCMakePresetsGraph::PrintTestPresetList( + PrintPrecedingNewline* newline) const { std::vector presets; for (auto const& p : this->TestPresetOrder) { @@ -1150,6 +1167,7 @@ void cmCMakePresetsGraph::PrintTestPresetList() const } if (!presets.empty()) { + printPrecedingNewline(newline); std::cout << "Available test presets:\n\n"; cmCMakePresetsGraph::PrintPresets(presets); } @@ -1157,9 +1175,8 @@ void cmCMakePresetsGraph::PrintTestPresetList() const void cmCMakePresetsGraph::PrintAllPresets() const { - this->PrintConfigurePresetList(); - std::cout << std::endl; - this->PrintBuildPresetList(); - std::cout << std::endl; - this->PrintTestPresetList(); + PrintPrecedingNewline newline = PrintPrecedingNewline::False; + this->PrintConfigurePresetList(&newline); + this->PrintBuildPresetList(&newline); + this->PrintTestPresetList(&newline); } diff --git a/Source/cmCMakePresetsGraph.h b/Source/cmCMakePresetsGraph.h index 4f3e108..efedc5b4 100644 --- a/Source/cmCMakePresetsGraph.h +++ b/Source/cmCMakePresetsGraph.h @@ -383,13 +383,22 @@ public: return ""; } + enum class PrintPrecedingNewline + { + False, + True, + }; + static void printPrecedingNewline(PrintPrecedingNewline* p); + static void PrintPresets( const std::vector& presets); - void PrintConfigurePresetList() const; void PrintConfigurePresetList( - const std::function& filter) const; - void PrintBuildPresetList() const; - void PrintTestPresetList() const; + PrintPrecedingNewline* newline = nullptr) const; + void PrintConfigurePresetList( + const std::function& filter, + PrintPrecedingNewline* newline = nullptr) const; + void PrintBuildPresetList(PrintPrecedingNewline* newline = nullptr) const; + void PrintTestPresetList(PrintPrecedingNewline* newline = nullptr) const; void PrintAllPresets() const; private: diff --git a/Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild-stdout.txt b/Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild-stdout.txt new file mode 100644 index 0000000..38f52aa --- /dev/null +++ b/Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild-stdout.txt @@ -0,0 +1,8 @@ +^Not searching for unused variables given on the command line. +Available configure presets: + + "default" + +Available test presets: + + "default"$ diff --git a/Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild.json.in b/Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild.json.in new file mode 100644 index 0000000..9259477 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/ListAllPresetsNoBuild.json.in @@ -0,0 +1,14 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "default" + } + ], + "testPresets": [ + { + "name": "default", + "configurePreset": "default" + } + ] +} diff --git a/Tests/RunCMake/CMakePresets/ListAllPresetsNoTest-stdout.txt b/Tests/RunCMake/CMakePresets/ListAllPresetsNoTest-stdout.txt new file mode 100644 index 0000000..8cac0a8 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/ListAllPresetsNoTest-stdout.txt @@ -0,0 +1,8 @@ +^Not searching for unused variables given on the command line. +Available configure presets: + + "default" + +Available build presets: + + "default"$ diff --git a/Tests/RunCMake/CMakePresets/ListAllPresetsNoTest.json.in b/Tests/RunCMake/CMakePresets/ListAllPresetsNoTest.json.in new file mode 100644 index 0000000..33fd036 --- /dev/null +++ b/Tests/RunCMake/CMakePresets/ListAllPresetsNoTest.json.in @@ -0,0 +1,14 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "default" + } + ], + "buildPresets": [ + { + "name": "default", + "configurePreset": "default" + } + ] +} diff --git a/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake b/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake index d097086..c511b34 100644 --- a/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake +++ b/Tests/RunCMake/CMakePresets/RunCMakeTest.cmake @@ -304,6 +304,12 @@ unset(RunCMake_TEST_BINARY_DIR) run_cmake_presets(ListPresetsNoSuchPreset) run_cmake_presets(ListPresetsHidden) +set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/ListAllPresetsNoBuild.json.in") +run_cmake_presets(ListAllPresetsNoBuild --list-presets=all) + +set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/ListAllPresetsNoTest.json.in") +run_cmake_presets(ListAllPresetsNoTest --list-presets=all) + # Test warning and error flags set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/Warnings.json.in") set(CMakePresets_WARN_UNUSED_CLI 1) -- cgit v0.12 From b699610df4878a3a68cb91a0a698f77e120282d1 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Wed, 2 Feb 2022 10:50:21 -0500 Subject: cmCMakePresetsGraph: Add package presets --- Source/CMakeLists.txt | 1 + Source/cmCMakePresetsGraph.cxx | 117 +++++++++++++++++++++ Source/cmCMakePresetsGraph.h | 43 ++++++++ Source/cmCMakePresetsGraphInternal.h | 4 + Source/cmCMakePresetsGraphReadJSON.cxx | 29 ++++- .../cmCMakePresetsGraphReadJSONPackagePresets.cxx | 95 +++++++++++++++++ 6 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 Source/cmCMakePresetsGraphReadJSONPackagePresets.cxx diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index b6f7c85..86f8116 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -168,6 +168,7 @@ set(SRCS cmCMakePresetsGraphReadJSON.cxx cmCMakePresetsGraphReadJSONBuildPresets.cxx cmCMakePresetsGraphReadJSONConfigurePresets.cxx + cmCMakePresetsGraphReadJSONPackagePresets.cxx cmCMakePresetsGraphReadJSONTestPresets.cxx cmCommandArgumentParserHelper.cxx cmCommonTargetGenerator.cxx diff --git a/Source/cmCMakePresetsGraph.cxx b/Source/cmCMakePresetsGraph.cxx index c64c633..c168a45 100644 --- a/Source/cmCMakePresetsGraph.cxx +++ b/Source/cmCMakePresetsGraph.cxx @@ -43,6 +43,7 @@ using ReadFileResult = cmCMakePresetsGraph::ReadFileResult; using ConfigurePreset = cmCMakePresetsGraph::ConfigurePreset; using BuildPreset = cmCMakePresetsGraph::BuildPreset; using TestPreset = cmCMakePresetsGraph::TestPreset; +using PackagePreset = cmCMakePresetsGraph::PackagePreset; using ExpandMacroResult = cmCMakePresetsGraphInternal::ExpandMacroResult; using MacroExpander = cmCMakePresetsGraphInternal::MacroExpander; @@ -301,6 +302,28 @@ bool ExpandMacros(const cmCMakePresetsGraph& graph, const TestPreset& preset, return true; } +bool ExpandMacros(const cmCMakePresetsGraph& graph, + const PackagePreset& preset, + cm::optional& out, + const std::vector& macroExpanders) +{ + for (auto& variable : out->Variables) { + CHECK_EXPAND(out, variable.second, macroExpanders, + graph.GetVersion(preset)); + } + + CHECK_EXPAND(out, out->ConfigFile, macroExpanders, graph.GetVersion(preset)); + CHECK_EXPAND(out, out->PackageName, macroExpanders, + graph.GetVersion(preset)); + CHECK_EXPAND(out, out->PackageVersion, macroExpanders, + graph.GetVersion(preset)); + CHECK_EXPAND(out, out->PackageDirectory, macroExpanders, + graph.GetVersion(preset)); + CHECK_EXPAND(out, out->VendorName, macroExpanders, graph.GetVersion(preset)); + + return true; +} + template bool ExpandMacros(const cmCMakePresetsGraph& graph, const T& preset, cm::optional& out) @@ -868,6 +891,44 @@ cmCMakePresetsGraph::TestPreset::VisitPresetAfterInherit(int /* version */) return ReadFileResult::READ_OK; } +cmCMakePresetsGraph::ReadFileResult +cmCMakePresetsGraph::PackagePreset::VisitPresetInherit( + const cmCMakePresetsGraph::Preset& parentPreset) +{ + auto& preset = *this; + const PackagePreset& parent = + static_cast(parentPreset); + + InheritString(preset.ConfigurePreset, parent.ConfigurePreset); + InheritOptionalValue(preset.InheritConfigureEnvironment, + parent.InheritConfigureEnvironment); + InheritVector(preset.Generators, parent.Generators); + InheritVector(preset.Configurations, parent.Configurations); + + for (auto const& v : parent.Variables) { + preset.Variables.insert(v); + } + + InheritOptionalValue(preset.DebugOutput, parent.DebugOutput); + InheritOptionalValue(preset.VerboseOutput, parent.VerboseOutput); + InheritString(preset.PackageName, parent.PackageName); + InheritString(preset.PackageVersion, parent.PackageVersion); + InheritString(preset.PackageDirectory, parent.PackageDirectory); + InheritString(preset.VendorName, parent.VendorName); + + return ReadFileResult::READ_OK; +} + +cmCMakePresetsGraph::ReadFileResult +cmCMakePresetsGraph::PackagePreset::VisitPresetAfterInherit(int /* version */) +{ + auto& preset = *this; + if (!preset.Hidden && preset.ConfigurePreset.empty()) { + return ReadFileResult::INVALID_PRESET; + } + return ReadFileResult::READ_OK; +} + std::string cmCMakePresetsGraph::GetFilename(const std::string& sourceDir) { return cmStrCat(sourceDir, "/CMakePresets.json"); @@ -930,6 +991,7 @@ cmCMakePresetsGraph::ReadProjectPresetsInternal(bool allowNoFiles) CHECK_OK(ComputePresetInheritance(this->ConfigurePresets, *this)); CHECK_OK(ComputePresetInheritance(this->BuildPresets, *this)); CHECK_OK(ComputePresetInheritance(this->TestPresets, *this)); + CHECK_OK(ComputePresetInheritance(this->PackagePresets, *this)); for (auto& it : this->ConfigurePresets) { if (!ExpandMacros(*this, it.second.Unexpanded, it.second.Expanded)) { @@ -985,6 +1047,30 @@ cmCMakePresetsGraph::ReadProjectPresetsInternal(bool allowNoFiles) } } + for (auto& it : this->PackagePresets) { + if (!it.second.Unexpanded.Hidden) { + const auto configurePreset = + this->ConfigurePresets.find(it.second.Unexpanded.ConfigurePreset); + if (configurePreset == this->ConfigurePresets.end()) { + return ReadFileResult::INVALID_CONFIGURE_PRESET; + } + if (!it.second.Unexpanded.OriginFile->ReachableFiles.count( + configurePreset->second.Unexpanded.OriginFile)) { + return ReadFileResult::CONFIGURE_PRESET_UNREACHABLE_FROM_FILE; + } + + if (it.second.Unexpanded.InheritConfigureEnvironment.value_or(true)) { + it.second.Unexpanded.Environment.insert( + configurePreset->second.Unexpanded.Environment.begin(), + configurePreset->second.Unexpanded.Environment.end()); + } + } + + if (!ExpandMacros(*this, it.second.Unexpanded, it.second.Expanded)) { + return ReadFileResult::INVALID_MACRO_EXPANSION; + } + } + return ReadFileResult::READ_OK; } @@ -1028,6 +1114,8 @@ const char* cmCMakePresetsGraph::ResultToString(ReadFileResult result) case ReadFileResult::BUILD_TEST_PRESETS_UNSUPPORTED: return "File version must be 2 or higher for build and test preset " "support."; + case ReadFileResult::PACKAGE_PRESETS_UNSUPPORTED: + return "File version must be 5 or higher for package preset support"; case ReadFileResult::INCLUDE_UNSUPPORTED: return "File version must be 4 or higher for include support"; case ReadFileResult::INVALID_INCLUDE: @@ -1173,10 +1261,39 @@ void cmCMakePresetsGraph::PrintTestPresetList( } } +void cmCMakePresetsGraph::PrintPackagePresetList( + PrintPrecedingNewline* newline) const +{ + this->PrintPackagePresetList([](const PackagePreset&) { return true; }, + newline); +} + +void cmCMakePresetsGraph::PrintPackagePresetList( + const std::function& filter, + PrintPrecedingNewline* newline) const +{ + std::vector presets; + for (auto const& p : this->PackagePresetOrder) { + auto const& preset = this->PackagePresets.at(p); + if (!preset.Unexpanded.Hidden && preset.Expanded && + preset.Expanded->ConditionResult && filter(preset.Unexpanded)) { + presets.push_back( + static_cast(&preset.Unexpanded)); + } + } + + if (!presets.empty()) { + printPrecedingNewline(newline); + std::cout << "Available package presets:\n\n"; + cmCMakePresetsGraph::PrintPresets(presets); + } +} + void cmCMakePresetsGraph::PrintAllPresets() const { PrintPrecedingNewline newline = PrintPrecedingNewline::False; this->PrintConfigurePresetList(&newline); this->PrintBuildPresetList(&newline); this->PrintTestPresetList(&newline); + this->PrintPackagePresetList(&newline); } diff --git a/Source/cmCMakePresetsGraph.h b/Source/cmCMakePresetsGraph.h index efedc5b4..806a36d 100644 --- a/Source/cmCMakePresetsGraph.h +++ b/Source/cmCMakePresetsGraph.h @@ -41,6 +41,7 @@ public: CONFIGURE_PRESET_UNREACHABLE_FROM_FILE, INVALID_MACRO_EXPANSION, BUILD_TEST_PRESETS_UNSUPPORTED, + PACKAGE_PRESETS_UNSUPPORTED, INCLUDE_UNSUPPORTED, INVALID_INCLUDE, INVALID_CONFIGURE_PRESET, @@ -326,6 +327,42 @@ public: ReadFileResult VisitPresetAfterInherit(int /* version */) override; }; + class PackagePreset : public Preset + { + public: + PackagePreset() = default; + PackagePreset(PackagePreset&& /*other*/) = default; + PackagePreset(const PackagePreset& /*other*/) = default; + PackagePreset& operator=(const PackagePreset& /*other*/) = default; + ~PackagePreset() override = default; +#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) + PackagePreset& operator=(PackagePreset&& /*other*/) = default; +#else + // The move assignment operators for several STL classes did not become + // noexcept until C++17, which causes some tools to warn about this move + // assignment operator throwing an exception when it shouldn't. + PackagePreset& operator=(PackagePreset&& /*other*/) = delete; +#endif + + std::string ConfigurePreset; + cm::optional InheritConfigureEnvironment; + std::vector Generators; + std::vector Configurations; + std::map Variables; + std::string ConfigFile; + + cm::optional DebugOutput; + cm::optional VerboseOutput; + + std::string PackageName; + std::string PackageVersion; + std::string PackageDirectory; + std::string VendorName; + + ReadFileResult VisitPresetInherit(const Preset& parent) override; + ReadFileResult VisitPresetAfterInherit(int /* version */) override; + }; + template class PresetPair { @@ -337,10 +374,12 @@ public: std::map> ConfigurePresets; std::map> BuildPresets; std::map> TestPresets; + std::map> PackagePresets; std::vector ConfigurePresetOrder; std::vector BuildPresetOrder; std::vector TestPresetOrder; + std::vector PackagePresetOrder; std::string SourceDir; std::vector> Files; @@ -399,6 +438,10 @@ public: PrintPrecedingNewline* newline = nullptr) const; void PrintBuildPresetList(PrintPrecedingNewline* newline = nullptr) const; void PrintTestPresetList(PrintPrecedingNewline* newline = nullptr) const; + void PrintPackagePresetList(PrintPrecedingNewline* newline = nullptr) const; + void PrintPackagePresetList( + const std::function& filter, + PrintPrecedingNewline* newline = nullptr) const; void PrintAllPresets() const; private: diff --git a/Source/cmCMakePresetsGraphInternal.h b/Source/cmCMakePresetsGraphInternal.h index f7c7349..40af356 100644 --- a/Source/cmCMakePresetsGraphInternal.h +++ b/Source/cmCMakePresetsGraphInternal.h @@ -147,6 +147,10 @@ cmCMakePresetsGraph::ReadFileResult BuildPresetsHelper( cmCMakePresetsGraph::ReadFileResult TestPresetsHelper( std::vector& out, const Json::Value* value); +cmCMakePresetsGraph::ReadFileResult PackagePresetsHelper( + std::vector& out, + const Json::Value* value); + cmJSONHelper VendorHelper( cmCMakePresetsGraph::ReadFileResult error); diff --git a/Source/cmCMakePresetsGraphReadJSON.cxx b/Source/cmCMakePresetsGraphReadJSON.cxx index d68af22..4fba7d9 100644 --- a/Source/cmCMakePresetsGraphReadJSON.cxx +++ b/Source/cmCMakePresetsGraphReadJSON.cxx @@ -34,7 +34,7 @@ using ArchToolsetStrategy = cmCMakePresetsGraph::ArchToolsetStrategy; using JSONHelperBuilder = cmJSONHelperBuilder; constexpr int MIN_VERSION = 1; -constexpr int MAX_VERSION = 5; +constexpr int MAX_VERSION = 6; struct CMakeVersion { @@ -49,6 +49,7 @@ struct RootPresets std::vector ConfigurePresets; std::vector BuildPresets; std::vector TestPresets; + std::vector PackagePresets; std::vector Include; }; @@ -281,6 +282,8 @@ auto const RootPresetsHelper = cmCMakePresetsGraphInternal::BuildPresetsHelper, false) .Bind("testPresets"_s, &RootPresets::TestPresets, cmCMakePresetsGraphInternal::TestPresetsHelper, false) + .Bind("packagePresets"_s, &RootPresets::PackagePresets, + cmCMakePresetsGraphInternal::PackagePresetsHelper, false) .Bind("cmakeMinimumRequired"_s, &RootPresets::CMakeMinimumRequired, CMakeVersionHelper, false) .Bind("include"_s, &RootPresets::Include, IncludeVectorHelper, false) @@ -458,6 +461,11 @@ cmCMakePresetsGraph::ReadFileResult cmCMakePresetsGraph::ReadJSONFile( return ReadFileResult::BUILD_TEST_PRESETS_UNSUPPORTED; } + // Support for package presets added in version 6. + if (v < 6 && root.isMember("packagePresets")) { + return ReadFileResult::PACKAGE_PRESETS_UNSUPPORTED; + } + // Support for include added in version 4. if (v < 4 && root.isMember("include")) { return ReadFileResult::INCLUDE_UNSUPPORTED; @@ -573,6 +581,25 @@ cmCMakePresetsGraph::ReadFileResult cmCMakePresetsGraph::ReadJSONFile( this->TestPresetOrder.push_back(preset.Name); } + for (auto& preset : presets.PackagePresets) { + preset.OriginFile = file; + if (preset.Name.empty()) { + return ReadFileResult::INVALID_PRESET; + } + + PresetPair presetPair; + presetPair.Unexpanded = preset; + presetPair.Expanded = cm::nullopt; + if (!this->PackagePresets.emplace(preset.Name, presetPair).second) { + return ReadFileResult::DUPLICATE_PRESETS; + } + + // Support for conditions added in version 3, but this requires version 5 + // already, so no action needed. + + this->PackagePresetOrder.push_back(preset.Name); + } + auto const includeFile = [this, &inProgressFiles, file]( const std::string& include, RootType rootType2, ReadReason readReason2, diff --git a/Source/cmCMakePresetsGraphReadJSONPackagePresets.cxx b/Source/cmCMakePresetsGraphReadJSONPackagePresets.cxx new file mode 100644 index 0000000..4ae51b1 --- /dev/null +++ b/Source/cmCMakePresetsGraphReadJSONPackagePresets.cxx @@ -0,0 +1,95 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "cmCMakePresetsGraph.h" +#include "cmCMakePresetsGraphInternal.h" +#include "cmJSONHelpers.h" + +namespace { +using ReadFileResult = cmCMakePresetsGraph::ReadFileResult; +using PackagePreset = cmCMakePresetsGraph::PackagePreset; + +auto const OutputHelper = + cmJSONHelperBuilder::Object( + ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false) + .Bind("debug"_s, &PackagePreset::DebugOutput, + cmCMakePresetsGraphInternal::PresetOptionalBoolHelper, false) + .Bind("verbose"_s, &PackagePreset::VerboseOutput, + cmCMakePresetsGraphInternal::PresetOptionalBoolHelper, false); + +auto const VariableHelper = cmJSONHelperBuilder::String( + ReadFileResult::READ_OK, ReadFileResult::INVALID_VARIABLE); + +auto const VariablesHelper = + cmJSONHelperBuilder::Map( + ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, VariableHelper); + +auto const PackagePresetHelper = + cmJSONHelperBuilder::Object( + ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESET, false) + .Bind("name"_s, &PackagePreset::Name, + cmCMakePresetsGraphInternal::PresetStringHelper) + .Bind("inherits"_s, &PackagePreset::Inherits, + cmCMakePresetsGraphInternal::PresetVectorOneOrMoreStringHelper, + false) + .Bind("hidden"_s, &PackagePreset::Hidden, + cmCMakePresetsGraphInternal::PresetBoolHelper, false) + .Bind("vendor"_s, nullptr, + cmCMakePresetsGraphInternal::VendorHelper( + ReadFileResult::INVALID_PRESET), + false) + .Bind("displayName"_s, &PackagePreset::DisplayName, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("description"_s, &PackagePreset::Description, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("environment"_s, &PackagePreset::Environment, + cmCMakePresetsGraphInternal::EnvironmentMapHelper, false) + .Bind("configurePreset"_s, &PackagePreset::ConfigurePreset, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("inheritConfigureEnvironment"_s, + &PackagePreset::InheritConfigureEnvironment, + cmCMakePresetsGraphInternal::PresetOptionalBoolHelper, false) + .Bind("generators"_s, &PackagePreset::Generators, + cmCMakePresetsGraphInternal::PresetVectorStringHelper, false) + .Bind("configurations"_s, &PackagePreset::Configurations, + cmCMakePresetsGraphInternal::PresetVectorStringHelper, false) + .Bind("variables"_s, &PackagePreset::Variables, VariablesHelper, false) + .Bind("configFile"_s, &PackagePreset::ConfigFile, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("output"_s, OutputHelper, false) + .Bind("packageName"_s, &PackagePreset::PackageName, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("packageVersion"_s, &PackagePreset::PackageVersion, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("packageDirectory"_s, &PackagePreset::PackageDirectory, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("vendorName"_s, &PackagePreset::VendorName, + cmCMakePresetsGraphInternal::PresetStringHelper, false) + .Bind("condition"_s, &PackagePreset::ConditionEvaluator, + cmCMakePresetsGraphInternal::PresetConditionHelper, false); +} + +namespace cmCMakePresetsGraphInternal { +cmCMakePresetsGraph::ReadFileResult PackagePresetsHelper( + std::vector& out, + const Json::Value* value) +{ + static auto const helper = + cmJSONHelperBuilder::Vector( + ReadFileResult::READ_OK, ReadFileResult::INVALID_PRESETS, + PackagePresetHelper); + + return helper(out, value); +} +} -- cgit v0.12 From a8d73085f4779390fe52a4115a7ba5d0edc5631b Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Thu, 3 Feb 2022 17:36:51 -0500 Subject: CPack: Add support for presets Fixes: #23117 --- Help/manual/cmake-presets.7.rst | 140 ++++++++++++++- Help/manual/cmake.1.rst | 6 +- Help/manual/cpack.1.rst | 8 + Help/manual/presets/schema.json | 200 ++++++++++++++++++++- Help/release/dev/cmake-presets-package.rst | 5 + Source/CPack/cpack.cxx | 169 ++++++++++++++++- Source/cmake.cxx | 5 + Tests/RunCMake/CMakeLists.txt | 4 + .../RunCMake/CMakePresetsPackage/CMakeLists.txt.in | 3 + .../Good-package-config-file-check.cmake | 6 + .../Good-package-configurations-check.cmake | 18 ++ .../Good-package-debug-stdout.txt | 2 + .../Good-package-generators-check.cmake | 1 + .../Good-package-no-environment-check.cmake | 7 + .../Good-package-package-directory-check.cmake | 6 + .../Good-package-package-name-check.cmake | 7 + .../Good-package-package-version-check.cmake | 7 + .../Good-package-variables-check.cmake | 6 + .../Good-package-verbose-stdout.txt | 1 + .../Good-package-with-environment-check.cmake | 7 + Tests/RunCMake/CMakePresetsPackage/Good.cmake | 31 ++++ Tests/RunCMake/CMakePresetsPackage/Good.json.in | 135 ++++++++++++++ .../CMakePresetsPackage/RunCMakeTest.cmake | 101 +++++++++++ .../UnsupportedVersion-configure-x-result.txt | 1 + .../UnsupportedVersion-configure-x-stderr.txt | 2 + .../CMakePresetsPackage/UnsupportedVersion.json.in | 4 + Tests/RunCMake/CMakePresetsPackage/check.cmake | 3 + 27 files changed, 872 insertions(+), 13 deletions(-) create mode 100644 Help/release/dev/cmake-presets-package.rst create mode 100644 Tests/RunCMake/CMakePresetsPackage/CMakeLists.txt.in create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-config-file-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-configurations-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-debug-stdout.txt create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-generators-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-no-environment-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-package-directory-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-package-name-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-package-version-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-variables-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-verbose-stdout.txt create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good-package-with-environment-check.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/Good.json.in create mode 100644 Tests/RunCMake/CMakePresetsPackage/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-result.txt create mode 100644 Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-stderr.txt create mode 100644 Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion.json.in create mode 100644 Tests/RunCMake/CMakePresetsPackage/check.cmake diff --git a/Help/manual/cmake-presets.7.rst b/Help/manual/cmake-presets.7.rst index 16cd54e..35dac76 100644 --- a/Help/manual/cmake-presets.7.rst +++ b/Help/manual/cmake-presets.7.rst @@ -58,6 +58,9 @@ The root object recognizes the following fields: ``5`` .. versionadded:: 3.24 + ``6`` + .. versionadded:: 3.25 + ``cmakeMinimumRequired`` An optional object representing the minimum version of CMake needed to build this project. This object consists of the following fields: @@ -128,7 +131,8 @@ that may contain the following fields: This identifier is used in the :ref:`cmake --preset ` option. There must not be two configure presets in the union of ``CMakePresets.json`` and ``CMakeUserPresets.json`` in the same directory with the same name. - However, a configure preset may have the same name as a build or test preset. + However, a configure preset may have the same name as a build, test, or + package preset. ``hidden`` An optional boolean specifying whether or not a preset should be hidden. @@ -350,7 +354,8 @@ that may contain the following fields: :ref:`cmake --build --preset ` option. There must not be two build presets in the union of ``CMakePresets.json`` and ``CMakeUserPresets.json`` in the same directory with the same name. - However, a build preset may have the same name as a configure or test preset. + However, a build preset may have the same name as a configure, test, or + package preset. ``hidden`` An optional boolean specifying whether or not a preset should be hidden. @@ -510,7 +515,8 @@ that may contain the following fields: This identifier is used in the :option:`ctest --preset` option. There must not be two test presets in the union of ``CMakePresets.json`` and ``CMakeUserPresets.json`` in the same directory with the same name. - However, a test preset may have the same name as a configure or build preset. + However, a test preset may have the same name as a configure, build, or + package preset. ``hidden`` An optional boolean specifying whether or not a preset should be hidden. @@ -833,6 +839,134 @@ that may contain the following fields: Equivalent to passing :option:`--no-tests=ignore ` on the command line. +Package Preset +^^^^^^^^^^^^^^ + +Package presets may be used in schema version ``6`` or above. Each entry of +the ``packagePresets`` array is a JSON object that may contain the following +fields: + +``name`` + A required string representing the machine-friendly name of the preset. + This identifier is used in the :option:`cpack --preset` option. + There must not be two package presets in the union of ``CMakePresets.json`` + and ``CMakeUserPresets.json`` in the same directory with the same name. + However, a package preset may have the same name as a configure, build, or + test preset. + +``hidden`` + An optional boolean specifying whether or not a preset should be hidden. + If a preset is hidden, it cannot be used in the + :option:`--preset ` argument + and does not have to have a valid ``configurePreset``, even from + inheritance. ``hidden`` presets are intended to be used as a base for + other presets to inherit via the ``inherits`` field. + +``inherits`` + An optional array of strings representing the names of presets to inherit + from. This field can also be a string, which is equivalent to an array + containing one string. + + The preset will inherit all of the fields from the + ``inherits`` presets by default (except ``name``, ``hidden``, + ``inherits``, ``description``, and ``displayName``), but can override + them as desired. If multiple ``inherits`` presets provide conflicting + values for the same field, the earlier preset in the ``inherits`` list + will be preferred. + + A preset can only inherit from another preset that is defined in the + same file or in one of the files it includes (directly or indirectly). + Presets in ``CMakePresets.json`` may not inherit from presets in + ``CMakeUserPresets.json``. + +``condition`` + An optional `Condition`_ object. + +``vendor`` + An optional map containing vendor-specific information. CMake does not + interpret the contents of this field except to verify that it is a map + if it does exist. However, it should follow the same conventions as the + root-level ``vendor`` field. If vendors use their own per-preset + ``vendor`` field, they should implement inheritance in a sensible manner + when appropriate. + +``displayName`` + An optional string with a human-friendly name of the preset. + +``description`` + An optional string with a human-friendly description of the preset. + +``environment`` + An optional map of environment variables. The key is the variable name + (which may not be an empty string), and the value is either ``null`` or + a string representing the value of the variable. Each variable is set + regardless of whether or not a value was given to it by the process's + environment. This field supports macro expansion, and environment + variables in this map may reference each other, and may be listed in any + order, as long as such references do not cause a cycle (for example, if + ``ENV_1`` is ``$env{ENV_2}``, ``ENV_2`` may not be ``$env{ENV_1}``.) + + Environment variables are inherited through the ``inherits`` field, and + the preset's environment will be the union of its own ``environment`` + and the ``environment`` from all its parents. If multiple presets in + this union define the same variable, the standard rules of ``inherits`` + are applied. Setting a variable to ``null`` causes it to not be set, + even if a value was inherited from another preset. + +``configurePreset`` + An optional string specifying the name of a configure preset to + associate with this package preset. If ``configurePreset`` is not + specified, it must be inherited from the inherits preset (unless this + preset is hidden). The build directory is inferred from the configure + preset, so packaging will run in the same ``binaryDir`` that the + configuration did and build did. + +``inheritConfigureEnvironment`` + An optional boolean that defaults to true. If true, the environment + variables from the associated configure preset are inherited after all + inherited package preset environments, but before environment variables + explicitly specified in this package preset. + +``generators`` + An optional list of strings representing generators for CPack to use. + +``configurations`` + An optional list of strings representing build configurations for CPack to + package. + +``variables`` + An optional map of variables to pass to CPack, equivalent to + :option:`-D ` arguments. Each key is the name of a variable, and + the value is the string to assign to that variable. + +``configFile`` + An optional string representing the config file for CPack to use. + +``output`` + An optional object specifying output options. Valid keys are: + + ``debug`` + An optional boolean specifying whether or not to print debug information. + A value of ``true`` is equivalent to passing + :option:`--debug ` on the command line. + + ``verbose`` + An optional boolean specifying whether or not to print verbosely. A value + of ``true`` is equivalent to passing :option:`--verbose ` + on the command line. + +``packageName`` + An optional string representing the package name. + +``packageVersion`` + An optional string representing the package version. + +``packageDirectory`` + An optional string representing the directory in which to place the package. + +``vendorName`` + An optional string representing the vendor name. + Condition ^^^^^^^^^ diff --git a/Help/manual/cmake.1.rst b/Help/manual/cmake.1.rst index 8cec9d4..a3e9fd0 100644 --- a/Help/manual/cmake.1.rst +++ b/Help/manual/cmake.1.rst @@ -500,9 +500,9 @@ Options .. option:: --list-presets[=] Lists the available presets of the specified ````. Valid values for - ```` are ``configure``, ``build``, ``test``, or ``all``. If ```` - is omitted, ``configure`` is assumed. The current working directory must - contain CMake preset files. + ```` are ``configure``, ``build``, ``test``, ``package``, or ``all``. + If ```` is omitted, ``configure`` is assumed. The current working + directory must contain CMake preset files. .. _`Build Tool Mode`: diff --git a/Help/manual/cpack.1.rst b/Help/manual/cpack.1.rst index a95df8b..3f26d72 100644 --- a/Help/manual/cpack.1.rst +++ b/Help/manual/cpack.1.rst @@ -119,6 +119,14 @@ Options Override/define :variable:`CPACK_PACKAGE_VENDOR`. +.. option:: --preset + + Use a preset from :manual:`cmake-presets(7)`. + +.. option:: --list-presets + + List presets from :manual:`cmake-presets(7)`. + .. include:: OPTIONS_HELP.txt See Also diff --git a/Help/manual/presets/schema.json b/Help/manual/presets/schema.json index c96405c..f3a7532 100644 --- a/Help/manual/presets/schema.json +++ b/Help/manual/presets/schema.json @@ -72,6 +72,22 @@ "include": { "$ref": "#/definitions/include"} }, "additionalProperties": false + }, + { + "properties": { + "version": { + "const": 6, + "description": "A required integer representing the version of the JSON schema." + }, + "cmakeMinimumRequired": { "$ref": "#/definitions/cmakeMinimumRequired"}, + "vendor": { "$ref": "#/definitions/vendor" }, + "configurePresets": { "$ref": "#/definitions/configurePresetsV3"}, + "buildPresets": { "$ref": "#/definitions/buildPresetsV4"}, + "testPresets": { "$ref": "#/definitions/testPresetsV5"}, + "packagePresets": { "$ref": "#/definitions/packagePresetsV6"}, + "include": { "$ref": "#/definitions/include"} + }, + "additionalProperties": false } ], "required": [ @@ -476,12 +492,12 @@ "properties": { "name": { "type": "string", - "description": "A required string representing the machine-friendly name of the preset. This identifier is used in the --preset argument. There must not be two presets (configure, build, or test) in the union of CMakePresets.json and CMakeUserPresets.json in the same directory with the same name.", + "description": "A required string representing the machine-friendly name of the preset. This identifier is used in the --preset argument. There must not be two presets (configure, build, test, or package) in the union of CMakePresets.json and CMakeUserPresets.json in the same directory with the same name.", "minLength": 1 }, "hidden": { "type": "boolean", - "description": "An optional boolean specifying whether or not a preset should be hidden. If a preset is hidden, it cannot be used in the --preset argument, will not show up in the CMake GUI, and does not have to have a valid configurePreset, even from inheritance. Hidden presets are intended to be used as a base for other presets to inherit via the inherits field." + "description": "An optional boolean specifying whether or not a preset should be hidden. If a preset is hidden, it cannot be used in the --preset argument and does not have to have a valid configurePreset, even from inheritance. Hidden presets are intended to be used as a base for other presets to inherit via the inherits field." }, "inherits": { "anyOf": [ @@ -728,12 +744,12 @@ "properties": { "name": { "type": "string", - "description": "A required string representing the machine-friendly name of the preset. This identifier is used in the --preset argument. There must not be two presets (configure, build, or test) in the union of CMakePresets.json and CMakeUserPresets.json in the same directory with the same name.", + "description": "A required string representing the machine-friendly name of the preset. This identifier is used in the --preset argument. There must not be two presets (configure, build, test, or package) in the union of CMakePresets.json and CMakeUserPresets.json in the same directory with the same name.", "minLength": 1 }, "hidden": { "type": "boolean", - "description": "An optional boolean specifying whether or not a preset should be hidden. If a preset is hidden, it cannot be used in the --preset argument, will not show up in the CMake GUI, and does not have to have a valid configurePreset, even from inheritance. Hidden presets are intended to be used as a base for other presets to inherit via the inherits field." + "description": "An optional boolean specifying whether or not a preset should be hidden. If a preset is hidden, it cannot be used in the --preset argument and does not have to have a valid configurePreset, even from inheritance. Hidden presets are intended to be used as a base for other presets to inherit via the inherits field." }, "inherits": { "anyOf": [ @@ -1129,6 +1145,182 @@ "additionalProperties": false } }, + "packagePresetsItemsV6": { + "type": "array", + "description": "An optional array of package preset objects. Used to specify arguments to cpack. Available in version 6 and higher.", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "A required string representing the machine-friendly name of the preset. This identifier is used in the --preset argument. There must not be two presets (configure, build, test, or package) in the union of CMakePresets.json and CMakeUserPresets.json in the same directory with the same name.", + "minLength": 1 + }, + "hidden": { + "type": "boolean", + "description": "An optional boolean specifying whether or not a preset should be hidden. If a preset is hidden, it cannot be used in the --preset argument and does not have to have a valid configurePreset, even from inheritance. Hidden presets are intended to be used as a base for other presets to inherit via the inherits field." + }, + "inherits": { + "anyOf": [ + { + "type": "string", + "description": "An optional string representing the name of the package preset to inherit from.", + "minLength": 1 + }, + { + "type": "array", + "description": "An optional array of strings representing the names of package presets to inherit from. The preset will inherit all of the fields from the inherits presets by default (except name, hidden, inherits, description, and displayName), but can override them as desired. If multiple inherits presets provide conflicting values for the same field, the earlier preset in the inherits list will be preferred. Presets in CMakePresets.json must not inherit from presets in CMakeUserPresets.json.", + "items": { + "type": "string", + "description": "An optional string representing the name of the preset to inherit from.", + "minLength": 1 + } + } + ] + }, + "configurePreset": { + "type": "string", + "description": "An optional string specifying the name of a configure preset to associate with this package preset. If configurePreset is not specified, it must be inherited from the inherits preset (unless this preset is hidden). The build tree directory is inferred from the configure preset.", + "minLength": 1 + }, + "vendor": { + "type": "object", + "description": "An optional map containing vendor-specific information. CMake does not interpret the contents of this field except to verify that it is a map if it does exist. However, it should follow the same conventions as the root-level vendor field. If vendors use their own per-preset vendor field, they should implement inheritance in a sensible manner when appropriate.", + "properties": {} + }, + "displayName": { + "type": "string", + "description": "An optional string with a human-friendly name of the preset." + }, + "description": { + "type": "string", + "description": "An optional string with a human-friendly description of the preset." + }, + "inheritConfigureEnvironment": { + "type": "boolean", + "description": "An optional boolean that defaults to true. If true, the environment variables from the associated configure preset are inherited after all inherited package preset environments, but before environment variables explicitly specified in this package preset." + }, + "environment": { + "type": "object", + "description": "An optional map of environment variables. The key is the variable name (which must not be an empty string). Each variable is set regardless of whether or not a value was given to it by the process's environment. This field supports macro expansion, and environment variables in this map may reference each other, and may be listed in any order, as long as such references do not cause a cycle (for example,if ENV_1 is $env{ENV_2}, ENV_2 may not be $env{ENV_1}.) Environment variables are inherited through the inherits field, and the preset's environment will be the union of its own environment and the environment from all its parents. If multiple presets in this union define the same variable, the standard rules of inherits are applied. Setting a variable to null causes it to not be set, even if a value was inherited from another preset.", + "properties": {}, + "additionalProperties": { + "anyOf": [ + { + "type": "null", + "description": "Setting a variable to null causes it to not be set, even if a value was inherited from another preset." + }, + { + "type": "string", + "description": "A string representing the value of the variable." + } + ] + }, + "propertyNames": { + "pattern": "^.+$" + } + }, + "condition": { "$ref": "#/definitions/topCondition" }, + "generators": { + "type": "array", + "description": "An optional list of strings representing generators for CPack to use.", + "items": { + "type": "string", + "description": "An optional string representing the name of the CPack generator to use." + } + }, + "configurations": { + "type": "array", + "description": "An optional list of strings representing build configurations for CPack to package.", + "items": { + "type": "string", + "description": "An optional string representing the name of the configuration to use." + } + }, + "variables": { + "type": "object", + "description": "An optional map of variables to pass to CPack, equivalent to -D arguments. Each key is the name of a variable, and the value is the string to assign to that variable.", + "items": { + "type": "string", + "description": "An optional string representing the value of the variable." + } + }, + "configFile": { + "type": "string", + "description": "An optional string representing the config file for CPack to use." + }, + "output": { + "type": "object", + "description": "An optional object specifying output options.", + "properties": { + "debug": { + "type": "boolean", + "description": "An optional boolean specifying whether or not to print debug information. A value of true is equivalent to passing --debug on the command line." + }, + "verbose": { + "type": "boolean", + "description": "An optional boolean specifying whether or not to print verbosely. A value of true is equivalent to passing --verbose on the command line." + } + }, + "additionalProperties": false + }, + "packageName": { + "type": "string", + "description": "An optional string representing the package name." + }, + "packageVersion": { + "type": "string", + "description": "An optional string representing the package version." + }, + "packageDirectory": { + "type": "string", + "description": "An optional string representing the directory in which to place the package." + }, + "vendorName": { + "type": "string", + "description": "An optional string representing the vendor name." + } + }, + "required": [ + "name" + ] + } + }, + "packagePresetsV6": { + "type": "array", + "description": "An optional array of package preset objects. Used to specify arguments to cpack. Available in version 6 and higher.", + "allOf": [ + { "$ref": "#/definitions/packagePresetsItemsV6" } + ], + "items": { + "type": "object", + "properties": { + "name": {}, + "hidden": {}, + "inherits": {}, + "configurePreset": {}, + "vendor": {}, + "displayName": {}, + "description": {}, + "inheritConfigureEnvironment": {}, + "environment": {}, + "condition": {}, + "generators": {}, + "configurations": {}, + "variables": {}, + "configFile": {}, + "output": {}, + "packageName": {}, + "packageVersion": {}, + "packageDirectory": {}, + "vendorName": {} + }, + "required": [ + "name" + ], + "additionalProperties": false + } + }, "condition": { "anyOf": [ { diff --git a/Help/release/dev/cmake-presets-package.rst b/Help/release/dev/cmake-presets-package.rst new file mode 100644 index 0000000..8f7ea6b --- /dev/null +++ b/Help/release/dev/cmake-presets-package.rst @@ -0,0 +1,5 @@ +cmake-presets-package +--------------------- + +* The :manual:`cmake-presets(7)` schema version has been bumped to ``6``. +* The :manual:`cmake-presets(7)` format now supports a ``packagePresets`` field. diff --git a/Source/CPack/cpack.cxx b/Source/CPack/cpack.cxx index 4d9a42a..631bfd9 100644 --- a/Source/CPack/cpack.cxx +++ b/Source/CPack/cpack.cxx @@ -1,6 +1,7 @@ /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying file Copyright.txt or https://cmake.org/licensing for details. */ +#include #include #include #include @@ -11,10 +12,12 @@ #include #include +#include #include #include "cmsys/Encoding.hxx" +#include "cmCMakePresetsGraph.h" #include "cmCPackGenerator.h" #include "cmCPackGeneratorFactory.h" #include "cmCPackLog.h" @@ -58,6 +61,8 @@ const char* cmDocumentationOptions[][2] = { { "-R ", "Override/define CPACK_PACKAGE_VERSION" }, { "-B ", "Override/define CPACK_PACKAGE_DIRECTORY" }, { "--vendor ", "Override/define CPACK_PACKAGE_VENDOR" }, + { "--preset", "Read arguments from a package preset" }, + { "--list-presets", "List available package presets" }, { nullptr, nullptr } }; @@ -116,6 +121,9 @@ int main(int argc, char const* const* argv) std::string cpackProjectVendor; std::string cpackConfigFile; + std::string preset; + bool listPresets = false; + std::map definitions; auto const verboseLambda = [&log](const std::string&, cmake*, @@ -182,6 +190,10 @@ int main(int argc, char const* const* argv) CommandArgument::setToValue(cpackProjectPatch) }, CommandArgument{ "--vendor", CommandArgument::Values::One, CommandArgument::setToValue(cpackProjectVendor) }, + CommandArgument{ "--preset", CommandArgument::Values::One, + CommandArgument::setToValue(preset) }, + CommandArgument{ "--list-presets", CommandArgument::Values::Zero, + CommandArgument::setToTrue(listPresets) }, CommandArgument{ "-D", CommandArgument::Values::One, [&log, &definitions](const std::string& arg, cmake*, @@ -228,6 +240,160 @@ int main(int argc, char const* const* argv) } } + cmCPackGeneratorFactory generators; + generators.SetLogger(&log); + + // Set up presets + if (!preset.empty() || listPresets) { + const auto workingDirectory = cmSystemTools::GetCurrentWorkingDirectory(); + + auto const presetGeneratorsPresent = + [&generators](const cmCMakePresetsGraph::PackagePreset& p) { + return std::all_of(p.Generators.begin(), p.Generators.end(), + [&generators](const std::string& gen) { + return generators.GetGeneratorsList().count( + gen) != 0; + }); + }; + + cmCMakePresetsGraph presetsGraph; + auto result = presetsGraph.ReadProjectPresets(workingDirectory); + if (result != cmCMakePresetsGraph::ReadFileResult::READ_OK) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "Could not read presets from " + << workingDirectory << ": " + << cmCMakePresetsGraph::ResultToString(result) + << std::endl); + return 1; + } + + if (listPresets) { + presetsGraph.PrintPackagePresetList(presetGeneratorsPresent); + return 0; + } + + auto presetPair = presetsGraph.PackagePresets.find(preset); + if (presetPair == presetsGraph.PackagePresets.end()) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "No such package preset in " << workingDirectory << ": \"" + << preset << '"' << std::endl); + presetsGraph.PrintPackagePresetList(presetGeneratorsPresent); + return 1; + } + + if (presetPair->second.Unexpanded.Hidden) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "Cannot use hidden package preset in " + << workingDirectory << ": \"" << preset << '"' + << std::endl); + presetsGraph.PrintPackagePresetList(presetGeneratorsPresent); + return 1; + } + + auto const& expandedPreset = presetPair->second.Expanded; + if (!expandedPreset) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "Could not evaluate package preset \"" + << preset << "\": Invalid macro expansion" << std::endl); + presetsGraph.PrintPackagePresetList(presetGeneratorsPresent); + return 1; + } + + if (!expandedPreset->ConditionResult) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "Cannot use disabled package preset in " + << workingDirectory << ": \"" << preset << '"' + << std::endl); + presetsGraph.PrintPackagePresetList(presetGeneratorsPresent); + return 1; + } + + if (!presetGeneratorsPresent(presetPair->second.Unexpanded)) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, "Cannot use preset"); + presetsGraph.PrintPackagePresetList(presetGeneratorsPresent); + return 1; + } + + auto configurePresetPair = + presetsGraph.ConfigurePresets.find(expandedPreset->ConfigurePreset); + if (configurePresetPair == presetsGraph.ConfigurePresets.end()) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "No such configure preset in " + << workingDirectory << ": \"" + << expandedPreset->ConfigurePreset << '"' << std::endl); + presetsGraph.PrintConfigurePresetList(); + return 1; + } + + if (configurePresetPair->second.Unexpanded.Hidden) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "Cannot use hidden configure preset in " + << workingDirectory << ": \"" + << expandedPreset->ConfigurePreset << '"' << std::endl); + presetsGraph.PrintConfigurePresetList(); + return 1; + } + + auto const& expandedConfigurePreset = configurePresetPair->second.Expanded; + if (!expandedConfigurePreset) { + cmCPack_Log(&log, cmCPackLog::LOG_ERROR, + "Could not evaluate configure preset \"" + << expandedPreset->ConfigurePreset + << "\": Invalid macro expansion" << std::endl); + return 1; + } + + cmSystemTools::ChangeDirectory(expandedConfigurePreset->BinaryDir); + + auto presetEnvironment = expandedPreset->Environment; + for (auto const& var : presetEnvironment) { + if (var.second) { + cmSystemTools::PutEnv(cmStrCat(var.first, '=', *var.second)); + } + } + + if (!expandedPreset->ConfigFile.empty() && cpackConfigFile.empty()) { + cpackConfigFile = expandedPreset->ConfigFile; + } + + if (!expandedPreset->Generators.empty() && generator.empty()) { + generator = cmJoin(expandedPreset->Generators, ";"); + } + + if (!expandedPreset->Configurations.empty() && cpackBuildConfig.empty()) { + cpackBuildConfig = cmJoin(expandedPreset->Configurations, ";"); + } + + definitions.insert(expandedPreset->Variables.begin(), + expandedPreset->Variables.end()); + + if (expandedPreset->DebugOutput == true) { + debugLambda("", &cminst, &globalMF); + } + + if (expandedPreset->VerboseOutput == true) { + verboseLambda("", &cminst, &globalMF); + } + + if (!expandedPreset->PackageName.empty() && cpackProjectName.empty()) { + cpackProjectName = expandedPreset->PackageName; + } + + if (!expandedPreset->PackageVersion.empty() && + cpackProjectVersion.empty()) { + cpackProjectVersion = expandedPreset->PackageVersion; + } + + if (!expandedPreset->PackageDirectory.empty() && + cpackProjectDirectory.empty()) { + cpackProjectDirectory = expandedPreset->PackageDirectory; + } + + if (!expandedPreset->VendorName.empty() && cpackProjectVendor.empty()) { + cpackProjectVendor = expandedPreset->VendorName; + } + } + cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE, "Read CPack config file: " << cpackConfigFile << std::endl); @@ -238,9 +404,6 @@ int main(int argc, char const* const* argv) cpackConfigFileSpecified = false; } - cmCPackGeneratorFactory generators; - generators.SetLogger(&log); - cmDocumentation doc; doc.addCPackStandardDocSections(); /* Were we invoked to display doc or to do some work ? diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 9b2b119..ee587b2 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -778,6 +778,7 @@ enum class ListPresets Configure, Build, Test, + Package, All, }; } @@ -1133,6 +1134,8 @@ void cmake::SetArgs(const std::vector& args) listPresets = ListPresets::Build; } else if (value == "test") { listPresets = ListPresets::Test; + } else if (value == "package") { + listPresets = ListPresets::Package; } else if (value == "all") { listPresets = ListPresets::All; } else { @@ -1300,6 +1303,8 @@ void cmake::SetArgs(const std::vector& args) presetsGraph.PrintBuildPresetList(); } else if (listPresets == ListPresets::Test) { presetsGraph.PrintTestPresetList(); + } else if (listPresets == ListPresets::Package) { + presetsGraph.PrintPackagePresetList(); } else if (listPresets == ListPresets::All) { presetsGraph.PrintAllPresets(); } diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 542742c..42ff450 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -999,6 +999,10 @@ add_RunCMake_test(CMakePresetsTest -DPython_EXECUTABLE=${Python_EXECUTABLE} -DCMake_TEST_JSON_SCHEMA=${CMake_TEST_JSON_SCHEMA} ) +add_RunCMake_test(CMakePresetsPackage + -DPython_EXECUTABLE=${Python_EXECUTABLE} + -DCMake_TEST_JSON_SCHEMA=${CMake_TEST_JSON_SCHEMA} + ) add_RunCMake_test(VerifyHeaderSets) diff --git a/Tests/RunCMake/CMakePresetsPackage/CMakeLists.txt.in b/Tests/RunCMake/CMakePresetsPackage/CMakeLists.txt.in new file mode 100644 index 0000000..129184a --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/CMakeLists.txt.in @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 3.19) +project("@CASE_NAME@" NONE) +include("@CASE_SOURCE_DIR@/@CASE_NAME@.cmake") diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-config-file-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-config-file-check.cmake new file mode 100644 index 0000000..40240f9 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-config-file-check.cmake @@ -0,0 +1,6 @@ +include("${RunCMake_TEST_BINARY_DIR}/default/CPackConfig.cmake") + +set(filename "${RunCMake_TEST_BINARY_DIR}/default/_CPack_Packages/${CPACK_TOPLEVEL_TAG}/TGZ/config-file-alt.tar.gz") +if(NOT EXISTS "${filename}") + set(RunCMake_TEST_FAILED "Expected ${filename} to exist but it does not") +endif() diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-configurations-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-configurations-check.cmake new file mode 100644 index 0000000..3d684af --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-configurations-check.cmake @@ -0,0 +1,18 @@ +if(RunCMake_GENERATOR_IS_MULTI_CONFIG) + include("${RunCMake_TEST_BINARY_DIR}/default/CPackConfig.cmake") + set(cpack_dir "${RunCMake_TEST_BINARY_DIR}/default/_CPack_Packages/${CPACK_TOPLEVEL_TAG}") + set(contents [[Debug +Release +]]) + + file(GLOB dirs RELATIVE "${cpack_dir}" "${cpack_dir}/*") + foreach(dir IN LISTS dirs) + set(configs_file "${cpack_dir}/${dir}/${CPACK_PACKAGE_FILE_NAME}/configs.txt") + file(READ "${configs_file}" actual_contents) + if(NOT contents STREQUAL actual_contents) + string(REPLACE "\n" "\n " contents_formatted "${contents}") + string(REPLACE "\n" "\n " actual_contents_formatted "${actual_contents}") + string(APPEND RunCMake_TEST_FAILED "Expected contents of ${configs_file}:\n ${contents_formatted}\nActual contents:\n ${actual_contents_formatted}\n") + endif() + endforeach() +endif() diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-debug-stdout.txt b/Tests/RunCMake/CMakePresetsPackage/Good-package-debug-stdout.txt new file mode 100644 index 0000000..be885b4 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-debug-stdout.txt @@ -0,0 +1,2 @@ +CPack: [^ +]* Enable Debug diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-generators-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-generators-check.cmake new file mode 100644 index 0000000..aaa75e4 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-generators-check.cmake @@ -0,0 +1 @@ +check_cpack_packages("TBZ2;TXZ" "") diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-no-environment-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-no-environment-check.cmake new file mode 100644 index 0000000..205e7b7 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-no-environment-check.cmake @@ -0,0 +1,7 @@ +check_cpack_packages("TGZ;TXZ" [[TEST_ENV not defined +TEST_ENV_REF=xx +TEST_ENV_OVERRIDE not defined +TEST_ENV_OVERRIDE_REF not defined +]]) + +include("${RunCMake_SOURCE_DIR}/check.cmake") diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-package-directory-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-package-directory-check.cmake new file mode 100644 index 0000000..8f4b771 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-package-directory-check.cmake @@ -0,0 +1,6 @@ +include("${RunCMake_TEST_BINARY_DIR}/default/CPackConfig.cmake") + +set(filename "${RunCMake_TEST_BINARY_DIR}/default/package-directory/_CPack_Packages/${CPACK_TOPLEVEL_TAG}/TGZ/${CPACK_PACKAGE_FILE_NAME}.tar.gz") +if(NOT EXISTS "${filename}") + set(RunCMake_TEST_FAILED "Expected ${filename} to exist but it does not") +endif() diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-package-name-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-package-name-check.cmake new file mode 100644 index 0000000..fdc4824 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-package-name-check.cmake @@ -0,0 +1,7 @@ +include("${RunCMake_TEST_BINARY_DIR}/default/CPackConfig.cmake") + +file(READ "${RunCMake_TEST_BINARY_DIR}/default/${CPACK_PACKAGE_FILE_NAME}.json" contents) +string(JSON package_name GET "${contents}" packageName) +if(NOT package_name STREQUAL "package-name") + set(RunCMake_TEST_FAILED "Expected package name to be \"package-name\" but it was \"${package_name}\"") +endif() diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-package-version-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-package-version-check.cmake new file mode 100644 index 0000000..dba9110 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-package-version-check.cmake @@ -0,0 +1,7 @@ +include("${RunCMake_TEST_BINARY_DIR}/default/CPackConfig.cmake") + +file(READ "${RunCMake_TEST_BINARY_DIR}/default/${CPACK_PACKAGE_FILE_NAME}.json" contents) +string(JSON package_version GET "${contents}" packageVersion) +if(NOT package_version STREQUAL "1.0") + set(RunCMake_TEST_FAILED "Expected package version to be \"1.0\" but it was \"${package_version}\"") +endif() diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-variables-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-variables-check.cmake new file mode 100644 index 0000000..2858170 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-variables-check.cmake @@ -0,0 +1,6 @@ +include("${RunCMake_TEST_BINARY_DIR}/default/CPackConfig.cmake") + +set(filename "${RunCMake_TEST_BINARY_DIR}/default/_CPack_Packages/${CPACK_TOPLEVEL_TAG}/TGZ/variables-package.tar.gz") +if(NOT EXISTS "${filename}") + set(RunCMake_TEST_FAILED "Expected ${filename} to exist but it does not") +endif() diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-verbose-stdout.txt b/Tests/RunCMake/CMakePresetsPackage/Good-package-verbose-stdout.txt new file mode 100644 index 0000000..22fd115 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-verbose-stdout.txt @@ -0,0 +1 @@ +CPack: Enable Verbose diff --git a/Tests/RunCMake/CMakePresetsPackage/Good-package-with-environment-check.cmake b/Tests/RunCMake/CMakePresetsPackage/Good-package-with-environment-check.cmake new file mode 100644 index 0000000..a775e4d --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good-package-with-environment-check.cmake @@ -0,0 +1,7 @@ +check_cpack_packages("TGZ;TXZ" [[TEST_ENV=Environment variable +TEST_ENV_REF=xEnvironment variablex +TEST_ENV_OVERRIDE=Override +TEST_ENV_OVERRIDE_REF=xOverridex +]]) + +include("${RunCMake_SOURCE_DIR}/check.cmake") diff --git a/Tests/RunCMake/CMakePresetsPackage/Good.cmake b/Tests/RunCMake/CMakePresetsPackage/Good.cmake new file mode 100644 index 0000000..d019443 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good.cmake @@ -0,0 +1,31 @@ +set(CPACK_PACKAGE_NAME Good) +set(CPACK_GENERATOR "TGZ;TXZ") + +include(CPack) + +install(CODE [[ +function(print_env name) + if(DEFINED ENV{${name}}) + file(APPEND $ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/env.txt "${name}=$ENV{${name}}\n") + else() + file(APPEND $ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/env.txt "${name} not defined\n") + endif() +endfunction() + +file(REMOVE $ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/env.txt) +print_env(TEST_ENV) +print_env(TEST_ENV_REF) +print_env(TEST_ENV_OVERRIDE) +print_env(TEST_ENV_OVERRIDE_REF) + +file(APPEND $ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/configs.txt "$\n") +]]) + +file(WRITE "${CMAKE_BINARY_DIR}/CPackConfigAlt.cmake" [[include(${CMAKE_CURRENT_LIST_DIR}/CPackConfig.cmake) +set(CPACK_PACKAGE_FILE_NAME "config-file-alt") +]]) + +file(WRITE "${CMAKE_BINARY_DIR}/external_package.cmake" [[if(NOT CPACK_PACKAGE_VENDOR STREQUAL "some-vendor") + message(FATAL_ERROR "Expected vendor to be \"some-vendor\" but it was \"${CPACK_PACKAGE_VENDOR}\"") +endif() +]]) diff --git a/Tests/RunCMake/CMakePresetsPackage/Good.json.in b/Tests/RunCMake/CMakePresetsPackage/Good.json.in new file mode 100644 index 0000000..0c0e7d9 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/Good.json.in @@ -0,0 +1,135 @@ +{ + "version": 6, + "configurePresets": [ + { + "name": "default", + "generator": "@RunCMake_GENERATOR@", + "binaryDir": "${sourceDir}/build/${presetName}", + "environment": { + "TEST_ENV": "Environment variable", + "TEST_ENV_OVERRIDE": "Overridden environment variable" + } + } + ], + "buildPresets": [ + { + "name": "build-default-debug", + "configurePreset": "default", + "configuration": "Debug" + }, + { + "name": "build-default-release", + "inherits": "build-default-debug", + "configuration": "Release" + } + ], + "packagePresets": [ + { + "name": "minimal", + "configurePreset": "default" + }, + { + "name": "defaults", + "hidden": false, + "inherits": [], + "vendor": {}, + "displayName": "", + "description": "", + "environment": {}, + "configurePreset": "default", + "inheritConfigureEnvironment": true + }, + { + "name": "no-environment", + "configurePreset": "default", + "inheritConfigureEnvironment": false, + "environment": { + "TEST_ENV_REF": "x$env{TEST_ENV}x" + } + }, + { + "name": "with-environment", + "inherits": "no-environment", + "inheritConfigureEnvironment": true, + "environment": { + "TEST_ENV_OVERRIDE": "Override", + "TEST_ENV_OVERRIDE_REF": "x$env{TEST_ENV_OVERRIDE}x", + "TEST_ENV_REF": "x$env{TEST_ENV}x" + } + }, + { + "name": "generators", + "inherits": "minimal", + "generators": [ + "TBZ2", + "TXZ" + ] + }, + { + "name": "configurations", + "inherits": "minimal", + "configurations": [ + "Debug", + "Release" + ] + }, + { + "name": "variables", + "inherits": "minimal", + "variables": { + "CPACK_PACKAGE_FILE_NAME": "variables-package" + } + }, + { + "name": "config-file", + "inherits": "minimal", + "configFile": "CPackConfigAlt.cmake" + }, + { + "name": "debug", + "inherits": "minimal", + "output": { + "debug": true + } + }, + { + "name": "verbose", + "inherits": "minimal", + "output": { + "verbose": true + } + }, + { + "name": "package-name", + "inherits": "minimal", + "generators": [ + "External" + ], + "packageName": "package-name" + }, + { + "name": "package-version", + "inherits": "minimal", + "generators": [ + "External" + ], + "packageVersion": "1.0" + }, + { + "name": "package-directory", + "inherits": "minimal", + "packageDirectory": "${sourceDir}/build/default/package-directory" + }, + { + "name": "vendor-name", + "inherits": "minimal", + "generators": [ + "External" + ], + "variables": { + "CPACK_EXTERNAL_PACKAGE_SCRIPT": "${sourceDir}/build/default/external_package.cmake" + }, + "vendorName": "some-vendor" + } + ] +} diff --git a/Tests/RunCMake/CMakePresetsPackage/RunCMakeTest.cmake b/Tests/RunCMake/CMakePresetsPackage/RunCMakeTest.cmake new file mode 100644 index 0000000..0d5def1 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/RunCMakeTest.cmake @@ -0,0 +1,101 @@ +include(RunCMake) + +# Presets do not support legacy VS generator name architecture suffix. +if(RunCMake_GENERATOR MATCHES "^(Visual Studio [0-9]+ [0-9]+) ") + set(RunCMake_GENERATOR "${CMAKE_MATCH_1}") +endif() + +function(run_cmake_package_presets name CMakePresetsPackage_CONFIGURE_PRESETS CMakePresetsPackage_BUILD_PRESETS CMakePresetsPackage_PACKAGE_PRESETS) + set(RunCMake_TEST_SOURCE_DIR "${RunCMake_BINARY_DIR}/${name}") + set(RunCMake_TEST_BINARY_DIR "${RunCMake_TEST_SOURCE_DIR}/build") + set(RunCMake_TEST_COMMAND_WORKING_DIRECTORY "${RunCMake_TEST_SOURCE_DIR}") + + set(RunCMake_TEST_NO_CLEAN TRUE) + + file(REMOVE_RECURSE "${RunCMake_TEST_SOURCE_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + + set(CASE_NAME "${name}") + set(CASE_SOURCE_DIR "${RunCMake_SOURCE_DIR}") + configure_file("${RunCMake_SOURCE_DIR}/CMakeLists.txt.in" "${RunCMake_TEST_SOURCE_DIR}/CMakeLists.txt" @ONLY) + + if(NOT CMakePresetsPackage_FILE) + set(CMakePresetsPackage_FILE "${RunCMake_SOURCE_DIR}/${name}.json.in") + endif() + if(EXISTS "${CMakePresetsPackage_FILE}") + configure_file("${CMakePresetsPackage_FILE}" "${RunCMake_TEST_SOURCE_DIR}/CMakePresets.json" @ONLY) + endif() + + if(NOT CMakeUserPresets_FILE) + set(CMakeUserPresets_FILE "${RunCMake_SOURCE_DIR}/${name}User.json.in") + endif() + if(EXISTS "${CMakeUserPresets_FILE}") + configure_file("${CMakeUserPresets_FILE}" "${RunCMake_TEST_SOURCE_DIR}/CMakeUserPresets.json" @ONLY) + endif() + + foreach(ASSET ${CMakePresetsPackage_ASSETS}) + configure_file("${RunCMake_SOURCE_DIR}/${ASSET}" "${RunCMake_TEST_SOURCE_DIR}" COPYONLY) + endforeach() + + if (NOT CMakePresetsPackage_NO_CONFIGURE) + foreach(CONFIGURE_PRESET ${CMakePresetsPackage_CONFIGURE_PRESETS}) + run_cmake_command("${name}-configure-${CONFIGURE_PRESET}" + "${CMAKE_COMMAND}" "--preset" "${CONFIGURE_PRESET}") + endforeach() + endif() + + if (NOT CMakePresetsPackage_NO_BUILD) + foreach(BUILD_PRESET ${CMakePresetsPackage_BUILD_PRESETS}) + run_cmake_command("${name}-build-${BUILD_PRESET}" + "${CMAKE_COMMAND}" "--build" "--preset" "${BUILD_PRESET}") + endforeach() + endif() + + set(eq 0) + foreach(PACKAGE_PRESET ${CMakePresetsPackage_PACKAGE_PRESETS}) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}/default/_CPack_Packages") + + if (EXISTS "${RunCMake_SOURCE_DIR}/${name}-package-${PACKAGE_PRESET}-check.cmake") + set(RunCMake-check-file "${name}-package-${PACKAGE_PRESET}-check.cmake") + else() + set(RunCMake-check-file "check.cmake") + endif() + + if(eq) + run_cmake_command(${name}-package-${PACKAGE_PRESET} + ${CMAKE_CPACK_COMMAND} "--preset=${PACKAGE_PRESET}" ${ARGN}) + set(eq 0) + else() + run_cmake_command(${name}-package-${PACKAGE_PRESET} + ${CMAKE_CPACK_COMMAND} "--preset" "${PACKAGE_PRESET}" ${ARGN}) + set(eq 1) + endif() + endforeach() +endfunction() + +function(check_cpack_packages generators contents) + include("${RunCMake_TEST_BINARY_DIR}/default/CPackConfig.cmake") + + set(cpack_dir "${RunCMake_TEST_BINARY_DIR}/default/_CPack_Packages/${CPACK_TOPLEVEL_TAG}") + file(GLOB dirs RELATIVE "${cpack_dir}" "${cpack_dir}/*") + if(NOT dirs STREQUAL generators) + string(APPEND RunCMake_TEST_FAILED "Expected CPack generators: ${generators}\nActual CPack generators: ${dirs}\n") + endif() + + if(contents) + foreach(dir IN LISTS dirs) + set(env_file "${cpack_dir}/${dir}/${CPACK_PACKAGE_FILE_NAME}/env.txt") + file(READ "${env_file}" actual_contents) + if(NOT contents STREQUAL actual_contents) + string(REPLACE "\n" "\n " contents_formatted "${contents}") + string(REPLACE "\n" "\n " actual_contents_formatted "${actual_contents}") + string(APPEND RunCMake_TEST_FAILED "Expected contents of ${env_file}:\n ${contents_formatted}\nActual contents:\n ${actual_contents_formatted}\n") + endif() + endforeach() + endif() + + set(RunCMake_TEST_FAILED ${RunCMake_TEST_FAILED} PARENT_SCOPE) +endfunction() + +run_cmake_package_presets(UnsupportedVersion "x" "" "") +run_cmake_package_presets(Good "default" "build-default-debug" "no-environment;with-environment;generators;configurations;variables;config-file;debug;verbose;package-name;package-version;package-directory;vendor-name") diff --git a/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-result.txt b/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-stderr.txt b/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-stderr.txt new file mode 100644 index 0000000..325af70 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion-configure-x-stderr.txt @@ -0,0 +1,2 @@ +^CMake Error: Could not read presets from [^ +]*/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion: File version must be 5 or higher for package preset support$ diff --git a/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion.json.in b/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion.json.in new file mode 100644 index 0000000..e5f7240 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/UnsupportedVersion.json.in @@ -0,0 +1,4 @@ +{ + "version": 5, + "packagePresets": [] +} diff --git a/Tests/RunCMake/CMakePresetsPackage/check.cmake b/Tests/RunCMake/CMakePresetsPackage/check.cmake new file mode 100644 index 0000000..e79c4f1 --- /dev/null +++ b/Tests/RunCMake/CMakePresetsPackage/check.cmake @@ -0,0 +1,3 @@ +set(CMakePresets_VALIDATE_SCRIPT_PATH "${RunCMake_SOURCE_DIR}/../CMakePresets/validate_schema.py") +include("${RunCMake_SOURCE_DIR}/../CMakePresets/validate_schema.cmake") +include("${RunCMake_SOURCE_DIR}/../CMakePresets/check.cmake") -- cgit v0.12