cmSourceFile.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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, std::string const& 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. enum class SpecialSourceType
  37. {
  38. // Sources for user-provided sources.
  39. User,
  40. // Source files for object files.
  41. Object,
  42. // Sources representing `CMakeLists.txt` files.
  43. CMakeLists,
  44. // Xcode `Info.plist` files for bundle targets.
  45. BundleInfoPlist,
  46. // Xcode source to force a link to occur with an appropriate language.
  47. XcodeForceLinkerSource,
  48. // Xcode `
  49. XcodeXCConfigFile,
  50. // Header set verification files.
  51. HeaderSetVerificationSource,
  52. // PCH files.
  53. PchHeader,
  54. PchSource,
  55. PchPdbReuseSource,
  56. // Unity sources.
  57. UnitySource,
  58. // Qt support sources.
  59. QtWrapCppSource,
  60. QtAutogenSource,
  61. };
  62. void SetSpecialSourceType(SpecialSourceType type);
  63. bool IsPchHeader() const;
  64. bool IsPchSource() const;
  65. //! Set/Get a property of this source file
  66. void SetProperty(std::string const& prop, cmValue value);
  67. void RemoveProperty(std::string const& prop)
  68. {
  69. this->SetProperty(prop, cmValue{ nullptr });
  70. }
  71. void SetProperty(std::string const& prop, std::string const& value)
  72. {
  73. this->SetProperty(prop, cmValue(value));
  74. }
  75. void AppendProperty(std::string const& prop, std::string const& value,
  76. bool asString = false);
  77. //! Might return a nullptr if the property is not set or invalid
  78. cmValue GetProperty(std::string const& prop) const;
  79. //! Always returns a valid pointer
  80. std::string const& GetSafeProperty(std::string const& prop) const;
  81. bool GetPropertyAsBool(std::string const& prop) const;
  82. /** Implement getting a property when called from a CMake language
  83. command like get_property or get_source_file_property. */
  84. cmValue GetPropertyForUser(std::string const& prop);
  85. /// Marks this file as generated
  86. /**
  87. * This stores this file's path in the global table for all generated source
  88. * files.
  89. */
  90. void MarkAsGenerated();
  91. enum class CheckScope
  92. {
  93. Global,
  94. GlobalAndLocal
  95. };
  96. /// Determines if this source file is marked as generated.
  97. /**
  98. * This will check if this file's path is stored in the global table of all
  99. * generated source files. If that is not the case and checkScope is set to
  100. * GlobalAndLocal the value of the possibly existing local GENERATED property
  101. * is returned instead.
  102. * @param checkScope Determines if alternatively for backwards-compatibility
  103. * a local GENERATED property should be considered, too.
  104. * @return true if this source file is marked as generated, otherwise false.
  105. */
  106. bool GetIsGenerated(
  107. CheckScope checkScope = CheckScope::GlobalAndLocal) const;
  108. std::vector<BT<std::string>> const& GetCompileOptions() const
  109. {
  110. return this->CompileOptions;
  111. }
  112. std::vector<BT<std::string>> const& GetCompileDefinitions() const
  113. {
  114. return this->CompileDefinitions;
  115. }
  116. std::vector<BT<std::string>> const& GetIncludeDirectories() const
  117. {
  118. return this->IncludeDirectories;
  119. }
  120. /**
  121. * Resolves the full path to the file. Attempts to locate the file on disk
  122. * and finalizes its location.
  123. */
  124. std::string const& ResolveFullPath(std::string* error = nullptr,
  125. std::string* cmp0115Warning = nullptr);
  126. /**
  127. * The resolved full path to the file. The returned file name might be empty
  128. * if the path has not yet been resolved.
  129. */
  130. std::string const& GetFullPath() const;
  131. /**
  132. * Get the information currently known about the source file
  133. * location without attempting to locate the file as GetFullPath
  134. * would. See cmSourceFileLocation documentation.
  135. */
  136. cmSourceFileLocation const& GetLocation() const;
  137. /**
  138. * Get the file extension of this source file.
  139. */
  140. std::string const& GetExtension() const;
  141. /**
  142. * Get the language of the compiler to use for this source file.
  143. */
  144. std::string const& GetOrDetermineLanguage();
  145. std::string GetLanguage() const;
  146. /**
  147. * Return the vector that holds the list of dependencies
  148. */
  149. std::vector<std::string> const& GetDepends() const { return this->Depends; }
  150. void AddDepend(std::string const& d) { this->Depends.push_back(d); }
  151. // Get the properties
  152. cmPropertyMap const& GetProperties() const { return this->Properties; }
  153. // Set the properties
  154. void SetProperties(cmPropertyMap properties);
  155. /**
  156. * Check whether the given source file location could refer to this
  157. * source.
  158. */
  159. bool Matches(cmSourceFileLocation const&);
  160. void SetObjectLibrary(std::string const& objlib);
  161. std::string GetObjectLibrary() const;
  162. private:
  163. cmSourceFileLocation Location;
  164. cmPropertyMap Properties;
  165. std::unique_ptr<cmCustomCommand> CustomCommand;
  166. std::string Extension;
  167. std::string Language;
  168. std::string FullPath;
  169. std::string ObjectLibrary;
  170. std::vector<std::string> Depends;
  171. std::vector<BT<std::string>> CompileOptions;
  172. std::vector<BT<std::string>> CompileDefinitions;
  173. std::vector<BT<std::string>> IncludeDirectories;
  174. bool FindFullPathFailed = false;
  175. bool IsGenerated = false;
  176. SpecialSourceType SpecialSource = SpecialSourceType::User;
  177. bool FindFullPath(std::string* error, std::string* cmp0115Warning);
  178. void CheckExtension();
  179. void CheckLanguage(std::string const& ext);
  180. static std::string const propLANGUAGE;
  181. static std::string const propLOCATION;
  182. static std::string const propGENERATED;
  183. static std::string const propCOMPILE_DEFINITIONS;
  184. static std::string const propCOMPILE_OPTIONS;
  185. static std::string const propINCLUDE_DIRECTORIES;
  186. };
  187. // TODO: Factor out into platform information modules.
  188. #define CM_HEADER_REGEX "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$"
  189. #define CM_SOURCE_REGEX \
  190. "\\.(C|F|M|c|c\\+\\+|cc|cpp|mpp|cxx|ixx|cppm|ccm|cxxm|c\\+\\+m|cu" \
  191. "|f|f90|for|fpp|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$"
  192. #define CM_PCH_REGEX "cmake_pch(_[^.]+)?\\.(h|hxx)$"
  193. #define CM_RESOURCE_REGEX "\\.(pdf|plist|png|jpeg|jpg|storyboard|xcassets)$"