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