cmSourceFile.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSourceFile.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmake.h"
  19. //----------------------------------------------------------------------------
  20. cmSourceFile::cmSourceFile(cmMakefile* mf, const char* name):
  21. Location(mf, name)
  22. {
  23. this->CustomCommand = 0;
  24. this->Properties.SetCMakeInstance(mf->GetCMakeInstance());
  25. this->FindFullPathFailed = false;
  26. }
  27. //----------------------------------------------------------------------------
  28. cmSourceFile::~cmSourceFile()
  29. {
  30. this->SetCustomCommand(0);
  31. }
  32. //----------------------------------------------------------------------------
  33. std::string const& cmSourceFile::GetExtension() const
  34. {
  35. return this->Extension;
  36. }
  37. //----------------------------------------------------------------------------
  38. const char* cmSourceFile::GetLanguage()
  39. {
  40. // Compute the final location of the file if necessary.
  41. if(this->FullPath.empty())
  42. {
  43. this->GetFullPath();
  44. }
  45. // Now try to determine the language.
  46. return static_cast<cmSourceFile const*>(this)->GetLanguage();
  47. }
  48. //----------------------------------------------------------------------------
  49. const char* cmSourceFile::GetLanguage() const
  50. {
  51. // If the language was set explicitly by the user then use it.
  52. if(const char* lang = this->GetProperty("LANGUAGE"))
  53. {
  54. return lang;
  55. }
  56. // If the language was determined from the source file extension use it.
  57. if(!this->Language.empty())
  58. {
  59. return this->Language.c_str();
  60. }
  61. // The language is not known.
  62. return 0;
  63. }
  64. //----------------------------------------------------------------------------
  65. cmSourceFileLocation const& cmSourceFile::GetLocation() const
  66. {
  67. return this->Location;
  68. }
  69. //----------------------------------------------------------------------------
  70. std::string const& cmSourceFile::GetFullPath()
  71. {
  72. if(this->FullPath.empty())
  73. {
  74. if(this->FindFullPath())
  75. {
  76. this->CheckExtension();
  77. }
  78. }
  79. return this->FullPath;
  80. }
  81. //----------------------------------------------------------------------------
  82. std::string const& cmSourceFile::GetFullPath() const
  83. {
  84. return this->FullPath;
  85. }
  86. //----------------------------------------------------------------------------
  87. bool cmSourceFile::FindFullPath()
  88. {
  89. // If thie method has already failed once do not try again.
  90. if(this->FindFullPathFailed)
  91. {
  92. return false;
  93. }
  94. // If the file is generated compute the location without checking on
  95. // disk.
  96. if(this->GetPropertyAsBool("GENERATED"))
  97. {
  98. // The file is either already a full path or is relative to the
  99. // build directory for the target.
  100. this->Location.DirectoryUseBinary();
  101. this->FullPath = this->Location.GetDirectory();
  102. this->FullPath += "/";
  103. this->FullPath += this->Location.GetName();
  104. return true;
  105. }
  106. // The file is not generated. It must exist on disk.
  107. cmMakefile* mf = this->Location.GetMakefile();
  108. const char* tryDirs[3] = {0, 0, 0};
  109. if(this->Location.DirectoryIsAmbiguous())
  110. {
  111. tryDirs[0] = mf->GetCurrentDirectory();
  112. tryDirs[1] = mf->GetCurrentOutputDirectory();
  113. }
  114. else
  115. {
  116. tryDirs[0] = "";
  117. }
  118. const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
  119. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  120. for(const char* const* di = tryDirs; *di; ++di)
  121. {
  122. std::string tryPath = this->Location.GetDirectory();
  123. if(!tryPath.empty())
  124. {
  125. tryPath += "/";
  126. }
  127. tryPath += this->Location.GetName();
  128. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str(), *di);
  129. if(this->TryFullPath(tryPath.c_str(), 0))
  130. {
  131. return true;
  132. }
  133. for(std::vector<std::string>::const_iterator ei = srcExts.begin();
  134. ei != srcExts.end(); ++ei)
  135. {
  136. if(this->TryFullPath(tryPath.c_str(), ei->c_str()))
  137. {
  138. return true;
  139. }
  140. }
  141. for(std::vector<std::string>::const_iterator ei = hdrExts.begin();
  142. ei != hdrExts.end(); ++ei)
  143. {
  144. if(this->TryFullPath(tryPath.c_str(), ei->c_str()))
  145. {
  146. return true;
  147. }
  148. }
  149. }
  150. cmOStringStream e;
  151. e << "Cannot find source file \"" << this->Location.GetName() << "\"";
  152. e << "\n\nTried extensions";
  153. for(std::vector<std::string>::const_iterator ext = srcExts.begin();
  154. ext != srcExts.end(); ++ext)
  155. {
  156. e << " ." << *ext;
  157. }
  158. for(std::vector<std::string>::const_iterator ext = hdrExts.begin();
  159. ext != hdrExts.end(); ++ext)
  160. {
  161. e << " ." << *ext;
  162. }
  163. cmSystemTools::Error(e.str().c_str());
  164. this->FindFullPathFailed = true;
  165. return false;
  166. }
  167. //----------------------------------------------------------------------------
  168. bool cmSourceFile::TryFullPath(const char* tp, const char* ext)
  169. {
  170. std::string tryPath = tp;
  171. if(ext && *ext)
  172. {
  173. tryPath += ".";
  174. tryPath += ext;
  175. }
  176. if(cmSystemTools::FileExists(tryPath.c_str()))
  177. {
  178. this->FullPath = tryPath;
  179. return true;
  180. }
  181. return false;
  182. }
  183. //----------------------------------------------------------------------------
  184. void cmSourceFile::CheckExtension()
  185. {
  186. // Compute the extension.
  187. std::string realExt =
  188. cmSystemTools::GetFilenameLastExtension(this->FullPath);
  189. if(!realExt.empty())
  190. {
  191. // Store the extension without the leading '.'.
  192. this->Extension = realExt.substr(1);
  193. }
  194. // Look for object files.
  195. if(this->Extension == "obj" ||
  196. this->Extension == "o" ||
  197. this->Extension == "lo")
  198. {
  199. this->SetProperty("EXTERNAL_OBJECT", "1");
  200. }
  201. // Look for header files.
  202. cmMakefile* mf = this->Location.GetMakefile();
  203. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  204. if(std::find(hdrExts.begin(), hdrExts.end(), this->Extension) ==
  205. hdrExts.end())
  206. {
  207. // This is not a known header file extension. Mark it as not a
  208. // header unless the user has already explicitly set the property.
  209. if(!this->GetProperty("HEADER_FILE_ONLY"))
  210. {
  211. this->SetProperty("HEADER_FILE_ONLY", "0");
  212. }
  213. }
  214. else
  215. {
  216. // This is a known header file extension. The source cannot be compiled.
  217. this->SetProperty("HEADER_FILE_ONLY", "1");
  218. }
  219. // Try to identify the source file language from the extension.
  220. cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();
  221. if(const char* l = gg->GetLanguageFromExtension(this->Extension.c_str()))
  222. {
  223. this->Language = l;
  224. }
  225. }
  226. //----------------------------------------------------------------------------
  227. bool cmSourceFile::Matches(cmSourceFileLocation const& loc)
  228. {
  229. return this->Location.Matches(loc);
  230. }
  231. //----------------------------------------------------------------------------
  232. void cmSourceFile::SetProperty(const char* prop, const char* value)
  233. {
  234. if (!prop)
  235. {
  236. return;
  237. }
  238. if (!value)
  239. {
  240. value = "NOTFOUND";
  241. }
  242. this->Properties.SetProperty(prop, value, cmProperty::SOURCE_FILE);
  243. }
  244. //----------------------------------------------------------------------------
  245. void cmSourceFile::AppendProperty(const char* prop, const char* value)
  246. {
  247. if (!prop)
  248. {
  249. return;
  250. }
  251. this->Properties.AppendProperty(prop, value, cmProperty::SOURCE_FILE);
  252. }
  253. //----------------------------------------------------------------------------
  254. const char* cmSourceFile::GetPropertyForUser(const char *prop)
  255. {
  256. // This method is a consequence of design history and backwards
  257. // compatibility. GetProperty is (and should be) a const method.
  258. // Computed properties should not be stored back in the property map
  259. // but instead reference information already known. If they need to
  260. // cache information in a mutable ivar to provide the return string
  261. // safely then so be it.
  262. //
  263. // The LOCATION property is particularly problematic. The CMake
  264. // language has very loose restrictions on the names that will match
  265. // a given source file (for historical reasons). Implementing
  266. // lookups correctly with such loose naming requires the
  267. // cmSourceFileLocation class to commit to a particular full path to
  268. // the source file as late as possible. If the users requests the
  269. // LOCATION property we must commit now.
  270. if(strcmp(prop, "LOCATION") == 0)
  271. {
  272. // Commit to a location.
  273. this->GetFullPath();
  274. }
  275. // Perform the normal property lookup.
  276. return this->GetProperty(prop);
  277. }
  278. //----------------------------------------------------------------------------
  279. const char* cmSourceFile::GetProperty(const char* prop) const
  280. {
  281. // Check for computed properties.
  282. if(strcmp(prop, "LOCATION") == 0)
  283. {
  284. if(this->FullPath.empty())
  285. {
  286. return 0;
  287. }
  288. else
  289. {
  290. return this->FullPath.c_str();
  291. }
  292. }
  293. bool chain = false;
  294. const char *retVal =
  295. this->Properties.GetPropertyValue(prop, cmProperty::SOURCE_FILE, chain);
  296. if (chain)
  297. {
  298. cmMakefile* mf = this->Location.GetMakefile();
  299. return mf->GetProperty(prop,cmProperty::SOURCE_FILE);
  300. }
  301. return retVal;
  302. }
  303. //----------------------------------------------------------------------------
  304. bool cmSourceFile::GetPropertyAsBool(const char* prop) const
  305. {
  306. return cmSystemTools::IsOn(this->GetProperty(prop));
  307. }
  308. //----------------------------------------------------------------------------
  309. cmCustomCommand* cmSourceFile::GetCustomCommand()
  310. {
  311. return this->CustomCommand;
  312. }
  313. //----------------------------------------------------------------------------
  314. cmCustomCommand const* cmSourceFile::GetCustomCommand() const
  315. {
  316. return this->CustomCommand;
  317. }
  318. //----------------------------------------------------------------------------
  319. void cmSourceFile::SetCustomCommand(cmCustomCommand* cc)
  320. {
  321. cmCustomCommand* old = this->CustomCommand;
  322. this->CustomCommand = cc;
  323. delete old;
  324. }
  325. //----------------------------------------------------------------------------
  326. void cmSourceFile::DefineProperties(cmake *cm)
  327. {
  328. // define properties
  329. cm->DefineProperty
  330. ("ABSTRACT", cmProperty::SOURCE_FILE,
  331. "Is this source file an abstract class.",
  332. "A property on a source file that indicates if the source file "
  333. "represents a class that is abstract. This only makes sense for "
  334. "languages that have a notion of an abstract class and it is "
  335. "only used by some tools that wrap classes into other languages.");
  336. cm->DefineProperty
  337. ("COMPILE_FLAGS", cmProperty::SOURCE_FILE,
  338. "Additional flags to be added when compiling this source file.",
  339. "These flags will be added to the list of compile flags when "
  340. "this source file builds. Use COMPILE_DEFINITIONS to pass additional "
  341. "preprocessor definitions.");
  342. cm->DefineProperty
  343. ("COMPILE_DEFINITIONS", cmProperty::SOURCE_FILE,
  344. "Preprocessor definitions for compiling a source file.",
  345. "The COMPILE_DEFINITIONS property may be set to a list of preprocessor "
  346. "definitions using the syntax VAR or VAR=value. Function-style "
  347. "definitions are not supported. CMake will automatically escape "
  348. "the value correctly for the native build system (note that CMake "
  349. "language syntax may require escapes to specify some values). "
  350. "This property may be set on a per-configuration basis using the name "
  351. "COMPILE_DEFINITIONS_<CONFIG> where <CONFIG> is an upper-case name "
  352. "(ex. \"COMPILE_DEFINITIONS_DEBUG\").\n"
  353. "CMake will automatically drop some definitions that "
  354. "are not supported by the native build tool. "
  355. "The VS6 IDE does not support definitions with values "
  356. "(but NMake does). Xcode does not support per-configuration "
  357. "definitions on source files.\n"
  358. "Dislaimer: Most native build tools have poor support for escaping "
  359. "certain values. CMake has work-arounds for many cases but some "
  360. "values may just not be possible to pass correctly. If a value "
  361. "does not seem to be escaped correctly, do not attempt to "
  362. "work-around the problem by adding escape sequences to the value. "
  363. "Your work-around may break in a future version of CMake that "
  364. "has improved escape support. Instead consider defining the macro "
  365. "in a (configured) header file. Then report the limitation.");
  366. cm->DefineProperty
  367. ("COMPILE_DEFINITIONS_<CONFIG>", cmProperty::SOURCE_FILE,
  368. "Per-configuration preprocessor definitions on a source file.",
  369. "This is the configuration-specific version of "
  370. "COMPILE_DEFINITIONS. Note that Xcode does not support "
  371. "per-configuration source file flags so this property will "
  372. "be ignored by the Xcode generator.");
  373. cm->DefineProperty
  374. ("EXTERNAL_OBJECT", cmProperty::SOURCE_FILE,
  375. "If set to true then this is an object file.",
  376. "If this property is set to true then the source file "
  377. "is really an object file and should not be compiled. "
  378. "It will still be linked into the target though.");
  379. cm->DefineProperty
  380. ("GENERATED", cmProperty::SOURCE_FILE,
  381. "Is this source file generated as part of the build process.",
  382. "If a source file is generated by the build process CMake will "
  383. "handle it differently in temrs of dependency checking etc. "
  384. "Otherwise having a non-existent source file could create problems.");
  385. cm->DefineProperty
  386. ("HEADER_FILE_ONLY", cmProperty::SOURCE_FILE,
  387. "Is this source file only a header file.",
  388. "A property on a source file that indicates if the source file "
  389. "is a header file with no associated implementation. This is "
  390. "set automatically based on the file extension and is used by "
  391. "CMake to determine is certain dependency information should be "
  392. "computed.");
  393. cm->DefineProperty
  394. ("KEEP_EXTENSION", cmProperty::SOURCE_FILE,
  395. "Make the output file have the same extension as the source file.",
  396. "If this property is set then the file extension of the output "
  397. "file will be the same as that of the source file. Normally "
  398. "the output file extension is computed based on the language "
  399. "of the source file, for example .cxx will go to a .o extension.");
  400. cm->DefineProperty
  401. ("LANGUAGE", cmProperty::SOURCE_FILE,
  402. "What programming language is the file.",
  403. "A property that can be set to indicate what programming language "
  404. "the source file is. If it is not set the language is determined "
  405. "based on the file extension. Typical values are CXX C etc.");
  406. cm->DefineProperty
  407. ("LOCATION", cmProperty::SOURCE_FILE,
  408. "The full path to a source file.",
  409. "A read only property on a SOURCE FILE that contains the full path "
  410. "to the source file.");
  411. cm->DefineProperty
  412. ("MACOSX_PACKAGE_LOCATION", cmProperty::SOURCE_FILE,
  413. "Place a source file inside a Mac OS X bundle or framework.",
  414. "Executable targets with the MACOSX_BUNDLE property set are built "
  415. "as Mac OS X application bundles on Apple platforms. "
  416. "Shared library targets with the FRAMEWORK property set are built "
  417. "as Mac OS X frameworks on Apple platforms. "
  418. "Source files listed in the target with this property set will "
  419. "be copied to a directory inside the bundle or framework content "
  420. "folder specified by the property value. "
  421. "For bundles the content folder is \"<name>.app/Contents\". "
  422. "For frameworks the content folder is "
  423. "\"<name>.framework/Versions/<version>\". "
  424. "See the PUBLIC_HEADER, PRIVATE_HEADER, and RESOURCE target "
  425. "properties for specifying files meant for Headers, PrivateHeadres, "
  426. "or Resources directories.");
  427. cm->DefineProperty
  428. ("OBJECT_DEPENDS", cmProperty::SOURCE_FILE,
  429. "Additional dependencies.",
  430. "Additional dependencies that should be checked as part of "
  431. "building this source file.");
  432. cm->DefineProperty
  433. ("OBJECT_OUTPUTS", cmProperty::SOURCE_FILE,
  434. "Additional outputs for a Makefile rule.",
  435. "Additional outputs created by compilation of this source file. "
  436. "If any of these outputs is missing the object will be recompiled. "
  437. "This is supported only on Makefile generators and will be ignored "
  438. "on other generators.");
  439. cm->DefineProperty
  440. ("SYMBOLIC", cmProperty::SOURCE_FILE,
  441. "Is this just a name for a rule.",
  442. "If SYMBOLIC (boolean) is set to true the build system will be "
  443. "informed that the source file is not actually created on disk but "
  444. "instead used as a symbolic name for a build rule.");
  445. cm->DefineProperty
  446. ("WRAP_EXCLUDE", cmProperty::SOURCE_FILE,
  447. "Exclude this source file from any code wrapping techniques.",
  448. "Some packages can wrap source files into alternate languages "
  449. "to provide additional functionality. For example, C++ code "
  450. "can be wrapped into Java or Python etc using SWIG etc. "
  451. "If WRAP_EXCLUDE is set to true (1 etc) that indicates then "
  452. "this source file should not be wrapped.");
  453. }