cmSourceFile.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmSourceFile.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmSystemTools.h"
  14. #include "cmake.h"
  15. //----------------------------------------------------------------------------
  16. cmSourceFile::cmSourceFile(cmMakefile* mf, const std::string& name):
  17. Location(mf, name)
  18. {
  19. this->CustomCommand = 0;
  20. this->FindFullPathFailed = false;
  21. this->IsUiFile = (".ui" ==
  22. cmSystemTools::GetFilenameLastExtension(this->Location.GetName()));
  23. }
  24. //----------------------------------------------------------------------------
  25. cmSourceFile::~cmSourceFile()
  26. {
  27. this->SetCustomCommand(0);
  28. }
  29. //----------------------------------------------------------------------------
  30. std::string const& cmSourceFile::GetExtension() const
  31. {
  32. return this->Extension;
  33. }
  34. const std::string cmSourceFile::propLANGUAGE = "LANGUAGE";
  35. //----------------------------------------------------------------------------
  36. void cmSourceFile::SetObjectLibrary(std::string const& objlib)
  37. {
  38. this->ObjectLibrary = objlib;
  39. }
  40. //----------------------------------------------------------------------------
  41. std::string cmSourceFile::GetObjectLibrary() const
  42. {
  43. return this->ObjectLibrary;
  44. }
  45. //----------------------------------------------------------------------------
  46. std::string cmSourceFile::GetLanguage()
  47. {
  48. // If the language was set explicitly by the user then use it.
  49. if(const char* lang = this->GetProperty(propLANGUAGE))
  50. {
  51. return lang;
  52. }
  53. // Perform computation needed to get the language if necessary.
  54. if(this->FullPath.empty() && this->Language.empty())
  55. {
  56. // If a known extension is given or a known full path is given
  57. // then trust that the current extension is sufficient to
  58. // determine the language. This will fail only if the user
  59. // specifies a full path to the source but leaves off the
  60. // extension, which is kind of weird.
  61. if(this->Location.ExtensionIsAmbiguous() &&
  62. this->Location.DirectoryIsAmbiguous())
  63. {
  64. // Finalize the file location to get the extension and set the
  65. // language.
  66. this->GetFullPath();
  67. }
  68. else
  69. {
  70. // Use the known extension to get the language if possible.
  71. std::string ext =
  72. cmSystemTools::GetFilenameLastExtension(this->Location.GetName());
  73. this->CheckLanguage(ext);
  74. }
  75. }
  76. // Now try to determine the language.
  77. return static_cast<cmSourceFile const*>(this)->GetLanguage();
  78. }
  79. //----------------------------------------------------------------------------
  80. std::string cmSourceFile::GetLanguage() const
  81. {
  82. // If the language was set explicitly by the user then use it.
  83. if(const char* lang = this->GetProperty(propLANGUAGE))
  84. {
  85. return lang;
  86. }
  87. // If the language was determined from the source file extension use it.
  88. if(!this->Language.empty())
  89. {
  90. return this->Language;
  91. }
  92. // The language is not known.
  93. return "";
  94. }
  95. //----------------------------------------------------------------------------
  96. cmSourceFileLocation const& cmSourceFile::GetLocation() const
  97. {
  98. return this->Location;
  99. }
  100. //----------------------------------------------------------------------------
  101. std::string const& cmSourceFile::GetFullPath(std::string* error)
  102. {
  103. if(this->FullPath.empty())
  104. {
  105. if(this->FindFullPath(error))
  106. {
  107. this->CheckExtension();
  108. }
  109. }
  110. return this->FullPath;
  111. }
  112. //----------------------------------------------------------------------------
  113. std::string const& cmSourceFile::GetFullPath() const
  114. {
  115. return this->FullPath;
  116. }
  117. //----------------------------------------------------------------------------
  118. bool cmSourceFile::FindFullPath(std::string* error)
  119. {
  120. // If thie method has already failed once do not try again.
  121. if(this->FindFullPathFailed)
  122. {
  123. return false;
  124. }
  125. // If the file is generated compute the location without checking on
  126. // disk.
  127. if(this->GetPropertyAsBool("GENERATED"))
  128. {
  129. // The file is either already a full path or is relative to the
  130. // build directory for the target.
  131. this->Location.DirectoryUseBinary();
  132. this->FullPath = this->Location.GetDirectory();
  133. this->FullPath += "/";
  134. this->FullPath += this->Location.GetName();
  135. return true;
  136. }
  137. // The file is not generated. It must exist on disk.
  138. cmMakefile const* mf = this->Location.GetMakefile();
  139. const char* tryDirs[3] = {0, 0, 0};
  140. if(this->Location.DirectoryIsAmbiguous())
  141. {
  142. tryDirs[0] = mf->GetCurrentSourceDirectory();
  143. tryDirs[1] = mf->GetCurrentBinaryDirectory();
  144. }
  145. else
  146. {
  147. tryDirs[0] = "";
  148. }
  149. const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
  150. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  151. for(const char* const* di = tryDirs; *di; ++di)
  152. {
  153. std::string tryPath = this->Location.GetDirectory();
  154. if(!tryPath.empty())
  155. {
  156. tryPath += "/";
  157. }
  158. tryPath += this->Location.GetName();
  159. tryPath = cmSystemTools::CollapseFullPath(tryPath, *di);
  160. if(this->TryFullPath(tryPath, ""))
  161. {
  162. return true;
  163. }
  164. for(std::vector<std::string>::const_iterator ei = srcExts.begin();
  165. ei != srcExts.end(); ++ei)
  166. {
  167. if(this->TryFullPath(tryPath, *ei))
  168. {
  169. return true;
  170. }
  171. }
  172. for(std::vector<std::string>::const_iterator ei = hdrExts.begin();
  173. ei != hdrExts.end(); ++ei)
  174. {
  175. if(this->TryFullPath(tryPath, *ei))
  176. {
  177. return true;
  178. }
  179. }
  180. }
  181. std::ostringstream e;
  182. std::string missing = this->Location.GetDirectory();
  183. if(!missing.empty())
  184. {
  185. missing += "/";
  186. }
  187. missing += this->Location.GetName();
  188. e << "Cannot find source file:\n " << missing << "\nTried extensions";
  189. for(std::vector<std::string>::const_iterator ext = srcExts.begin();
  190. ext != srcExts.end(); ++ext)
  191. {
  192. e << " ." << *ext;
  193. }
  194. for(std::vector<std::string>::const_iterator ext = hdrExts.begin();
  195. ext != hdrExts.end(); ++ext)
  196. {
  197. e << " ." << *ext;
  198. }
  199. if(error)
  200. {
  201. *error = e.str();
  202. }
  203. else
  204. {
  205. this->Location.GetMakefile()->IssueMessage(cmake::FATAL_ERROR, e.str());
  206. }
  207. this->FindFullPathFailed = true;
  208. return false;
  209. }
  210. //----------------------------------------------------------------------------
  211. bool cmSourceFile::TryFullPath(const std::string& path,
  212. const std::string& ext)
  213. {
  214. std::string tryPath = path;
  215. if(!ext.empty())
  216. {
  217. tryPath += ".";
  218. tryPath += ext;
  219. }
  220. if(cmSystemTools::FileExists(tryPath.c_str()))
  221. {
  222. this->FullPath = tryPath;
  223. return true;
  224. }
  225. return false;
  226. }
  227. //----------------------------------------------------------------------------
  228. void cmSourceFile::CheckExtension()
  229. {
  230. // Compute the extension.
  231. std::string realExt =
  232. cmSystemTools::GetFilenameLastExtension(this->FullPath);
  233. if(!realExt.empty())
  234. {
  235. // Store the extension without the leading '.'.
  236. this->Extension = realExt.substr(1);
  237. }
  238. // Look for object files.
  239. if(this->Extension == "obj" ||
  240. this->Extension == "o" ||
  241. this->Extension == "lo")
  242. {
  243. this->SetProperty("EXTERNAL_OBJECT", "1");
  244. }
  245. // Try to identify the source file language from the extension.
  246. if(this->Language.empty())
  247. {
  248. this->CheckLanguage(this->Extension);
  249. }
  250. }
  251. //----------------------------------------------------------------------------
  252. void cmSourceFile::CheckLanguage(std::string const& ext)
  253. {
  254. // Try to identify the source file language from the extension.
  255. cmMakefile const* mf = this->Location.GetMakefile();
  256. cmGlobalGenerator* gg = mf->GetGlobalGenerator();
  257. std::string l = gg->GetLanguageFromExtension(ext.c_str());
  258. if(!l.empty())
  259. {
  260. this->Language = l;
  261. }
  262. }
  263. //----------------------------------------------------------------------------
  264. bool cmSourceFile::Matches(cmSourceFileLocation const& loc)
  265. {
  266. return this->Location.Matches(loc);
  267. }
  268. //----------------------------------------------------------------------------
  269. void cmSourceFile::SetProperty(const std::string& prop, const char* value)
  270. {
  271. this->Properties.SetProperty(prop, value);
  272. if (this->IsUiFile)
  273. {
  274. cmMakefile const* mf = this->Location.GetMakefile();
  275. if (prop == "AUTOUIC_OPTIONS")
  276. {
  277. const_cast<cmMakefile*>(mf)->AddQtUiFileWithOptions(this);
  278. }
  279. }
  280. }
  281. //----------------------------------------------------------------------------
  282. void cmSourceFile::AppendProperty(const std::string& prop, const char* value,
  283. bool asString)
  284. {
  285. this->Properties.AppendProperty(prop, value, asString);
  286. }
  287. //----------------------------------------------------------------------------
  288. const char* cmSourceFile::GetPropertyForUser(const std::string& prop)
  289. {
  290. // This method is a consequence of design history and backwards
  291. // compatibility. GetProperty is (and should be) a const method.
  292. // Computed properties should not be stored back in the property map
  293. // but instead reference information already known. If they need to
  294. // cache information in a mutable ivar to provide the return string
  295. // safely then so be it.
  296. //
  297. // The LOCATION property is particularly problematic. The CMake
  298. // language has very loose restrictions on the names that will match
  299. // a given source file (for historical reasons). Implementing
  300. // lookups correctly with such loose naming requires the
  301. // cmSourceFileLocation class to commit to a particular full path to
  302. // the source file as late as possible. If the users requests the
  303. // LOCATION property we must commit now.
  304. if(prop == "LOCATION")
  305. {
  306. // Commit to a location.
  307. this->GetFullPath();
  308. }
  309. // Perform the normal property lookup.
  310. return this->GetProperty(prop);
  311. }
  312. //----------------------------------------------------------------------------
  313. const char* cmSourceFile::GetProperty(const std::string& prop) const
  314. {
  315. // Check for computed properties.
  316. if(prop == "LOCATION")
  317. {
  318. if(this->FullPath.empty())
  319. {
  320. return 0;
  321. }
  322. else
  323. {
  324. return this->FullPath.c_str();
  325. }
  326. }
  327. const char *retVal = this->Properties.GetPropertyValue(prop);
  328. if (!retVal)
  329. {
  330. cmMakefile const* mf = this->Location.GetMakefile();
  331. const bool chain = mf->GetState()->
  332. IsPropertyChained(prop, cmProperty::SOURCE_FILE);
  333. if (chain)
  334. {
  335. return mf->GetProperty(prop, chain);
  336. }
  337. }
  338. return retVal;
  339. }
  340. //----------------------------------------------------------------------------
  341. bool cmSourceFile::GetPropertyAsBool(const std::string& prop) const
  342. {
  343. return cmSystemTools::IsOn(this->GetProperty(prop));
  344. }
  345. //----------------------------------------------------------------------------
  346. cmCustomCommand* cmSourceFile::GetCustomCommand()
  347. {
  348. return this->CustomCommand;
  349. }
  350. //----------------------------------------------------------------------------
  351. cmCustomCommand const* cmSourceFile::GetCustomCommand() const
  352. {
  353. return this->CustomCommand;
  354. }
  355. //----------------------------------------------------------------------------
  356. void cmSourceFile::SetCustomCommand(cmCustomCommand* cc)
  357. {
  358. cmCustomCommand* old = this->CustomCommand;
  359. this->CustomCommand = cc;
  360. delete old;
  361. }