cmMakefileTargetGenerator.cxx 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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 "cmMakefileTargetGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmGlobalUnixMakefileGenerator3.h"
  17. #include "cmLocalUnixMakefileGenerator3.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmTarget.h"
  21. #include "cmake.h"
  22. #include "cmMakefileExecutableTargetGenerator.h"
  23. #include "cmMakefileLibraryTargetGenerator.h"
  24. #include "cmMakefileUtilityTargetGenerator.h"
  25. cmMakefileTargetGenerator::cmMakefileTargetGenerator(cmTarget* target)
  26. {
  27. this->BuildFileStream = 0;
  28. this->InfoFileStream = 0;
  29. this->FlagFileStream = 0;
  30. this->CustomCommandDriver = OnBuild;
  31. this->FortranModuleDirectoryComputed = false;
  32. this->Target = target;
  33. this->Makefile = this->Target->GetMakefile();
  34. this->LocalGenerator =
  35. static_cast<cmLocalUnixMakefileGenerator3*>(
  36. this->Makefile->GetLocalGenerator());
  37. this->GlobalGenerator =
  38. static_cast<cmGlobalUnixMakefileGenerator3*>(
  39. this->LocalGenerator->GetGlobalGenerator());
  40. }
  41. cmMakefileTargetGenerator *
  42. cmMakefileTargetGenerator::New(cmTarget *tgt)
  43. {
  44. cmMakefileTargetGenerator *result = 0;
  45. switch (tgt->GetType())
  46. {
  47. case cmTarget::EXECUTABLE:
  48. result = new cmMakefileExecutableTargetGenerator(tgt);
  49. break;
  50. case cmTarget::STATIC_LIBRARY:
  51. case cmTarget::SHARED_LIBRARY:
  52. case cmTarget::MODULE_LIBRARY:
  53. result = new cmMakefileLibraryTargetGenerator(tgt);
  54. break;
  55. case cmTarget::UTILITY:
  56. result = new cmMakefileUtilityTargetGenerator(tgt);
  57. break;
  58. default:
  59. return result;
  60. // break; /* unreachable */
  61. }
  62. return result;
  63. }
  64. //----------------------------------------------------------------------------
  65. void cmMakefileTargetGenerator::CreateRuleFile()
  66. {
  67. // Create a directory for this target.
  68. this->TargetBuildDirectory =
  69. this->LocalGenerator->GetTargetDirectory(*this->Target);
  70. this->TargetBuildDirectoryFull =
  71. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  72. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  73. // Construct the rule file name.
  74. this->BuildFileName = this->TargetBuildDirectory;
  75. this->BuildFileName += "/build.make";
  76. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  77. this->BuildFileNameFull += "/build.make";
  78. // Construct the rule file name.
  79. this->ProgressFileName = this->TargetBuildDirectory;
  80. this->ProgressFileName += "/progress.make";
  81. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  82. this->ProgressFileNameFull += "/progress.make";
  83. // reset the progress count
  84. this->NumberOfProgressActions = 0;
  85. // Open the rule file. This should be copy-if-different because the
  86. // rules may depend on this file itself.
  87. this->BuildFileStream =
  88. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  89. this->BuildFileStream->SetCopyIfDifferent(true);
  90. if(!this->BuildFileStream)
  91. {
  92. return;
  93. }
  94. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  95. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  96. }
  97. //----------------------------------------------------------------------------
  98. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  99. {
  100. // write the custom commands for this target
  101. // Look for files registered for cleaning in this directory.
  102. if(const char* additional_clean_files =
  103. this->Makefile->GetProperty
  104. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  105. {
  106. cmSystemTools::ExpandListArgument(additional_clean_files,
  107. this->CleanFiles);
  108. }
  109. // add custom commands to the clean rules?
  110. const char* clean_no_custom =
  111. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  112. bool clean = cmSystemTools::IsOff(clean_no_custom);
  113. // First generate the object rule files. Save a list of all object
  114. // files for this target.
  115. const std::vector<cmSourceFile*>& sources = this->Target->GetSourceFiles();
  116. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  117. source != sources.end(); ++source)
  118. {
  119. cmTarget::SourceFileFlags tsFlags =
  120. this->Target->GetTargetSourceFileFlags(*source);
  121. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  122. {
  123. this->GenerateCustomRuleFile(*cc);
  124. if (clean)
  125. {
  126. const std::vector<std::string>& outputs = cc->GetOutputs();
  127. for(std::vector<std::string>::const_iterator o = outputs.begin();
  128. o != outputs.end(); ++o)
  129. {
  130. this->CleanFiles.push_back
  131. (this->Convert(o->c_str(),
  132. cmLocalGenerator::START_OUTPUT,
  133. cmLocalGenerator::UNCHANGED));
  134. }
  135. }
  136. }
  137. else if(tsFlags.Type != cmTarget::SourceFileTypeNormal)
  138. {
  139. this->WriteMacOSXContentRules(*(*source), tsFlags.MacFolder);
  140. }
  141. else if(!(*source)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  142. {
  143. if(!this->GlobalGenerator->IgnoreFile
  144. ((*source)->GetExtension().c_str()))
  145. {
  146. // Generate this object file's rule file.
  147. this->WriteObjectRuleFiles(*(*source));
  148. }
  149. else if((*source)->GetPropertyAsBool("EXTERNAL_OBJECT"))
  150. {
  151. // This is an external object file. Just add it.
  152. this->ExternalObjects.push_back((*source)->GetFullPath());
  153. }
  154. else
  155. {
  156. // We only get here if a source file is not an external object
  157. // and has an extension that is listed as an ignored file type
  158. // for this language. No message or diagnosis should be
  159. // given.
  160. }
  161. }
  162. }
  163. }
  164. //----------------------------------------------------------------------------
  165. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  166. {
  167. // Include the dependencies for the target.
  168. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  169. dependFileNameFull += "/depend.make";
  170. *this->BuildFileStream
  171. << "# Include any dependencies generated for this target.\n"
  172. << this->LocalGenerator->IncludeDirective << " "
  173. << this->Convert(dependFileNameFull.c_str(),
  174. cmLocalGenerator::HOME_OUTPUT,
  175. cmLocalGenerator::MAKEFILE)
  176. << "\n\n";
  177. // Include the progress variables for the target.
  178. *this->BuildFileStream
  179. << "# Include the progress variables for this target.\n"
  180. << this->LocalGenerator->IncludeDirective << " "
  181. << this->Convert(this->ProgressFileNameFull.c_str(),
  182. cmLocalGenerator::HOME_OUTPUT,
  183. cmLocalGenerator::MAKEFILE)
  184. << "\n\n";
  185. // make sure the depend file exists
  186. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  187. {
  188. // Write an empty dependency file.
  189. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  190. depFileStream
  191. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  192. << "# This may be replaced when dependencies are built." << std::endl;
  193. }
  194. // Open the flags file. This should be copy-if-different because the
  195. // rules may depend on this file itself.
  196. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  197. this->FlagFileNameFull += "/flags.make";
  198. this->FlagFileStream =
  199. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  200. this->FlagFileStream->SetCopyIfDifferent(true);
  201. if(!this->FlagFileStream)
  202. {
  203. return;
  204. }
  205. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  206. // Include the flags for the target.
  207. *this->BuildFileStream
  208. << "# Include the compile flags for this target's objects.\n"
  209. << this->LocalGenerator->IncludeDirective << " "
  210. << this->Convert(this->FlagFileNameFull.c_str(),
  211. cmLocalGenerator::HOME_OUTPUT,
  212. cmLocalGenerator::MAKEFILE)
  213. << "\n\n";
  214. }
  215. //----------------------------------------------------------------------------
  216. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  217. {
  218. // write language flags for target
  219. std::set<cmStdString> languages;
  220. this->Target->GetLanguages(languages);
  221. // put the compiler in the rules.make file so that if it changes
  222. // things rebuild
  223. for(std::set<cmStdString>::const_iterator l = languages.begin();
  224. l != languages.end(); ++l)
  225. {
  226. cmStdString compiler = "CMAKE_";
  227. compiler += *l;
  228. compiler += "_COMPILER";
  229. *this->FlagFileStream << "# compile " << l->c_str() << " with " <<
  230. this->Makefile->GetSafeDefinition(compiler.c_str()) << "\n";
  231. }
  232. for(std::set<cmStdString>::const_iterator l = languages.begin();
  233. l != languages.end(); ++l)
  234. {
  235. const char *lang = l->c_str();
  236. std::string flags;
  237. std::string defines;
  238. bool shared = ((this->Target->GetType() == cmTarget::SHARED_LIBRARY) ||
  239. (this->Target->GetType() == cmTarget::MODULE_LIBRARY));
  240. // Add the export symbol definition for shared library objects.
  241. if(const char* exportMacro = this->Target->GetExportMacro())
  242. {
  243. flags += "-D";
  244. flags += exportMacro;
  245. }
  246. // Add preprocessor definitions for this target and configuration.
  247. this->LocalGenerator->AppendDefines
  248. (defines, this->Makefile->GetProperty("COMPILE_DEFINITIONS"), lang);
  249. this->LocalGenerator->AppendDefines
  250. (defines, this->Target->GetProperty("COMPILE_DEFINITIONS"), lang);
  251. std::string defPropName = "COMPILE_DEFINITIONS_";
  252. defPropName +=
  253. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  254. this->LocalGenerator->AppendDefines
  255. (defines, this->Makefile->GetProperty(defPropName.c_str()), lang);
  256. this->LocalGenerator->AppendDefines
  257. (defines, this->Target->GetProperty(defPropName.c_str()), lang);
  258. // Add language-specific flags.
  259. this->LocalGenerator
  260. ->AddLanguageFlags(flags, lang,
  261. this->LocalGenerator->ConfigurationName.c_str());
  262. // Fortran-specific flags computed for this target.
  263. if(*l == "Fortran")
  264. {
  265. this->AddFortranFlags(flags);
  266. }
  267. // Add shared-library flags if needed.
  268. this->LocalGenerator->AddSharedFlags(flags, lang, shared);
  269. // Add include directory flags.
  270. this->LocalGenerator->
  271. AppendFlags(flags, this->LocalGenerator->GetIncludeFlags(lang));
  272. // Add include directory flags.
  273. this->LocalGenerator->
  274. AppendFlags(flags,this->GetFrameworkFlags().c_str());
  275. *this->FlagFileStream << lang << "_FLAGS = " << flags << "\n\n";
  276. *this->FlagFileStream << lang << "_DEFINES = " << defines << "\n\n";
  277. }
  278. // Add target-specific flags.
  279. if(this->Target->GetProperty("COMPILE_FLAGS"))
  280. {
  281. std::string flags;
  282. this->LocalGenerator->AppendFlags
  283. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  284. *this->FlagFileStream << "# TARGET_FLAGS = " << flags << "\n\n";
  285. }
  286. }
  287. //----------------------------------------------------------------------------
  288. void cmMakefileTargetGenerator::WriteMacOSXContentRules(cmSourceFile& source,
  289. const char* pkgloc)
  290. {
  291. // Skip OS X content when not building a Framework or Bundle.
  292. if(this->MacContentDirectory.empty())
  293. {
  294. return;
  295. }
  296. // Construct the full path to the content subdirectory.
  297. std::string macdir = this->MacContentDirectory;
  298. macdir += pkgloc;
  299. cmSystemTools::MakeDirectory(macdir.c_str());
  300. // Record use of this content location. Only the first level
  301. // directory is needed.
  302. {
  303. std::string loc = pkgloc;
  304. loc = loc.substr(0, loc.find('/'));
  305. this->MacContentFolders.insert(loc);
  306. }
  307. // Get the input file location.
  308. std::string input = source.GetFullPath();
  309. // Get the output file location.
  310. std::string output = macdir;
  311. output += "/";
  312. output += cmSystemTools::GetFilenameName(input);
  313. this->CleanFiles.push_back(this->Convert(output.c_str(),
  314. cmLocalGenerator::START_OUTPUT));
  315. output = this->Convert(output.c_str(), cmLocalGenerator::HOME_OUTPUT);
  316. // Create a rule to copy the content into the bundle.
  317. std::vector<std::string> depends;
  318. std::vector<std::string> commands;
  319. depends.push_back(input);
  320. std::string copyEcho = "Copying OS X content ";
  321. copyEcho += output;
  322. this->LocalGenerator->AppendEcho(commands, copyEcho.c_str(),
  323. cmLocalUnixMakefileGenerator3::EchoBuild);
  324. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  325. copyCommand += this->Convert(input.c_str(),
  326. cmLocalGenerator::NONE,
  327. cmLocalGenerator::SHELL);
  328. copyCommand += " ";
  329. copyCommand += this->Convert(output.c_str(),
  330. cmLocalGenerator::NONE,
  331. cmLocalGenerator::SHELL);
  332. commands.push_back(copyCommand);
  333. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  334. output.c_str(),
  335. depends, commands, false);
  336. this->ExtraFiles.insert(output);
  337. }
  338. //----------------------------------------------------------------------------
  339. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  340. {
  341. // Identify the language of the source file.
  342. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  343. if(!lang)
  344. {
  345. // don't know anything about this file so skip it
  346. return;
  347. }
  348. // Get the full path name of the object file.
  349. bool hasSourceExtension;
  350. std::string objNoTargetDir;
  351. std::string obj =
  352. this->LocalGenerator->GetObjectFileName(*this->Target, source,
  353. &objNoTargetDir,
  354. &hasSourceExtension);
  355. // Avoid generating duplicate rules.
  356. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  357. {
  358. this->ObjectFiles.insert(obj);
  359. }
  360. else
  361. {
  362. cmOStringStream err;
  363. err << "Warning: Source file \""
  364. << source.GetFullPath()
  365. << "\" is listed multiple times for target \""
  366. << this->Target->GetName()
  367. << "\".";
  368. cmSystemTools::Message(err.str().c_str(), "Warning");
  369. return;
  370. }
  371. // Create the directory containing the object file. This may be a
  372. // subdirectory under the target's directory.
  373. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  374. cmSystemTools::MakeDirectory
  375. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  376. // Save this in the target's list of object files.
  377. this->Objects.push_back(obj);
  378. this->CleanFiles.push_back(obj);
  379. // TODO: Remove
  380. //std::string relativeObj
  381. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  382. //relativeObj += obj;
  383. // we compute some depends when writing the depend.make that we will also
  384. // use in the build.make, same with depMakeFile
  385. std::vector<std::string> depends;
  386. std::string depMakeFile;
  387. // generate the build rule file
  388. this->WriteObjectBuildFile(obj, lang, source, depends);
  389. // The object file should be checked for dependency integrity.
  390. std::string objFullPath = this->Makefile->GetCurrentOutputDirectory();
  391. objFullPath += "/";
  392. objFullPath += obj;
  393. objFullPath =
  394. this->Convert(objFullPath.c_str(), cmLocalGenerator::FULL);
  395. std::string srcFullPath =
  396. this->Convert(source.GetFullPath().c_str(), cmLocalGenerator::FULL);
  397. this->LocalGenerator->
  398. AddImplicitDepends(*this->Target, lang,
  399. objFullPath.c_str(),
  400. srcFullPath.c_str());
  401. // add this to the list of objects for this local generator
  402. if(cmSystemTools::FileIsFullPath(objNoTargetDir.c_str()))
  403. {
  404. objNoTargetDir = cmSystemTools::GetFilenameName(objNoTargetDir);
  405. }
  406. cmLocalUnixMakefileGenerator3::LocalObjectInfo& info =
  407. this->LocalGenerator->LocalObjectFiles[objNoTargetDir];
  408. info.HasSourceExtension = hasSourceExtension;
  409. info.push_back(
  410. cmLocalUnixMakefileGenerator3::LocalObjectEntry(this->Target, lang)
  411. );
  412. }
  413. //----------------------------------------------------------------------------
  414. void
  415. cmMakefileTargetGenerator
  416. ::WriteObjectBuildFile(std::string &obj,
  417. const char *lang,
  418. cmSourceFile& source,
  419. std::vector<std::string>& depends)
  420. {
  421. this->LocalGenerator->AppendRuleDepend(depends,
  422. this->FlagFileNameFull.c_str());
  423. // generate the depend scanning rule
  424. this->WriteObjectDependRules(source, depends);
  425. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  426. relativeObj += obj;
  427. // Write the build rule.
  428. // Build the set of compiler flags.
  429. std::string flags;
  430. // Add language-specific flags.
  431. std::string langFlags = "$(";
  432. langFlags += lang;
  433. langFlags += "_FLAGS)";
  434. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  435. // Add target-specific flags.
  436. if(this->Target->GetProperty("COMPILE_FLAGS"))
  437. {
  438. this->LocalGenerator->AppendFlags
  439. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  440. }
  441. // Add flags from source file properties.
  442. if (source.GetProperty("COMPILE_FLAGS"))
  443. {
  444. this->LocalGenerator->AppendFlags
  445. (flags, source.GetProperty("COMPILE_FLAGS"));
  446. *this->FlagFileStream << "# Custom flags: "
  447. << relativeObj << "_FLAGS = "
  448. << source.GetProperty("COMPILE_FLAGS")
  449. << "\n"
  450. << "\n";
  451. }
  452. // Add language-specific defines.
  453. std::string defines = "$(";
  454. defines += lang;
  455. defines += "_DEFINES)";
  456. // Add source-sepcific preprocessor definitions.
  457. if(const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS"))
  458. {
  459. this->LocalGenerator->AppendDefines(defines, compile_defs, lang);
  460. *this->FlagFileStream << "# Custom defines: "
  461. << relativeObj << "_DEFINES = "
  462. << compile_defs << "\n"
  463. << "\n";
  464. }
  465. std::string configUpper =
  466. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  467. std::string defPropName = "COMPILE_DEFINITIONS_";
  468. defPropName += configUpper;
  469. if(const char* config_compile_defs =
  470. source.GetProperty(defPropName.c_str()))
  471. {
  472. this->LocalGenerator->AppendDefines(defines, config_compile_defs, lang);
  473. *this->FlagFileStream
  474. << "# Custom defines: "
  475. << relativeObj << "_DEFINES_" << configUpper
  476. << " = " << config_compile_defs << "\n"
  477. << "\n";
  478. }
  479. // Get the output paths for source and object files.
  480. std::string sourceFile = source.GetFullPath();
  481. if(this->LocalGenerator->UseRelativePaths)
  482. {
  483. sourceFile = this->Convert(sourceFile.c_str(),
  484. cmLocalGenerator::HOME_OUTPUT);
  485. }
  486. sourceFile = this->Convert(sourceFile.c_str(),
  487. cmLocalGenerator::NONE,
  488. cmLocalGenerator::SHELL);
  489. std::string objectFile = this->Convert(obj.c_str(),
  490. cmLocalGenerator::START_OUTPUT,
  491. cmLocalGenerator::SHELL);
  492. // Construct the build message.
  493. std::vector<std::string> no_commands;
  494. std::vector<std::string> commands;
  495. // add in a progress call if needed
  496. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  497. progressDir += cmake::GetCMakeFilesDirectory();
  498. cmOStringStream progCmd;
  499. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  500. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  501. cmLocalGenerator::FULL,
  502. cmLocalGenerator::SHELL);
  503. this->NumberOfProgressActions++;
  504. progCmd << " $(CMAKE_PROGRESS_"
  505. << this->NumberOfProgressActions
  506. << ")";
  507. commands.push_back(progCmd.str());
  508. std::string buildEcho = "Building ";
  509. buildEcho += lang;
  510. buildEcho += " object ";
  511. buildEcho += relativeObj;
  512. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  513. cmLocalUnixMakefileGenerator3::EchoBuild);
  514. // Construct the compile rules.
  515. std::string compileRuleVar = "CMAKE_";
  516. compileRuleVar += lang;
  517. compileRuleVar += "_COMPILE_OBJECT";
  518. std::string compileRule =
  519. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  520. std::vector<std::string> compileCommands;
  521. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  522. // Change the command working directory to the local build tree.
  523. this->LocalGenerator->CreateCDCommand
  524. (compileCommands,
  525. this->Makefile->GetStartOutputDirectory(),
  526. this->Makefile->GetHomeOutputDirectory());
  527. commands.insert(commands.end(),
  528. compileCommands.begin(), compileCommands.end());
  529. std::string targetOutPathPDB;
  530. {
  531. std::string targetFullPathPDB;
  532. const char* configName = this->LocalGenerator->ConfigurationName.c_str();
  533. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  534. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  535. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  536. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  537. {
  538. targetFullPathPDB = this->Target->GetDirectory();
  539. targetFullPathPDB += "/";
  540. targetFullPathPDB += this->Target->GetPDBName(configName);
  541. }
  542. targetOutPathPDB =
  543. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::FULL,
  544. cmLocalGenerator::SHELL);
  545. }
  546. cmLocalGenerator::RuleVariables vars;
  547. vars.Language = lang;
  548. vars.TargetPDB = targetOutPathPDB.c_str();
  549. vars.Source = sourceFile.c_str();
  550. std::string shellObj =
  551. this->Convert(obj.c_str(),
  552. cmLocalGenerator::NONE,
  553. cmLocalGenerator::SHELL).c_str();
  554. vars.Object = shellObj.c_str();
  555. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  556. objectDir = this->Convert(objectDir.c_str(),
  557. cmLocalGenerator::START_OUTPUT,
  558. cmLocalGenerator::SHELL);
  559. vars.ObjectDir = objectDir.c_str();
  560. vars.Flags = flags.c_str();
  561. vars.Defines = defines.c_str();
  562. // Expand placeholders in the commands.
  563. for(std::vector<std::string>::iterator i = commands.begin();
  564. i != commands.end(); ++i)
  565. {
  566. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  567. }
  568. // Write the rule.
  569. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  570. relativeObj.c_str(),
  571. depends, commands, false);
  572. // Check for extra outputs created by the compilation.
  573. if(const char* extra_outputs_str =
  574. source.GetProperty("OBJECT_OUTPUTS"))
  575. {
  576. std::vector<std::string> extra_outputs;
  577. cmSystemTools::ExpandListArgument(extra_outputs_str, extra_outputs);
  578. for(std::vector<std::string>::const_iterator eoi = extra_outputs.begin();
  579. eoi != extra_outputs.end(); ++eoi)
  580. {
  581. // Register this as an extra output for the object file rule.
  582. // This will cause the object file to be rebuilt if the extra
  583. // output is missing.
  584. this->GenerateExtraOutput(eoi->c_str(), relativeObj.c_str(), false);
  585. // Register this as an extra file to clean.
  586. this->CleanFiles.push_back(eoi->c_str());
  587. }
  588. }
  589. bool lang_is_c_or_cxx = ((strcmp(lang, "C") == 0) ||
  590. (strcmp(lang, "CXX") == 0));
  591. bool do_preprocess_rules = lang_is_c_or_cxx &&
  592. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  593. bool do_assembly_rules = lang_is_c_or_cxx &&
  594. this->LocalGenerator->GetCreateAssemblySourceRules();
  595. if(do_preprocess_rules || do_assembly_rules)
  596. {
  597. std::vector<std::string> force_depends;
  598. force_depends.push_back("cmake_force");
  599. std::string::size_type dot_pos = relativeObj.rfind(".");
  600. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  601. dot_pos = obj.rfind(".");
  602. std::string objBase = obj.substr(0, dot_pos);
  603. if(do_preprocess_rules)
  604. {
  605. commands.clear();
  606. std::string relativeObjI = relativeObjBase + ".i";
  607. std::string objI = objBase + ".i";
  608. std::string preprocessEcho = "Preprocessing ";
  609. preprocessEcho += lang;
  610. preprocessEcho += " source to ";
  611. preprocessEcho += objI;
  612. this->LocalGenerator->AppendEcho(
  613. commands, preprocessEcho.c_str(),
  614. cmLocalUnixMakefileGenerator3::EchoBuild
  615. );
  616. std::string preprocessRuleVar = "CMAKE_";
  617. preprocessRuleVar += lang;
  618. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  619. if(const char* preprocessRule =
  620. this->Makefile->GetDefinition(preprocessRuleVar.c_str()))
  621. {
  622. std::vector<std::string> preprocessCommands;
  623. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  624. this->LocalGenerator->CreateCDCommand
  625. (preprocessCommands,
  626. this->Makefile->GetStartOutputDirectory(),
  627. this->Makefile->GetHomeOutputDirectory());
  628. commands.insert(commands.end(),
  629. preprocessCommands.begin(),
  630. preprocessCommands.end());
  631. std::string shellObjI =
  632. this->Convert(objI.c_str(),
  633. cmLocalGenerator::NONE,
  634. cmLocalGenerator::SHELL).c_str();
  635. vars.PreprocessedSource = shellObjI.c_str();
  636. // Expand placeholders in the commands.
  637. for(std::vector<std::string>::iterator i = commands.begin();
  638. i != commands.end(); ++i)
  639. {
  640. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  641. }
  642. }
  643. else
  644. {
  645. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  646. cmd += preprocessRuleVar;
  647. commands.push_back(cmd);
  648. }
  649. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  650. relativeObjI.c_str(),
  651. force_depends, commands, false);
  652. }
  653. if(do_assembly_rules)
  654. {
  655. commands.clear();
  656. std::string relativeObjS = relativeObjBase + ".s";
  657. std::string objS = objBase + ".s";
  658. std::string assemblyEcho = "Compiling ";
  659. assemblyEcho += lang;
  660. assemblyEcho += " source to assembly ";
  661. assemblyEcho += objS;
  662. this->LocalGenerator->AppendEcho(
  663. commands, assemblyEcho.c_str(),
  664. cmLocalUnixMakefileGenerator3::EchoBuild
  665. );
  666. std::string assemblyRuleVar = "CMAKE_";
  667. assemblyRuleVar += lang;
  668. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  669. if(const char* assemblyRule =
  670. this->Makefile->GetDefinition(assemblyRuleVar.c_str()))
  671. {
  672. std::vector<std::string> assemblyCommands;
  673. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  674. this->LocalGenerator->CreateCDCommand
  675. (assemblyCommands,
  676. this->Makefile->GetStartOutputDirectory(),
  677. this->Makefile->GetHomeOutputDirectory());
  678. commands.insert(commands.end(),
  679. assemblyCommands.begin(),
  680. assemblyCommands.end());
  681. std::string shellObjS =
  682. this->Convert(objS.c_str(),
  683. cmLocalGenerator::NONE,
  684. cmLocalGenerator::SHELL).c_str();
  685. vars.AssemblySource = shellObjS.c_str();
  686. // Expand placeholders in the commands.
  687. for(std::vector<std::string>::iterator i = commands.begin();
  688. i != commands.end(); ++i)
  689. {
  690. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  691. }
  692. }
  693. else
  694. {
  695. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  696. cmd += assemblyRuleVar;
  697. commands.push_back(cmd);
  698. }
  699. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  700. relativeObjS.c_str(),
  701. force_depends, commands, false);
  702. }
  703. }
  704. // If the language needs provides-requires mode, create the
  705. // corresponding targets.
  706. std::string objectRequires = relativeObj;
  707. objectRequires += ".requires";
  708. std::vector<std::string> p_depends;
  709. // always provide an empty requires target
  710. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  711. objectRequires.c_str(), p_depends,
  712. no_commands, true);
  713. // write a build rule to recursively build what this obj provides
  714. std::string objectProvides = relativeObj;
  715. objectProvides += ".provides";
  716. std::string temp = relativeObj;
  717. temp += ".provides.build";
  718. std::vector<std::string> r_commands;
  719. std::string tgtMakefileName =
  720. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  721. tgtMakefileName += "/build.make";
  722. r_commands.push_back
  723. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  724. temp.c_str()));
  725. p_depends.clear();
  726. p_depends.push_back(objectRequires);
  727. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  728. objectProvides.c_str(), p_depends,
  729. r_commands, true);
  730. // write the provides.build rule dependency on the obj file
  731. p_depends.clear();
  732. p_depends.push_back(relativeObj);
  733. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  734. temp.c_str(), p_depends, no_commands,
  735. true);
  736. }
  737. //----------------------------------------------------------------------------
  738. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  739. {
  740. std::vector<std::string> depends;
  741. std::vector<std::string> no_commands;
  742. // Construct the name of the dependency generation target.
  743. std::string depTarget =
  744. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  745. depTarget += "/requires";
  746. // This target drives dependency generation for all object files.
  747. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  748. std::string objTarget;
  749. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  750. obj != this->Objects.end(); ++obj)
  751. {
  752. objTarget = relPath;
  753. objTarget += *obj;
  754. objTarget += ".requires";
  755. depends.push_back(objTarget);
  756. }
  757. // Write the rule.
  758. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  759. depTarget.c_str(),
  760. depends, no_commands, true);
  761. }
  762. //----------------------------------------------------------------------------
  763. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  764. {
  765. std::vector<std::string> depends;
  766. std::vector<std::string> commands;
  767. // Construct the clean target name.
  768. std::string cleanTarget =
  769. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  770. cleanTarget += "/clean";
  771. // Construct the clean command.
  772. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  773. *this->Target);
  774. this->LocalGenerator->CreateCDCommand
  775. (commands,
  776. this->Makefile->GetStartOutputDirectory(),
  777. this->Makefile->GetHomeOutputDirectory());
  778. // Write the rule.
  779. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  780. cleanTarget.c_str(),
  781. depends, commands, true);
  782. }
  783. //----------------------------------------------------------------------------
  784. void cmMakefileTargetGenerator::WriteTargetDependRules()
  785. {
  786. // must write the targets depend info file
  787. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  788. this->InfoFileNameFull = dir;
  789. this->InfoFileNameFull += "/DependInfo.cmake";
  790. this->InfoFileNameFull =
  791. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  792. this->InfoFileStream =
  793. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  794. this->InfoFileStream->SetCopyIfDifferent(true);
  795. if(!*this->InfoFileStream)
  796. {
  797. return;
  798. }
  799. this->LocalGenerator->
  800. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  801. // Store multiple output pairs in the depend info file.
  802. if(!this->MultipleOutputPairs.empty())
  803. {
  804. *this->InfoFileStream
  805. << "\n"
  806. << "# Pairs of files generated by the same build rule.\n"
  807. << "SET(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  808. for(MultipleOutputPairsType::const_iterator pi =
  809. this->MultipleOutputPairs.begin();
  810. pi != this->MultipleOutputPairs.end(); ++pi)
  811. {
  812. *this->InfoFileStream << " \"" << pi->first << "\" \""
  813. << pi->second << "\"\n";
  814. }
  815. *this->InfoFileStream << " )\n\n";
  816. }
  817. // Store list of targets linked directly or transitively.
  818. {
  819. *this->InfoFileStream
  820. << "\n"
  821. << "# Targets to which this target links.\n"
  822. << "SET(CMAKE_TARGET_LINKED_INFO_FILES\n";
  823. cmGlobalGenerator* gg = this->GlobalGenerator;
  824. std::set<cmTarget const*> emitted;
  825. cmTarget::LinkLibraryVectorType const& libs =
  826. this->Target->GetLinkLibraries();
  827. for(cmTarget::LinkLibraryVectorType::const_iterator j = libs.begin();
  828. j != libs.end(); ++j)
  829. {
  830. if(cmTarget const* linkee = gg->FindTarget(0, j->first.c_str()))
  831. {
  832. if(emitted.insert(linkee).second)
  833. {
  834. cmMakefile* mf = linkee->GetMakefile();
  835. cmLocalGenerator* lg = mf->GetLocalGenerator();
  836. std::string di = mf->GetStartOutputDirectory();
  837. di += "/";
  838. di += lg->GetTargetDirectory(*linkee);
  839. di += "/DependInfo.cmake";
  840. *this->InfoFileStream << " \"" << di << "\"\n";
  841. }
  842. }
  843. }
  844. *this->InfoFileStream
  845. << " )\n";
  846. }
  847. // Check for a target-specific module output directory.
  848. if(const char* mdir = this->GetFortranModuleDirectory())
  849. {
  850. *this->InfoFileStream
  851. << "\n"
  852. << "# Fortran module output directory.\n"
  853. << "SET(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  854. }
  855. // and now write the rule to use it
  856. std::vector<std::string> depends;
  857. std::vector<std::string> commands;
  858. // Construct the name of the dependency generation target.
  859. std::string depTarget =
  860. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  861. depTarget += "/depend";
  862. // Add a command to call CMake to scan dependencies. CMake will
  863. // touch the corresponding depends file after scanning dependencies.
  864. cmOStringStream depCmd;
  865. // TODO: Account for source file properties and directory-level
  866. // definitions when scanning for dependencies.
  867. #if !defined(_WIN32) || defined(__CYGWIN__)
  868. // This platform supports symlinks, so cmSystemTools will translate
  869. // paths. Make sure PWD is set to the original name of the home
  870. // output directory to help cmSystemTools to create the same
  871. // translation table for the dependency scanning process.
  872. depCmd << "cd "
  873. << (this->LocalGenerator->Convert(
  874. this->Makefile->GetHomeOutputDirectory(),
  875. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  876. << " && ";
  877. #endif
  878. // Generate a call this signature:
  879. //
  880. // cmake -E cmake_depends <generator>
  881. // <home-src-dir> <start-src-dir>
  882. // <home-out-dir> <start-out-dir>
  883. // <dep-info> --color=$(COLOR)
  884. //
  885. // This gives the dependency scanner enough information to recreate
  886. // the state of our local generator sufficiently for its needs.
  887. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  888. << this->GlobalGenerator->GetName() << "\" "
  889. << this->Convert(this->Makefile->GetHomeDirectory(),
  890. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  891. << " "
  892. << this->Convert(this->Makefile->GetStartDirectory(),
  893. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  894. << " "
  895. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  896. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  897. << " "
  898. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  899. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  900. << " "
  901. << this->Convert(this->InfoFileNameFull.c_str(),
  902. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  903. << " --color=$(COLOR)";
  904. commands.push_back(depCmd.str());
  905. // Make sure all custom command outputs in this target are built.
  906. if(this->CustomCommandDriver == OnDepends)
  907. {
  908. this->DriveCustomCommands(depends);
  909. }
  910. // Write the rule.
  911. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  912. depTarget.c_str(),
  913. depends, commands, true);
  914. }
  915. //----------------------------------------------------------------------------
  916. void
  917. cmMakefileTargetGenerator
  918. ::DriveCustomCommands(std::vector<std::string>& depends)
  919. {
  920. // Depend on all custom command outputs.
  921. const std::vector<cmSourceFile*>& sources =
  922. this->Target->GetSourceFiles();
  923. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  924. source != sources.end(); ++source)
  925. {
  926. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  927. {
  928. const std::vector<std::string>& outputs = cc->GetOutputs();
  929. for(std::vector<std::string>::const_iterator o = outputs.begin();
  930. o != outputs.end(); ++o)
  931. {
  932. depends.push_back(*o);
  933. }
  934. }
  935. }
  936. }
  937. //----------------------------------------------------------------------------
  938. void cmMakefileTargetGenerator
  939. ::WriteObjectDependRules(cmSourceFile& source,
  940. std::vector<std::string>& depends)
  941. {
  942. // Create the list of dependencies known at cmake time. These are
  943. // shared between the object file and dependency scanning rule.
  944. depends.push_back(source.GetFullPath());
  945. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  946. {
  947. std::vector<std::string> deps;
  948. cmSystemTools::ExpandListArgument(objectDeps, deps);
  949. for(std::vector<std::string>::iterator i = deps.begin();
  950. i != deps.end(); ++i)
  951. {
  952. depends.push_back(i->c_str());
  953. }
  954. }
  955. }
  956. //----------------------------------------------------------------------------
  957. void cmMakefileTargetGenerator
  958. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  959. {
  960. // Collect the commands.
  961. std::vector<std::string> commands;
  962. std::string comment = this->LocalGenerator->ConstructComment(cc);
  963. if(!comment.empty())
  964. {
  965. // add in a progress call if needed
  966. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  967. progressDir += cmake::GetCMakeFilesDirectory();
  968. cmOStringStream progCmd;
  969. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  970. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  971. cmLocalGenerator::FULL,
  972. cmLocalGenerator::SHELL);
  973. this->NumberOfProgressActions++;
  974. progCmd << " $(CMAKE_PROGRESS_"
  975. << this->NumberOfProgressActions
  976. << ")";
  977. commands.push_back(progCmd.str());
  978. this->LocalGenerator
  979. ->AppendEcho(commands, comment.c_str(),
  980. cmLocalUnixMakefileGenerator3::EchoGenerate);
  981. }
  982. this->LocalGenerator->AppendCustomCommand(commands, cc);
  983. // Collect the dependencies.
  984. std::vector<std::string> depends;
  985. this->LocalGenerator->AppendCustomDepend(depends, cc);
  986. // Add a dependency on the rule file itself.
  987. this->LocalGenerator->AppendRuleDepend(depends,
  988. this->BuildFileNameFull.c_str());
  989. // Check whether we need to bother checking for a symbolic output.
  990. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  991. // Write the rule.
  992. const std::vector<std::string>& outputs = cc.GetOutputs();
  993. std::vector<std::string>::const_iterator o = outputs.begin();
  994. {
  995. bool symbolic = false;
  996. if(need_symbolic)
  997. {
  998. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  999. {
  1000. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1001. }
  1002. }
  1003. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1004. o->c_str(), depends, commands,
  1005. symbolic);
  1006. }
  1007. // Write rules to drive building any outputs beyond the first.
  1008. const char* in = o->c_str();
  1009. for(++o; o != outputs.end(); ++o)
  1010. {
  1011. bool symbolic = false;
  1012. if(need_symbolic)
  1013. {
  1014. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  1015. {
  1016. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1017. }
  1018. }
  1019. this->GenerateExtraOutput(o->c_str(), in, symbolic);
  1020. }
  1021. // Setup implicit dependency scanning.
  1022. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1023. idi = cc.GetImplicitDepends().begin();
  1024. idi != cc.GetImplicitDepends().end(); ++idi)
  1025. {
  1026. std::string objFullPath =
  1027. this->Convert(outputs[0].c_str(), cmLocalGenerator::FULL);
  1028. std::string srcFullPath =
  1029. this->Convert(idi->second.c_str(), cmLocalGenerator::FULL);
  1030. this->LocalGenerator->
  1031. AddImplicitDepends(*this->Target, idi->first.c_str(),
  1032. objFullPath.c_str(),
  1033. srcFullPath.c_str());
  1034. }
  1035. }
  1036. //----------------------------------------------------------------------------
  1037. void
  1038. cmMakefileTargetGenerator
  1039. ::GenerateExtraOutput(const char* out, const char* in, bool symbolic)
  1040. {
  1041. // Add a rule to build the primary output if the extra output needs
  1042. // to be created.
  1043. std::vector<std::string> commands;
  1044. std::vector<std::string> depends;
  1045. std::string emptyCommand = this->GlobalGenerator->GetEmptyRuleHackCommand();
  1046. if(!emptyCommand.empty())
  1047. {
  1048. commands.push_back(emptyCommand);
  1049. }
  1050. depends.push_back(in);
  1051. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1052. out, depends, commands,
  1053. symbolic);
  1054. // Register the extra output as paired with the first output so that
  1055. // the check-build-system step will remove the primary output if any
  1056. // extra outputs are missing. This forces the rule to regenerate
  1057. // all outputs.
  1058. this->AddMultipleOutputPair(out, in);
  1059. }
  1060. //----------------------------------------------------------------------------
  1061. void
  1062. cmMakefileTargetGenerator
  1063. ::WriteObjectsVariable(std::string& variableName,
  1064. std::string& variableNameExternal)
  1065. {
  1066. // Write a make variable assignment that lists all objects for the
  1067. // target.
  1068. variableName =
  1069. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1070. "_OBJECTS");
  1071. *this->BuildFileStream
  1072. << "# Object files for target " << this->Target->GetName() << "\n"
  1073. << variableName.c_str() << " =";
  1074. std::string object;
  1075. const char* objName =
  1076. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  1077. const char* lineContinue =
  1078. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1079. if(!lineContinue)
  1080. {
  1081. lineContinue = "\\";
  1082. }
  1083. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1084. i != this->Objects.end(); ++i)
  1085. {
  1086. *this->BuildFileStream << " " << lineContinue << "\n";
  1087. if(objName)
  1088. {
  1089. *this->BuildFileStream <<
  1090. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1091. cmLocalGenerator::MAKEFILE);
  1092. }
  1093. else
  1094. {
  1095. *this->BuildFileStream <<
  1096. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1097. }
  1098. }
  1099. *this->BuildFileStream << "\n";
  1100. // Write a make variable assignment that lists all external objects
  1101. // for the target.
  1102. variableNameExternal =
  1103. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1104. "_EXTERNAL_OBJECTS");
  1105. *this->BuildFileStream
  1106. << "\n"
  1107. << "# External object files for target "
  1108. << this->Target->GetName() << "\n"
  1109. << variableNameExternal.c_str() << " =";
  1110. for(std::vector<std::string>::const_iterator i =
  1111. this->ExternalObjects.begin();
  1112. i != this->ExternalObjects.end(); ++i)
  1113. {
  1114. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  1115. *this->BuildFileStream
  1116. << " " << lineContinue << "\n"
  1117. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1118. if(objName)
  1119. {
  1120. *this->BuildFileStream <<
  1121. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1122. cmLocalGenerator::MAKEFILE);
  1123. }
  1124. else
  1125. {
  1126. *this->BuildFileStream <<
  1127. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1128. }
  1129. }
  1130. *this->BuildFileStream << "\n" << "\n";
  1131. }
  1132. //----------------------------------------------------------------------------
  1133. void
  1134. cmMakefileTargetGenerator
  1135. ::WriteObjectsString(std::string& buildObjs)
  1136. {
  1137. std::vector<std::string> objStrings;
  1138. this->WriteObjectsStrings(objStrings);
  1139. buildObjs = objStrings[0];
  1140. }
  1141. //----------------------------------------------------------------------------
  1142. class cmMakefileTargetGeneratorObjectStrings
  1143. {
  1144. public:
  1145. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1146. cmMakefile* mf,
  1147. cmLocalUnixMakefileGenerator3* lg,
  1148. std::string::size_type limit):
  1149. Strings(strings), Makefile(mf), LocalGenerator(lg), LengthLimit(limit)
  1150. {
  1151. this->Space = "";
  1152. }
  1153. void Feed(std::string const& obj)
  1154. {
  1155. // Construct the name of the next object.
  1156. this->NextObject =
  1157. this->LocalGenerator->Convert(obj.c_str(),
  1158. cmLocalGenerator::START_OUTPUT,
  1159. cmLocalGenerator::SHELL);
  1160. // Roll over to next string if the limit will be exceeded.
  1161. if(this->LengthLimit != std::string::npos &&
  1162. (this->CurrentString.length() + 1 + this->NextObject.length()
  1163. > this->LengthLimit))
  1164. {
  1165. this->Strings.push_back(this->CurrentString);
  1166. this->CurrentString = "";
  1167. this->Space = "";
  1168. }
  1169. // Separate from previous object.
  1170. this->CurrentString += this->Space;
  1171. this->Space = " ";
  1172. // Append this object.
  1173. this->CurrentString += this->NextObject;
  1174. }
  1175. void Done()
  1176. {
  1177. this->Strings.push_back(this->CurrentString);
  1178. }
  1179. private:
  1180. std::vector<std::string>& Strings;
  1181. cmMakefile* Makefile;
  1182. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1183. std::string::size_type LengthLimit;
  1184. std::string CurrentString;
  1185. std::string NextObject;
  1186. const char* Space;
  1187. };
  1188. //----------------------------------------------------------------------------
  1189. void
  1190. cmMakefileTargetGenerator
  1191. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1192. std::string::size_type limit)
  1193. {
  1194. cmMakefileTargetGeneratorObjectStrings
  1195. helper(objStrings, this->Makefile, this->LocalGenerator, limit);
  1196. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1197. i != this->Objects.end(); ++i)
  1198. {
  1199. helper.Feed(*i);
  1200. }
  1201. for(std::vector<std::string>::const_iterator i =
  1202. this->ExternalObjects.begin();
  1203. i != this->ExternalObjects.end(); ++i)
  1204. {
  1205. helper.Feed(*i);
  1206. }
  1207. helper.Done();
  1208. }
  1209. //----------------------------------------------------------------------------
  1210. void cmMakefileTargetGenerator::WriteTargetDriverRule(const char* main_output,
  1211. bool relink)
  1212. {
  1213. // Compute the name of the driver target.
  1214. std::string dir =
  1215. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1216. std::string buildTargetRuleName = dir;
  1217. buildTargetRuleName += relink?"/preinstall":"/build";
  1218. buildTargetRuleName = this->Convert(buildTargetRuleName.c_str(),
  1219. cmLocalGenerator::HOME_OUTPUT,
  1220. cmLocalGenerator::UNCHANGED);
  1221. // Build the list of target outputs to drive.
  1222. std::vector<std::string> depends;
  1223. if(main_output)
  1224. {
  1225. depends.push_back(main_output);
  1226. }
  1227. const char* comment = 0;
  1228. if(relink)
  1229. {
  1230. // Setup the comment for the preinstall driver.
  1231. comment = "Rule to relink during preinstall.";
  1232. }
  1233. else
  1234. {
  1235. // Setup the comment for the main build driver.
  1236. comment = "Rule to build all files generated by this target.";
  1237. // Make sure all custom command outputs in this target are built.
  1238. if(this->CustomCommandDriver == OnBuild)
  1239. {
  1240. this->DriveCustomCommands(depends);
  1241. }
  1242. // Make sure the extra files are built.
  1243. for(std::set<cmStdString>::const_iterator i = this->ExtraFiles.begin();
  1244. i != this->ExtraFiles.end(); ++i)
  1245. {
  1246. depends.push_back(*i);
  1247. }
  1248. }
  1249. // Write the driver rule.
  1250. std::vector<std::string> no_commands;
  1251. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1252. buildTargetRuleName.c_str(),
  1253. depends, no_commands, true);
  1254. }
  1255. //----------------------------------------------------------------------------
  1256. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  1257. {
  1258. #ifndef __APPLE__
  1259. return std::string();
  1260. #else
  1261. std::set<cmStdString> emitted;
  1262. emitted.insert("/System/Library/Frameworks");
  1263. std::vector<std::string> includes;
  1264. this->LocalGenerator->GetIncludeDirectories(includes);
  1265. std::vector<std::string>::iterator i;
  1266. // check all include directories for frameworks as this
  1267. // will already have added a -F for the framework
  1268. for(i = includes.begin(); i != includes.end(); ++i)
  1269. {
  1270. if(this->Target->NameResolvesToFramework(i->c_str()))
  1271. {
  1272. std::string frameworkDir = *i;
  1273. frameworkDir += "/../";
  1274. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1275. emitted.insert(frameworkDir);
  1276. }
  1277. }
  1278. std::string flags;
  1279. std::vector<std::string>& frameworks = this->Target->GetFrameworks();
  1280. for(i = frameworks.begin();
  1281. i != frameworks.end(); ++i)
  1282. {
  1283. if(emitted.insert(*i).second)
  1284. {
  1285. flags += "-F";
  1286. flags += this->LocalGenerator->ConvertToOutputForExisting(i->c_str());
  1287. flags += " ";
  1288. }
  1289. }
  1290. return flags;
  1291. #endif
  1292. }
  1293. //----------------------------------------------------------------------------
  1294. void cmMakefileTargetGenerator
  1295. ::AppendTargetDepends(std::vector<std::string>& depends)
  1296. {
  1297. // Static libraries never depend on anything for linking.
  1298. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1299. {
  1300. return;
  1301. }
  1302. // Compute which library configuration to link.
  1303. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  1304. if(cmSystemTools::UpperCase(
  1305. this->LocalGenerator->ConfigurationName.c_str()) == "DEBUG")
  1306. {
  1307. linkType = cmTarget::DEBUG;
  1308. }
  1309. // Keep track of dependencies already listed.
  1310. std::set<cmStdString> emitted;
  1311. // A target should not depend on itself.
  1312. emitted.insert(this->Target->GetName());
  1313. // Loop over all library dependencies.
  1314. const cmTarget::LinkLibraryVectorType& tlibs =
  1315. this->Target->GetLinkLibraries();
  1316. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  1317. lib != tlibs.end(); ++lib)
  1318. {
  1319. // skip the library if it is not general and the link type
  1320. // does not match the current target
  1321. if(lib->second != cmTarget::GENERAL &&
  1322. lib->second != linkType)
  1323. {
  1324. continue;
  1325. }
  1326. // Don't emit the same library twice for this target.
  1327. if(emitted.insert(lib->first).second)
  1328. {
  1329. // Depend on other CMake targets.
  1330. if(cmTarget* tgt =
  1331. this->GlobalGenerator->FindTarget(0, lib->first.c_str()))
  1332. {
  1333. const char* config = this->LocalGenerator->ConfigurationName.c_str();
  1334. depends.push_back(tgt->GetFullPath(config, false));
  1335. }
  1336. // depend on full path libs as well
  1337. else if(cmSystemTools::FileIsFullPath(lib->first.c_str()))
  1338. {
  1339. depends.push_back(lib->first.c_str());
  1340. }
  1341. }
  1342. }
  1343. }
  1344. //----------------------------------------------------------------------------
  1345. void cmMakefileTargetGenerator
  1346. ::CloseFileStreams()
  1347. {
  1348. delete this->BuildFileStream;
  1349. delete this->InfoFileStream;
  1350. delete this->FlagFileStream;
  1351. }
  1352. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1353. const char* linkLang,
  1354. std::string& linkFlags)
  1355. {
  1356. // check for language flags that are not allowed at link time, and
  1357. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1358. // which fails, there may be more]
  1359. std::string removeFlags = "CMAKE_";
  1360. removeFlags += linkLang;
  1361. removeFlags += flagVar;
  1362. std::string removeflags =
  1363. this->Makefile->GetSafeDefinition(removeFlags.c_str());
  1364. std::vector<std::string> removeList;
  1365. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1366. for(std::vector<std::string>::iterator i = removeList.begin();
  1367. i != removeList.end(); ++i)
  1368. {
  1369. cmSystemTools::ReplaceString(linkFlags, i->c_str(), "");
  1370. }
  1371. }
  1372. void cmMakefileTargetGenerator::WriteProgressVariables(unsigned long total,
  1373. unsigned long &current)
  1374. {
  1375. cmGeneratedFileStream *progressFileStream =
  1376. new cmGeneratedFileStream(this->ProgressFileNameFull.c_str());
  1377. if(!progressFileStream)
  1378. {
  1379. return;
  1380. }
  1381. unsigned long num;
  1382. unsigned long i;
  1383. for (i = 1; i <= this->NumberOfProgressActions; ++i)
  1384. {
  1385. *progressFileStream
  1386. << "CMAKE_PROGRESS_" << i << " = ";
  1387. if (total <= 100)
  1388. {
  1389. num = i + current;
  1390. *progressFileStream << num;
  1391. this->LocalGenerator->ProgressFiles[this->Target->GetName()]
  1392. .push_back(num);
  1393. }
  1394. else if (((i+current)*100)/total > ((i-1+current)*100)/total)
  1395. {
  1396. num = ((i+current)*100)/total;
  1397. *progressFileStream << num;
  1398. this->LocalGenerator->ProgressFiles[this->Target->GetName()]
  1399. .push_back(num);
  1400. }
  1401. *progressFileStream << "\n";
  1402. }
  1403. *progressFileStream << "\n";
  1404. current += this->NumberOfProgressActions;
  1405. delete progressFileStream;
  1406. }
  1407. //----------------------------------------------------------------------------
  1408. void
  1409. cmMakefileTargetGenerator
  1410. ::AddMultipleOutputPair(const char* depender, const char* dependee)
  1411. {
  1412. MultipleOutputPairsType::value_type p(depender, dependee);
  1413. this->MultipleOutputPairs.insert(p);
  1414. }
  1415. //----------------------------------------------------------------------------
  1416. void
  1417. cmMakefileTargetGenerator
  1418. ::CreateLinkScript(const char* name,
  1419. std::vector<std::string> const& link_commands,
  1420. std::vector<std::string>& makefile_commands,
  1421. std::vector<std::string>& makefile_depends)
  1422. {
  1423. // Create the link script file.
  1424. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1425. linkScriptName += "/";
  1426. linkScriptName += name;
  1427. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1428. linkScriptStream.SetCopyIfDifferent(true);
  1429. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1430. cmd != link_commands.end(); ++cmd)
  1431. {
  1432. // Do not write out empty commands or commands beginning in the
  1433. // shell no-op ":".
  1434. if(!cmd->empty() && (*cmd)[0] != ':')
  1435. {
  1436. linkScriptStream << *cmd << "\n";
  1437. }
  1438. }
  1439. // Create the makefile command to invoke the link script.
  1440. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1441. link_command += this->Convert(linkScriptName.c_str(),
  1442. cmLocalGenerator::START_OUTPUT,
  1443. cmLocalGenerator::SHELL);
  1444. link_command += " --verbose=$(VERBOSE)";
  1445. makefile_commands.push_back(link_command);
  1446. makefile_depends.push_back(linkScriptName);
  1447. }
  1448. //----------------------------------------------------------------------------
  1449. std::string
  1450. cmMakefileTargetGenerator
  1451. ::CreateResponseFile(const char* name, std::string const& options,
  1452. std::vector<std::string>& makefile_depends)
  1453. {
  1454. // Create the response file.
  1455. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1456. responseFileNameFull += "/";
  1457. responseFileNameFull += name;
  1458. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1459. responseStream.SetCopyIfDifferent(true);
  1460. responseStream << options << "\n";
  1461. // Add a dependency so the target will rebuild when the set of
  1462. // objects changes.
  1463. makefile_depends.push_back(responseFileNameFull);
  1464. // Construct the name to be used on the command line.
  1465. std::string responseFileName = this->TargetBuildDirectory;
  1466. responseFileName += "/";
  1467. responseFileName += name;
  1468. return responseFileName;
  1469. }
  1470. //----------------------------------------------------------------------------
  1471. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1472. {
  1473. // Compute the module directory.
  1474. if(!this->FortranModuleDirectoryComputed)
  1475. {
  1476. const char* target_mod_dir =
  1477. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1478. const char* moddir_flag =
  1479. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1480. if(target_mod_dir && moddir_flag)
  1481. {
  1482. // Compute the full path to the module directory.
  1483. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1484. {
  1485. // Already a full path.
  1486. this->FortranModuleDirectory = target_mod_dir;
  1487. }
  1488. else
  1489. {
  1490. // Interpret relative to the current output directory.
  1491. this->FortranModuleDirectory =
  1492. this->Makefile->GetCurrentOutputDirectory();
  1493. this->FortranModuleDirectory += "/";
  1494. this->FortranModuleDirectory += target_mod_dir;
  1495. }
  1496. // Make sure the module output directory exists.
  1497. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1498. }
  1499. this->FortranModuleDirectoryComputed = true;
  1500. }
  1501. // Return the computed directory.
  1502. if(this->FortranModuleDirectory.empty())
  1503. {
  1504. return 0;
  1505. }
  1506. else
  1507. {
  1508. return this->FortranModuleDirectory.c_str();
  1509. }
  1510. }
  1511. //----------------------------------------------------------------------------
  1512. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1513. {
  1514. // Add a module output directory flag if necessary.
  1515. if(const char* mod_dir = this->GetFortranModuleDirectory())
  1516. {
  1517. const char* moddir_flag =
  1518. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1519. std::string modflag = moddir_flag;
  1520. modflag += this->Convert(mod_dir,
  1521. cmLocalGenerator::START_OUTPUT,
  1522. cmLocalGenerator::SHELL);
  1523. this->LocalGenerator->AppendFlags(flags, modflag.c_str());
  1524. }
  1525. // If there is a separate module path flag then duplicate the
  1526. // include path with it. This compiler does not search the include
  1527. // path for modules.
  1528. if(const char* modpath_flag =
  1529. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1530. {
  1531. std::vector<std::string> includes;
  1532. this->LocalGenerator->GetIncludeDirectories(includes);
  1533. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1534. idi != includes.end(); ++idi)
  1535. {
  1536. std::string flg = modpath_flag;
  1537. flg += this->Convert(idi->c_str(),
  1538. cmLocalGenerator::NONE,
  1539. cmLocalGenerator::SHELL);
  1540. this->LocalGenerator->AppendFlags(flags, flg.c_str());
  1541. }
  1542. }
  1543. }