cmAuxSourceDirectoryCommand.cxx 2.3 KB

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