1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils/makegen/makegen.cpp
3 // Purpose: a tool to generate the makefiles for samples
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
15 * 1. support for programs with multiple object files
16 * 2. support for programs under utils and demos, not only samples
19 // ============================================================================
21 // ============================================================================
24 #error "This is a console mode program and must be linked with wxBase."
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 #include "wx/string.h"
37 #include "wx/textfile.h"
38 #include "wx/datetime.h"
39 #include "wx/cmdline.h"
41 // ----------------------------------------------------------------------------
42 // the application class
43 // ----------------------------------------------------------------------------
45 class MakeGenApp
: public wxApp
48 MakeGenApp() { m_quiet
= FALSE
; }
50 virtual bool OnInit();
55 bool GenerateMakefile(const wxString
& filename
);
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
61 bool m_quiet
; // don't give non essential messages
64 IMPLEMENT_APP(MakeGenApp
);
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 bool MakeGenApp::GenerateMakefile(const wxString
& filename
)
76 wxTextFile
fileIn(m_dirname
+ filename
);
79 wxLogError(_T("Makefile '%s' couldn't be generated."), filename
.c_str());
85 fileOutName
<< m_outdir
<< _T('/') << filename
;
86 wxFFile
fileOut(fileOutName
, "w");
87 if ( !fileOut
.IsOpened() )
89 wxLogError(_T("Makefile '%s' couldn't be generated."), filename
.c_str());
94 wxLogVerbose(_T("Generating '%s' for '%s'..."),
95 fileOutName
.c_str(), m_progname
.c_str());
97 size_t count
= fileIn
.GetLineCount();
98 for ( size_t n
= 0; n
< count
; n
++ )
100 wxString line
= fileIn
[n
];
102 line
.Replace(_T("#DATE"), wxDateTime::Now().FormatISODate());
103 line
.Replace(_T("#NAME"), m_progname
);
105 fileOut
.Write(line
+ _T('\n'));
111 // parse the cmd line
112 bool MakeGenApp::OnInit()
114 static const wxCmdLineEntryDesc cmdLineDesc
[] =
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") },
121 { wxCMD_LINE_OPTION
, _T("i"), _T("input"), _T("directory with template files") },
123 { wxCMD_LINE_PARAM
, NULL
, NULL
, _T("output_directory") },
128 wxCmdLineParser
parser(cmdLineDesc
, argc
, argv
);
129 parser
.SetLogo(_T("MakeGen: a makefile generator for wxWindows\n"
130 "Copyright (c) 2000 Vadim Zeitlin"));
132 if ( parser
.Parse() != 0 )
134 // failed to parse the cmd line or help was requested (and given)
138 (void)parser
.Found(_T("i"), &m_dirname
);
139 if ( parser
.Found(_T("q")) )
143 wxLog::GetActiveTarget()->SetVerbose(FALSE
);
145 else if ( parser
.Found(_T("v")) )
147 wxLog::GetActiveTarget()->SetVerbose();
150 m_outdir
= parser
.GetParam();
153 m_outdir
.Replace(_T("\\"), _T("/"));
156 if ( !!m_outdir
&& m_outdir
.Last() == _T('/') )
158 m_outdir
.Truncate(m_outdir
.length() - 1);
161 m_progname
= m_outdir
.AfterLast(_T('/'));
165 wxLogError(_T("Output directory should be specified."));
175 int MakeGenApp::OnRun()
179 m_dirname
= wxGetenv(_T("MAKEGEN_PATH"));
182 m_dirname
= wxGetCwd();
186 if ( !wxEndsWithPathSeparator(m_dirname
) )
188 m_dirname
+= _T('/');
191 m_dirname
+= _T("templates");
193 wxDir
dir(m_dirname
);
195 m_dirname
+= _T('/');
197 if ( !dir
.IsOpened() )
199 wxLogError(_T("Aborting generating the makefiles."));
206 bool cont
= dir
.GetFirst(&filename
, wxEmptyString
, wxDIR_FILES
);
211 if ( !GenerateMakefile(filename
) )
213 wxLogError(_T("Error during makefile generation, aborting."));
218 cont
= dir
.GetNext(&filename
);
223 wxLogVerbose(_T("Successfully generated %u makefiles in '%s'."),
224 n
, m_outdir
.c_str());
228 wxLogWarning(_T("No makefiles found: either set MAKEGEN_PATH variable "
229 "or run the program from its directory"));