cmSourceFile.cxx 19 KB

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