cmSourceFile.cxx 12 KB

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