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 "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 = cmStrCat(mf.GetCurrentSourceDirectory(), '/', templateDirectory);
  27. } else {
  28. tdir = templateDirectory;
  29. }
  30. // was the list already populated
  31. const char* def = mf.GetDefinition(args[1]);
  32. if (def) {
  33. sourceListValue = def;
  34. }
  35. std::vector<std::string> files;
  36. // Load all the files in the directory
  37. cmsys::Directory dir;
  38. if (dir.Load(tdir)) {
  39. size_t numfiles = dir.GetNumberOfFiles();
  40. for (size_t i = 0; i < numfiles; ++i) {
  41. std::string file = dir.GetFile(static_cast<unsigned long>(i));
  42. // Split the filename into base and extension
  43. std::string::size_type dotpos = file.rfind('.');
  44. if (dotpos != std::string::npos) {
  45. std::string ext = file.substr(dotpos + 1);
  46. std::string base = file.substr(0, dotpos);
  47. // Process only source files
  48. auto cm = mf.GetCMakeInstance();
  49. if (!base.empty() && 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. }