packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmFLTKWrapUICommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmFLTKWrapUICommand.h"
13
14 #include "cmSourceFile.h"
15
16 // cmFLTKWrapUICommand
17 bool cmFLTKWrapUICommand
18 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
19 {
20   if(args.size() < 2 )
21     {
22     this->SetError("called with incorrect number of arguments");
23     return false;
24     }
25
26   // what is the current source dir
27   std::string cdir = this->Makefile->GetCurrentDirectory();
28   const char* fluid_exe =
29     this->Makefile->GetRequiredDefinition("FLTK_FLUID_EXECUTABLE");
30
31   // get parameter for the command
32   this->Target = args[0];  // Target that will use the generated files
33
34   std::vector<std::string> newArgs;
35   this->Makefile->ExpandSourceListArguments(args,newArgs, 1);
36
37   // get the list of GUI files from which .cxx and .h will be generated
38   std::string outputDirectory = this->Makefile->GetCurrentOutputDirectory();
39
40   {
41   // Some of the generated files are *.h so the directory "GUI"
42   // where they are created have to be added to the include path
43   std::vector<std::string> outputDirectories;
44   outputDirectories.push_back(outputDirectory);
45   this->Makefile->AddIncludeDirectories( outputDirectories );
46   }
47
48   for(std::vector<std::string>::iterator i = (newArgs.begin() + 1);
49       i != newArgs.end(); i++)
50     {
51     cmSourceFile *curr = this->Makefile->GetSource(i->c_str());
52     // if we should use the source GUI
53     // to generate .cxx and .h files
54     if (!curr || !curr->GetPropertyAsBool("WRAP_EXCLUDE"))
55       {
56       std::string outName = outputDirectory;
57       outName += "/";
58       outName += cmSystemTools::GetFilenameWithoutExtension(*i);
59       std::string hname = outName;
60       hname += ".h";
61       std::string origname = cdir + "/" + *i;
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 = outName;
67       cxxres += ".cxx";
68
69       cmCustomCommandLine commandLine;
70       commandLine.push_back(fluid_exe);
71       commandLine.push_back("-c"); // instructs Fluid to run in command line
72       commandLine.push_back("-h"); // optionally rename .h files
73       commandLine.push_back(hname);
74       commandLine.push_back("-o"); // optionally rename .cxx files
75       commandLine.push_back(cxxres);
76       commandLine.push_back(origname);// name of the GUI fluid file
77       cmCustomCommandLines commandLines;
78       commandLines.push_back(commandLine);
79
80       // Add command for generating the .h and .cxx files
81       const char* no_main_dependency = 0;
82       const char* no_comment = 0;
83       const char* no_working_dir = 0;
84       this->Makefile->AddCustomCommandToOutput(cxxres.c_str(),
85                                            depends, no_main_dependency,
86                                            commandLines, no_comment,
87                                            no_working_dir);
88       this->Makefile->AddCustomCommandToOutput(hname.c_str(),
89                                            depends, no_main_dependency,
90                                            commandLines, no_comment,
91                                            no_working_dir);
92
93       cmSourceFile *sf = this->Makefile->GetSource(cxxres.c_str());
94       sf->AddDepend(hname.c_str());
95       sf->AddDepend(origname.c_str());
96       this->GeneratedSourcesClasses.push_back(sf);
97       }
98     }
99
100   // create the variable with the list of sources in it
101   size_t lastHeadersClass = this->GeneratedSourcesClasses.size();
102   std::string sourceListValue;
103   for(size_t classNum = 0; classNum < lastHeadersClass; classNum++)
104     {
105     if (classNum)
106       {
107       sourceListValue += ";";
108       }
109     sourceListValue += this->GeneratedSourcesClasses[classNum]->GetFullPath();
110     }
111   std::string varName = this->Target;
112   varName += "_FLTK_UI_SRCS";
113   this->Makefile->AddDefinition(varName.c_str(), sourceListValue.c_str());
114
115   return true;
116 }
117
118 void cmFLTKWrapUICommand::FinalPass()
119 {
120   // people should add the srcs to the target themselves, but the old command
121   // didn't support that, so check and see if they added the files in and if
122   // they didn;t then print a warning and add then anyhow
123   cmTarget* target = this->Makefile->FindTarget(this->Target.c_str());
124   if(!target)
125     {
126     std::string msg =
127       "FLTK_WRAP_UI was called with a target that was never created: ";
128     msg += this->Target;
129     msg +=".  The problem was found while processing the source directory: ";
130     msg += this->Makefile->GetStartDirectory();
131     msg += ".  This FLTK_WRAP_UI call will be ignored.";
132     cmSystemTools::Message(msg.c_str(),"Warning");
133     return;
134     }
135   std::vector<cmSourceFile*> const& srcs =
136     target->GetSourceFiles();
137   bool found = false;
138   for (unsigned int i = 0; i < srcs.size(); ++i)
139     {
140     if (srcs[i]->GetFullPath() ==
141         this->GeneratedSourcesClasses[0]->GetFullPath())
142       {
143       found = true;
144       break;
145       }
146     }
147   if (!found)
148     {
149     std::string msg =
150       "In CMake 2.2 the FLTK_WRAP_UI command sets a variable to the list of "
151       "source files that should be added to your executable or library. It "
152       "appears that you have not added these source files to your target. "
153       "You should change your CMakeLists.txt file to "
154       "directly add the generated files to the target. "
155       "For example FTLK_WRAP_UI(foo src1 src2 src3) "
156       "will create a variable named foo_FLTK_UI_SRCS that contains the list "
157       "of sources to add to your target when you call ADD_LIBRARY or "
158       "ADD_EXECUTABLE. For now CMake will add the sources to your target "
159       "for you as was done in CMake 2.0 and earlier. In the future this may "
160       "become an error.";
161     msg +="The problem was found while processing the source directory: ";
162     msg += this->Makefile->GetStartDirectory();
163     cmSystemTools::Message(msg.c_str(),"Warning");
164     // first we add the rules for all the .fl to .h and .cxx files
165     size_t lastHeadersClass = this->GeneratedSourcesClasses.size();
166
167     // Generate code for all the .fl files
168     for(size_t classNum = 0; classNum < lastHeadersClass; classNum++)
169       {
170       this->Makefile->GetTargets()[this->Target]
171         .AddSourceFile(this->GeneratedSourcesClasses[classNum]);
172       }
173     }
174 }
175
176
177