Newer
Older
#include "util.h"
using namespace std;
using namespace util;
int main(int argc, const char * const * argv)
{
bool isCompileOnly = false;
for (int i = 2; i < argc; i++)
if (strcmp(argv[i], "-c") == 0)
isCompileOnly = true;
if (!isCompileOnly) // not really using cc-monad, just linking
{
if (argc < 3)
{
cerr << "cc-monad: usage error" << endl;
exit(1);
}
vector<string> linkargs(argv + 2, argv + argc);
exec(argv[1], linkargs);
exit(0);
}
if (argc < 4)
{
cerr << "cc-monad: usage error" << endl;
exit(1);
}
string compiler = argv[1];
string inputFile;
vector<string> options;
string outputFile;
bool isNextOutput = false;
for (int32_t i = 2; i < argc; i++)
{
if (strlen(argv[i]) > 0)
{
if (argv[i][0] == '-')
{
if (strlen(argv[i]) > 1 && argv[i][1] == 'o')
isNextOutput = true;
else if (strcmp(argv[i], "-c") != 0)
options.push_back(argv[i]);
}
else
{
if (isNextOutput)
outputFile = argv[i];
else
{
if (inputFile == "")
inputFile = argv[i];
else
{
cerr << "cc-monad: error: cc-monad can only take one input file" << endl;
exit(1);
}
}
}
}
}
{
outputFile = inputFile.substr(0, inputFile.find_last_of(".")) + ".o";
}
if (inputFile == "")
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
{
cerr << "cc-monad: usage error" << endl;
exit(1);
}
#if 0
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);
}
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);
}
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;
#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);
return exec("ld", intermediate_outputs);
//return exec("mv", intermediate_outputs[2], outputFile);
}
else
return 0;
}