resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmInstallImportedRuntimeArtifactsGenerator.cxx
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 "cmInstallImportedRuntimeArtifactsGenerator.h"
4
5 #include <cassert>
6 #include <string>
7 #include <utility>
8 #include <vector>
9
10 #include "cmsys/RegularExpression.hxx"
11
12 #include "cmGeneratorExpression.h"
13 #include "cmGeneratorTarget.h"
14 #include "cmGlobalGenerator.h"
15 #include "cmInstallType.h"
16 #include "cmListFileCache.h"
17 #include "cmLocalGenerator.h"
18 #include "cmStateTypes.h"
19 #include "cmStringAlgorithms.h"
20
21 namespace {
22 const cmsys::RegularExpression FrameworkRegularExpression(
23   "^(.*/)?([^/]*)\\.framework/(.*)$");
24
25 const cmsys::RegularExpression BundleRegularExpression(
26   "^(.*/)?([^/]*)\\.app/(.*)$");
27
28 const cmsys::RegularExpression CFBundleRegularExpression(
29   "^(.*/)?([^/]*)\\.bundle/(.*)$");
30 }
31
32 cmInstallImportedRuntimeArtifactsGenerator::
33   cmInstallImportedRuntimeArtifactsGenerator(
34     std::string targetName, std::string const& dest,
35     std::string file_permissions,
36     std::vector<std::string> const& configurations,
37     std::string const& component, MessageLevel message, bool exclude_from_all,
38     bool optional, cmListFileBacktrace backtrace)
39   : cmInstallGenerator(dest, configurations, component, message,
40                        exclude_from_all, false, std::move(backtrace))
41   , TargetName(std::move(targetName))
42   , FilePermissions(std::move(file_permissions))
43   , Optional(optional)
44 {
45   this->ActionsPerConfig = true;
46 }
47
48 bool cmInstallImportedRuntimeArtifactsGenerator::Compute(cmLocalGenerator* lg)
49 {
50   // Lookup this target in the current directory.
51   this->Target = lg->FindGeneratorTargetToUse(this->TargetName);
52   if (!this->Target || !this->Target->IsImported()) {
53     // If no local target has been found, find it in the global scope.
54     this->Target =
55       lg->GetGlobalGenerator()->FindGeneratorTarget(this->TargetName);
56   }
57
58   return true;
59 }
60
61 std::string cmInstallImportedRuntimeArtifactsGenerator::GetDestination(
62   std::string const& config) const
63 {
64   return cmGeneratorExpression::Evaluate(
65     this->Destination, this->Target->GetLocalGenerator(), config);
66 }
67
68 void cmInstallImportedRuntimeArtifactsGenerator::GenerateScriptForConfig(
69   std::ostream& os, const std::string& config, Indent indent)
70 {
71   auto location = this->Target->GetFullPath(config);
72
73   switch (this->Target->GetType()) {
74     case cmStateEnums::EXECUTABLE:
75       if (this->Target->IsBundleOnApple()) {
76         cmsys::RegularExpressionMatch match;
77         if (BundleRegularExpression.find(location.c_str(), match)) {
78           auto bundleDir = match.match(1);
79           auto bundleName = match.match(2);
80           auto bundlePath = cmStrCat(bundleDir, bundleName, ".app");
81           this->AddInstallRule(os, this->GetDestination(config),
82                                cmInstallType_DIRECTORY, { bundlePath },
83                                this->Optional, nullptr,
84                                this->FilePermissions.c_str(), nullptr,
85                                " USE_SOURCE_PERMISSIONS", indent);
86         }
87       } else {
88         this->AddInstallRule(os, this->GetDestination(config),
89                              cmInstallType_EXECUTABLE, { location },
90                              this->Optional, this->FilePermissions.c_str(),
91                              nullptr, nullptr, nullptr, indent);
92       }
93       break;
94     case cmStateEnums::SHARED_LIBRARY:
95       if (this->Target->IsFrameworkOnApple()) {
96         cmsys::RegularExpressionMatch match;
97         if (FrameworkRegularExpression.find(location.c_str(), match)) {
98           auto frameworkDir = match.match(1);
99           auto frameworkName = match.match(2);
100           auto frameworkPath =
101             cmStrCat(frameworkDir, frameworkName, ".framework");
102           this->AddInstallRule(os, this->GetDestination(config),
103                                cmInstallType_DIRECTORY, { frameworkPath },
104                                this->Optional, nullptr,
105                                this->FilePermissions.c_str(), nullptr,
106                                " USE_SOURCE_PERMISSIONS", indent);
107         }
108       } else {
109         std::vector<std::string> files{ location };
110         auto soName = this->Target->GetSOName(config);
111         auto soNameFile =
112           cmStrCat(this->Target->GetDirectory(config), '/', soName);
113         if (!soName.empty() && soNameFile != location) {
114           files.push_back(soNameFile);
115         }
116         this->AddInstallRule(os, this->GetDestination(config),
117                              cmInstallType_SHARED_LIBRARY, files,
118                              this->Optional, this->FilePermissions.c_str(),
119                              nullptr, nullptr, nullptr, indent);
120       }
121       break;
122     case cmStateEnums::MODULE_LIBRARY:
123       if (this->Target->IsCFBundleOnApple()) {
124         cmsys::RegularExpressionMatch match;
125         if (CFBundleRegularExpression.find(location.c_str(), match)) {
126           auto bundleDir = match.match(1);
127           auto bundleName = match.match(2);
128           auto bundlePath = cmStrCat(bundleDir, bundleName, ".bundle");
129           this->AddInstallRule(os, this->GetDestination(config),
130                                cmInstallType_DIRECTORY, { bundlePath },
131                                this->Optional, nullptr,
132                                this->FilePermissions.c_str(), nullptr,
133                                " USE_SOURCE_PERMISSIONS", indent);
134         }
135       } else {
136         this->AddInstallRule(os, this->GetDestination(config),
137                              cmInstallType_MODULE_LIBRARY, { location },
138                              this->Optional, this->FilePermissions.c_str(),
139                              nullptr, nullptr, nullptr, indent);
140       }
141       break;
142     default:
143       assert(false && "This should never happen");
144       break;
145   }
146 }