Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 84 → Rev 85

/trunk/C-CPP-utils/supermakemake/MakefileObject.cpp
81,30 → 81,32
 
Makefile::Makefile( const char* makefile, map<string,string>* inConfig ){
ifstream theFile( makefile );
char* buffer;
configuration = inConfig;
 
initializeMakefileVariables();
 
char* buffer;
buffer = (char*)malloc( 300 );
 
initializeMakefileVariables();
 
if( theFile.fail() ){
cerr << "ERROR: Couldn't open file " << makefile << endl;
return;
}
 
string* lastUsedVariable = NULL;
set<string>* lastUsedList = NULL;
bool append = false;
char* equalsLocation;
parseMakefileVariables( &theFile );
theFile.seekg( 0 );
 
char* equalsLocation = NULL;
bool appendTarget = false;
string* targetName = NULL;
while( theFile.good() ){
theFile.getline( buffer, 300 );
 
if( equalsLocation = strstr( buffer, "=" ) ){
equalsLocation = strstr( buffer, "=" );
if( equalsLocation != NULL && buffer[ 0 ] != '\t' ){
string beginning( buffer, equalsLocation );
string end( equalsLocation + 1 );
if( end[ end.size() ] == '\\' ){
append = true;
end.resize( end.size() - 1 );
}
beginning = trim( beginning );
115,100 → 117,30
}
}
 
//if the append variable is set, we have not reached the end of our line
if( append ){
if( lastUsedVariable != NULL ){
//append to our string variable
lastUsedVariable->append( parseList( buffer ) );
}else if( lastUsedList != NULL ){
//append to our set variable
parseFileList( buffer, lastUsedList );
}
 
//if we still haven't reached the end of our line,
//loop again
if( theFile.gcount() >= 300 ){
if( appendTarget ){
if( buffer[ 0 ] == '\t' ){
//this is still part of our target, so append it
string tmpLine( buffer );
if( targetName == NULL ){
cerr << "ERROR: target is null!" << endl;
continue;
}
makefileTargets[ *targetName ].push_back( tmpLine );
}else if( targetName == NULL ) {
targetName = new string( buffer );
continue;
}else{
//we must be finished; delete the target name and continue on
delete targetName;
appendTarget = false;
}
 
lastUsedVariable = NULL;
lastUsedList = NULL;
append = false;
}
 
 
//if we have more than 300 characters, OR the last character on the line is a \,
//we go onto the next line
if( theFile.gcount() >= 300 || buffer[ theFile.gcount() ] == '\\' ){
append = true;
if( memcmp( buffer, "# makemake keep target", 22 ) == 0 ){
//keep this target for output
appendTarget = true;
}
 
if( memcmp( buffer, "CPP_FILES", 9 ) == 0 ){
parseFileList( buffer, &cpp_files );
lastUsedList = &cpp_files;
}
 
if( memcmp( buffer, "H_FILES", 7 ) == 0 ){
parseFileList( buffer, &h_files );
lastUsedList = &h_files;
}
 
if( memcmp( buffer, "C_FILES", 7 ) == 0 ){
parseFileList( buffer, &c_files );
lastUsedList = &c_files;
}
 
if( memcmp( buffer, "PS_FILES", 8 ) == 0 ){
parseFileList( buffer, &ps_files );
lastUsedList = &ps_files;
}
 
if( memcmp( buffer, "S_FILES", 7 ) == 0 ){
parseFileList( buffer, &s_files );
lastUsedList = &s_files;
}
 
if( memcmp( buffer, "CPPFLAGS", 8 ) == 0 ){
cpp_flags = parseList( buffer );
lastUsedVariable = &cpp_flags;
}
 
if( memcmp( buffer, "CFLAGS", 6 ) == 0 ){
c_flags = parseList( buffer );
lastUsedVariable = &c_flags;
}
 
if( memcmp( buffer, "CXXFLAGS", 8 ) == 0 ){
cxx_flags = parseList( buffer );
lastUsedVariable = &cxx_flags;
}
 
if( memcmp( buffer, "CLIBFLAGS", 9 ) == 0 ){
clib_flags = parseList( buffer );
lastUsedVariable = &clib_flags;
}
if( memcmp( buffer, "CCLIBFLAGS", 10 ) == 0 ){
cxxlib_flags = parseList( buffer );
lastUsedVariable = &cxxlib_flags;
}
 
if( memcmp( buffer, "CPPFLAGS", 8 ) == 0 ){
cpp_flags = parseList( buffer );
lastUsedVariable = &cpp_flags;
}
if( memcmp( buffer, "CC =", 4 ) == 0 ){
string parsed = parseList( buffer );
(*configuration)["c_compiler"] = trim( parsed );
}
 
if( memcmp( buffer, "CXX =", 5 ) == 0 ){
string parsed = parseList( buffer );
(*configuration)["cpp_compiler"] = trim( parsed );
}
 
/*
printf("BUFFER IS |%s|\n", buffer );
fflush(stdout);
217,13 → 149,6
 
free( buffer );
 
//add in the variables that we parsed
makefileVariables[ "CPPFLAGS" ] = cpp_flags;
makefileVariables[ "CFLAGS" ] = c_flags;
makefileVariables[ "CXXFLAGS" ] = cxx_flags;
makefileVariables[ "CLIBFLAGS" ] = clib_flags;
makefileVariables[ "CCLIBFLAGS" ] = cxxlib_flags;
 
remove_old_files();
}
 
261,6 → 186,7
write_variables();
write_lists();
write_main_targets();
writeCustomTargets();
write_dependencies();
write_trailer( (*configuration)["c_compiler"] == "gcc" );
}
428,6 → 354,8
ostreambuf_iterator<char>(cout));
cout << '\n'; // in case no end-of-line at end of file
cout << "########## End of flags from " << (*configuration)["header_file"] << "\n\n";
ifstream headerMak( (*configuration)["header_file"].c_str() );
parseMakefileVariables( &headerMak );
} else {
cout << "########## Default flags (redefine these with a header.mak file if desired)\n";
cout << "CPPFLAGS = \t" << makefileVariables[ "CPPFLAGS" ] << "\n";
635,7 → 563,7
 
cout << basename << ":\t";
cout << basename << ".o $(OBJFILES)\n";
cout << "$(CC) -shared -Wl,-soname," << basename << ".so -o $(OBJFILES)\n";
cout << "$(CC) -shared -Wl,-soname," << basename << ".so.$(MAJOR).$(MINOR) -o $(OBJFILES)\n";
}
}
 
672,20 → 600,19
//Child process
dup2( fileds, STDOUT_FILENO ); //make output go to pipe instead of STDOUT
 
set<string> allDefines;
list<string> cppFlags;
 
printf("cppflags: %s\n", cpp_flags.c_str() );
 
char* cstrCppFlags = (char*)malloc( cpp_flags.size() );
memcpy( cstrCppFlags, cpp_flags.c_str(), cpp_flags.size() + 1 );
char* define = strtok( cstrCppFlags, " " );
while( define != NULL && define < ( cstrCppFlags + cpp_flags.size() ) ){
if( define[ 0 ] == '-' && define[ 1 ] == 'D' ){
//this is a define
allDefines.insert( string( define ) );
}
define = strtok( NULL, " " );
char* flag = strtok( cstrCppFlags, " " );
while( flag != NULL && flag < ( cstrCppFlags + cpp_flags.size() ) ){
cppFlags.push_back( string( flag ) );
flag = strtok( NULL, " " );
}
 
int totalElements = cpp_files.size() + c_files.size() + allDefines.size() + 3; //all c/cpp files, plus three for "gcc" "-MM" [defines] [args] NULL
int totalElements = cpp_files.size() + c_files.size() + cppFlags.size() + 3; //all c/cpp files, plus three for "gcc" "-MM" [defines] [args] NULL
char** argArray = (char**)malloc( sizeof(char*) * totalElements );
int currentArgSpot = 0;
 
698,8 → 625,8
argArray[currentArgSpot++] = gcc;
argArray[currentArgSpot++] = mm;
//Now, let's add in each define statement
for( set<string>::iterator it = allDefines.begin(); it != allDefines.end(); it++ ){
//Now, let's add in each preprocessor flag
for( list<string>::iterator it = cppFlags.begin(); it != cppFlags.end(); it++ ){
char* thedefine = (char*)malloc( (*it).size() + 1 );
memcpy( thedefine, (*it).c_str(), (*it).size() + 1 );
argArray[currentArgSpot++] = thedefine;
720,6 → 647,11
}
argArray[currentArgSpot] = NULL;
 
currentArgSpot = 0;
while( argArray[ currentArgSpot ] != NULL ){
printf( "%s\n", argArray[ currentArgSpot++ ] );
}
 
execvp( "gcc", argArray );
 
//should not get here
1014,3 → 946,181
standardVariables.insert( "SOURCEFILES" );
standardVariables.insert( "S_FILES" );
}
 
void Makefile::writeCustomTargets(){
map<string,list<string> >::iterator iter;
list<string>:: iterator lIter;
 
for( iter = makefileTargets.begin(); iter != makefileTargets.end(); iter++ ){
cout << "# makemake keep target" << endl;
cout << iter->first << endl;
for( lIter = iter->second.begin(); lIter != iter->second.end(); lIter++ ){
cout << *lIter << endl;
}
}
}
 
void Makefile::parseMakefileVariables( ifstream* theFile ){
char* buffer;
buffer = (char*)malloc( 300 );
 
if( theFile->fail() ){
cerr << "ERROR: Couldn't open file " << theFile << endl;
return;
}
 
string* lastUsedVariable = NULL;
set<string>* lastUsedList = NULL;
bool append = false;
char* equalsLocation;
bool appendTarget = false;
string* targetName = NULL;
while( theFile->good() ){
theFile->getline( buffer, 300 );
 
equalsLocation = strstr( buffer, "=" );
if( equalsLocation != NULL && buffer[ 0 ] != '\t' ){
string beginning( buffer, equalsLocation );
string end( equalsLocation + 1 );
if( end[ end.size() ] == '\\' ){
append = true;
end.resize( end.size() - 1 );
}
beginning = trim( beginning );
end = trim( end );
if( standardVariables.find( beginning ) == standardVariables.end() ){
//cout << "inserting |" << beginning << "| => |" << end << "|\n";
makefileVariables.insert( pair<string,string>( beginning, end ) );
}
}
 
//if the append variable is set, we have not reached the end of our line
if( append ){
if( lastUsedVariable != NULL ){
//append to our string variable
lastUsedVariable->append( parseList( buffer ) );
}else if( lastUsedList != NULL ){
//append to our set variable
parseFileList( buffer, lastUsedList );
}
 
//if we still haven't reached the end of our line,
//loop again
if( theFile->gcount() >= 300 ){
continue;
}
 
lastUsedVariable = NULL;
lastUsedList = NULL;
append = false;
}
 
if( appendTarget ){
if( buffer[ 0 ] == '\t' ){
//this is still part of our target, so append it
string tmpLine( buffer );
if( targetName == NULL ){
cerr << "ERROR: target is null!" << endl;
continue;
}
makefileTargets[ *targetName ].push_back( tmpLine );
}else if( targetName == NULL ) {
targetName = new string( buffer );
continue;
}else{
//we must be finished; delete the target name and continue on
delete targetName;
appendTarget = false;
}
}
 
//if we have more than 300 characters, OR the last character on the line is a \,
//we go onto the next line
if( theFile->gcount() >= 300 || buffer[ theFile->gcount() ] == '\\' ){
append = true;
}
 
if( memcmp( buffer, "CPP_FILES", 9 ) == 0 ){
parseFileList( buffer, &cpp_files );
lastUsedList = &cpp_files;
}
 
if( memcmp( buffer, "H_FILES", 7 ) == 0 ){
parseFileList( buffer, &h_files );
lastUsedList = &h_files;
}
 
if( memcmp( buffer, "C_FILES", 7 ) == 0 ){
parseFileList( buffer, &c_files );
lastUsedList = &c_files;
}
 
if( memcmp( buffer, "PS_FILES", 8 ) == 0 ){
parseFileList( buffer, &ps_files );
lastUsedList = &ps_files;
}
 
if( memcmp( buffer, "S_FILES", 7 ) == 0 ){
parseFileList( buffer, &s_files );
lastUsedList = &s_files;
}
 
if( memcmp( buffer, "CPPFLAGS", 8 ) == 0 ){
cpp_flags = parseList( buffer );
lastUsedVariable = &cpp_flags;
}
 
if( memcmp( buffer, "CFLAGS", 6 ) == 0 ){
c_flags = parseList( buffer );
lastUsedVariable = &c_flags;
}
 
if( memcmp( buffer, "CXXFLAGS", 8 ) == 0 ){
cxx_flags = parseList( buffer );
lastUsedVariable = &cxx_flags;
}
 
if( memcmp( buffer, "CLIBFLAGS", 9 ) == 0 ){
clib_flags = parseList( buffer );
lastUsedVariable = &clib_flags;
}
if( memcmp( buffer, "CCLIBFLAGS", 10 ) == 0 ){
cxxlib_flags = parseList( buffer );
lastUsedVariable = &cxxlib_flags;
}
if( memcmp( buffer, "CC =", 4 ) == 0 ){
string parsed = parseList( buffer );
(*configuration)["c_compiler"] = trim( parsed );
}
 
if( memcmp( buffer, "CXX =", 5 ) == 0 ){
string parsed = parseList( buffer );
(*configuration)["cpp_compiler"] = trim( parsed );
}
 
 
if( memcmp( buffer, "# makemake keep target", 22 ) == 0 ){
//keep this target for output
appendTarget = true;
}
/*
printf("BUFFER IS |%s|\n", buffer );
fflush(stdout);
*/
}
 
free( buffer );
 
//add in the variables that we parsed
makefileVariables[ "CPPFLAGS" ] = cpp_flags;
makefileVariables[ "CFLAGS" ] = c_flags;
makefileVariables[ "CXXFLAGS" ] = cxx_flags;
makefileVariables[ "CLIBFLAGS" ] = clib_flags;
makefileVariables[ "CCLIBFLAGS" ] = cxxlib_flags;
 
 
}
/trunk/C-CPP-utils/supermakemake/MakefileObject.h
4,6 → 4,7
#include <set>
#include <string>
#include <map>
#include <list>
 
#include "dirlist.h"
 
65,6 → 66,16
*/
void initializeMakefileVariables();
 
/**
* Write out any custom targets that the user has specified
*/
void writeCustomTargets();
 
/**
* Parse the given file for makefile variables
*/
void parseMakefileVariables( std::ifstream* file );
 
private:
 
//Lists of files
97,6 → 108,8
//in the makefile. These may be
//standard variables, or they
//may be custom
std::map<std::string, std::list<std::string> > makefileTargets; //custom targets that we should keep
 
std::map<std::string, std::string>* configuration;
 
/trunk/C-CPP-utils/supermakemake/makemake.cpp
203,7 → 203,7
config["ar"] = "false";
config["so"] = "false";
config["install"] = "false";
config["prefix"] = "/usr/bin";
config["prefix"] = "/usr";
config["dirs"] = ".";
 
// check gnu compiler make or sun compiler make