cmSourceFile.cxx 19 KB

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