cmSourceFile.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. // If the user provided a full path, trust it. If the file is not
  151. // there the native tool will complain at build time.
  152. if(!this->Location.DirectoryIsAmbiguous())
  153. {
  154. this->FullPath = this->Location.GetDirectory();
  155. this->FullPath += "/";
  156. this->FullPath += this->Location.GetName();
  157. return true;
  158. }
  159. cmOStringStream e;
  160. e << "Cannot find source file \"" << this->Location.GetName() << "\"";
  161. e << "\n\nTried extensions";
  162. for(std::vector<std::string>::const_iterator ext = srcExts.begin();
  163. ext != srcExts.end(); ++ext)
  164. {
  165. e << " ." << *ext;
  166. }
  167. for(std::vector<std::string>::const_iterator ext = hdrExts.begin();
  168. ext != hdrExts.end(); ++ext)
  169. {
  170. e << " ." << *ext;
  171. }
  172. cmSystemTools::Error(e.str().c_str());
  173. this->FindFullPathFailed = true;
  174. return false;
  175. }
  176. //----------------------------------------------------------------------------
  177. bool cmSourceFile::TryFullPath(const char* tp, const char* ext)
  178. {
  179. std::string tryPath = tp;
  180. if(ext && *ext)
  181. {
  182. tryPath += ".";
  183. tryPath += ext;
  184. }
  185. if(cmSystemTools::FileExists(tryPath.c_str()))
  186. {
  187. this->FullPath = tryPath;
  188. return true;
  189. }
  190. return false;
  191. }
  192. //----------------------------------------------------------------------------
  193. void cmSourceFile::CheckExtension()
  194. {
  195. // Compute the extension.
  196. std::string realExt =
  197. cmSystemTools::GetFilenameLastExtension(this->FullPath);
  198. if(!realExt.empty())
  199. {
  200. // Store the extension without the leading '.'.
  201. this->Extension = realExt.substr(1);
  202. }
  203. // Look for object files.
  204. if(this->Extension == "obj" ||
  205. this->Extension == "o" ||
  206. this->Extension == "lo")
  207. {
  208. this->SetProperty("EXTERNAL_OBJECT", "1");
  209. }
  210. // Look for header files.
  211. cmMakefile* mf = this->Location.GetMakefile();
  212. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  213. if(std::find(hdrExts.begin(), hdrExts.end(), this->Extension) ==
  214. hdrExts.end())
  215. {
  216. // This is not a known header file extension. Mark it as not a
  217. // header unless the user has already explicitly set the property.
  218. if(!this->GetProperty("HEADER_FILE_ONLY"))
  219. {
  220. this->SetProperty("HEADER_FILE_ONLY", "0");
  221. }
  222. }
  223. else
  224. {
  225. // This is a known header file extension. The source cannot be compiled.
  226. this->SetProperty("HEADER_FILE_ONLY", "1");
  227. }
  228. // Try to identify the source file language from the extension.
  229. cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();
  230. if(const char* l = gg->GetLanguageFromExtension(this->Extension.c_str()))
  231. {
  232. this->Language = l;
  233. }
  234. }
  235. //----------------------------------------------------------------------------
  236. bool cmSourceFile::Matches(cmSourceFileLocation const& loc)
  237. {
  238. return this->Location.Matches(loc);
  239. }
  240. //----------------------------------------------------------------------------
  241. void cmSourceFile::SetProperty(const char* prop, const char* value)
  242. {
  243. if (!prop)
  244. {
  245. return;
  246. }
  247. this->Properties.SetProperty(prop, value, cmProperty::SOURCE_FILE);
  248. }
  249. //----------------------------------------------------------------------------
  250. void cmSourceFile::AppendProperty(const char* prop, const char* value)
  251. {
  252. if (!prop)
  253. {
  254. return;
  255. }
  256. this->Properties.AppendProperty(prop, value, cmProperty::SOURCE_FILE);
  257. }
  258. //----------------------------------------------------------------------------
  259. const char* cmSourceFile::GetPropertyForUser(const char *prop)
  260. {
  261. // This method is a consequence of design history and backwards
  262. // compatibility. GetProperty is (and should be) a const method.
  263. // Computed properties should not be stored back in the property map
  264. // but instead reference information already known. If they need to
  265. // cache information in a mutable ivar to provide the return string
  266. // safely then so be it.
  267. //
  268. // The LOCATION property is particularly problematic. The CMake
  269. // language has very loose restrictions on the names that will match
  270. // a given source file (for historical reasons). Implementing
  271. // lookups correctly with such loose naming requires the
  272. // cmSourceFileLocation class to commit to a particular full path to
  273. // the source file as late as possible. If the users requests the
  274. // LOCATION property we must commit now.
  275. if(strcmp(prop, "LOCATION") == 0)
  276. {
  277. // Commit to a location.
  278. this->GetFullPath();
  279. }
  280. // Perform the normal property lookup.
  281. return this->GetProperty(prop);
  282. }
  283. //----------------------------------------------------------------------------
  284. const char* cmSourceFile::GetProperty(const char* prop) const
  285. {
  286. // Check for computed properties.
  287. if(strcmp(prop, "LOCATION") == 0)
  288. {
  289. if(this->FullPath.empty())
  290. {
  291. return 0;
  292. }
  293. else
  294. {
  295. return this->FullPath.c_str();
  296. }
  297. }
  298. bool chain = false;
  299. const char *retVal =
  300. this->Properties.GetPropertyValue(prop, cmProperty::SOURCE_FILE, chain);
  301. if (chain)
  302. {
  303. cmMakefile* mf = this->Location.GetMakefile();
  304. return mf->GetProperty(prop,cmProperty::SOURCE_FILE);
  305. }
  306. return retVal;
  307. }
  308. //----------------------------------------------------------------------------
  309. bool cmSourceFile::GetPropertyAsBool(const char* prop) const
  310. {
  311. return cmSystemTools::IsOn(this->GetProperty(prop));
  312. }
  313. //----------------------------------------------------------------------------
  314. cmCustomCommand* cmSourceFile::GetCustomCommand()
  315. {
  316. return this->CustomCommand;
  317. }
  318. //----------------------------------------------------------------------------
  319. cmCustomCommand const* cmSourceFile::GetCustomCommand() const
  320. {
  321. return this->CustomCommand;
  322. }
  323. //----------------------------------------------------------------------------
  324. void cmSourceFile::SetCustomCommand(cmCustomCommand* cc)
  325. {
  326. cmCustomCommand* old = this->CustomCommand;
  327. this->CustomCommand = cc;
  328. delete old;
  329. }
  330. //----------------------------------------------------------------------------
  331. void cmSourceFile::DefineProperties(cmake *cm)
  332. {
  333. // define properties
  334. cm->DefineProperty
  335. ("ABSTRACT", cmProperty::SOURCE_FILE,
  336. "Is this source file an abstract class.",
  337. "A property on a source file that indicates if the source file "
  338. "represents a class that is abstract. This only makes sense for "
  339. "languages that have a notion of an abstract class and it is "
  340. "only used by some tools that wrap classes into other languages.");
  341. cm->DefineProperty
  342. ("COMPILE_FLAGS", cmProperty::SOURCE_FILE,
  343. "Additional flags to be added when compiling this source file.",
  344. "These flags will be added to the list of compile flags when "
  345. "this source file builds. Use COMPILE_DEFINITIONS to pass additional "
  346. "preprocessor definitions.");
  347. cm->DefineProperty
  348. ("COMPILE_DEFINITIONS", cmProperty::SOURCE_FILE,
  349. "Preprocessor definitions for compiling a source file.",
  350. "The COMPILE_DEFINITIONS property may be set to a list of preprocessor "
  351. "definitions using the syntax VAR or VAR=value. Function-style "
  352. "definitions are not supported. CMake will automatically escape "
  353. "the value correctly for the native build system (note that CMake "
  354. "language syntax may require escapes to specify some values). "
  355. "This property may be set on a per-configuration basis using the name "
  356. "COMPILE_DEFINITIONS_<CONFIG> where <CONFIG> is an upper-case name "
  357. "(ex. \"COMPILE_DEFINITIONS_DEBUG\").\n"
  358. "CMake will automatically drop some definitions that "
  359. "are not supported by the native build tool. "
  360. "The VS6 IDE does not support definitions with values "
  361. "(but NMake does). Xcode does not support per-configuration "
  362. "definitions on source files.\n"
  363. "Dislaimer: Most native build tools have poor support for escaping "
  364. "certain values. CMake has work-arounds for many cases but some "
  365. "values may just not be possible to pass correctly. If a value "
  366. "does not seem to be escaped correctly, do not attempt to "
  367. "work-around the problem by adding escape sequences to the value. "
  368. "Your work-around may break in a future version of CMake that "
  369. "has improved escape support. Instead consider defining the macro "
  370. "in a (configured) header file. Then report the limitation.");
  371. cm->DefineProperty
  372. ("COMPILE_DEFINITIONS_<CONFIG>", cmProperty::SOURCE_FILE,
  373. "Per-configuration preprocessor definitions on a source file.",
  374. "This is the configuration-specific version of "
  375. "COMPILE_DEFINITIONS. Note that Xcode does not support "
  376. "per-configuration source file flags so this property will "
  377. "be ignored by the Xcode generator.");
  378. cm->DefineProperty
  379. ("EXTERNAL_OBJECT", cmProperty::SOURCE_FILE,
  380. "If set to true then this is an object file.",
  381. "If this property is set to true then the source file "
  382. "is really an object file and should not be compiled. "
  383. "It will still be linked into the target though.");
  384. cm->DefineProperty
  385. ("GENERATED", cmProperty::SOURCE_FILE,
  386. "Is this source file generated as part of the build process.",
  387. "If a source file is generated by the build process CMake will "
  388. "handle it differently in temrs of dependency checking etc. "
  389. "Otherwise having a non-existent source file could create problems.");
  390. cm->DefineProperty
  391. ("HEADER_FILE_ONLY", cmProperty::SOURCE_FILE,
  392. "Is this source file only a header file.",
  393. "A property on a source file that indicates if the source file "
  394. "is a header file with no associated implementation. This is "
  395. "set automatically based on the file extension and is used by "
  396. "CMake to determine is certain dependency information should be "
  397. "computed.");
  398. cm->DefineProperty
  399. ("KEEP_EXTENSION", cmProperty::SOURCE_FILE,
  400. "Make the output file have the same extension as the source file.",
  401. "If this property is set then the file extension of the output "
  402. "file will be the same as that of the source file. Normally "
  403. "the output file extension is computed based on the language "
  404. "of the source file, for example .cxx will go to a .o extension.");
  405. cm->DefineProperty
  406. ("LANGUAGE", cmProperty::SOURCE_FILE,
  407. "What programming language is the file.",
  408. "A property that can be set to indicate what programming language "
  409. "the source file is. If it is not set the language is determined "
  410. "based on the file extension. Typical values are CXX C etc.");
  411. cm->DefineProperty
  412. ("LOCATION", cmProperty::SOURCE_FILE,
  413. "The full path to a source file.",
  414. "A read only property on a SOURCE FILE that contains the full path "
  415. "to the source file.");
  416. cm->DefineProperty
  417. ("MACOSX_PACKAGE_LOCATION", cmProperty::SOURCE_FILE,
  418. "Place a source file inside a Mac OS X bundle or framework.",
  419. "Executable targets with the MACOSX_BUNDLE property set are built "
  420. "as Mac OS X application bundles on Apple platforms. "
  421. "Shared library targets with the FRAMEWORK property set are built "
  422. "as Mac OS X frameworks on Apple platforms. "
  423. "Source files listed in the target with this property set will "
  424. "be copied to a directory inside the bundle or framework content "
  425. "folder specified by the property value. "
  426. "For bundles the content folder is \"<name>.app/Contents\". "
  427. "For frameworks the content folder is "
  428. "\"<name>.framework/Versions/<version>\". "
  429. "See the PUBLIC_HEADER, PRIVATE_HEADER, and RESOURCE target "
  430. "properties for specifying files meant for Headers, PrivateHeadres, "
  431. "or Resources directories.");
  432. cm->DefineProperty
  433. ("OBJECT_DEPENDS", cmProperty::SOURCE_FILE,
  434. "Additional dependencies.",
  435. "Additional dependencies that should be checked as part of "
  436. "building this source file.");
  437. cm->DefineProperty
  438. ("OBJECT_OUTPUTS", cmProperty::SOURCE_FILE,
  439. "Additional outputs for a Makefile rule.",
  440. "Additional outputs created by compilation of this source file. "
  441. "If any of these outputs is missing the object will be recompiled. "
  442. "This is supported only on Makefile generators and will be ignored "
  443. "on other generators.");
  444. cm->DefineProperty
  445. ("SYMBOLIC", cmProperty::SOURCE_FILE,
  446. "Is this just a name for a rule.",
  447. "If SYMBOLIC (boolean) is set to true the build system will be "
  448. "informed that the source file is not actually created on disk but "
  449. "instead used as a symbolic name for a build rule.");
  450. cm->DefineProperty
  451. ("WRAP_EXCLUDE", cmProperty::SOURCE_FILE,
  452. "Exclude this source file from any code wrapping techniques.",
  453. "Some packages can wrap source files into alternate languages "
  454. "to provide additional functionality. For example, C++ code "
  455. "can be wrapped into Java or Python etc using SWIG etc. "
  456. "If WRAP_EXCLUDE is set to true (1 etc) that indicates then "
  457. "this source file should not be wrapped.");
  458. }