cmSourceFile.cxx 19 KB

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