Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSystemTools.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
4
#if !defined(_WIN32) && !defined(__sun) && !defined(__OpenBSD__)
5
// POSIX APIs are needed
6
// NOLINTNEXTLINE(bugprone-reserved-identifier)
7
#  define _POSIX_C_SOURCE 200809L
8
#endif
9
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) ||  \
10
  defined(__QNX__)
11
// For isascii
12
// NOLINTNEXTLINE(bugprone-reserved-identifier)
13
#  define _XOPEN_SOURCE 700
14
#endif
15
#if defined(__APPLE__)
16
// Restore Darwin APIs removed by _POSIX_C_SOURCE.
17
// NOLINTNEXTLINE(bugprone-reserved-identifier)
18
#  define _DARWIN_C_SOURCE
19
#endif
20
21
#ifndef __has_feature
22
#  define __has_feature(x) 0
23
#endif
24
25
#if !defined(__clang__) || __has_feature(cxx_thread_local)
26
#  define CM_HAVE_THREAD_LOCAL
27
#endif
28
29
#include "cmSystemTools.h"
30
31
#include <iterator>
32
33
#if defined(_WIN32) || defined(__APPLE__)
34
#  include <unordered_map>
35
#endif
36
37
#include <cm/optional>
38
#include <cmext/algorithm>
39
#include <cmext/string_view>
40
41
#include <cm3p/uv.h>
42
43
#include "cmDuration.h"
44
#include "cmELF.h"
45
#include "cmMessageMetadata.h"
46
#include "cmPathResolver.h"
47
#include "cmProcessOutput.h"
48
#include "cmRange.h"
49
#include "cmStringAlgorithms.h"
50
#include "cmUVHandlePtr.h"
51
#include "cmUVProcessChain.h"
52
#include "cmUVStream.h"
53
#include "cmValue.h"
54
#include "cmWorkingDirectory.h"
55
56
#if !defined(CMAKE_BOOTSTRAP)
57
#  include <cm3p/archive.h>
58
#  include <cm3p/archive_entry.h>
59
60
#  include "cmArchiveWrite.h"
61
#  ifndef __LA_INT64_T
62
#    define __LA_INT64_T la_int64_t
63
#  endif
64
#  ifndef __LA_SSIZE_T
65
#    define __LA_SSIZE_T la_ssize_t
66
#  endif
67
#endif
68
69
#if defined(CMake_USE_MACH_PARSER)
70
#  include "cmMachO.h"
71
#endif
72
73
#if defined(CMake_USE_XCOFF_PARSER)
74
#  include "cmXCOFF.h"
75
#endif
76
77
#include <algorithm>
78
#include <cassert>
79
#include <cerrno>
80
#include <cstdint>
81
#include <cstdio>
82
#include <cstdlib>
83
#include <cstring>
84
#include <ctime>
85
#include <functional>
86
#include <iostream>
87
#include <memory>
88
#include <random>
89
#include <sstream>
90
#include <utility>
91
#include <vector>
92
93
#ifndef CM_HAVE_THREAD_LOCAL
94
#  include <mutex>
95
#endif
96
97
#include <fcntl.h>
98
99
#include "cmsys/Directory.hxx"
100
#ifdef _WIN32
101
#  include "cmsys/Encoding.hxx"
102
#endif
103
#include "cmsys/FStream.hxx"
104
#include "cmsys/RegularExpression.hxx"
105
#include "cmsys/String.h"
106
#include "cmsys/System.h"
107
108
#if defined(_WIN32)
109
#  include <windows.h>
110
111
#  include <knownfolders.h>
112
#  include <shlobj.h>
113
// include wincrypt.h after windows.h
114
#  include <wincrypt.h>
115
#else
116
#  include <unistd.h>
117
118
#  include <sys/time.h>
119
#endif
120
121
#if defined(_WIN32) &&                                                        \
122
  (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__))
123
#  include <io.h>
124
#endif
125
126
#if defined(__APPLE__)
127
#  include <mach-o/dyld.h>
128
#endif
129
130
#ifdef __QNX__
131
#  include <malloc.h> /* for malloc/free on QNX */
132
#endif
133
134
#ifdef __linux__
135
#  include <linux/fs.h>
136
137
#  include <sys/ioctl.h>
138
#endif
139
140
#if !defined(_WIN32) && !defined(__ANDROID__)
141
#  include <sys/utsname.h>
142
#endif
143
144
#if defined(CMAKE_BOOTSTRAP) && defined(__sun) && defined(__i386)
145
#  define CMAKE_NO_MKDTEMP
146
#endif
147
148
#ifdef CMAKE_NO_MKDTEMP
149
#  include <dlfcn.h>
150
#endif
151
152
#ifndef CMAKE_NO_GETPWNAM
153
#  if defined(_WIN32)
154
#    define CMAKE_NO_GETPWNAM
155
#  endif
156
#endif
157
#ifndef CMAKE_NO_GETPWNAM
158
#  include <pwd.h>
159
#endif
160
161
#if defined(_MSC_VER) && _MSC_VER >= 1800
162
#  define CM_WINDOWS_DEPRECATED_GetVersionEx
163
#endif
164
165
namespace {
166
167
cmSystemTools::InterruptCallback s_InterruptCallback;
168
cmSystemTools::MessageCallback s_MessageCallback;
169
cmSystemTools::OutputCallback s_StderrCallback;
170
cmSystemTools::OutputCallback s_StdoutCallback;
171
172
std::string ResolveTildePath(std::string p)
173
11
{
174
11
  if (!p.empty() && p[0] == '~') {
175
0
    cm::optional<std::string> home;
176
0
    std::string::size_type last = p.find_first_of("/\\");
177
0
    if (last == std::string::npos) {
178
0
      last = p.size();
179
0
    }
180
0
    if (last == 1) {
181
#if defined(_WIN32) && !defined(__CYGWIN__)
182
      home = cmSystemTools::GetEnvVar("USERPROFILE");
183
      if (!home)
184
#endif
185
0
        home = cmSystemTools::GetEnvVar("HOME");
186
0
#ifndef CMAKE_NO_GETPWNAM
187
0
    } else if (last > 1) {
188
0
      std::string user = p.substr(1, last - 1);
189
0
      if (passwd* pw = getpwnam(user.c_str())) {
190
0
        home = std::string(pw->pw_dir);
191
0
      }
192
0
#endif
193
0
    }
194
0
    if (home) {
195
0
      p.replace(0, last, *home);
196
0
    }
197
0
  }
198
11
  return p;
199
11
}
200
201
#ifdef _WIN32
202
std::string GetDosDriveWorkingDirectory(char letter)
203
{
204
  // The Windows command processor tracks a per-drive working
205
  // directory for compatibility with MS-DOS by using special
206
  // environment variables named "=C:".
207
  // https://web.archive.org/web/20100522040616/
208
  // https://blogs.msdn.com/oldnewthing/archive/2010/05/06/10008132.aspx
209
  return cmSystemTools::GetEnvVar(cmStrCat('=', letter, ':'))
210
    .value_or(std::string());
211
}
212
213
cmsys::Status ReadNameOnDisk(std::string const& path, std::string& name)
214
{
215
  std::wstring wp = cmsys::Encoding::ToWide(path);
216
  HANDLE h = CreateFileW(
217
    wp.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
218
    FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr);
219
  if (h == INVALID_HANDLE_VALUE) {
220
    return cmsys::Status::Windows_GetLastError();
221
  }
222
223
  WCHAR local_fni[((sizeof(FILE_NAME_INFO) - 1) / sizeof(WCHAR)) + 1024];
224
  size_t fni_size = sizeof(local_fni);
225
  auto* fni = reinterpret_cast<FILE_NAME_INFO*>(local_fni);
226
  if (!GetFileInformationByHandleEx(h, FileNameInfo, fni, fni_size)) {
227
    DWORD e = GetLastError();
228
    if (e != ERROR_MORE_DATA) {
229
      CloseHandle(h);
230
      return cmsys::Status::Windows(e);
231
    }
232
    fni_size = fni->FileNameLength;
233
    fni = static_cast<FILE_NAME_INFO*>(malloc(fni_size));
234
    if (!fni) {
235
      e = ERROR_NOT_ENOUGH_MEMORY;
236
      CloseHandle(h);
237
      return cmsys::Status::Windows(e);
238
    }
239
    if (!GetFileInformationByHandleEx(h, FileNameInfo, fni, fni_size)) {
240
      e = GetLastError();
241
      free(fni);
242
      CloseHandle(h);
243
      return cmsys::Status::Windows(e);
244
    }
245
  }
246
247
  std::wstring wn{ fni->FileName, fni->FileNameLength / sizeof(WCHAR) };
248
  std::string nn = cmsys::Encoding::ToNarrow(wn);
249
  std::string::size_type last_slash = nn.find_last_of("/\\");
250
  if (last_slash != std::string::npos) {
251
    name = nn.substr(last_slash + 1);
252
  }
253
  if (fni != reinterpret_cast<FILE_NAME_INFO*>(local_fni)) {
254
    free(fni);
255
  }
256
  CloseHandle(h);
257
  return cmsys::Status::Success();
258
}
259
#elif defined(__APPLE__)
260
cmsys::Status ReadNameOnDiskIterateDir(std::string const& path,
261
                                       std::string& name)
262
{
263
  // Read contents of the parent directory to find the
264
  // entry matching the given path.
265
  std::string const bn = cmSystemTools::GetFilenameName(path);
266
  std::string const dn = cmSystemTools::GetFilenamePath(path);
267
  DIR* d = opendir(dn.c_str());
268
  while (struct dirent* dr = readdir(d)) {
269
    if (strcasecmp(dr->d_name, bn.c_str()) == 0) {
270
      name = dr->d_name;
271
      closedir(d);
272
      return cmsys::Status::Success();
273
    }
274
  }
275
  closedir(d);
276
  return cmsys::Status::POSIX(ENOENT);
277
}
278
279
cmsys::Status ReadNameOnDiskFcntlGetPath(std::string const& path,
280
                                         std::string& name)
281
{
282
  // macOS (and *BSD) offer a syscall to get an on-disk path to
283
  // a descriptor's file.
284
  int fd = open(path.c_str(), O_SYMLINK | O_RDONLY);
285
  if (fd == -1) {
286
    return cmsys::Status::POSIX(errno);
287
  }
288
  char out[MAXPATHLEN + 1];
289
  if (fcntl(fd, F_GETPATH, out) == -1) {
290
    int e = errno;
291
    close(fd);
292
    return cmsys::Status::POSIX(e);
293
  }
294
  close(fd);
295
  name = cmSystemTools::GetFilenameName(out);
296
  return cmsys::Status::Success();
297
}
298
299
cmsys::Status ReadNameOnDisk(std::string const& path, std::string& name)
300
{
301
  struct stat stat_path;
302
  if (lstat(path.c_str(), &stat_path) != 0) {
303
    return cmsys::Status::POSIX(errno);
304
  }
305
  // macOS (and *BSD) use namei(9) to cache file paths.  Use it unless
306
  // the inode has multiple hardlinks: if it is opened through multiple
307
  // paths, the results may be unpredictable.
308
  if (S_ISDIR(stat_path.st_mode) || stat_path.st_nlink < 2) {
309
    return ReadNameOnDiskFcntlGetPath(path, name);
310
  }
311
  // Fall back to reading the parent directory.
312
  return ReadNameOnDiskIterateDir(path, name);
313
}
314
#endif
315
316
class RealSystem : public cm::PathResolver::System
317
{
318
public:
319
  ~RealSystem() override = default;
320
  cmsys::Status ReadSymlink(std::string const& path,
321
                            std::string& link) override
322
0
  {
323
0
    return cmSystemTools::ReadSymlink(path, link);
324
0
  }
325
  bool PathExists(std::string const& path) override
326
0
  {
327
0
    return cmSystemTools::PathExists(path);
328
0
  }
329
  std::string GetWorkingDirectory() override
330
0
  {
331
0
    return cmSystemTools::GetLogicalWorkingDirectory();
332
0
  }
333
#ifdef _WIN32
334
  std::string GetWorkingDirectoryOnDrive(char letter) override
335
  {
336
    return GetDosDriveWorkingDirectory(letter);
337
  }
338
#endif
339
340
#if defined(_WIN32) || defined(__APPLE__)
341
  struct NameOnDisk
342
  {
343
    cmsys::Status Status;
344
    std::string Name;
345
  };
346
  using NameOnDiskMap = std::unordered_map<std::string, NameOnDisk>;
347
  NameOnDiskMap CachedNameOnDisk;
348
349
  cmsys::Status ReadName(std::string const& path, std::string& name) override
350
  {
351
    // Cache results to avoid repeated filesystem access.
352
    // We assume any files created by our own process keep their case.
353
    // Index the cache by lower-case paths to make it case-insensitive.
354
    std::string path_lower = cmSystemTools::LowerCase(path);
355
    auto i = this->CachedNameOnDisk.find(path_lower);
356
    if (i == this->CachedNameOnDisk.end()) {
357
      i = this->CachedNameOnDisk.emplace(path_lower, NameOnDisk()).first;
358
      i->second.Status = ReadNameOnDisk(path, i->second.Name);
359
    }
360
    name = i->second.Name;
361
    return i->second.Status;
362
  }
363
#endif
364
};
365
366
RealSystem RealOS;
367
368
} // namespace
369
370
#if !defined(HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
371
// For GetEnvironmentVariables
372
#  if defined(_WIN32)
373
extern __declspec(dllimport) char** environ;
374
#  else
375
extern char** environ; // NOLINT(readability-redundant-declaration)
376
#  endif
377
#endif
378
379
#if !defined(CMAKE_BOOTSTRAP)
380
// Get path that was read from the archive.
381
static char const* cm_archive_entry_pathname(struct archive_entry* entry)
382
16.9k
{
383
#  ifdef _WIN32
384
  // libarchive converts the archive's encoding to our UTF-8 encoding.
385
  return archive_entry_pathname_utf8(entry);
386
#  else
387
  // libarchive converts the archive's encoding to our locale's encoding.
388
16.9k
  return archive_entry_pathname(entry);
389
16.9k
#  endif
390
16.9k
}
391
392
// Open archive file for reading.
393
static int cm_archive_read_open_filename(struct archive* a, char const* file,
394
                                         int block_size)
395
32.3k
{
396
#  ifdef _WIN32
397
  std::wstring wfile = cmsys::Encoding::ToWide(file);
398
  return archive_read_open_filename_w(a, wfile.c_str(), block_size);
399
#  else
400
32.3k
  return archive_read_open_filename(a, file, block_size);
401
32.3k
#  endif
402
32.3k
}
403
#endif
404
405
#ifdef _WIN32
406
#elif defined(__APPLE__)
407
#  include <crt_externs.h>
408
409
#  define environ (*_NSGetEnviron())
410
#endif
411
412
bool cmSystemTools::s_RunCommandHideConsole = false;
413
bool cmSystemTools::s_DisableRunCommandOutput = false;
414
bool cmSystemTools::s_ErrorOccurred = false;
415
bool cmSystemTools::s_FatalErrorOccurred = false;
416
bool cmSystemTools::s_ForceUnixPaths = false;
417
418
// replace replace with with as many times as it shows up in source.
419
// write the result into source.
420
#if defined(_WIN32) && !defined(__CYGWIN__)
421
void cmSystemTools::ExpandRegistryValues(std::string& source, KeyWOW64 view)
422
{
423
  // Regular expression to match anything inside [...] that begins in HKEY.
424
  // Note that there is a special rule for regular expressions to match a
425
  // close square-bracket inside a list delimited by square brackets.
426
  // The "[^]]" part of this expression will match any character except
427
  // a close square-bracket.  The ']' character must be the first in the
428
  // list of characters inside the [^...] block of the expression.
429
  cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
430
431
  // check for black line or comment
432
  while (regEntry.find(source)) {
433
    // the arguments are the second match
434
    std::string key = regEntry.match(1);
435
    std::string val;
436
    if (ReadRegistryValue(key.c_str(), val, view)) {
437
      std::string reg = cmStrCat('[', key, ']');
438
      cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
439
    } else {
440
      std::string reg = cmStrCat('[', key, ']');
441
      cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
442
    }
443
  }
444
}
445
#else
446
void cmSystemTools::ExpandRegistryValues(std::string& source,
447
                                         KeyWOW64 /*unused*/)
448
0
{
449
0
  cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
450
0
  while (regEntry.find(source)) {
451
    // the arguments are the second match
452
0
    std::string key = regEntry.match(1);
453
0
    std::string reg = cmStrCat('[', key, ']');
454
0
    cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
455
0
  }
456
0
}
457
#endif
458
459
std::string cmSystemTools::HelpFileName(cm::string_view str)
460
0
{
461
0
  std::string name(str);
462
0
  cmSystemTools::ReplaceString(name, "<", "");
463
0
  cmSystemTools::ReplaceString(name, ">", "");
464
0
  return name;
465
0
}
466
467
void cmSystemTools::Error(std::string const& m)
468
28.1k
{
469
28.1k
  std::string message = "CMake Error: " + m;
470
28.1k
  cmSystemTools::s_ErrorOccurred = true;
471
28.1k
  cmSystemTools::Message(message, "Error");
472
28.1k
}
473
474
void cmSystemTools::SetInterruptCallback(InterruptCallback f)
475
0
{
476
0
  s_InterruptCallback = std::move(f);
477
0
}
478
479
bool cmSystemTools::GetInterruptFlag()
480
1
{
481
1
  if (s_InterruptCallback) {
482
0
    return s_InterruptCallback();
483
0
  }
484
1
  return false;
485
1
}
486
487
void cmSystemTools::SetMessageCallback(MessageCallback f)
488
2
{
489
2
  s_MessageCallback = std::move(f);
490
2
}
491
492
void cmSystemTools::SetStdoutCallback(OutputCallback f)
493
2
{
494
2
  s_StdoutCallback = std::move(f);
495
2
}
496
497
void cmSystemTools::SetStderrCallback(OutputCallback f)
498
2
{
499
2
  s_StderrCallback = std::move(f);
500
2
}
501
502
void cmSystemTools::Stderr(std::string const& s)
503
0
{
504
0
  if (s_StderrCallback) {
505
0
    s_StderrCallback(s);
506
0
  } else {
507
0
    std::cerr << s << std::flush;
508
0
  }
509
0
}
510
511
void cmSystemTools::Stdout(std::string const& s)
512
31.4k
{
513
31.4k
  if (s_StdoutCallback) {
514
0
    s_StdoutCallback(s);
515
31.4k
  } else {
516
31.4k
    std::cout << s << std::flush;
517
31.4k
  }
518
31.4k
}
519
520
void cmSystemTools::Message(std::string const& m, char const* title)
521
28.1k
{
522
28.1k
  cmMessageMetadata md;
523
28.1k
  md.title = title;
524
28.1k
  Message(m, md);
525
28.1k
}
526
527
void cmSystemTools::Message(std::string const& m, cmMessageMetadata const& md)
528
28.1k
{
529
28.1k
  if (s_MessageCallback) {
530
3
    s_MessageCallback(m, md);
531
28.1k
  } else {
532
28.1k
    std::cerr << m << std::endl;
533
28.1k
  }
534
28.1k
}
535
536
void cmSystemTools::ReportLastSystemError(char const* msg)
537
0
{
538
0
  std::string m =
539
0
    cmStrCat(msg, ": System Error: ", Superclass::GetLastSystemError());
540
0
  cmSystemTools::Error(m);
541
0
}
542
543
void cmSystemTools::ParseWindowsCommandLine(char const* command,
544
                                            std::vector<std::string>& args)
545
0
{
546
  // See the MSDN document "Parsing C Command-Line Arguments" at
547
  // http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx for rules
548
  // of parsing the windows command line.
549
550
0
  bool in_argument = false;
551
0
  bool in_quotes = false;
552
0
  int backslashes = 0;
553
0
  std::string arg;
554
0
  for (char const* c = command; *c; ++c) {
555
0
    if (*c == '\\') {
556
0
      ++backslashes;
557
0
      in_argument = true;
558
0
    } else if (*c == '"') {
559
0
      int backslash_pairs = backslashes >> 1;
560
0
      int backslash_escaped = backslashes & 1;
561
0
      arg.append(backslash_pairs, '\\');
562
0
      backslashes = 0;
563
0
      if (backslash_escaped) {
564
        /* An odd number of backslashes precede this quote.
565
           It is escaped.  */
566
0
        arg.append(1, '"');
567
0
      } else {
568
        /* An even number of backslashes precede this quote.
569
           It is not escaped.  */
570
0
        in_quotes = !in_quotes;
571
0
      }
572
0
      in_argument = true;
573
0
    } else {
574
0
      arg.append(backslashes, '\\');
575
0
      backslashes = 0;
576
0
      if (cmsysString_isspace(*c)) {
577
0
        if (in_quotes) {
578
0
          arg.append(1, *c);
579
0
        } else if (in_argument) {
580
0
          args.push_back(arg);
581
0
          arg.clear();
582
0
          in_argument = false;
583
0
        }
584
0
      } else {
585
0
        in_argument = true;
586
0
        arg.append(1, *c);
587
0
      }
588
0
    }
589
0
  }
590
0
  arg.append(backslashes, '\\');
591
0
  if (in_argument) {
592
0
    args.push_back(arg);
593
0
  }
594
0
}
595
596
class cmSystemToolsArgV
597
{
598
  char** ArgV;
599
600
public:
601
  cmSystemToolsArgV(char** argv)
602
0
    : ArgV(argv)
603
0
  {
604
0
  }
605
  ~cmSystemToolsArgV()
606
0
  {
607
0
    for (char** arg = this->ArgV; arg && *arg; ++arg) {
608
0
      free(*arg);
609
0
    }
610
0
    free(this->ArgV);
611
0
  }
612
  cmSystemToolsArgV(cmSystemToolsArgV const&) = delete;
613
  cmSystemToolsArgV& operator=(cmSystemToolsArgV const&) = delete;
614
  void Store(std::vector<std::string>& args) const
615
0
  {
616
0
    for (char** arg = this->ArgV; arg && *arg; ++arg) {
617
0
      args.emplace_back(*arg);
618
0
    }
619
0
  }
620
};
621
622
void cmSystemTools::ParseUnixCommandLine(char const* command,
623
                                         std::vector<std::string>& args)
624
0
{
625
  // Invoke the underlying parser.
626
0
  cmSystemToolsArgV argv(cmsysSystem_Parse_CommandForUnix(command, 0));
627
0
  argv.Store(args);
628
0
}
629
630
std::vector<std::string> cmSystemTools::HandleResponseFile(
631
  std::vector<std::string>::const_iterator argBeg,
632
  std::vector<std::string>::const_iterator argEnd)
633
0
{
634
0
  std::vector<std::string> arg_full;
635
0
  for (std::string const& arg : cmMakeRange(argBeg, argEnd)) {
636
0
    if (cmHasPrefix(arg, '@')) {
637
0
      cmsys::ifstream responseFile(arg.substr(1).c_str(), std::ios::in);
638
0
      if (!responseFile) {
639
0
        std::string error = cmStrCat("failed to open for reading (",
640
0
                                     cmSystemTools::GetLastSystemError(),
641
0
                                     "):\n  ", cm::string_view(arg).substr(1));
642
0
        cmSystemTools::Error(error);
643
0
      } else {
644
0
        std::string line;
645
0
        cmSystemTools::GetLineFromStream(responseFile, line);
646
0
        std::vector<std::string> args2;
647
#ifdef _WIN32
648
        cmSystemTools::ParseWindowsCommandLine(line.c_str(), args2);
649
#else
650
0
        cmSystemTools::ParseUnixCommandLine(line.c_str(), args2);
651
0
#endif
652
0
        cm::append(arg_full, args2);
653
0
      }
654
0
    } else {
655
0
      arg_full.push_back(arg);
656
0
    }
657
0
  }
658
0
  return arg_full;
659
0
}
660
661
std::vector<std::string> cmSystemTools::ParseArguments(std::string const& cmd)
662
0
{
663
0
  std::vector<std::string> args;
664
0
  std::string arg;
665
666
0
  bool win_path = false;
667
668
0
  char const* command = cmd.c_str();
669
0
  if (command[0] && command[1] &&
670
0
      ((command[0] != '/' && command[1] == ':' && command[2] == '\\') ||
671
0
       (command[0] == '\"' && command[1] != '/' && command[2] == ':' &&
672
0
        command[3] == '\\') ||
673
0
       (command[0] == '\'' && command[1] != '/' && command[2] == ':' &&
674
0
        command[3] == '\\') ||
675
0
       (command[0] == '\\' && command[1] == '\\'))) {
676
0
    win_path = true;
677
0
  }
678
  // Split the command into an argv array.
679
0
  for (char const* c = command; *c;) {
680
    // Skip over whitespace.
681
0
    while (*c == ' ' || *c == '\t') {
682
0
      ++c;
683
0
    }
684
0
    arg.clear();
685
0
    if (*c == '"') {
686
      // Parse a quoted argument.
687
0
      ++c;
688
0
      while (*c && *c != '"') {
689
0
        arg.append(1, *c);
690
0
        ++c;
691
0
      }
692
0
      if (*c) {
693
0
        ++c;
694
0
      }
695
0
      args.push_back(arg);
696
0
    } else if (*c == '\'') {
697
      // Parse a quoted argument.
698
0
      ++c;
699
0
      while (*c && *c != '\'') {
700
0
        arg.append(1, *c);
701
0
        ++c;
702
0
      }
703
0
      if (*c) {
704
0
        ++c;
705
0
      }
706
0
      args.push_back(arg);
707
0
    } else if (*c) {
708
      // Parse an unquoted argument.
709
0
      while (*c && *c != ' ' && *c != '\t') {
710
0
        if (*c == '\\' && !win_path) {
711
0
          ++c;
712
0
          if (*c) {
713
0
            arg.append(1, *c);
714
0
            ++c;
715
0
          }
716
0
        } else {
717
0
          arg.append(1, *c);
718
0
          ++c;
719
0
        }
720
0
      }
721
0
      args.push_back(arg);
722
0
    }
723
0
  }
724
725
0
  return args;
726
0
}
727
728
bool cmSystemTools::SplitProgramFromArgs(std::string const& command,
729
                                         std::string& program,
730
                                         std::string& args)
731
0
{
732
0
  char const* c = command.c_str();
733
734
  // Skip leading whitespace.
735
0
  while (cmsysString_isspace(*c)) {
736
0
    ++c;
737
0
  }
738
739
  // Parse one command-line element up to an unquoted space.
740
0
  bool in_escape = false;
741
0
  bool in_double = false;
742
0
  bool in_single = false;
743
0
  for (; *c; ++c) {
744
0
    if (in_single) {
745
0
      if (*c == '\'') {
746
0
        in_single = false;
747
0
      } else {
748
0
        program += *c;
749
0
      }
750
0
    } else if (in_escape) {
751
0
      in_escape = false;
752
0
      program += *c;
753
0
    } else if (*c == '\\') {
754
0
      in_escape = true;
755
0
    } else if (in_double) {
756
0
      if (*c == '"') {
757
0
        in_double = false;
758
0
      } else {
759
0
        program += *c;
760
0
      }
761
0
    } else if (*c == '"') {
762
0
      in_double = true;
763
0
    } else if (*c == '\'') {
764
0
      in_single = true;
765
0
    } else if (cmsysString_isspace(*c)) {
766
0
      break;
767
0
    } else {
768
0
      program += *c;
769
0
    }
770
0
  }
771
772
  // The remainder of the command line holds unparsed arguments.
773
0
  args = c;
774
775
0
  return !in_single && !in_escape && !in_double;
776
0
}
777
778
std::size_t cmSystemTools::CalculateCommandLineLengthLimit()
779
0
{
780
0
  size_t sz =
781
#ifdef _WIN32
782
    // There's a maximum of 65536 bytes and thus 32768 WCHARs on Windows
783
    // However, cmd.exe itself can only handle 8191 WCHARs and Ninja for
784
    // example uses it to spawn processes.
785
    size_t(8191);
786
#elif defined(__linux)
787
    // MAX_ARG_STRLEN is the maximum length of a string permissible for
788
    // the execve() syscall on Linux. It's defined as (PAGE_SIZE * 32)
789
    // in Linux's binfmts.h
790
0
    static_cast<size_t>(sysconf(_SC_PAGESIZE) * 32);
791
#else
792
    size_t(0);
793
#endif
794
795
0
#if defined(_SC_ARG_MAX)
796
  // ARG_MAX is the maximum size of the command and environment
797
  // that can be passed to the exec functions on UNIX.
798
  // The value in climits does not need to be present and may
799
  // depend upon runtime memory constraints, hence sysconf()
800
  // should be used to query it.
801
0
  long szArgMax = sysconf(_SC_ARG_MAX);
802
  // A return value of -1 signifies an undetermined limit, but
803
  // it does not imply an infinite limit, and thus is ignored.
804
0
  if (szArgMax != -1) {
805
    // We estimate the size of the environment block to be 1000.
806
    // This isn't accurate at all, but leaves some headroom.
807
0
    szArgMax = szArgMax < 1000 ? 0 : szArgMax - 1000;
808
0
#  if defined(_WIN32) || defined(__linux)
809
0
    sz = std::min(sz, static_cast<size_t>(szArgMax));
810
#  else
811
    sz = static_cast<size_t>(szArgMax);
812
#  endif
813
0
  }
814
0
#endif
815
0
  return sz;
816
0
}
817
818
void cmSystemTools::MaybePrependCmdExe(std::vector<std::string>& cmdLine)
819
0
{
820
#if defined(_WIN32) && !defined(__CYGWIN__)
821
  if (!cmdLine.empty()) {
822
    std::string& applicationName = cmdLine.at(0);
823
    static cmsys::RegularExpression const winCmdRegex(
824
      "\\.([Bb][Aa][Tt]|[Cc][Mm][Dd])$");
825
    cmsys::RegularExpressionMatch winCmdMatch;
826
    if (winCmdRegex.find(applicationName.c_str(), winCmdMatch)) {
827
      // Wrap `.bat` and `.cmd` commands with `cmd /c call`.
828
      std::vector<std::string> output;
829
      output.reserve(cmdLine.size() + 3);
830
      output.emplace_back(cmSystemTools::GetComspec());
831
      output.emplace_back("/c");
832
      output.emplace_back("call");
833
      // Convert the batch file path to use backslashes for cmd.exe to parse.
834
      std::replace(applicationName.begin(), applicationName.end(), '/', '\\');
835
      output.emplace_back(applicationName);
836
      std::move(cmdLine.begin() + 1, cmdLine.end(),
837
                std::back_inserter(output));
838
      cmdLine = std::move(output);
839
    }
840
  }
841
#else
842
0
  static_cast<void>(cmdLine);
843
0
#endif
844
0
}
845
846
bool cmSystemTools::RunSingleCommand(std::vector<std::string> const& command,
847
                                     std::string* captureStdOut,
848
                                     std::string* captureStdErr, int* retVal,
849
                                     char const* dir, OutputOption outputflag,
850
                                     cmDuration timeout, Encoding encoding,
851
                                     std::vector<std::string> env)
852
0
{
853
0
  cmUVProcessChainBuilder builder;
854
0
  builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, stdin)
855
0
    .SetEnvironment(std::move(env))
856
0
    .AddCommand(command);
857
0
  if (dir) {
858
0
    builder.SetWorkingDirectory(dir);
859
0
  }
860
861
0
  if (outputflag == OUTPUT_PASSTHROUGH) {
862
0
    captureStdOut = nullptr;
863
0
    captureStdErr = nullptr;
864
0
    builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout)
865
0
      .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr);
866
0
  } else if (outputflag == OUTPUT_MERGE ||
867
0
             (captureStdErr && captureStdErr == captureStdOut)) {
868
0
    builder.SetMergedBuiltinStreams();
869
0
    captureStdErr = nullptr;
870
0
  } else {
871
0
    builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
872
0
      .SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
873
0
  }
874
0
  assert(!captureStdErr || captureStdErr != captureStdOut);
875
876
0
  auto chain = builder.Start();
877
0
  bool timedOut = false;
878
0
  cm::uv_timer_ptr timer;
879
0
  if (timeout.count()) {
880
0
    timer.init(chain.GetLoop(), &timedOut);
881
0
    timer.start(
882
0
      [](uv_timer_t* t) {
883
0
        auto* timedOutPtr = static_cast<bool*>(t->data);
884
0
        *timedOutPtr = true;
885
0
      },
886
0
      static_cast<uint64_t>(timeout.count() * 1000.0), 0,
887
0
      cm::uv_update_time::yes);
888
0
  }
889
890
0
  std::vector<char> tempStdOut;
891
0
  std::vector<char> tempStdErr;
892
0
  bool outFinished = true;
893
0
  bool errFinished = true;
894
0
  cmProcessOutput processOutput(encoding);
895
0
  std::unique_ptr<cmUVStreamReadHandle> outputHandle;
896
0
  std::unique_ptr<cmUVStreamReadHandle> errorHandle;
897
0
  if (outputflag != OUTPUT_PASSTHROUGH &&
898
0
      (captureStdOut || captureStdErr || outputflag != OUTPUT_NONE)) {
899
0
    auto startRead =
900
0
      [&outputflag, &processOutput](
901
0
        uv_stream_t* stream, std::string* captureStd,
902
0
        std::vector<char>& tempStd, int id,
903
0
        void (*outputFunc)(std::string const&),
904
0
        bool& finished) -> std::unique_ptr<cmUVStreamReadHandle> {
905
0
      finished = false;
906
0
      return cmUVStreamRead(
907
0
        stream,
908
0
        [outputflag, &processOutput, captureStd, &tempStd, id,
909
0
         outputFunc](std::vector<char> data) {
910
          // Translate NULL characters in the output into valid text.
911
0
          for (auto& c : data) {
912
0
            if (c == '\0') {
913
0
              c = ' ';
914
0
            }
915
0
          }
916
917
0
          if (outputflag != OUTPUT_NONE) {
918
0
            std::string strdata;
919
0
            processOutput.DecodeText(data.data(), data.size(), strdata, id);
920
0
            outputFunc(strdata);
921
0
          }
922
0
          if (captureStd) {
923
0
            cm::append(tempStd, data.data(), data.data() + data.size());
924
0
          }
925
0
        },
926
0
        [&finished, outputflag, &processOutput, id, outputFunc]() {
927
0
          finished = true;
928
0
          if (outputflag != OUTPUT_NONE) {
929
0
            std::string strdata;
930
0
            processOutput.DecodeText(std::string(), strdata, id);
931
0
            if (!strdata.empty()) {
932
0
              outputFunc(strdata);
933
0
            }
934
0
          }
935
0
        });
936
0
    };
937
938
0
    outputHandle = startRead(chain.OutputStream(), captureStdOut, tempStdOut,
939
0
                             1, cmSystemTools::Stdout, outFinished);
940
0
    if (chain.ErrorStream()) {
941
0
      errorHandle = startRead(chain.ErrorStream(), captureStdErr, tempStdErr,
942
0
                              2, cmSystemTools::Stderr, errFinished);
943
0
    }
944
0
  }
945
946
0
  while (!timedOut && !(chain.Finished() && outFinished && errFinished)) {
947
0
    uv_run(&chain.GetLoop(), UV_RUN_ONCE);
948
0
  }
949
950
0
  if (captureStdOut) {
951
0
    captureStdOut->assign(tempStdOut.begin(), tempStdOut.end());
952
0
    processOutput.DecodeText(*captureStdOut, *captureStdOut);
953
0
  }
954
0
  if (captureStdErr) {
955
0
    captureStdErr->assign(tempStdErr.begin(), tempStdErr.end());
956
0
    processOutput.DecodeText(*captureStdErr, *captureStdErr);
957
0
  }
958
959
0
  bool result = true;
960
0
  if (timedOut) {
961
0
    chain.Terminate();
962
0
    char const* error_str = "Process terminated due to timeout\n";
963
0
    if (outputflag != OUTPUT_NONE) {
964
0
      std::cerr << error_str << std::endl;
965
0
    }
966
0
    if (captureStdErr) {
967
0
      captureStdErr->append(error_str, strlen(error_str));
968
0
    }
969
0
    result = false;
970
0
  } else {
971
0
    auto const& status = chain.GetStatus(0);
972
0
    auto exception = status.GetException();
973
974
0
    switch (exception.first) {
975
0
      case cmUVProcessChain::ExceptionCode::None:
976
0
        if (retVal) {
977
0
          *retVal = static_cast<int>(status.ExitStatus);
978
0
        } else {
979
0
          if (status.ExitStatus != 0) {
980
0
            result = false;
981
0
          }
982
0
        }
983
0
        break;
984
0
      default: {
985
0
        if (outputflag != OUTPUT_NONE) {
986
0
          std::cerr << exception.second << std::endl;
987
0
        }
988
0
        if (captureStdErr) {
989
0
          captureStdErr->append(exception.second);
990
0
        } else if (captureStdOut) {
991
0
          captureStdOut->append(exception.second);
992
0
        }
993
0
        result = false;
994
0
      } break;
995
0
    }
996
0
  }
997
998
0
  return result;
999
0
}
1000
1001
bool cmSystemTools::RunSingleCommand(std::string const& command,
1002
                                     std::string* captureStdOut,
1003
                                     std::string* captureStdErr, int* retVal,
1004
                                     char const* dir, OutputOption outputflag,
1005
                                     cmDuration timeout)
1006
0
{
1007
0
  if (s_DisableRunCommandOutput) {
1008
0
    outputflag = OUTPUT_NONE;
1009
0
  }
1010
1011
0
  std::vector<std::string> args = cmSystemTools::ParseArguments(command);
1012
1013
0
  if (args.empty()) {
1014
0
    return false;
1015
0
  }
1016
0
  return cmSystemTools::RunSingleCommand(args, captureStdOut, captureStdErr,
1017
0
                                         retVal, dir, outputflag, timeout);
1018
0
}
1019
1020
std::string cmSystemTools::PrintSingleCommand(
1021
  std::vector<std::string> const& command)
1022
0
{
1023
0
  if (command.empty()) {
1024
0
    return std::string();
1025
0
  }
1026
1027
0
  return cmWrap('"', command, '"', " ");
1028
0
}
1029
1030
bool cmSystemTools::DoesFileExistWithExtensions(
1031
  std::string const& name, std::vector<std::string> const& headerExts)
1032
0
{
1033
0
  std::string hname;
1034
1035
0
  for (std::string const& headerExt : headerExts) {
1036
0
    hname = cmStrCat(name, '.', headerExt);
1037
0
    if (cmSystemTools::FileExists(hname)) {
1038
0
      return true;
1039
0
    }
1040
0
  }
1041
0
  return false;
1042
0
}
1043
1044
std::string cmSystemTools::FileExistsInParentDirectories(
1045
  std::string const& fname, std::string const& directory,
1046
  std::string const& toplevel)
1047
0
{
1048
0
  std::string file = fname;
1049
0
  cmSystemTools::ConvertToUnixSlashes(file);
1050
0
  std::string dir = directory;
1051
0
  cmSystemTools::ConvertToUnixSlashes(dir);
1052
0
  std::string prevDir;
1053
0
  while (dir != prevDir) {
1054
0
    std::string path = cmStrCat(dir, '/', file);
1055
0
    if (cmSystemTools::FileExists(path)) {
1056
0
      return path;
1057
0
    }
1058
0
    if (dir.size() < toplevel.size()) {
1059
0
      break;
1060
0
    }
1061
0
    prevDir = dir;
1062
0
    dir = cmSystemTools::GetParentDirectory(dir);
1063
0
  }
1064
0
  return "";
1065
0
}
1066
1067
#ifdef _WIN32
1068
namespace {
1069
1070
/* Helper class to save and restore the specified file (or directory)
1071
   attribute bits. Instantiate this class as an automatic variable on the
1072
   stack. Its constructor saves a copy of the file attributes, and then its
1073
   destructor restores the original attribute settings.  */
1074
class SaveRestoreFileAttributes
1075
{
1076
public:
1077
  SaveRestoreFileAttributes(std::wstring const& path,
1078
                            uint32_t file_attrs_to_set);
1079
  ~SaveRestoreFileAttributes();
1080
1081
  SaveRestoreFileAttributes(SaveRestoreFileAttributes const&) = delete;
1082
  SaveRestoreFileAttributes& operator=(SaveRestoreFileAttributes const&) =
1083
    delete;
1084
1085
  void SetPath(std::wstring const& path) { path_ = path; }
1086
1087
private:
1088
  std::wstring path_;
1089
  uint32_t original_attr_bits_;
1090
};
1091
1092
SaveRestoreFileAttributes::SaveRestoreFileAttributes(
1093
  std::wstring const& path, uint32_t file_attrs_to_set)
1094
  : path_(path)
1095
  , original_attr_bits_(0)
1096
{
1097
  // Set the specified attributes for the source file/directory.
1098
  original_attr_bits_ = GetFileAttributesW(path_.c_str());
1099
  if ((INVALID_FILE_ATTRIBUTES != original_attr_bits_) &&
1100
      ((file_attrs_to_set & original_attr_bits_) != file_attrs_to_set)) {
1101
    SetFileAttributesW(path_.c_str(), original_attr_bits_ | file_attrs_to_set);
1102
  }
1103
}
1104
1105
// We set attribute bits.  Now we need to restore their original state.
1106
SaveRestoreFileAttributes::~SaveRestoreFileAttributes()
1107
{
1108
  DWORD last_error = GetLastError();
1109
  // Verify or restore the original attributes.
1110
  const DWORD source_attr_bits = GetFileAttributesW(path_.c_str());
1111
  if (INVALID_FILE_ATTRIBUTES != source_attr_bits) {
1112
    if (original_attr_bits_ != source_attr_bits) {
1113
      // The file still exists, and its attributes aren't our saved values.
1114
      // Time to restore them.
1115
      SetFileAttributesW(path_.c_str(), original_attr_bits_);
1116
    }
1117
  }
1118
  SetLastError(last_error);
1119
}
1120
1121
struct WindowsFileRetryInit
1122
{
1123
  cmSystemTools::WindowsFileRetry Retry;
1124
  bool Explicit;
1125
};
1126
1127
WindowsFileRetryInit InitWindowsFileRetry(wchar_t const* const values[2],
1128
                                          unsigned int const defaults[2])
1129
{
1130
  unsigned int data[2] = { 0, 0 };
1131
  HKEY const keys[2] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
1132
  for (int k = 0; k < 2; ++k) {
1133
    HKEY hKey;
1134
    if (RegOpenKeyExW(keys[k], L"Software\\Kitware\\CMake\\Config", 0,
1135
                      KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
1136
      for (int v = 0; v < 2; ++v) {
1137
        DWORD dwData, dwType, dwSize = 4;
1138
        if (!data[v] &&
1139
            RegQueryValueExW(hKey, values[v], 0, &dwType, (BYTE*)&dwData,
1140
                             &dwSize) == ERROR_SUCCESS &&
1141
            dwType == REG_DWORD && dwSize == 4) {
1142
          data[v] = static_cast<unsigned int>(dwData);
1143
        }
1144
      }
1145
      RegCloseKey(hKey);
1146
    }
1147
  }
1148
  WindowsFileRetryInit init;
1149
  init.Explicit = data[0] || data[1];
1150
  init.Retry.Count = data[0] ? data[0] : defaults[0];
1151
  init.Retry.Delay = data[1] ? data[1] : defaults[1];
1152
  return init;
1153
}
1154
1155
WindowsFileRetryInit InitWindowsFileRetry()
1156
{
1157
  static wchar_t const* const values[2] = { L"FilesystemRetryCount",
1158
                                            L"FilesystemRetryDelay" };
1159
  static unsigned int const defaults[2] = { 5, 500 };
1160
  return InitWindowsFileRetry(values, defaults);
1161
}
1162
1163
WindowsFileRetryInit InitWindowsDirectoryRetry()
1164
{
1165
  static wchar_t const* const values[2] = { L"FilesystemDirectoryRetryCount",
1166
                                            L"FilesystemDirectoryRetryDelay" };
1167
  static unsigned int const defaults[2] = { 120, 500 };
1168
  WindowsFileRetryInit dirInit = InitWindowsFileRetry(values, defaults);
1169
  if (dirInit.Explicit) {
1170
    return dirInit;
1171
  }
1172
  WindowsFileRetryInit fileInit = InitWindowsFileRetry();
1173
  if (fileInit.Explicit) {
1174
    return fileInit;
1175
  }
1176
  return dirInit;
1177
}
1178
1179
cmSystemTools::WindowsFileRetry GetWindowsRetry(std::wstring const& path)
1180
{
1181
  // If we are performing a directory operation, then try and get the
1182
  // appropriate timing info.
1183
  DWORD const attrs = GetFileAttributesW(path.c_str());
1184
  if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
1185
    return cmSystemTools::GetWindowsDirectoryRetry();
1186
  }
1187
  return cmSystemTools::GetWindowsFileRetry();
1188
}
1189
1190
} // end of anonymous namespace
1191
1192
cmSystemTools::WindowsFileRetry cmSystemTools::GetWindowsFileRetry()
1193
{
1194
  static WindowsFileRetry retry = InitWindowsFileRetry().Retry;
1195
  return retry;
1196
}
1197
1198
cmSystemTools::WindowsFileRetry cmSystemTools::GetWindowsDirectoryRetry()
1199
{
1200
  static cmSystemTools::WindowsFileRetry retry =
1201
    InitWindowsDirectoryRetry().Retry;
1202
  return retry;
1203
}
1204
1205
cmSystemTools::WindowsVersion cmSystemTools::GetWindowsVersion()
1206
{
1207
  /* Windows version number data.  */
1208
  OSVERSIONINFOEXW osviex;
1209
  ZeroMemory(&osviex, sizeof(osviex));
1210
  osviex.dwOSVersionInfoSize = sizeof(osviex);
1211
1212
#  ifdef CM_WINDOWS_DEPRECATED_GetVersionEx
1213
#    pragma warning(push)
1214
#    ifdef __INTEL_COMPILER
1215
#      pragma warning(disable : 1478)
1216
#    elif defined __clang__
1217
#      pragma clang diagnostic push
1218
#      pragma clang diagnostic ignored "-Wdeprecated-declarations"
1219
#    else
1220
#      pragma warning(disable : 4996)
1221
#    endif
1222
#  endif
1223
  GetVersionExW((OSVERSIONINFOW*)&osviex);
1224
#  ifdef CM_WINDOWS_DEPRECATED_GetVersionEx
1225
#    ifdef __clang__
1226
#      pragma clang diagnostic pop
1227
#    else
1228
#      pragma warning(pop)
1229
#    endif
1230
#  endif
1231
1232
  WindowsVersion result;
1233
  result.dwMajorVersion = osviex.dwMajorVersion;
1234
  result.dwMinorVersion = osviex.dwMinorVersion;
1235
  result.dwBuildNumber = osviex.dwBuildNumber;
1236
  return result;
1237
}
1238
1239
std::string cmSystemTools::GetComspec()
1240
{
1241
  std::string comspec;
1242
  if (!cmSystemTools::GetEnv("COMSPEC", comspec) ||
1243
      !cmSystemTools::FileIsFullPath(comspec)) {
1244
    comspec = "cmd.exe";
1245
  }
1246
  return comspec;
1247
}
1248
1249
#endif
1250
1251
// File changes involve removing SETUID/SETGID bits when a file is modified.
1252
// This behavior is consistent across most Unix-like operating systems.
1253
class FileModeGuard
1254
{
1255
public:
1256
  FileModeGuard(std::string const& file_path, std::string* emsg);
1257
  bool Restore(std::string* emsg);
1258
  bool HasErrors() const;
1259
1260
private:
1261
#ifndef _WIN32
1262
  mode_t mode_;
1263
#endif
1264
  std::string filepath_;
1265
};
1266
1267
FileModeGuard::FileModeGuard(std::string const& file_path, std::string* emsg)
1268
0
{
1269
0
#ifndef _WIN32
1270
0
  struct stat file_stat;
1271
0
  if (stat(file_path.c_str(), &file_stat) != 0) {
1272
0
    if (emsg) {
1273
0
      *emsg = cmStrCat("Cannot get file stat: ", strerror(errno));
1274
0
    }
1275
0
    return;
1276
0
  }
1277
1278
0
  mode_ = file_stat.st_mode;
1279
#else
1280
  static_cast<void>(emsg);
1281
#endif
1282
0
  filepath_ = file_path;
1283
0
}
1284
1285
bool FileModeGuard::Restore(std::string* emsg)
1286
0
{
1287
0
  assert(filepath_.empty() == false);
1288
1289
0
#ifndef _WIN32
1290
0
  struct stat file_stat;
1291
0
  if (stat(filepath_.c_str(), &file_stat) != 0) {
1292
0
    if (emsg) {
1293
0
      *emsg = cmStrCat("Cannot get file stat: ", strerror(errno));
1294
0
    }
1295
0
    return false;
1296
0
  }
1297
1298
  // Nothing changed; everything is in the expected state
1299
0
  if (file_stat.st_mode == mode_) {
1300
0
    return true;
1301
0
  }
1302
1303
0
  if (chmod(filepath_.c_str(), mode_) != 0) {
1304
0
    if (emsg) {
1305
0
      *emsg = cmStrCat("Cannot restore the file mode: ", strerror(errno));
1306
0
    }
1307
0
    return false;
1308
0
  }
1309
#else
1310
  static_cast<void>(emsg);
1311
#endif
1312
1313
0
  return true;
1314
0
}
1315
1316
bool FileModeGuard::HasErrors() const
1317
0
{
1318
0
  return filepath_.empty();
1319
0
}
1320
1321
std::string cmSystemTools::GetRealPathResolvingWindowsSubst(
1322
  std::string const& path, std::string* errorMessage)
1323
0
{
1324
#ifdef _WIN32
1325
  // uv_fs_realpath uses Windows Vista API so fallback to kwsys if not found
1326
  std::string resolved_path;
1327
  uv_fs_t req;
1328
  int err = uv_fs_realpath(nullptr, &req, path.c_str(), nullptr);
1329
  if (!err) {
1330
    resolved_path = std::string((char*)req.ptr);
1331
    cmSystemTools::ConvertToUnixSlashes(resolved_path);
1332
  } else if (err == UV_ENOSYS) {
1333
    resolved_path = cmsys::SystemTools::GetRealPath(path, errorMessage);
1334
  } else if (errorMessage) {
1335
    cmsys::Status status =
1336
      cmsys::Status::Windows(uv_fs_get_system_error(&req));
1337
    *errorMessage = status.GetString();
1338
    resolved_path.clear();
1339
  } else {
1340
    resolved_path = path;
1341
  }
1342
  // Normalize to upper-case drive letter as cm::PathResolver does.
1343
  if (resolved_path.size() > 1 && resolved_path[1] == ':') {
1344
    resolved_path[0] = cmsysString_toupper(resolved_path[0]);
1345
  }
1346
  return resolved_path;
1347
#else
1348
0
  return cmsys::SystemTools::GetRealPath(path, errorMessage);
1349
0
#endif
1350
0
}
1351
1352
std::string cmSystemTools::GetRealPath(std::string const& path,
1353
                                       std::string* errorMessage)
1354
10
{
1355
#ifdef _WIN32
1356
  std::string resolved_path =
1357
    cmSystemTools::GetRealPathResolvingWindowsSubst(path, errorMessage);
1358
1359
  // If the original path used a subst drive and the real path starts
1360
  // with the substitution, restore the subst drive prefix.  This may
1361
  // incorrectly restore a subst drive if the underlying drive was
1362
  // encountered via an absolute symlink, but this is an acceptable
1363
  // limitation to otherwise preserve susbt drives.
1364
  if (resolved_path.size() >= 2 && resolved_path[1] == ':' &&
1365
      path.size() >= 2 && path[1] == ':' &&
1366
      cmsysString_toupper(resolved_path[0]) != cmsysString_toupper(path[0])) {
1367
    // FIXME: Add thread_local or mutex if we use threads.
1368
    static std::map<char, std::string> substMap;
1369
    char const drive = static_cast<char>(cmsysString_toupper(path[0]));
1370
    std::string maybe_subst = cmStrCat(drive, ":/");
1371
    auto smi = substMap.find(drive);
1372
    if (smi == substMap.end()) {
1373
      smi = substMap
1374
              .emplace(
1375
                drive,
1376
                cmSystemTools::GetRealPathResolvingWindowsSubst(maybe_subst))
1377
              .first;
1378
    }
1379
    std::string const& resolved_subst = smi->second;
1380
    std::string::size_type const ns = resolved_subst.size();
1381
    if (ns > 0) {
1382
      std::string::size_type const np = resolved_path.size();
1383
      if (ns == np && resolved_path == resolved_subst) {
1384
        resolved_path = maybe_subst;
1385
      } else if (ns > 0 && ns < np && resolved_path[ns] == '/' &&
1386
                 resolved_path.compare(0, ns, resolved_subst) == 0) {
1387
        resolved_path.replace(0, ns + 1, maybe_subst);
1388
      }
1389
    }
1390
  }
1391
1392
  return resolved_path;
1393
#else
1394
10
  return cmsys::SystemTools::GetRealPath(path, errorMessage);
1395
10
#endif
1396
10
}
1397
1398
void cmSystemTools::InitializeLibUV()
1399
0
{
1400
#if defined(_WIN32)
1401
  // Perform libuv one-time initialization now, and then un-do its
1402
  // global _fmode setting so that using libuv does not change the
1403
  // default file text/binary mode.  See libuv issue 840.
1404
  if (uv_loop_t* loop = uv_default_loop()) {
1405
    uv_loop_close(loop);
1406
  }
1407
#  ifdef _MSC_VER
1408
  _set_fmode(_O_TEXT);
1409
#  else
1410
  _fmode = _O_TEXT;
1411
#  endif
1412
  // Replace libuv's report handler with our own to suppress popups.
1413
  cmSystemTools::EnableMSVCDebugHook();
1414
#endif
1415
0
}
1416
1417
#if defined(_WIN32)
1418
#  include <random>
1419
1420
#  include <wctype.h>
1421
#  ifdef _MSC_VER
1422
using mode_t = cmSystemTools::SystemTools::mode_t;
1423
#  endif
1424
#else
1425
#  include <sys/stat.h>
1426
#endif
1427
1428
inline int Mkdir(char const* dir, mode_t const* mode)
1429
0
{
1430
#if defined(_WIN32)
1431
  int ret = _wmkdir(cmSystemTools::ConvertToWindowsExtendedPath(dir).c_str());
1432
  if (ret == 0 && mode)
1433
    cmSystemTools::SystemTools::SetPermissions(dir, *mode);
1434
  return ret;
1435
#else
1436
0
  return mkdir(dir, mode ? *mode : 0777);
1437
0
#endif
1438
0
}
1439
1440
#ifdef CMAKE_NO_MKDTEMP
1441
namespace {
1442
char* cm_mkdtemp_fallback(char* template_)
1443
{
1444
  if (mktemp(template_) == nullptr || mkdir(template_, 0700) != 0) {
1445
    return nullptr;
1446
  }
1447
  return template_;
1448
}
1449
using cm_mkdtemp_t = char* (*)(char*);
1450
cm_mkdtemp_t const cm_mkdtemp = []() -> cm_mkdtemp_t {
1451
  cm_mkdtemp_t f = (cm_mkdtemp_t)dlsym(RTLD_DEFAULT, "mkdtemp");
1452
  dlerror(); // Ignore/cleanup dlsym errors.
1453
  if (!f) {
1454
    f = cm_mkdtemp_fallback;
1455
  }
1456
  return f;
1457
}();
1458
}
1459
#else
1460
0
#  define cm_mkdtemp mkdtemp
1461
#endif
1462
1463
cmsys::Status cmSystemTools::MakeTempDirectory(std::string& path,
1464
                                               mode_t const* mode)
1465
0
{
1466
0
  if (path.empty()) {
1467
0
    return cmsys::Status::POSIX(EINVAL);
1468
0
  }
1469
0
  return cmSystemTools::MakeTempDirectory(&path.front(), mode);
1470
0
}
1471
1472
cmsys::Status cmSystemTools::MakeTempDirectory(char* path, mode_t const* mode)
1473
0
{
1474
0
  if (!path) {
1475
0
    return cmsys::Status::POSIX(EINVAL);
1476
0
  }
1477
1478
  // verify that path ends with "XXXXXX"
1479
0
  auto const l = std::strlen(path);
1480
0
  if (!cmHasLiteralSuffix(cm::string_view{ path, l }, "XXXXXX")) {
1481
0
    return cmsys::Status::POSIX(EINVAL);
1482
0
  }
1483
1484
  // create parent directories
1485
0
  auto* sep = path;
1486
0
  while ((sep = strchr(sep, '/'))) {
1487
    // all underlying functions use C strings,
1488
    // so temporarily end the string here
1489
0
    *sep = '\0';
1490
0
    Mkdir(path, mode);
1491
1492
0
    *sep = '/';
1493
0
    ++sep;
1494
0
  }
1495
1496
#ifdef _WIN32
1497
  int const nchars = 36;
1498
  char const chars[nchars + 1] = "abcdefghijklmnopqrstuvwxyz0123456789";
1499
1500
  std::random_device rd;
1501
  std::mt19937 rg{ rd() };
1502
  std::uniform_int_distribution<int> dist{ 0, nchars - 1 };
1503
1504
  for (auto tries = 100; tries; --tries) {
1505
    for (auto n = l - 6; n < l; ++n) {
1506
      path[n] = chars[dist(rg)];
1507
    }
1508
    if (Mkdir(path, mode) == 0) {
1509
      return cmsys::Status::Success();
1510
    } else if (errno != EEXIST) {
1511
      return cmsys::Status::POSIX_errno();
1512
    }
1513
  }
1514
  return cmsys::Status::POSIX(EAGAIN);
1515
#else
1516
0
  if (cm_mkdtemp(path)) {
1517
0
    if (mode) {
1518
0
      chmod(path, *mode);
1519
0
    }
1520
0
  } else {
1521
0
    return cmsys::Status::POSIX_errno();
1522
0
  }
1523
0
  return cmsys::Status::Success();
1524
0
#endif
1525
0
}
1526
1527
#ifdef _WIN32
1528
namespace {
1529
bool cmMoveFile(std::wstring const& oldname, std::wstring const& newname,
1530
                cmSystemTools::Replace replace)
1531
{
1532
  // Not only ignore any previous error, but clear any memory of it.
1533
  SetLastError(0);
1534
1535
  DWORD flags = 0;
1536
  if (replace == cmSystemTools::Replace::Yes) {
1537
    // Use MOVEFILE_REPLACE_EXISTING to replace an existing destination file.
1538
    flags = flags | MOVEFILE_REPLACE_EXISTING;
1539
  }
1540
1541
  return MoveFileExW(oldname.c_str(), newname.c_str(), flags);
1542
}
1543
}
1544
#endif
1545
1546
cmSystemTools::CopyResult cmSystemTools::CopySingleFile(
1547
  std::string const& oldname, std::string const& newname, CopyWhen when,
1548
  CopyInputRecent inputRecent, std::string* err)
1549
0
{
1550
0
  switch (when) {
1551
0
    case CopyWhen::Always:
1552
0
      break;
1553
0
    case CopyWhen::OnlyIfDifferent:
1554
0
      if (!FilesDiffer(oldname, newname)) {
1555
0
        return CopyResult::Success;
1556
0
      }
1557
0
      break;
1558
0
    case CopyWhen::OnlyIfNewer: {
1559
0
      if (!SystemTools::FileExists(newname)) {
1560
0
        break;
1561
0
      }
1562
0
      int timeResult = 0;
1563
0
      cmsys::Status timeStatus =
1564
0
        cmsys::SystemTools::FileTimeCompare(oldname, newname, &timeResult);
1565
0
      if (timeStatus.IsSuccess() && timeResult <= 0) {
1566
0
        return CopyResult::Success;
1567
0
      }
1568
0
      break;
1569
0
    }
1570
0
  }
1571
1572
0
  mode_t perm = 0;
1573
0
  cmsys::Status perms = SystemTools::GetPermissions(oldname, perm);
1574
1575
  // If files are the same do not copy
1576
0
  if (SystemTools::SameFile(oldname, newname)) {
1577
0
    return CopyResult::Success;
1578
0
  }
1579
1580
0
  cmsys::SystemTools::CopyStatus status;
1581
0
  status = cmsys::SystemTools::CloneFileContent(oldname, newname);
1582
0
  if (!status) {
1583
    // if cloning did not succeed, fall back to blockwise copy
1584
#ifdef _WIN32
1585
    if (inputRecent == CopyInputRecent::Yes) {
1586
      // Windows sometimes locks a file immediately after creation.
1587
      // Retry a few times.
1588
      WindowsFileRetry retry = cmSystemTools::GetWindowsFileRetry();
1589
      while ((status =
1590
                cmsys::SystemTools::CopyFileContentBlockwise(oldname, newname),
1591
              status.Path == cmsys::SystemTools::CopyStatus::SourcePath &&
1592
                status.GetPOSIX() == EACCES && --retry.Count)) {
1593
        cmSystemTools::Delay(retry.Delay);
1594
      }
1595
    } else {
1596
      status = cmsys::SystemTools::CopyFileContentBlockwise(oldname, newname);
1597
    }
1598
#else
1599
0
    static_cast<void>(inputRecent);
1600
0
    status = cmsys::SystemTools::CopyFileContentBlockwise(oldname, newname);
1601
0
#endif
1602
0
  }
1603
0
  if (!status) {
1604
0
    if (err) {
1605
0
      *err = status.GetString();
1606
0
      switch (status.Path) {
1607
0
        case cmsys::SystemTools::CopyStatus::SourcePath:
1608
0
          *err = cmStrCat(*err, " (input)");
1609
0
          break;
1610
0
        case cmsys::SystemTools::CopyStatus::DestPath:
1611
0
          *err = cmStrCat(*err, " (output)");
1612
0
          break;
1613
0
        default:
1614
0
          break;
1615
0
      }
1616
0
    }
1617
0
    return CopyResult::Failure;
1618
0
  }
1619
0
  if (perms) {
1620
0
    perms = SystemTools::SetPermissions(newname, perm);
1621
0
    if (!perms) {
1622
0
      if (err) {
1623
0
        *err = cmStrCat(perms.GetString(), " (output)");
1624
0
      }
1625
0
      return CopyResult::Failure;
1626
0
    }
1627
0
  }
1628
0
  return CopyResult::Success;
1629
0
}
1630
1631
bool cmSystemTools::RenameFile(std::string const& oldname,
1632
                               std::string const& newname)
1633
0
{
1634
0
  return cmSystemTools::RenameFile(oldname, newname, Replace::Yes) ==
1635
0
    RenameResult::Success;
1636
0
}
1637
1638
cmSystemTools::RenameResult cmSystemTools::RenameFile(
1639
  std::string const& oldname, std::string const& newname, Replace replace,
1640
  std::string* err)
1641
0
{
1642
#ifdef _WIN32
1643
#  ifndef INVALID_FILE_ATTRIBUTES
1644
#    define INVALID_FILE_ATTRIBUTES ((DWORD) - 1)
1645
#  endif
1646
  std::wstring const oldname_wstr =
1647
    SystemTools::ConvertToWindowsExtendedPath(oldname);
1648
  std::wstring const newname_wstr =
1649
    SystemTools::ConvertToWindowsExtendedPath(newname);
1650
1651
  /* Windows MoveFileEx may not replace read-only or in-use files.  If it
1652
     fails then remove the read-only attribute from any existing destination.
1653
     Try multiple times since we may be racing against another process
1654
     creating/opening the destination file just before our MoveFileEx.  */
1655
  WindowsFileRetry retry = GetWindowsRetry(oldname_wstr);
1656
1657
  // Use RAII to set the attribute bit blocking Microsoft Search Indexing,
1658
  // and restore the previous value upon return.
1659
  SaveRestoreFileAttributes save_restore_file_attributes(
1660
    oldname_wstr, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);
1661
1662
  DWORD move_last_error = 0;
1663
  while (!cmMoveFile(oldname_wstr, newname_wstr, replace) && --retry.Count) {
1664
    move_last_error = GetLastError();
1665
1666
    // There was no error ==> the operation is not yet complete.
1667
    if (move_last_error == NO_ERROR) {
1668
      break;
1669
    }
1670
1671
    // Try again only if failure was due to access/sharing permissions.
1672
    // Most often ERROR_ACCESS_DENIED (a.k.a. I/O error) for a directory, and
1673
    // ERROR_SHARING_VIOLATION for a file, are caused by one of the following:
1674
    // 1) Anti-Virus Software
1675
    // 2) Windows Search Indexer
1676
    // 3) Windows Explorer has an associated directory already opened.
1677
    if (move_last_error != ERROR_ACCESS_DENIED &&
1678
        move_last_error != ERROR_SHARING_VIOLATION) {
1679
      if (replace == Replace::No && move_last_error == ERROR_ALREADY_EXISTS) {
1680
        return RenameResult::NoReplace;
1681
      }
1682
      if (err) {
1683
        *err = cmsys::Status::Windows(move_last_error).GetString();
1684
      }
1685
      return RenameResult::Failure;
1686
    }
1687
1688
    DWORD const attrs = GetFileAttributesW(newname_wstr.c_str());
1689
    if ((attrs != INVALID_FILE_ATTRIBUTES) &&
1690
        (attrs & FILE_ATTRIBUTE_READONLY) &&
1691
        // FILE_ATTRIBUTE_READONLY is not honored on directories.
1692
        !(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
1693
      // Remove the read-only attribute from the destination file.
1694
      SetFileAttributesW(newname_wstr.c_str(),
1695
                         attrs & ~FILE_ATTRIBUTE_READONLY);
1696
    } else {
1697
      // The file may be temporarily in use so wait a bit.
1698
      cmSystemTools::Delay(retry.Delay);
1699
    }
1700
  }
1701
1702
  // If we were successful, then there was no error.
1703
  if (retry.Count > 0) {
1704
    move_last_error = 0;
1705
    // Restore the attributes on the new name.
1706
    save_restore_file_attributes.SetPath(newname_wstr);
1707
  }
1708
  SetLastError(move_last_error);
1709
  if (retry.Count > 0) {
1710
    return RenameResult::Success;
1711
  }
1712
  if (replace == Replace::No && GetLastError() == ERROR_ALREADY_EXISTS) {
1713
    return RenameResult::NoReplace;
1714
  }
1715
  if (err) {
1716
    *err = cmsys::Status::Windows_GetLastError().GetString();
1717
  }
1718
  return RenameResult::Failure;
1719
#else
1720
  // On UNIX we have OS-provided calls to create 'newname' atomically.
1721
0
  if (replace == Replace::No) {
1722
0
    if (link(oldname.c_str(), newname.c_str()) == 0) {
1723
0
      if (unlink(oldname.c_str()) == 0) {
1724
0
        return RenameResult::Success;
1725
0
      }
1726
0
      if (err) {
1727
0
        *err = cmsys::Status::POSIX_errno().GetString();
1728
0
      }
1729
0
      return RenameResult::Failure;
1730
0
    }
1731
0
    if (errno == EEXIST) {
1732
0
      return RenameResult::NoReplace;
1733
0
    }
1734
0
    if (err) {
1735
0
      *err = cmsys::Status::POSIX_errno().GetString();
1736
0
    }
1737
0
    return RenameResult::Failure;
1738
0
  }
1739
0
  if (rename(oldname.c_str(), newname.c_str()) == 0) {
1740
0
    return RenameResult::Success;
1741
0
  }
1742
0
  if (err) {
1743
0
    *err = cmsys::Status::POSIX_errno().GetString();
1744
0
  }
1745
0
  return RenameResult::Failure;
1746
0
#endif
1747
0
}
1748
1749
cmsys::Status cmSystemTools::MoveFileIfDifferent(
1750
  std::string const& source, std::string const& destination)
1751
0
{
1752
0
  cmsys::Status res = {};
1753
0
  if (FilesDiffer(source, destination)) {
1754
0
    if (RenameFile(source, destination)) {
1755
0
      return res;
1756
0
    }
1757
0
    res = CopyFileAlways(source, destination);
1758
0
  }
1759
0
  RemoveFile(source);
1760
0
  return res;
1761
0
}
1762
1763
void cmSystemTools::Glob(std::string const& directory,
1764
                         std::string const& regexp,
1765
                         std::vector<std::string>& files)
1766
0
{
1767
0
  cmsys::Directory d;
1768
0
  cmsys::RegularExpression reg(regexp.c_str());
1769
1770
0
  if (d.Load(directory)) {
1771
0
    size_t numf;
1772
0
    unsigned int i;
1773
0
    numf = d.GetNumberOfFiles();
1774
0
    for (i = 0; i < numf; i++) {
1775
0
      std::string const& fname = d.GetFileName(i);
1776
0
      if (reg.find(fname)) {
1777
0
        files.push_back(fname);
1778
0
      }
1779
0
    }
1780
0
  }
1781
0
}
1782
1783
void cmSystemTools::GlobDirs(std::string const& path,
1784
                             std::vector<std::string>& files)
1785
0
{
1786
0
  std::string::size_type pos = path.find("/*");
1787
0
  if (pos == std::string::npos) {
1788
0
    files.push_back(path);
1789
0
    return;
1790
0
  }
1791
0
  std::string startPath = path.substr(0, pos);
1792
0
  std::string finishPath = path.substr(pos + 2);
1793
1794
0
  cmsys::Directory d;
1795
0
  if (d.Load(startPath)) {
1796
0
    for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i) {
1797
0
      std::string const& f = d.GetFileName(i);
1798
0
      if (f != "." && f != "..") {
1799
0
        std::string fname = cmStrCat(startPath, '/', f);
1800
0
        if (cmSystemTools::FileIsDirectory(fname)) {
1801
0
          fname += finishPath;
1802
0
          cmSystemTools::GlobDirs(fname, files);
1803
0
        }
1804
0
      }
1805
0
    }
1806
0
  }
1807
0
}
1808
1809
bool cmSystemTools::SimpleGlob(std::string const& glob,
1810
                               std::vector<std::string>& files,
1811
                               int type /* = 0 */)
1812
0
{
1813
0
  files.clear();
1814
0
  if (glob.back() != '*') {
1815
0
    return false;
1816
0
  }
1817
0
  std::string path = cmSystemTools::GetFilenamePath(glob);
1818
0
  std::string ppath = cmSystemTools::GetFilenameName(glob);
1819
0
  ppath = ppath.substr(0, ppath.size() - 1);
1820
0
  if (path.empty()) {
1821
0
    path = "/";
1822
0
  }
1823
1824
0
  bool res = false;
1825
0
  cmsys::Directory d;
1826
0
  if (d.Load(path)) {
1827
0
    for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i) {
1828
0
      std::string const& sfname = d.GetFileName(i);
1829
0
      if (sfname != "." && sfname != "..") {
1830
0
        std::string fname = path;
1831
0
        if (path.back() != '/') {
1832
0
          fname += "/";
1833
0
        }
1834
0
        fname += sfname;
1835
0
        if (type > 0 && cmSystemTools::FileIsDirectory(fname)) {
1836
0
          continue;
1837
0
        }
1838
0
        if (type < 0 && !cmSystemTools::FileIsDirectory(fname)) {
1839
0
          continue;
1840
0
        }
1841
0
        if (cmHasPrefix(sfname, ppath)) {
1842
0
          files.push_back(fname);
1843
0
          res = true;
1844
0
        }
1845
0
      }
1846
0
    }
1847
0
  }
1848
0
  return res;
1849
0
}
1850
1851
std::string cmSystemTools::ConvertToOutputPath(std::string const& path)
1852
0
{
1853
#if defined(_WIN32) && !defined(__CYGWIN__)
1854
  if (s_ForceUnixPaths) {
1855
    return cmSystemTools::ConvertToUnixOutputPath(path);
1856
  }
1857
  return cmSystemTools::ConvertToWindowsOutputPath(path);
1858
#else
1859
0
  return cmSystemTools::ConvertToUnixOutputPath(path);
1860
0
#endif
1861
0
}
1862
1863
void cmSystemTools::ConvertToOutputSlashes(std::string& path)
1864
0
{
1865
#if defined(_WIN32) && !defined(__CYGWIN__)
1866
  if (!s_ForceUnixPaths) {
1867
    // Convert to windows slashes.
1868
    std::string::size_type pos = 0;
1869
    while ((pos = path.find('/', pos)) != std::string::npos) {
1870
      path[pos++] = '\\';
1871
    }
1872
  }
1873
#else
1874
0
  static_cast<void>(path);
1875
0
#endif
1876
0
}
1877
1878
void cmSystemTools::ConvertToLongPath(std::string& path)
1879
132k
{
1880
#if defined(_WIN32) && !defined(__CYGWIN__)
1881
  // Try to convert path to a long path only if the path contains character '~'
1882
  if (path.find('~') == std::string::npos) {
1883
    return;
1884
  }
1885
1886
  std::wstring wPath = cmsys::Encoding::ToWide(path);
1887
  DWORD ret = GetLongPathNameW(wPath.c_str(), nullptr, 0);
1888
  std::vector<wchar_t> buffer(ret);
1889
  if (ret != 0) {
1890
    ret = GetLongPathNameW(wPath.c_str(), buffer.data(),
1891
                           static_cast<DWORD>(buffer.size()));
1892
  }
1893
1894
  if (ret != 0) {
1895
    path = cmsys::Encoding::ToNarrow(buffer.data());
1896
  }
1897
#else
1898
132k
  static_cast<void>(path);
1899
132k
#endif
1900
132k
}
1901
1902
std::string cmSystemTools::ConvertToRunCommandPath(std::string const& path)
1903
0
{
1904
#if defined(_WIN32) && !defined(__CYGWIN__)
1905
  return cmSystemTools::ConvertToWindowsOutputPath(path);
1906
#else
1907
0
  return cmSystemTools::ConvertToUnixOutputPath(path);
1908
0
#endif
1909
0
}
1910
1911
// compute the relative path from here to there
1912
std::string cmSystemTools::RelativePath(std::string const& local,
1913
                                        std::string const& remote)
1914
0
{
1915
0
  if (!cmSystemTools::FileIsFullPath(local)) {
1916
0
    cmSystemTools::Error("RelativePath must be passed a full path to local: " +
1917
0
                         local);
1918
0
  }
1919
0
  if (!cmSystemTools::FileIsFullPath(remote)) {
1920
0
    cmSystemTools::Error(
1921
0
      "RelativePath must be passed a full path to remote: " + remote);
1922
0
  }
1923
0
  return cmsys::SystemTools::RelativePath(local, remote);
1924
0
}
1925
1926
std::string cmSystemTools::ForceToRelativePath(std::string const& local_path,
1927
                                               std::string const& remote_path)
1928
0
{
1929
  // The paths should never be quoted.
1930
0
  assert(local_path.front() != '\"');
1931
0
  assert(remote_path.front() != '\"');
1932
1933
  // The local path should never have a trailing slash except if it is just the
1934
  // bare root directory
1935
0
  assert(local_path.empty() || local_path.back() != '/' ||
1936
0
         local_path.size() == 1 ||
1937
0
         (local_path.size() == 3 && local_path[1] == ':' &&
1938
0
          ((local_path[0] >= 'A' && local_path[0] <= 'Z') ||
1939
0
           (local_path[0] >= 'a' && local_path[0] <= 'z'))));
1940
1941
  // If the path is already relative then just return the path.
1942
0
  if (!cmSystemTools::FileIsFullPath(remote_path)) {
1943
0
    return remote_path;
1944
0
  }
1945
1946
  // Identify the longest shared path component between the remote
1947
  // path and the local path.
1948
0
  std::vector<std::string> local;
1949
0
  cmSystemTools::SplitPath(local_path, local);
1950
0
  std::vector<std::string> remote;
1951
0
  cmSystemTools::SplitPath(remote_path, remote);
1952
0
  unsigned int common = 0;
1953
0
  while (common < remote.size() && common < local.size() &&
1954
0
         cmSystemTools::ComparePath(remote[common], local[common])) {
1955
0
    ++common;
1956
0
  }
1957
1958
  // If no part of the path is in common then return the full path.
1959
0
  if (common == 0) {
1960
0
    return remote_path;
1961
0
  }
1962
1963
  // If the entire path is in common then just return a ".".
1964
0
  if (common == remote.size() && common == local.size()) {
1965
0
    return ".";
1966
0
  }
1967
1968
  // If the entire path is in common except for a trailing slash then
1969
  // just return a "./".
1970
0
  if (common + 1 == remote.size() && remote[common].empty() &&
1971
0
      common == local.size()) {
1972
0
    return "./";
1973
0
  }
1974
1975
  // Construct the relative path.
1976
0
  std::string relative;
1977
1978
  // First add enough ../ to get up to the level of the shared portion
1979
  // of the path.  Leave off the trailing slash.  Note that the last
1980
  // component of local will never be empty because local should never
1981
  // have a trailing slash.
1982
0
  for (unsigned int i = common; i < local.size(); ++i) {
1983
0
    relative += "..";
1984
0
    if (i < local.size() - 1) {
1985
0
      relative += "/";
1986
0
    }
1987
0
  }
1988
1989
  // Now add the portion of the destination path that is not included
1990
  // in the shared portion of the path.  Add a slash the first time
1991
  // only if there was already something in the path.  If there was a
1992
  // trailing slash in the input then the last iteration of the loop
1993
  // will add a slash followed by an empty string which will preserve
1994
  // the trailing slash in the output.
1995
1996
0
  if (!relative.empty() && !remote.empty()) {
1997
0
    relative += "/";
1998
0
  }
1999
0
  relative += cmJoin(cmMakeRange(remote).advance(common), "/");
2000
2001
  // Finally return the path.
2002
0
  return relative;
2003
0
}
2004
2005
std::string cmSystemTools::RelativeIfUnder(std::string const& top,
2006
                                           std::string const& in)
2007
1
{
2008
1
  std::string out;
2009
1
  if (in == top) {
2010
0
    out = ".";
2011
1
  } else if (cmSystemTools::IsSubDirectory(in, top)) {
2012
0
    out = in.substr(top.size() + (top.back() == '/' ? 0 : 1));
2013
1
  } else {
2014
1
    out = in;
2015
1
  }
2016
1
  return out;
2017
1
}
2018
2019
cm::optional<std::string> cmSystemTools::GetEnvVar(std::string const& var)
2020
17
{
2021
17
  cm::optional<std::string> result;
2022
17
  {
2023
17
    std::string value;
2024
17
    if (cmSystemTools::GetEnv(var, value)) {
2025
0
      result = std::move(value);
2026
0
    }
2027
17
  }
2028
17
  return result;
2029
17
}
2030
2031
std::vector<std::string> cmSystemTools::GetEnvPathNormalized(
2032
  std::string const& var)
2033
0
{
2034
0
  std::vector<std::string> result;
2035
0
  if (cm::optional<std::string> env = cmSystemTools::GetEnvVar(var)) {
2036
0
    std::vector<std::string> p = cmSystemTools::SplitEnvPathNormalized(*env);
2037
0
    std::move(p.begin(), p.end(), std::back_inserter(result));
2038
0
  }
2039
0
  return result;
2040
0
}
2041
2042
std::vector<std::string> cmSystemTools::SplitEnvPath(cm::string_view in)
2043
0
{
2044
#if defined(_WIN32) && !defined(__CYGWIN__)
2045
  static cm::string_view sep = ";"_s;
2046
#else
2047
0
  static cm::string_view sep = ":"_s;
2048
0
#endif
2049
0
  std::vector<std::string> paths;
2050
0
  cm::string_view::size_type e = 0;
2051
0
  for (;;) {
2052
0
    cm::string_view::size_type b = in.find_first_not_of(sep, e);
2053
0
    if (b == cm::string_view::npos) {
2054
0
      break;
2055
0
    }
2056
0
    e = in.find_first_of(sep, b);
2057
0
    if (e == cm::string_view::npos) {
2058
0
      paths.emplace_back(in.substr(b));
2059
0
      break;
2060
0
    }
2061
0
    paths.emplace_back(in.substr(b, e - b));
2062
0
  }
2063
0
  return paths;
2064
0
}
2065
2066
std::vector<std::string> cmSystemTools::SplitEnvPathNormalized(
2067
  cm::string_view in)
2068
0
{
2069
0
  std::vector<std::string> paths = cmSystemTools::SplitEnvPath(in);
2070
0
  std::transform(paths.begin(), paths.end(), paths.begin(),
2071
0
                 cmSystemTools::ToNormalizedPathOnDisk);
2072
0
  return paths;
2073
0
}
2074
2075
std::string cmSystemTools::ToNormalizedPathOnDisk(std::string p)
2076
11
{
2077
11
  p = ResolveTildePath(p);
2078
11
  using namespace cm::PathResolver;
2079
#ifdef _WIN32
2080
  // IWYU pragma: no_forward_declare cm::PathResolver::Policies::CasePath
2081
  static Resolver<Policies::CasePath> const resolver(RealOS);
2082
#else
2083
  // IWYU pragma: no_forward_declare cm::PathResolver::Policies::LogicalPath
2084
11
  static Resolver<Policies::LogicalPath> const resolver(RealOS);
2085
11
#endif
2086
11
  resolver.Resolve(std::move(p), p);
2087
11
  return p;
2088
11
}
2089
2090
#ifndef CMAKE_BOOTSTRAP
2091
bool cmSystemTools::UnsetEnv(char const* value)
2092
0
{
2093
#  if !defined(HAVE_UNSETENV)
2094
  return cmSystemTools::UnPutEnv(value);
2095
#  else
2096
0
  unsetenv(value);
2097
0
  return true;
2098
0
#  endif
2099
0
}
2100
2101
std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
2102
0
{
2103
0
  std::vector<std::string> env;
2104
#  ifdef _WIN32
2105
  struct EnvDeleter
2106
  {
2107
    void operator()(wchar_t* p) const { FreeEnvironmentStringsW(p); }
2108
  };
2109
2110
  auto block = std::unique_ptr<wchar_t, EnvDeleter>(GetEnvironmentStringsW());
2111
  if (!block) {
2112
    return env;
2113
  }
2114
2115
  for (wchar_t const* p = block.get(); *p; p += wcslen(p) + 1) {
2116
    if (p[0] != L'=') {
2117
      env.emplace_back(cmsys::Encoding::ToNarrow(p));
2118
    }
2119
  }
2120
#  else
2121
0
  for (int cc = 0; environ[cc]; ++cc) {
2122
0
    env.emplace_back(environ[cc]);
2123
0
  }
2124
0
#  endif
2125
0
  return env;
2126
0
}
2127
2128
cmSystemTools::SaveRestoreEnvironment::SaveRestoreEnvironment()
2129
0
{
2130
0
  this->Env = cmSystemTools::GetEnvironmentVariables();
2131
0
}
2132
2133
cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment()
2134
0
{
2135
  // First clear everything in the current environment:
2136
0
  std::vector<std::string> currentEnv = GetEnvironmentVariables();
2137
0
  for (std::string var : currentEnv) {
2138
0
    std::string::size_type pos = var.find('=');
2139
0
    if (pos != std::string::npos) {
2140
0
      var = var.substr(0, pos);
2141
0
    }
2142
2143
0
    cmSystemTools::UnsetEnv(var.c_str());
2144
0
  }
2145
2146
  // Then put back each entry from the original environment:
2147
0
  for (std::string const& var : this->Env) {
2148
0
    cmSystemTools::PutEnv(var);
2149
0
  }
2150
0
}
2151
#endif
2152
2153
cmSystemTools::ScopedEnv::ScopedEnv(cm::string_view var)
2154
0
{
2155
0
  std::string::size_type pos = var.find('=');
2156
0
  if (pos != std::string::npos) {
2157
0
    this->Key = std::string{ var.substr(0, pos) };
2158
0
    this->Original = cmSystemTools::GetEnvVar(this->Key);
2159
2160
0
    cm::string_view value = var.substr(pos + 1);
2161
2162
0
    if (!this->Original && value.empty()) {
2163
      // nothing to do if the environment variable wasn't already set and the
2164
      // new value is also empty. clear the Key member so the destructor also
2165
      // does nothing.
2166
0
      this->Key.clear();
2167
0
    } else {
2168
0
      if (value.empty()) {
2169
0
        cmSystemTools::UnPutEnv(this->Key);
2170
0
      } else {
2171
0
        cmSystemTools::PutEnv(cmStrCat(this->Key, '=', value));
2172
0
      }
2173
0
    }
2174
0
  }
2175
0
}
2176
2177
cmSystemTools::ScopedEnv::~ScopedEnv()
2178
0
{
2179
0
  if (!this->Key.empty()) {
2180
0
    if (this->Original) {
2181
0
      cmSystemTools::PutEnv(cmStrCat(this->Key, '=', *this->Original));
2182
0
    } else {
2183
0
      cmSystemTools::UnPutEnv(Key);
2184
0
    }
2185
0
  }
2186
0
}
2187
2188
void cmSystemTools::EnableVSConsoleOutput()
2189
35
{
2190
#ifdef _WIN32
2191
  // Visual Studio tools like devenv may not
2192
  // display output to the console unless this environment variable is
2193
  // set.  We need it to capture the output of these build tools.
2194
  // Note for future work that one could pass "/out \\.\pipe\NAME" to
2195
  // either of these executables where NAME is created with
2196
  // CreateNamedPipe.  This would bypass the internal buffering of the
2197
  // output and allow it to be captured on the fly.
2198
  cmSystemTools::PutEnv("vsconsoleoutput=1");
2199
2200
#  ifndef CMAKE_BOOTSTRAP
2201
  // VS sets an environment variable to tell MS tools like "cl" to report
2202
  // output through a backdoor pipe instead of stdout/stderr.  Unset the
2203
  // environment variable to close this backdoor for any path of process
2204
  // invocations that passes through CMake so we can capture the output.
2205
  cmSystemTools::UnsetEnv("VS_UNICODE_OUTPUT");
2206
#  endif
2207
#endif
2208
35
}
2209
2210
bool cmSystemTools::IsPathToFramework(std::string const& path)
2211
0
{
2212
0
  return (cmSystemTools::FileIsFullPath(path) &&
2213
0
          cmHasLiteralSuffix(path, ".framework"));
2214
0
}
2215
2216
bool cmSystemTools::IsPathToXcFramework(std::string const& path)
2217
0
{
2218
0
  return (cmSystemTools::FileIsFullPath(path) &&
2219
0
          cmHasLiteralSuffix(path, ".xcframework"));
2220
0
}
2221
2222
bool cmSystemTools::IsPathToMacOSSharedLibrary(std::string const& path)
2223
0
{
2224
0
  return (cmSystemTools::FileIsFullPath(path) &&
2225
0
          cmHasLiteralSuffix(path, ".dylib"));
2226
0
}
2227
2228
bool cmSystemTools::CreateTar(
2229
  std::string const& arFileName, std::vector<std::string> const& files,
2230
  std::string const& workingDirectory, cmTarCompression compressType,
2231
  std::string const& encoding, bool verbose, std::string const& mtime,
2232
  std::string const& format, int compressionLevel, int numThreads)
2233
0
{
2234
0
#if !defined(CMAKE_BOOTSTRAP)
2235
0
  cmWorkingDirectory workdir(cmSystemTools::GetLogicalWorkingDirectory());
2236
0
  if (!workingDirectory.empty()) {
2237
0
    workdir.SetDirectory(workingDirectory);
2238
0
  }
2239
2240
0
  std::string const cwd = cmSystemTools::GetLogicalWorkingDirectory();
2241
0
  cmsys::ofstream fout(arFileName.c_str(), std::ios::out | std::ios::binary);
2242
0
  if (!fout) {
2243
0
    std::string e = cmStrCat("Cannot open output file \"", arFileName,
2244
0
                             "\": ", cmSystemTools::GetLastSystemError());
2245
0
    cmSystemTools::Error(e);
2246
0
    return false;
2247
0
  }
2248
0
  cmArchiveWrite::Compress compress = cmArchiveWrite::CompressNone;
2249
0
  switch (compressType) {
2250
0
    case TarCompressGZip:
2251
0
      compress = cmArchiveWrite::CompressGZip;
2252
0
      break;
2253
0
    case TarCompressBZip2:
2254
0
      compress = cmArchiveWrite::CompressBZip2;
2255
0
      break;
2256
0
    case TarCompressXZ:
2257
0
      compress = cmArchiveWrite::CompressXZ;
2258
0
      break;
2259
0
    case TarCompressZstd:
2260
0
      compress = cmArchiveWrite::CompressZstd;
2261
0
      break;
2262
0
    case TarCompressLZMA:
2263
0
      compress = cmArchiveWrite::CompressLZMA;
2264
0
      break;
2265
0
    case TarCompressPPMd:
2266
0
      compress = cmArchiveWrite::CompressPPMd;
2267
0
      break;
2268
0
    case TarCompressAuto:
2269
      // Kept for backwards compatibility with pre-4.3 versions of CMake
2270
0
      if (format == "zip") {
2271
0
        compress = cmArchiveWrite::CompressGZip;
2272
0
      } else if (format == "7zip") {
2273
0
        compress = cmArchiveWrite::CompressLZMA;
2274
0
      } else {
2275
0
        compress = cmArchiveWrite::CompressNone;
2276
0
      }
2277
0
      break;
2278
0
    case TarCompressNone:
2279
0
      compress = cmArchiveWrite::CompressNone;
2280
0
      break;
2281
0
  }
2282
2283
0
  cmArchiveWrite a(fout, compress, format.empty() ? "paxr" : format, encoding,
2284
0
                   compressionLevel, numThreads);
2285
2286
0
  if (!a.Open()) {
2287
0
    cmSystemTools::Error(a.GetError());
2288
0
    return false;
2289
0
  }
2290
0
  a.SetMTime(mtime);
2291
0
  a.SetVerbose(verbose);
2292
0
  bool tarCreatedSuccessfully = true;
2293
0
  for (auto path : files) {
2294
0
    if (cmSystemTools::FileIsFullPath(path)) {
2295
      // Get the relative path to the file.
2296
0
      path = cmSystemTools::RelativePath(cwd, path);
2297
0
    }
2298
0
    if (!a.Add(path)) {
2299
0
      cmSystemTools::Error(a.GetError());
2300
0
      tarCreatedSuccessfully = false;
2301
0
    }
2302
0
  }
2303
0
  return tarCreatedSuccessfully;
2304
#else
2305
  (void)arFileName;
2306
  (void)files;
2307
  (void)encoding;
2308
  (void)verbose;
2309
  return false;
2310
#endif
2311
0
}
2312
2313
#if !defined(CMAKE_BOOTSTRAP)
2314
namespace {
2315
0
#  define BSDTAR_FILESIZE_PRINTF "%lu"
2316
#  define BSDTAR_FILESIZE_TYPE unsigned long
2317
void list_item_verbose(FILE* out, struct archive_entry* entry)
2318
0
{
2319
0
  char tmp[100];
2320
0
  size_t w;
2321
0
  char const* p;
2322
0
  char const* fmt;
2323
0
  time_t tim;
2324
0
  static time_t now;
2325
0
  size_t u_width = 6;
2326
0
  size_t gs_width = 13;
2327
2328
  /*
2329
   * We avoid collecting the entire list in memory at once by
2330
   * listing things as we see them.  However, that also means we can't
2331
   * just pre-compute the field widths.  Instead, we start with guesses
2332
   * and just widen them as necessary.  These numbers are completely
2333
   * arbitrary.
2334
   */
2335
0
  if (!now) {
2336
0
    time(&now);
2337
0
  }
2338
0
  fprintf(out, "%s %u ", archive_entry_strmode(entry),
2339
0
          archive_entry_nlink(entry));
2340
2341
  /* Use uname if it's present, else uid. */
2342
0
  p = archive_entry_uname(entry);
2343
0
  if (!p || (*p == '\0')) {
2344
0
    snprintf(tmp, sizeof(tmp), "%lu ",
2345
0
             static_cast<unsigned long>(archive_entry_uid(entry)));
2346
0
    p = tmp;
2347
0
  }
2348
0
  w = strlen(p);
2349
0
  if (w > u_width) {
2350
0
    u_width = w;
2351
0
  }
2352
0
  fprintf(out, "%-*s ", static_cast<int>(u_width), p);
2353
  /* Use gname if it's present, else gid. */
2354
0
  p = archive_entry_gname(entry);
2355
0
  if (p && p[0] != '\0') {
2356
0
    fprintf(out, "%s", p);
2357
0
    w = strlen(p);
2358
0
  } else {
2359
0
    snprintf(tmp, sizeof(tmp), "%lu",
2360
0
             static_cast<unsigned long>(archive_entry_gid(entry)));
2361
0
    w = strlen(tmp);
2362
0
    fprintf(out, "%s", tmp);
2363
0
  }
2364
2365
  /*
2366
   * Print device number or file size, right-aligned so as to make
2367
   * total width of group and devnum/filesize fields be gs_width.
2368
   * If gs_width is too small, grow it.
2369
   */
2370
0
  if (archive_entry_filetype(entry) == AE_IFCHR ||
2371
0
      archive_entry_filetype(entry) == AE_IFBLK) {
2372
0
    unsigned long rdevmajor = archive_entry_rdevmajor(entry);
2373
0
    unsigned long rdevminor = archive_entry_rdevminor(entry);
2374
0
    snprintf(tmp, sizeof(tmp), "%lu,%lu", rdevmajor, rdevminor);
2375
0
  } else {
2376
    /*
2377
     * Note the use of platform-dependent macros to format
2378
     * the filesize here.  We need the format string and the
2379
     * corresponding type for the cast.
2380
     */
2381
0
    snprintf(tmp, sizeof(tmp), BSDTAR_FILESIZE_PRINTF,
2382
0
             static_cast<BSDTAR_FILESIZE_TYPE>(archive_entry_size(entry)));
2383
0
  }
2384
0
  if (w + strlen(tmp) >= gs_width) {
2385
0
    gs_width = w + strlen(tmp) + 1;
2386
0
  }
2387
0
  fprintf(out, "%*s", static_cast<int>(gs_width - w), tmp);
2388
2389
  /* Format the time using 'ls -l' conventions. */
2390
0
  tim = archive_entry_mtime(entry);
2391
0
#  define HALF_YEAR ((time_t)365 * 86400 / 2)
2392
#  if defined(_WIN32) && !defined(__CYGWIN__)
2393
/* Windows' strftime function does not support %e format. */
2394
#    define DAY_FMT "%d"
2395
#  else
2396
0
#    define DAY_FMT "%e" /* Day number without leading zeros */
2397
0
#  endif
2398
0
  if (tim < now - HALF_YEAR || tim > now + HALF_YEAR) {
2399
0
    fmt = DAY_FMT " %b  %Y";
2400
0
  } else {
2401
0
    fmt = DAY_FMT " %b %H:%M";
2402
0
  }
2403
0
  strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
2404
0
  fprintf(out, " %s ", tmp);
2405
0
  fprintf(out, "%s", cm_archive_entry_pathname(entry));
2406
2407
  /* Extra information for links. */
2408
0
  if (archive_entry_hardlink(entry)) /* Hard link */
2409
0
  {
2410
0
    fprintf(out, " link to %s", archive_entry_hardlink(entry));
2411
0
  } else if (archive_entry_symlink(entry)) /* Symbolic link */
2412
0
  {
2413
0
    fprintf(out, " -> %s", archive_entry_symlink(entry));
2414
0
  }
2415
0
  fflush(out);
2416
0
}
2417
2418
void ArchiveError(char const* m1, struct archive* a)
2419
26.9k
{
2420
26.9k
  std::string message(m1);
2421
26.9k
  char const* m2 = archive_error_string(a);
2422
26.9k
  if (m2) {
2423
26.0k
    message += m2;
2424
26.0k
  }
2425
26.9k
  cmSystemTools::Error(message);
2426
26.9k
}
2427
2428
bool la_diagnostic(struct archive* ar, __LA_SSIZE_T r)
2429
61.2k
{
2430
  // See archive.h definition of ARCHIVE_OK for return values.
2431
2432
61.2k
  if (r >= ARCHIVE_OK) {
2433
45.9k
    return true;
2434
45.9k
  }
2435
2436
15.3k
  if (r >= ARCHIVE_WARN) {
2437
10.7k
    char const* warn = archive_error_string(ar);
2438
10.7k
    if (!warn) {
2439
10.7k
      warn = "unknown warning";
2440
10.7k
    }
2441
10.7k
    std::cerr << "cmake -E tar: warning: " << warn << '\n';
2442
10.7k
    return true;
2443
10.7k
  }
2444
2445
  // Error.
2446
4.61k
  char const* err = archive_error_string(ar);
2447
4.61k
  if (!err) {
2448
128
    err = "unknown error";
2449
128
  }
2450
4.61k
  std::cerr << "cmake -E tar: error: " << err << '\n';
2451
4.61k
  return false;
2452
15.3k
}
2453
2454
// Return 'true' on success
2455
bool copy_data(struct archive* ar, struct archive* aw)
2456
30.2k
{
2457
30.2k
  long r;
2458
30.2k
  void const* buff;
2459
30.2k
  size_t size;
2460
30.2k
#  if defined(ARCHIVE_VERSION_NUMBER) && ARCHIVE_VERSION_NUMBER >= 3000000
2461
30.2k
  __LA_INT64_T offset;
2462
#  else
2463
  off_t offset;
2464
#  endif
2465
2466
58.6k
  for (;;) {
2467
    // See archive.h definition of ARCHIVE_OK for return values.
2468
58.6k
    r = archive_read_data_block(ar, &buff, &size, &offset);
2469
58.6k
    if (r == ARCHIVE_EOF) {
2470
25.6k
      return true;
2471
25.6k
    }
2472
32.9k
    if (!la_diagnostic(ar, r)) {
2473
4.61k
      return false;
2474
4.61k
    }
2475
    // See archive.h definition of ARCHIVE_OK for return values.
2476
32.9k
    __LA_SSIZE_T const w = archive_write_data_block(aw, buff, size, offset);
2477
28.3k
    if (!la_diagnostic(ar, w)) {
2478
0
      return false;
2479
0
    }
2480
28.3k
  }
2481
#  if !defined(__clang__) && !defined(__NVCOMPILER) && !defined(__HP_aCC)
2482
  return false; /* this should not happen but it quiets some compilers */
2483
#  endif
2484
30.2k
}
2485
2486
bool extract_tar(std::string const& arFileName,
2487
                 std::vector<std::string> const& files,
2488
                 std::string const& encoding, bool verbose,
2489
                 cmSystemTools::cmTarExtractTimestamps extractTimestamps,
2490
                 bool extract)
2491
32.3k
{
2492
32.3k
  struct archive* a = archive_read_new();
2493
32.3k
  struct archive* ext = archive_write_disk_new();
2494
32.3k
  if (extract) {
2495
32.3k
    int flags =
2496
32.3k
      ARCHIVE_EXTRACT_SECURE_NODOTDOT | ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS;
2497
32.3k
    if (extractTimestamps == cmSystemTools::cmTarExtractTimestamps::Yes) {
2498
16.1k
      flags |= ARCHIVE_EXTRACT_TIME;
2499
16.1k
    }
2500
32.3k
    if (archive_write_disk_set_options(ext, flags) != ARCHIVE_OK) {
2501
0
      ArchiveError("Problem with archive_write_disk_set_options(): ", ext);
2502
0
      archive_write_free(ext);
2503
0
      archive_read_free(a);
2504
0
      return false;
2505
0
    }
2506
32.3k
  }
2507
32.3k
  archive_read_support_filter_all(a);
2508
32.3k
  archive_read_support_format_all(a);
2509
2510
32.3k
  if (encoding != "OEM") {
2511
32.3k
    if (archive_read_set_options(
2512
32.3k
          a, cmStrCat("hdrcharset=", encoding).c_str()) != ARCHIVE_OK) {
2513
0
      cmSystemTools::Error(
2514
0
        cmStrCat("Cannot set archive encoding: ", encoding));
2515
0
      return false;
2516
0
    }
2517
32.3k
  }
2518
32.3k
  struct archive_entry* entry;
2519
2520
32.3k
  struct archive* matching = archive_match_new();
2521
32.3k
  if (!matching) {
2522
0
    cmSystemTools::Error("Out of memory");
2523
0
    return false;
2524
0
  }
2525
2526
32.3k
  for (auto const& filename : files) {
2527
0
    if (archive_match_include_pattern(matching, filename.c_str()) !=
2528
0
        ARCHIVE_OK) {
2529
0
      cmSystemTools::Error("Failed to add to inclusion list: " + filename);
2530
0
      return false;
2531
0
    }
2532
0
  }
2533
2534
32.3k
  int r = cm_archive_read_open_filename(a, arFileName.c_str(), 10240);
2535
32.3k
  if (r) {
2536
17.0k
    ArchiveError("Problem with archive_read_open_filename(): ", a);
2537
17.0k
    archive_write_free(ext);
2538
17.0k
    archive_read_close(a);
2539
17.0k
    archive_read_free(a);
2540
17.0k
    archive_match_free(matching);
2541
17.0k
    return false;
2542
17.0k
  }
2543
40.9k
  for (;;) {
2544
40.9k
    r = archive_read_next_header(a, &entry);
2545
40.9k
    if (r == ARCHIVE_EOF) {
2546
776
      break;
2547
776
    }
2548
40.1k
    if (r != ARCHIVE_OK) {
2549
8.66k
      ArchiveError("Problem with archive_read_next_header(): ", a);
2550
8.66k
      break;
2551
8.66k
    }
2552
2553
31.5k
    if (archive_match_excluded(matching, entry)) {
2554
0
      continue;
2555
0
    }
2556
2557
31.5k
    if (verbose) {
2558
15.7k
      if (extract) {
2559
15.7k
        cmSystemTools::Stdout(
2560
15.7k
          cmStrCat("x ", cm_archive_entry_pathname(entry)));
2561
15.7k
      } else {
2562
0
        list_item_verbose(stdout, entry);
2563
0
      }
2564
15.7k
      cmSystemTools::Stdout("\n");
2565
15.7k
    } else if (!extract) {
2566
0
      cmSystemTools::Stdout(cmStrCat(cm_archive_entry_pathname(entry), '\n'));
2567
0
    }
2568
31.5k
    if (extract) {
2569
31.5k
      r = archive_write_header(ext, entry);
2570
31.5k
      if (r == ARCHIVE_OK) {
2571
30.2k
        if (!copy_data(a, ext)) {
2572
4.61k
          break;
2573
4.61k
        }
2574
25.6k
        r = archive_write_finish_entry(ext);
2575
25.6k
        if (r != ARCHIVE_OK) {
2576
0
          ArchiveError("Problem with archive_write_finish_entry(): ", ext);
2577
0
          break;
2578
0
        }
2579
25.6k
      }
2580
#  ifdef _WIN32
2581
      else if (char const* linktext = archive_entry_symlink(entry)) {
2582
        std::cerr << "cmake -E tar: warning: skipping symbolic link \""
2583
                  << cm_archive_entry_pathname(entry) << "\" -> \"" << linktext
2584
                  << "\"." << std::endl;
2585
      }
2586
#  endif
2587
1.22k
      else {
2588
1.22k
        ArchiveError("Problem with archive_write_header(): ", ext);
2589
1.22k
        cmSystemTools::Error(
2590
1.22k
          cmStrCat("Current file:\n  ", cm_archive_entry_pathname(entry)));
2591
1.22k
        break;
2592
1.22k
      }
2593
31.5k
    }
2594
31.5k
  }
2595
2596
15.2k
  bool error_occurred = false;
2597
15.2k
  if (matching) {
2598
15.2k
    char const* p;
2599
15.2k
    int ar;
2600
2601
15.2k
    while ((ar = archive_match_path_unmatched_inclusions_next(matching, &p)) ==
2602
15.2k
           ARCHIVE_OK) {
2603
0
      cmSystemTools::Error("tar: " + std::string(p) +
2604
0
                           ": Not found in archive");
2605
0
      error_occurred = true;
2606
0
    }
2607
15.2k
    if (error_occurred) {
2608
0
      return false;
2609
0
    }
2610
15.2k
    if (ar == ARCHIVE_FATAL) {
2611
0
      cmSystemTools::Error("tar: Out of memory");
2612
0
      return false;
2613
0
    }
2614
15.2k
  }
2615
15.2k
  archive_match_free(matching);
2616
15.2k
  archive_write_free(ext);
2617
15.2k
  archive_read_close(a);
2618
15.2k
  archive_read_free(a);
2619
15.2k
  return r == ARCHIVE_EOF || r == ARCHIVE_OK;
2620
15.2k
}
2621
}
2622
#endif
2623
2624
bool cmSystemTools::ExtractTar(std::string const& arFileName,
2625
                               std::vector<std::string> const& files,
2626
                               cmTarExtractTimestamps extractTimestamps,
2627
                               std::string const& encoding, bool verbose)
2628
32.3k
{
2629
32.3k
#if !defined(CMAKE_BOOTSTRAP)
2630
32.3k
  return extract_tar(arFileName, files, encoding, verbose, extractTimestamps,
2631
32.3k
                     true);
2632
#else
2633
  (void)arFileName;
2634
  (void)files;
2635
  (void)extractTimestamps;
2636
  (void)encoding;
2637
  (void)verbose;
2638
  return false;
2639
#endif
2640
32.3k
}
2641
2642
bool cmSystemTools::ListTar(std::string const& arFileName,
2643
                            std::vector<std::string> const& files,
2644
                            std::string const& encoding, bool verbose)
2645
0
{
2646
0
#if !defined(CMAKE_BOOTSTRAP)
2647
0
  return extract_tar(arFileName, files, encoding, verbose,
2648
0
                     cmTarExtractTimestamps::Yes, false);
2649
#else
2650
  (void)arFileName;
2651
  (void)files;
2652
  (void)encoding;
2653
  (void)verbose;
2654
  return false;
2655
#endif
2656
0
}
2657
2658
cmSystemTools::WaitForLineResult cmSystemTools::WaitForLine(
2659
  uv_loop_t* loop, uv_stream_t* outPipe, uv_stream_t* errPipe,
2660
  std::string& line, std::vector<char>& out, std::vector<char>& err)
2661
0
{
2662
0
  line.clear();
2663
0
  auto outiter = out.begin();
2664
0
  auto erriter = err.begin();
2665
0
  cmProcessOutput processOutput;
2666
0
  std::string strdata;
2667
0
  while (true) {
2668
    // Check for a newline in stdout.
2669
0
    for (; outiter != out.end(); ++outiter) {
2670
0
      if ((*outiter == '\r') && ((outiter + 1) == out.end())) {
2671
0
        break;
2672
0
      }
2673
0
      if (*outiter == '\n' || *outiter == '\0') {
2674
0
        std::vector<char>::size_type length = outiter - out.begin();
2675
0
        if (length > 1 && *(outiter - 1) == '\r') {
2676
0
          --length;
2677
0
        }
2678
0
        if (length > 0) {
2679
0
          line.append(out.data(), length);
2680
0
        }
2681
0
        out.erase(out.begin(), outiter + 1);
2682
0
        return WaitForLineResult::STDOUT;
2683
0
      }
2684
0
    }
2685
2686
    // Check for a newline in stderr.
2687
0
    for (; erriter != err.end(); ++erriter) {
2688
0
      if ((*erriter == '\r') && ((erriter + 1) == err.end())) {
2689
0
        break;
2690
0
      }
2691
0
      if (*erriter == '\n' || *erriter == '\0') {
2692
0
        std::vector<char>::size_type length = erriter - err.begin();
2693
0
        if (length > 1 && *(erriter - 1) == '\r') {
2694
0
          --length;
2695
0
        }
2696
0
        if (length > 0) {
2697
0
          line.append(err.data(), length);
2698
0
        }
2699
0
        err.erase(err.begin(), erriter + 1);
2700
0
        return WaitForLineResult::STDERR;
2701
0
      }
2702
0
    }
2703
2704
    // No newlines found.  Wait for more data from the process.
2705
0
    struct ReadData
2706
0
    {
2707
0
      uv_stream_t* Stream;
2708
0
      std::vector<char> Buffer;
2709
0
      bool Read = false;
2710
0
      bool Finished = false;
2711
0
    };
2712
0
    auto startRead =
2713
0
      [](uv_stream_t* stream,
2714
0
         ReadData& data) -> std::unique_ptr<cmUVStreamReadHandle> {
2715
0
      data.Stream = stream;
2716
0
      return cmUVStreamRead(
2717
0
        stream,
2718
0
        [&data](std::vector<char> buf) {
2719
0
          data.Buffer = std::move(buf);
2720
0
          data.Read = true;
2721
0
          uv_read_stop(data.Stream);
2722
0
        },
2723
0
        [&data]() { data.Finished = true; });
2724
0
    };
2725
0
    ReadData outData;
2726
0
    auto outHandle = startRead(outPipe, outData);
2727
0
    ReadData errData;
2728
0
    auto errHandle = startRead(errPipe, errData);
2729
2730
0
    uv_run(loop, UV_RUN_ONCE);
2731
0
    if (outData.Read) {
2732
0
      processOutput.DecodeText(outData.Buffer.data(), outData.Buffer.size(),
2733
0
                               strdata, 1);
2734
      // Append to the stdout buffer.
2735
0
      std::vector<char>::size_type size = out.size();
2736
0
      cm::append(out, strdata);
2737
0
      outiter = out.begin() + size;
2738
0
    } else if (errData.Read) {
2739
0
      processOutput.DecodeText(errData.Buffer.data(), errData.Buffer.size(),
2740
0
                               strdata, 2);
2741
      // Append to the stderr buffer.
2742
0
      std::vector<char>::size_type size = err.size();
2743
0
      cm::append(err, strdata);
2744
0
      erriter = err.begin() + size;
2745
0
    } else if (outData.Finished && errData.Finished) {
2746
      // Both stdout and stderr pipes have broken.  Return leftover data.
2747
0
      processOutput.DecodeText(std::string(), strdata, 1);
2748
0
      if (!strdata.empty()) {
2749
0
        std::vector<char>::size_type size = out.size();
2750
0
        cm::append(out, strdata);
2751
0
        outiter = out.begin() + size;
2752
0
      }
2753
0
      processOutput.DecodeText(std::string(), strdata, 2);
2754
0
      if (!strdata.empty()) {
2755
0
        std::vector<char>::size_type size = err.size();
2756
0
        cm::append(err, strdata);
2757
0
        erriter = err.begin() + size;
2758
0
      }
2759
0
      if (!out.empty()) {
2760
0
        line.append(out.data(), outiter - out.begin());
2761
0
        out.erase(out.begin(), out.end());
2762
0
        return WaitForLineResult::STDOUT;
2763
0
      }
2764
0
      if (!err.empty()) {
2765
0
        line.append(err.data(), erriter - err.begin());
2766
0
        err.erase(err.begin(), err.end());
2767
0
        return WaitForLineResult::STDERR;
2768
0
      }
2769
0
      return WaitForLineResult::None;
2770
0
    }
2771
0
    if (!outData.Finished) {
2772
0
      uv_read_stop(outPipe);
2773
0
    }
2774
0
    if (!errData.Finished) {
2775
0
      uv_read_stop(errPipe);
2776
0
    }
2777
0
  }
2778
0
}
2779
2780
#ifdef _WIN32
2781
#  ifndef CRYPT_SILENT
2782
#    define CRYPT_SILENT 0x40 /* Not defined by VS 6 version of header.  */
2783
#  endif
2784
static int WinCryptRandom(void* data, size_t size)
2785
{
2786
  int result = 0;
2787
  HCRYPTPROV hProvider = 0;
2788
  if (CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL,
2789
                           CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
2790
    result = CryptGenRandom(hProvider, (DWORD)size, (BYTE*)data) ? 1 : 0;
2791
    CryptReleaseContext(hProvider, 0);
2792
  }
2793
  return result;
2794
}
2795
#endif
2796
2797
unsigned int cmSystemTools::RandomSeed()
2798
0
{
2799
#if defined(_WIN32) && !defined(__CYGWIN__)
2800
  unsigned int seed = 0;
2801
2802
  // Try using a real random source.
2803
  if (WinCryptRandom(&seed, sizeof(seed))) {
2804
    return seed;
2805
  }
2806
2807
  // Fall back to the time and pid.
2808
  FILETIME ft;
2809
  GetSystemTimeAsFileTime(&ft);
2810
  unsigned int t1 = static_cast<unsigned int>(ft.dwHighDateTime);
2811
  unsigned int t2 = static_cast<unsigned int>(ft.dwLowDateTime);
2812
  unsigned int pid = static_cast<unsigned int>(GetCurrentProcessId());
2813
  return t1 ^ t2 ^ pid;
2814
#else
2815
0
  union
2816
0
  {
2817
0
    unsigned int integer;
2818
0
    char bytes[sizeof(unsigned int)];
2819
0
  } seed;
2820
2821
  // Try using a real random source.
2822
0
  cmsys::ifstream fin;
2823
0
  fin.rdbuf()->pubsetbuf(nullptr, 0); // Unbuffered read.
2824
0
  fin.open("/dev/urandom");
2825
0
  if (fin.good() && fin.read(seed.bytes, sizeof(seed)) &&
2826
0
      fin.gcount() == sizeof(seed)) {
2827
0
    return seed.integer;
2828
0
  }
2829
2830
  // Fall back to the time and pid.
2831
0
  struct timeval t;
2832
0
  gettimeofday(&t, nullptr);
2833
0
  unsigned int pid = static_cast<unsigned int>(getpid());
2834
0
  unsigned int tv_sec = static_cast<unsigned int>(t.tv_sec);
2835
0
  unsigned int tv_usec = static_cast<unsigned int>(t.tv_usec);
2836
  // Since tv_usec never fills more than 11 bits we shift it to fill
2837
  // in the slow-changing high-order bits of tv_sec.
2838
0
  return tv_sec ^ (tv_usec << 21) ^ pid;
2839
0
#endif
2840
0
}
2841
2842
unsigned int cmSystemTools::RandomNumber()
2843
0
{
2844
#ifndef CM_HAVE_THREAD_LOCAL
2845
  static std::mutex gen_mutex;
2846
  std::lock_guard<std::mutex> gen_mutex_lock(gen_mutex);
2847
#else
2848
0
  thread_local
2849
0
#endif
2850
0
  static std::mt19937 gen{ cmSystemTools::RandomSeed() };
2851
0
  return static_cast<unsigned int>(gen());
2852
0
}
2853
2854
std::string cmSystemTools::FindProgram(std::string const& name,
2855
                                       std::vector<std::string> const& path)
2856
0
{
2857
0
  std::string exe = cmsys::SystemTools::FindProgram(name, path);
2858
0
  if (!exe.empty()) {
2859
0
    exe = cmSystemTools::ToNormalizedPathOnDisk(std::move(exe));
2860
0
  }
2861
0
  return exe;
2862
0
}
2863
2864
namespace {
2865
std::string InitLogicalWorkingDirectory()
2866
10
{
2867
10
  std::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
2868
10
  std::string pwd;
2869
10
  if (cmSystemTools::GetEnv("PWD", pwd) &&
2870
10
      cmSystemTools::FileIsFullPath(pwd)) {
2871
10
    std::string const pwd_real = cmSystemTools::GetRealPath(pwd);
2872
10
    if (pwd_real == cwd) {
2873
10
      cwd = cmSystemTools::ToNormalizedPathOnDisk(std::move(pwd));
2874
10
    }
2875
10
  }
2876
10
  return cwd;
2877
10
}
2878
2879
std::string cmSystemToolsLogicalWorkingDirectory =
2880
  InitLogicalWorkingDirectory();
2881
2882
std::string cmSystemToolsCMakeCommand;
2883
std::string cmSystemToolsCTestCommand;
2884
std::string cmSystemToolsCPackCommand;
2885
std::string cmSystemToolsCMakeCursesCommand;
2886
std::string cmSystemToolsCMakeGUICommand;
2887
std::string cmSystemToolsCMClDepsCommand;
2888
std::string cmSystemToolsCMakeRoot;
2889
std::string cmSystemToolsHTMLDoc;
2890
2891
#if defined(__APPLE__)
2892
bool IsCMakeAppBundleExe(std::string const& exe)
2893
{
2894
  return cmHasLiteralSuffix(cmSystemTools::LowerCase(exe), "/macos/cmake");
2895
}
2896
#endif
2897
2898
std::string FindOwnExecutable(char const* argv0)
2899
0
{
2900
#if defined(_WIN32) && !defined(__CYGWIN__)
2901
  static_cast<void>(argv0);
2902
  wchar_t modulepath[_MAX_PATH];
2903
  ::GetModuleFileNameW(nullptr, modulepath, sizeof(modulepath));
2904
  std::string exe = cmsys::Encoding::ToNarrow(modulepath);
2905
#elif defined(__APPLE__)
2906
  static_cast<void>(argv0);
2907
#  define CM_EXE_PATH_LOCAL_SIZE 16384
2908
  char exe_path_local[CM_EXE_PATH_LOCAL_SIZE];
2909
#  if defined(MAC_OS_X_VERSION_10_3) && !defined(MAC_OS_X_VERSION_10_4)
2910
  unsigned long exe_path_size = CM_EXE_PATH_LOCAL_SIZE;
2911
#  else
2912
  uint32_t exe_path_size = CM_EXE_PATH_LOCAL_SIZE;
2913
#  endif
2914
#  undef CM_EXE_PATH_LOCAL_SIZE
2915
  char* exe_path = exe_path_local;
2916
  if (_NSGetExecutablePath(exe_path, &exe_path_size) < 0) {
2917
    exe_path = static_cast<char*>(malloc(exe_path_size));
2918
    _NSGetExecutablePath(exe_path, &exe_path_size);
2919
  }
2920
  std::string exe = exe_path;
2921
  if (exe_path != exe_path_local) {
2922
    free(exe_path);
2923
  }
2924
  if (IsCMakeAppBundleExe(exe)) {
2925
    // The executable is inside an application bundle.
2926
    // The install tree has "..<CMAKE_BIN_DIR>/cmake-gui".
2927
    // The build tree has '../../../cmake-gui".
2928
    std::string dir = cmSystemTools::GetFilenamePath(exe);
2929
    dir = cmSystemTools::GetFilenamePath(dir);
2930
    exe = cmStrCat(dir, CMAKE_BIN_DIR "/cmake-gui");
2931
    if (!cmSystemTools::PathExists(exe)) {
2932
      dir = cmSystemTools::GetFilenamePath(dir);
2933
      dir = cmSystemTools::GetFilenamePath(dir);
2934
      exe = cmStrCat(dir, "/cmake-gui");
2935
    }
2936
  }
2937
#else
2938
0
  std::string exe = cmsys::SystemTools::FindProgram(argv0);
2939
0
#endif
2940
0
  exe = cmSystemTools::ToNormalizedPathOnDisk(std::move(exe));
2941
0
  return exe;
2942
0
}
2943
2944
#ifndef CMAKE_BOOTSTRAP
2945
bool ResolveSymlinkToOwnExecutable(std::string& exe, std::string& exe_dir)
2946
0
{
2947
0
  std::string linked_exe;
2948
0
  if (!cmSystemTools::ReadSymlink(exe, linked_exe)) {
2949
0
    return false;
2950
0
  }
2951
#  if defined(__APPLE__)
2952
  // Ignore "cmake-gui -> ../MacOS/CMake".
2953
  if (IsCMakeAppBundleExe(linked_exe)) {
2954
    return false;
2955
  }
2956
#  endif
2957
0
  if (cmSystemTools::FileIsFullPath(linked_exe)) {
2958
0
    exe = std::move(linked_exe);
2959
0
  } else {
2960
0
    exe = cmStrCat(exe_dir, '/', std::move(linked_exe));
2961
0
  }
2962
0
  exe = cmSystemTools::ToNormalizedPathOnDisk(std::move(exe));
2963
0
  exe_dir = cmSystemTools::GetFilenamePath(exe);
2964
0
  return true;
2965
0
}
2966
2967
bool FindCMakeResourcesInInstallTree(std::string const& exe_dir)
2968
0
{
2969
  // Install tree has
2970
  // - "<prefix><CMAKE_BIN_DIR>/cmake"
2971
  // - "<prefix><CMAKE_DATA_DIR>"
2972
  // - "<prefix><CMAKE_DOC_DIR>"
2973
0
  if (cmHasLiteralSuffix(exe_dir, CMAKE_BIN_DIR)) {
2974
0
    std::string const prefix =
2975
0
      exe_dir.substr(0, exe_dir.size() - cmStrLen(CMAKE_BIN_DIR));
2976
    // Set cmSystemToolsCMakeRoot set to the location expected in an
2977
    // install tree, even if it does not exist, so that
2978
    // cmake::AddCMakePaths can print the location in its error message.
2979
0
    cmSystemToolsCMakeRoot = cmStrCat(prefix, CMAKE_DATA_DIR);
2980
0
    if (cmSystemTools::FileExists(
2981
0
          cmStrCat(cmSystemToolsCMakeRoot, "/Modules/CMake.cmake"))) {
2982
0
      if (cmSystemTools::FileExists(
2983
0
            cmStrCat(prefix, CMAKE_DOC_DIR "/html/index.html"))) {
2984
0
        cmSystemToolsHTMLDoc = cmStrCat(prefix, CMAKE_DOC_DIR "/html");
2985
0
      }
2986
0
      return true;
2987
0
    }
2988
0
  }
2989
0
  return false;
2990
0
}
2991
2992
void FindCMakeResourcesInBuildTree(std::string const& exe_dir)
2993
0
{
2994
  // Build tree has "<build>/bin[/<config>]/cmake" and
2995
  // "<build>/CMakeFiles/CMakeSourceDir.txt".
2996
0
  std::string dir = cmSystemTools::GetFilenamePath(exe_dir);
2997
0
  std::string src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt");
2998
0
  cmsys::ifstream fin(src_dir_txt.c_str());
2999
0
  std::string src_dir;
3000
0
  if (fin && cmSystemTools::GetLineFromStream(fin, src_dir) &&
3001
0
      cmSystemTools::FileIsDirectory(src_dir)) {
3002
0
    cmSystemToolsCMakeRoot = src_dir;
3003
0
  } else {
3004
0
    dir = cmSystemTools::GetFilenamePath(dir);
3005
0
    src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt");
3006
0
    cmsys::ifstream fin2(src_dir_txt.c_str());
3007
0
    if (fin2 && cmSystemTools::GetLineFromStream(fin2, src_dir) &&
3008
0
        cmSystemTools::FileIsDirectory(src_dir)) {
3009
0
      cmSystemToolsCMakeRoot = src_dir;
3010
0
    }
3011
0
  }
3012
0
  if (!cmSystemToolsCMakeRoot.empty() && cmSystemToolsHTMLDoc.empty() &&
3013
0
      cmSystemTools::FileExists(
3014
0
        cmStrCat(dir, "/Utilities/Sphinx/html/index.html"))) {
3015
0
    cmSystemToolsHTMLDoc = cmStrCat(dir, "/Utilities/Sphinx/html");
3016
0
  }
3017
0
}
3018
#endif
3019
}
3020
3021
void cmSystemTools::FindCMakeResources(char const* argv0)
3022
0
{
3023
0
  std::string exe = FindOwnExecutable(argv0);
3024
#ifdef CMAKE_BOOTSTRAP
3025
  // The bootstrap cmake knows its resource locations.
3026
  cmSystemToolsCMakeRoot = CMAKE_BOOTSTRAP_SOURCE_DIR;
3027
  cmSystemToolsCMakeCommand = exe;
3028
  // The bootstrap cmake does not provide the other tools,
3029
  // so use the directory where they are about to be built.
3030
  std::string exe_dir = CMAKE_BOOTSTRAP_BINARY_DIR "/bin";
3031
#else
3032
  // Find resources relative to our own executable.
3033
0
  std::string exe_dir = cmSystemTools::GetFilenamePath(exe);
3034
0
  bool found = false;
3035
  // When running through a symlink to our own executable,
3036
  // preserve symlinks in directory components if possible.
3037
0
  do {
3038
0
    found = FindCMakeResourcesInInstallTree(exe_dir);
3039
0
  } while (!found && ResolveSymlinkToOwnExecutable(exe, exe_dir));
3040
  // If we have not yet found the resources, the above loop will
3041
  // have left 'exe' referring to a real file, not a symlink, so
3042
  // all our binaries should exist under 'exe_dir'.  However, the
3043
  // resources may be discoverable only in the real path.
3044
0
  if (!found) {
3045
0
    found =
3046
0
      FindCMakeResourcesInInstallTree(cmSystemTools::GetRealPath(exe_dir));
3047
0
  }
3048
0
  if (!found) {
3049
0
    FindCMakeResourcesInBuildTree(exe_dir);
3050
0
  }
3051
0
  cmSystemToolsCMakeCommand =
3052
0
    cmStrCat(exe_dir, "/cmake", cmSystemTools::GetExecutableExtension());
3053
0
#endif
3054
0
  cmSystemToolsCTestCommand =
3055
0
    cmStrCat(exe_dir, "/ctest", cmSystemTools::GetExecutableExtension());
3056
0
  cmSystemToolsCPackCommand =
3057
0
    cmStrCat(exe_dir, "/cpack", cmSystemTools::GetExecutableExtension());
3058
0
  cmSystemToolsCMakeGUICommand =
3059
0
    cmStrCat(exe_dir, "/cmake-gui", cmSystemTools::GetExecutableExtension());
3060
0
  if (!cmSystemTools::FileExists(cmSystemToolsCMakeGUICommand)) {
3061
0
    cmSystemToolsCMakeGUICommand.clear();
3062
0
  }
3063
0
  cmSystemToolsCMakeCursesCommand =
3064
0
    cmStrCat(exe_dir, "/ccmake", cmSystemTools::GetExecutableExtension());
3065
0
  if (!cmSystemTools::FileExists(cmSystemToolsCMakeCursesCommand)) {
3066
0
    cmSystemToolsCMakeCursesCommand.clear();
3067
0
  }
3068
0
  cmSystemToolsCMClDepsCommand =
3069
0
    cmStrCat(exe_dir, "/cmcldeps", cmSystemTools::GetExecutableExtension());
3070
0
  if (!cmSystemTools::FileExists(cmSystemToolsCMClDepsCommand)) {
3071
0
    cmSystemToolsCMClDepsCommand.clear();
3072
0
  }
3073
0
}
3074
3075
std::string const& cmSystemTools::GetCMakeCommand()
3076
1
{
3077
1
  return cmSystemToolsCMakeCommand;
3078
1
}
3079
3080
std::string const& cmSystemTools::GetCTestCommand()
3081
1
{
3082
1
  return cmSystemToolsCTestCommand;
3083
1
}
3084
3085
std::string const& cmSystemTools::GetCPackCommand()
3086
1
{
3087
1
  return cmSystemToolsCPackCommand;
3088
1
}
3089
3090
std::string const& cmSystemTools::GetCMakeCursesCommand()
3091
0
{
3092
0
  return cmSystemToolsCMakeCursesCommand;
3093
0
}
3094
3095
std::string const& cmSystemTools::GetCMakeGUICommand()
3096
0
{
3097
0
  return cmSystemToolsCMakeGUICommand;
3098
0
}
3099
3100
std::string const& cmSystemTools::GetCMClDepsCommand()
3101
0
{
3102
0
  return cmSystemToolsCMClDepsCommand;
3103
0
}
3104
3105
std::string const& cmSystemTools::GetCMakeRoot()
3106
2
{
3107
2
  return cmSystemToolsCMakeRoot;
3108
2
}
3109
3110
std::string const& cmSystemTools::GetHTMLDoc()
3111
0
{
3112
0
  return cmSystemToolsHTMLDoc;
3113
0
}
3114
3115
cm::optional<std::string> cmSystemTools::GetSystemConfigDirectory()
3116
0
{
3117
#if defined(_WIN32)
3118
  LPWSTR lpwstr;
3119
  if (FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &lpwstr))) {
3120
    return cm::nullopt;
3121
  }
3122
  std::wstring wstr = std::wstring(lpwstr);
3123
  CoTaskMemFree(lpwstr);
3124
  std::string config = cmsys::Encoding::ToNarrow(wstr);
3125
  cmSystemTools::ConvertToUnixSlashes(config);
3126
  return config;
3127
#else
3128
0
  auto config = cmSystemTools::GetEnvVar("XDG_CONFIG_HOME");
3129
0
  if (!config.has_value()) {
3130
0
    config = cmSystemTools::GetEnvVar("HOME");
3131
0
    if (config.has_value()) {
3132
#  if defined(__APPLE__)
3133
      config = cmStrCat(config.value(), "/Library/Application Support");
3134
#  else
3135
0
      config = cmStrCat(config.value(), "/.config");
3136
0
#  endif
3137
0
    }
3138
0
  }
3139
0
  return config;
3140
0
#endif
3141
0
}
3142
3143
cm::optional<std::string> cmSystemTools::GetCMakeConfigDirectory()
3144
0
{
3145
0
  auto config = cmSystemTools::GetEnvVar("CMAKE_CONFIG_DIR");
3146
0
  if (!config.has_value()) {
3147
0
    config = cmSystemTools::GetSystemConfigDirectory();
3148
0
    if (config.has_value()) {
3149
#if defined(_WIN32) || defined(__APPLE__)
3150
      config = cmStrCat(config.value(), "/CMake");
3151
#else
3152
0
      config = cmStrCat(config.value(), "/cmake");
3153
0
#endif
3154
0
    }
3155
0
  }
3156
0
  return config;
3157
0
}
3158
3159
std::string const& cmSystemTools::GetLogicalWorkingDirectory()
3160
37
{
3161
37
  return cmSystemToolsLogicalWorkingDirectory;
3162
37
}
3163
3164
cmsys::Status cmSystemTools::SetLogicalWorkingDirectory(std::string const& lwd)
3165
0
{
3166
0
  cmsys::Status status = cmSystemTools::ChangeDirectory(lwd);
3167
0
  if (status) {
3168
0
    cmSystemToolsLogicalWorkingDirectory = lwd;
3169
0
  }
3170
0
  return status;
3171
0
}
3172
3173
bool cmSystemTools::GuessLibrarySOName(std::string const& fullPath,
3174
                                       std::string& soname)
3175
0
{
3176
  // For ELF shared libraries use a real parser to get the correct
3177
  // soname.
3178
0
  cmELF elf(fullPath.c_str());
3179
0
  if (elf) {
3180
0
    return elf.GetSOName(soname);
3181
0
  }
3182
3183
  // If the file is not a symlink we have no guess for its soname.
3184
0
  if (!cmSystemTools::FileIsSymlink(fullPath)) {
3185
0
    return false;
3186
0
  }
3187
0
  if (!cmSystemTools::ReadSymlink(fullPath, soname)) {
3188
0
    return false;
3189
0
  }
3190
3191
  // If the symlink has a path component we have no guess for the soname.
3192
0
  if (!cmSystemTools::GetFilenamePath(soname).empty()) {
3193
0
    return false;
3194
0
  }
3195
3196
  // If the symlink points at an extended version of the same name
3197
  // assume it is the soname.
3198
0
  std::string name = cmSystemTools::GetFilenameName(fullPath);
3199
0
  return soname.length() > name.length() &&
3200
0
    soname.compare(0, name.length(), name) == 0;
3201
0
}
3202
3203
bool cmSystemTools::GuessLibraryInstallName(std::string const& fullPath,
3204
                                            std::string& soname)
3205
0
{
3206
#if defined(CMake_USE_MACH_PARSER)
3207
  cmMachO macho(fullPath.c_str());
3208
  if (macho) {
3209
    return macho.GetInstallName(soname);
3210
  }
3211
#else
3212
0
  (void)fullPath;
3213
0
  (void)soname;
3214
0
#endif
3215
3216
0
  return false;
3217
0
}
3218
3219
static std::string::size_type cmSystemToolsFindRPath(cm::string_view have,
3220
                                                     cm::string_view want)
3221
0
{
3222
0
  std::string::size_type pos = 0;
3223
0
  while (pos < have.size()) {
3224
    // Look for an occurrence of the string.
3225
0
    std::string::size_type const beg = have.find(want, pos);
3226
0
    if (beg == std::string::npos) {
3227
0
      return std::string::npos;
3228
0
    }
3229
3230
    // Make sure it is separated from preceding entries.
3231
0
    if (beg > 0 && have[beg - 1] != ':') {
3232
0
      pos = beg + 1;
3233
0
      continue;
3234
0
    }
3235
3236
    // Make sure it is separated from following entries.
3237
0
    std::string::size_type const end = beg + want.size();
3238
0
    if (end < have.size() && have[end] != ':') {
3239
0
      pos = beg + 1;
3240
0
      continue;
3241
0
    }
3242
3243
    // Return the position of the path portion.
3244
0
    return beg;
3245
0
  }
3246
3247
  // The desired rpath was not found.
3248
0
  return std::string::npos;
3249
0
}
3250
3251
namespace {
3252
struct cmSystemToolsRPathInfo
3253
{
3254
  unsigned long Position;
3255
  unsigned long Size;
3256
  std::string Name;
3257
  std::string Value;
3258
};
3259
3260
using EmptyCallback = std::function<bool(std::string*, cmELF const&)>;
3261
using AdjustCallback = std::function<bool(
3262
  cm::optional<std::string>&, std::string const&, char const*, std::string*)>;
3263
3264
cm::optional<bool> AdjustRPathELF(std::string const& file,
3265
                                  EmptyCallback const& emptyCallback,
3266
                                  AdjustCallback const& adjustCallback,
3267
                                  std::string* emsg, bool* changed)
3268
0
{
3269
0
  if (changed) {
3270
0
    *changed = false;
3271
0
  }
3272
0
  int rp_count = 0;
3273
0
  bool remove_rpath = true;
3274
0
  cmSystemToolsRPathInfo rp[2];
3275
0
  {
3276
    // Parse the ELF binary.
3277
0
    cmELF elf(file.c_str());
3278
0
    if (!elf) {
3279
0
      return cm::nullopt; // Not a valid ELF file.
3280
0
    }
3281
3282
0
    if (!elf.HasDynamicSection()) {
3283
0
      return true; // No dynamic section to update.
3284
0
    }
3285
3286
    // Get the RPATH and RUNPATH entries from it.
3287
0
    int se_count = 0;
3288
0
    cmELF::StringEntry const* se[2] = { nullptr, nullptr };
3289
0
    char const* se_name[2] = { nullptr, nullptr };
3290
0
    if (cmELF::StringEntry const* se_rpath = elf.GetRPath()) {
3291
0
      se[se_count] = se_rpath;
3292
0
      se_name[se_count] = "RPATH";
3293
0
      ++se_count;
3294
0
    }
3295
0
    if (cmELF::StringEntry const* se_runpath = elf.GetRunPath()) {
3296
0
      se[se_count] = se_runpath;
3297
0
      se_name[se_count] = "RUNPATH";
3298
0
      ++se_count;
3299
0
    }
3300
0
    if (se_count == 0) {
3301
0
      return emptyCallback(emsg, elf);
3302
0
    }
3303
3304
0
    for (int i = 0; i < se_count; ++i) {
3305
      // If both RPATH and RUNPATH refer to the same string literal it
3306
      // needs to be changed only once.
3307
0
      if (rp_count && rp[0].Position == se[i]->Position) {
3308
0
        continue;
3309
0
      }
3310
3311
      // Store information about the entry in the file.
3312
0
      rp[rp_count].Position = se[i]->Position;
3313
0
      rp[rp_count].Size = se[i]->Size;
3314
0
      rp[rp_count].Name = se_name[i];
3315
3316
      // Adjust the rpath.
3317
0
      cm::optional<std::string> outRPath;
3318
0
      if (!adjustCallback(outRPath, se[i]->Value, se_name[i], emsg)) {
3319
0
        return false;
3320
0
      }
3321
3322
0
      if (outRPath) {
3323
0
        if (!outRPath->empty()) {
3324
0
          remove_rpath = false;
3325
0
        }
3326
3327
        // Make sure there is enough room to store the new rpath and at
3328
        // least one null terminator.
3329
0
        if (rp[rp_count].Size < outRPath->length() + 1) {
3330
0
          if (emsg) {
3331
0
            *emsg = cmStrCat("The replacement path is too long for the ",
3332
0
                             se_name[i], " entry.");
3333
0
          }
3334
0
          return false;
3335
0
        }
3336
3337
        // This entry is ready for update.
3338
0
        rp[rp_count].Value = std::move(*outRPath);
3339
0
        ++rp_count;
3340
0
      } else {
3341
0
        remove_rpath = false;
3342
0
      }
3343
0
    }
3344
0
  }
3345
3346
  // If no runtime path needs to be changed, we are done.
3347
0
  if (rp_count == 0) {
3348
0
    return true;
3349
0
  }
3350
3351
  // If the resulting rpath is empty, just remove the entire entry instead.
3352
0
  if (remove_rpath) {
3353
0
    return cmSystemTools::RemoveRPath(file, emsg, changed);
3354
0
  }
3355
3356
0
  FileModeGuard file_mode_guard(file, emsg);
3357
0
  if (file_mode_guard.HasErrors()) {
3358
0
    return false;
3359
0
  }
3360
3361
0
  {
3362
    // Open the file for update.
3363
0
    cmsys::ofstream f(file.c_str(),
3364
0
                      std::ios::in | std::ios::out | std::ios::binary);
3365
0
    if (!f) {
3366
0
      if (emsg) {
3367
0
        *emsg = "Error opening file for update.";
3368
0
      }
3369
0
      return false;
3370
0
    }
3371
3372
    // Store the new RPATH and RUNPATH strings.
3373
0
    for (int i = 0; i < rp_count; ++i) {
3374
      // Seek to the RPATH position.
3375
0
      if (!f.seekp(rp[i].Position)) {
3376
0
        if (emsg) {
3377
0
          *emsg = cmStrCat("Error seeking to ", rp[i].Name, " position.");
3378
0
        }
3379
0
        return false;
3380
0
      }
3381
3382
      // Write the new rpath.  Follow it with enough null terminators to
3383
      // fill the string table entry.
3384
0
      f << rp[i].Value;
3385
0
      for (unsigned long j = rp[i].Value.length(); j < rp[i].Size; ++j) {
3386
0
        f << '\0';
3387
0
      }
3388
3389
      // Make sure it wrote correctly.
3390
0
      if (!f) {
3391
0
        if (emsg) {
3392
0
          *emsg = cmStrCat("Error writing the new ", rp[i].Name,
3393
0
                           " string to the file.");
3394
0
        }
3395
0
        return false;
3396
0
      }
3397
0
    }
3398
0
  }
3399
3400
0
  if (!file_mode_guard.Restore(emsg)) {
3401
0
    return false;
3402
0
  }
3403
3404
  // Everything was updated successfully.
3405
0
  if (changed) {
3406
0
    *changed = true;
3407
0
  }
3408
0
  return true;
3409
0
}
3410
3411
std::function<bool(std::string*, cmELF const&)> MakeEmptyCallback(
3412
  std::string const& newRPath)
3413
0
{
3414
0
  return [newRPath](std::string* emsg, cmELF const& elf) -> bool {
3415
0
    if (newRPath.empty()) {
3416
      // The new rpath is empty and there is no rpath anyway so it is
3417
      // okay.
3418
0
      return true;
3419
0
    }
3420
0
    if (emsg) {
3421
0
      *emsg =
3422
0
        cmStrCat("No valid ELF RPATH or RUNPATH entry exists in the file; ",
3423
0
                 elf.GetErrorMessage());
3424
0
    }
3425
0
    return false;
3426
0
  };
3427
0
}
3428
}
3429
3430
static cm::optional<bool> ChangeRPathELF(std::string const& file,
3431
                                         std::string const& oldRPath,
3432
                                         std::string const& newRPath,
3433
                                         bool removeEnvironmentRPath,
3434
                                         std::string* emsg, bool* changed)
3435
0
{
3436
0
  auto adjustCallback = [oldRPath, newRPath, removeEnvironmentRPath](
3437
0
                          cm::optional<std::string>& outRPath,
3438
0
                          std::string const& inRPath, char const* se_name,
3439
0
                          std::string* emsg2) -> bool {
3440
    // Make sure the current rpath contains the old rpath.
3441
0
    std::string::size_type pos = cmSystemToolsFindRPath(inRPath, oldRPath);
3442
0
    if (pos == std::string::npos) {
3443
      // If it contains the new rpath instead then it is okay.
3444
0
      if (cmSystemToolsFindRPath(inRPath, newRPath) != std::string::npos) {
3445
0
        return true;
3446
0
      }
3447
0
      if (emsg2) {
3448
0
        std::ostringstream e;
3449
        /* clang-format off */
3450
0
        e << "The current " << se_name << " is:\n"
3451
0
             "  " << inRPath << "\n"
3452
0
             "which does not contain:\n"
3453
0
             "  " << oldRPath << "\n"
3454
0
             "as was expected.";
3455
        /* clang-format on */
3456
0
        *emsg2 = e.str();
3457
0
      }
3458
0
      return false;
3459
0
    }
3460
3461
0
    std::string::size_type prefix_len = pos;
3462
3463
    // If oldRPath was at the end of the file's RPath, and newRPath is empty,
3464
    // we should remove the unnecessary ':' at the end.
3465
0
    if (newRPath.empty() && pos > 0 && inRPath[pos - 1] == ':' &&
3466
0
        pos + oldRPath.length() == inRPath.length()) {
3467
0
      prefix_len--;
3468
0
    }
3469
3470
    // Construct the new value which preserves the part of the path
3471
    // not being changed.
3472
0
    outRPath.emplace();
3473
0
    if (!removeEnvironmentRPath) {
3474
0
      *outRPath += inRPath.substr(0, prefix_len);
3475
0
    }
3476
0
    *outRPath += newRPath;
3477
0
    *outRPath += inRPath.substr(pos + oldRPath.length());
3478
3479
0
    return true;
3480
0
  };
3481
3482
0
  return AdjustRPathELF(file, MakeEmptyCallback(newRPath), adjustCallback,
3483
0
                        emsg, changed);
3484
0
}
3485
3486
static cm::optional<bool> SetRPathELF(std::string const& file,
3487
                                      std::string const& newRPath,
3488
                                      std::string* emsg, bool* changed)
3489
0
{
3490
0
  auto adjustCallback = [newRPath](cm::optional<std::string>& outRPath,
3491
0
                                   std::string const& inRPath,
3492
0
                                   char const* /*se_name*/, std::string*
3493
0
                                   /*emsg*/) -> bool {
3494
0
    if (inRPath != newRPath) {
3495
0
      outRPath = newRPath;
3496
0
    }
3497
0
    return true;
3498
0
  };
3499
3500
0
  return AdjustRPathELF(file, MakeEmptyCallback(newRPath), adjustCallback,
3501
0
                        emsg, changed);
3502
0
}
3503
static cm::optional<bool> ChangeRPathXCOFF(std::string const& file,
3504
                                           std::string const& oldRPath,
3505
                                           std::string const& newRPath,
3506
                                           bool removeEnvironmentRPath,
3507
                                           std::string* emsg, bool* changed)
3508
0
{
3509
0
  if (changed) {
3510
0
    *changed = false;
3511
0
  }
3512
0
#if !defined(CMake_USE_XCOFF_PARSER)
3513
0
  (void)file;
3514
0
  (void)oldRPath;
3515
0
  (void)newRPath;
3516
0
  (void)removeEnvironmentRPath;
3517
0
  (void)emsg;
3518
0
  return cm::nullopt;
3519
#else
3520
  bool chg = false;
3521
  cmXCOFF xcoff(file.c_str(), cmXCOFF::Mode::ReadWrite);
3522
  if (!xcoff) {
3523
    return cm::nullopt; // Not a valid XCOFF file
3524
  }
3525
  if (cm::optional<cm::string_view> maybeLibPath = xcoff.GetLibPath()) {
3526
    cm::string_view libPath = *maybeLibPath;
3527
    // Make sure the current rpath contains the old rpath.
3528
    std::string::size_type pos = cmSystemToolsFindRPath(libPath, oldRPath);
3529
    if (pos == std::string::npos) {
3530
      // If it contains the new rpath instead then it is okay.
3531
      if (cmSystemToolsFindRPath(libPath, newRPath) != std::string::npos) {
3532
        return true;
3533
      }
3534
      if (emsg) {
3535
        std::ostringstream e;
3536
        /* clang-format off */
3537
        e << "The current RPATH is:\n"
3538
             "  " << libPath << "\n"
3539
             "which does not contain:\n"
3540
             "  " << oldRPath << "\n"
3541
             "as was expected.";
3542
        /* clang-format on */
3543
        *emsg = e.str();
3544
      }
3545
      return false;
3546
    }
3547
3548
    // The prefix is either empty or ends in a ':'.
3549
    cm::string_view prefix = libPath.substr(0, pos);
3550
    if (newRPath.empty() && !prefix.empty()) {
3551
      prefix.remove_suffix(1);
3552
    }
3553
3554
    // The suffix is either empty or starts in a ':'.
3555
    cm::string_view suffix = libPath.substr(pos + oldRPath.length());
3556
3557
    // Construct the new value which preserves the part of the path
3558
    // not being changed.
3559
    std::string newLibPath;
3560
    if (!removeEnvironmentRPath) {
3561
      newLibPath = std::string(prefix);
3562
    }
3563
    newLibPath += newRPath;
3564
    newLibPath += suffix;
3565
3566
    chg = xcoff.SetLibPath(newLibPath);
3567
  }
3568
  if (!xcoff) {
3569
    if (emsg) {
3570
      *emsg = xcoff.GetErrorMessage();
3571
    }
3572
    return false;
3573
  }
3574
3575
  // Everything was updated successfully.
3576
  if (changed) {
3577
    *changed = chg;
3578
  }
3579
  return true;
3580
#endif
3581
0
}
3582
3583
static cm::optional<bool> SetRPathXCOFF(std::string const& /*file*/,
3584
                                        std::string const& /*newRPath*/,
3585
                                        std::string* /*emsg*/,
3586
                                        bool* /*changed*/)
3587
0
{
3588
0
  return cm::nullopt; // Not implemented.
3589
0
}
3590
3591
bool cmSystemTools::ChangeRPath(std::string const& file,
3592
                                std::string const& oldRPath,
3593
                                std::string const& newRPath,
3594
                                bool removeEnvironmentRPath, std::string* emsg,
3595
                                bool* changed)
3596
0
{
3597
0
  if (cm::optional<bool> result = ChangeRPathELF(
3598
0
        file, oldRPath, newRPath, removeEnvironmentRPath, emsg, changed)) {
3599
0
    return result.value();
3600
0
  }
3601
0
  if (cm::optional<bool> result = ChangeRPathXCOFF(
3602
0
        file, oldRPath, newRPath, removeEnvironmentRPath, emsg, changed)) {
3603
0
    return result.value();
3604
0
  }
3605
  // The file format is not recognized.  Assume it has no RPATH.
3606
0
  if (newRPath.empty()) {
3607
    // The caller wanted no RPATH anyway.
3608
0
    return true;
3609
0
  }
3610
0
  if (emsg) {
3611
0
    *emsg = "The file format is not recognized.";
3612
0
  }
3613
0
  return false;
3614
0
}
3615
3616
bool cmSystemTools::SetRPath(std::string const& file,
3617
                             std::string const& newRPath, std::string* emsg,
3618
                             bool* changed)
3619
0
{
3620
0
  if (cm::optional<bool> result = SetRPathELF(file, newRPath, emsg, changed)) {
3621
0
    return result.value();
3622
0
  }
3623
0
  if (cm::optional<bool> result =
3624
0
        SetRPathXCOFF(file, newRPath, emsg, changed)) {
3625
0
    return result.value();
3626
0
  }
3627
  // The file format is not recognized.  Assume it has no RPATH.
3628
0
  if (newRPath.empty()) {
3629
    // The caller wanted no RPATH anyway.
3630
0
    return true;
3631
0
  }
3632
0
  if (emsg) {
3633
0
    *emsg = "The file format is not recognized.";
3634
0
  }
3635
0
  return false;
3636
0
}
3637
3638
namespace {
3639
bool VersionCompare(cmSystemTools::CompareOp op, char const* lhss,
3640
                    char const* rhss)
3641
3.66k
{
3642
3.66k
  char const* endl = lhss;
3643
3.66k
  char const* endr = rhss;
3644
3645
21.3k
  while (((*endl >= '0') && (*endl <= '9')) ||
3646
20.5k
         ((*endr >= '0') && (*endr <= '9'))) {
3647
    // Do component-wise comparison, ignoring leading zeros
3648
    // (components are treated as integers, not as mantissas)
3649
45.3k
    while (*endl == '0') {
3650
24.7k
      endl++;
3651
24.7k
    }
3652
29.4k
    while (*endr == '0') {
3653
8.85k
      endr++;
3654
8.85k
    }
3655
3656
20.5k
    char const* beginl = endl;
3657
20.5k
    char const* beginr = endr;
3658
3659
    // count significant digits
3660
74.8k
    while ((*endl >= '0') && (*endl <= '9')) {
3661
54.2k
      endl++;
3662
54.2k
    }
3663
38.0k
    while ((*endr >= '0') && (*endr <= '9')) {
3664
17.4k
      endr++;
3665
17.4k
    }
3666
3667
    // compare number of digits first
3668
20.5k
    ptrdiff_t r = ((endl - beginl) - (endr - beginr));
3669
20.5k
    if (r == 0) {
3670
      // compare the digits if number of digits is equal
3671
18.3k
      r = strncmp(beginl, beginr, endl - beginl);
3672
18.3k
    }
3673
3674
20.5k
    if (r < 0) {
3675
      // lhs < rhs, so true if operation is LESS
3676
1.52k
      return (op & cmSystemTools::OP_LESS) != 0;
3677
1.52k
    }
3678
19.0k
    if (r > 0) {
3679
      // lhs > rhs, so true if operation is GREATER
3680
1.35k
      return (op & cmSystemTools::OP_GREATER) != 0;
3681
1.35k
    }
3682
3683
17.6k
    if (*endr == '.') {
3684
4.32k
      endr++;
3685
4.32k
    }
3686
3687
17.6k
    if (*endl == '.') {
3688
14.8k
      endl++;
3689
14.8k
    }
3690
17.6k
  }
3691
  // lhs == rhs, so true if operation is EQUAL
3692
789
  return (op & cmSystemTools::OP_EQUAL) != 0;
3693
3.66k
}
3694
}
3695
3696
bool cmSystemTools::VersionCompare(cmSystemTools::CompareOp op,
3697
                                   std::string const& lhs,
3698
                                   std::string const& rhs)
3699
1.89k
{
3700
1.89k
  return ::VersionCompare(op, lhs.c_str(), rhs.c_str());
3701
1.89k
}
3702
bool cmSystemTools::VersionCompare(cmSystemTools::CompareOp op,
3703
                                   std::string const& lhs, char const rhs[])
3704
1.77k
{
3705
1.77k
  return ::VersionCompare(op, lhs.c_str(), rhs);
3706
1.77k
}
3707
3708
bool cmSystemTools::VersionCompareEqual(std::string const& lhs,
3709
                                        std::string const& rhs)
3710
0
{
3711
0
  return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL, lhs, rhs);
3712
0
}
3713
3714
bool cmSystemTools::VersionCompareGreater(std::string const& lhs,
3715
                                          std::string const& rhs)
3716
355
{
3717
355
  return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER, lhs, rhs);
3718
355
}
3719
3720
bool cmSystemTools::VersionCompareGreaterEq(std::string const& lhs,
3721
                                            std::string const& rhs)
3722
355
{
3723
355
  return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL, lhs,
3724
355
                                       rhs);
3725
355
}
3726
3727
static size_t cm_strverscmp_find_first_difference_or_end(char const* lhs,
3728
                                                         char const* rhs)
3729
0
{
3730
0
  size_t i = 0;
3731
  /* Step forward until we find a difference or both strings end together.
3732
     The difference may lie on the null-terminator of one string.  */
3733
0
  while (lhs[i] == rhs[i] && lhs[i] != 0) {
3734
0
    ++i;
3735
0
  }
3736
0
  return i;
3737
0
}
3738
3739
static size_t cm_strverscmp_find_digits_begin(char const* s, size_t i)
3740
0
{
3741
  /* Step back until we are not preceded by a digit.  */
3742
0
  while (i > 0 && cmsysString_isdigit(s[i - 1])) {
3743
0
    --i;
3744
0
  }
3745
0
  return i;
3746
0
}
3747
3748
static size_t cm_strverscmp_find_digits_end(char const* s, size_t i)
3749
0
{
3750
  /* Step forward over digits.  */
3751
0
  while (cmsysString_isdigit(s[i])) {
3752
0
    ++i;
3753
0
  }
3754
0
  return i;
3755
0
}
3756
3757
static size_t cm_strverscmp_count_leading_zeros(char const* s, size_t b)
3758
0
{
3759
0
  size_t i = b;
3760
  /* Step forward over zeros that are followed by another digit.  */
3761
0
  while (s[i] == '0' && cmsysString_isdigit(s[i + 1])) {
3762
0
    ++i;
3763
0
  }
3764
0
  return i - b;
3765
0
}
3766
3767
static int cm_strverscmp(char const* lhs, char const* rhs)
3768
0
{
3769
0
  size_t const i = cm_strverscmp_find_first_difference_or_end(lhs, rhs);
3770
0
  if (lhs[i] != rhs[i]) {
3771
    /* The strings differ starting at 'i'.  Check for a digit sequence.  */
3772
0
    size_t const b = cm_strverscmp_find_digits_begin(lhs, i);
3773
0
    if (b != i ||
3774
0
        (cmsysString_isdigit(lhs[i]) && cmsysString_isdigit(rhs[i]))) {
3775
      /* A digit sequence starts at 'b', preceding or at 'i'.  */
3776
3777
      /* Look for leading zeros, implying a leading decimal point.  */
3778
0
      size_t const lhs_zeros = cm_strverscmp_count_leading_zeros(lhs, b);
3779
0
      size_t const rhs_zeros = cm_strverscmp_count_leading_zeros(rhs, b);
3780
0
      if (lhs_zeros != rhs_zeros) {
3781
        /* The side with more leading zeros orders first.  */
3782
0
        return rhs_zeros > lhs_zeros ? 1 : -1;
3783
0
      }
3784
0
      if (lhs_zeros == 0) {
3785
        /* No leading zeros; compare digit sequence lengths.  */
3786
0
        size_t const lhs_end = cm_strverscmp_find_digits_end(lhs, i);
3787
0
        size_t const rhs_end = cm_strverscmp_find_digits_end(rhs, i);
3788
0
        if (lhs_end != rhs_end) {
3789
          /* The side with fewer digits orders first.  */
3790
0
          return lhs_end > rhs_end ? 1 : -1;
3791
0
        }
3792
0
      }
3793
0
    }
3794
0
  }
3795
3796
  /* Ordering was not decided by digit sequence lengths; compare bytes.  */
3797
0
  return lhs[i] - rhs[i];
3798
0
}
3799
3800
int cmSystemTools::strverscmp(std::string const& lhs, std::string const& rhs)
3801
0
{
3802
0
  return cm_strverscmp(lhs.c_str(), rhs.c_str());
3803
0
}
3804
3805
static cm::optional<bool> RemoveRPathELF(std::string const& file,
3806
                                         std::string* emsg, bool* removed)
3807
0
{
3808
0
  if (removed) {
3809
0
    *removed = false;
3810
0
  }
3811
0
  int zeroCount = 0;
3812
0
  unsigned long zeroPosition[2] = { 0, 0 };
3813
0
  unsigned long zeroSize[2] = { 0, 0 };
3814
0
  unsigned long bytesBegin = 0;
3815
0
  std::vector<char> bytes;
3816
0
  {
3817
    // Parse the ELF binary.
3818
0
    cmELF elf(file.c_str());
3819
0
    if (!elf) {
3820
0
      return cm::nullopt; // Not a valid ELF file.
3821
0
    }
3822
3823
    // Get the RPATH and RUNPATH entries from it and sort them by index
3824
    // in the dynamic section header.
3825
0
    int se_count = 0;
3826
0
    cmELF::StringEntry const* se[2] = { nullptr, nullptr };
3827
0
    if (cmELF::StringEntry const* se_rpath = elf.GetRPath()) {
3828
0
      se[se_count++] = se_rpath;
3829
0
    }
3830
0
    if (cmELF::StringEntry const* se_runpath = elf.GetRunPath()) {
3831
0
      se[se_count++] = se_runpath;
3832
0
    }
3833
0
    if (se_count == 0) {
3834
      // There is no RPATH or RUNPATH anyway.
3835
0
      return true;
3836
0
    }
3837
0
    if (se_count == 2 && se[1]->IndexInSection < se[0]->IndexInSection) {
3838
0
      std::swap(se[0], se[1]);
3839
0
    }
3840
3841
    // Obtain a copy of the dynamic entries
3842
0
    cmELF::DynamicEntryList dentries = elf.GetDynamicEntries();
3843
0
    if (dentries.empty()) {
3844
      // This should happen only for invalid ELF files where a DT_NULL
3845
      // appears before the end of the table.
3846
0
      if (emsg) {
3847
0
        *emsg = "DYNAMIC section contains a DT_NULL before the end.";
3848
0
      }
3849
0
      return false;
3850
0
    }
3851
3852
    // Save information about the string entries to be zeroed.
3853
0
    zeroCount = se_count;
3854
0
    for (int i = 0; i < se_count; ++i) {
3855
0
      zeroPosition[i] = se[i]->Position;
3856
0
      zeroSize[i] = se[i]->Size;
3857
0
    }
3858
3859
    // Get size of one DYNAMIC entry
3860
0
    unsigned long const sizeof_dentry =
3861
0
      elf.GetDynamicEntryPosition(1) - elf.GetDynamicEntryPosition(0);
3862
3863
    // Adjust the entry list as necessary to remove the run path
3864
0
    unsigned long entriesErased = 0;
3865
0
    for (auto it = dentries.begin(); it != dentries.end();) {
3866
0
      if (it->first == cmELF::TagRPath || it->first == cmELF::TagRunPath) {
3867
0
        it = dentries.erase(it);
3868
0
        entriesErased++;
3869
0
        continue;
3870
0
      }
3871
0
      if (it->first == cmELF::TagMipsRldMapRel && elf.IsMIPS()) {
3872
        // Background: debuggers need to know the "linker map" which contains
3873
        // the addresses each dynamic object is loaded at. Most arches use
3874
        // the DT_DEBUG tag which the dynamic linker writes to (directly) and
3875
        // contain the location of the linker map, however on MIPS the
3876
        // .dynamic section is always read-only so this is not possible. MIPS
3877
        // objects instead contain a DT_MIPS_RLD_MAP tag which contains the
3878
        // address where the dynamic linker will write to (an indirect
3879
        // version of DT_DEBUG). Since this doesn't work when using PIE, a
3880
        // relative equivalent was created - DT_MIPS_RLD_MAP_REL. Since this
3881
        // version contains a relative offset, moving it changes the
3882
        // calculated address. This may cause the dynamic linker to write
3883
        // into memory it should not be changing.
3884
        //
3885
        // To fix this, we adjust the value of DT_MIPS_RLD_MAP_REL here. If
3886
        // we move it up by n bytes, we add n bytes to the value of this tag.
3887
0
        it->second += entriesErased * sizeof_dentry;
3888
0
      }
3889
3890
0
      it++;
3891
0
    }
3892
3893
    // Encode new entries list
3894
0
    bytes = elf.EncodeDynamicEntries(dentries);
3895
0
    bytesBegin = elf.GetDynamicEntryPosition(0);
3896
0
  }
3897
3898
0
  FileModeGuard file_mode_guard(file, emsg);
3899
0
  if (file_mode_guard.HasErrors()) {
3900
0
    return false;
3901
0
  }
3902
3903
  // Open the file for update.
3904
0
  cmsys::ofstream f(file.c_str(),
3905
0
                    std::ios::in | std::ios::out | std::ios::binary);
3906
0
  if (!f) {
3907
0
    if (emsg) {
3908
0
      *emsg = "Error opening file for update.";
3909
0
    }
3910
0
    return false;
3911
0
  }
3912
3913
  // Write the new DYNAMIC table header.
3914
0
  if (!f.seekp(bytesBegin)) {
3915
0
    if (emsg) {
3916
0
      *emsg = "Error seeking to DYNAMIC table header for RPATH.";
3917
0
    }
3918
0
    return false;
3919
0
  }
3920
0
  if (!f.write(bytes.data(), bytes.size())) {
3921
0
    if (emsg) {
3922
0
      *emsg = "Error replacing DYNAMIC table header.";
3923
0
    }
3924
0
    return false;
3925
0
  }
3926
3927
  // Fill the RPATH and RUNPATH strings with zero bytes.
3928
0
  for (int i = 0; i < zeroCount; ++i) {
3929
0
    if (!f.seekp(zeroPosition[i])) {
3930
0
      if (emsg) {
3931
0
        *emsg = "Error seeking to RPATH position.";
3932
0
      }
3933
0
      return false;
3934
0
    }
3935
0
    for (unsigned long j = 0; j < zeroSize[i]; ++j) {
3936
0
      f << '\0';
3937
0
    }
3938
0
    if (!f) {
3939
0
      if (emsg) {
3940
0
        *emsg = "Error writing the empty rpath string to the file.";
3941
0
      }
3942
0
      return false;
3943
0
    }
3944
0
  }
3945
3946
  // Close the handle to allow further operations on the file
3947
0
  f.close();
3948
3949
0
  if (!file_mode_guard.Restore(emsg)) {
3950
0
    return false;
3951
0
  }
3952
3953
  // Everything was updated successfully.
3954
0
  if (removed) {
3955
0
    *removed = true;
3956
0
  }
3957
0
  return true;
3958
0
}
3959
3960
static cm::optional<bool> RemoveRPathXCOFF(std::string const& file,
3961
                                           std::string* emsg, bool* removed)
3962
0
{
3963
0
  if (removed) {
3964
0
    *removed = false;
3965
0
  }
3966
0
#if !defined(CMake_USE_XCOFF_PARSER)
3967
0
  (void)file;
3968
0
  (void)emsg;
3969
0
  return cm::nullopt; // Cannot handle XCOFF files.
3970
#else
3971
  bool rm = false;
3972
3973
  FileModeGuard file_mode_guard(file, emsg);
3974
  if (file_mode_guard.HasErrors()) {
3975
    return false;
3976
  }
3977
3978
  {
3979
    cmXCOFF xcoff(file.c_str(), cmXCOFF::Mode::ReadWrite);
3980
    if (!xcoff) {
3981
      return cm::nullopt; // Not a valid XCOFF file.
3982
    }
3983
    rm = xcoff.RemoveLibPath();
3984
    if (!xcoff) {
3985
      if (emsg) {
3986
        *emsg = xcoff.GetErrorMessage();
3987
      }
3988
      return false;
3989
    }
3990
  }
3991
3992
  if (!file_mode_guard.Restore(emsg)) {
3993
    return false;
3994
  }
3995
3996
  if (removed) {
3997
    *removed = rm;
3998
  }
3999
  return true;
4000
#endif
4001
0
}
4002
bool cmSystemTools::RemoveRPath(std::string const& file, std::string* emsg,
4003
                                bool* removed)
4004
0
{
4005
0
  if (cm::optional<bool> result = RemoveRPathELF(file, emsg, removed)) {
4006
0
    return result.value();
4007
0
  }
4008
0
  if (cm::optional<bool> result = RemoveRPathXCOFF(file, emsg, removed)) {
4009
0
    return result.value();
4010
0
  }
4011
  // The file format is not recognized.  Assume it has no RPATH.
4012
0
  return true;
4013
0
}
4014
4015
bool cmSystemTools::CheckRPath(std::string const& file,
4016
                               std::string const& newRPath)
4017
0
{
4018
  // Parse the ELF binary.
4019
0
  cmELF elf(file.c_str());
4020
0
  if (elf) {
4021
    // Get the RPATH or RUNPATH entry from it.
4022
0
    cmELF::StringEntry const* se = elf.GetRPath();
4023
0
    if (!se) {
4024
0
      se = elf.GetRunPath();
4025
0
    }
4026
4027
    // Make sure the current rpath contains the new rpath.
4028
0
    if (newRPath.empty()) {
4029
0
      if (!se) {
4030
0
        return true;
4031
0
      }
4032
0
    } else {
4033
0
      if (se &&
4034
0
          cmSystemToolsFindRPath(se->Value, newRPath) != std::string::npos) {
4035
0
        return true;
4036
0
      }
4037
0
    }
4038
0
    return false;
4039
0
  }
4040
#if defined(CMake_USE_XCOFF_PARSER)
4041
  // Parse the XCOFF binary.
4042
  cmXCOFF xcoff(file.c_str());
4043
  if (xcoff) {
4044
    if (cm::optional<cm::string_view> libPath = xcoff.GetLibPath()) {
4045
      if (cmSystemToolsFindRPath(*libPath, newRPath) != std::string::npos) {
4046
        return true;
4047
      }
4048
    }
4049
    return false;
4050
  }
4051
#endif
4052
  // The file format is not recognized.  Assume it has no RPATH.
4053
  // Therefore we succeed if the new rpath is empty anyway.
4054
0
  return newRPath.empty();
4055
0
}
4056
4057
cmsys::Status cmSystemTools::RepeatedRemoveDirectory(std::string const& dir)
4058
0
{
4059
#ifdef _WIN32
4060
  // Windows sometimes locks files temporarily so try a few times.
4061
  WindowsFileRetry retry = cmSystemTools::GetWindowsFileRetry();
4062
4063
  cmsys::Status status;
4064
  unsigned int tries = 0;
4065
  while (!(status = cmSystemTools::RemoveADirectory(dir)) &&
4066
         ++tries < retry.Count) {
4067
    cmSystemTools::Delay(retry.Delay);
4068
  }
4069
  return status;
4070
#else
4071
0
  return cmSystemTools::RemoveADirectory(dir);
4072
0
#endif
4073
0
}
4074
4075
std::string cmSystemTools::EncodeURL(std::string const& in, bool escapeSlashes)
4076
0
{
4077
0
  std::string out;
4078
0
  for (char c : in) {
4079
0
    char hexCh[4] = { 0, 0, 0, 0 };
4080
0
    hexCh[0] = c;
4081
0
    switch (c) {
4082
0
      case '+':
4083
0
      case '?':
4084
0
      case '\\':
4085
0
      case '&':
4086
0
      case ' ':
4087
0
      case '=':
4088
0
      case '%':
4089
0
        snprintf(hexCh, sizeof(hexCh), "%%%02X", static_cast<unsigned int>(c));
4090
0
        break;
4091
0
      case '/':
4092
0
        if (escapeSlashes) {
4093
0
          strcpy(hexCh, "%2F");
4094
0
        }
4095
0
        break;
4096
0
      default:
4097
0
        break;
4098
0
    }
4099
0
    out.append(hexCh);
4100
0
  }
4101
0
  return out;
4102
0
}
4103
4104
cm::optional<cmSystemTools::DirCase> cmSystemTools::GetDirCase(
4105
  std::string const& dir)
4106
0
{
4107
0
  if (!cmSystemTools::FileIsDirectory(dir)) {
4108
0
    return cm::nullopt;
4109
0
  }
4110
#if defined(_WIN32) || defined(__APPLE__)
4111
  return DirCase::Insensitive;
4112
#elif defined(__linux__)
4113
0
  int fd = open(dir.c_str(), O_RDONLY);
4114
0
  if (fd == -1) {
4115
    // cannot open dir but it exists, assume dir is case sensitive.
4116
0
    return DirCase::Sensitive;
4117
0
  }
4118
0
  int attr = 0;
4119
0
  int ioctl_res = ioctl(fd, FS_IOC_GETFLAGS, &attr);
4120
0
  close(fd);
4121
4122
0
  if (ioctl_res == -1) {
4123
0
    return DirCase::Sensitive;
4124
0
  }
4125
4126
  // FS_CASEFOLD_FD from linux/fs.h, in Linux libc-dev 5.4+
4127
  // For compat with old libc-dev, define it here.
4128
0
  int const CMAKE_FS_CASEFOLD_FL = 0x40000000;
4129
0
  return (attr & CMAKE_FS_CASEFOLD_FL) != 0 ? DirCase::Insensitive
4130
0
                                            : DirCase::Sensitive;
4131
#else
4132
  return DirCase::Sensitive;
4133
#endif
4134
0
}
4135
4136
cmsys::Status cmSystemTools::CreateSymlink(std::string const& origName,
4137
                                           std::string const& newName)
4138
0
{
4139
0
  cmsys::Status status =
4140
0
    cmSystemTools::CreateSymlinkQuietly(origName, newName);
4141
0
  if (!status) {
4142
0
    cmSystemTools::Error(cmStrCat("failed to create symbolic link '", newName,
4143
0
                                  "': ", status.GetString()));
4144
0
  }
4145
0
  return status;
4146
0
}
4147
4148
cmsys::Status cmSystemTools::CreateSymlinkQuietly(std::string const& origName,
4149
                                                  std::string const& newName)
4150
0
{
4151
0
  uv_fs_t req;
4152
0
  int flags = 0;
4153
#if defined(_WIN32)
4154
  if (cmsys::SystemTools::FileIsDirectory(origName)) {
4155
    flags |= UV_FS_SYMLINK_DIR;
4156
  }
4157
#endif
4158
0
  int err = uv_fs_symlink(nullptr, &req, origName.c_str(), newName.c_str(),
4159
0
                          flags, nullptr);
4160
0
  cmsys::Status status;
4161
0
  if (err) {
4162
#if defined(_WIN32)
4163
    status = cmsys::Status::Windows(uv_fs_get_system_error(&req));
4164
#elif UV_VERSION_MAJOR > 1 || (UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR >= 38)
4165
    status = cmsys::Status::POSIX(uv_fs_get_system_error(&req));
4166
#else
4167
    status = cmsys::Status::POSIX(-err);
4168
#endif
4169
0
  }
4170
0
  return status;
4171
0
}
4172
4173
cmsys::Status cmSystemTools::CreateLink(std::string const& origName,
4174
                                        std::string const& newName)
4175
0
{
4176
0
  cmsys::Status status = cmSystemTools::CreateLinkQuietly(origName, newName);
4177
0
  if (!status) {
4178
0
    cmSystemTools::Error(
4179
0
      cmStrCat("failed to create link '", newName, "': ", status.GetString()));
4180
0
  }
4181
0
  return status;
4182
0
}
4183
4184
cmsys::Status cmSystemTools::CreateLinkQuietly(std::string const& origName,
4185
                                               std::string const& newName)
4186
0
{
4187
0
  uv_fs_t req;
4188
0
  int err =
4189
0
    uv_fs_link(nullptr, &req, origName.c_str(), newName.c_str(), nullptr);
4190
0
  cmsys::Status status;
4191
0
  if (err) {
4192
#if defined(_WIN32)
4193
    status = cmsys::Status::Windows(uv_fs_get_system_error(&req));
4194
#elif UV_VERSION_MAJOR > 1 || (UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR >= 38)
4195
    status = cmsys::Status::POSIX(uv_fs_get_system_error(&req));
4196
#else
4197
    status = cmsys::Status::POSIX(-err);
4198
#endif
4199
0
  }
4200
0
  return status;
4201
0
}
4202
4203
cm::string_view cmSystemTools::GetSystemName()
4204
1
{
4205
#if defined(_WIN32)
4206
  return "Windows";
4207
#elif defined(__MSYS__)
4208
  return "MSYS";
4209
#elif defined(__CYGWIN__)
4210
  return "CYGWIN";
4211
#elif defined(__ANDROID__)
4212
  return "Android";
4213
#else
4214
1
  static struct utsname uts_name;
4215
1
  static bool initialized = false;
4216
1
  static cm::string_view systemName;
4217
1
  if (initialized) {
4218
0
    return systemName;
4219
0
  }
4220
1
  if (uname(&uts_name) >= 0) {
4221
1
    initialized = true;
4222
1
    systemName = uts_name.sysname;
4223
4224
1
    if (cmIsOff(systemName)) {
4225
0
      systemName = "UnknownOS";
4226
0
    }
4227
4228
    // fix for BSD/OS, remove the /
4229
1
    static cmsys::RegularExpression const bsdOsRegex("BSD.OS");
4230
1
    cmsys::RegularExpressionMatch match;
4231
1
    if (bsdOsRegex.find(uts_name.sysname, match)) {
4232
0
      systemName = "BSDOS";
4233
0
    }
4234
4235
1
    return systemName;
4236
1
  }
4237
0
  return "";
4238
1
#endif
4239
1
}
4240
4241
char cmSystemTools::GetSystemPathlistSeparator()
4242
0
{
4243
#if defined(_WIN32)
4244
  return ';';
4245
#else
4246
0
  return ':';
4247
0
#endif
4248
0
}
4249
4250
cm::string_view cmSystemTools::GetFilenameNameView(cm::string_view filename)
4251
0
{
4252
// implementation mostly taken from cmsys::SystemTools
4253
#if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
4254
  cm::static_string_view separators = "/\\"_s;
4255
#else
4256
0
  char separators = '/';
4257
0
#endif
4258
0
  std::string::size_type slash_pos = filename.find_last_of(separators);
4259
0
  if (slash_pos == std::string::npos) {
4260
0
    return filename;
4261
0
  }
4262
0
  return filename.substr(slash_pos + 1);
4263
0
}
4264
4265
cm::string_view cmSystemTools::GetFilenameLastExtensionView(
4266
  cm::string_view filename)
4267
0
{
4268
0
  cm::string_view name = cmSystemTools::GetFilenameNameView(filename);
4269
0
  cm::string_view::size_type dot_pos = name.rfind('.');
4270
0
  if (dot_pos == std::string::npos) {
4271
0
    return cm::string_view();
4272
0
  }
4273
0
  return name.substr(dot_pos);
4274
0
}