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