cmAuxSourceDirectoryCommand.cxx 2.3 KB

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