cmMakefileTargetGenerator.cxx 59 KB

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