resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmAuxSourceDirectoryCommand.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmAuxSourceDirectoryCommand.h"
4
5 #include <algorithm>
6 #include <cstddef>
7 #include <utility>
8
9 #include <cm/string_view>
10
11 #include "cmsys/Directory.hxx"
12
13 #include "cmExecutionStatus.h"
14 #include "cmMakefile.h"
15 #include "cmSourceFile.h"
16 #include "cmStringAlgorithms.h"
17 #include "cmSystemTools.h"
18 #include "cmake.h"
19
20 bool cmAuxSourceDirectoryCommand(std::vector<std::string> const& args,
21                                  cmExecutionStatus& status)
22 {
23   if (args.size() != 2) {
24     status.SetError("called with incorrect number of arguments");
25     return false;
26   }
27
28   cmMakefile& mf = status.GetMakefile();
29   std::string sourceListValue;
30   std::string const& templateDirectory = args[0];
31   std::string tdir;
32   if (!cmSystemTools::FileIsFullPath(templateDirectory)) {
33     tdir = cmStrCat(mf.GetCurrentSourceDirectory(), '/', templateDirectory);
34   } else {
35     tdir = templateDirectory;
36   }
37
38   // was the list already populated
39   sourceListValue = mf.GetSafeDefinition(args[1]);
40
41   std::vector<std::string> files;
42
43   // Load all the files in the directory
44   cmsys::Directory dir;
45   if (dir.Load(tdir)) {
46     size_t numfiles = dir.GetNumberOfFiles();
47     for (size_t i = 0; i < numfiles; ++i) {
48       std::string file = dir.GetFile(static_cast<unsigned long>(i));
49       // Split the filename into base and extension
50       std::string::size_type dotpos = file.rfind('.');
51       if (dotpos != std::string::npos) {
52         auto ext = cm::string_view(file).substr(dotpos + 1);
53         // Process only source files
54         auto* cm = mf.GetCMakeInstance();
55         if (dotpos > 0 && cm->IsACLikeSourceExtension(ext)) {
56           std::string fullname = cmStrCat(templateDirectory, '/', file);
57           // add the file as a class file so
58           // depends can be done
59           cmSourceFile* sf = mf.GetOrCreateSource(fullname);
60           sf->SetProperty("ABSTRACT", "0");
61           files.push_back(std::move(fullname));
62         }
63       }
64     }
65   }
66   std::sort(files.begin(), files.end());
67   if (!sourceListValue.empty()) {
68     sourceListValue += ";";
69   }
70   sourceListValue += cmJoin(files, ";");
71   mf.AddDefinition(args[1], sourceListValue);
72   return true;
73 }