cmFLTKWrapUICommand.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmFLTKWrapUICommand.h"
  4. #include <cstddef>
  5. #include <utility>
  6. #include <cm/memory>
  7. #include <cm/string_view>
  8. #include "cmCustomCommand.h"
  9. #include "cmCustomCommandLines.h"
  10. #include "cmExecutionStatus.h"
  11. #include "cmLocalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmMessageType.h"
  14. #include "cmRange.h"
  15. #include "cmSourceFile.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmake.h"
  19. class cmListFileBacktrace;
  20. class cmTarget;
  21. static void FinalAction(cmMakefile& makefile, std::string const& name,
  22. cmListFileBacktrace const& lfbt)
  23. {
  24. // people should add the srcs to the target themselves, but the old command
  25. // didn't support that, so check and see if they added the files in and if
  26. // they didn;t then print a warning and add then anyhow
  27. cmTarget* target = makefile.FindLocalNonAliasTarget(name);
  28. if (!target) {
  29. std::string msg = cmStrCat(
  30. "FLTK_WRAP_UI was called with a target that was never created: ", name,
  31. ". The problem was found while processing the source directory: ",
  32. makefile.GetCurrentSourceDirectory(),
  33. ". This FLTK_WRAP_UI call will be ignored.");
  34. makefile.GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_ERROR, msg,
  35. lfbt);
  36. }
  37. }
  38. bool cmFLTKWrapUICommand(std::vector<std::string> const& args,
  39. cmExecutionStatus& status)
  40. {
  41. if (args.size() < 2) {
  42. status.SetError("called with incorrect number of arguments");
  43. return false;
  44. }
  45. cmMakefile& mf = status.GetMakefile();
  46. // what is the current source dir
  47. std::string cdir = mf.GetCurrentSourceDirectory();
  48. std::string const& fluid_exe =
  49. mf.GetRequiredDefinition("FLTK_FLUID_EXECUTABLE");
  50. // Target that will use the generated files
  51. std::string const& target = args[0];
  52. // get the list of GUI files from which .cxx and .h will be generated
  53. std::string outputDirectory = mf.GetCurrentBinaryDirectory();
  54. {
  55. // Some of the generated files are *.h so the directory "GUI"
  56. // where they are created have to be added to the include path
  57. std::vector<std::string> outputDirectories;
  58. outputDirectories.push_back(outputDirectory);
  59. mf.AddIncludeDirectories(outputDirectories);
  60. }
  61. // List of produced files.
  62. std::vector<cmSourceFile*> generatedSourcesClasses;
  63. for (std::string const& arg : cmMakeRange(args).advance(1)) {
  64. cmSourceFile* curr = mf.GetSource(arg);
  65. // if we should use the source GUI
  66. // to generate .cxx and .h files
  67. if (!curr || !curr->GetPropertyAsBool("WRAP_EXCLUDE")) {
  68. std::string outName = cmStrCat(
  69. outputDirectory, '/', cmSystemTools::GetFilenameWithoutExtension(arg));
  70. std::string hname = cmStrCat(outName, ".h");
  71. std::string origname = cmStrCat(cdir, '/', arg);
  72. // add starting depends
  73. std::vector<std::string> depends;
  74. depends.push_back(origname);
  75. depends.push_back(fluid_exe);
  76. std::string cxxres = cmStrCat(outName, ".cxx");
  77. cmCustomCommandLines commandLines = cmMakeSingleCommandLine({
  78. fluid_exe,
  79. "-c", // instructs Fluid to run in command line
  80. "-h", // optionally rename .h files
  81. hname,
  82. "-o", // optionally rename .cxx files
  83. cxxres,
  84. origname // name of the GUI fluid file
  85. });
  86. // Add command for generating the .h and .cxx files
  87. auto hcc = cm::make_unique<cmCustomCommand>();
  88. hcc->SetDepends(depends);
  89. hcc->SetCommandLines(commandLines);
  90. auto ccc = cm::make_unique<cmCustomCommand>(*hcc);
  91. hcc->SetOutputs(cxxres);
  92. mf.AddCustomCommandToOutput(std::move(hcc));
  93. ccc->SetOutputs(hname);
  94. mf.AddCustomCommandToOutput(std::move(ccc));
  95. cmSourceFile* sf = mf.GetSource(cxxres);
  96. sf->AddDepend(hname);
  97. sf->AddDepend(origname);
  98. generatedSourcesClasses.push_back(sf);
  99. }
  100. }
  101. // create the variable with the list of sources in it
  102. size_t lastHeadersClass = generatedSourcesClasses.size();
  103. std::string sourceListValue;
  104. for (size_t classNum = 0; classNum < lastHeadersClass; classNum++) {
  105. if (classNum) {
  106. sourceListValue += ";";
  107. }
  108. sourceListValue += generatedSourcesClasses[classNum]->ResolveFullPath();
  109. }
  110. std::string const varName = target + "_FLTK_UI_SRCS";
  111. mf.AddDefinition(varName, sourceListValue);
  112. mf.AddGeneratorAction(
  113. [target](cmLocalGenerator& lg, cmListFileBacktrace const& lfbt) {
  114. FinalAction(*lg.GetMakefile(), target, lfbt);
  115. });
  116. return true;
  117. }