1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmCPackExternalGenerator.h"
11 #include <cm3p/json/value.h>
12 #include <cm3p/json/writer.h>
14 #include "cmsys/FStream.hxx"
16 #include "cmCPackComponentGroup.h"
17 #include "cmCPackLog.h"
19 #include "cmMakefile.h"
20 #include "cmSystemTools.h"
23 int cmCPackExternalGenerator::InitializeInternal()
25 this->SetOption("CPACK_EXTERNAL_KNOWN_VERSIONS", "1.0");
27 if (!this->ReadListFile("Internal/CPack/CPackExternal.cmake")) {
28 cmCPackLogger(cmCPackLog::LOG_ERROR
,
29 "Error while executing CPackExternal.cmake" << std::endl
);
33 std::string major
= this->GetOption("CPACK_EXTERNAL_SELECTED_MAJOR");
35 this->Generator
= cm::make_unique
<cmCPackExternalVersion1Generator
>(this);
38 return this->Superclass::InitializeInternal();
41 int cmCPackExternalGenerator::PackageFiles()
43 Json::StreamWriterBuilder builder
;
44 builder
["indentation"] = " ";
46 std::string filename
= "package.json";
47 if (!this->packageFileNames
.empty()) {
48 filename
= this->packageFileNames
[0];
51 cmsys::ofstream
fout(filename
.c_str());
52 std::unique_ptr
<Json::StreamWriter
> jout(builder
.newStreamWriter());
54 Json::Value
root(Json::objectValue
);
56 if (!this->Generator
->WriteToJSON(root
)) {
60 if (jout
->write(root
, &fout
)) {
64 cmValue packageScript
= this->GetOption("CPACK_EXTERNAL_PACKAGE_SCRIPT");
65 if (cmNonempty(packageScript
)) {
66 if (!cmSystemTools::FileIsFullPath(*packageScript
)) {
68 cmCPackLog::LOG_ERROR
,
69 "CPACK_EXTERNAL_PACKAGE_SCRIPT does not contain a full file path"
74 bool res
= this->MakefileMap
->ReadListFile(*packageScript
);
76 if (cmSystemTools::GetErrorOccurredFlag() || !res
) {
80 cmValue builtPackages
= this->GetOption("CPACK_EXTERNAL_BUILT_PACKAGES");
82 cmExpandList(builtPackages
, this->packageFileNames
);
89 bool cmCPackExternalGenerator::SupportsComponentInstallation() const
94 int cmCPackExternalGenerator::InstallProjectViaInstallCommands(
95 bool setDestDir
, const std::string
& tempInstallDirectory
)
97 if (this->StagingEnabled()) {
98 return this->cmCPackGenerator::InstallProjectViaInstallCommands(
99 setDestDir
, tempInstallDirectory
);
105 int cmCPackExternalGenerator::InstallProjectViaInstallScript(
106 bool setDestDir
, const std::string
& tempInstallDirectory
)
108 if (this->StagingEnabled()) {
109 return this->cmCPackGenerator::InstallProjectViaInstallScript(
110 setDestDir
, tempInstallDirectory
);
116 int cmCPackExternalGenerator::InstallProjectViaInstalledDirectories(
117 bool setDestDir
, const std::string
& tempInstallDirectory
,
118 const mode_t
* default_dir_mode
)
120 if (this->StagingEnabled()) {
121 return this->cmCPackGenerator::InstallProjectViaInstalledDirectories(
122 setDestDir
, tempInstallDirectory
, default_dir_mode
);
128 int cmCPackExternalGenerator::RunPreinstallTarget(
129 const std::string
& installProjectName
, const std::string
& installDirectory
,
130 cmGlobalGenerator
* globalGenerator
, const std::string
& buildConfig
)
132 if (this->StagingEnabled()) {
133 return this->cmCPackGenerator::RunPreinstallTarget(
134 installProjectName
, installDirectory
, globalGenerator
, buildConfig
);
140 int cmCPackExternalGenerator::InstallCMakeProject(
141 bool setDestDir
, const std::string
& installDirectory
,
142 const std::string
& baseTempInstallDirectory
, const mode_t
* default_dir_mode
,
143 const std::string
& component
, bool componentInstall
,
144 const std::string
& installSubDirectory
, const std::string
& buildConfig
,
145 std::string
& absoluteDestFiles
)
147 if (this->StagingEnabled()) {
148 return this->cmCPackGenerator::InstallCMakeProject(
149 setDestDir
, installDirectory
, baseTempInstallDirectory
, default_dir_mode
,
150 component
, componentInstall
, installSubDirectory
, buildConfig
,
157 bool cmCPackExternalGenerator::StagingEnabled() const
159 return !this->GetOption("CPACK_EXTERNAL_ENABLE_STAGING").IsOff();
162 cmCPackExternalGenerator::cmCPackExternalVersionGenerator::
163 cmCPackExternalVersionGenerator(cmCPackExternalGenerator
* parent
)
168 int cmCPackExternalGenerator::cmCPackExternalVersionGenerator::WriteVersion(
171 root
["formatVersionMajor"] = this->GetVersionMajor();
172 root
["formatVersionMinor"] = this->GetVersionMinor();
177 int cmCPackExternalGenerator::cmCPackExternalVersionGenerator::WriteToJSON(
180 if (!this->WriteVersion(root
)) {
184 cmValue packageName
= this->Parent
->GetOption("CPACK_PACKAGE_NAME");
186 root
["packageName"] = *packageName
;
189 cmValue packageVersion
= this->Parent
->GetOption("CPACK_PACKAGE_VERSION");
190 if (packageVersion
) {
191 root
["packageVersion"] = *packageVersion
;
194 cmValue packageDescriptionFile
=
195 this->Parent
->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
196 if (packageDescriptionFile
) {
197 root
["packageDescriptionFile"] = *packageDescriptionFile
;
200 cmValue packageDescriptionSummary
=
201 this->Parent
->GetOption("CPACK_PACKAGE_DESCRIPTION_SUMMARY");
202 if (packageDescriptionSummary
) {
203 root
["packageDescriptionSummary"] = *packageDescriptionSummary
;
206 cmValue buildConfigCstr
= this->Parent
->GetOption("CPACK_BUILD_CONFIG");
207 if (buildConfigCstr
) {
208 root
["buildConfig"] = *buildConfigCstr
;
211 cmValue defaultDirectoryPermissions
=
212 this->Parent
->GetOption("CPACK_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS");
213 if (cmNonempty(defaultDirectoryPermissions
)) {
214 root
["defaultDirectoryPermissions"] = *defaultDirectoryPermissions
;
216 if (cmIsInternallyOn(this->Parent
->GetOption("CPACK_SET_DESTDIR"))) {
217 root
["setDestdir"] = true;
218 root
["packagingInstallPrefix"] =
219 *this->Parent
->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
221 root
["setDestdir"] = false;
224 root
["stripFiles"] = !this->Parent
->GetOption("CPACK_STRIP_FILES").IsOff();
225 root
["warnOnAbsoluteInstallDestination"] =
226 this->Parent
->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION");
227 root
["errorOnAbsoluteInstallDestination"] =
228 this->Parent
->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION");
230 Json::Value
& projects
= root
["projects"] = Json::Value(Json::arrayValue
);
231 for (auto& project
: this->Parent
->CMakeProjects
) {
232 Json::Value
jsonProject(Json::objectValue
);
234 jsonProject
["projectName"] = project
.ProjectName
;
235 jsonProject
["component"] = project
.Component
;
236 jsonProject
["directory"] = project
.Directory
;
237 jsonProject
["subDirectory"] = project
.SubDirectory
;
239 Json::Value
& installationTypes
= jsonProject
["installationTypes"] =
240 Json::Value(Json::arrayValue
);
241 for (auto& installationType
: project
.InstallationTypes
) {
242 installationTypes
.append(installationType
->Name
);
245 Json::Value
& components
= jsonProject
["components"] =
246 Json::Value(Json::arrayValue
);
247 for (auto& component
: project
.Components
) {
248 components
.append(component
->Name
);
251 projects
.append(jsonProject
);
254 Json::Value
& installationTypes
= root
["installationTypes"] =
255 Json::Value(Json::objectValue
);
256 for (auto& installationType
: this->Parent
->InstallationTypes
) {
257 Json::Value
& jsonInstallationType
=
258 installationTypes
[installationType
.first
] =
259 Json::Value(Json::objectValue
);
261 jsonInstallationType
["name"] = installationType
.second
.Name
;
262 jsonInstallationType
["displayName"] = installationType
.second
.DisplayName
;
263 jsonInstallationType
["index"] = installationType
.second
.Index
;
266 Json::Value
& components
= root
["components"] =
267 Json::Value(Json::objectValue
);
268 for (auto& component
: this->Parent
->Components
) {
269 Json::Value
& jsonComponent
= components
[component
.first
] =
270 Json::Value(Json::objectValue
);
272 jsonComponent
["name"] = component
.second
.Name
;
273 jsonComponent
["displayName"] = component
.second
.DisplayName
;
274 if (component
.second
.Group
) {
275 jsonComponent
["group"] = component
.second
.Group
->Name
;
277 jsonComponent
["isRequired"] = component
.second
.IsRequired
;
278 jsonComponent
["isHidden"] = component
.second
.IsHidden
;
279 jsonComponent
["isDisabledByDefault"] =
280 component
.second
.IsDisabledByDefault
;
281 jsonComponent
["isDownloaded"] = component
.second
.IsDownloaded
;
282 jsonComponent
["description"] = component
.second
.Description
;
283 jsonComponent
["archiveFile"] = component
.second
.ArchiveFile
;
285 Json::Value
& cmpInstallationTypes
= jsonComponent
["installationTypes"] =
286 Json::Value(Json::arrayValue
);
287 for (auto& installationType
: component
.second
.InstallationTypes
) {
288 cmpInstallationTypes
.append(installationType
->Name
);
291 Json::Value
& dependencies
= jsonComponent
["dependencies"] =
292 Json::Value(Json::arrayValue
);
293 for (auto& dep
: component
.second
.Dependencies
) {
294 dependencies
.append(dep
->Name
);
298 Json::Value
& groups
= root
["componentGroups"] =
299 Json::Value(Json::objectValue
);
300 for (auto& group
: this->Parent
->ComponentGroups
) {
301 Json::Value
& jsonGroup
= groups
[group
.first
] =
302 Json::Value(Json::objectValue
);
304 jsonGroup
["name"] = group
.second
.Name
;
305 jsonGroup
["displayName"] = group
.second
.DisplayName
;
306 jsonGroup
["description"] = group
.second
.Description
;
307 jsonGroup
["isBold"] = group
.second
.IsBold
;
308 jsonGroup
["isExpandedByDefault"] = group
.second
.IsExpandedByDefault
;
309 if (group
.second
.ParentGroup
) {
310 jsonGroup
["parentGroup"] = group
.second
.ParentGroup
->Name
;
313 Json::Value
& subgroups
= jsonGroup
["subgroups"] =
314 Json::Value(Json::arrayValue
);
315 for (auto& subgroup
: group
.second
.Subgroups
) {
316 subgroups
.append(subgroup
->Name
);
319 Json::Value
& groupComponents
= jsonGroup
["components"] =
320 Json::Value(Json::arrayValue
);
321 for (auto& component
: group
.second
.Components
) {
322 groupComponents
.append(component
->Name
);