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"
10 #include "cmCustomCommand.h"
11 #include "cmCustomCommandLines.h"
12 #include "cmExecutionStatus.h"
13 #include "cmLocalGenerator.h"
14 #include "cmMakefile.h"
15 #include "cmMessageType.h"
17 #include "cmSourceFile.h"
18 #include "cmStringAlgorithms.h"
19 #include "cmSystemTools.h"
22 class cmListFileBacktrace;
25 static void FinalAction(cmMakefile& makefile, std::string const& name,
26 const cmListFileBacktrace& lfbt)
28 // people should add the srcs to the target themselves, but the old command
29 // didn't support that, so check and see if they added the files in and if
30 // they didn;t then print a warning and add then anyhow
31 cmTarget* target = makefile.FindLocalNonAliasTarget(name);
33 std::string msg = cmStrCat(
34 "FLTK_WRAP_UI was called with a target that was never created: ", name,
35 ". The problem was found while processing the source directory: ",
36 makefile.GetCurrentSourceDirectory(),
37 ". This FLTK_WRAP_UI call will be ignored.");
38 makefile.GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_ERROR, msg,
43 bool cmFLTKWrapUICommand(std::vector<std::string> const& args,
44 cmExecutionStatus& status)
46 if (args.size() < 2) {
47 status.SetError("called with incorrect number of arguments");
51 cmMakefile& mf = status.GetMakefile();
53 // what is the current source dir
54 std::string cdir = mf.GetCurrentSourceDirectory();
55 std::string const& fluid_exe =
56 mf.GetRequiredDefinition("FLTK_FLUID_EXECUTABLE");
58 // Target that will use the generated files
59 std::string const& target = args[0];
61 // get the list of GUI files from which .cxx and .h will be generated
62 std::string outputDirectory = mf.GetCurrentBinaryDirectory();
65 // Some of the generated files are *.h so the directory "GUI"
66 // where they are created have to be added to the include path
67 std::vector<std::string> outputDirectories;
68 outputDirectories.push_back(outputDirectory);
69 mf.AddIncludeDirectories(outputDirectories);
72 // List of produced files.
73 std::vector<cmSourceFile*> generatedSourcesClasses;
75 for (std::string const& arg : cmMakeRange(args).advance(1)) {
76 cmSourceFile* curr = mf.GetSource(arg);
77 // if we should use the source GUI
78 // to generate .cxx and .h files
79 if (!curr || !curr->GetPropertyAsBool("WRAP_EXCLUDE")) {
80 std::string outName = cmStrCat(
81 outputDirectory, "/", cmSystemTools::GetFilenameWithoutExtension(arg));
82 std::string hname = cmStrCat(outName, ".h");
83 std::string origname = cmStrCat(cdir, "/", arg);
84 // add starting depends
85 std::vector<std::string> depends;
86 depends.push_back(origname);
87 depends.push_back(fluid_exe);
88 std::string cxxres = cmStrCat(outName, ".cxx");
90 cmCustomCommandLines commandLines = cmMakeSingleCommandLine({
92 "-c", // instructs Fluid to run in command line
93 "-h", // optionally rename .h files
95 "-o", // optionally rename .cxx files
97 origname // name of the GUI fluid file
100 // Add command for generating the .h and .cxx files
102 auto hcc = cm::make_unique<cmCustomCommand>();
103 hcc->SetDepends(depends);
104 hcc->SetCommandLines(commandLines);
105 auto ccc = cm::make_unique<cmCustomCommand>(*hcc);
107 hcc->SetOutputs(cxxres);
108 mf.AddCustomCommandToOutput(std::move(hcc));
110 ccc->SetOutputs(hname);
111 mf.AddCustomCommandToOutput(std::move(ccc));
113 cmSourceFile* sf = mf.GetSource(cxxres);
114 sf->AddDepend(hname);
115 sf->AddDepend(origname);
116 generatedSourcesClasses.push_back(sf);
120 // create the variable with the list of sources in it
121 size_t lastHeadersClass = generatedSourcesClasses.size();
122 std::string sourceListValue;
123 for (size_t classNum = 0; classNum < lastHeadersClass; classNum++) {
125 sourceListValue += ";";
127 sourceListValue += generatedSourcesClasses[classNum]->ResolveFullPath();
130 std::string const varName = target + "_FLTK_UI_SRCS";
131 mf.AddDefinition(varName, sourceListValue);
133 mf.AddGeneratorAction(
134 [target](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt) {
135 FinalAction(*lg.GetMakefile(), target, lfbt);