cmSourceFile.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "cmPropertyMap.h"
  11. #include "cmSourceFileLocation.h"
  12. #include "cmSourceFileLocationKind.h"
  13. #include "cmValue.h"
  14. class cmMakefile;
  15. /** \class cmSourceFile
  16. * \brief Represent a class loaded from a makefile.
  17. *
  18. * cmSourceFile represents a class loaded from a makefile.
  19. */
  20. class cmSourceFile
  21. {
  22. public:
  23. /**
  24. * Construct with the makefile storing the source and the initial name
  25. * referencing it. If it shall be marked as generated, this source file's
  26. * kind is assumed to be known, regardless of the given value.
  27. */
  28. cmSourceFile(
  29. cmMakefile* mf, const std::string& name, bool generated,
  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, cmValue value);
  38. void RemoveProperty(const std::string& prop)
  39. {
  40. this->SetProperty(prop, cmValue{ nullptr });
  41. }
  42. void SetProperty(const std::string& prop, const std::string& value)
  43. {
  44. this->SetProperty(prop, cmValue(value));
  45. }
  46. void AppendProperty(const std::string& prop, const std::string& value,
  47. bool asString = false);
  48. //! Might return a nullptr if the property is not set or invalid
  49. cmValue GetProperty(const std::string& prop) const;
  50. //! Always returns a valid pointer
  51. const std::string& GetSafeProperty(const std::string& prop) const;
  52. bool GetPropertyAsBool(const std::string& prop) const;
  53. /** Implement getting a property when called from a CMake language
  54. command like get_property or get_source_file_property. */
  55. cmValue GetPropertyForUser(const std::string& prop);
  56. /// Marks this file as generated
  57. /**
  58. * This stores this file's path in the global table for all generated source
  59. * files.
  60. */
  61. void MarkAsGenerated();
  62. enum class CheckScope
  63. {
  64. Global,
  65. GlobalAndLocal
  66. };
  67. /// Determines if this source file is marked as generated.
  68. /**
  69. * This will check if this file's path is stored in the global table of all
  70. * generated source files. If that is not the case and checkScope is set to
  71. * GlobalAndLocal the value of the possibly existing local GENERATED property
  72. * is returned instead.
  73. * @param checkScope Determines if alternatively for backwards-compatibility
  74. * a local GENERATED property should be considered, too.
  75. * @return true if this source file is marked as generated, otherwise false.
  76. */
  77. bool GetIsGenerated(
  78. CheckScope checkScope = CheckScope::GlobalAndLocal) const;
  79. const std::vector<BT<std::string>>& GetCompileOptions() const
  80. {
  81. return this->CompileOptions;
  82. }
  83. const std::vector<BT<std::string>>& GetCompileDefinitions() const
  84. {
  85. return this->CompileDefinitions;
  86. }
  87. const std::vector<BT<std::string>>& GetIncludeDirectories() const
  88. {
  89. return this->IncludeDirectories;
  90. }
  91. /**
  92. * Resolves the full path to the file. Attempts to locate the file on disk
  93. * and finalizes its location.
  94. */
  95. std::string const& ResolveFullPath(std::string* error = nullptr,
  96. std::string* cmp0115Warning = nullptr);
  97. /**
  98. * The resolved full path to the file. The returned file name might be empty
  99. * if the path has not yet been resolved.
  100. */
  101. std::string const& GetFullPath() const;
  102. /**
  103. * Get the information currently known about the source file
  104. * location without attempting to locate the file as GetFullPath
  105. * would. See cmSourceFileLocation documentation.
  106. */
  107. cmSourceFileLocation const& GetLocation() const;
  108. /**
  109. * Get the file extension of this source file.
  110. */
  111. std::string const& GetExtension() const;
  112. /**
  113. * Get the language of the compiler to use for this source file.
  114. */
  115. std::string const& GetOrDetermineLanguage();
  116. std::string GetLanguage() const;
  117. /**
  118. * Return the vector that holds the list of dependencies
  119. */
  120. const std::vector<std::string>& GetDepends() const { return this->Depends; }
  121. void AddDepend(const std::string& d) { this->Depends.push_back(d); }
  122. // Get the properties
  123. const cmPropertyMap& GetProperties() const { return this->Properties; }
  124. // Set the properties
  125. void SetProperties(cmPropertyMap properties);
  126. /**
  127. * Check whether the given source file location could refer to this
  128. * source.
  129. */
  130. bool Matches(cmSourceFileLocation const&);
  131. void SetObjectLibrary(std::string const& objlib);
  132. std::string GetObjectLibrary() const;
  133. private:
  134. cmSourceFileLocation Location;
  135. cmPropertyMap Properties;
  136. std::unique_ptr<cmCustomCommand> CustomCommand;
  137. std::string Extension;
  138. std::string Language;
  139. std::string FullPath;
  140. std::string ObjectLibrary;
  141. std::vector<std::string> Depends;
  142. std::vector<BT<std::string>> CompileOptions;
  143. std::vector<BT<std::string>> CompileDefinitions;
  144. std::vector<BT<std::string>> IncludeDirectories;
  145. bool FindFullPathFailed = false;
  146. bool IsGenerated = false;
  147. bool FindFullPath(std::string* error, std::string* cmp0115Warning);
  148. void CheckExtension();
  149. void CheckLanguage(std::string const& ext);
  150. static const std::string propLANGUAGE;
  151. static const std::string propLOCATION;
  152. static const std::string propGENERATED;
  153. static const std::string propCOMPILE_DEFINITIONS;
  154. static const std::string propCOMPILE_OPTIONS;
  155. static const std::string propINCLUDE_DIRECTORIES;
  156. };
  157. // TODO: Factor out into platform information modules.
  158. #define CM_HEADER_REGEX "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$"
  159. #define CM_SOURCE_REGEX \
  160. "\\.(C|F|M|c|c\\+\\+|cc|cpp|mpp|cxx|ixx|cppm|ccm|cxxm|c\\+\\+m|cu" \
  161. "|f|f90|for|fpp|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$"
  162. #define CM_PCH_REGEX "cmake_pch(_[^.]+)?\\.(h|hxx)$"
  163. #define CM_RESOURCE_REGEX "\\.(pdf|plist|png|jpeg|jpg|storyboard|xcassets)$"