Coverage Report

Created: 2026-07-14 07:09

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