From 2c9089ce0b7899e3ab3a75cc018fb69dfafea96e Mon Sep 17 00:00:00 2001
From: geigle1 <geigle1@6fbd10e7-183d-0410-a318-cb416676e4f2>
Date: Thu, 13 Sep 2012 05:20:14 +0000
Subject: [PATCH] Update monad with ccmonad change.

git-svn-id: https://subversion.cs.illinois.edu/svn/cs225@7744 6fbd10e7-183d-0410-a318-cb416676e4f2
---
 source/ccmonad.cpp | 150 ++++++++++++++++++++++++++-------------------
 1 file changed, 86 insertions(+), 64 deletions(-)

diff --git a/source/ccmonad.cpp b/source/ccmonad.cpp
index 4410192..5bf482e 100644
--- a/source/ccmonad.cpp
+++ b/source/ccmonad.cpp
@@ -29,7 +29,7 @@ int main(int argc, const char * const * argv)
 		exit(1);
 	}
 	string compiler = argv[1];
-	string inputFile;
+    vector<string> inputFiles;
 	vector<string> options;
 	string outputFile;
 	bool isNextOutput = false;
@@ -51,6 +51,8 @@ int main(int argc, const char * const * argv)
 					outputFile = argv[i];
 				else
 				{
+                    inputFiles.push_back( argv[i] );
+                    /*
 					if (inputFile == "")
 						inputFile = argv[i];
 					else
@@ -58,82 +60,102 @@ int main(int argc, const char * const * argv)
 						cerr << "cc-monad: error: cc-monad can only take one input file" << endl;
 						exit(1);
 					}
+                    */
 				}
 			}
 		}
 	}
 
-	if (outputFile == "")
-	{
-		outputFile = inputFile.substr(0, inputFile.find_last_of(".")) + ".o";
-	}
-	if (inputFile == "")
-	{
-		cerr << "cc-monad: usage error" << endl;
-		exit(1);
-	}
+    if (outputFile != "" && inputFiles.size() != 1) {
+        cerr << "cc-monad: error: cc-monad can only take one input file when given an explicit output file" << endl;
+        exit(1);
+    }
+
+    for (size_t i = 0; i < inputFiles.size(); ++i )
+    {
+        string inputFile = inputFiles[i];
+        if (outputFile == "")
+        {
+            size_t dot = inputFile.find_last_of(".");
+            string extension = inputFile.substr(dot, inputFile.length());
+            string name = inputFile.substr(0, dot);
+            if (extension == ".h" || extension == ".hpp")
+                outputFile = name + extension + ".gch";
+            else
+                outputFile = name + ".o";
+        }
+        if (inputFile == "")
+        {
+            cerr << "cc-monad: usage error" << endl;
+            continue;
+        }
 #if 0
-	cout << "Compiler: " << compiler << endl;
-	cout << "Input: " << inputFile << endl;
-	cout << "Output: " << outputFile << endl;
+        cout << "Compiler: " << compiler << endl;
+        cout << "Input: " << inputFile << endl;
+        cout << "Output: " << outputFile << endl;
 #endif
 
-	if (!exists(inputFile))
-	{
-		cerr << "cc-monad: error: file not found: " << inputFile << endl;
-		exit(1);
-	}
+        if (!exists(inputFile))
+        {
+            cerr << "cc-monad: error: file not found: " << inputFile << endl;
+            continue;
+        }
 
-	vector<string> args1 = options;
-	args1.push_back("-c");
-	args1.push_back(inputFile);
-	args1.push_back("-o");
-	args1.push_back(outputFile);
-	int8_t result1 = exec(compiler, args1);
-	if (result1 != 0)
-	{
-		vector<size_t> splits;
-		splits.push_back(0); // for compiling something always (should be empty)
+        vector<string> args1 = options;
+        args1.push_back("-c");
+        args1.push_back(inputFile);
+        args1.push_back("-o");
+        args1.push_back(outputFile);
+        int8_t result1 = exec(compiler, args1);
+        if (result1 != 0)
+        {
+            vector<size_t> splits;
+            splits.push_back(0); // for compiling something always (should be empty)
 
-		ifstream input(inputFile.c_str());
-		for (size_t line_i = 1; input.good(); line_i++)
-		{
-			string line;
-			getline(input, line);
-			vector<string> tokens = tokenize(line, " \t");
-			if (tokens.size() >= 2 && tokens[0] == "#if" && tokens[1] == "MONAD_SPLIT")
-				splits.push_back(line_i);
-		}
+            ifstream input(inputFile.c_str());
+            for (size_t line_i = 1; input.good(); line_i++)
+            {
+                string line;
+                getline(input, line);
+                vector<string> tokens = tokenize(line, " \t");
+                if (tokens.size() >= 2 && tokens[0] == "#if" && tokens[1] == "MONAD_SPLIT")
+                    splits.push_back(line_i);
+            }
 
-		vector<string> intermediate_outputs; // = options;
-		//intermediate_outputs.push_back("-shared");
-		intermediate_outputs.push_back("-r");
-		for (size_t split_i = 0; split_i < splits.size(); split_i++)
-		{
-			vector<string> args = options;
-			args.push_back("-c");
-			args.push_back(inputFile);
-			args.push_back("-DMONAD_SPLIT_LINE_NUMBER=" + lexical_cast<string>(splits[split_i]));
-			args.push_back("-o");
-			args.push_back(outputFile + "." + split_i);
-		
+            vector<string> intermediate_outputs; // = options;
+            //intermediate_outputs.push_back("-shared");
+            intermediate_outputs.push_back("-r");
+            for (size_t split_i = 0; split_i < splits.size(); split_i++)
+            {
+                vector<string> args = options;
+                args.push_back("-c");
+                args.push_back(inputFile);
+                args.push_back("-DMONAD_SPLIT_LINE_NUMBER=" + lexical_cast<string>(splits[split_i]));
+                args.push_back("-o");
+                args.push_back(outputFile + "." + split_i);
+            
 #if 0
-			cout << "\tmonacc: " << compiler;
-			for (size_t i = 0; i < args.size(); i++)
-				cout << ' ' << args[i];
-			cout << endl;
+                cout << "\tmonacc: " << compiler;
+                for (size_t i = 0; i < args.size(); i++)
+                    cout << ' ' << args[i];
+                cout << endl;
 #endif
 
-			int8_t result2 = exec(compiler, args);
-			if (result2 == 0)
-				intermediate_outputs.push_back(outputFile + "." + split_i);
-		}
-		intermediate_outputs.push_back("-o");
-		intermediate_outputs.push_back(outputFile);
+                int8_t result2 = exec(compiler, args);
+                if (result2 == 0)
+                    intermediate_outputs.push_back(outputFile + "." + split_i);
+            }
+            intermediate_outputs.push_back("-o");
+            intermediate_outputs.push_back(outputFile);
 
-		return exec("ld", intermediate_outputs);
-		//return exec("mv", intermediate_outputs[2], outputFile);
-	}
-	else
-		return 0;
+            exec("ld", intermediate_outputs);
+            //return exec("ld", intermediate_outputs);
+            //return exec("mv", intermediate_outputs[2], outputFile);
+        }
+        // reset the output file if we are running multiple times: this
+        // case only happens when there are multiple implied output files,
+        // so resetting the output file name is both safe and correct
+        outputFile = "";
+    }
+    return 0;
 }
-- 
GitLab