cmSourceFile.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmSourceFile_h
  4. #define cmSourceFile_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "cmCustomCommand.h"
  10. #include "cmListFileCache.h"
  11. #include "cmPropertyMap.h"
  12. #include "cmSourceFileLocation.h"
  13. #include "cmSourceFileLocationKind.h"
  14. class cmMakefile;
  15. using cmProp = const std::string*;
  16. /** \class cmSourceFile
  17. * \brief Represent a class loaded from a makefile.
  18. *
  19. * cmSourceFile is represents a class loaded from
  20. * a makefile.
  21. */
  22. class cmSourceFile
  23. {
  24. public:
  25. /**
  26. * Construct with the makefile storing the source and the initial
  27. * name referencing it.
  28. */
  29. cmSourceFile(
  30. cmMakefile* mf, const std::string& name,
  31. cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous);
  32. /**
  33. * Get the custom command for this source file
  34. */
  35. cmCustomCommand* GetCustomCommand() const;
  36. void SetCustomCommand(std::unique_ptr<cmCustomCommand> cc);
  37. //! Set/Get a property of this source file
  38. void SetProperty(const std::string& prop, const char* value);
  39. void AppendProperty(const std::string& prop, const std::string& value,
  40. bool asString = false);
  41. //! Might return a nullptr if the property is not set or invalid
  42. cmProp GetProperty(const std::string& prop) const;
  43. //! Always returns a valid pointer
  44. const char* GetSafeProperty(const std::string& prop) const;
  45. bool GetPropertyAsBool(const std::string& prop) const;
  46. /** Implement getting a property when called from a CMake language
  47. command like get_property or get_source_file_property. */
  48. const char* GetPropertyForUser(const std::string& prop);
  49. //! Checks is the GENERATED property is set and true
  50. /// @return Equivalent to GetPropertyAsBool("GENERATED")
  51. bool GetIsGenerated() const { return this->IsGenerated; }
  52. const std::vector<BT<std::string>>& GetCompileOptions() const
  53. {
  54. return this->CompileOptions;
  55. }
  56. const std::vector<BT<std::string>>& GetCompileDefinitions() const
  57. {
  58. return this->CompileDefinitions;
  59. }
  60. const std::vector<BT<std::string>>& GetIncludeDirectories() const
  61. {
  62. return this->IncludeDirectories;
  63. }
  64. /**
  65. * Resolves the full path to the file. Attempts to locate the file on disk
  66. * and finalizes its location.
  67. */
  68. std::string const& ResolveFullPath(std::string* error = 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);
  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)$"
  136. #endif