Imported Upstream version 2.8.9
[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   // Some of the generated files are *.h so the directory "GUI" 
41   // where they are created have to be added to the include path
42   this->Makefile->AddIncludeDirectory( outputDirectory.c_str() );
43
44   for(std::vector<std::string>::iterator i = (newArgs.begin() + 1); 
45       i != newArgs.end(); i++)
46     {
47     cmSourceFile *curr = this->Makefile->GetSource(i->c_str());
48     // if we should use the source GUI 
49     // to generate .cxx and .h files
50     if (!curr || !curr->GetPropertyAsBool("WRAP_EXCLUDE"))
51       {
52       std::string outName = outputDirectory;
53       outName += "/";
54       outName += cmSystemTools::GetFilenameWithoutExtension(*i);
55       std::string hname = outName;
56       hname += ".h";
57       std::string origname = cdir + "/" + *i;
58       // add starting depends
59       std::vector<std::string> depends;
60       depends.push_back(origname);
61       depends.push_back(fluid_exe);
62       std::string cxxres = outName;
63       cxxres += ".cxx";
64
65       cmCustomCommandLine commandLine;
66       commandLine.push_back(fluid_exe);
67       commandLine.push_back("-c"); // instructs Fluid to run in command line
68       commandLine.push_back("-h"); // optionally rename .h files
69       commandLine.push_back(hname);
70       commandLine.push_back("-o"); // optionally rename .cxx files
71       commandLine.push_back(cxxres);
72       commandLine.push_back(origname);// name of the GUI fluid file
73       cmCustomCommandLines commandLines;
74       commandLines.push_back(commandLine);
75
76       // Add command for generating the .h and .cxx files
77       const char* no_main_dependency = 0;
78       const char* no_comment = 0;
79       const char* no_working_dir = 0;
80       this->Makefile->AddCustomCommandToOutput(cxxres.c_str(),
81                                            depends, no_main_dependency,
82                                            commandLines, no_comment,
83                                            no_working_dir);
84       this->Makefile->AddCustomCommandToOutput(hname.c_str(),
85                                            depends, no_main_dependency,
86                                            commandLines, no_comment,
87                                            no_working_dir);
88
89       cmSourceFile *sf = this->Makefile->GetSource(cxxres.c_str());
90       sf->AddDepend(hname.c_str());
91       sf->AddDepend(origname.c_str());
92       this->GeneratedSourcesClasses.push_back(sf);
93       }
94     }
95
96   // create the variable with the list of sources in it
97   size_t lastHeadersClass = this->GeneratedSourcesClasses.size();
98   std::string sourceListValue;
99   for(size_t classNum = 0; classNum < lastHeadersClass; classNum++)
100     {
101     if (classNum)
102       {
103       sourceListValue += ";";
104       }
105     sourceListValue += this->GeneratedSourcesClasses[classNum]->GetFullPath();
106     }
107   std::string varName = this->Target;
108   varName += "_FLTK_UI_SRCS";
109   this->Makefile->AddDefinition(varName.c_str(), sourceListValue.c_str());
110   
111   return true;
112 }
113
114 void cmFLTKWrapUICommand::FinalPass() 
115 {
116   // people should add the srcs to the target themselves, but the old command
117   // didn't support that, so check and see if they added the files in and if
118   // they didn;t then print a warning and add then anyhow
119   cmTarget* target = this->Makefile->FindTarget(this->Target.c_str());
120   if(!target)
121     {
122     std::string msg = 
123       "FLTK_WRAP_UI was called with a target that was never created: ";
124     msg += this->Target;
125     msg +=".  The problem was found while processing the source directory: ";
126     msg += this->Makefile->GetStartDirectory();
127     msg += ".  This FLTK_WRAP_UI call will be ignored.";
128     cmSystemTools::Message(msg.c_str(),"Warning");
129     return;
130     }
131   std::vector<cmSourceFile*> const& srcs = 
132     target->GetSourceFiles();
133   bool found = false;
134   for (unsigned int i = 0; i < srcs.size(); ++i)
135     {
136     if (srcs[i]->GetFullPath() == 
137         this->GeneratedSourcesClasses[0]->GetFullPath())
138       {
139       found = true;
140       break;
141       }
142     }
143   if (!found)
144     {
145     std::string msg = 
146       "In CMake 2.2 the FLTK_WRAP_UI command sets a variable to the list of "
147       "source files that should be added to your executable or library. It "
148       "appears that you have not added these source files to your target. "
149       "You should change your CMakeLists.txt file to "
150       "directly add the generated files to the target. "
151       "For example FTLK_WRAP_UI(foo src1 src2 src3) "
152       "will create a variable named foo_FLTK_UI_SRCS that contains the list "
153       "of sources to add to your target when you call ADD_LIBRARY or "
154       "ADD_EXECUTABLE. For now CMake will add the sources to your target "
155       "for you as was done in CMake 2.0 and earlier. In the future this may "
156       "become an error."; 
157     msg +="The problem was found while processing the source directory: ";
158     msg += this->Makefile->GetStartDirectory();
159     cmSystemTools::Message(msg.c_str(),"Warning");
160     // first we add the rules for all the .fl to .h and .cxx files
161     size_t lastHeadersClass = this->GeneratedSourcesClasses.size();
162     
163     // Generate code for all the .fl files
164     for(size_t classNum = 0; classNum < lastHeadersClass; classNum++)
165       {
166       this->Makefile->GetTargets()[this->Target]
167         .AddSourceFile(this->GeneratedSourcesClasses[classNum]);
168       }
169     }
170 }
171
172
173