cmFLTKWrapUICommand.cxx 4.2 KB

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