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