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