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

5
#include "cm_kwiml.h"
6
#include "cm_static_string_view.hxx"
7 8 9
#include "cmsys/FStream.hxx"
#include "cmsys/Glob.hxx"
#include "cmsys/RegularExpression.hxx"
10

Daniel Pfeifer's avatar
Daniel Pfeifer committed
11 12
#include <algorithm>
#include <assert.h>
13
#include <cmath>
14
#include <ctype.h>
15 16
#include <map>
#include <set>
Daniel Pfeifer's avatar
Daniel Pfeifer committed
17 18 19
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
20
#include <utility>
21
#include <vector>
Daniel Pfeifer's avatar
Daniel Pfeifer committed
22

23 24
#include "cm_memory.hxx"

25
#include "cmAlgorithms.h"
26
#include "cmArgumentParser.h"
27
#include "cmCryptoHash.h"
28
#include "cmExecutionStatus.h"
29 30
#include "cmFileCopier.h"
#include "cmFileInstaller.h"
Daniel Pfeifer's avatar
Daniel Pfeifer committed
31
#include "cmFileLockPool.h"
32
#include "cmFileTimes.h"
Daniel Pfeifer's avatar
Daniel Pfeifer committed
33
#include "cmGeneratorExpression.h"
34
#include "cmGlobalGenerator.h"
35
#include "cmHexFileConverter.h"
Daniel Pfeifer's avatar
Daniel Pfeifer committed
36
#include "cmListFileCache.h"
37
#include "cmMakefile.h"
38
#include "cmMessageType.h"
Daniel Pfeifer's avatar
Daniel Pfeifer committed
39
#include "cmPolicies.h"
40
#include "cmRange.h"
41 42
#include "cmRuntimeDependencyArchive.h"
#include "cmState.h"
43
#include "cmStringAlgorithms.h"
44
#include "cmSubcommandTable.h"
Daniel Pfeifer's avatar
Daniel Pfeifer committed
45
#include "cmSystemTools.h"
46
#include "cmTimestamp.h"
47
#include "cm_sys_stat.h"
48
#include "cmake.h"
49

50
#if !defined(CMAKE_BOOTSTRAP)
51 52 53
#  include "cmCurl.h"
#  include "cmFileLockResult.h"
#  include "cm_curl.h"
54 55
#endif

56
#if defined(CMAKE_USE_ELF_PARSER)
57
#  include "cmELF.h"
58 59
#endif

60
#if defined(_WIN32)
61
#  include <windows.h>
62 63
#endif

64 65
namespace {

66
#if defined(_WIN32)
67 68
// libcurl doesn't support file:// urls for unicode filenames on Windows.
// Convert string from UTF-8 to ACP if this is a file:// URL.
69
std::string fix_file_url_windows(const std::string& url)
70 71
{
  std::string ret = url;
72
  if (strncmp(url.c_str(), "file://", 7) == 0) {
Stephen Kelly's avatar
Stephen Kelly committed
73
    std::wstring wurl = cmsys::Encoding::ToWide(url);
74 75 76 77
    if (!wurl.empty()) {
      int mblen =
        WideCharToMultiByte(CP_ACP, 0, wurl.c_str(), -1, NULL, 0, NULL, NULL);
      if (mblen > 0) {
78
        std::vector<char> chars(mblen);
79 80 81
        mblen = WideCharToMultiByte(CP_ACP, 0, wurl.c_str(), -1, &chars[0],
                                    mblen, NULL, NULL);
        if (mblen > 0) {
82 83 84 85
          ret = &chars[0];
        }
      }
    }
86
  }
87 88 89 90
  return ret;
}
#endif

91 92
bool HandleWriteImpl(std::vector<std::string> const& args, bool append,
                     cmExecutionStatus& status)
93 94 95 96 97 98
{
  std::vector<std::string>::const_iterator i = args.begin();

  i++; // Get rid of subcommand

  std::string fileName = *i;
99
  if (!cmsys::SystemTools::FileIsFullPath(*i)) {
100
    fileName = status.GetMakefile().GetCurrentSourceDirectory();
101
    fileName += "/" + *i;
102
  }
103

104 105
  i++;

106
  if (!status.GetMakefile().CanIWriteThisFile(fileName)) {
107 108
    std::string e =
      "attempted to write a file: " + fileName + " into a source directory.";
109
    status.SetError(e);
110 111
    cmSystemTools::SetFatalErrorOccured();
    return false;
112
  }
113
  std::string dir = cmSystemTools::GetFilenamePath(fileName);
114
  cmSystemTools::MakeDirectory(dir);
115

116
  mode_t mode = 0;
117
  bool writable = false;
118 119

  // Set permissions to writable
120
  if (cmSystemTools::GetPermissions(fileName, mode)) {
121
#if defined(_MSC_VER) || defined(__MINGW32__)
122
    writable = (mode & S_IWRITE) != 0;
123
    mode_t newMode = mode | S_IWRITE;
124
#else
125 126
    writable = mode & S_IWUSR;
    mode_t newMode = mode | S_IWUSR | S_IWGRP;
127
#endif
128
    if (!writable) {
129
      cmSystemTools::SetPermissions(fileName, newMode);
130
    }
131
  }
132 133
  // If GetPermissions fails, pretend like it is ok. File open will fail if
  // the file is not writable
134 135 136
  cmsys::ofstream file(fileName.c_str(),
                       append ? std::ios::app : std::ios::out);
  if (!file) {
137 138 139 140
    std::string error = "failed to open for writing (";
    error += cmSystemTools::GetLastSystemError();
    error += "):\n  ";
    error += fileName;
141
    status.SetError(error);
142
    return false;
143
  }
144
  std::string message = cmJoin(cmMakeRange(i, args.end()), std::string());
145
  file << message;
146 147 148 149 150
  if (!file) {
    std::string error = "write failed (";
    error += cmSystemTools::GetLastSystemError();
    error += "):\n  ";
    error += fileName;
151
    status.SetError(error);
152 153
    return false;
  }
154
  file.close();
155
  if (mode && !writable) {
156
    cmSystemTools::SetPermissions(fileName, mode);
157
  }
158 159 160
  return true;
}

161 162 163 164 165 166 167 168 169 170 171 172
bool HandleWriteCommand(std::vector<std::string> const& args,
                        cmExecutionStatus& status)
{
  return HandleWriteImpl(args, false, status);
}

bool HandleAppendCommand(std::vector<std::string> const& args,
                         cmExecutionStatus& status)
{
  return HandleWriteImpl(args, true, status);
}

173 174
bool HandleReadCommand(std::vector<std::string> const& args,
                       cmExecutionStatus& status)
175
{
176
  if (args.size() < 3) {
177 178
    status.SetError("READ must be called with at least two additional "
                    "arguments");
179
    return false;
180
  }
181

182 183
  std::string const& fileNameArg = args[1];
  std::string const& variable = args[2];
184

185 186 187 188 189 190 191 192 193 194 195
  struct Arguments
  {
    std::string Offset;
    std::string Limit;
    bool Hex = false;
  };

  static auto const parser = cmArgumentParser<Arguments>{}
                               .Bind("OFFSET"_s, &Arguments::Offset)
                               .Bind("LIMIT"_s, &Arguments::Limit)
                               .Bind("HEX"_s, &Arguments::Hex);
196

197
  Arguments const arguments = parser.Parse(cmMakeRange(args).advance(3));
198

199
  std::string fileName = fileNameArg;
200
  if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
201
    fileName = status.GetMakefile().GetCurrentSourceDirectory();
202
    fileName += "/" + fileNameArg;
203
  }
204

205
// Open the specified file.
206
#if defined(_WIN32) || defined(__CYGWIN__)
207 208 209
  cmsys::ifstream file(fileName.c_str(),
                       arguments.Hex ? (std::ios::binary | std::ios::in)
                                     : std::ios::in);
210
#else
211
  cmsys::ifstream file(fileName.c_str());
212 213
#endif

214
  if (!file) {
215 216 217 218
    std::string error = "failed to open for reading (";
    error += cmSystemTools::GetLastSystemError();
    error += "):\n  ";
    error += fileName;
219
    status.SetError(error);
220
    return false;
221
  }
222

223
  // is there a limit?
224
  long sizeLimit = -1;
225 226
  if (!arguments.Limit.empty()) {
    sizeLimit = atoi(arguments.Limit.c_str());
227
  }
228 229 230

  // is there an offset?
  long offset = 0;
231 232
  if (!arguments.Offset.empty()) {
    offset = atoi(arguments.Offset.c_str());
233
  }
234

235
  file.seekg(offset, std::ios::beg); // explicit ios::beg for IBM VisualAge 6
236

237
  std::string output;
238

239
  if (arguments.Hex) {
240
    // Convert part of the file into hex code
241
    char c;
242
    while ((sizeLimit != 0) && (file.get(c))) {
243
      char hex[4];
244
      sprintf(hex, "%.2x", c & 0xff);
245
      output += hex;
246
      if (sizeLimit > 0) {
247 248
        sizeLimit--;
      }
249
    }
250
  } else {
251 252
    std::string line;
    bool has_newline = false;
253 254 255
    while (
      sizeLimit != 0 &&
      cmSystemTools::GetLineFromStream(file, line, &has_newline, sizeLimit)) {
256
      if (sizeLimit > 0) {
257
        sizeLimit = sizeLimit - static_cast<long>(line.size());
258
        if (has_newline) {
259
          sizeLimit--;
260 261
        }
        if (sizeLimit < 0) {
262 263
          sizeLimit = 0;
        }
264
      }
265
      output += line;
266
      if (has_newline) {
267
        output += "\n";
268 269
      }
    }
270
  }
271
  status.GetMakefile().AddDefinition(variable, output);
272 273 274
  return true;
}

275 276
bool HandleHashCommand(std::vector<std::string> const& args,
                       cmExecutionStatus& status)
277
{
278
#if !defined(CMAKE_BOOTSTRAP)
279
  if (args.size() != 3) {
280
    std::ostringstream e;
281
    e << args[0] << " requires a file name and output variable";
282
    status.SetError(e.str());
283
    return false;
284
  }
285

286
  std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0]));
287
  if (hash) {
288
    std::string out = hash->HashFile(args[1]);
289
    if (!out.empty()) {
290
      status.GetMakefile().AddDefinition(args[2], out);
291
      return true;
292
    }
293
    std::ostringstream e;
294 295
    e << args[0] << " failed to read file \"" << args[1]
      << "\": " << cmSystemTools::GetLastSystemError();
296
    status.SetError(e.str());
297
  }
298
  return false;
299
#else
300
  std::ostringstream e;
301
  e << args[0] << " not available during bootstrap";
302
  status.SetError(e.str());
303 304
  return false;
#endif
305 306
}

307 308
bool HandleStringsCommand(std::vector<std::string> const& args,
                          cmExecutionStatus& status)
309
{
310
  if (args.size() < 3) {
311
    status.SetError("STRINGS requires a file name and output variable");
312
    return false;
313
  }
314 315 316

  // Get the file to read.
  std::string fileName = args[1];
317
  if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
318
    fileName = status.GetMakefile().GetCurrentSourceDirectory();
319
    fileName += "/" + args[1];
320
  }
321 322 323 324 325

  // Get the variable in which to store the results.
  std::string outVar = args[2];

  // Parse the options.
326 327 328 329 330 331 332 333 334 335 336 337
  enum
  {
    arg_none,
    arg_limit_input,
    arg_limit_output,
    arg_limit_count,
    arg_length_minimum,
    arg_length_maximum,
    arg__maximum,
    arg_regex,
    arg_encoding
  };
338 339 340 341 342 343 344 345
  unsigned int minlen = 0;
  unsigned int maxlen = 0;
  int limit_input = -1;
  int limit_output = -1;
  unsigned int limit_count = 0;
  cmsys::RegularExpression regex;
  bool have_regex = false;
  bool newline_consume = false;
346
  bool hex_conversion_enabled = true;
347 348 349 350 351 352 353 354 355
  enum
  {
    encoding_none = cmsys::FStream::BOM_None,
    encoding_utf8 = cmsys::FStream::BOM_UTF8,
    encoding_utf16le = cmsys::FStream::BOM_UTF16LE,
    encoding_utf16be = cmsys::FStream::BOM_UTF16BE,
    encoding_utf32le = cmsys::FStream::BOM_UTF32LE,
    encoding_utf32be = cmsys::FStream::BOM_UTF32BE
  };
356
  int encoding = encoding_none;
357
  int arg_mode = arg_none;
358 359
  for (unsigned int i = 3; i < args.size(); ++i) {
    if (args[i] == "LIMIT_INPUT") {
360
      arg_mode = arg_limit_input;
361
    } else if (args[i] == "LIMIT_OUTPUT") {
362
      arg_mode = arg_limit_output;
363
    } else if (args[i] == "LIMIT_COUNT") {
364
      arg_mode = arg_limit_count;
365
    } else if (args[i] == "LENGTH_MINIMUM") {
366
      arg_mode = arg_length_minimum;
367
    } else if (args[i] == "LENGTH_MAXIMUM") {
368
      arg_mode = arg_length_maximum;
369
    } else if (args[i] == "REGEX") {
370
      arg_mode = arg_regex;
371
    } else if (args[i] == "NEWLINE_CONSUME") {
372 373
      newline_consume = true;
      arg_mode = arg_none;
374
    } else if (args[i] == "NO_HEX_CONVERSION") {
375 376
      hex_conversion_enabled = false;
      arg_mode = arg_none;
377
    } else if (args[i] == "ENCODING") {
378
      arg_mode = arg_encoding;
379 380 381
    } else if (arg_mode == arg_limit_input) {
      if (sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
          limit_input < 0) {
382
        std::ostringstream e;
383 384
        e << "STRINGS option LIMIT_INPUT value \"" << args[i]
          << "\" is not an unsigned integer.";
385
        status.SetError(e.str());
386 387
        return false;
      }
388 389 390 391
      arg_mode = arg_none;
    } else if (arg_mode == arg_limit_output) {
      if (sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
          limit_output < 0) {
392
        std::ostringstream e;
393 394
        e << "STRINGS option LIMIT_OUTPUT value \"" << args[i]
          << "\" is not an unsigned integer.";
395
        status.SetError(e.str());
396 397
        return false;
      }
398 399
      arg_mode = arg_none;
    } else if (arg_mode == arg_limit_count) {
400
      int count;
401
      if (sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0) {
402
        std::ostringstream e;
403 404
        e << "STRINGS option LIMIT_COUNT value \"" << args[i]
          << "\" is not an unsigned integer.";
405
        status.SetError(e.str());
406
        return false;
407
      }
408 409
      limit_count = count;
      arg_mode = arg_none;
410
    } else if (arg_mode == arg_length_minimum) {
411
      int len;
412
      if (sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0) {
413
        std::ostringstream e;
414 415
        e << "STRINGS option LENGTH_MINIMUM value \"" << args[i]
          << "\" is not an unsigned integer.";
416
        status.SetError(e.str());
417
        return false;
418
      }
419 420
      minlen = len;
      arg_mode = arg_none;
421
    } else if (arg_mode == arg_length_maximum) {
422
      int len;
423
      if (sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0) {
424
        std::ostringstream e;
425 426
        e << "STRINGS option LENGTH_MAXIMUM value \"" << args[i]
          << "\" is not an unsigned integer.";
427
        status.SetError(e.str());
428
        return false;
429
      }
430 431
      maxlen = len;
      arg_mode = arg_none;
432
    } else if (arg_mode == arg_regex) {
433
      if (!regex.compile(args[i])) {
434
        std::ostringstream e;
435 436
        e << "STRINGS option REGEX value \"" << args[i]
          << "\" could not be compiled.";
437
        status.SetError(e.str());
438
        return false;
439
      }
440 441
      have_regex = true;
      arg_mode = arg_none;
442 443
    } else if (arg_mode == arg_encoding) {
      if (args[i] == "UTF-8") {
444
        encoding = encoding_utf8;
445
      } else if (args[i] == "UTF-16LE") {
446
        encoding = encoding_utf16le;
447
      } else if (args[i] == "UTF-16BE") {
448
        encoding = encoding_utf16be;
449
      } else if (args[i] == "UTF-32LE") {
450
        encoding = encoding_utf32le;
451
      } else if (args[i] == "UTF-32BE") {
452
        encoding = encoding_utf32be;
453
      } else {
454
        std::ostringstream e;
455
        e << "STRINGS option ENCODING \"" << args[i] << "\" not recognized.";
456
        status.SetError(e.str());
457 458
        return false;
      }
459 460
      arg_mode = arg_none;
    } else {
461
      std::ostringstream e;
462
      e << "STRINGS given unknown argument \"" << args[i] << "\"";
463
      status.SetError(e.str());
464 465
      return false;
    }
466
  }
467

468
  if (hex_conversion_enabled) {
469
    // TODO: should work without temp file, but just on a memory buffer
470 471
    std::string binaryFileName =
      status.GetMakefile().GetCurrentBinaryDirectory();
472
    binaryFileName += "/CMakeFiles/FileCommandStringsBinaryFile";
473
    if (cmHexFileConverter::TryConvert(fileName, binaryFileName)) {
474
      fileName = binaryFileName;
475
    }
476
  }
477

478
// Open the specified file.
479
#if defined(_WIN32) || defined(__CYGWIN__)
480
  cmsys::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
481
#else
482
  cmsys::ifstream fin(fileName.c_str());
483
#endif
484
  if (!fin) {
485
    std::ostringstream e;
486
    e << "STRINGS file \"" << fileName << "\" cannot be read.";
487
    status.SetError(e.str());
488
    return false;
489
  }
490

491
  // If BOM is found and encoding was not specified, use the BOM
492
  int bom_found = cmsys::FStream::ReadBOM(fin);
493
  if (encoding == encoding_none && bom_found != cmsys::FStream::BOM_None) {
494
    encoding = bom_found;
495
  }
496 497

  unsigned int bytes_rem = 0;
498
  if (encoding == encoding_utf16le || encoding == encoding_utf16be) {
499
    bytes_rem = 1;
500 501
  }
  if (encoding == encoding_utf32le || encoding == encoding_utf32be) {
502
    bytes_rem = 3;
503
  }
504

505 506 507 508
  // Parse strings out of the file.
  int output_size = 0;
  std::vector<std::string> strings;
  std::string s;
509 510 511
  while ((!limit_count || strings.size() < limit_count) &&
         (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
         fin) {
512 513 514
    std::string current_str;

    int c = fin.get();
515
    for (unsigned int i = 0; i < bytes_rem; ++i) {
516
      int c1 = fin.get();
517
      if (!fin) {
518 519 520
        fin.putback(static_cast<char>(c1));
        break;
      }
521 522 523
      c = (c << 8) | c1;
    }
    if (encoding == encoding_utf16le) {
524
      c = ((c & 0xFF) << 8) | ((c & 0xFF00) >> 8);
525 526 527 528
    } else if (encoding == encoding_utf32le) {
      c = (((c & 0xFF) << 24) | ((c & 0xFF00) << 8) | ((c & 0xFF0000) >> 8) |
           ((c & 0xFF000000) >> 24));
    }
529

530
    if (c == '\r') {
531 532
      // Ignore CR character to make output always have UNIX newlines.
      continue;
533
    }
534

535 536
    if (c >= 0 && c <= 0xFF &&
        (isprint(c) || c == '\t' || (c == '\n' && newline_consume))) {
537 538 539 540
      // This is an ASCII character that may be part of a string.
      // Cast added to avoid compiler warning. Cast is ok because
      // c is guaranteed to fit in char by the above if...
      current_str += static_cast<char>(c);
541
    } else if (encoding == encoding_utf8) {
542
      // Check for UTF-8 encoded string (up to 4 octets)
543
      static const unsigned char utf8_check_table[3][2] = {
544 545 546
        { 0xE0, 0xC0 },
        { 0xF0, 0xE0 },
        { 0xF8, 0xF0 },
547
      };
548 549 550

      // how many octets are there?
      unsigned int num_utf8_bytes = 0;
551
      for (unsigned int j = 0; num_utf8_bytes == 0 && j < 3; j++) {
552
        if ((c & utf8_check_table[j][0]) == utf8_check_table[j][1]) {
553
          num_utf8_bytes = j + 2;
554
        }
555
      }
556 557

      // get subsequent octets and check that they are valid
558 559
      for (unsigned int j = 0; j < num_utf8_bytes; j++) {
        if (j != 0) {
560
          c = fin.get();
561
          if (!fin || (c & 0xC0) != 0x80) {
562 563 564 565
            fin.putback(static_cast<char>(c));
            break;
          }
        }
566 567
        current_str += static_cast<char>(c);
      }
568 569 570

      // if this was an invalid utf8 sequence, discard the data, and put
      // back subsequent characters
571 572
      if ((current_str.length() != num_utf8_bytes)) {
        for (unsigned int j = 0; j < current_str.size() - 1; j++) {
573 574 575
          c = current_str[current_str.size() - 1 - j];
          fin.putback(static_cast<char>(c));
        }
576
        current_str.clear();
577
      }
578
    }
579

580
    if (c == '\n' && !newline_consume) {
581 582 583
      // The current line has been terminated.  Check if the current
      // string matches the requirements.  The length may now be as
      // low as zero since blank lines are allowed.
584
      if (s.length() >= minlen && (!have_regex || regex.find(s))) {
Brad King's avatar
Brad King committed
585
        output_size += static_cast<int>(s.size()) + 1;
586
        if (limit_output >= 0 && output_size >= limit_output) {
587
          s.clear();
588 589
          break;
        }
590 591
        strings.push_back(s);
      }
592 593

      // Reset the string to empty.
594
      s.clear();
595
    } else if (current_str.empty()) {
596 597 598
      // A non-string character has been found.  Check if the current
      // string matches the requirements.  We require that the length
      // be at least one no matter what the user specified.
599
      if (s.length() >= minlen && !s.empty() &&
600
          (!have_regex || regex.find(s))) {
601
        output_size += static_cast<int>(s.size()) + 1;
602
        if (limit_output >= 0 && output_size >= limit_output) {
603
          s.clear();
604 605
          break;
        }
606 607
        strings.push_back(s);
      }
608 609

      // Reset the string to empty.
610
      s.clear();
611
    } else {
612
      s += current_str;
613
    }
614

615
    if (maxlen > 0 && s.size() == maxlen) {
616
      // Terminate a string if the maximum length is reached.
617
      if (s.length() >= minlen && (!have_regex || regex.find(s))) {
Brad King's avatar
Brad King committed
618
        output_size += static_cast<int>(s.size()) + 1;
619
        if (limit_output >= 0 && output_size >= limit_output) {
620
          s.clear();
621 622
          break;
        }
623
        strings.push_back(s);
624
      }
625
      s.clear();
626
    }
627
  }
628 629 630 631

  // If there is a non-empty current string we have hit the end of the
  // input file or the input size limit.  Check if the current string
  // matches the requirements.
632
  if ((!limit_count || strings.size() < limit_count) && !s.empty() &&
633
      s.length() >= minlen && (!have_regex || regex.find(s))) {
Brad King's avatar
Brad King committed
634
    output_size += static_cast<int>(s.size()) + 1;
635
    if (limit_output < 0 || output_size < limit_output) {
636 637
      strings.push_back(s);
    }
638
  }
639 640 641 642

  // Encode the result in a CMake list.
  const char* sep = "";
  std::string output;
643
  for (std::string const& sr : strings) {
644 645 646 647 648 649
    // Separate the strings in the output to make it a list.
    output += sep;
    sep = ";";

    // Store the string in the output, but escape semicolons to
    // make sure it is a list.
650 651
    for (char i : sr) {
      if (i == ';') {
652 653
        output += '\\';
      }
654
      output += i;
655
    }
656
  }
657 658

  // Save the output in a makefile variable.
659
  status.GetMakefile().AddDefinition(outVar, output);
660 661 662
  return true;
}

663 664
bool HandleGlobImpl(std::vector<std::string> const& args, bool recurse,
                    cmExecutionStatus& status)
665
{
Amine Chadly's avatar
Amine Chadly committed
666 667
  // File commands has at least one argument
  assert(args.size() > 1);
668 669 670 671 672 673 674

  std::vector<std::string>::const_iterator i = args.begin();

  i++; // Get rid of subcommand

  std::string variable = *i;
  i++;
675
  cmsys::Glob g;
676
  g.SetRecurse(recurse);
677 678

  bool explicitFollowSymlinks = false;
679 680
  cmPolicies::PolicyStatus policyStatus =
    status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0009);
681
  if (recurse) {
682
    switch (policyStatus) {
683 684
      case cmPolicies::REQUIRED_IF_USED:
      case cmPolicies::REQUIRED_ALWAYS:
685 686 687 688 689 690 691 692
      case cmPolicies::NEW:
        g.RecurseThroughSymlinksOff();
        break;
      case cmPolicies::OLD:
      case cmPolicies::WARN:
        g.RecurseThroughSymlinksOn();
        break;
    }
693
  }
694

695
  std::vector<std::string> files;
696 697
  bool configureDepends = false;
  bool warnConfigureLate = false;
698
  bool warnFollowedSymlinks = false;
699
  const cmake::WorkingMode workingMode =
700
    status.GetMakefile().GetCMakeInstance()->GetWorkingMode();
701
  while (i != args.end()) {
702
    if (*i == "LIST_DIRECTORIES") {
703
      ++i; // skip LIST_DIRECTORIES
704
      if (i != args.end()) {
705
        if (cmIsOn(*i)) {
706 707
          g.SetListDirs(true);
          g.SetRecurseListDirs(true);
708
        } else if (cmIsOff(*i)) {
709 710
          g.SetListDirs(false);
          g.SetRecurseListDirs(false);
711
        } else {
712
          status.SetError("LIST_DIRECTORIES missing bool value.");
713 714
          return false;
        }
715
        ++i;
716
      } else {
717
        status.SetError("LIST_DIRECTORIES missing bool value.");
718 719
        return false;
      }
720
    } else if (*i == "FOLLOW_SYMLINKS") {
721 722 723 724 725
      ++i; // skip FOLLOW_SYMLINKS
      if (recurse) {
        explicitFollowSymlinks = true;
        g.RecurseThroughSymlinksOn();
        if (i == args.end()) {
726
          status.SetError(
727 728 729
            "GLOB_RECURSE requires a glob expression after FOLLOW_SYMLINKS.");
          return false;
        }
730
      }
731
    } else if (*i == "RELATIVE") {
732
      ++i; // skip RELATIVE
733
      if (i == args.end()) {
734
        status.SetError("GLOB requires a directory after the RELATIVE tag.");
735
        return false;
736
      }
737 738
      g.SetRelative(i->c_str());
      ++i;
739
      if (i == args.end()) {
740 741
        status.SetError(
          "GLOB requires a glob expression after the directory.");
742
        return false;
743
      }
744 745 746
    } else if (*i == "CONFIGURE_DEPENDS") {
      // Generated build system depends on glob results
      if (!configureDepends && warnConfigureLate) {
747
        status.GetMakefile().IssueMessage(
748
          MessageType::AUTHOR_WARNING,
749 750 751 752
          "CONFIGURE_DEPENDS flag was given after a glob expression was "
          "already evaluated.");
      }
      if (workingMode != cmake::NORMAL_MODE) {
753
        status.GetMakefile().IssueMessage(
754
          MessageType::FATAL_ERROR,
755 756 757 758 759 760
          "CONFIGURE_DEPENDS is invalid for script and find package modes.");
        return false;
      }
      configureDepends = true;
      ++i;
      if (i == args.end()) {
761
        status.SetError(
762 763 764
          "GLOB requires a glob expression after CONFIGURE_DEPENDS.");
        return false;
      }
765
    } else {
766 767
      std::string expr = *i;
      if (!cmsys::SystemTools::FileIsFullPath(*i)) {
768
        expr = status.GetMakefile().GetCurrentSourceDirectory();
769 770 771
        // Handle script mode
        if (!expr.empty()) {
          expr += "/" + *i;
772
        } else {
773
          expr = *i;
774
        }
775
      }
776 777 778 779 780 781 782 783

      cmsys::Glob::GlobMessages globMessages;
      g.FindFiles(expr, &globMessages);

      if (!globMessages.empty()) {
        bool shouldExit = false;
        for (cmsys::Glob::Message const& globMessage : globMessages) {
          if (globMessage.type == cmsys::Glob::cyclicRecursion) {
784
            status.GetMakefile().IssueMessage(
785
              MessageType::AUTHOR_WARNING,
786 787 788
              "Cyclic recursion detected while globbing for '" + *i + "':\n" +
                globMessage.content);
          } else {
789
            status.GetMakefile().IssueMessage(
790
              MessageType::FATAL_ERROR,
791 792
              "Error has occurred while globbing for '" + *i + "' - " +
                globMessage.content);
793 794 795 796 797 798
            shouldExit = true;
          }
        }
        if (shouldExit) {
          return false;
        }
799
      }
800

801 802 803
      if (recurse && !explicitFollowSymlinks &&
          g.GetFollowedSymlinkCount() != 0) {
        warnFollowedSymlinks = true;
804
      }
805

806
      std::vector<std::string>& foundFiles = g.GetFiles();
807
      cmAppend(files, foundFiles);
808 809 810 811 812

      if (configureDepends) {
        std::sort(foundFiles.begin(), foundFiles.end());
        foundFiles.erase(std::unique(foundFiles.begin(), foundFiles.end()),
                         foundFiles.end());
813
        status.GetMakefile().GetCMakeInstance()->AddGlobCacheEntry(
814 815 816
          recurse, (recurse ? g.GetRecurseListDirs() : g.GetListDirs()),
          (recurse ? g.GetRecurseThroughSymlinks() : false),
          (g.GetRelative() ? g.GetRelative() : ""), expr, foundFiles, variable,
817
          status.GetMakefile().GetBacktrace());
818 819 820
      } else {
        warnConfigureLate = true;
      }
821
      ++i;
822
    }
823
  }
824

825
  switch (policyStatus) {
826 827 828 829 830 831 832 833 834 835 836 837
    case cmPolicies::REQUIRED_IF_USED:
    case cmPolicies::REQUIRED_ALWAYS:
    case cmPolicies::NEW:
      // Correct behavior, yay!
      break;
    case cmPolicies::OLD:
    // Probably not really the expected behavior, but the author explicitly
    // asked for the old behavior... no warning.
    case cmPolicies::WARN:
      // Possibly unexpected old behavior *and* we actually traversed
      // symlinks without being explicitly asked to: warn the author.
      if (warnFollowedSymlinks) {
838
        status.GetMakefile().IssueMessage(
839
          MessageType::AUTHOR_WARNING,
840 841 842 843
          cmPolicies::GetPolicyWarning(cmPolicies::CMP0009));
      }
      break;
  }
844 845 846

  std::sort(files.begin(), files.end());
  files.erase(std::unique(files.begin(), files.end()), files.end());
847
  status.GetMakefile().AddDefinition(variable, cmJoin(files, ";"));
848 849
  return true;
}
850

851 852 853 854 855 856 857 858 859 860 861 862
bool HandleGlobCommand(std::vector<std::string> const& args,
                       cmExecutionStatus& status)
{
  return HandleGlobImpl(args, false, status);
}

bool HandleGlobRecurseCommand(std::vector<std::string> const& args,
                              cmExecutionStatus& status)
{
  return HandleGlobImpl(args, true, status);
}

863 864
bool HandleMakeDirectoryCommand(std::vector<std::string> const& args,
                                cmExecutionStatus& status)
865
{
Amine Chadly's avatar
Amine Chadly committed
866 867
  // File command has at least one argument
  assert(args.size() > 1);
868 869

  std::string expr;
870 871 872 873 874
  for (std::string const& arg :
       cmMakeRange(args).advance(1)) // Get rid of subcommand
  {
    const std::string* cdir = &arg;
    if (!cmsys::SystemTools::FileIsFullPath(arg)) {
875
      expr = status.GetMakefile().GetCurrentSourceDirectory();
876
      expr += "/" + arg;
877
      cdir = &expr;
878
    }
879
    if (!status.GetMakefile().CanIWriteThisFile(*cdir)) {
880 881
      std::string e = "attempted to create a directory: " + *cdir +
        " into a source directory.";
882
      status.SetError(e);
883 884
      cmSystemTools::SetFatalErrorOccured();
      return false;
885
    }
886
    if (!cmSystemTools::MakeDirectory(*cdir)) {
887
      std::string error = "problem creating directory: " + *cdir;
888
      status.SetError(error);
889 890
      return false;
    }
891
  }
892 893
  return true;
}
894

895 896
bool HandleTouchImpl(std::vector<std::string> const& args, bool create,
                     cmExecutionStatus& status)
897 898 899 900
{
  // File command has at least one argument
  assert(args.size() > 1);

901 902 903 904
  for (std::string const& arg :
       cmMakeRange(args).advance(1)) // Get rid of subcommand
  {
    std::string tfile = arg;
905
    if (!cmsys::SystemTools::FileIsFullPath(tfile)) {
906
      tfile = status.GetMakefile().GetCurrentSourceDirectory();
907
      tfile += "/" + arg;
908
    }
909
    if (!status.GetMakefile().CanIWriteThisFile(tfile)) {
910 911
      std::string e =
        "attempted to touch a file: " + tfile + " in a source directory.";
912
      status.SetError(e);
913 914 915 916 917
      cmSystemTools::SetFatalErrorOccured();
      return false;
    }
    if (!cmSystemTools::Touch(tfile, create)) {
      std::string error = "problem touching file: " + tfile;
918
      status.SetError(error);
919 920 921 922 923 924
      return false;
    }
  }
  return true;
}

925 926 927 928 929 930 931 932 933 934 935 936
bool HandleTouchCommand(std::vector<std::string> const& args,
                        cmExecutionStatus& status)
{
  return HandleTouchImpl(args, true, status);
}

bool HandleTouchNocreateCommand(std::vector<std::string> const& args,
                                cmExecutionStatus& status)
{
  return HandleTouchImpl(args, false, status);
}

937 938
bool HandleDifferentCommand(std::vector<std::string> const& args,
                            cmExecutionStatus& status)
939 940 941 942 943 944
{
  /*
    FILE(DIFFERENT <variable> FILES <lhs> <rhs>)
   */

  // Evaluate arguments.
Daniel Pfeifer's avatar
Daniel Pfeifer committed
945 946 947
  const char* file_lhs = nullptr;
  const char* file_rhs = nullptr;
  const char* var = nullptr;
948 949 950 951 952 953 954
  enum Doing
  {
    DoingNone,
    DoingVar,
    DoingFileLHS,
    DoingFileRHS
  };
955
  Doing doing = DoingVar;
956 957
  for (unsigned int i = 1; i < args.size(); ++i) {
    if (args[i] == "FILES") {
958
      doing = DoingFileLHS;
959
    } else if (doing == DoingVar) {
960 961
      var = args[i].c_str();
      doing = DoingNone;
962
    } else if (doing == DoingFileLHS) {
963 964
      file_lhs = args[i].c_str();
      doing = DoingFileRHS;
965
    } else if (doing == DoingFileRHS) {
966 967
      file_rhs = args[i].c_str();
      doing = DoingNone;
968
    } else {
969
      std::ostringstream e;
970
      e << "DIFFERENT given unknown argument " << args[i];
971
      status.SetError(e.str());
972 973
      return false;
    }
974 975
  }
  if (!var) {
976
    status.SetError("DIFFERENT not given result variable name.");