]> git.saurik.com Git - wxWidgets.git/blame_incremental - utils/makegen/makegen.cpp
makegen program for makefile generation for the samples
[wxWidgets.git] / utils / makegen / makegen.cpp
... / ...
CommitLineData
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// declarations
14// ============================================================================
15
16#if wxUSE_GUI
17 #error "This is a console mode program and must be linked with wxBase."
18#endif
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
24#include <wx/string.h>
25#include <wx/file.h>
26#include <wx/ffile.h>
27#include <wx/app.h>
28#include <wx/log.h>
29#include <wx/dir.h>
30#include <wx/textfile.h>
31#include <wx/datetime.h>
32
33// ----------------------------------------------------------------------------
34// the application class
35// ----------------------------------------------------------------------------
36
37class MakeGenApp : public wxApp
38{
39public:
40 virtual bool OnInit();
41
42 virtual int OnRun();
43
44private:
45 void Usage(); // give the usage message
46
47 bool GenerateMakefile(const wxString& filename);
48
49 wxString m_sampleName, // the name of the sample
50 m_dirname, // directory with the template files
51 m_outputDir; // directory to output files to
52};
53
54IMPLEMENT_APP(MakeGenApp);
55
56// ============================================================================
57// implementation
58// ============================================================================
59
60// ----------------------------------------------------------------------------
61// MakeGenApp
62// ----------------------------------------------------------------------------
63
64void MakeGenApp::Usage()
65{
66 wxLogError(_T("Usage: %s [-o output_dir] sample_name"), argv[0]);
67}
68
69bool MakeGenApp::GenerateMakefile(const wxString& filename)
70{
71 wxTextFile fileIn(m_dirname + filename);
72 if ( !fileIn.Open() )
73 {
74 wxLogError(_T("Makefile '%s' couldn't be generated."), filename.c_str());
75
76 return FALSE;
77 }
78
79 wxFFile fileOut(m_outputDir + filename, "w");
80 if ( !fileOut.IsOpened() )
81 {
82 wxLogError(_T("Makefile '%s' couldn't be generated."), filename.c_str());
83
84 return FALSE;
85 }
86
87 size_t count = fileIn.GetLineCount();
88 for ( size_t n = 0; n < count; n++ )
89 {
90 wxString line = fileIn[n];
91
92 line.Replace(_T("#DATE"), wxDateTime::Now().FormatISODate());
93 line.Replace(_T("#NAME"), m_sampleName);
94
95 fileOut.Write(line + _T('\n'));
96 }
97
98 return TRUE;
99}
100
101bool MakeGenApp::OnInit()
102{
103 // parse the cmd line
104 if ( (argc == 1) || (argc == 3) ||
105 (argv[1][0] == _T('-') && argv[1][1] != _T('o')) ||
106 (argc == 2 && argv[1][0] == _T('-')) )
107 {
108 Usage();
109
110 return FALSE;
111 }
112
113 m_sampleName = argv[1];
114 if ( m_sampleName[0u] == _T('-') )
115 {
116 m_outputDir = argv[2];
117 if ( !wxEndsWithPathSeparator(m_outputDir) )
118 {
119 m_outputDir += _T('/');
120 }
121
122 m_sampleName = argv[3];
123 }
124
125 return TRUE;
126}
127
128int MakeGenApp::OnRun()
129{
130 m_dirname = wxGetenv(_T("MAKEGEN_PATH"));
131 if ( !m_dirname )
132 {
133 m_dirname = wxGetCwd();
134 }
135
136 if ( !wxEndsWithPathSeparator(m_dirname) )
137 {
138 m_dirname += _T('/');
139 }
140
141 m_dirname += _T("templates");
142
143 wxDir dir(m_dirname);
144
145 m_dirname += _T('/');
146
147 if ( !dir.IsOpened() )
148 {
149 wxLogError(_T("Aborting generating the makefiles."));
150
151 return 1;
152 }
153
154 wxString filename;
155 size_t n = 0;
156 bool cont = dir.GetFirst(&filename, _T("?akefile.*"), wxDIR_FILES);
157 while ( cont )
158 {
159 n++;
160
161 if ( !GenerateMakefile(filename) )
162 {
163 wxLogError(_T("Error during makefile generation, aborting."));
164
165 return 2;
166 }
167
168 cont = dir.GetNext(&filename);
169 }
170
171 if ( n )
172 {
173 wxLogVerbose(_T("Successfully generated %u makefiles in '%s'."),
174 n, m_outputDir.c_str());
175 }
176 else
177 {
178 wxLogWarning(_T("No makefiles found: either set MAKEGEN_PATH variable "
179 "or run the program from its directory"));
180 }
181
182 return 0;
183}