resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmFLTKWrapUICommand.cxx
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
5 #include <cstddef>
6 #include <utility>
7
8 #include <cm/memory>
9
10 #include "cmCustomCommand.h"
11 #include "cmCustomCommandLines.h"
12 #include "cmExecutionStatus.h"
13 #include "cmLocalGenerator.h"
14 #include "cmMakefile.h"
15 #include "cmMessageType.h"
16 #include "cmRange.h"
17 #include "cmSourceFile.h"
18 #include "cmStringAlgorithms.h"
19 #include "cmSystemTools.h"
20 #include "cmake.h"
21
22 class cmListFileBacktrace;
23 class cmTarget;
24
25 static void FinalAction(cmMakefile& makefile, std::string const& name,
26                         const cmListFileBacktrace& lfbt)
27 {
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);
32   if (!target) {
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,
39                                               lfbt);
40   }
41 }
42
43 bool cmFLTKWrapUICommand(std::vector<std::string> const& args,
44                          cmExecutionStatus& status)
45 {
46   if (args.size() < 2) {
47     status.SetError("called with incorrect number of arguments");
48     return false;
49   }
50
51   cmMakefile& mf = status.GetMakefile();
52
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");
57
58   // Target that will use the generated files
59   std::string const& target = args[0];
60
61   // get the list of GUI files from which .cxx and .h will be generated
62   std::string outputDirectory = mf.GetCurrentBinaryDirectory();
63
64   {
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);
70   }
71
72   // List of produced files.
73   std::vector<cmSourceFile*> generatedSourcesClasses;
74
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");
89
90       cmCustomCommandLines commandLines = cmMakeSingleCommandLine({
91         fluid_exe,
92         "-c", // instructs Fluid to run in command line
93         "-h", // optionally rename .h files
94         hname,
95         "-o", // optionally rename .cxx files
96         cxxres,
97         origname // name of the GUI fluid file
98       });
99
100       // Add command for generating the .h and .cxx files
101
102       auto hcc = cm::make_unique<cmCustomCommand>();
103       hcc->SetDepends(depends);
104       hcc->SetCommandLines(commandLines);
105       auto ccc = cm::make_unique<cmCustomCommand>(*hcc);
106
107       hcc->SetOutputs(cxxres);
108       mf.AddCustomCommandToOutput(std::move(hcc));
109
110       ccc->SetOutputs(hname);
111       mf.AddCustomCommandToOutput(std::move(ccc));
112
113       cmSourceFile* sf = mf.GetSource(cxxres);
114       sf->AddDepend(hname);
115       sf->AddDepend(origname);
116       generatedSourcesClasses.push_back(sf);
117     }
118   }
119
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++) {
124     if (classNum) {
125       sourceListValue += ";";
126     }
127     sourceListValue += generatedSourcesClasses[classNum]->ResolveFullPath();
128   }
129
130   std::string const varName = target + "_FLTK_UI_SRCS";
131   mf.AddDefinition(varName, sourceListValue);
132
133   mf.AddGeneratorAction(
134     [target](cmLocalGenerator& lg, const cmListFileBacktrace& lfbt) {
135       FinalAction(*lg.GetMakefile(), target, lfbt);
136     });
137   return true;
138 }