Coverage Report

Created: 2026-03-12 06:35

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
2.71k
{
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
2.71k
  return archive_entry_pathname(entry);
389
2.71k
#  endif
390
2.71k
}
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
29.0k
{
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
29.0k
  return archive_read_open_filename(a, file, block_size);
401
29.0k
#  endif
402
29.0k
}
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
25.1k
{
469
25.1k
  std::string message = "CMake Error: " + m;
470
25.1k
  cmSystemTools::s_ErrorOccurred = true;
471
25.1k
  cmSystemTools::Message(message, "Error");
472
25.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
4.65k
{
513
4.65k
  if (s_StdoutCallback) {
514
0
    s_StdoutCallback(s);
515
4.65k
  } else {
516
4.65k
    std::cout << s << std::flush;
517
4.65k
  }
518
4.65k
}
519
520
void cmSystemTools::Message(std::string const& m, char const* title)
521
25.1k
{
522
25.1k
  cmMessageMetadata md;
523
25.1k
  md.title = title;
524
25.1k
  Message(m, md);
525
25.1k
}
526
527
void cmSystemTools::Message(std::string const& m, cmMessageMetadata const& md)
528
25.1k
{
529
25.1k
  if (s_MessageCallback) {
530
3
    s_MessageCallback(m, md);
531
25.1k
  } else {
532
25.1k
    std::cerr << m << std::endl;
533
25.1k
  }
534
25.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
0
{
852
0
  cmUVProcessChainBuilder builder;
853
0
  builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT, stdin)
854
0
    .AddCommand(command);
855
0
  if (dir) {
856
0
    builder.SetWorkingDirectory(dir);
857
0
  }
858
859
0
  if (outputflag == OUTPUT_PASSTHROUGH) {
860
0
    captureStdOut = nullptr;
861
0
    captureStdErr = nullptr;
862
0
    builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout)
863
0
      .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr);
864
0
  } else if (outputflag == OUTPUT_MERGE ||
865
0
             (captureStdErr && captureStdErr == captureStdOut)) {
866
0
    builder.SetMergedBuiltinStreams();
867
0
    captureStdErr = nullptr;
868
0
  } else {
869
0
    builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
870
0
      .SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
871
0
  }
872
0
  assert(!captureStdErr || captureStdErr != captureStdOut);
873
874
0
  auto chain = builder.Start();
875
0
  bool timedOut = false;
876
0
  cm::uv_timer_ptr timer;
877
0
  if (timeout.count()) {
878
0
    timer.init(chain.GetLoop(), &timedOut);
879
0
    timer.start(
880
0
      [](uv_timer_t* t) {
881
0
        auto* timedOutPtr = static_cast<bool*>(t->data);
882
0
        *timedOutPtr = true;
883
0
      },
884
0
      static_cast<uint64_t>(timeout.count() * 1000.0), 0,
885
0
      cm::uv_update_time::yes);
886
0
  }
887
888
0
  std::vector<char> tempStdOut;
889
0
  std::vector<char> tempStdErr;
890
0
  bool outFinished = true;
891
0
  bool errFinished = true;
892
0
  cmProcessOutput processOutput(encoding);
893
0
  std::unique_ptr<cmUVStreamReadHandle> outputHandle;
894
0
  std::unique_ptr<cmUVStreamReadHandle> errorHandle;
895
0
  if (outputflag != OUTPUT_PASSTHROUGH &&
896
0
      (captureStdOut || captureStdErr || outputflag != OUTPUT_NONE)) {
897
0
    auto startRead =
898
0
      [&outputflag, &processOutput](
899
0
        uv_stream_t* stream, std::string* captureStd,
900
0
        std::vector<char>& tempStd, int id,
901
0
        void (*outputFunc)(std::string const&),
902
0
        bool& finished) -> std::unique_ptr<cmUVStreamReadHandle> {
903
0
      finished = false;
904
0
      return cmUVStreamRead(
905
0
        stream,
906
0
        [outputflag, &processOutput, captureStd, &tempStd, id,
907
0
         outputFunc](std::vector<char> data) {
908
          // Translate NULL characters in the output into valid text.
909
0
          for (auto& c : data) {
910
0
            if (c == '\0') {
911
0
              c = ' ';
912
0
            }
913
0
          }
914
915
0
          if (outputflag != OUTPUT_NONE) {
916
0
            std::string strdata;
917
0
            processOutput.DecodeText(data.data(), data.size(), strdata, id);
918
0
            outputFunc(strdata);
919
0
          }
920
0
          if (captureStd) {
921
0
            cm::append(tempStd, data.data(), data.data() + data.size());
922
0
          }
923
0
        },
924
0
        [&finished, outputflag, &processOutput, id, outputFunc]() {
925
0
          finished = true;
926
0
          if (outputflag != OUTPUT_NONE) {
927
0
            std::string strdata;
928
0
            processOutput.DecodeText(std::string(), strdata, id);
929
0
            if (!strdata.empty()) {
930
0
              outputFunc(strdata);
931
0
            }
932
0
          }
933
0
        });
934
0
    };
935
936
0
    outputHandle = startRead(chain.OutputStream(), captureStdOut, tempStdOut,
937
0
                             1, cmSystemTools::Stdout, outFinished);
938
0
    if (chain.ErrorStream()) {
939
0
      errorHandle = startRead(chain.ErrorStream(), captureStdErr, tempStdErr,
940
0
                              2, cmSystemTools::Stderr, errFinished);
941
0
    }
942
0
  }
943
944
0
  while (!timedOut && !(chain.Finished() && outFinished && errFinished)) {
945
0
    uv_run(&chain.GetLoop(), UV_RUN_ONCE);
946
0
  }
947
948
0
  if (captureStdOut) {
949
0
    captureStdOut->assign(tempStdOut.begin(), tempStdOut.end());
950
0
    processOutput.DecodeText(*captureStdOut, *captureStdOut);
951
0
  }
952
0
  if (captureStdErr) {
953
0
    captureStdErr->assign(tempStdErr.begin(), tempStdErr.end());
954
0
    processOutput.DecodeText(*captureStdErr, *captureStdErr);
955
0
  }
956
957
0
  bool result = true;
958
0
  if (timedOut) {
959
0
    chain.Terminate();
960
0
    char const* error_str = "Process terminated due to timeout\n";
961
0
    if (outputflag != OUTPUT_NONE) {
962
0
      std::cerr << error_str << std::endl;
963
0
    }
964
0
    if (captureStdErr) {
965
0
      captureStdErr->append(error_str, strlen(error_str));
966
0
    }
967
0
    result = false;
968
0
  } else {
969
0
    auto const& status = chain.GetStatus(0);
970
0
    auto exception = status.GetException();
971
972
0
    switch (exception.first) {
973
0
      case cmUVProcessChain::ExceptionCode::None:
974
0
        if (retVal) {
975
0
          *retVal = static_cast<int>(status.ExitStatus);
976
0
        } else {
977
0
          if (status.ExitStatus != 0) {
978
0
            result = false;
979
0
          }
980
0
        }
981
0
        break;
982
0
      default: {
983
0
        if (outputflag != OUTPUT_NONE) {
984
0
          std::cerr << exception.second << std::endl;
985
0
        }
986
0
        if (captureStdErr) {
987
0
          captureStdErr->append(exception.second);
988
0
        } else if (captureStdOut) {
989
0
          captureStdOut->append(exception.second);
990
0
        }
991
0
        result = false;
992
0
      } break;
993
0
    }
994
0
  }
995
996
0
  return result;
997
0
}
998
999
bool cmSystemTools::RunSingleCommand(std::string const& command,
1000
                                     std::string* captureStdOut,
1001
                                     std::string* captureStdErr, int* retVal,
1002
                                     char const* dir, OutputOption outputflag,
1003
                                     cmDuration timeout)
1004
0
{
1005
0
  if (s_DisableRunCommandOutput) {
1006
0
    outputflag = OUTPUT_NONE;
1007
0
  }
1008
1009
0
  std::vector<std::string> args = cmSystemTools::ParseArguments(command);
1010
1011
0
  if (args.empty()) {
1012
0
    return false;
1013
0
  }
1014
0
  return cmSystemTools::RunSingleCommand(args, captureStdOut, captureStdErr,
1015
0
                                         retVal, dir, outputflag, timeout);
1016
0
}
1017
1018
std::string cmSystemTools::PrintSingleCommand(
1019
  std::vector<std::string> const& command)
1020
0
{
1021
0
  if (command.empty()) {
1022
0
    return std::string();
1023
0
  }
1024
1025
0
  return cmWrap('"', command, '"', " ");
1026
0
}
1027
1028
bool cmSystemTools::DoesFileExistWithExtensions(
1029
  std::string const& name, std::vector<std::string> const& headerExts)
1030
0
{
1031
0
  std::string hname;
1032
1033
0
  for (std::string const& headerExt : headerExts) {
1034
0
    hname = cmStrCat(name, '.', headerExt);
1035
0
    if (cmSystemTools::FileExists(hname)) {
1036
0
      return true;
1037
0
    }
1038
0
  }
1039
0
  return false;
1040
0
}
1041
1042
std::string cmSystemTools::FileExistsInParentDirectories(
1043
  std::string const& fname, std::string const& directory,
1044
  std::string const& toplevel)
1045
0
{
1046
0
  std::string file = fname;
1047
0
  cmSystemTools::ConvertToUnixSlashes(file);
1048
0
  std::string dir = directory;
1049
0
  cmSystemTools::ConvertToUnixSlashes(dir);
1050
0
  std::string prevDir;
1051
0
  while (dir != prevDir) {
1052
0
    std::string path = cmStrCat(dir, '/', file);
1053
0
    if (cmSystemTools::FileExists(path)) {
1054
0
      return path;
1055
0
    }
1056
0
    if (dir.size() < toplevel.size()) {
1057
0
      break;
1058
0
    }
1059
0
    prevDir = dir;
1060
0
    dir = cmSystemTools::GetParentDirectory(dir);
1061
0
  }
1062
0
  return "";
1063
0
}
1064
1065
#ifdef _WIN32
1066
namespace {
1067
1068
/* Helper class to save and restore the specified file (or directory)
1069
   attribute bits. Instantiate this class as an automatic variable on the
1070
   stack. Its constructor saves a copy of the file attributes, and then its
1071
   destructor restores the original attribute settings.  */
1072
class SaveRestoreFileAttributes
1073
{
1074
public:
1075
  SaveRestoreFileAttributes(std::wstring const& path,
1076
                            uint32_t file_attrs_to_set);
1077
  ~SaveRestoreFileAttributes();
1078
1079
  SaveRestoreFileAttributes(SaveRestoreFileAttributes const&) = delete;
1080
  SaveRestoreFileAttributes& operator=(SaveRestoreFileAttributes const&) =
1081
    delete;
1082
1083
  void SetPath(std::wstring const& path) { path_ = path; }
1084
1085
private:
1086
  std::wstring path_;
1087
  uint32_t original_attr_bits_;
1088
};
1089
1090
SaveRestoreFileAttributes::SaveRestoreFileAttributes(
1091
  std::wstring const& path, uint32_t file_attrs_to_set)
1092
  : path_(path)
1093
  , original_attr_bits_(0)
1094
{
1095
  // Set the specified attributes for the source file/directory.
1096
  original_attr_bits_ = GetFileAttributesW(path_.c_str());
1097
  if ((INVALID_FILE_ATTRIBUTES != original_attr_bits_) &&
1098
      ((file_attrs_to_set & original_attr_bits_) != file_attrs_to_set)) {
1099
    SetFileAttributesW(path_.c_str(), original_attr_bits_ | file_attrs_to_set);
1100
  }
1101
}
1102
1103
// We set attribute bits.  Now we need to restore their original state.
1104
SaveRestoreFileAttributes::~SaveRestoreFileAttributes()
1105
{
1106
  DWORD last_error = GetLastError();
1107
  // Verify or restore the original attributes.
1108
  const DWORD source_attr_bits = GetFileAttributesW(path_.c_str());
1109
  if (INVALID_FILE_ATTRIBUTES != source_attr_bits) {
1110
    if (original_attr_bits_ != source_attr_bits) {
1111
      // The file still exists, and its attributes aren't our saved values.
1112
      // Time to restore them.
1113
      SetFileAttributesW(path_.c_str(), original_attr_bits_);
1114
    }
1115
  }
1116
  SetLastError(last_error);
1117
}
1118
1119
struct WindowsFileRetryInit
1120
{
1121
  cmSystemTools::WindowsFileRetry Retry;
1122
  bool Explicit;
1123
};
1124
1125
WindowsFileRetryInit InitWindowsFileRetry(wchar_t const* const values[2],
1126
                                          unsigned int const defaults[2])
1127
{
1128
  unsigned int data[2] = { 0, 0 };
1129
  HKEY const keys[2] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
1130
  for (int k = 0; k < 2; ++k) {
1131
    HKEY hKey;
1132
    if (RegOpenKeyExW(keys[k], L"Software\\Kitware\\CMake\\Config", 0,
1133
                      KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
1134
      for (int v = 0; v < 2; ++v) {
1135
        DWORD dwData, dwType, dwSize = 4;
1136
        if (!data[v] &&
1137
            RegQueryValueExW(hKey, values[v], 0, &dwType, (BYTE*)&dwData,
1138
                             &dwSize) == ERROR_SUCCESS &&
1139
            dwType == REG_DWORD && dwSize == 4) {
1140
          data[v] = static_cast<unsigned int>(dwData);
1141
        }
1142
      }
1143
      RegCloseKey(hKey);
1144
    }
1145
  }
1146
  WindowsFileRetryInit init;
1147
  init.Explicit = data[0] || data[1];
1148
  init.Retry.Count = data[0] ? data[0] : defaults[0];
1149
  init.Retry.Delay = data[1] ? data[1] : defaults[1];
1150
  return init;
1151
}
1152
1153
WindowsFileRetryInit InitWindowsFileRetry()
1154
{
1155
  static wchar_t const* const values[2] = { L"FilesystemRetryCount",
1156
                                            L"FilesystemRetryDelay" };
1157
  static unsigned int const defaults[2] = { 5, 500 };
1158
  return InitWindowsFileRetry(values, defaults);
1159
}
1160
1161
WindowsFileRetryInit InitWindowsDirectoryRetry()
1162
{
1163
  static wchar_t const* const values[2] = { L"FilesystemDirectoryRetryCount",
1164
                                            L"FilesystemDirectoryRetryDelay" };
1165
  static unsigned int const defaults[2] = { 120, 500 };
1166
  WindowsFileRetryInit dirInit = InitWindowsFileRetry(values, defaults);
1167
  if (dirInit.Explicit) {
1168
    return dirInit;
1169
  }
1170
  WindowsFileRetryInit fileInit = InitWindowsFileRetry();
1171
  if (fileInit.Explicit) {
1172
    return fileInit;
1173
  }
1174
  return dirInit;
1175
}
1176
1177
cmSystemTools::WindowsFileRetry GetWindowsRetry(std::wstring const& path)
1178
{
1179
  // If we are performing a directory operation, then try and get the
1180
  // appropriate timing info.
1181
  DWORD const attrs = GetFileAttributesW(path.c_str());
1182
  if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
1183
    return cmSystemTools::GetWindowsDirectoryRetry();
1184
  }
1185
  return cmSystemTools::GetWindowsFileRetry();
1186
}
1187
1188
} // end of anonymous namespace
1189
1190
cmSystemTools::WindowsFileRetry cmSystemTools::GetWindowsFileRetry()
1191
{
1192
  static WindowsFileRetry retry = InitWindowsFileRetry().Retry;
1193
  return retry;
1194
}
1195
1196
cmSystemTools::WindowsFileRetry cmSystemTools::GetWindowsDirectoryRetry()
1197
{
1198
  static cmSystemTools::WindowsFileRetry retry =
1199
    InitWindowsDirectoryRetry().Retry;
1200
  return retry;
1201
}
1202
1203
cmSystemTools::WindowsVersion cmSystemTools::GetWindowsVersion()
1204
{
1205
  /* Windows version number data.  */
1206
  OSVERSIONINFOEXW osviex;
1207
  ZeroMemory(&osviex, sizeof(osviex));
1208
  osviex.dwOSVersionInfoSize = sizeof(osviex);
1209
1210
#  ifdef CM_WINDOWS_DEPRECATED_GetVersionEx
1211
#    pragma warning(push)
1212
#    ifdef __INTEL_COMPILER
1213
#      pragma warning(disable : 1478)
1214
#    elif defined __clang__
1215
#      pragma clang diagnostic push
1216
#      pragma clang diagnostic ignored "-Wdeprecated-declarations"
1217
#    else
1218
#      pragma warning(disable : 4996)
1219
#    endif
1220
#  endif
1221
  GetVersionExW((OSVERSIONINFOW*)&osviex);
1222
#  ifdef CM_WINDOWS_DEPRECATED_GetVersionEx
1223
#    ifdef __clang__
1224
#      pragma clang diagnostic pop
1225
#    else
1226
#      pragma warning(pop)
1227
#    endif
1228
#  endif
1229
1230
  WindowsVersion result;
1231
  result.dwMajorVersion = osviex.dwMajorVersion;
1232
  result.dwMinorVersion = osviex.dwMinorVersion;
1233
  result.dwBuildNumber = osviex.dwBuildNumber;
1234
  return result;
1235
}
1236
1237
std::string cmSystemTools::GetComspec()
1238
{
1239
  std::string comspec;
1240
  if (!cmSystemTools::GetEnv("COMSPEC", comspec) ||
1241
      !cmSystemTools::FileIsFullPath(comspec)) {
1242
    comspec = "cmd.exe";
1243
  }
1244
  return comspec;
1245
}
1246
1247
#endif
1248
1249
// File changes involve removing SETUID/SETGID bits when a file is modified.
1250
// This behavior is consistent across most Unix-like operating systems.
1251
class FileModeGuard
1252
{
1253
public:
1254
  FileModeGuard(std::string const& file_path, std::string* emsg);
1255
  bool Restore(std::string* emsg);
1256
  bool HasErrors() const;
1257
1258
private:
1259
#ifndef _WIN32
1260
  mode_t mode_;
1261
#endif
1262
  std::string filepath_;
1263
};
1264
1265
FileModeGuard::FileModeGuard(std::string const& file_path, std::string* emsg)
1266
0
{
1267
0
#ifndef _WIN32
1268
0
  struct stat file_stat;
1269
0
  if (stat(file_path.c_str(), &file_stat) != 0) {
1270
0
    if (emsg) {
1271
0
      *emsg = cmStrCat("Cannot get file stat: ", strerror(errno));
1272
0
    }
1273
0
    return;
1274
0
  }
1275
1276
0
  mode_ = file_stat.st_mode;
1277
#else
1278
  static_cast<void>(emsg);
1279
#endif
1280
0
  filepath_ = file_path;
1281
0
}
1282
1283
bool FileModeGuard::Restore(std::string* emsg)
1284
0
{
1285
0
  assert(filepath_.empty() == false);
1286
1287
0
#ifndef _WIN32
1288
0
  struct stat file_stat;
1289
0
  if (stat(filepath_.c_str(), &file_stat) != 0) {
1290
0
    if (emsg) {
1291
0
      *emsg = cmStrCat("Cannot get file stat: ", strerror(errno));
1292
0
    }
1293
0
    return false;
1294
0
  }
1295
1296
  // Nothing changed; everything is in the expected state
1297
0
  if (file_stat.st_mode == mode_) {
1298
0
    return true;
1299
0
  }
1300
1301
0
  if (chmod(filepath_.c_str(), mode_) != 0) {
1302
0
    if (emsg) {
1303
0
      *emsg = cmStrCat("Cannot restore the file mode: ", strerror(errno));
1304
0
    }
1305
0
    return false;
1306
0
  }
1307
#else
1308
  static_cast<void>(emsg);
1309
#endif
1310
1311
0
  return true;
1312
0
}
1313
1314
bool FileModeGuard::HasErrors() const
1315
0
{
1316
0
  return filepath_.empty();
1317
0
}
1318
1319
std::string cmSystemTools::GetRealPathResolvingWindowsSubst(
1320
  std::string const& path, std::string* errorMessage)
1321
0
{
1322
#ifdef _WIN32
1323
  // uv_fs_realpath uses Windows Vista API so fallback to kwsys if not found
1324
  std::string resolved_path;
1325
  uv_fs_t req;
1326
  int err = uv_fs_realpath(nullptr, &req, path.c_str(), nullptr);
1327
  if (!err) {
1328
    resolved_path = std::string((char*)req.ptr);
1329
    cmSystemTools::ConvertToUnixSlashes(resolved_path);
1330
  } else if (err == UV_ENOSYS) {
1331
    resolved_path = cmsys::SystemTools::GetRealPath(path, errorMessage);
1332
  } else if (errorMessage) {
1333
    cmsys::Status status =
1334
      cmsys::Status::Windows(uv_fs_get_system_error(&req));
1335
    *errorMessage = status.GetString();
1336
    resolved_path.clear();
1337
  } else {
1338
    resolved_path = path;
1339
  }
1340
  // Normalize to upper-case drive letter as cm::PathResolver does.
1341
  if (resolved_path.size() > 1 && resolved_path[1] == ':') {
1342
    resolved_path[0] = cmsysString_toupper(resolved_path[0]);
1343
  }
1344
  return resolved_path;
1345
#else
1346
0
  return cmsys::SystemTools::GetRealPath(path, errorMessage);
1347
0
#endif
1348
0
}
1349
1350
std::string cmSystemTools::GetRealPath(std::string const& path,
1351
                                       std::string* errorMessage)
1352
10
{
1353
#ifdef _WIN32
1354
  std::string resolved_path =
1355
    cmSystemTools::GetRealPathResolvingWindowsSubst(path, errorMessage);
1356
1357
  // If the original path used a subst drive and the real path starts
1358
  // with the substitution, restore the subst drive prefix.  This may
1359
  // incorrectly restore a subst drive if the underlying drive was
1360
  // encountered via an absolute symlink, but this is an acceptable
1361
  // limitation to otherwise preserve susbt drives.
1362
  if (resolved_path.size() >= 2 && resolved_path[1] == ':' &&
1363
      path.size() >= 2 && path[1] == ':' &&
1364
      cmsysString_toupper(resolved_path[0]) != cmsysString_toupper(path[0])) {
1365
    // FIXME: Add thread_local or mutex if we use threads.
1366
    static std::map<char, std::string> substMap;
1367
    char const drive = static_cast<char>(cmsysString_toupper(path[0]));
1368
    std::string maybe_subst = cmStrCat(drive, ":/");
1369
    auto smi = substMap.find(drive);
1370
    if (smi == substMap.end()) {
1371
      smi = substMap
1372
              .emplace(
1373
                drive,
1374
                cmSystemTools::GetRealPathResolvingWindowsSubst(maybe_subst))
1375
              .first;
1376
    }
1377
    std::string const& resolved_subst = smi->second;
1378
    std::string::size_type const ns = resolved_subst.size();
1379
    if (ns > 0) {
1380
      std::string::size_type const np = resolved_path.size();
1381
      if (ns == np && resolved_path == resolved_subst) {
1382
        resolved_path = maybe_subst;
1383
      } else if (ns > 0 && ns < np && resolved_path[ns] == '/' &&
1384
                 resolved_path.compare(0, ns, resolved_subst) == 0) {
1385
        resolved_path.replace(0, ns + 1, maybe_subst);
1386
      }
1387
    }
1388
  }
1389
1390
  return resolved_path;
1391
#else
1392
10
  return cmsys::SystemTools::GetRealPath(path, errorMessage);
1393
10
#endif
1394
10
}
1395
1396
void cmSystemTools::InitializeLibUV()
1397
0
{
1398
#if defined(_WIN32)
1399
  // Perform libuv one-time initialization now, and then un-do its
1400
  // global _fmode setting so that using libuv does not change the
1401
  // default file text/binary mode.  See libuv issue 840.
1402
  if (uv_loop_t* loop = uv_default_loop()) {
1403
    uv_loop_close(loop);
1404
  }
1405
#  ifdef _MSC_VER
1406
  _set_fmode(_O_TEXT);
1407
#  else
1408
  _fmode = _O_TEXT;
1409
#  endif
1410
  // Replace libuv's report handler with our own to suppress popups.
1411
  cmSystemTools::EnableMSVCDebugHook();
1412
#endif
1413
0
}
1414
1415
#if defined(_WIN32)
1416
#  include <random>
1417
1418
#  include <wctype.h>
1419
#  ifdef _MSC_VER
1420
using mode_t = cmSystemTools::SystemTools::mode_t;
1421
#  endif
1422
#else
1423
#  include <sys/stat.h>
1424
#endif
1425
1426
inline int Mkdir(char const* dir, mode_t const* mode)
1427
0
{
1428
#if defined(_WIN32)
1429
  int ret = _wmkdir(cmSystemTools::ConvertToWindowsExtendedPath(dir).c_str());
1430
  if (ret == 0 && mode)
1431
    cmSystemTools::SystemTools::SetPermissions(dir, *mode);
1432
  return ret;
1433
#else
1434
0
  return mkdir(dir, mode ? *mode : 0777);
1435
0
#endif
1436
0
}
1437
1438
#ifdef CMAKE_NO_MKDTEMP
1439
namespace {
1440
char* cm_mkdtemp_fallback(char* template_)
1441
{
1442
  if (mktemp(template_) == nullptr || mkdir(template_, 0700) != 0) {
1443
    return nullptr;
1444
  }
1445
  return template_;
1446
}
1447
using cm_mkdtemp_t = char* (*)(char*);
1448
cm_mkdtemp_t const cm_mkdtemp = []() -> cm_mkdtemp_t {
1449
  cm_mkdtemp_t f = (cm_mkdtemp_t)dlsym(RTLD_DEFAULT, "mkdtemp");
1450
  dlerror(); // Ignore/cleanup dlsym errors.
1451
  if (!f) {
1452
    f = cm_mkdtemp_fallback;
1453
  }
1454
  return f;
1455
}();
1456
}
1457
#else
1458
0
#  define cm_mkdtemp mkdtemp
1459
#endif
1460
1461
cmsys::Status cmSystemTools::MakeTempDirectory(std::string& path,
1462
                                               mode_t const* mode)
1463
0
{
1464
0
  if (path.empty()) {
1465
0
    return cmsys::Status::POSIX(EINVAL);
1466
0
  }
1467
0
  return cmSystemTools::MakeTempDirectory(&path.front(), mode);
1468
0
}
1469
1470
cmsys::Status cmSystemTools::MakeTempDirectory(char* path, mode_t const* mode)
1471
0
{
1472
0
  if (!path) {
1473
0
    return cmsys::Status::POSIX(EINVAL);
1474
0
  }
1475
1476
  // verify that path ends with "XXXXXX"
1477
0
  auto const l = std::strlen(path);
1478
0
  if (!cmHasLiteralSuffix(cm::string_view{ path, l }, "XXXXXX")) {
1479
0
    return cmsys::Status::POSIX(EINVAL);
1480
0
  }
1481
1482
  // create parent directories
1483
0
  auto* sep = path;
1484
0
  while ((sep = strchr(sep, '/'))) {
1485
    // all underlying functions use C strings,
1486
    // so temporarily end the string here
1487
0
    *sep = '\0';
1488
0
    Mkdir(path, mode);
1489
1490
0
    *sep = '/';
1491
0
    ++sep;
1492
0
  }
1493
1494
#ifdef _WIN32
1495
  int const nchars = 36;
1496
  char const chars[nchars + 1] = "abcdefghijklmnopqrstuvwxyz0123456789";
1497
1498
  std::random_device rd;
1499
  std::mt19937 rg{ rd() };
1500
  std::uniform_int_distribution<int> dist{ 0, nchars - 1 };
1501
1502
  for (auto tries = 100; tries; --tries) {
1503
    for (auto n = l - 6; n < l; ++n) {
1504
      path[n] = chars[dist(rg)];
1505
    }
1506
    if (Mkdir(path, mode) == 0) {
1507
      return cmsys::Status::Success();
1508
    } else if (errno != EEXIST) {
1509
      return cmsys::Status::POSIX_errno();
1510
    }
1511
  }
1512
  return cmsys::Status::POSIX(EAGAIN);
1513
#else
1514
0
  if (cm_mkdtemp(path)) {
1515
0
    if (mode) {
1516
0
      chmod(path, *mode);
1517
0
    }
1518
0
  } else {
1519
0
    return cmsys::Status::POSIX_errno();
1520
0
  }
1521
0
  return cmsys::Status::Success();
1522
0
#endif
1523
0
}
1524
1525
#ifdef _WIN32
1526
namespace {
1527
bool cmMoveFile(std::wstring const& oldname, std::wstring const& newname,
1528
                cmSystemTools::Replace replace)
1529
{
1530
  // Not only ignore any previous error, but clear any memory of it.
1531
  SetLastError(0);
1532
1533
  DWORD flags = 0;
1534
  if (replace == cmSystemTools::Replace::Yes) {
1535
    // Use MOVEFILE_REPLACE_EXISTING to replace an existing destination file.
1536
    flags = flags | MOVEFILE_REPLACE_EXISTING;
1537
  }
1538
1539
  return MoveFileExW(oldname.c_str(), newname.c_str(), flags);
1540
}
1541
}
1542
#endif
1543
1544
cmSystemTools::CopyResult cmSystemTools::CopySingleFile(
1545
  std::string const& oldname, std::string const& newname, CopyWhen when,
1546
  CopyInputRecent inputRecent, std::string* err)
1547
0
{
1548
0
  switch (when) {
1549
0
    case CopyWhen::Always:
1550
0
      break;
1551
0
    case CopyWhen::OnlyIfDifferent:
1552
0
      if (!FilesDiffer(oldname, newname)) {
1553
0
        return CopyResult::Success;
1554
0
      }
1555
0
      break;
1556
0
    case CopyWhen::OnlyIfNewer: {
1557
0
      if (!SystemTools::FileExists(newname)) {
1558
0
        break;
1559
0
      }
1560
0
      int timeResult = 0;
1561
0
      cmsys::Status timeStatus =
1562
0
        cmsys::SystemTools::FileTimeCompare(oldname, newname, &timeResult);
1563
0
      if (timeStatus.IsSuccess() && timeResult <= 0) {
1564
0
        return CopyResult::Success;
1565
0
      }
1566
0
      break;
1567
0
    }
1568
0
  }
1569
1570
0
  mode_t perm = 0;
1571
0
  cmsys::Status perms = SystemTools::GetPermissions(oldname, perm);
1572
1573
  // If files are the same do not copy
1574
0
  if (SystemTools::SameFile(oldname, newname)) {
1575
0
    return CopyResult::Success;
1576
0
  }
1577
1578
0
  cmsys::SystemTools::CopyStatus status;
1579
0
  status = cmsys::SystemTools::CloneFileContent(oldname, newname);
1580
0
  if (!status) {
1581
    // if cloning did not succeed, fall back to blockwise copy
1582
#ifdef _WIN32
1583
    if (inputRecent == CopyInputRecent::Yes) {
1584
      // Windows sometimes locks a file immediately after creation.
1585
      // Retry a few times.
1586
      WindowsFileRetry retry = cmSystemTools::GetWindowsFileRetry();
1587
      while ((status =
1588
                cmsys::SystemTools::CopyFileContentBlockwise(oldname, newname),
1589
              status.Path == cmsys::SystemTools::CopyStatus::SourcePath &&
1590
                status.GetPOSIX() == EACCES && --retry.Count)) {
1591
        cmSystemTools::Delay(retry.Delay);
1592
      }
1593
    } else {
1594
      status = cmsys::SystemTools::CopyFileContentBlockwise(oldname, newname);
1595
    }
1596
#else
1597
0
    static_cast<void>(inputRecent);
1598
0
    status = cmsys::SystemTools::CopyFileContentBlockwise(oldname, newname);
1599
0
#endif
1600
0
  }
1601
0
  if (!status) {
1602
0
    if (err) {
1603
0
      *err = status.GetString();
1604
0
      switch (status.Path) {
1605
0
        case cmsys::SystemTools::CopyStatus::SourcePath:
1606
0
          *err = cmStrCat(*err, " (input)");
1607
0
          break;
1608
0
        case cmsys::SystemTools::CopyStatus::DestPath:
1609
0
          *err = cmStrCat(*err, " (output)");
1610
0
          break;
1611
0
        default:
1612
0
          break;
1613
0
      }
1614
0
    }
1615
0
    return CopyResult::Failure;
1616
0
  }
1617
0
  if (perms) {
1618
0
    perms = SystemTools::SetPermissions(newname, perm);
1619
0
    if (!perms) {
1620
0
      if (err) {
1621
0
        *err = cmStrCat(perms.GetString(), " (output)");
1622
0
      }
1623
0
      return CopyResult::Failure;
1624
0
    }
1625
0
  }
1626
0
  return CopyResult::Success;
1627
0
}
1628
1629
bool cmSystemTools::RenameFile(std::string const& oldname,
1630
                               std::string const& newname)
1631
0
{
1632
0
  return cmSystemTools::RenameFile(oldname, newname, Replace::Yes) ==
1633
0
    RenameResult::Success;
1634
0
}
1635
1636
cmSystemTools::RenameResult cmSystemTools::RenameFile(
1637
  std::string const& oldname, std::string const& newname, Replace replace,
1638
  std::string* err)
1639
0
{
1640
#ifdef _WIN32
1641
#  ifndef INVALID_FILE_ATTRIBUTES
1642
#    define INVALID_FILE_ATTRIBUTES ((DWORD) - 1)
1643
#  endif
1644
  std::wstring const oldname_wstr =
1645
    SystemTools::ConvertToWindowsExtendedPath(oldname);
1646
  std::wstring const newname_wstr =
1647
    SystemTools::ConvertToWindowsExtendedPath(newname);
1648
1649
  /* Windows MoveFileEx may not replace read-only or in-use files.  If it
1650
     fails then remove the read-only attribute from any existing destination.
1651
     Try multiple times since we may be racing against another process
1652
     creating/opening the destination file just before our MoveFileEx.  */
1653
  WindowsFileRetry retry = GetWindowsRetry(oldname_wstr);
1654
1655
  // Use RAII to set the attribute bit blocking Microsoft Search Indexing,
1656
  // and restore the previous value upon return.
1657
  SaveRestoreFileAttributes save_restore_file_attributes(
1658
    oldname_wstr, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);
1659
1660
  DWORD move_last_error = 0;
1661
  while (!cmMoveFile(oldname_wstr, newname_wstr, replace) && --retry.Count) {
1662
    move_last_error = GetLastError();
1663
1664
    // There was no error ==> the operation is not yet complete.
1665
    if (move_last_error == NO_ERROR) {
1666
      break;
1667
    }
1668
1669
    // Try again only if failure was due to access/sharing permissions.
1670
    // Most often ERROR_ACCESS_DENIED (a.k.a. I/O error) for a directory, and
1671
    // ERROR_SHARING_VIOLATION for a file, are caused by one of the following:
1672
    // 1) Anti-Virus Software
1673
    // 2) Windows Search Indexer
1674
    // 3) Windows Explorer has an associated directory already opened.
1675
    if (move_last_error != ERROR_ACCESS_DENIED &&
1676
        move_last_error != ERROR_SHARING_VIOLATION) {
1677
      if (replace == Replace::No && move_last_error == ERROR_ALREADY_EXISTS) {
1678
        return RenameResult::NoReplace;
1679
      }
1680
      if (err) {
1681
        *err = cmsys::Status::Windows(move_last_error).GetString();
1682
      }
1683
      return RenameResult::Failure;
1684
    }
1685
1686
    DWORD const attrs = GetFileAttributesW(newname_wstr.c_str());
1687
    if ((attrs != INVALID_FILE_ATTRIBUTES) &&
1688
        (attrs & FILE_ATTRIBUTE_READONLY) &&
1689
        // FILE_ATTRIBUTE_READONLY is not honored on directories.
1690
        !(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
1691
      // Remove the read-only attribute from the destination file.
1692
      SetFileAttributesW(newname_wstr.c_str(),
1693
                         attrs & ~FILE_ATTRIBUTE_READONLY);
1694
    } else {
1695
      // The file may be temporarily in use so wait a bit.
1696
      cmSystemTools::Delay(retry.Delay);
1697
    }
1698
  }
1699
1700
  // If we were successful, then there was no error.
1701
  if (retry.Count > 0) {
1702
    move_last_error = 0;
1703
    // Restore the attributes on the new name.
1704
    save_restore_file_attributes.SetPath(newname_wstr);
1705
  }
1706
  SetLastError(move_last_error);
1707
  if (retry.Count > 0) {
1708
    return RenameResult::Success;
1709
  }
1710
  if (replace == Replace::No && GetLastError() == ERROR_ALREADY_EXISTS) {
1711
    return RenameResult::NoReplace;
1712
  }
1713
  if (err) {
1714
    *err = cmsys::Status::Windows_GetLastError().GetString();
1715
  }
1716
  return RenameResult::Failure;
1717
#else
1718
  // On UNIX we have OS-provided calls to create 'newname' atomically.
1719
0
  if (replace == Replace::No) {
1720
0
    if (link(oldname.c_str(), newname.c_str()) == 0) {
1721
0
      if (unlink(oldname.c_str()) == 0) {
1722
0
        return RenameResult::Success;
1723
0
      }
1724
0
      if (err) {
1725
0
        *err = cmsys::Status::POSIX_errno().GetString();
1726
0
      }
1727
0
      return RenameResult::Failure;
1728
0
    }
1729
0
    if (errno == EEXIST) {
1730
0
      return RenameResult::NoReplace;
1731
0
    }
1732
0
    if (err) {
1733
0
      *err = cmsys::Status::POSIX_errno().GetString();
1734
0
    }
1735
0
    return RenameResult::Failure;
1736
0
  }
1737
0
  if (rename(oldname.c_str(), newname.c_str()) == 0) {
1738
0
    return RenameResult::Success;
1739
0
  }
1740
0
  if (err) {
1741
0
    *err = cmsys::Status::POSIX_errno().GetString();
1742
0
  }
1743
0
  return RenameResult::Failure;
1744
0
#endif
1745
0
}
1746
1747
cmsys::Status cmSystemTools::MoveFileIfDifferent(
1748
  std::string const& source, std::string const& destination)
1749
0
{
1750
0
  cmsys::Status res = {};
1751
0
  if (FilesDiffer(source, destination)) {
1752
0
    if (RenameFile(source, destination)) {
1753
0
      return res;
1754
0
    }
1755
0
    res = CopyFileAlways(source, destination);
1756
0
  }
1757
0
  RemoveFile(source);
1758
0
  return res;
1759
0
}
1760
1761
void cmSystemTools::Glob(std::string const& directory,
1762
                         std::string const& regexp,
1763
                         std::vector<std::string>& files)
1764
0
{
1765
0
  cmsys::Directory d;
1766
0
  cmsys::RegularExpression reg(regexp.c_str());
1767
1768
0
  if (d.Load(directory)) {
1769
0
    size_t numf;
1770
0
    unsigned int i;
1771
0
    numf = d.GetNumberOfFiles();
1772
0
    for (i = 0; i < numf; i++) {
1773
0
      std::string const& fname = d.GetFileName(i);
1774
0
      if (reg.find(fname)) {
1775
0
        files.push_back(fname);
1776
0
      }
1777
0
    }
1778
0
  }
1779
0
}
1780
1781
void cmSystemTools::GlobDirs(std::string const& path,
1782
                             std::vector<std::string>& files)
1783
0
{
1784
0
  std::string::size_type pos = path.find("/*");
1785
0
  if (pos == std::string::npos) {
1786
0
    files.push_back(path);
1787
0
    return;
1788
0
  }
1789
0
  std::string startPath = path.substr(0, pos);
1790
0
  std::string finishPath = path.substr(pos + 2);
1791
1792
0
  cmsys::Directory d;
1793
0
  if (d.Load(startPath)) {
1794
0
    for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i) {
1795
0
      std::string const& f = d.GetFileName(i);
1796
0
      if (f != "." && f != "..") {
1797
0
        std::string fname = cmStrCat(startPath, '/', f);
1798
0
        if (cmSystemTools::FileIsDirectory(fname)) {
1799
0
          fname += finishPath;
1800
0
          cmSystemTools::GlobDirs(fname, files);
1801
0
        }
1802
0
      }
1803
0
    }
1804
0
  }
1805
0
}
1806
1807
bool cmSystemTools::SimpleGlob(std::string const& glob,
1808
                               std::vector<std::string>& files,
1809
                               int type /* = 0 */)
1810
0
{
1811
0
  files.clear();
1812
0
  if (glob.back() != '*') {
1813
0
    return false;
1814
0
  }
1815
0
  std::string path = cmSystemTools::GetFilenamePath(glob);
1816
0
  std::string ppath = cmSystemTools::GetFilenameName(glob);
1817
0
  ppath = ppath.substr(0, ppath.size() - 1);
1818
0
  if (path.empty()) {
1819
0
    path = "/";
1820
0
  }
1821
1822
0
  bool res = false;
1823
0
  cmsys::Directory d;
1824
0
  if (d.Load(path)) {
1825
0
    for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i) {
1826
0
      std::string const& sfname = d.GetFileName(i);
1827
0
      if (sfname != "." && sfname != "..") {
1828
0
        std::string fname = path;
1829
0
        if (path.back() != '/') {
1830
0
          fname += "/";
1831
0
        }
1832
0
        fname += sfname;
1833
0
        if (type > 0 && cmSystemTools::FileIsDirectory(fname)) {
1834
0
          continue;
1835
0
        }
1836
0
        if (type < 0 && !cmSystemTools::FileIsDirectory(fname)) {
1837
0
          continue;
1838
0
        }
1839
0
        if (cmHasPrefix(sfname, ppath)) {
1840
0
          files.push_back(fname);
1841
0
          res = true;
1842
0
        }
1843
0
      }
1844
0
    }
1845
0
  }
1846
0
  return res;
1847
0
}
1848
1849
std::string cmSystemTools::ConvertToOutputPath(std::string const& path)
1850
0
{
1851
#if defined(_WIN32) && !defined(__CYGWIN__)
1852
  if (s_ForceUnixPaths) {
1853
    return cmSystemTools::ConvertToUnixOutputPath(path);
1854
  }
1855
  return cmSystemTools::ConvertToWindowsOutputPath(path);
1856
#else
1857
0
  return cmSystemTools::ConvertToUnixOutputPath(path);
1858
0
#endif
1859
0
}
1860
1861
void cmSystemTools::ConvertToOutputSlashes(std::string& path)
1862
0
{
1863
#if defined(_WIN32) && !defined(__CYGWIN__)
1864
  if (!s_ForceUnixPaths) {
1865
    // Convert to windows slashes.
1866
    std::string::size_type pos = 0;
1867
    while ((pos = path.find('/', pos)) != std::string::npos) {
1868
      path[pos++] = '\\';
1869
    }
1870
  }
1871
#else
1872
0
  static_cast<void>(path);
1873
0
#endif
1874
0
}
1875
1876
void cmSystemTools::ConvertToLongPath(std::string& path)
1877
133k
{
1878
#if defined(_WIN32) && !defined(__CYGWIN__)
1879
  // Try to convert path to a long path only if the path contains character '~'
1880
  if (path.find('~') == std::string::npos) {
1881
    return;
1882
  }
1883
1884
  std::wstring wPath = cmsys::Encoding::ToWide(path);
1885
  DWORD ret = GetLongPathNameW(wPath.c_str(), nullptr, 0);
1886
  std::vector<wchar_t> buffer(ret);
1887
  if (ret != 0) {
1888
    ret = GetLongPathNameW(wPath.c_str(), buffer.data(),
1889
                           static_cast<DWORD>(buffer.size()));
1890
  }
1891
1892
  if (ret != 0) {
1893
    path = cmsys::Encoding::ToNarrow(buffer.data());
1894
  }
1895
#else
1896
133k
  static_cast<void>(path);
1897
133k
#endif
1898
133k
}
1899
1900
std::string cmSystemTools::ConvertToRunCommandPath(std::string const& path)
1901
0
{
1902
#if defined(_WIN32) && !defined(__CYGWIN__)
1903
  return cmSystemTools::ConvertToWindowsOutputPath(path);
1904
#else
1905
0
  return cmSystemTools::ConvertToUnixOutputPath(path);
1906
0
#endif
1907
0
}
1908
1909
// compute the relative path from here to there
1910
std::string cmSystemTools::RelativePath(std::string const& local,
1911
                                        std::string const& remote)
1912
0
{
1913
0
  if (!cmSystemTools::FileIsFullPath(local)) {
1914
0
    cmSystemTools::Error("RelativePath must be passed a full path to local: " +
1915
0
                         local);
1916
0
  }
1917
0
  if (!cmSystemTools::FileIsFullPath(remote)) {
1918
0
    cmSystemTools::Error(
1919
0
      "RelativePath must be passed a full path to remote: " + remote);
1920
0
  }
1921
0
  return cmsys::SystemTools::RelativePath(local, remote);
1922
0
}
1923
1924
std::string cmSystemTools::ForceToRelativePath(std::string const& local_path,
1925
                                               std::string const& remote_path)
1926
0
{
1927
  // The paths should never be quoted.
1928
0
  assert(local_path.front() != '\"');
1929
0
  assert(remote_path.front() != '\"');
1930
1931
  // The local path should never have a trailing slash except if it is just the
1932
  // bare root directory
1933
0
  assert(local_path.empty() || local_path.back() != '/' ||
1934
0
         local_path.size() == 1 ||
1935
0
         (local_path.size() == 3 && local_path[1] == ':' &&
1936
0
          ((local_path[0] >= 'A' && local_path[0] <= 'Z') ||
1937
0
           (local_path[0] >= 'a' && local_path[0] <= 'z'))));
1938
1939
  // If the path is already relative then just return the path.
1940
0
  if (!cmSystemTools::FileIsFullPath(remote_path)) {
1941
0
    return remote_path;
1942
0
  }
1943
1944
  // Identify the longest shared path component between the remote
1945
  // path and the local path.
1946
0
  std::vector<std::string> local;
1947
0
  cmSystemTools::SplitPath(local_path, local);
1948
0
  std::vector<std::string> remote;
1949
0
  cmSystemTools::SplitPath(remote_path, remote);
1950
0
  unsigned int common = 0;
1951
0
  while (common < remote.size() && common < local.size() &&
1952
0
         cmSystemTools::ComparePath(remote[common], local[common])) {
1953
0
    ++common;
1954
0
  }
1955
1956
  // If no part of the path is in common then return the full path.
1957
0
  if (common == 0) {
1958
0
    return remote_path;
1959
0
  }
1960
1961
  // If the entire path is in common then just return a ".".
1962
0
  if (common == remote.size() && common == local.size()) {
1963
0
    return ".";
1964
0
  }
1965
1966
  // If the entire path is in common except for a trailing slash then
1967
  // just return a "./".
1968
0
  if (common + 1 == remote.size() && remote[common].empty() &&
1969
0
      common == local.size()) {
1970
0
    return "./";
1971
0
  }
1972
1973
  // Construct the relative path.
1974
0
  std::string relative;
1975
1976
  // First add enough ../ to get up to the level of the shared portion
1977
  // of the path.  Leave off the trailing slash.  Note that the last
1978
  // component of local will never be empty because local should never
1979
  // have a trailing slash.
1980
0
  for (unsigned int i = common; i < local.size(); ++i) {
1981
0
    relative += "..";
1982
0
    if (i < local.size() - 1) {
1983
0
      relative += "/";
1984
0
    }
1985
0
  }
1986
1987
  // Now add the portion of the destination path that is not included
1988
  // in the shared portion of the path.  Add a slash the first time
1989
  // only if there was already something in the path.  If there was a
1990
  // trailing slash in the input then the last iteration of the loop
1991
  // will add a slash followed by an empty string which will preserve
1992
  // the trailing slash in the output.
1993
1994
0
  if (!relative.empty() && !remote.empty()) {
1995
0
    relative += "/";
1996
0
  }
1997
0
  relative += cmJoin(cmMakeRange(remote).advance(common), "/");
1998
1999
  // Finally return the path.
2000
0
  return relative;
2001
0
}
2002
2003
std::string cmSystemTools::RelativeIfUnder(std::string const& top,
2004
                                           std::string const& in)
2005
1
{
2006
1
  std::string out;
2007
1
  if (in == top) {
2008
0
    out = ".";
2009
1
  } else if (cmSystemTools::IsSubDirectory(in, top)) {
2010
0
    out = in.substr(top.size() + (top.back() == '/' ? 0 : 1));
2011
1
  } else {
2012
1
    out = in;
2013
1
  }
2014
1
  return out;
2015
1
}
2016
2017
cm::optional<std::string> cmSystemTools::GetEnvVar(std::string const& var)
2018
17
{
2019
17
  cm::optional<std::string> result;
2020
17
  {
2021
17
    std::string value;
2022
17
    if (cmSystemTools::GetEnv(var, value)) {
2023
0
      result = std::move(value);
2024
0
    }
2025
17
  }
2026
17
  return result;
2027
17
}
2028
2029
std::vector<std::string> cmSystemTools::GetEnvPathNormalized(
2030
  std::string const& var)
2031
0
{
2032
0
  std::vector<std::string> result;
2033
0
  if (cm::optional<std::string> env = cmSystemTools::GetEnvVar(var)) {
2034
0
    std::vector<std::string> p = cmSystemTools::SplitEnvPathNormalized(*env);
2035
0
    std::move(p.begin(), p.end(), std::back_inserter(result));
2036
0
  }
2037
0
  return result;
2038
0
}
2039
2040
std::vector<std::string> cmSystemTools::SplitEnvPath(cm::string_view in)
2041
0
{
2042
#if defined(_WIN32) && !defined(__CYGWIN__)
2043
  static cm::string_view sep = ";"_s;
2044
#else
2045
0
  static cm::string_view sep = ":"_s;
2046
0
#endif
2047
0
  std::vector<std::string> paths;
2048
0
  cm::string_view::size_type e = 0;
2049
0
  for (;;) {
2050
0
    cm::string_view::size_type b = in.find_first_not_of(sep, e);
2051
0
    if (b == cm::string_view::npos) {
2052
0
      break;
2053
0
    }
2054
0
    e = in.find_first_of(sep, b);
2055
0
    if (e == cm::string_view::npos) {
2056
0
      paths.emplace_back(in.substr(b));
2057
0
      break;
2058
0
    }
2059
0
    paths.emplace_back(in.substr(b, e - b));
2060
0
  }
2061
0
  return paths;
2062
0
}
2063
2064
std::vector<std::string> cmSystemTools::SplitEnvPathNormalized(
2065
  cm::string_view in)
2066
0
{
2067
0
  std::vector<std::string> paths = cmSystemTools::SplitEnvPath(in);
2068
0
  std::transform(paths.begin(), paths.end(), paths.begin(),
2069
0
                 cmSystemTools::ToNormalizedPathOnDisk);
2070
0
  return paths;
2071
0
}
2072
2073
std::string cmSystemTools::ToNormalizedPathOnDisk(std::string p)
2074
11
{
2075
11
  p = ResolveTildePath(p);
2076
11
  using namespace cm::PathResolver;
2077
#ifdef _WIN32
2078
  // IWYU pragma: no_forward_declare cm::PathResolver::Policies::CasePath
2079
  static Resolver<Policies::CasePath> const resolver(RealOS);
2080
#else
2081
  // IWYU pragma: no_forward_declare cm::PathResolver::Policies::LogicalPath
2082
11
  static Resolver<Policies::LogicalPath> const resolver(RealOS);
2083
11
#endif
2084
11
  resolver.Resolve(std::move(p), p);
2085
11
  return p;
2086
11
}
2087
2088
#ifndef CMAKE_BOOTSTRAP
2089
bool cmSystemTools::UnsetEnv(char const* value)
2090
0
{
2091
#  if !defined(HAVE_UNSETENV)
2092
  return cmSystemTools::UnPutEnv(value);
2093
#  else
2094
0
  unsetenv(value);
2095
0
  return true;
2096
0
#  endif
2097
0
}
2098
2099
std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
2100
0
{
2101
0
  std::vector<std::string> env;
2102
0
  int cc;
2103
#  ifdef _WIN32
2104
  // if program starts with main, _wenviron is initially NULL, call to
2105
  // _wgetenv and create wide-character string environment
2106
  _wgetenv(L"");
2107
  for (cc = 0; _wenviron[cc]; ++cc) {
2108
    env.emplace_back(cmsys::Encoding::ToNarrow(_wenviron[cc]));
2109
  }
2110
#  else
2111
0
  for (cc = 0; environ[cc]; ++cc) {
2112
0
    env.emplace_back(environ[cc]);
2113
0
  }
2114
0
#  endif
2115
0
  return env;
2116
0
}
2117
2118
void cmSystemTools::AppendEnv(std::vector<std::string> const& env)
2119
0
{
2120
0
  for (std::string const& var : env) {
2121
0
    cmSystemTools::PutEnv(var);
2122
0
  }
2123
0
}
2124
2125
void cmSystemTools::EnvDiff::AppendEnv(std::vector<std::string> const& env)
2126
0
{
2127
0
  for (std::string const& var : env) {
2128
0
    this->PutEnv(var);
2129
0
  }
2130
0
}
2131
2132
void cmSystemTools::EnvDiff::PutEnv(std::string const& env)
2133
0
{
2134
0
  auto const eq_loc = env.find('=');
2135
0
  if (eq_loc != std::string::npos) {
2136
0
    std::string name = env.substr(0, eq_loc);
2137
0
    diff[name] = env.substr(eq_loc + 1);
2138
0
  } else {
2139
0
    this->UnPutEnv(env);
2140
0
  }
2141
0
}
2142
2143
void cmSystemTools::EnvDiff::UnPutEnv(std::string const& env)
2144
0
{
2145
0
  diff[env] = cm::nullopt;
2146
0
}
2147
2148
bool cmSystemTools::EnvDiff::ParseOperation(std::string const& envmod)
2149
0
{
2150
0
  char path_sep = GetSystemPathlistSeparator();
2151
2152
0
  auto apply_diff = [this](std::string const& name,
2153
0
                           std::function<void(std::string&)> const& apply) {
2154
0
    cm::optional<std::string> old_value = diff[name];
2155
0
    std::string output;
2156
0
    if (old_value) {
2157
0
      output = *old_value;
2158
0
    } else {
2159
0
      char const* curval = cmSystemTools::GetEnv(name);
2160
0
      if (curval) {
2161
0
        output = curval;
2162
0
      }
2163
0
    }
2164
0
    apply(output);
2165
0
    diff[name] = output;
2166
0
  };
2167
2168
  // Split on `=`
2169
0
  auto const eq_loc = envmod.find_first_of('=');
2170
0
  if (eq_loc == std::string::npos) {
2171
0
    cmSystemTools::Error(cmStrCat(
2172
0
      "Error: Missing `=` after the variable name in: ", envmod, '\n'));
2173
0
    return false;
2174
0
  }
2175
2176
0
  auto const name = envmod.substr(0, eq_loc);
2177
2178
  // Split value on `:`
2179
0
  auto const op_value_start = eq_loc + 1;
2180
0
  auto const colon_loc = envmod.find_first_of(':', op_value_start);
2181
0
  if (colon_loc == std::string::npos) {
2182
0
    cmSystemTools::Error(
2183
0
      cmStrCat("Error: Missing `:` after the operation in: ", envmod, '\n'));
2184
0
    return false;
2185
0
  }
2186
0
  auto const op = envmod.substr(op_value_start, colon_loc - op_value_start);
2187
2188
0
  auto const value_start = colon_loc + 1;
2189
0
  auto const value = envmod.substr(value_start);
2190
2191
  // Determine what to do with the operation.
2192
0
  if (op == "reset"_s) {
2193
0
    auto entry = diff.find(name);
2194
0
    if (entry != diff.end()) {
2195
0
      diff.erase(entry);
2196
0
    }
2197
0
  } else if (op == "set"_s) {
2198
0
    diff[name] = value;
2199
0
  } else if (op == "unset"_s) {
2200
0
    diff[name] = cm::nullopt;
2201
0
  } else if (op == "string_append"_s) {
2202
0
    apply_diff(name, [&value](std::string& output) { output += value; });
2203
0
  } else if (op == "string_prepend"_s) {
2204
0
    apply_diff(name,
2205
0
               [&value](std::string& output) { output.insert(0, value); });
2206
0
  } else if (op == "path_list_append"_s) {
2207
0
    apply_diff(name, [&value, path_sep](std::string& output) {
2208
0
      if (!output.empty()) {
2209
0
        output += path_sep;
2210
0
      }
2211
0
      output += value;
2212
0
    });
2213
0
  } else if (op == "path_list_prepend"_s) {
2214
0
    apply_diff(name, [&value, path_sep](std::string& output) {
2215
0
      if (!output.empty()) {
2216
0
        output.insert(output.begin(), path_sep);
2217
0
      }
2218
0
      output.insert(0, value);
2219
0
    });
2220
0
  } else if (op == "cmake_list_append"_s) {
2221
0
    apply_diff(name, [&value](std::string& output) {
2222
0
      if (!output.empty()) {
2223
0
        output += ';';
2224
0
      }
2225
0
      output += value;
2226
0
    });
2227
0
  } else if (op == "cmake_list_prepend"_s) {
2228
0
    apply_diff(name, [&value](std::string& output) {
2229
0
      if (!output.empty()) {
2230
0
        output.insert(output.begin(), ';');
2231
0
      }
2232
0
      output.insert(0, value);
2233
0
    });
2234
0
  } else {
2235
0
    cmSystemTools::Error(cmStrCat(
2236
0
      "Error: Unrecognized environment manipulation argument: ", op, '\n'));
2237
0
    return false;
2238
0
  }
2239
2240
0
  return true;
2241
0
}
2242
2243
void cmSystemTools::EnvDiff::ApplyToCurrentEnv(std::ostringstream* measurement)
2244
0
{
2245
0
  for (auto const& env_apply : diff) {
2246
0
    if (env_apply.second) {
2247
0
      auto const env_update =
2248
0
        cmStrCat(env_apply.first, '=', *env_apply.second);
2249
0
      cmSystemTools::PutEnv(env_update);
2250
0
      if (measurement) {
2251
0
        *measurement << env_update << std::endl;
2252
0
      }
2253
0
    } else {
2254
0
      cmSystemTools::UnsetEnv(env_apply.first.c_str());
2255
0
      if (measurement) {
2256
        // Signify that this variable is being actively unset
2257
0
        *measurement << '#' << env_apply.first << "=\n";
2258
0
      }
2259
0
    }
2260
0
  }
2261
0
}
2262
2263
cmSystemTools::SaveRestoreEnvironment::SaveRestoreEnvironment()
2264
0
{
2265
0
  this->Env = cmSystemTools::GetEnvironmentVariables();
2266
0
}
2267
2268
cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment()
2269
0
{
2270
  // First clear everything in the current environment:
2271
0
  std::vector<std::string> currentEnv = GetEnvironmentVariables();
2272
0
  for (std::string var : currentEnv) {
2273
0
    std::string::size_type pos = var.find('=');
2274
0
    if (pos != std::string::npos) {
2275
0
      var = var.substr(0, pos);
2276
0
    }
2277
2278
0
    cmSystemTools::UnsetEnv(var.c_str());
2279
0
  }
2280
2281
  // Then put back each entry from the original environment:
2282
0
  cmSystemTools::AppendEnv(this->Env);
2283
0
}
2284
#endif
2285
2286
cmSystemTools::ScopedEnv::ScopedEnv(cm::string_view var)
2287
0
{
2288
0
  std::string::size_type pos = var.find('=');
2289
0
  if (pos != std::string::npos) {
2290
0
    this->Key = std::string{ var.substr(0, pos) };
2291
0
    this->Original = cmSystemTools::GetEnvVar(this->Key);
2292
2293
0
    cm::string_view value = var.substr(pos + 1);
2294
2295
0
    if (!this->Original && value.empty()) {
2296
      // nothing to do if the environment variable wasn't already set and the
2297
      // new value is also empty. clear the Key member so the destructor also
2298
      // does nothing.
2299
0
      this->Key.clear();
2300
0
    } else {
2301
0
      if (value.empty()) {
2302
0
        cmSystemTools::UnPutEnv(this->Key);
2303
0
      } else {
2304
0
        cmSystemTools::PutEnv(cmStrCat(this->Key, '=', value));
2305
0
      }
2306
0
    }
2307
0
  }
2308
0
}
2309
2310
cmSystemTools::ScopedEnv::~ScopedEnv()
2311
0
{
2312
0
  if (!this->Key.empty()) {
2313
0
    if (this->Original) {
2314
0
      cmSystemTools::PutEnv(cmStrCat(this->Key, '=', *this->Original));
2315
0
    } else {
2316
0
      cmSystemTools::UnPutEnv(Key);
2317
0
    }
2318
0
  }
2319
0
}
2320
2321
void cmSystemTools::EnableVSConsoleOutput()
2322
35
{
2323
#ifdef _WIN32
2324
  // Visual Studio tools like devenv may not
2325
  // display output to the console unless this environment variable is
2326
  // set.  We need it to capture the output of these build tools.
2327
  // Note for future work that one could pass "/out \\.\pipe\NAME" to
2328
  // either of these executables where NAME is created with
2329
  // CreateNamedPipe.  This would bypass the internal buffering of the
2330
  // output and allow it to be captured on the fly.
2331
  cmSystemTools::PutEnv("vsconsoleoutput=1");
2332
2333
#  ifndef CMAKE_BOOTSTRAP
2334
  // VS sets an environment variable to tell MS tools like "cl" to report
2335
  // output through a backdoor pipe instead of stdout/stderr.  Unset the
2336
  // environment variable to close this backdoor for any path of process
2337
  // invocations that passes through CMake so we can capture the output.
2338
  cmSystemTools::UnsetEnv("VS_UNICODE_OUTPUT");
2339
#  endif
2340
#endif
2341
35
}
2342
2343
bool cmSystemTools::IsPathToFramework(std::string const& path)
2344
0
{
2345
0
  return (cmSystemTools::FileIsFullPath(path) &&
2346
0
          cmHasLiteralSuffix(path, ".framework"));
2347
0
}
2348
2349
bool cmSystemTools::IsPathToXcFramework(std::string const& path)
2350
0
{
2351
0
  return (cmSystemTools::FileIsFullPath(path) &&
2352
0
          cmHasLiteralSuffix(path, ".xcframework"));
2353
0
}
2354
2355
bool cmSystemTools::IsPathToMacOSSharedLibrary(std::string const& path)
2356
0
{
2357
0
  return (cmSystemTools::FileIsFullPath(path) &&
2358
0
          cmHasLiteralSuffix(path, ".dylib"));
2359
0
}
2360
2361
bool cmSystemTools::CreateTar(std::string const& arFileName,
2362
                              std::vector<std::string> const& files,
2363
                              std::string const& workingDirectory,
2364
                              cmTarCompression compressType, bool verbose,
2365
                              std::string const& mtime,
2366
                              std::string const& format, int compressionLevel,
2367
                              int numThreads)
2368
0
{
2369
0
#if !defined(CMAKE_BOOTSTRAP)
2370
0
  cmWorkingDirectory workdir(cmSystemTools::GetLogicalWorkingDirectory());
2371
0
  if (!workingDirectory.empty()) {
2372
0
    workdir.SetDirectory(workingDirectory);
2373
0
  }
2374
2375
0
  std::string const cwd = cmSystemTools::GetLogicalWorkingDirectory();
2376
0
  cmsys::ofstream fout(arFileName.c_str(), std::ios::out | std::ios::binary);
2377
0
  if (!fout) {
2378
0
    std::string e = cmStrCat("Cannot open output file \"", arFileName,
2379
0
                             "\": ", cmSystemTools::GetLastSystemError());
2380
0
    cmSystemTools::Error(e);
2381
0
    return false;
2382
0
  }
2383
0
  cmArchiveWrite::Compress compress = cmArchiveWrite::CompressNone;
2384
0
  switch (compressType) {
2385
0
    case TarCompressGZip:
2386
0
      compress = cmArchiveWrite::CompressGZip;
2387
0
      break;
2388
0
    case TarCompressBZip2:
2389
0
      compress = cmArchiveWrite::CompressBZip2;
2390
0
      break;
2391
0
    case TarCompressXZ:
2392
0
      compress = cmArchiveWrite::CompressXZ;
2393
0
      break;
2394
0
    case TarCompressZstd:
2395
0
      compress = cmArchiveWrite::CompressZstd;
2396
0
      break;
2397
0
    case TarCompressLZMA:
2398
0
      compress = cmArchiveWrite::CompressLZMA;
2399
0
      break;
2400
0
    case TarCompressPPMd:
2401
0
      compress = cmArchiveWrite::CompressPPMd;
2402
0
      break;
2403
0
    case TarCompressAuto:
2404
      // Kept for backwards compatibility with pre-4.3 versions of CMake
2405
0
      if (format == "zip") {
2406
0
        compress = cmArchiveWrite::CompressGZip;
2407
0
      } else if (format == "7zip") {
2408
0
        compress = cmArchiveWrite::CompressLZMA;
2409
0
      } else {
2410
0
        compress = cmArchiveWrite::CompressNone;
2411
0
      }
2412
0
      break;
2413
0
    case TarCompressNone:
2414
0
      compress = cmArchiveWrite::CompressNone;
2415
0
      break;
2416
0
  }
2417
2418
0
  cmArchiveWrite a(fout, compress, format.empty() ? "paxr" : format,
2419
0
                   compressionLevel, numThreads);
2420
2421
0
  if (!a.Open()) {
2422
0
    cmSystemTools::Error(a.GetError());
2423
0
    return false;
2424
0
  }
2425
0
  a.SetMTime(mtime);
2426
0
  a.SetVerbose(verbose);
2427
0
  bool tarCreatedSuccessfully = true;
2428
0
  for (auto path : files) {
2429
0
    if (cmSystemTools::FileIsFullPath(path)) {
2430
      // Get the relative path to the file.
2431
0
      path = cmSystemTools::RelativePath(cwd, path);
2432
0
    }
2433
0
    if (!a.Add(path)) {
2434
0
      cmSystemTools::Error(a.GetError());
2435
0
      tarCreatedSuccessfully = false;
2436
0
    }
2437
0
  }
2438
0
  return tarCreatedSuccessfully;
2439
#else
2440
  (void)arFileName;
2441
  (void)files;
2442
  (void)verbose;
2443
  return false;
2444
#endif
2445
0
}
2446
2447
#if !defined(CMAKE_BOOTSTRAP)
2448
namespace {
2449
0
#  define BSDTAR_FILESIZE_PRINTF "%lu"
2450
#  define BSDTAR_FILESIZE_TYPE unsigned long
2451
void list_item_verbose(FILE* out, struct archive_entry* entry)
2452
0
{
2453
0
  char tmp[100];
2454
0
  size_t w;
2455
0
  char const* p;
2456
0
  char const* fmt;
2457
0
  time_t tim;
2458
0
  static time_t now;
2459
0
  size_t u_width = 6;
2460
0
  size_t gs_width = 13;
2461
2462
  /*
2463
   * We avoid collecting the entire list in memory at once by
2464
   * listing things as we see them.  However, that also means we can't
2465
   * just pre-compute the field widths.  Instead, we start with guesses
2466
   * and just widen them as necessary.  These numbers are completely
2467
   * arbitrary.
2468
   */
2469
0
  if (!now) {
2470
0
    time(&now);
2471
0
  }
2472
0
  fprintf(out, "%s %u ", archive_entry_strmode(entry),
2473
0
          archive_entry_nlink(entry));
2474
2475
  /* Use uname if it's present, else uid. */
2476
0
  p = archive_entry_uname(entry);
2477
0
  if (!p || (*p == '\0')) {
2478
0
    snprintf(tmp, sizeof(tmp), "%lu ",
2479
0
             static_cast<unsigned long>(archive_entry_uid(entry)));
2480
0
    p = tmp;
2481
0
  }
2482
0
  w = strlen(p);
2483
0
  if (w > u_width) {
2484
0
    u_width = w;
2485
0
  }
2486
0
  fprintf(out, "%-*s ", static_cast<int>(u_width), p);
2487
  /* Use gname if it's present, else gid. */
2488
0
  p = archive_entry_gname(entry);
2489
0
  if (p && p[0] != '\0') {
2490
0
    fprintf(out, "%s", p);
2491
0
    w = strlen(p);
2492
0
  } else {
2493
0
    snprintf(tmp, sizeof(tmp), "%lu",
2494
0
             static_cast<unsigned long>(archive_entry_gid(entry)));
2495
0
    w = strlen(tmp);
2496
0
    fprintf(out, "%s", tmp);
2497
0
  }
2498
2499
  /*
2500
   * Print device number or file size, right-aligned so as to make
2501
   * total width of group and devnum/filesize fields be gs_width.
2502
   * If gs_width is too small, grow it.
2503
   */
2504
0
  if (archive_entry_filetype(entry) == AE_IFCHR ||
2505
0
      archive_entry_filetype(entry) == AE_IFBLK) {
2506
0
    unsigned long rdevmajor = archive_entry_rdevmajor(entry);
2507
0
    unsigned long rdevminor = archive_entry_rdevminor(entry);
2508
0
    snprintf(tmp, sizeof(tmp), "%lu,%lu", rdevmajor, rdevminor);
2509
0
  } else {
2510
    /*
2511
     * Note the use of platform-dependent macros to format
2512
     * the filesize here.  We need the format string and the
2513
     * corresponding type for the cast.
2514
     */
2515
0
    snprintf(tmp, sizeof(tmp), BSDTAR_FILESIZE_PRINTF,
2516
0
             static_cast<BSDTAR_FILESIZE_TYPE>(archive_entry_size(entry)));
2517
0
  }
2518
0
  if (w + strlen(tmp) >= gs_width) {
2519
0
    gs_width = w + strlen(tmp) + 1;
2520
0
  }
2521
0
  fprintf(out, "%*s", static_cast<int>(gs_width - w), tmp);
2522
2523
  /* Format the time using 'ls -l' conventions. */
2524
0
  tim = archive_entry_mtime(entry);
2525
0
#  define HALF_YEAR ((time_t)365 * 86400 / 2)
2526
#  if defined(_WIN32) && !defined(__CYGWIN__)
2527
/* Windows' strftime function does not support %e format. */
2528
#    define DAY_FMT "%d"
2529
#  else
2530
0
#    define DAY_FMT "%e" /* Day number without leading zeros */
2531
0
#  endif
2532
0
  if (tim < now - HALF_YEAR || tim > now + HALF_YEAR) {
2533
0
    fmt = DAY_FMT " %b  %Y";
2534
0
  } else {
2535
0
    fmt = DAY_FMT " %b %H:%M";
2536
0
  }
2537
0
  strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
2538
0
  fprintf(out, " %s ", tmp);
2539
0
  fprintf(out, "%s", cm_archive_entry_pathname(entry));
2540
2541
  /* Extra information for links. */
2542
0
  if (archive_entry_hardlink(entry)) /* Hard link */
2543
0
  {
2544
0
    fprintf(out, " link to %s", archive_entry_hardlink(entry));
2545
0
  } else if (archive_entry_symlink(entry)) /* Symbolic link */
2546
0
  {
2547
0
    fprintf(out, " -> %s", archive_entry_symlink(entry));
2548
0
  }
2549
0
  fflush(out);
2550
0
}
2551
2552
void ArchiveError(char const* m1, struct archive* a)
2553
24.8k
{
2554
24.8k
  std::string message(m1);
2555
24.8k
  char const* m2 = archive_error_string(a);
2556
24.8k
  if (m2) {
2557
24.3k
    message += m2;
2558
24.3k
  }
2559
24.8k
  cmSystemTools::Error(message);
2560
24.8k
}
2561
2562
bool la_diagnostic(struct archive* ar, __LA_SSIZE_T r)
2563
6.06k
{
2564
  // See archive.h definition of ARCHIVE_OK for return values.
2565
2566
6.06k
  if (r >= ARCHIVE_OK) {
2567
2.37k
    return true;
2568
2.37k
  }
2569
2570
3.68k
  if (r >= ARCHIVE_WARN) {
2571
224
    char const* warn = archive_error_string(ar);
2572
224
    if (!warn) {
2573
224
      warn = "unknown warning";
2574
224
    }
2575
224
    std::cerr << "cmake -E tar: warning: " << warn << '\n';
2576
224
    return true;
2577
224
  }
2578
2579
  // Error.
2580
3.46k
  char const* err = archive_error_string(ar);
2581
3.46k
  if (!err) {
2582
266
    err = "unknown error";
2583
266
  }
2584
3.46k
  std::cerr << "cmake -E tar: error: " << err << '\n';
2585
3.46k
  return false;
2586
3.68k
}
2587
2588
// Return 'true' on success
2589
bool copy_data(struct archive* ar, struct archive* aw)
2590
4.27k
{
2591
4.27k
  long r;
2592
4.27k
  void const* buff;
2593
4.27k
  size_t size;
2594
4.27k
#  if defined(ARCHIVE_VERSION_NUMBER) && ARCHIVE_VERSION_NUMBER >= 3000000
2595
4.27k
  __LA_INT64_T offset;
2596
#  else
2597
  off_t offset;
2598
#  endif
2599
2600
5.57k
  for (;;) {
2601
    // See archive.h definition of ARCHIVE_OK for return values.
2602
5.57k
    r = archive_read_data_block(ar, &buff, &size, &offset);
2603
5.57k
    if (r == ARCHIVE_EOF) {
2604
808
      return true;
2605
808
    }
2606
4.76k
    if (!la_diagnostic(ar, r)) {
2607
3.46k
      return false;
2608
3.46k
    }
2609
    // See archive.h definition of ARCHIVE_OK for return values.
2610
4.76k
    __LA_SSIZE_T const w = archive_write_data_block(aw, buff, size, offset);
2611
1.29k
    if (!la_diagnostic(ar, w)) {
2612
0
      return false;
2613
0
    }
2614
1.29k
  }
2615
#  if !defined(__clang__) && !defined(__NVCOMPILER) && !defined(__HP_aCC)
2616
  return false; /* this should not happen but it quiets some compilers */
2617
#  endif
2618
4.27k
}
2619
2620
bool extract_tar(std::string const& arFileName,
2621
                 std::vector<std::string> const& files, bool verbose,
2622
                 cmSystemTools::cmTarExtractTimestamps extractTimestamps,
2623
                 bool extract)
2624
29.0k
{
2625
29.0k
  struct archive* a = archive_read_new();
2626
29.0k
  struct archive* ext = archive_write_disk_new();
2627
29.0k
  if (extract) {
2628
29.0k
    int flags = ARCHIVE_EXTRACT_SECURE_NODOTDOT |
2629
29.0k
      ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS | ARCHIVE_EXTRACT_SECURE_SYMLINKS;
2630
29.0k
    if (extractTimestamps == cmSystemTools::cmTarExtractTimestamps::Yes) {
2631
14.5k
      flags |= ARCHIVE_EXTRACT_TIME;
2632
14.5k
    }
2633
29.0k
    if (archive_write_disk_set_options(ext, flags) != ARCHIVE_OK) {
2634
0
      ArchiveError("Problem with archive_write_disk_set_options(): ", ext);
2635
0
      archive_write_free(ext);
2636
0
      archive_read_free(a);
2637
0
      return false;
2638
0
    }
2639
29.0k
  }
2640
29.0k
  archive_read_support_filter_all(a);
2641
29.0k
  archive_read_support_format_all(a);
2642
29.0k
  struct archive_entry* entry;
2643
2644
29.0k
  struct archive* matching = archive_match_new();
2645
29.0k
  if (!matching) {
2646
0
    cmSystemTools::Error("Out of memory");
2647
0
    return false;
2648
0
  }
2649
2650
29.0k
  for (auto const& filename : files) {
2651
0
    if (archive_match_include_pattern(matching, filename.c_str()) !=
2652
0
        ARCHIVE_OK) {
2653
0
      cmSystemTools::Error("Failed to add to inclusion list: " + filename);
2654
0
      return false;
2655
0
    }
2656
0
  }
2657
2658
29.0k
  int r = cm_archive_read_open_filename(a, arFileName.c_str(), 10240);
2659
29.0k
  if (r) {
2660
18.1k
    ArchiveError("Problem with archive_read_open_filename(): ", a);
2661
18.1k
    archive_write_free(ext);
2662
18.1k
    archive_read_close(a);
2663
18.1k
    archive_read_free(a);
2664
18.1k
    archive_match_free(matching);
2665
18.1k
    return false;
2666
18.1k
  }
2667
11.7k
  for (;;) {
2668
11.7k
    r = archive_read_next_header(a, &entry);
2669
11.7k
    if (r == ARCHIVE_EOF) {
2670
808
      break;
2671
808
    }
2672
10.9k
    if (r != ARCHIVE_OK) {
2673
6.24k
      ArchiveError("Problem with archive_read_next_header(): ", a);
2674
6.24k
      break;
2675
6.24k
    }
2676
2677
4.65k
    if (archive_match_excluded(matching, entry)) {
2678
0
      continue;
2679
0
    }
2680
2681
4.65k
    if (verbose) {
2682
2.32k
      if (extract) {
2683
2.32k
        cmSystemTools::Stdout(
2684
2.32k
          cmStrCat("x ", cm_archive_entry_pathname(entry)));
2685
2.32k
      } else {
2686
0
        list_item_verbose(stdout, entry);
2687
0
      }
2688
2.32k
      cmSystemTools::Stdout("\n");
2689
2.32k
    } else if (!extract) {
2690
0
      cmSystemTools::Stdout(cmStrCat(cm_archive_entry_pathname(entry), '\n'));
2691
0
    }
2692
4.65k
    if (extract) {
2693
4.65k
      r = archive_write_header(ext, entry);
2694
4.65k
      if (r == ARCHIVE_OK) {
2695
4.27k
        if (!copy_data(a, ext)) {
2696
3.46k
          break;
2697
3.46k
        }
2698
808
        r = archive_write_finish_entry(ext);
2699
808
        if (r != ARCHIVE_OK) {
2700
2
          ArchiveError("Problem with archive_write_finish_entry(): ", ext);
2701
2
          break;
2702
2
        }
2703
808
      }
2704
#  ifdef _WIN32
2705
      else if (char const* linktext = archive_entry_symlink(entry)) {
2706
        std::cerr << "cmake -E tar: warning: skipping symbolic link \""
2707
                  << cm_archive_entry_pathname(entry) << "\" -> \"" << linktext
2708
                  << "\"." << std::endl;
2709
      }
2710
#  endif
2711
384
      else {
2712
384
        ArchiveError("Problem with archive_write_header(): ", ext);
2713
384
        cmSystemTools::Error(
2714
384
          cmStrCat("Current file:\n  ", cm_archive_entry_pathname(entry)));
2715
384
        break;
2716
384
      }
2717
4.65k
    }
2718
4.65k
  }
2719
2720
10.9k
  bool error_occurred = false;
2721
10.9k
  if (matching) {
2722
10.9k
    char const* p;
2723
10.9k
    int ar;
2724
2725
10.9k
    while ((ar = archive_match_path_unmatched_inclusions_next(matching, &p)) ==
2726
10.9k
           ARCHIVE_OK) {
2727
0
      cmSystemTools::Error("tar: " + std::string(p) +
2728
0
                           ": Not found in archive");
2729
0
      error_occurred = true;
2730
0
    }
2731
10.9k
    if (error_occurred) {
2732
0
      return false;
2733
0
    }
2734
10.9k
    if (ar == ARCHIVE_FATAL) {
2735
0
      cmSystemTools::Error("tar: Out of memory");
2736
0
      return false;
2737
0
    }
2738
10.9k
  }
2739
10.9k
  archive_match_free(matching);
2740
10.9k
  archive_write_free(ext);
2741
10.9k
  archive_read_close(a);
2742
10.9k
  archive_read_free(a);
2743
10.9k
  return r == ARCHIVE_EOF || r == ARCHIVE_OK;
2744
10.9k
}
2745
}
2746
#endif
2747
2748
bool cmSystemTools::ExtractTar(std::string const& arFileName,
2749
                               std::vector<std::string> const& files,
2750
                               cmTarExtractTimestamps extractTimestamps,
2751
                               bool verbose)
2752
29.0k
{
2753
29.0k
#if !defined(CMAKE_BOOTSTRAP)
2754
29.0k
  return extract_tar(arFileName, files, verbose, extractTimestamps, true);
2755
#else
2756
  (void)arFileName;
2757
  (void)files;
2758
  (void)extractTimestamps;
2759
  (void)verbose;
2760
  return false;
2761
#endif
2762
29.0k
}
2763
2764
bool cmSystemTools::ListTar(std::string const& arFileName,
2765
                            std::vector<std::string> const& files,
2766
                            bool verbose)
2767
0
{
2768
0
#if !defined(CMAKE_BOOTSTRAP)
2769
0
  return extract_tar(arFileName, files, verbose, cmTarExtractTimestamps::Yes,
2770
0
                     false);
2771
#else
2772
  (void)arFileName;
2773
  (void)files;
2774
  (void)verbose;
2775
  return false;
2776
#endif
2777
0
}
2778
2779
cmSystemTools::WaitForLineResult cmSystemTools::WaitForLine(
2780
  uv_loop_t* loop, uv_stream_t* outPipe, uv_stream_t* errPipe,
2781
  std::string& line, std::vector<char>& out, std::vector<char>& err)
2782
0
{
2783
0
  line.clear();
2784
0
  auto outiter = out.begin();
2785
0
  auto erriter = err.begin();
2786
0
  cmProcessOutput processOutput;
2787
0
  std::string strdata;
2788
0
  while (true) {
2789
    // Check for a newline in stdout.
2790
0
    for (; outiter != out.end(); ++outiter) {
2791
0
      if ((*outiter == '\r') && ((outiter + 1) == out.end())) {
2792
0
        break;
2793
0
      }
2794
0
      if (*outiter == '\n' || *outiter == '\0') {
2795
0
        std::vector<char>::size_type length = outiter - out.begin();
2796
0
        if (length > 1 && *(outiter - 1) == '\r') {
2797
0
          --length;
2798
0
        }
2799
0
        if (length > 0) {
2800
0
          line.append(out.data(), length);
2801
0
        }
2802
0
        out.erase(out.begin(), outiter + 1);
2803
0
        return WaitForLineResult::STDOUT;
2804
0
      }
2805
0
    }
2806
2807
    // Check for a newline in stderr.
2808
0
    for (; erriter != err.end(); ++erriter) {
2809
0
      if ((*erriter == '\r') && ((erriter + 1) == err.end())) {
2810
0
        break;
2811
0
      }
2812
0
      if (*erriter == '\n' || *erriter == '\0') {
2813
0
        std::vector<char>::size_type length = erriter - err.begin();
2814
0
        if (length > 1 && *(erriter - 1) == '\r') {
2815
0
          --length;
2816
0
        }
2817
0
        if (length > 0) {
2818
0
          line.append(err.data(), length);
2819
0
        }
2820
0
        err.erase(err.begin(), erriter + 1);
2821
0
        return WaitForLineResult::STDERR;
2822
0
      }
2823
0
    }
2824
2825
    // No newlines found.  Wait for more data from the process.
2826
0
    struct ReadData
2827
0
    {
2828
0
      uv_stream_t* Stream;
2829
0
      std::vector<char> Buffer;
2830
0
      bool Read = false;
2831
0
      bool Finished = false;
2832
0
    };
2833
0
    auto startRead =
2834
0
      [](uv_stream_t* stream,
2835
0
         ReadData& data) -> std::unique_ptr<cmUVStreamReadHandle> {
2836
0
      data.Stream = stream;
2837
0
      return cmUVStreamRead(
2838
0
        stream,
2839
0
        [&data](std::vector<char> buf) {
2840
0
          data.Buffer = std::move(buf);
2841
0
          data.Read = true;
2842
0
          uv_read_stop(data.Stream);
2843
0
        },
2844
0
        [&data]() { data.Finished = true; });
2845
0
    };
2846
0
    ReadData outData;
2847
0
    auto outHandle = startRead(outPipe, outData);
2848
0
    ReadData errData;
2849
0
    auto errHandle = startRead(errPipe, errData);
2850
2851
0
    uv_run(loop, UV_RUN_ONCE);
2852
0
    if (outData.Read) {
2853
0
      processOutput.DecodeText(outData.Buffer.data(), outData.Buffer.size(),
2854
0
                               strdata, 1);
2855
      // Append to the stdout buffer.
2856
0
      std::vector<char>::size_type size = out.size();
2857
0
      cm::append(out, strdata);
2858
0
      outiter = out.begin() + size;
2859
0
    } else if (errData.Read) {
2860
0
      processOutput.DecodeText(errData.Buffer.data(), errData.Buffer.size(),
2861
0
                               strdata, 2);
2862
      // Append to the stderr buffer.
2863
0
      std::vector<char>::size_type size = err.size();
2864
0
      cm::append(err, strdata);
2865
0
      erriter = err.begin() + size;
2866
0
    } else if (outData.Finished && errData.Finished) {
2867
      // Both stdout and stderr pipes have broken.  Return leftover data.
2868
0
      processOutput.DecodeText(std::string(), strdata, 1);
2869
0
      if (!strdata.empty()) {
2870
0
        std::vector<char>::size_type size = out.size();
2871
0
        cm::append(out, strdata);
2872
0
        outiter = out.begin() + size;
2873
0
      }
2874
0
      processOutput.DecodeText(std::string(), strdata, 2);
2875
0
      if (!strdata.empty()) {
2876
0
        std::vector<char>::size_type size = err.size();
2877
0
        cm::append(err, strdata);
2878
0
        erriter = err.begin() + size;
2879
0
      }
2880
0
      if (!out.empty()) {
2881
0
        line.append(out.data(), outiter - out.begin());
2882
0
        out.erase(out.begin(), out.end());
2883
0
        return WaitForLineResult::STDOUT;
2884
0
      }
2885
0
      if (!err.empty()) {
2886
0
        line.append(err.data(), erriter - err.begin());
2887
0
        err.erase(err.begin(), err.end());
2888
0
        return WaitForLineResult::STDERR;
2889
0
      }
2890
0
      return WaitForLineResult::None;
2891
0
    }
2892
0
    if (!outData.Finished) {
2893
0
      uv_read_stop(outPipe);
2894
0
    }
2895
0
    if (!errData.Finished) {
2896
0
      uv_read_stop(errPipe);
2897
0
    }
2898
0
  }
2899
0
}
2900
2901
#ifdef _WIN32
2902
#  ifndef CRYPT_SILENT
2903
#    define CRYPT_SILENT 0x40 /* Not defined by VS 6 version of header.  */
2904
#  endif
2905
static int WinCryptRandom(void* data, size_t size)
2906
{
2907
  int result = 0;
2908
  HCRYPTPROV hProvider = 0;
2909
  if (CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL,
2910
                           CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
2911
    result = CryptGenRandom(hProvider, (DWORD)size, (BYTE*)data) ? 1 : 0;
2912
    CryptReleaseContext(hProvider, 0);
2913
  }
2914
  return result;
2915
}
2916
#endif
2917
2918
unsigned int cmSystemTools::RandomSeed()
2919
0
{
2920
#if defined(_WIN32) && !defined(__CYGWIN__)
2921
  unsigned int seed = 0;
2922
2923
  // Try using a real random source.
2924
  if (WinCryptRandom(&seed, sizeof(seed))) {
2925
    return seed;
2926
  }
2927
2928
  // Fall back to the time and pid.
2929
  FILETIME ft;
2930
  GetSystemTimeAsFileTime(&ft);
2931
  unsigned int t1 = static_cast<unsigned int>(ft.dwHighDateTime);
2932
  unsigned int t2 = static_cast<unsigned int>(ft.dwLowDateTime);
2933
  unsigned int pid = static_cast<unsigned int>(GetCurrentProcessId());
2934
  return t1 ^ t2 ^ pid;
2935
#else
2936
0
  union
2937
0
  {
2938
0
    unsigned int integer;
2939
0
    char bytes[sizeof(unsigned int)];
2940
0
  } seed;
2941
2942
  // Try using a real random source.
2943
0
  cmsys::ifstream fin;
2944
0
  fin.rdbuf()->pubsetbuf(nullptr, 0); // Unbuffered read.
2945
0
  fin.open("/dev/urandom");
2946
0
  if (fin.good() && fin.read(seed.bytes, sizeof(seed)) &&
2947
0
      fin.gcount() == sizeof(seed)) {
2948
0
    return seed.integer;
2949
0
  }
2950
2951
  // Fall back to the time and pid.
2952
0
  struct timeval t;
2953
0
  gettimeofday(&t, nullptr);
2954
0
  unsigned int pid = static_cast<unsigned int>(getpid());
2955
0
  unsigned int tv_sec = static_cast<unsigned int>(t.tv_sec);
2956
0
  unsigned int tv_usec = static_cast<unsigned int>(t.tv_usec);
2957
  // Since tv_usec never fills more than 11 bits we shift it to fill
2958
  // in the slow-changing high-order bits of tv_sec.
2959
0
  return tv_sec ^ (tv_usec << 21) ^ pid;
2960
0
#endif
2961
0
}
2962
2963
unsigned int cmSystemTools::RandomNumber()
2964
0
{
2965
#ifndef CM_HAVE_THREAD_LOCAL
2966
  static std::mutex gen_mutex;
2967
  std::lock_guard<std::mutex> gen_mutex_lock(gen_mutex);
2968
#else
2969
0
  thread_local
2970
0
#endif
2971
0
  static std::mt19937 gen{ cmSystemTools::RandomSeed() };
2972
0
  return static_cast<unsigned int>(gen());
2973
0
}
2974
2975
std::string cmSystemTools::FindProgram(std::string const& name,
2976
                                       std::vector<std::string> const& path)
2977
0
{
2978
0
  std::string exe = cmsys::SystemTools::FindProgram(name, path);
2979
0
  if (!exe.empty()) {
2980
0
    exe = cmSystemTools::ToNormalizedPathOnDisk(std::move(exe));
2981
0
  }
2982
0
  return exe;
2983
0
}
2984
2985
namespace {
2986
std::string InitLogicalWorkingDirectory()
2987
10
{
2988
10
  std::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
2989
10
  std::string pwd;
2990
10
  if (cmSystemTools::GetEnv("PWD", pwd) &&
2991
10
      cmSystemTools::FileIsFullPath(pwd)) {
2992
10
    std::string const pwd_real = cmSystemTools::GetRealPath(pwd);
2993
10
    if (pwd_real == cwd) {
2994
10
      cwd = cmSystemTools::ToNormalizedPathOnDisk(std::move(pwd));
2995
10
    }
2996
10
  }
2997
10
  return cwd;
2998
10
}
2999
3000
std::string cmSystemToolsLogicalWorkingDirectory =
3001
  InitLogicalWorkingDirectory();
3002
3003
std::string cmSystemToolsCMakeCommand;
3004
std::string cmSystemToolsCTestCommand;
3005
std::string cmSystemToolsCPackCommand;
3006
std::string cmSystemToolsCMakeCursesCommand;
3007
std::string cmSystemToolsCMakeGUICommand;
3008
std::string cmSystemToolsCMClDepsCommand;
3009
std::string cmSystemToolsCMakeRoot;
3010
std::string cmSystemToolsHTMLDoc;
3011
3012
#if defined(__APPLE__)
3013
bool IsCMakeAppBundleExe(std::string const& exe)
3014
{
3015
  return cmHasLiteralSuffix(cmSystemTools::LowerCase(exe), "/macos/cmake");
3016
}
3017
#endif
3018
3019
std::string FindOwnExecutable(char const* argv0)
3020
0
{
3021
#if defined(_WIN32) && !defined(__CYGWIN__)
3022
  static_cast<void>(argv0);
3023
  wchar_t modulepath[_MAX_PATH];
3024
  ::GetModuleFileNameW(nullptr, modulepath, sizeof(modulepath));
3025
  std::string exe = cmsys::Encoding::ToNarrow(modulepath);
3026
#elif defined(__APPLE__)
3027
  static_cast<void>(argv0);
3028
#  define CM_EXE_PATH_LOCAL_SIZE 16384
3029
  char exe_path_local[CM_EXE_PATH_LOCAL_SIZE];
3030
#  if defined(MAC_OS_X_VERSION_10_3) && !defined(MAC_OS_X_VERSION_10_4)
3031
  unsigned long exe_path_size = CM_EXE_PATH_LOCAL_SIZE;
3032
#  else
3033
  uint32_t exe_path_size = CM_EXE_PATH_LOCAL_SIZE;
3034
#  endif
3035
#  undef CM_EXE_PATH_LOCAL_SIZE
3036
  char* exe_path = exe_path_local;
3037
  if (_NSGetExecutablePath(exe_path, &exe_path_size) < 0) {
3038
    exe_path = static_cast<char*>(malloc(exe_path_size));
3039
    _NSGetExecutablePath(exe_path, &exe_path_size);
3040
  }
3041
  std::string exe = exe_path;
3042
  if (exe_path != exe_path_local) {
3043
    free(exe_path);
3044
  }
3045
  if (IsCMakeAppBundleExe(exe)) {
3046
    // The executable is inside an application bundle.
3047
    // The install tree has "..<CMAKE_BIN_DIR>/cmake-gui".
3048
    // The build tree has '../../../cmake-gui".
3049
    std::string dir = cmSystemTools::GetFilenamePath(exe);
3050
    dir = cmSystemTools::GetFilenamePath(dir);
3051
    exe = cmStrCat(dir, CMAKE_BIN_DIR "/cmake-gui");
3052
    if (!cmSystemTools::PathExists(exe)) {
3053
      dir = cmSystemTools::GetFilenamePath(dir);
3054
      dir = cmSystemTools::GetFilenamePath(dir);
3055
      exe = cmStrCat(dir, "/cmake-gui");
3056
    }
3057
  }
3058
#else
3059
0
  std::string exe = cmsys::SystemTools::FindProgram(argv0);
3060
0
#endif
3061
0
  exe = cmSystemTools::ToNormalizedPathOnDisk(std::move(exe));
3062
0
  return exe;
3063
0
}
3064
3065
#ifndef CMAKE_BOOTSTRAP
3066
bool ResolveSymlinkToOwnExecutable(std::string& exe, std::string& exe_dir)
3067
0
{
3068
0
  std::string linked_exe;
3069
0
  if (!cmSystemTools::ReadSymlink(exe, linked_exe)) {
3070
0
    return false;
3071
0
  }
3072
#  if defined(__APPLE__)
3073
  // Ignore "cmake-gui -> ../MacOS/CMake".
3074
  if (IsCMakeAppBundleExe(linked_exe)) {
3075
    return false;
3076
  }
3077
#  endif
3078
0
  if (cmSystemTools::FileIsFullPath(linked_exe)) {
3079
0
    exe = std::move(linked_exe);
3080
0
  } else {
3081
0
    exe = cmStrCat(exe_dir, '/', std::move(linked_exe));
3082
0
  }
3083
0
  exe = cmSystemTools::ToNormalizedPathOnDisk(std::move(exe));
3084
0
  exe_dir = cmSystemTools::GetFilenamePath(exe);
3085
0
  return true;
3086
0
}
3087
3088
bool FindCMakeResourcesInInstallTree(std::string const& exe_dir)
3089
0
{
3090
  // Install tree has
3091
  // - "<prefix><CMAKE_BIN_DIR>/cmake"
3092
  // - "<prefix><CMAKE_DATA_DIR>"
3093
  // - "<prefix><CMAKE_DOC_DIR>"
3094
0
  if (cmHasLiteralSuffix(exe_dir, CMAKE_BIN_DIR)) {
3095
0
    std::string const prefix =
3096
0
      exe_dir.substr(0, exe_dir.size() - cmStrLen(CMAKE_BIN_DIR));
3097
    // Set cmSystemToolsCMakeRoot set to the location expected in an
3098
    // install tree, even if it does not exist, so that
3099
    // cmake::AddCMakePaths can print the location in its error message.
3100
0
    cmSystemToolsCMakeRoot = cmStrCat(prefix, CMAKE_DATA_DIR);
3101
0
    if (cmSystemTools::FileExists(
3102
0
          cmStrCat(cmSystemToolsCMakeRoot, "/Modules/CMake.cmake"))) {
3103
0
      if (cmSystemTools::FileExists(
3104
0
            cmStrCat(prefix, CMAKE_DOC_DIR "/html/index.html"))) {
3105
0
        cmSystemToolsHTMLDoc = cmStrCat(prefix, CMAKE_DOC_DIR "/html");
3106
0
      }
3107
0
      return true;
3108
0
    }
3109
0
  }
3110
0
  return false;
3111
0
}
3112
3113
void FindCMakeResourcesInBuildTree(std::string const& exe_dir)
3114
0
{
3115
  // Build tree has "<build>/bin[/<config>]/cmake" and
3116
  // "<build>/CMakeFiles/CMakeSourceDir.txt".
3117
0
  std::string dir = cmSystemTools::GetFilenamePath(exe_dir);
3118
0
  std::string src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt");
3119
0
  cmsys::ifstream fin(src_dir_txt.c_str());
3120
0
  std::string src_dir;
3121
0
  if (fin && cmSystemTools::GetLineFromStream(fin, src_dir) &&
3122
0
      cmSystemTools::FileIsDirectory(src_dir)) {
3123
0
    cmSystemToolsCMakeRoot = src_dir;
3124
0
  } else {
3125
0
    dir = cmSystemTools::GetFilenamePath(dir);
3126
0
    src_dir_txt = cmStrCat(dir, "/CMakeFiles/CMakeSourceDir.txt");
3127
0
    cmsys::ifstream fin2(src_dir_txt.c_str());
3128
0
    if (fin2 && cmSystemTools::GetLineFromStream(fin2, src_dir) &&
3129
0
        cmSystemTools::FileIsDirectory(src_dir)) {
3130
0
      cmSystemToolsCMakeRoot = src_dir;
3131
0
    }
3132
0
  }
3133
0
  if (!cmSystemToolsCMakeRoot.empty() && cmSystemToolsHTMLDoc.empty() &&
3134
0
      cmSystemTools::FileExists(
3135
0
        cmStrCat(dir, "/Utilities/Sphinx/html/index.html"))) {
3136
0
    cmSystemToolsHTMLDoc = cmStrCat(dir, "/Utilities/Sphinx/html");
3137
0
  }
3138
0
}
3139
#endif
3140
}
3141
3142
void cmSystemTools::FindCMakeResources(char const* argv0)
3143
0
{
3144
0
  std::string exe = FindOwnExecutable(argv0);
3145
#ifdef CMAKE_BOOTSTRAP
3146
  // The bootstrap cmake knows its resource locations.
3147
  cmSystemToolsCMakeRoot = CMAKE_BOOTSTRAP_SOURCE_DIR;
3148
  cmSystemToolsCMakeCommand = exe;
3149
  // The bootstrap cmake does not provide the other tools,
3150
  // so use the directory where they are about to be built.
3151
  std::string exe_dir = CMAKE_BOOTSTRAP_BINARY_DIR "/bin";
3152
#else
3153
  // Find resources relative to our own executable.
3154
0
  std::string exe_dir = cmSystemTools::GetFilenamePath(exe);
3155
0
  bool found = false;
3156
  // When running through a symlink to our own executable,
3157
  // preserve symlinks in directory components if possible.
3158
0
  do {
3159
0
    found = FindCMakeResourcesInInstallTree(exe_dir);
3160
0
  } while (!found && ResolveSymlinkToOwnExecutable(exe, exe_dir));
3161
  // If we have not yet found the resources, the above loop will
3162
  // have left 'exe' referring to a real file, not a symlink, so
3163
  // all our binaries should exist under 'exe_dir'.  However, the
3164
  // resources may be discoverable only in the real path.
3165
0
  if (!found) {
3166
0
    found =
3167
0
      FindCMakeResourcesInInstallTree(cmSystemTools::GetRealPath(exe_dir));
3168
0
  }
3169
0
  if (!found) {
3170
0
    FindCMakeResourcesInBuildTree(exe_dir);
3171
0
  }
3172
0
  cmSystemToolsCMakeCommand =
3173
0
    cmStrCat(exe_dir, "/cmake", cmSystemTools::GetExecutableExtension());
3174
0
#endif
3175
0
  cmSystemToolsCTestCommand =
3176
0
    cmStrCat(exe_dir, "/ctest", cmSystemTools::GetExecutableExtension());
3177
0
  cmSystemToolsCPackCommand =
3178
0
    cmStrCat(exe_dir, "/cpack", cmSystemTools::GetExecutableExtension());
3179
0
  cmSystemToolsCMakeGUICommand =
3180
0
    cmStrCat(exe_dir, "/cmake-gui", cmSystemTools::GetExecutableExtension());
3181
0
  if (!cmSystemTools::FileExists(cmSystemToolsCMakeGUICommand)) {
3182
0
    cmSystemToolsCMakeGUICommand.clear();
3183
0
  }
3184
0
  cmSystemToolsCMakeCursesCommand =
3185
0
    cmStrCat(exe_dir, "/ccmake", cmSystemTools::GetExecutableExtension());
3186
0
  if (!cmSystemTools::FileExists(cmSystemToolsCMakeCursesCommand)) {
3187
0
    cmSystemToolsCMakeCursesCommand.clear();
3188
0
  }
3189
0
  cmSystemToolsCMClDepsCommand =
3190
0
    cmStrCat(exe_dir, "/cmcldeps", cmSystemTools::GetExecutableExtension());
3191
0
  if (!cmSystemTools::FileExists(cmSystemToolsCMClDepsCommand)) {
3192
0
    cmSystemToolsCMClDepsCommand.clear();
3193
0
  }
3194
0
}
3195
3196
std::string const& cmSystemTools::GetCMakeCommand()
3197
1
{
3198
1
  return cmSystemToolsCMakeCommand;
3199
1
}
3200
3201
std::string const& cmSystemTools::GetCTestCommand()
3202
1
{
3203
1
  return cmSystemToolsCTestCommand;
3204
1
}
3205
3206
std::string const& cmSystemTools::GetCPackCommand()
3207
1
{
3208
1
  return cmSystemToolsCPackCommand;
3209
1
}
3210
3211
std::string const& cmSystemTools::GetCMakeCursesCommand()
3212
0
{
3213
0
  return cmSystemToolsCMakeCursesCommand;
3214
0
}
3215
3216
std::string const& cmSystemTools::GetCMakeGUICommand()
3217
0
{
3218
0
  return cmSystemToolsCMakeGUICommand;
3219
0
}
3220
3221
std::string const& cmSystemTools::GetCMClDepsCommand()
3222
0
{
3223
0
  return cmSystemToolsCMClDepsCommand;
3224
0
}
3225
3226
std::string const& cmSystemTools::GetCMakeRoot()
3227
2
{
3228
2
  return cmSystemToolsCMakeRoot;
3229
2
}
3230
3231
std::string const& cmSystemTools::GetHTMLDoc()
3232
0
{
3233
0
  return cmSystemToolsHTMLDoc;
3234
0
}
3235
3236
cm::optional<std::string> cmSystemTools::GetSystemConfigDirectory()
3237
0
{
3238
#if defined(_WIN32)
3239
  LPWSTR lpwstr;
3240
  if (FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &lpwstr))) {
3241
    return cm::nullopt;
3242
  }
3243
  std::wstring wstr = std::wstring(lpwstr);
3244
  CoTaskMemFree(lpwstr);
3245
  std::string config = cmsys::Encoding::ToNarrow(wstr);
3246
  cmSystemTools::ConvertToUnixSlashes(config);
3247
  return config;
3248
#else
3249
0
  auto config = cmSystemTools::GetEnvVar("XDG_CONFIG_HOME");
3250
0
  if (!config.has_value()) {
3251
0
    config = cmSystemTools::GetEnvVar("HOME");
3252
0
    if (config.has_value()) {
3253
#  if defined(__APPLE__)
3254
      config = cmStrCat(config.value(), "/Library/Application Support");
3255
#  else
3256
0
      config = cmStrCat(config.value(), "/.config");
3257
0
#  endif
3258
0
    }
3259
0
  }
3260
0
  return config;
3261
0
#endif
3262
0
}
3263
3264
cm::optional<std::string> cmSystemTools::GetCMakeConfigDirectory()
3265
0
{
3266
0
  auto config = cmSystemTools::GetEnvVar("CMAKE_CONFIG_DIR");
3267
0
  if (!config.has_value()) {
3268
0
    config = cmSystemTools::GetSystemConfigDirectory();
3269
0
    if (config.has_value()) {
3270
#if defined(_WIN32) || defined(__APPLE__)
3271
      config = cmStrCat(config.value(), "/CMake");
3272
#else
3273
0
      config = cmStrCat(config.value(), "/cmake");
3274
0
#endif
3275
0
    }
3276
0
  }
3277
0
  return config;
3278
0
}
3279
3280
std::string const& cmSystemTools::GetLogicalWorkingDirectory()
3281
37
{
3282
37
  return cmSystemToolsLogicalWorkingDirectory;
3283
37
}
3284
3285
cmsys::Status cmSystemTools::SetLogicalWorkingDirectory(std::string const& lwd)
3286
0
{
3287
0
  cmsys::Status status = cmSystemTools::ChangeDirectory(lwd);
3288
0
  if (status) {
3289
0
    cmSystemToolsLogicalWorkingDirectory = lwd;
3290
0
  }
3291
0
  return status;
3292
0
}
3293
3294
bool cmSystemTools::GuessLibrarySOName(std::string const& fullPath,
3295
                                       std::string& soname)
3296
0
{
3297
  // For ELF shared libraries use a real parser to get the correct
3298
  // soname.
3299
0
  cmELF elf(fullPath.c_str());
3300
0
  if (elf) {
3301
0
    return elf.GetSOName(soname);
3302
0
  }
3303
3304
  // If the file is not a symlink we have no guess for its soname.
3305
0
  if (!cmSystemTools::FileIsSymlink(fullPath)) {
3306
0
    return false;
3307
0
  }
3308
0
  if (!cmSystemTools::ReadSymlink(fullPath, soname)) {
3309
0
    return false;
3310
0
  }
3311
3312
  // If the symlink has a path component we have no guess for the soname.
3313
0
  if (!cmSystemTools::GetFilenamePath(soname).empty()) {
3314
0
    return false;
3315
0
  }
3316
3317
  // If the symlink points at an extended version of the same name
3318
  // assume it is the soname.
3319
0
  std::string name = cmSystemTools::GetFilenameName(fullPath);
3320
0
  return soname.length() > name.length() &&
3321
0
    soname.compare(0, name.length(), name) == 0;
3322
0
}
3323
3324
bool cmSystemTools::GuessLibraryInstallName(std::string const& fullPath,
3325
                                            std::string& soname)
3326
0
{
3327
#if defined(CMake_USE_MACH_PARSER)
3328
  cmMachO macho(fullPath.c_str());
3329
  if (macho) {
3330
    return macho.GetInstallName(soname);
3331
  }
3332
#else
3333
0
  (void)fullPath;
3334
0
  (void)soname;
3335
0
#endif
3336
3337
0
  return false;
3338
0
}
3339
3340
static std::string::size_type cmSystemToolsFindRPath(cm::string_view have,
3341
                                                     cm::string_view want)
3342
0
{
3343
0
  std::string::size_type pos = 0;
3344
0
  while (pos < have.size()) {
3345
    // Look for an occurrence of the string.
3346
0
    std::string::size_type const beg = have.find(want, pos);
3347
0
    if (beg == std::string::npos) {
3348
0
      return std::string::npos;
3349
0
    }
3350
3351
    // Make sure it is separated from preceding entries.
3352
0
    if (beg > 0 && have[beg - 1] != ':') {
3353
0
      pos = beg + 1;
3354
0
      continue;
3355
0
    }
3356
3357
    // Make sure it is separated from following entries.
3358
0
    std::string::size_type const end = beg + want.size();
3359
0
    if (end < have.size() && have[end] != ':') {
3360
0
      pos = beg + 1;
3361
0
      continue;
3362
0
    }
3363
3364
    // Return the position of the path portion.
3365
0
    return beg;
3366
0
  }
3367
3368
  // The desired rpath was not found.
3369
0
  return std::string::npos;
3370
0
}
3371
3372
namespace {
3373
struct cmSystemToolsRPathInfo
3374
{
3375
  unsigned long Position;
3376
  unsigned long Size;
3377
  std::string Name;
3378
  std::string Value;
3379
};
3380
3381
using EmptyCallback = std::function<bool(std::string*, cmELF const&)>;
3382
using AdjustCallback = std::function<bool(
3383
  cm::optional<std::string>&, std::string const&, char const*, std::string*)>;
3384
3385
cm::optional<bool> AdjustRPathELF(std::string const& file,
3386
                                  EmptyCallback const& emptyCallback,
3387
                                  AdjustCallback const& adjustCallback,
3388
                                  std::string* emsg, bool* changed)
3389
0
{
3390
0
  if (changed) {
3391
0
    *changed = false;
3392
0
  }
3393
0
  int rp_count = 0;
3394
0
  bool remove_rpath = true;
3395
0
  cmSystemToolsRPathInfo rp[2];
3396
0
  {
3397
    // Parse the ELF binary.
3398
0
    cmELF elf(file.c_str());
3399
0
    if (!elf) {
3400
0
      return cm::nullopt; // Not a valid ELF file.
3401
0
    }
3402
3403
0
    if (!elf.HasDynamicSection()) {
3404
0
      return true; // No dynamic section to update.
3405
0
    }
3406
3407
    // Get the RPATH and RUNPATH entries from it.
3408
0
    int se_count = 0;
3409
0
    cmELF::StringEntry const* se[2] = { nullptr, nullptr };
3410
0
    char const* se_name[2] = { nullptr, nullptr };
3411
0
    if (cmELF::StringEntry const* se_rpath = elf.GetRPath()) {
3412
0
      se[se_count] = se_rpath;
3413
0
      se_name[se_count] = "RPATH";
3414
0
      ++se_count;
3415
0
    }
3416
0
    if (cmELF::StringEntry const* se_runpath = elf.GetRunPath()) {
3417
0
      se[se_count] = se_runpath;
3418
0
      se_name[se_count] = "RUNPATH";
3419
0
      ++se_count;
3420
0
    }
3421
0
    if (se_count == 0) {
3422
0
      return emptyCallback(emsg, elf);
3423
0
    }
3424
3425
0
    for (int i = 0; i < se_count; ++i) {
3426
      // If both RPATH and RUNPATH refer to the same string literal it
3427
      // needs to be changed only once.
3428
0
      if (rp_count && rp[0].Position == se[i]->Position) {
3429
0
        continue;
3430
0
      }
3431
3432
      // Store information about the entry in the file.
3433
0
      rp[rp_count].Position = se[i]->Position;
3434
0
      rp[rp_count].Size = se[i]->Size;
3435
0
      rp[rp_count].Name = se_name[i];
3436
3437
      // Adjust the rpath.
3438
0
      cm::optional<std::string> outRPath;
3439
0
      if (!adjustCallback(outRPath, se[i]->Value, se_name[i], emsg)) {
3440
0
        return false;
3441
0
      }
3442
3443
0
      if (outRPath) {
3444
0
        if (!outRPath->empty()) {
3445
0
          remove_rpath = false;
3446
0
        }
3447
3448
        // Make sure there is enough room to store the new rpath and at
3449
        // least one null terminator.
3450
0
        if (rp[rp_count].Size < outRPath->length() + 1) {
3451
0
          if (emsg) {
3452
0
            *emsg = cmStrCat("The replacement path is too long for the ",
3453
0
                             se_name[i], " entry.");
3454
0
          }
3455
0
          return false;
3456
0
        }
3457
3458
        // This entry is ready for update.
3459
0
        rp[rp_count].Value = std::move(*outRPath);
3460
0
        ++rp_count;
3461
0
      } else {
3462
0
        remove_rpath = false;
3463
0
      }
3464
0
    }
3465
0
  }
3466
3467
  // If no runtime path needs to be changed, we are done.
3468
0
  if (rp_count == 0) {
3469
0
    return true;
3470
0
  }
3471
3472
  // If the resulting rpath is empty, just remove the entire entry instead.
3473
0
  if (remove_rpath) {
3474
0
    return cmSystemTools::RemoveRPath(file, emsg, changed);
3475
0
  }
3476
3477
0
  FileModeGuard file_mode_guard(file, emsg);
3478
0
  if (file_mode_guard.HasErrors()) {
3479
0
    return false;
3480
0
  }
3481
3482
0
  {
3483
    // Open the file for update.
3484
0
    cmsys::ofstream f(file.c_str(),
3485
0
                      std::ios::in | std::ios::out | std::ios::binary);
3486
0
    if (!f) {
3487
0
      if (emsg) {
3488
0
        *emsg = "Error opening file for update.";
3489
0
      }
3490
0
      return false;
3491
0
    }
3492
3493
    // Store the new RPATH and RUNPATH strings.
3494
0
    for (int i = 0; i < rp_count; ++i) {
3495
      // Seek to the RPATH position.
3496
0
      if (!f.seekp(rp[i].Position)) {
3497
0
        if (emsg) {
3498
0
          *emsg = cmStrCat("Error seeking to ", rp[i].Name, " position.");
3499
0
        }
3500
0
        return false;
3501
0
      }
3502
3503
      // Write the new rpath.  Follow it with enough null terminators to
3504
      // fill the string table entry.
3505
0
      f << rp[i].Value;
3506
0
      for (unsigned long j = rp[i].Value.length(); j < rp[i].Size; ++j) {
3507
0
        f << '\0';
3508
0
      }
3509
3510
      // Make sure it wrote correctly.
3511
0
      if (!f) {
3512
0
        if (emsg) {
3513
0
          *emsg = cmStrCat("Error writing the new ", rp[i].Name,
3514
0
                           " string to the file.");
3515
0
        }
3516
0
        return false;
3517
0
      }
3518
0
    }
3519
0
  }
3520
3521
0
  if (!file_mode_guard.Restore(emsg)) {
3522
0
    return false;
3523
0
  }
3524
3525
  // Everything was updated successfully.
3526
0
  if (changed) {
3527
0
    *changed = true;
3528
0
  }
3529
0
  return true;
3530
0
}
3531
3532
std::function<bool(std::string*, cmELF const&)> MakeEmptyCallback(
3533
  std::string const& newRPath)
3534
0
{
3535
0
  return [newRPath](std::string* emsg, cmELF const& elf) -> bool {
3536
0
    if (newRPath.empty()) {
3537
      // The new rpath is empty and there is no rpath anyway so it is
3538
      // okay.
3539
0
      return true;
3540
0
    }
3541
0
    if (emsg) {
3542
0
      *emsg =
3543
0
        cmStrCat("No valid ELF RPATH or RUNPATH entry exists in the file; ",
3544
0
                 elf.GetErrorMessage());
3545
0
    }
3546
0
    return false;
3547
0
  };
3548
0
}
3549
}
3550
3551
static cm::optional<bool> ChangeRPathELF(std::string const& file,
3552
                                         std::string const& oldRPath,
3553
                                         std::string const& newRPath,
3554
                                         bool removeEnvironmentRPath,
3555
                                         std::string* emsg, bool* changed)
3556
0
{
3557
0
  auto adjustCallback = [oldRPath, newRPath, removeEnvironmentRPath](
3558
0
                          cm::optional<std::string>& outRPath,
3559
0
                          std::string const& inRPath, char const* se_name,
3560
0
                          std::string* emsg2) -> bool {
3561
    // Make sure the current rpath contains the old rpath.
3562
0
    std::string::size_type pos = cmSystemToolsFindRPath(inRPath, oldRPath);
3563
0
    if (pos == std::string::npos) {
3564
      // If it contains the new rpath instead then it is okay.
3565
0
      if (cmSystemToolsFindRPath(inRPath, newRPath) != std::string::npos) {
3566
0
        return true;
3567
0
      }
3568
0
      if (emsg2) {
3569
0
        std::ostringstream e;
3570
        /* clang-format off */
3571
0
        e << "The current " << se_name << " is:\n"
3572
0
             "  " << inRPath << "\n"
3573
0
             "which does not contain:\n"
3574
0
             "  " << oldRPath << "\n"
3575
0
             "as was expected.";
3576
        /* clang-format on */
3577
0
        *emsg2 = e.str();
3578
0
      }
3579
0
      return false;
3580
0
    }
3581
3582
0
    std::string::size_type prefix_len = pos;
3583
3584
    // If oldRPath was at the end of the file's RPath, and newRPath is empty,
3585
    // we should remove the unnecessary ':' at the end.
3586
0
    if (newRPath.empty() && pos > 0 && inRPath[pos - 1] == ':' &&
3587
0
        pos + oldRPath.length() == inRPath.length()) {
3588
0
      prefix_len--;
3589
0
    }
3590
3591
    // Construct the new value which preserves the part of the path
3592
    // not being changed.
3593
0
    outRPath.emplace();
3594
0
    if (!removeEnvironmentRPath) {
3595
0
      *outRPath += inRPath.substr(0, prefix_len);
3596
0
    }
3597
0
    *outRPath += newRPath;
3598
0
    *outRPath += inRPath.substr(pos + oldRPath.length());
3599
3600
0
    return true;
3601
0
  };
3602
3603
0
  return AdjustRPathELF(file, MakeEmptyCallback(newRPath), adjustCallback,
3604
0
                        emsg, changed);
3605
0
}
3606
3607
static cm::optional<bool> SetRPathELF(std::string const& file,
3608
                                      std::string const& newRPath,
3609
                                      std::string* emsg, bool* changed)
3610
0
{
3611
0
  auto adjustCallback = [newRPath](cm::optional<std::string>& outRPath,
3612
0
                                   std::string const& inRPath,
3613
0
                                   char const* /*se_name*/, std::string*
3614
0
                                   /*emsg*/) -> bool {
3615
0
    if (inRPath != newRPath) {
3616
0
      outRPath = newRPath;
3617
0
    }
3618
0
    return true;
3619
0
  };
3620
3621
0
  return AdjustRPathELF(file, MakeEmptyCallback(newRPath), adjustCallback,
3622
0
                        emsg, changed);
3623
0
}
3624
static cm::optional<bool> ChangeRPathXCOFF(std::string const& file,
3625
                                           std::string const& oldRPath,
3626
                                           std::string const& newRPath,
3627
                                           bool removeEnvironmentRPath,
3628
                                           std::string* emsg, bool* changed)
3629
0
{
3630
0
  if (changed) {
3631
0
    *changed = false;
3632
0
  }
3633
0
#if !defined(CMake_USE_XCOFF_PARSER)
3634
0
  (void)file;
3635
0
  (void)oldRPath;
3636
0
  (void)newRPath;
3637
0
  (void)removeEnvironmentRPath;
3638
0
  (void)emsg;
3639
0
  return cm::nullopt;
3640
#else
3641
  bool chg = false;
3642
  cmXCOFF xcoff(file.c_str(), cmXCOFF::Mode::ReadWrite);
3643
  if (!xcoff) {
3644
    return cm::nullopt; // Not a valid XCOFF file
3645
  }
3646
  if (cm::optional<cm::string_view> maybeLibPath = xcoff.GetLibPath()) {
3647
    cm::string_view libPath = *maybeLibPath;
3648
    // Make sure the current rpath contains the old rpath.
3649
    std::string::size_type pos = cmSystemToolsFindRPath(libPath, oldRPath);
3650
    if (pos == std::string::npos) {
3651
      // If it contains the new rpath instead then it is okay.
3652
      if (cmSystemToolsFindRPath(libPath, newRPath) != std::string::npos) {
3653
        return true;
3654
      }
3655
      if (emsg) {
3656
        std::ostringstream e;
3657
        /* clang-format off */
3658
        e << "The current RPATH is:\n"
3659
             "  " << libPath << "\n"
3660
             "which does not contain:\n"
3661
             "  " << oldRPath << "\n"
3662
             "as was expected.";
3663
        /* clang-format on */
3664
        *emsg = e.str();
3665
      }
3666
      return false;
3667
    }
3668
3669
    // The prefix is either empty or ends in a ':'.
3670
    cm::string_view prefix = libPath.substr(0, pos);
3671
    if (newRPath.empty() && !prefix.empty()) {
3672
      prefix.remove_suffix(1);
3673
    }
3674
3675
    // The suffix is either empty or starts in a ':'.
3676
    cm::string_view suffix = libPath.substr(pos + oldRPath.length());
3677
3678
    // Construct the new value which preserves the part of the path
3679
    // not being changed.
3680
    std::string newLibPath;
3681
    if (!removeEnvironmentRPath) {
3682
      newLibPath = std::string(prefix);
3683
    }
3684
    newLibPath += newRPath;
3685
    newLibPath += suffix;
3686
3687
    chg = xcoff.SetLibPath(newLibPath);
3688
  }
3689
  if (!xcoff) {
3690
    if (emsg) {
3691
      *emsg = xcoff.GetErrorMessage();
3692
    }
3693
    return false;
3694
  }
3695
3696
  // Everything was updated successfully.
3697
  if (changed) {
3698
    *changed = chg;
3699
  }
3700
  return true;
3701
#endif
3702
0
}
3703
3704
static cm::optional<bool> SetRPathXCOFF(std::string const& /*file*/,
3705
                                        std::string const& /*newRPath*/,
3706
                                        std::string* /*emsg*/,
3707
                                        bool* /*changed*/)
3708
0
{
3709
0
  return cm::nullopt; // Not implemented.
3710
0
}
3711
3712
bool cmSystemTools::ChangeRPath(std::string const& file,
3713
                                std::string const& oldRPath,
3714
                                std::string const& newRPath,
3715
                                bool removeEnvironmentRPath, std::string* emsg,
3716
                                bool* changed)
3717
0
{
3718
0
  if (cm::optional<bool> result = ChangeRPathELF(
3719
0
        file, oldRPath, newRPath, removeEnvironmentRPath, emsg, changed)) {
3720
0
    return result.value();
3721
0
  }
3722
0
  if (cm::optional<bool> result = ChangeRPathXCOFF(
3723
0
        file, oldRPath, newRPath, removeEnvironmentRPath, emsg, changed)) {
3724
0
    return result.value();
3725
0
  }
3726
  // The file format is not recognized.  Assume it has no RPATH.
3727
0
  if (newRPath.empty()) {
3728
    // The caller wanted no RPATH anyway.
3729
0
    return true;
3730
0
  }
3731
0
  if (emsg) {
3732
0
    *emsg = "The file format is not recognized.";
3733
0
  }
3734
0
  return false;
3735
0
}
3736
3737
bool cmSystemTools::SetRPath(std::string const& file,
3738
                             std::string const& newRPath, std::string* emsg,
3739
                             bool* changed)
3740
0
{
3741
0
  if (cm::optional<bool> result = SetRPathELF(file, newRPath, emsg, changed)) {
3742
0
    return result.value();
3743
0
  }
3744
0
  if (cm::optional<bool> result =
3745
0
        SetRPathXCOFF(file, newRPath, emsg, changed)) {
3746
0
    return result.value();
3747
0
  }
3748
  // The file format is not recognized.  Assume it has no RPATH.
3749
0
  if (newRPath.empty()) {
3750
    // The caller wanted no RPATH anyway.
3751
0
    return true;
3752
0
  }
3753
0
  if (emsg) {
3754
0
    *emsg = "The file format is not recognized.";
3755
0
  }
3756
0
  return false;
3757
0
}
3758
3759
namespace {
3760
bool VersionCompare(cmSystemTools::CompareOp op, char const* lhss,
3761
                    char const* rhss)
3762
3.47k
{
3763
3.47k
  char const* endl = lhss;
3764
3.47k
  char const* endr = rhss;
3765
3766
23.2k
  while (((*endl >= '0') && (*endl <= '9')) ||
3767
22.4k
         ((*endr >= '0') && (*endr <= '9'))) {
3768
    // Do component-wise comparison, ignoring leading zeros
3769
    // (components are treated as integers, not as mantissas)
3770
50.0k
    while (*endl == '0') {
3771
27.5k
      endl++;
3772
27.5k
    }
3773
30.9k
    while (*endr == '0') {
3774
8.52k
      endr++;
3775
8.52k
    }
3776
3777
22.4k
    char const* beginl = endl;
3778
22.4k
    char const* beginr = endr;
3779
3780
    // count significant digits
3781
80.6k
    while ((*endl >= '0') && (*endl <= '9')) {
3782
58.2k
      endl++;
3783
58.2k
    }
3784
41.1k
    while ((*endr >= '0') && (*endr <= '9')) {
3785
18.6k
      endr++;
3786
18.6k
    }
3787
3788
    // compare number of digits first
3789
22.4k
    ptrdiff_t r = ((endl - beginl) - (endr - beginr));
3790
22.4k
    if (r == 0) {
3791
      // compare the digits if number of digits is equal
3792
20.4k
      r = strncmp(beginl, beginr, endl - beginl);
3793
20.4k
    }
3794
3795
22.4k
    if (r < 0) {
3796
      // lhs < rhs, so true if operation is LESS
3797
1.39k
      return (op & cmSystemTools::OP_LESS) != 0;
3798
1.39k
    }
3799
21.0k
    if (r > 0) {
3800
      // lhs > rhs, so true if operation is GREATER
3801
1.28k
      return (op & cmSystemTools::OP_GREATER) != 0;
3802
1.28k
    }
3803
3804
19.7k
    if (*endr == '.') {
3805
4.70k
      endr++;
3806
4.70k
    }
3807
3808
19.7k
    if (*endl == '.') {
3809
16.8k
      endl++;
3810
16.8k
    }
3811
19.7k
  }
3812
  // lhs == rhs, so true if operation is EQUAL
3813
792
  return (op & cmSystemTools::OP_EQUAL) != 0;
3814
3.47k
}
3815
}
3816
3817
bool cmSystemTools::VersionCompare(cmSystemTools::CompareOp op,
3818
                                   std::string const& lhs,
3819
                                   std::string const& rhs)
3820
1.80k
{
3821
1.80k
  return ::VersionCompare(op, lhs.c_str(), rhs.c_str());
3822
1.80k
}
3823
bool cmSystemTools::VersionCompare(cmSystemTools::CompareOp op,
3824
                                   std::string const& lhs, char const rhs[])
3825
1.67k
{
3826
1.67k
  return ::VersionCompare(op, lhs.c_str(), rhs);
3827
1.67k
}
3828
3829
bool cmSystemTools::VersionCompareEqual(std::string const& lhs,
3830
                                        std::string const& rhs)
3831
0
{
3832
0
  return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL, lhs, rhs);
3833
0
}
3834
3835
bool cmSystemTools::VersionCompareGreater(std::string const& lhs,
3836
                                          std::string const& rhs)
3837
334
{
3838
334
  return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER, lhs, rhs);
3839
334
}
3840
3841
bool cmSystemTools::VersionCompareGreaterEq(std::string const& lhs,
3842
                                            std::string const& rhs)
3843
334
{
3844
334
  return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL, lhs,
3845
334
                                       rhs);
3846
334
}
3847
3848
static size_t cm_strverscmp_find_first_difference_or_end(char const* lhs,
3849
                                                         char const* rhs)
3850
0
{
3851
0
  size_t i = 0;
3852
  /* Step forward until we find a difference or both strings end together.
3853
     The difference may lie on the null-terminator of one string.  */
3854
0
  while (lhs[i] == rhs[i] && lhs[i] != 0) {
3855
0
    ++i;
3856
0
  }
3857
0
  return i;
3858
0
}
3859
3860
static size_t cm_strverscmp_find_digits_begin(char const* s, size_t i)
3861
0
{
3862
  /* Step back until we are not preceded by a digit.  */
3863
0
  while (i > 0 && cmsysString_isdigit(s[i - 1])) {
3864
0
    --i;
3865
0
  }
3866
0
  return i;
3867
0
}
3868
3869
static size_t cm_strverscmp_find_digits_end(char const* s, size_t i)
3870
0
{
3871
  /* Step forward over digits.  */
3872
0
  while (cmsysString_isdigit(s[i])) {
3873
0
    ++i;
3874
0
  }
3875
0
  return i;
3876
0
}
3877
3878
static size_t cm_strverscmp_count_leading_zeros(char const* s, size_t b)
3879
0
{
3880
0
  size_t i = b;
3881
  /* Step forward over zeros that are followed by another digit.  */
3882
0
  while (s[i] == '0' && cmsysString_isdigit(s[i + 1])) {
3883
0
    ++i;
3884
0
  }
3885
0
  return i - b;
3886
0
}
3887
3888
static int cm_strverscmp(char const* lhs, char const* rhs)
3889
0
{
3890
0
  size_t const i = cm_strverscmp_find_first_difference_or_end(lhs, rhs);
3891
0
  if (lhs[i] != rhs[i]) {
3892
    /* The strings differ starting at 'i'.  Check for a digit sequence.  */
3893
0
    size_t const b = cm_strverscmp_find_digits_begin(lhs, i);
3894
0
    if (b != i ||
3895
0
        (cmsysString_isdigit(lhs[i]) && cmsysString_isdigit(rhs[i]))) {
3896
      /* A digit sequence starts at 'b', preceding or at 'i'.  */
3897
3898
      /* Look for leading zeros, implying a leading decimal point.  */
3899
0
      size_t const lhs_zeros = cm_strverscmp_count_leading_zeros(lhs, b);
3900
0
      size_t const rhs_zeros = cm_strverscmp_count_leading_zeros(rhs, b);
3901
0
      if (lhs_zeros != rhs_zeros) {
3902
        /* The side with more leading zeros orders first.  */
3903
0
        return rhs_zeros > lhs_zeros ? 1 : -1;
3904
0
      }
3905
0
      if (lhs_zeros == 0) {
3906
        /* No leading zeros; compare digit sequence lengths.  */
3907
0
        size_t const lhs_end = cm_strverscmp_find_digits_end(lhs, i);
3908
0
        size_t const rhs_end = cm_strverscmp_find_digits_end(rhs, i);
3909
0
        if (lhs_end != rhs_end) {
3910
          /* The side with fewer digits orders first.  */
3911
0
          return lhs_end > rhs_end ? 1 : -1;
3912
0
        }
3913
0
      }
3914
0
    }
3915
0
  }
3916
3917
  /* Ordering was not decided by digit sequence lengths; compare bytes.  */
3918
0
  return lhs[i] - rhs[i];
3919
0
}
3920
3921
int cmSystemTools::strverscmp(std::string const& lhs, std::string const& rhs)
3922
0
{
3923
0
  return cm_strverscmp(lhs.c_str(), rhs.c_str());
3924
0
}
3925
3926
static cm::optional<bool> RemoveRPathELF(std::string const& file,
3927
                                         std::string* emsg, bool* removed)
3928
0
{
3929
0
  if (removed) {
3930
0
    *removed = false;
3931
0
  }
3932
0
  int zeroCount = 0;
3933
0
  unsigned long zeroPosition[2] = { 0, 0 };
3934
0
  unsigned long zeroSize[2] = { 0, 0 };
3935
0
  unsigned long bytesBegin = 0;
3936
0
  std::vector<char> bytes;
3937
0
  {
3938
    // Parse the ELF binary.
3939
0
    cmELF elf(file.c_str());
3940
0
    if (!elf) {
3941
0
      return cm::nullopt; // Not a valid ELF file.
3942
0
    }
3943
3944
    // Get the RPATH and RUNPATH entries from it and sort them by index
3945
    // in the dynamic section header.
3946
0
    int se_count = 0;
3947
0
    cmELF::StringEntry const* se[2] = { nullptr, nullptr };
3948
0
    if (cmELF::StringEntry const* se_rpath = elf.GetRPath()) {
3949
0
      se[se_count++] = se_rpath;
3950
0
    }
3951
0
    if (cmELF::StringEntry const* se_runpath = elf.GetRunPath()) {
3952
0
      se[se_count++] = se_runpath;
3953
0
    }
3954
0
    if (se_count == 0) {
3955
      // There is no RPATH or RUNPATH anyway.
3956
0
      return true;
3957
0
    }
3958
0
    if (se_count == 2 && se[1]->IndexInSection < se[0]->IndexInSection) {
3959
0
      std::swap(se[0], se[1]);
3960
0
    }
3961
3962
    // Obtain a copy of the dynamic entries
3963
0
    cmELF::DynamicEntryList dentries = elf.GetDynamicEntries();
3964
0
    if (dentries.empty()) {
3965
      // This should happen only for invalid ELF files where a DT_NULL
3966
      // appears before the end of the table.
3967
0
      if (emsg) {
3968
0
        *emsg = "DYNAMIC section contains a DT_NULL before the end.";
3969
0
      }
3970
0
      return false;
3971
0
    }
3972
3973
    // Save information about the string entries to be zeroed.
3974
0
    zeroCount = se_count;
3975
0
    for (int i = 0; i < se_count; ++i) {
3976
0
      zeroPosition[i] = se[i]->Position;
3977
0
      zeroSize[i] = se[i]->Size;
3978
0
    }
3979
3980
    // Get size of one DYNAMIC entry
3981
0
    unsigned long const sizeof_dentry =
3982
0
      elf.GetDynamicEntryPosition(1) - elf.GetDynamicEntryPosition(0);
3983
3984
    // Adjust the entry list as necessary to remove the run path
3985
0
    unsigned long entriesErased = 0;
3986
0
    for (auto it = dentries.begin(); it != dentries.end();) {
3987
0
      if (it->first == cmELF::TagRPath || it->first == cmELF::TagRunPath) {
3988
0
        it = dentries.erase(it);
3989
0
        entriesErased++;
3990
0
        continue;
3991
0
      }
3992
0
      if (it->first == cmELF::TagMipsRldMapRel && elf.IsMIPS()) {
3993
        // Background: debuggers need to know the "linker map" which contains
3994
        // the addresses each dynamic object is loaded at. Most arches use
3995
        // the DT_DEBUG tag which the dynamic linker writes to (directly) and
3996
        // contain the location of the linker map, however on MIPS the
3997
        // .dynamic section is always read-only so this is not possible. MIPS
3998
        // objects instead contain a DT_MIPS_RLD_MAP tag which contains the
3999
        // address where the dynamic linker will write to (an indirect
4000
        // version of DT_DEBUG). Since this doesn't work when using PIE, a
4001
        // relative equivalent was created - DT_MIPS_RLD_MAP_REL. Since this
4002
        // version contains a relative offset, moving it changes the
4003
        // calculated address. This may cause the dynamic linker to write
4004
        // into memory it should not be changing.
4005
        //
4006
        // To fix this, we adjust the value of DT_MIPS_RLD_MAP_REL here. If
4007
        // we move it up by n bytes, we add n bytes to the value of this tag.
4008
0
        it->second += entriesErased * sizeof_dentry;
4009
0
      }
4010
4011
0
      it++;
4012
0
    }
4013
4014
    // Encode new entries list
4015
0
    bytes = elf.EncodeDynamicEntries(dentries);
4016
0
    bytesBegin = elf.GetDynamicEntryPosition(0);
4017
0
  }
4018
4019
0
  FileModeGuard file_mode_guard(file, emsg);
4020
0
  if (file_mode_guard.HasErrors()) {
4021
0
    return false;
4022
0
  }
4023
4024
  // Open the file for update.
4025
0
  cmsys::ofstream f(file.c_str(),
4026
0
                    std::ios::in | std::ios::out | std::ios::binary);
4027
0
  if (!f) {
4028
0
    if (emsg) {
4029
0
      *emsg = "Error opening file for update.";
4030
0
    }
4031
0
    return false;
4032
0
  }
4033
4034
  // Write the new DYNAMIC table header.
4035
0
  if (!f.seekp(bytesBegin)) {
4036
0
    if (emsg) {
4037
0
      *emsg = "Error seeking to DYNAMIC table header for RPATH.";
4038
0
    }
4039
0
    return false;
4040
0
  }
4041
0
  if (!f.write(bytes.data(), bytes.size())) {
4042
0
    if (emsg) {
4043
0
      *emsg = "Error replacing DYNAMIC table header.";
4044
0
    }
4045
0
    return false;
4046
0
  }
4047
4048
  // Fill the RPATH and RUNPATH strings with zero bytes.
4049
0
  for (int i = 0; i < zeroCount; ++i) {
4050
0
    if (!f.seekp(zeroPosition[i])) {
4051
0
      if (emsg) {
4052
0
        *emsg = "Error seeking to RPATH position.";
4053
0
      }
4054
0
      return false;
4055
0
    }
4056
0
    for (unsigned long j = 0; j < zeroSize[i]; ++j) {
4057
0
      f << '\0';
4058
0
    }
4059
0
    if (!f) {
4060
0
      if (emsg) {
4061
0
        *emsg = "Error writing the empty rpath string to the file.";
4062
0
      }
4063
0
      return false;
4064
0
    }
4065
0
  }
4066
4067
  // Close the handle to allow further operations on the file
4068
0
  f.close();
4069
4070
0
  if (!file_mode_guard.Restore(emsg)) {
4071
0
    return false;
4072
0
  }
4073
4074
  // Everything was updated successfully.
4075
0
  if (removed) {
4076
0
    *removed = true;
4077
0
  }
4078
0
  return true;
4079
0
}
4080
4081
static cm::optional<bool> RemoveRPathXCOFF(std::string const& file,
4082
                                           std::string* emsg, bool* removed)
4083
0
{
4084
0
  if (removed) {
4085
0
    *removed = false;
4086
0
  }
4087
0
#if !defined(CMake_USE_XCOFF_PARSER)
4088
0
  (void)file;
4089
0
  (void)emsg;
4090
0
  return cm::nullopt; // Cannot handle XCOFF files.
4091
#else
4092
  bool rm = false;
4093
4094
  FileModeGuard file_mode_guard(file, emsg);
4095
  if (file_mode_guard.HasErrors()) {
4096
    return false;
4097
  }
4098
4099
  {
4100
    cmXCOFF xcoff(file.c_str(), cmXCOFF::Mode::ReadWrite);
4101
    if (!xcoff) {
4102
      return cm::nullopt; // Not a valid XCOFF file.
4103
    }
4104
    rm = xcoff.RemoveLibPath();
4105
    if (!xcoff) {
4106
      if (emsg) {
4107
        *emsg = xcoff.GetErrorMessage();
4108
      }
4109
      return false;
4110
    }
4111
  }
4112
4113
  if (!file_mode_guard.Restore(emsg)) {
4114
    return false;
4115
  }
4116
4117
  if (removed) {
4118
    *removed = rm;
4119
  }
4120
  return true;
4121
#endif
4122
0
}
4123
bool cmSystemTools::RemoveRPath(std::string const& file, std::string* emsg,
4124
                                bool* removed)
4125
0
{
4126
0
  if (cm::optional<bool> result = RemoveRPathELF(file, emsg, removed)) {
4127
0
    return result.value();
4128
0
  }
4129
0
  if (cm::optional<bool> result = RemoveRPathXCOFF(file, emsg, removed)) {
4130
0
    return result.value();
4131
0
  }
4132
  // The file format is not recognized.  Assume it has no RPATH.
4133
0
  return true;
4134
0
}
4135
4136
bool cmSystemTools::CheckRPath(std::string const& file,
4137
                               std::string const& newRPath)
4138
0
{
4139
  // Parse the ELF binary.
4140
0
  cmELF elf(file.c_str());
4141
0
  if (elf) {
4142
    // Get the RPATH or RUNPATH entry from it.
4143
0
    cmELF::StringEntry const* se = elf.GetRPath();
4144
0
    if (!se) {
4145
0
      se = elf.GetRunPath();
4146
0
    }
4147
4148
    // Make sure the current rpath contains the new rpath.
4149
0
    if (newRPath.empty()) {
4150
0
      if (!se) {
4151
0
        return true;
4152
0
      }
4153
0
    } else {
4154
0
      if (se &&
4155
0
          cmSystemToolsFindRPath(se->Value, newRPath) != std::string::npos) {
4156
0
        return true;
4157
0
      }
4158
0
    }
4159
0
    return false;
4160
0
  }
4161
#if defined(CMake_USE_XCOFF_PARSER)
4162
  // Parse the XCOFF binary.
4163
  cmXCOFF xcoff(file.c_str());
4164
  if (xcoff) {
4165
    if (cm::optional<cm::string_view> libPath = xcoff.GetLibPath()) {
4166
      if (cmSystemToolsFindRPath(*libPath, newRPath) != std::string::npos) {
4167
        return true;
4168
      }
4169
    }
4170
    return false;
4171
  }
4172
#endif
4173
  // The file format is not recognized.  Assume it has no RPATH.
4174
  // Therefore we succeed if the new rpath is empty anyway.
4175
0
  return newRPath.empty();
4176
0
}
4177
4178
cmsys::Status cmSystemTools::RepeatedRemoveDirectory(std::string const& dir)
4179
0
{
4180
#ifdef _WIN32
4181
  // Windows sometimes locks files temporarily so try a few times.
4182
  WindowsFileRetry retry = cmSystemTools::GetWindowsFileRetry();
4183
4184
  cmsys::Status status;
4185
  unsigned int tries = 0;
4186
  while (!(status = cmSystemTools::RemoveADirectory(dir)) &&
4187
         ++tries < retry.Count) {
4188
    cmSystemTools::Delay(retry.Delay);
4189
  }
4190
  return status;
4191
#else
4192
0
  return cmSystemTools::RemoveADirectory(dir);
4193
0
#endif
4194
0
}
4195
4196
std::string cmSystemTools::EncodeURL(std::string const& in, bool escapeSlashes)
4197
0
{
4198
0
  std::string out;
4199
0
  for (char c : in) {
4200
0
    char hexCh[4] = { 0, 0, 0, 0 };
4201
0
    hexCh[0] = c;
4202
0
    switch (c) {
4203
0
      case '+':
4204
0
      case '?':
4205
0
      case '\\':
4206
0
      case '&':
4207
0
      case ' ':
4208
0
      case '=':
4209
0
      case '%':
4210
0
        snprintf(hexCh, sizeof(hexCh), "%%%02X", static_cast<unsigned int>(c));
4211
0
        break;
4212
0
      case '/':
4213
0
        if (escapeSlashes) {
4214
0
          strcpy(hexCh, "%2F");
4215
0
        }
4216
0
        break;
4217
0
      default:
4218
0
        break;
4219
0
    }
4220
0
    out.append(hexCh);
4221
0
  }
4222
0
  return out;
4223
0
}
4224
4225
cm::optional<cmSystemTools::DirCase> cmSystemTools::GetDirCase(
4226
  std::string const& dir)
4227
0
{
4228
0
  if (!cmSystemTools::FileIsDirectory(dir)) {
4229
0
    return cm::nullopt;
4230
0
  }
4231
#if defined(_WIN32) || defined(__APPLE__)
4232
  return DirCase::Insensitive;
4233
#elif defined(__linux__)
4234
0
  int fd = open(dir.c_str(), O_RDONLY);
4235
0
  if (fd == -1) {
4236
    // cannot open dir but it exists, assume dir is case sensitive.
4237
0
    return DirCase::Sensitive;
4238
0
  }
4239
0
  int attr = 0;
4240
0
  int ioctl_res = ioctl(fd, FS_IOC_GETFLAGS, &attr);
4241
0
  close(fd);
4242
4243
0
  if (ioctl_res == -1) {
4244
0
    return DirCase::Sensitive;
4245
0
  }
4246
4247
  // FS_CASEFOLD_FD from linux/fs.h, in Linux libc-dev 5.4+
4248
  // For compat with old libc-dev, define it here.
4249
0
  int const CMAKE_FS_CASEFOLD_FL = 0x40000000;
4250
0
  return (attr & CMAKE_FS_CASEFOLD_FL) != 0 ? DirCase::Insensitive
4251
0
                                            : DirCase::Sensitive;
4252
#else
4253
  return DirCase::Sensitive;
4254
#endif
4255
0
}
4256
4257
cmsys::Status cmSystemTools::CreateSymlink(std::string const& origName,
4258
                                           std::string const& newName)
4259
0
{
4260
0
  cmsys::Status status =
4261
0
    cmSystemTools::CreateSymlinkQuietly(origName, newName);
4262
0
  if (!status) {
4263
0
    cmSystemTools::Error(cmStrCat("failed to create symbolic link '", newName,
4264
0
                                  "': ", status.GetString()));
4265
0
  }
4266
0
  return status;
4267
0
}
4268
4269
cmsys::Status cmSystemTools::CreateSymlinkQuietly(std::string const& origName,
4270
                                                  std::string const& newName)
4271
0
{
4272
0
  uv_fs_t req;
4273
0
  int flags = 0;
4274
#if defined(_WIN32)
4275
  if (cmsys::SystemTools::FileIsDirectory(origName)) {
4276
    flags |= UV_FS_SYMLINK_DIR;
4277
  }
4278
#endif
4279
0
  int err = uv_fs_symlink(nullptr, &req, origName.c_str(), newName.c_str(),
4280
0
                          flags, nullptr);
4281
0
  cmsys::Status status;
4282
0
  if (err) {
4283
#if defined(_WIN32)
4284
    status = cmsys::Status::Windows(uv_fs_get_system_error(&req));
4285
#elif UV_VERSION_MAJOR > 1 || (UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR >= 38)
4286
    status = cmsys::Status::POSIX(uv_fs_get_system_error(&req));
4287
#else
4288
    status = cmsys::Status::POSIX(-err);
4289
#endif
4290
0
  }
4291
0
  return status;
4292
0
}
4293
4294
cmsys::Status cmSystemTools::CreateLink(std::string const& origName,
4295
                                        std::string const& newName)
4296
0
{
4297
0
  cmsys::Status status = cmSystemTools::CreateLinkQuietly(origName, newName);
4298
0
  if (!status) {
4299
0
    cmSystemTools::Error(
4300
0
      cmStrCat("failed to create link '", newName, "': ", status.GetString()));
4301
0
  }
4302
0
  return status;
4303
0
}
4304
4305
cmsys::Status cmSystemTools::CreateLinkQuietly(std::string const& origName,
4306
                                               std::string const& newName)
4307
0
{
4308
0
  uv_fs_t req;
4309
0
  int err =
4310
0
    uv_fs_link(nullptr, &req, origName.c_str(), newName.c_str(), nullptr);
4311
0
  cmsys::Status status;
4312
0
  if (err) {
4313
#if defined(_WIN32)
4314
    status = cmsys::Status::Windows(uv_fs_get_system_error(&req));
4315
#elif UV_VERSION_MAJOR > 1 || (UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR >= 38)
4316
    status = cmsys::Status::POSIX(uv_fs_get_system_error(&req));
4317
#else
4318
    status = cmsys::Status::POSIX(-err);
4319
#endif
4320
0
  }
4321
0
  return status;
4322
0
}
4323
4324
cm::string_view cmSystemTools::GetSystemName()
4325
1
{
4326
#if defined(_WIN32)
4327
  return "Windows";
4328
#elif defined(__MSYS__)
4329
  return "MSYS";
4330
#elif defined(__CYGWIN__)
4331
  return "CYGWIN";
4332
#elif defined(__ANDROID__)
4333
  return "Android";
4334
#else
4335
1
  static struct utsname uts_name;
4336
1
  static bool initialized = false;
4337
1
  static cm::string_view systemName;
4338
1
  if (initialized) {
4339
0
    return systemName;
4340
0
  }
4341
1
  if (uname(&uts_name) >= 0) {
4342
1
    initialized = true;
4343
1
    systemName = uts_name.sysname;
4344
4345
1
    if (cmIsOff(systemName)) {
4346
0
      systemName = "UnknownOS";
4347
0
    }
4348
4349
    // fix for BSD/OS, remove the /
4350
1
    static cmsys::RegularExpression const bsdOsRegex("BSD.OS");
4351
1
    cmsys::RegularExpressionMatch match;
4352
1
    if (bsdOsRegex.find(uts_name.sysname, match)) {
4353
0
      systemName = "BSDOS";
4354
0
    }
4355
4356
1
    return systemName;
4357
1
  }
4358
0
  return "";
4359
1
#endif
4360
1
}
4361
4362
char cmSystemTools::GetSystemPathlistSeparator()
4363
0
{
4364
#if defined(_WIN32)
4365
  return ';';
4366
#else
4367
0
  return ':';
4368
0
#endif
4369
0
}
4370
4371
cm::string_view cmSystemTools::GetFilenameNameView(cm::string_view filename)
4372
0
{
4373
// implementation mostly taken from cmsys::SystemTools
4374
#if defined(_WIN32) || defined(KWSYS_SYSTEMTOOLS_SUPPORT_WINDOWS_SLASHES)
4375
  cm::static_string_view separators = "/\\"_s;
4376
#else
4377
0
  char separators = '/';
4378
0
#endif
4379
0
  std::string::size_type slash_pos = filename.find_last_of(separators);
4380
0
  if (slash_pos == std::string::npos) {
4381
0
    return filename;
4382
0
  }
4383
0
  return filename.substr(slash_pos + 1);
4384
0
}
4385
4386
cm::string_view cmSystemTools::GetFilenameLastExtensionView(
4387
  cm::string_view filename)
4388
0
{
4389
0
  cm::string_view name = cmSystemTools::GetFilenameNameView(filename);
4390
0
  cm::string_view::size_type dot_pos = name.rfind('.');
4391
0
  if (dot_pos == std::string::npos) {
4392
0
    return cm::string_view();
4393
0
  }
4394
0
  return name.substr(dot_pos);
4395
0
}