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