cmAuxSourceDirectoryCommand.cxx 2.3 KB

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