summaryrefslogtreecommitdiff
path: root/Source/cmInstallGetRuntimeDependenciesGenerator.cxx
blob: 3e493bccdf5d2ccd994fa74d1ce83a985d8dd7b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmInstallGetRuntimeDependenciesGenerator.h"

#include <memory>
#include <ostream>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include <cm/optional>
#include <cm/string_view>
#include <cmext/string_view>

#include "cmGeneratorExpression.h"
#include "cmInstallRuntimeDependencySet.h"
#include "cmListFileCache.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmOutputConverter.h"
#include "cmStringAlgorithms.h"

namespace {
template <typename T, typename F>
void WriteMultiArgument(std::ostream& os, const cm::string_view& keyword,
                        const std::vector<T>& list,
                        cmScriptGeneratorIndent indent, F transform)
{
  bool first = true;
  for (auto const& item : list) {
    cm::optional<std::string> result = transform(item);
    if (result) {
      if (first) {
        os << indent << "  " << keyword << "\n";
        first = false;
      }
      os << indent << "    " << *result << "\n";
    }
  }
}

void WriteFilesArgument(
  std::ostream& os, const cm::string_view& keyword,
  const std::vector<std::unique_ptr<cmInstallRuntimeDependencySet::Item>>&
    items,
  const std::string& config, cmScriptGeneratorIndent indent)
{
  WriteMultiArgument(
    os, keyword, items, indent,
    [config](const std::unique_ptr<cmInstallRuntimeDependencySet::Item>& i)
      -> std::string { return cmStrCat('"', i->GetItemPath(config), '"'); });
}

void WriteGenexEvaluatorArgument(std::ostream& os,
                                 const cm::string_view& keyword,
                                 const std::vector<std::string>& genexes,
                                 const std::string& config,
                                 cmLocalGenerator* lg,
                                 cmScriptGeneratorIndent indent)
{
  WriteMultiArgument(
    os, keyword, genexes, indent,
    [config, lg](const std::string& genex) -> cm::optional<std::string> {
      std::string result = cmGeneratorExpression::Evaluate(genex, lg, config);
      if (result.empty()) {
        return cm::nullopt;
      }
      return cmOutputConverter::EscapeForCMake(result);
    });
}
}

cmInstallGetRuntimeDependenciesGenerator::
  cmInstallGetRuntimeDependenciesGenerator(
    cmInstallRuntimeDependencySet* runtimeDependencySet,
    std::vector<std::string> directories,
    std::vector<std::string> preIncludeRegexes,
    std::vector<std::string> preExcludeRegexes,
    std::vector<std::string> postIncludeRegexes,
    std::vector<std::string> postExcludeRegexes,
    std::vector<std::string> postIncludeFiles,
    std::vector<std::string> postExcludeFiles, std::string libraryComponent,
    std::string frameworkComponent, bool noInstallRPath, const char* depsVar,
    const char* rpathPrefix, std::vector<std::string> const& configurations,
    MessageLevel message, bool exclude_from_all, cmListFileBacktrace backtrace)
  : cmInstallGenerator("", configurations, "", message, exclude_from_all,
                       false, std::move(backtrace))
  , RuntimeDependencySet(runtimeDependencySet)
  , Directories(std::move(directories))
  , PreIncludeRegexes(std::move(preIncludeRegexes))
  , PreExcludeRegexes(std::move(preExcludeRegexes))
  , PostIncludeRegexes(std::move(postIncludeRegexes))
  , PostExcludeRegexes(std::move(postExcludeRegexes))
  , PostIncludeFiles(std::move(postIncludeFiles))
  , PostExcludeFiles(std::move(postExcludeFiles))
  , LibraryComponent(std::move(libraryComponent))
  , FrameworkComponent(std::move(frameworkComponent))
  , NoInstallRPath(noInstallRPath)
  , DepsVar(depsVar)
  , RPathPrefix(rpathPrefix)
{
  this->ActionsPerConfig = true;
}

bool cmInstallGetRuntimeDependenciesGenerator::Compute(cmLocalGenerator* lg)
{
  this->LocalGenerator = lg;
  return true;
}

void cmInstallGetRuntimeDependenciesGenerator::GenerateScript(std::ostream& os)
{
  // Track indentation.
  Indent indent;

  // Begin this block of installation.
  os << indent << "if(";
  if (this->FrameworkComponent.empty() ||
      this->FrameworkComponent == this->LibraryComponent) {
    os << this->CreateComponentTest(this->LibraryComponent,
                                    this->ExcludeFromAll);
  } else {
    os << this->CreateComponentTest(this->LibraryComponent, true) << " OR "
       << this->CreateComponentTest(this->FrameworkComponent,
                                    this->ExcludeFromAll);
  }
  os << ")\n";

  // Generate the script possibly with per-configuration code.
  this->GenerateScriptConfigs(os, indent.Next());

  // End this block of installation.
  os << indent << "endif()\n\n";
}

void cmInstallGetRuntimeDependenciesGenerator::GenerateScriptForConfig(
  std::ostream& os, const std::string& config, Indent indent)
{
  std::string installNameTool =
    this->LocalGenerator->GetMakefile()->GetSafeDefinition(
      "CMAKE_INSTALL_NAME_TOOL");

  os << indent << "file(GET_RUNTIME_DEPENDENCIES\n"
     << indent << "  RESOLVED_DEPENDENCIES_VAR " << this->DepsVar << '\n';
  WriteFilesArgument(os, "EXECUTABLES"_s,
                     this->RuntimeDependencySet->GetExecutables(), config,
                     indent);
  WriteFilesArgument(os, "LIBRARIES"_s,
                     this->RuntimeDependencySet->GetLibraries(), config,
                     indent);
  WriteFilesArgument(os, "MODULES"_s, this->RuntimeDependencySet->GetModules(),
                     config, indent);
  if (this->RuntimeDependencySet->GetBundleExecutable()) {
    os << indent << "  BUNDLE_EXECUTABLE \""
       << this->RuntimeDependencySet->GetBundleExecutable()->GetItemPath(
            config)
       << "\"\n";
  }
  WriteGenexEvaluatorArgument(os, "DIRECTORIES"_s, this->Directories, config,
                              this->LocalGenerator, indent);
  WriteGenexEvaluatorArgument(os, "PRE_INCLUDE_REGEXES"_s,
                              this->PreIncludeRegexes, config,
                              this->LocalGenerator, indent);
  WriteGenexEvaluatorArgument(os, "PRE_EXCLUDE_REGEXES"_s,
                              this->PreExcludeRegexes, config,
                              this->LocalGenerator, indent);
  WriteGenexEvaluatorArgument(os, "POST_INCLUDE_REGEXES"_s,
                              this->PostIncludeRegexes, config,
                              this->LocalGenerator, indent);
  WriteGenexEvaluatorArgument(os, "POST_EXCLUDE_REGEXES"_s,
                              this->PostExcludeRegexes, config,
                              this->LocalGenerator, indent);
  WriteGenexEvaluatorArgument(os, "POST_INCLUDE_FILES"_s,
                              this->PostIncludeFiles, config,
                              this->LocalGenerator, indent);
  WriteGenexEvaluatorArgument(os, "POST_EXCLUDE_FILES"_s,
                              this->PostExcludeFiles, config,
                              this->LocalGenerator, indent);

  std::set<std::string> postExcludeFiles;
  auto const addPostExclude =
    [config, &postExcludeFiles, this](
      const std::vector<std::unique_ptr<cmInstallRuntimeDependencySet::Item>>&
        tgts) {
      for (auto const& item : tgts) {
        item->AddPostExcludeFiles(config, postExcludeFiles,
                                  this->RuntimeDependencySet);
      }
    };
  addPostExclude(this->RuntimeDependencySet->GetExecutables());
  addPostExclude(this->RuntimeDependencySet->GetLibraries());
  addPostExclude(this->RuntimeDependencySet->GetModules());
  bool first = true;
  for (auto const& file : postExcludeFiles) {
    if (first) {
      os << indent << "  POST_EXCLUDE_FILES_STRICT\n";
      first = false;
    }
    os << indent << "    \"" << file << "\"\n";
  }

  if (!installNameTool.empty() && !this->NoInstallRPath) {
    os << indent << "  RPATH_PREFIX " << this->RPathPrefix << '\n';
  }
  os << indent << "  )\n";
}