1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// Copyright (c) 1995 David Engberg All rights reserved
// $Id: CompilerMain.C,v 1.10 1997/11/10 00:48:06 geppetto Exp $
#include "unicode_string.h"
#include "Compiler.h"
#include "CommandLine.h"
#include "JavaClassFile.h"
#include "FilePath.h"
#include "JavaDirectory.h"
#include "config.h"
#include <cstdio>
#include <iostream.h>
#include <fstream.h>
extern "C" {
#include <stdlib.h>
}
#if defined(__OS2__) || defined(__EMX__)
#define PATHSEPCHAR ';'
#else
#define PATHSEPCHAR ':'
#endif
static const string kSourceSuffix = ".java";
void SyntaxAbort(const string& command)
{
cerr << ">> Syntax: " << command
<< " [-version] [-classpath <path>] [-d <class-directory>] [-M] "
<< "<input-file> [<input-file>]*" << endl;
exit(1);
}
bool ParseClassPath(const string& pathString, deque<string>& pathList)
{
string::size_type startPosition = 0;
string::size_type colonPosition;
do {
colonPosition = pathString.find(PATHSEPCHAR, startPosition);
if (colonPosition > pathString.size()) {
colonPosition = pathString.size();
}
string directoryName(pathString, startPosition,
colonPosition - startPosition);
CJavaDirectory path(directoryName);
if (path.IsValid()) {
pathList.push_back(directoryName);
} else {
cerr << ">> Warning: Invalid classpath entry " << directoryName << endl;
}
startPosition = colonPosition + 1;
} while(colonPosition < pathString.size());
return !pathList.empty();
}
bool MakeDirectories(const string& fileName) {
string::size_type slash = 0;
while ((slash = fileName.find_first_of('/', slash + 1)) != string::npos) {
string directoryString = fileName.substr(0, slash);
CFilePath path(directoryString);
if (!path.Exists()) {
if (!path.MakeDirectory()) {
cerr << ">> Could not create directory: " << directoryString << endl;
return false;
}
}
}
return true;
}
int
main(int argc, const char** argv) {
CCommandLine arguments;
arguments.ParseArgumentVector(argc, argv);
deque<string> sourceFiles;
string classPathString;
CFilePath* classDestinationBase = 0;
bool dependMode = false;
if (arguments.GetArgumentCount() < 1) {
SyntaxAbort(arguments.GetCommand());
}
for (CCommandLine::ArgumentIteratorType arg = arguments.GetArgumentBegin();
arg != arguments.GetArgumentEnd(); ++arg) {
unsigned long optionLength = (*arg).length();
if (*arg == "-classpath") {
++arg;
if (arg == arguments.GetArgumentEnd()) {
SyntaxAbort(arguments.GetCommand());
} else {
classPathString = *arg;
}
} else if (*arg == "-version") {
cerr << "guavac version " << VERSION << endl;
} else if (*arg == "-M") {
dependMode = true;
} else if (*arg == "-d") {
++arg;
if (arg == arguments.GetArgumentEnd()) {
SyntaxAbort(arguments.GetCommand());
} else {
delete classDestinationBase;
classDestinationBase = new CFilePath(*arg);
if (!classDestinationBase->IsDirectory()) {
cerr << ">> Invalid destination directory: " << *arg << endl;
delete classDestinationBase;
classDestinationBase = 0;
}
}
} else if (optionLength > 0 && (*arg)[0] == '-') {
cerr << ">> Invalid compiler flag: " << *arg << endl;
SyntaxAbort(arguments.GetCommand());
} else {
CFilePath filePath(*arg);
if (!filePath.IsFile()) {
cerr << ">> Invalid argument (not a plain file): " << *arg << endl;
SyntaxAbort(arguments.GetCommand());
} else if (optionLength < kSourceSuffix.length() ||
(*arg).compare(".java",
optionLength - kSourceSuffix.length()) != 0) {
cerr << ">> Invalid source file (no .java suffix): " << *arg << endl;
SyntaxAbort(arguments.GetCommand());
} else {
sourceFiles.push_back(*arg);
}
}
}
if (classPathString.size() == 0) {
const char* classPathEnvironment = getenv("CLASSPATH");
if (classPathEnvironment == 0) {
classPathString = DEFAULT_CLASSPATH;
} else {
classPathString = classPathEnvironment;
}
}
if (sourceFiles.size() == 0) {
cerr << ">> No valid source file names given." << endl;
exit(1);
}
deque<string> classPath;
if (! ParseClassPath(classPathString, classPath)) {
cerr << ">> Invalid class path: " << classPathString << endl;
exit(1);
}
bool success = true;
for (deque<string>::const_iterator sourceIterator = sourceFiles.begin();
success && !(sourceIterator == sourceFiles.end()); ++sourceIterator) {
string fileName = *sourceIterator;
string::size_type lastSlash = fileName.find_last_of('/');
if (lastSlash == string::npos) {
lastSlash = 0;
} else {
lastSlash++;
}
string fileDirectory(fileName, 0, lastSlash);
CCompiler::ClassList resultClasses;
deque<string> depends;
success &=
CCompiler::CompileFile(fileName, classPath, resultClasses, depends);
if (success) {
if (!dependMode) {
cout << "Compilation Successful. Classes/interfaces found:" << endl;
}
for (CCompiler::ClassList::iterator i = resultClasses.begin();
i != resultClasses.end(); ++i) {
string className = ::UnicodeToString((*i)->GetShortName());
string outFileName;
if (classDestinationBase == 0) {
outFileName = fileDirectory + className + ".class";
} else {
outFileName = classDestinationBase->GetString() + "/" +
::UnicodeToString((*i)->GetClassName()) + ".class";
}
if (dependMode) {
cout << outFileName << ":";
for (deque<string>::iterator iter = depends.begin();
!(iter == depends.end()); ++iter) {
cout << " \\\n " << *iter;
}
cout << endl;
} else if (MakeDirectories(outFileName)) {
ofstream outFile;
outFile.open(outFileName.c_str(), ios::out | ios::bin | ios::trunc);
if (!outFile.is_open()) {
cerr << ">> could not open output file " << outFileName << endl;
} else {
if (!(*i)->IsInner()) {
cout << " * " << className << endl;
}
(*i)->Compile(outFile);
outFile.close();
}
}
}
} else {
cerr << "Compilation Failed" << endl;
}
for (CCompiler::ClassList::iterator i = resultClasses.begin();
i != resultClasses.end(); ++i) {
delete *i;
}
}
return success ? 0 : 1;
}
|