]> git.saurik.com Git - wxWidgets.git/blame - utils/makegen/makegen.cpp
fixed typo
[wxWidgets.git] / utils / makegen / makegen.cpp
CommitLineData
6809ee4b
VZ
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
e612f101
VZ
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
6809ee4b
VZ
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
5aa5c1e4
GD
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"
6809ee4b
VZ
40
41// ----------------------------------------------------------------------------
42// the application class
43// ----------------------------------------------------------------------------
44
45class MakeGenApp : public wxApp
46{
47public:
e612f101
VZ
48 MakeGenApp() { m_quiet = FALSE; }
49
6809ee4b
VZ
50 virtual bool OnInit();
51
52 virtual int OnRun();
53
54private:
6809ee4b
VZ
55 bool GenerateMakefile(const wxString& filename);
56
e612f101 57 wxString m_progname, // the name of the sample
6809ee4b 58 m_dirname, // directory with the template files
e612f101
VZ
59 m_outdir; // directory to output files to
60
61 bool m_quiet; // don't give non essential messages
6809ee4b
VZ
62};
63
64IMPLEMENT_APP(MakeGenApp);
65
66// ============================================================================
67// implementation
68// ============================================================================
69
70// ----------------------------------------------------------------------------
71// MakeGenApp
72// ----------------------------------------------------------------------------
73
6809ee4b
VZ
74bool 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
2286341c
VZ
84 wxString fileOutName;
85 fileOutName << m_outdir << _T('/') << filename;
86 wxFFile fileOut(fileOutName, "w");
6809ee4b
VZ
87 if ( !fileOut.IsOpened() )
88 {
89 wxLogError(_T("Makefile '%s' couldn't be generated."), filename.c_str());
90
91 return FALSE;
92 }
93
e612f101 94 wxLogVerbose(_T("Generating '%s' for '%s'..."),
2286341c 95 fileOutName.c_str(), m_progname.c_str());
e612f101 96
6809ee4b
VZ
97 size_t count = fileIn.GetLineCount();
98 for ( size_t n = 0; n < count; n++ )
99 {
100 wxString line = fileIn[n];
101
102 line.Replace(_T("#DATE"), wxDateTime::Now().FormatISODate());
e612f101 103 line.Replace(_T("#NAME"), m_progname);
6809ee4b
VZ
104
105 fileOut.Write(line + _T('\n'));
106 }
107
108 return TRUE;
109}
110
e612f101 111// parse the cmd line
6809ee4b
VZ
112bool MakeGenApp::OnInit()
113{
e612f101 114 static const wxCmdLineEntryDesc cmdLineDesc[] =
6809ee4b 115 {
e612f101
VZ
116 { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("give this usage message"),
117 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
118 { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be more verbose") },
119 { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") },
6809ee4b 120
e612f101
VZ
121 { wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("directory with template files") },
122
123 { wxCMD_LINE_PARAM, NULL, NULL, _T("output_directory") },
124
125 { wxCMD_LINE_NONE }
126 };
127
128 wxCmdLineParser parser(cmdLineDesc, argc, argv);
129 parser.SetLogo(_T("MakeGen: a makefile generator for wxWindows\n"
130 "Copyright (c) 2000 Vadim Zeitlin"));
131
132 if ( parser.Parse() != 0 )
133 {
134 // failed to parse the cmd line or help was requested (and given)
6809ee4b
VZ
135 return FALSE;
136 }
137
e612f101
VZ
138 (void)parser.Found(_T("i"), &m_dirname);
139 if ( parser.Found(_T("q")) )
6809ee4b 140 {
e612f101
VZ
141 m_quiet = TRUE;
142
143 wxLog::GetActiveTarget()->SetVerbose(FALSE);
144 }
145 else if ( parser.Found(_T("v")) )
146 {
147 wxLog::GetActiveTarget()->SetVerbose();
148 }
149
150 m_outdir = parser.GetParam();
151
152#ifdef __WINDOWS__
153 m_outdir.Replace(_T("\\"), _T("/"));
154#endif
155
156 if ( !!m_outdir && m_outdir.Last() == _T('/') )
157 {
158 m_outdir.Truncate(m_outdir.length() - 1);
159 }
160
161 m_progname = m_outdir.AfterLast(_T('/'));
162
163 if ( !m_progname )
164 {
165 wxLogError(_T("Output directory should be specified."));
6809ee4b 166
e612f101
VZ
167 parser.Usage();
168
169 return FALSE;
6809ee4b
VZ
170 }
171
172 return TRUE;
173}
174
175int MakeGenApp::OnRun()
176{
6809ee4b
VZ
177 if ( !m_dirname )
178 {
e612f101
VZ
179 m_dirname = wxGetenv(_T("MAKEGEN_PATH"));
180 if ( !m_dirname )
181 {
182 m_dirname = wxGetCwd();
183 }
6809ee4b
VZ
184 }
185
186 if ( !wxEndsWithPathSeparator(m_dirname) )
187 {
188 m_dirname += _T('/');
189 }
190
191 m_dirname += _T("templates");
192
193 wxDir dir(m_dirname);
194
195 m_dirname += _T('/');
196
197 if ( !dir.IsOpened() )
198 {
199 wxLogError(_T("Aborting generating the makefiles."));
200
201 return 1;
202 }
203
204 wxString filename;
205 size_t n = 0;
e612f101 206 bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES);
6809ee4b
VZ
207 while ( cont )
208 {
209 n++;
210
211 if ( !GenerateMakefile(filename) )
212 {
213 wxLogError(_T("Error during makefile generation, aborting."));
214
215 return 2;
216 }
217
218 cont = dir.GetNext(&filename);
219 }
220
221 if ( n )
222 {
223 wxLogVerbose(_T("Successfully generated %u makefiles in '%s'."),
e612f101 224 n, m_outdir.c_str());
6809ee4b 225 }
e612f101 226 else if ( !m_quiet )
6809ee4b
VZ
227 {
228 wxLogWarning(_T("No makefiles found: either set MAKEGEN_PATH variable "
229 "or run the program from its directory"));
230 }
231
232 return 0;
233}