cmMakefileTargetGenerator.cxx 54 KB

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