]> git.saurik.com Git - wxWidgets.git/blob - utils/makegen/makegen.cpp
1. added wfstream.cpp to wxBase (needed by filesys.cpp)
[wxWidgets.git] / utils / makegen / makegen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils/makegen/makegen.cpp
3 // Purpose: a tool to generate the makefiles for samples
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 03.01.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 * TODO
14 *
15 * 1. support for programs with multiple object files
16 * 2. support for programs under utils and demos, not only samples
17 */
18
19 // ============================================================================
20 // declarations
21 // ============================================================================
22
23 #if wxUSE_GUI
24 #error "This is a console mode program and must be linked with wxBase."
25 #endif
26
27 // ----------------------------------------------------------------------------
28 // headers
29 // ----------------------------------------------------------------------------
30
31 #include <wx/string.h>
32 #include <wx/file.h>
33 #include <wx/ffile.h>
34 #include <wx/app.h>
35 #include <wx/log.h>
36 #include <wx/dir.h>
37 #include <wx/textfile.h>
38 #include <wx/datetime.h>
39 #include <wx/cmdline.h>
40
41 // ----------------------------------------------------------------------------
42 // the application class
43 // ----------------------------------------------------------------------------
44
45 class MakeGenApp : public wxApp
46 {
47 public:
48 MakeGenApp() { m_quiet = FALSE; }
49
50 virtual bool OnInit();
51
52 virtual int OnRun();
53
54 private:
55 bool GenerateMakefile(const wxString& filename);
56
57 wxString m_progname, // the name of the sample
58 m_dirname, // directory with the template files
59 m_outdir; // directory to output files to
60
61 bool m_quiet; // don't give non essential messages
62 };
63
64 IMPLEMENT_APP(MakeGenApp);
65
66 // ============================================================================
67 // implementation
68 // ============================================================================
69
70 // ----------------------------------------------------------------------------
71 // MakeGenApp
72 // ----------------------------------------------------------------------------
73
74 bool MakeGenApp::GenerateMakefile(const wxString& filename)
75 {
76 wxTextFile fileIn(m_dirname + filename);
77 if ( !fileIn.Open() )
78 {
79 wxLogError(_T("Makefile '%s' couldn't be generated."), filename.c_str());
80
81 return FALSE;
82 }
83
84 wxFFile fileOut(m_outdir + filename, "w");
85 if ( !fileOut.IsOpened() )
86 {
87 wxLogError(_T("Makefile '%s' couldn't be generated."), filename.c_str());
88
89 return FALSE;
90 }
91
92 wxLogVerbose(_T("Generating '%s' for '%s'..."),
93 (m_outdir + filename).c_str(), m_progname.c_str());
94
95 size_t count = fileIn.GetLineCount();
96 for ( size_t n = 0; n < count; n++ )
97 {
98 wxString line = fileIn[n];
99
100 line.Replace(_T("#DATE"), wxDateTime::Now().FormatISODate());
101 line.Replace(_T("#NAME"), m_progname);
102
103 fileOut.Write(line + _T('\n'));
104 }
105
106 return TRUE;
107 }
108
109 // parse the cmd line
110 bool MakeGenApp::OnInit()
111 {
112 static const wxCmdLineEntryDesc cmdLineDesc[] =
113 {
114 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("give this usage message"),
115 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
116 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be more verbose") },
117 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") },
118
119 { wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("directory with template files") },
120
121 { wxCMD_LINE_PARAM, NULL, NULL, _T("output_directory") },
122
123 { wxCMD_LINE_NONE }
124 };
125
126 wxCmdLineParser parser(cmdLineDesc, argc, argv);
127 parser.SetLogo(_T("MakeGen: a makefile generator for wxWindows\n"
128 "Copyright (c) 2000 Vadim Zeitlin"));
129
130 if ( parser.Parse() != 0 )
131 {
132 // failed to parse the cmd line or help was requested (and given)
133 return FALSE;
134 }
135
136 (void)parser.Found(_T("i"), &m_dirname);
137 if ( parser.Found(_T("q")) )
138 {
139 m_quiet = TRUE;
140
141 wxLog::GetActiveTarget()->SetVerbose(FALSE);
142 }
143 else if ( parser.Found(_T("v")) )
144 {
145 wxLog::GetActiveTarget()->SetVerbose();
146 }
147
148 m_outdir = parser.GetParam();
149
150 #ifdef __WINDOWS__
151 m_outdir.Replace(_T("\\"), _T("/"));
152 #endif
153
154 if ( !!m_outdir && m_outdir.Last() == _T('/') )
155 {
156 m_outdir.Truncate(m_outdir.length() - 1);
157 }
158
159 m_progname = m_outdir.AfterLast(_T('/'));
160
161 if ( !m_progname )
162 {
163 wxLogError(_T("Output directory should be specified."));
164
165 parser.Usage();
166
167 return FALSE;
168 }
169
170 return TRUE;
171 }
172
173 int MakeGenApp::OnRun()
174 {
175 if ( !m_dirname )
176 {
177 m_dirname = wxGetenv(_T("MAKEGEN_PATH"));
178 if ( !m_dirname )
179 {
180 m_dirname = wxGetCwd();
181 }
182 }
183
184 if ( !wxEndsWithPathSeparator(m_dirname) )
185 {
186 m_dirname += _T('/');
187 }
188
189 m_dirname += _T("templates");
190
191 wxDir dir(m_dirname);
192
193 m_dirname += _T('/');
194
195 if ( !dir.IsOpened() )
196 {
197 wxLogError(_T("Aborting generating the makefiles."));
198
199 return 1;
200 }
201
202 wxString filename;
203 size_t n = 0;
204 bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES);
205 while ( cont )
206 {
207 n++;
208
209 if ( !GenerateMakefile(filename) )
210 {
211 wxLogError(_T("Error during makefile generation, aborting."));
212
213 return 2;
214 }
215
216 cont = dir.GetNext(&filename);
217 }
218
219 if ( n )
220 {
221 wxLogVerbose(_T("Successfully generated %u makefiles in '%s'."),
222 n, m_outdir.c_str());
223 }
224 else if ( !m_quiet )
225 {
226 wxLogWarning(_T("No makefiles found: either set MAKEGEN_PATH variable "
227 "or run the program from its directory"));
228 }
229
230 return 0;
231 }