cmAuxSourceDirectoryCommand.cxx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <algorithm>
  5. #include <cstddef>
  6. #include <utility>
  7. #include <cm/string_view>
  8. #include "cmsys/Directory.hxx"
  9. #include "cmExecutionStatus.h"
  10. #include "cmMakefile.h"
  11. #include "cmSourceFile.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. #include "cmake.h"
  15. bool cmAuxSourceDirectoryCommand(std::vector<std::string> const& args,
  16. cmExecutionStatus& status)
  17. {
  18. if (args.size() != 2) {
  19. status.SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. cmMakefile& mf = status.GetMakefile();
  23. std::string sourceListValue;
  24. std::string const& templateDirectory = args[0];
  25. std::string tdir;
  26. if (!cmSystemTools::FileIsFullPath(templateDirectory)) {
  27. tdir = cmStrCat(mf.GetCurrentSourceDirectory(), '/', templateDirectory);
  28. } else {
  29. tdir = templateDirectory;
  30. }
  31. // was the list already populated
  32. const char* def = mf.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. auto ext = cm::string_view(file).substr(dotpos + 1);
  47. // Process only source files
  48. auto cm = mf.GetCMakeInstance();
  49. if (dotpos > 0 && cm->IsSourceExtension(ext)) {
  50. std::string fullname = cmStrCat(templateDirectory, '/', file);
  51. // add the file as a class file so
  52. // depends can be done
  53. cmSourceFile* sf = mf.GetOrCreateSource(fullname);
  54. sf->SetProperty("ABSTRACT", "0");
  55. files.push_back(std::move(fullname));
  56. }
  57. }
  58. }
  59. }
  60. std::sort(files.begin(), files.end());
  61. if (!sourceListValue.empty()) {
  62. sourceListValue += ";";
  63. }
  64. sourceListValue += cmJoin(files, ";");
  65. mf.AddDefinition(args[1], sourceListValue);
  66. return true;
  67. }