2000-01-03 13:45:00 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: utils/makegen/makegen.cpp
|
|
|
|
// Purpose: a tool to generate the makefiles for samples
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 03.01.00
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-01-05 18:10:06 +00:00
|
|
|
/*
|
|
|
|
* TODO
|
|
|
|
*
|
|
|
|
* 1. support for programs with multiple object files
|
|
|
|
* 2. support for programs under utils and demos, not only samples
|
|
|
|
*/
|
|
|
|
|
2000-01-03 13:45:00 +00:00
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
#if wxUSE_GUI
|
|
|
|
#error "This is a console mode program and must be linked with wxBase."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2001-11-22 22:03:26 +00:00
|
|
|
#include "wx/string.h"
|
|
|
|
#include "wx/file.h"
|
|
|
|
#include "wx/ffile.h"
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/log.h"
|
|
|
|
#include "wx/dir.h"
|
|
|
|
#include "wx/textfile.h"
|
|
|
|
#include "wx/datetime.h"
|
|
|
|
#include "wx/cmdline.h"
|
2000-01-03 13:45:00 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MakeGenApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
2000-01-05 18:10:06 +00:00
|
|
|
MakeGenApp() { m_quiet = FALSE; }
|
|
|
|
|
2000-01-03 13:45:00 +00:00
|
|
|
virtual bool OnInit();
|
|
|
|
|
|
|
|
virtual int OnRun();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool GenerateMakefile(const wxString& filename);
|
|
|
|
|
2000-01-05 18:10:06 +00:00
|
|
|
wxString m_progname, // the name of the sample
|
2000-01-03 13:45:00 +00:00
|
|
|
m_dirname, // directory with the template files
|
2000-01-05 18:10:06 +00:00
|
|
|
m_outdir; // directory to output files to
|
|
|
|
|
|
|
|
bool m_quiet; // don't give non essential messages
|
2000-01-03 13:45:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MakeGenApp);
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MakeGenApp
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool MakeGenApp::GenerateMakefile(const wxString& filename)
|
|
|
|
{
|
|
|
|
wxTextFile fileIn(m_dirname + filename);
|
|
|
|
if ( !fileIn.Open() )
|
|
|
|
{
|
|
|
|
wxLogError(_T("Makefile '%s' couldn't be generated."), filename.c_str());
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-02-04 20:27:10 +00:00
|
|
|
wxString fileOutName;
|
|
|
|
fileOutName << m_outdir << _T('/') << filename;
|
|
|
|
wxFFile fileOut(fileOutName, "w");
|
2000-01-03 13:45:00 +00:00
|
|
|
if ( !fileOut.IsOpened() )
|
|
|
|
{
|
|
|
|
wxLogError(_T("Makefile '%s' couldn't be generated."), filename.c_str());
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-01-05 18:10:06 +00:00
|
|
|
wxLogVerbose(_T("Generating '%s' for '%s'..."),
|
2000-02-04 20:27:10 +00:00
|
|
|
fileOutName.c_str(), m_progname.c_str());
|
2000-01-05 18:10:06 +00:00
|
|
|
|
2000-01-03 13:45:00 +00:00
|
|
|
size_t count = fileIn.GetLineCount();
|
|
|
|
for ( size_t n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
wxString line = fileIn[n];
|
|
|
|
|
|
|
|
line.Replace(_T("#DATE"), wxDateTime::Now().FormatISODate());
|
2000-01-05 18:10:06 +00:00
|
|
|
line.Replace(_T("#NAME"), m_progname);
|
2000-01-03 13:45:00 +00:00
|
|
|
|
|
|
|
fileOut.Write(line + _T('\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-01-05 18:10:06 +00:00
|
|
|
// parse the cmd line
|
2000-01-03 13:45:00 +00:00
|
|
|
bool MakeGenApp::OnInit()
|
|
|
|
{
|
2000-01-05 18:10:06 +00:00
|
|
|
static const wxCmdLineEntryDesc cmdLineDesc[] =
|
2000-01-03 13:45:00 +00:00
|
|
|
{
|
2000-01-05 18:10:06 +00:00
|
|
|
{ wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("give this usage message"),
|
|
|
|
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be more verbose") },
|
|
|
|
{ wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") },
|
2000-01-03 13:45:00 +00:00
|
|
|
|
2000-01-05 18:10:06 +00:00
|
|
|
{ wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("directory with template files") },
|
|
|
|
|
|
|
|
{ wxCMD_LINE_PARAM, NULL, NULL, _T("output_directory") },
|
|
|
|
|
|
|
|
{ wxCMD_LINE_NONE }
|
|
|
|
};
|
|
|
|
|
|
|
|
wxCmdLineParser parser(cmdLineDesc, argc, argv);
|
|
|
|
parser.SetLogo(_T("MakeGen: a makefile generator for wxWindows\n"
|
|
|
|
"Copyright (c) 2000 Vadim Zeitlin"));
|
|
|
|
|
|
|
|
if ( parser.Parse() != 0 )
|
|
|
|
{
|
|
|
|
// failed to parse the cmd line or help was requested (and given)
|
2000-01-03 13:45:00 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-01-05 18:10:06 +00:00
|
|
|
(void)parser.Found(_T("i"), &m_dirname);
|
|
|
|
if ( parser.Found(_T("q")) )
|
2000-01-03 13:45:00 +00:00
|
|
|
{
|
2000-01-05 18:10:06 +00:00
|
|
|
m_quiet = TRUE;
|
|
|
|
|
|
|
|
wxLog::GetActiveTarget()->SetVerbose(FALSE);
|
|
|
|
}
|
|
|
|
else if ( parser.Found(_T("v")) )
|
|
|
|
{
|
|
|
|
wxLog::GetActiveTarget()->SetVerbose();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_outdir = parser.GetParam();
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
m_outdir.Replace(_T("\\"), _T("/"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ( !!m_outdir && m_outdir.Last() == _T('/') )
|
|
|
|
{
|
|
|
|
m_outdir.Truncate(m_outdir.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_progname = m_outdir.AfterLast(_T('/'));
|
|
|
|
|
|
|
|
if ( !m_progname )
|
|
|
|
{
|
|
|
|
wxLogError(_T("Output directory should be specified."));
|
2000-01-03 13:45:00 +00:00
|
|
|
|
2000-01-05 18:10:06 +00:00
|
|
|
parser.Usage();
|
|
|
|
|
|
|
|
return FALSE;
|
2000-01-03 13:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MakeGenApp::OnRun()
|
|
|
|
{
|
|
|
|
if ( !m_dirname )
|
|
|
|
{
|
2000-01-05 18:10:06 +00:00
|
|
|
m_dirname = wxGetenv(_T("MAKEGEN_PATH"));
|
|
|
|
if ( !m_dirname )
|
|
|
|
{
|
|
|
|
m_dirname = wxGetCwd();
|
|
|
|
}
|
2000-01-03 13:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !wxEndsWithPathSeparator(m_dirname) )
|
|
|
|
{
|
|
|
|
m_dirname += _T('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dirname += _T("templates");
|
|
|
|
|
|
|
|
wxDir dir(m_dirname);
|
|
|
|
|
|
|
|
m_dirname += _T('/');
|
|
|
|
|
|
|
|
if ( !dir.IsOpened() )
|
|
|
|
{
|
|
|
|
wxLogError(_T("Aborting generating the makefiles."));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString filename;
|
|
|
|
size_t n = 0;
|
2000-01-05 18:10:06 +00:00
|
|
|
bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES);
|
2000-01-03 13:45:00 +00:00
|
|
|
while ( cont )
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
|
|
|
|
if ( !GenerateMakefile(filename) )
|
|
|
|
{
|
|
|
|
wxLogError(_T("Error during makefile generation, aborting."));
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
cont = dir.GetNext(&filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( n )
|
|
|
|
{
|
|
|
|
wxLogVerbose(_T("Successfully generated %u makefiles in '%s'."),
|
2000-01-05 18:10:06 +00:00
|
|
|
n, m_outdir.c_str());
|
2000-01-03 13:45:00 +00:00
|
|
|
}
|
2000-01-05 18:10:06 +00:00
|
|
|
else if ( !m_quiet )
|
2000-01-03 13:45:00 +00:00
|
|
|
{
|
|
|
|
wxLogWarning(_T("No makefiles found: either set MAKEGEN_PATH variable "
|
|
|
|
"or run the program from its directory"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|