cmAuxSourceDirectoryCommand.cxx 2.35 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 "cmAuxSourceDirectoryCommand.h"
Brad King's avatar
Brad King committed
4

5
#include "cmsys/Directory.hxx"
6 7
#include <algorithm>
#include <stddef.h>
8
#include <utility>
9

10
#include "cmExecutionStatus.h"
11
#include "cmMakefile.h"
Ken Martin's avatar
Ken Martin committed
12
#include "cmSourceFile.h"
13
#include "cmStringAlgorithms.h"
14 15
#include "cmSystemTools.h"
#include "cmake.h"
16

17 18
bool cmAuxSourceDirectoryCommand(std::vector<std::string> const& args,
                                 cmExecutionStatus& status)
19
{
20
  if (args.size() != 2) {
21
    status.SetError("called with incorrect number of arguments");
22
    return false;
23
  }
24

25
  cmMakefile& mf = status.GetMakefile();
26
  std::string sourceListValue;
27
  std::string const& templateDirectory = args[0];
28
  std::string tdir;
29
  if (!cmSystemTools::FileIsFullPath(templateDirectory)) {
30
    tdir = mf.GetCurrentSourceDirectory();
31 32
    tdir += "/";
    tdir += templateDirectory;
33
  } else {
34
    tdir = templateDirectory;
35
  }
Ken Martin's avatar
Ken Martin committed
36 37

  // was the list already populated
38
  const char* def = mf.GetDefinition(args[1]);
39
  if (def) {
Ken Martin's avatar
Ken Martin committed
40
    sourceListValue = def;
41
  }
42

43 44
  std::vector<std::string> files;

45
  // Load all the files in the directory
46
  cmsys::Directory dir;
47
  if (dir.Load(tdir)) {
48
    size_t numfiles = dir.GetNumberOfFiles();
49
    for (size_t i = 0; i < numfiles; ++i) {
Ken Martin's avatar
Ken Martin committed
50
      std::string file = dir.GetFile(static_cast<unsigned long>(i));
51
      // Split the filename into base and extension
52
      std::string::size_type dotpos = file.rfind('.');
53 54
      if (dotpos != std::string::npos) {
        std::string ext = file.substr(dotpos + 1);
55
        std::string base = file.substr(0, dotpos);
56
        // Process only source files
57
        auto cm = mf.GetCMakeInstance();
58
        if (!base.empty() && cm->IsSourceExtension(ext)) {
59 60 61
          std::string fullname = templateDirectory;
          fullname += "/";
          fullname += file;
62
          // add the file as a class file so
63
          // depends can be done
64
          cmSourceFile* sf = mf.GetOrCreateSource(fullname);
65
          sf->SetProperty("ABSTRACT", "0");
66
          files.push_back(std::move(fullname));
67 68 69
        }
      }
    }
70
  }
71 72 73 74 75
  std::sort(files.begin(), files.end());
  if (!sourceListValue.empty()) {
    sourceListValue += ";";
  }
  sourceListValue += cmJoin(files, ";");
76
  mf.AddDefinition(args[1], sourceListValue);
77 78
  return true;
}