cmGetSourceFilePropertyCommand.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "cmGetSourceFilePropertyCommand.h"
  4. #include "cmExecutionStatus.h"
  5. #include "cmMakefile.h"
  6. #include "cmProperty.h"
  7. #include "cmSetPropertyCommand.h"
  8. #include "cmSourceFile.h"
  9. bool cmGetSourceFilePropertyCommand(std::vector<std::string> const& args,
  10. cmExecutionStatus& status)
  11. {
  12. std::vector<std::string>::size_type args_size = args.size();
  13. if (args_size != 3 && args_size != 5) {
  14. status.SetError("called with incorrect number of arguments");
  15. return false;
  16. }
  17. std::vector<std::string> source_file_directories;
  18. std::vector<std::string> source_file_target_directories;
  19. bool source_file_directory_option_enabled = false;
  20. bool source_file_target_option_enabled = false;
  21. int property_arg_index = 2;
  22. if (args[2] == "DIRECTORY" && args_size == 5) {
  23. property_arg_index = 4;
  24. source_file_directory_option_enabled = true;
  25. source_file_directories.push_back(args[3]);
  26. } else if (args[2] == "TARGET_DIRECTORY" && args_size == 5) {
  27. property_arg_index = 4;
  28. source_file_target_option_enabled = true;
  29. source_file_target_directories.push_back(args[3]);
  30. }
  31. std::vector<cmMakefile*> source_file_directory_makefiles;
  32. bool file_scopes_handled =
  33. SetPropertyCommand::HandleAndValidateSourceFileDirectortoryScopes(
  34. status, source_file_directory_option_enabled,
  35. source_file_target_option_enabled, source_file_directories,
  36. source_file_target_directories, source_file_directory_makefiles);
  37. if (!file_scopes_handled) {
  38. return false;
  39. }
  40. std::string const& var = args[0];
  41. bool source_file_paths_should_be_absolute =
  42. source_file_directory_option_enabled || source_file_target_option_enabled;
  43. std::string const file =
  44. SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded(
  45. status, args[1], source_file_paths_should_be_absolute);
  46. cmMakefile& mf = *source_file_directory_makefiles[0];
  47. cmSourceFile* sf = mf.GetSource(file);
  48. // for the location we must create a source file first
  49. if (!sf && args[property_arg_index] == "LOCATION") {
  50. sf = mf.CreateSource(file);
  51. }
  52. if (sf) {
  53. cmProp prop = nullptr;
  54. if (!args[property_arg_index].empty()) {
  55. prop = sf->GetPropertyForUser(args[property_arg_index]);
  56. }
  57. if (prop) {
  58. // Set the value on the original Makefile scope, not the scope of the
  59. // requested directory.
  60. status.GetMakefile().AddDefinition(var, *prop);
  61. return true;
  62. }
  63. }
  64. status.GetMakefile().AddDefinition(var, "NOTFOUND");
  65. return true;
  66. }