cmSourceFile.cxx 19 KB

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