cmSourceFile.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "cmCustomCommand.h"
  9. #include "cmListFileCache.h"
  10. #include "cmProperty.h"
  11. #include "cmPropertyMap.h"
  12. #include "cmSourceFileLocation.h"
  13. #include "cmSourceFileLocationKind.h"
  14. class cmMakefile;
  15. /** \class cmSourceFile
  16. * \brief Represent a class loaded from a makefile.
  17. *
  18. * cmSourceFile is represents a class loaded from
  19. * a makefile.
  20. */
  21. class cmSourceFile
  22. {
  23. public:
  24. /**
  25. * Construct with the makefile storing the source and the initial
  26. * name referencing it.
  27. */
  28. cmSourceFile(
  29. cmMakefile* mf, const std::string& name,
  30. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous);
  31. /**
  32. * Get the custom command for this source file
  33. */
  34. cmCustomCommand* GetCustomCommand() const;
  35. void SetCustomCommand(std::unique_ptr<cmCustomCommand> cc);
  36. //! Set/Get a property of this source file
  37. void SetProperty(const std::string& prop, const char* value);
  38. void AppendProperty(const std::string& prop, const std::string& value,
  39. bool asString = false);
  40. //! Might return a nullptr if the property is not set or invalid
  41. cmProp GetProperty(const std::string& prop) const;
  42. //! Always returns a valid pointer
  43. const std::string& GetSafeProperty(const std::string& prop) const;
  44. bool GetPropertyAsBool(const std::string& prop) const;
  45. /** Implement getting a property when called from a CMake language
  46. command like get_property or get_source_file_property. */
  47. cmProp GetPropertyForUser(const std::string& prop);
  48. //! Checks is the GENERATED property is set and true
  49. /// @return Equivalent to GetPropertyAsBool("GENERATED")
  50. bool GetIsGenerated() const { return this->IsGenerated; }
  51. const std::vector<BT<std::string>>& GetCompileOptions() const
  52. {
  53. return this->CompileOptions;
  54. }
  55. const std::vector<BT<std::string>>& GetCompileDefinitions() const
  56. {
  57. return this->CompileDefinitions;
  58. }
  59. const std::vector<BT<std::string>>& GetIncludeDirectories() const
  60. {
  61. return this->IncludeDirectories;
  62. }
  63. /**
  64. * Resolves the full path to the file. Attempts to locate the file on disk
  65. * and finalizes its location.
  66. */
  67. std::string const& ResolveFullPath(std::string* error = nullptr,
  68. std::string* cmp0115Warning = nullptr);
  69. /**
  70. * The resolved full path to the file. The returned file name might be empty
  71. * if the path has not yet been resolved.
  72. */
  73. std::string const& GetFullPath() const;
  74. /**
  75. * Get the information currently known about the source file
  76. * location without attempting to locate the file as GetFullPath
  77. * would. See cmSourceFileLocation documentation.
  78. */
  79. cmSourceFileLocation const& GetLocation() const;
  80. /**
  81. * Get the file extension of this source file.
  82. */
  83. std::string const& GetExtension() const;
  84. /**
  85. * Get the language of the compiler to use for this source file.
  86. */
  87. std::string const& GetOrDetermineLanguage();
  88. std::string GetLanguage() const;
  89. /**
  90. * Return the vector that holds the list of dependencies
  91. */
  92. const std::vector<std::string>& GetDepends() const { return this->Depends; }
  93. void AddDepend(const std::string& d) { this->Depends.push_back(d); }
  94. // Get the properties
  95. const cmPropertyMap& GetProperties() const { return this->Properties; }
  96. // Set the properties
  97. void SetProperties(cmPropertyMap properties);
  98. /**
  99. * Check whether the given source file location could refer to this
  100. * source.
  101. */
  102. bool Matches(cmSourceFileLocation const&);
  103. void SetObjectLibrary(std::string const& objlib);
  104. std::string GetObjectLibrary() const;
  105. private:
  106. cmSourceFileLocation Location;
  107. cmPropertyMap Properties;
  108. std::unique_ptr<cmCustomCommand> CustomCommand;
  109. std::string Extension;
  110. std::string Language;
  111. std::string FullPath;
  112. std::string ObjectLibrary;
  113. std::vector<std::string> Depends;
  114. std::vector<BT<std::string>> CompileOptions;
  115. std::vector<BT<std::string>> CompileDefinitions;
  116. std::vector<BT<std::string>> IncludeDirectories;
  117. bool FindFullPathFailed = false;
  118. bool IsGenerated = false;
  119. bool FindFullPath(std::string* error, std::string* cmp0115Warning);
  120. void CheckExtension();
  121. void CheckLanguage(std::string const& ext);
  122. static const std::string propLANGUAGE;
  123. static const std::string propLOCATION;
  124. static const std::string propGENERATED;
  125. static const std::string propCOMPILE_DEFINITIONS;
  126. static const std::string propCOMPILE_OPTIONS;
  127. static const std::string propINCLUDE_DIRECTORIES;
  128. };
  129. // TODO: Factor out into platform information modules.
  130. #define CM_HEADER_REGEX "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$"
  131. #define CM_SOURCE_REGEX \
  132. "\\.(C|F|M|c|c\\+\\+|cc|cpp|cxx|cu|f|f90|for|fpp|ftn|m|mm|" \
  133. "rc|def|r|odl|idl|hpj|bat)$"
  134. #define CM_PCH_REGEX "cmake_pch(_[^.]+)?\\.(h|hxx)$"
  135. #define CM_RESOURCE_REGEX "\\.(pdf|plist|png|jpeg|jpg|storyboard|xcassets)$"