cmSourceFile.cxx 11 KB

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