cmFindCommon.cxx 10.8 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
#include "cmFindCommon.h"
4

5 6 7 8
#include <algorithm>
#include <string.h>
#include <utility>

9
#include "cmMakefile.h"
10 11
#include "cmSystemTools.h"

12 13
cmFindCommon::PathGroup cmFindCommon::PathGroup::All("ALL");
cmFindCommon::PathLabel cmFindCommon::PathLabel::CMake("CMAKE");
14 15
cmFindCommon::PathLabel cmFindCommon::PathLabel::CMakeEnvironment(
  "CMAKE_ENVIRONMENT");
16
cmFindCommon::PathLabel cmFindCommon::PathLabel::Hints("HINTS");
17 18
cmFindCommon::PathLabel cmFindCommon::PathLabel::SystemEnvironment(
  "SYSTM_ENVIRONMENT");
19 20 21
cmFindCommon::PathLabel cmFindCommon::PathLabel::CMakeSystem("CMAKE_SYSTEM");
cmFindCommon::PathLabel cmFindCommon::PathLabel::Guess("GUESS");

22 23 24 25 26 27 28 29 30
cmFindCommon::cmFindCommon()
{
  this->FindRootPathMode = RootPathModeBoth;
  this->NoDefaultPath = false;
  this->NoCMakePath = false;
  this->NoCMakeEnvironmentPath = false;
  this->NoSystemEnvironmentPath = false;
  this->NoCMakeSystemPath = false;

31 32
// OS X Bundle and Framework search policy.  The default is to
// search frameworks first on apple.
33 34 35 36 37 38 39 40 41 42 43
#if defined(__APPLE__)
  this->SearchFrameworkFirst = true;
  this->SearchAppBundleFirst = true;
#else
  this->SearchFrameworkFirst = false;
  this->SearchAppBundleFirst = false;
#endif
  this->SearchFrameworkOnly = false;
  this->SearchFrameworkLast = false;
  this->SearchAppBundleOnly = false;
  this->SearchAppBundleLast = false;
44 45

  this->InitializeSearchPathGroups();
46
}
47 48 49 50 51

cmFindCommon::~cmFindCommon()
{
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
void cmFindCommon::InitializeSearchPathGroups()
{
  std::vector<PathLabel>* labels;

  // Define the varoius different groups of path types

  // All search paths
  labels = &this->PathGroupLabelMap[PathGroup::All];
  labels->push_back(PathLabel::CMake);
  labels->push_back(PathLabel::CMakeEnvironment);
  labels->push_back(PathLabel::Hints);
  labels->push_back(PathLabel::SystemEnvironment);
  labels->push_back(PathLabel::CMakeSystem);
  labels->push_back(PathLabel::Guess);

  // Define the search group order
  this->PathGroupOrder.push_back(PathGroup::All);

  // Create the idividual labeld search paths
71 72 73 74 75 76 77 78 79 80 81 82
  this->LabeledPaths.insert(
    std::make_pair(PathLabel::CMake, cmSearchPath(this)));
  this->LabeledPaths.insert(
    std::make_pair(PathLabel::CMakeEnvironment, cmSearchPath(this)));
  this->LabeledPaths.insert(
    std::make_pair(PathLabel::Hints, cmSearchPath(this)));
  this->LabeledPaths.insert(
    std::make_pair(PathLabel::SystemEnvironment, cmSearchPath(this)));
  this->LabeledPaths.insert(
    std::make_pair(PathLabel::CMakeSystem, cmSearchPath(this)));
  this->LabeledPaths.insert(
    std::make_pair(PathLabel::Guess, cmSearchPath(this)));
83 84
}

85 86 87 88 89 90
void cmFindCommon::SelectDefaultRootPathMode()
{
  // Check the policy variable for this find command type.
  std::string findRootPathVar = "CMAKE_FIND_ROOT_PATH_MODE_";
  findRootPathVar += this->CMakePathName;
  std::string rootPathMode =
91
    this->Makefile->GetSafeDefinition(findRootPathVar);
92
  if (rootPathMode == "NEVER") {
93
    this->FindRootPathMode = RootPathModeNever;
94
  } else if (rootPathMode == "ONLY") {
95
    this->FindRootPathMode = RootPathModeOnly;
96
  } else if (rootPathMode == "BOTH") {
97
    this->FindRootPathMode = RootPathModeBoth;
98
  }
99 100 101 102 103
}

void cmFindCommon::SelectDefaultMacMode()
{
  std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
104
  if (ff == "NEVER") {
105 106 107
    this->SearchFrameworkLast = false;
    this->SearchFrameworkFirst = false;
    this->SearchFrameworkOnly = false;
108
  } else if (ff == "ONLY") {
109 110 111
    this->SearchFrameworkLast = false;
    this->SearchFrameworkFirst = false;
    this->SearchFrameworkOnly = true;
112
  } else if (ff == "FIRST") {
113 114 115
    this->SearchFrameworkLast = false;
    this->SearchFrameworkFirst = true;
    this->SearchFrameworkOnly = false;
116
  } else if (ff == "LAST") {
117 118 119
    this->SearchFrameworkLast = true;
    this->SearchFrameworkFirst = false;
    this->SearchFrameworkOnly = false;
120
  }
121 122

  std::string fab = this->Makefile->GetSafeDefinition("CMAKE_FIND_APPBUNDLE");
123
  if (fab == "NEVER") {
124 125 126
    this->SearchAppBundleLast = false;
    this->SearchAppBundleFirst = false;
    this->SearchAppBundleOnly = false;
127
  } else if (fab == "ONLY") {
128 129 130
    this->SearchAppBundleLast = false;
    this->SearchAppBundleFirst = false;
    this->SearchAppBundleOnly = true;
131
  } else if (fab == "FIRST") {
132 133 134
    this->SearchAppBundleLast = false;
    this->SearchAppBundleFirst = true;
    this->SearchAppBundleOnly = false;
135
  } else if (fab == "LAST") {
136 137 138
    this->SearchAppBundleLast = true;
    this->SearchAppBundleFirst = false;
    this->SearchAppBundleOnly = false;
139
  }
140 141 142 143 144 145 146 147 148 149 150 151
}

void cmFindCommon::RerootPaths(std::vector<std::string>& paths)
{
#if 0
  for(std::vector<std::string>::const_iterator i = paths.begin();
      i != paths.end(); ++i)
    {
    fprintf(stderr, "[%s]\n", i->c_str());
    }
#endif
  // Short-circuit if there is nothing to do.
152
  if (this->FindRootPathMode == RootPathModeNever) {
153
    return;
154
  }
155

156 157
  const char* sysroot = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  const char* rootPath = this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
158 159
  const bool noSysroot = !sysroot || !*sysroot;
  const bool noRootPath = !rootPath || !*rootPath;
160
  if (noSysroot && noRootPath) {
161
    return;
162
  }
163 164 165

  // Construct the list of path roots with no trailing slashes.
  std::vector<std::string> roots;
166
  if (rootPath) {
167
    cmSystemTools::ExpandListArgument(rootPath, roots);
168 169
  }
  if (sysroot) {
170
    roots.push_back(sysroot);
171 172 173
  }
  for (std::vector<std::string>::iterator ri = roots.begin();
       ri != roots.end(); ++ri) {
174
    cmSystemTools::ConvertToUnixSlashes(*ri);
175
  }
176

177
  const char* stagePrefix =
178
    this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
179

180 181 182 183
  // Copy the original set of unrooted paths.
  std::vector<std::string> unrootedPaths = paths;
  paths.clear();

184 185 186 187
  for (std::vector<std::string>::const_iterator ri = roots.begin();
       ri != roots.end(); ++ri) {
    for (std::vector<std::string>::const_iterator ui = unrootedPaths.begin();
         ui != unrootedPaths.end(); ++ui) {
188 189 190 191
      // Place the unrooted path under the current root if it is not
      // already inside.  Skip the unrooted path if it is relative to
      // a user home directory or is empty.
      std::string rootedDir;
192 193
      if (cmSystemTools::IsSubDirectory(*ui, *ri) ||
          (stagePrefix && cmSystemTools::IsSubDirectory(*ui, stagePrefix))) {
194
        rootedDir = *ui;
195
      } else if (!ui->empty() && (*ui)[0] != '~') {
196 197 198 199 200
        // Start with the new root.
        rootedDir = *ri;
        rootedDir += "/";

        // Append the original path with its old root removed.
201
        rootedDir += cmSystemTools::SplitPathRootComponent(*ui);
202
      }
203 204 205 206

      // Store the new path.
      paths.push_back(rootedDir);
    }
207
  }
208 209 210

  // If searching both rooted and unrooted paths add the original
  // paths again.
211
  if (this->FindRootPathMode == RootPathModeBoth) {
212
    paths.insert(paths.end(), unrootedPaths.begin(), unrootedPaths.end());
213
  }
214 215
}

216 217 218
void cmFindCommon::FilterPaths(const std::vector<std::string>& inPaths,
                               const std::set<std::string>& ignore,
                               std::vector<std::string>& outPaths)
219
{
220 221 222
  for (std::vector<std::string>::const_iterator i = inPaths.begin();
       i != inPaths.end(); ++i) {
    if (ignore.count(*i) == 0) {
223
      outPaths.push_back(*i);
224
    }
225
  }
226 227 228 229 230
}

void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
{
  // null-terminated list of paths.
231
  static const char* paths[] = { "CMAKE_SYSTEM_IGNORE_PATH",
Daniel Pfeifer's avatar
Daniel Pfeifer committed
232
                                 "CMAKE_IGNORE_PATH", CM_NULLPTR };
233 234

  // Construct the list of path roots with no trailing slashes.
235
  for (const char** pathName = paths; *pathName; ++pathName) {
236 237
    // Get the list of paths to ignore from the variable.
    const char* ignorePath = this->Makefile->GetDefinition(*pathName);
Daniel Pfeifer's avatar
Daniel Pfeifer committed
238
    if ((ignorePath == CM_NULLPTR) || (strlen(ignorePath) == 0)) {
239
      continue;
240
    }
241 242

    cmSystemTools::ExpandListArgument(ignorePath, ignore);
243
  }
244

245 246
  for (std::vector<std::string>::iterator i = ignore.begin();
       i != ignore.end(); ++i) {
247
    cmSystemTools::ConvertToUnixSlashes(*i);
248
  }
249 250 251 252 253 254 255 256 257
}

void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
{
  std::vector<std::string> ignoreVec;
  GetIgnoredPaths(ignoreVec);
  ignore.insert(ignoreVec.begin(), ignoreVec.end());
}

258 259
bool cmFindCommon::CheckCommonArgument(std::string const& arg)
{
260
  if (arg == "NO_DEFAULT_PATH") {
261
    this->NoDefaultPath = true;
262
  } else if (arg == "NO_CMAKE_ENVIRONMENT_PATH") {
263
    this->NoCMakeEnvironmentPath = true;
264
  } else if (arg == "NO_CMAKE_PATH") {
265
    this->NoCMakePath = true;
266
  } else if (arg == "NO_SYSTEM_ENVIRONMENT_PATH") {
267
    this->NoSystemEnvironmentPath = true;
268
  } else if (arg == "NO_CMAKE_SYSTEM_PATH") {
269
    this->NoCMakeSystemPath = true;
270
  } else if (arg == "NO_CMAKE_FIND_ROOT_PATH") {
271
    this->FindRootPathMode = RootPathModeNever;
272
  } else if (arg == "ONLY_CMAKE_FIND_ROOT_PATH") {
273
    this->FindRootPathMode = RootPathModeOnly;
274
  } else if (arg == "CMAKE_FIND_ROOT_PATH_BOTH") {
275
    this->FindRootPathMode = RootPathModeBoth;
276
  } else {
277 278
    // The argument is not one of the above.
    return false;
279
  }
280 281 282 283 284 285 286 287 288 289

  // The argument is one of the above.
  return true;
}

void cmFindCommon::AddPathSuffix(std::string const& arg)
{
  std::string suffix = arg;

  // Strip leading and trailing slashes.
290
  if (suffix.empty()) {
291
    return;
292 293
  }
  if (suffix[0] == '/') {
294
    suffix = suffix.substr(1, suffix.npos);
295 296
  }
  if (suffix.empty()) {
297
    return;
298 299 300 301 302
  }
  if (suffix[suffix.size() - 1] == '/') {
    suffix = suffix.substr(0, suffix.size() - 1);
  }
  if (suffix.empty()) {
303
    return;
304
  }
305 306 307 308 309

  // Store the suffix.
  this->SearchPathSuffixes.push_back(suffix);
}

310 311
void AddTrailingSlash(std::string& s)
{
312
  if (!s.empty() && *s.rbegin() != '/') {
313
    s += '/';
314
  }
315
}
316
void cmFindCommon::ComputeFinalPaths()
317
{
318 319 320 321 322
  // Filter out ignored paths from the prefix list
  std::set<std::string> ignored;
  this->GetIgnoredPaths(ignored);

  // Combine the seperate path types, filtering out ignores
323 324
  this->SearchPaths.clear();
  std::vector<PathLabel>& allLabels = this->PathGroupLabelMap[PathGroup::All];
325 326
  for (std::vector<PathLabel>::const_iterator l = allLabels.begin();
       l != allLabels.end(); ++l) {
327
    this->LabeledPaths[*l].ExtractWithout(ignored, this->SearchPaths);
328
  }
329 330

  // Expand list of paths inside all search roots.
331
  this->RerootPaths(this->SearchPaths);
332

333
  // Add a trailing slash to all paths to aid the search process.
334 335
  std::for_each(this->SearchPaths.begin(), this->SearchPaths.end(),
                &AddTrailingSlash);
336
}
337 338 339 340 341 342 343

void cmFindCommon::SetMakefile(cmMakefile* makefile)
{
  cmCommand::SetMakefile(makefile);

  // If we are building for Apple (OSX or also iphone), make sure
  // that frameworks and bundles are searched first.
344
  if (this->Makefile->IsOn("APPLE")) {
345 346
    this->SearchFrameworkFirst = true;
    this->SearchAppBundleFirst = true;
347
  }
348
}