-
Notifications
You must be signed in to change notification settings - Fork 1
/
makefile3.cpp
189 lines (155 loc) · 4.19 KB
/
makefile3.cpp
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
// {"category": "Other", "notes": "Process makefile"}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include <vector>
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
//
// Given the following data structure for a makefile entry and support
// functions, please implement function:
// void Make(vector<Entry> entries, string target).
//
//------------------------------------------------------------------------------
class Entry
{
public:
string m_target;
vector<Entry> m_deps;
string m_command;
Entry(
string target,
vector<Entry> deps = vector<Entry>(),
string command = string())
: m_target(target), m_deps(deps), m_command(command)
{
}
};
bool Exists(string target)
{
return false;
}
void ExecuteCommand(string command)
{
cout << "Executing " << command.c_str() << "..." << endl;
}
//------------------------------------------------------------------------------
//
// Implementation
//
//------------------------------------------------------------------------------
void Make(Entry entry);
void Make(vector<Entry> entries, string target)
{
// Find the target in entries.
for (vector<Entry>::size_type i = 0; i < entries.size(); i++)
{
if (entries[i].m_target == target)
{
// Make the target.
Make(entries[i]);
return;
}
}
// Could not find the target.
throw exception();
}
void Make(Entry entry)
{
// Build the target if it does not already exist.
if (!Exists(entry.m_target))
{
// First make all dependencies.
for (vector<Entry>::size_type i = 0; i < entry.m_deps.size(); i++)
{
Make(entry.m_deps[i]);
}
// Make this target.
if (entry.m_command.c_str() != nullptr &&
strlen(entry.m_command.c_str()) != 0)
{
ExecuteCommand(entry.m_command);
}
}
}
//------------------------------------------------------------------------------
//
// Sample data and demo execution.
//
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
// all: hello
//
// hello : main.o factorial.o hello.o
// g++ main.o factorial.o hello.o - o hello
//
// main.o : main.cpp
// g++ - c main.cpp
//
// factorial.o : factorial.cpp
// g++ - c factorial.cpp
//
// hello.o : hello.cpp
// g++ - c hello.cpp
//
// clean :
// rm - rf *o hello
//
Entry clean(
string("clean"),
vector<Entry>(),
string("rm -rf *o hello"));
Entry hello_cpp(string("hello.cpp"));
vector<Entry> hello_o_deps;
hello_o_deps.push_back(hello_cpp);
Entry hello_o(
string("hello.o"),
hello_o_deps,
string("g++ -c hello.cpp"));
Entry factorial_cpp(string("factorial.cpp"));
vector<Entry> factorial_o_deps;
factorial_o_deps.push_back(factorial_cpp);
Entry factorial_o(
string("factorial.o"),
factorial_o_deps,
string("g++ -c factorial.cpp"));
Entry main_cpp(string("main.cpp"));
vector<Entry> main_o_deps;
main_o_deps.push_back(main_cpp);
Entry main_o(
string("main.o"),
main_o_deps,
string("g++ -c main.cpp"));
vector<Entry> hello_deps;
hello_deps.push_back(main_o);
hello_deps.push_back(factorial_o);
hello_deps.push_back(hello_o);
Entry hello(
string("hello"),
hello_deps,
string("g++ main.o factorial.o hello.o -o hello"));
vector<Entry> all_deps;
all_deps.push_back(hello);
Entry all(string("all"), all_deps);
vector<Entry> entries;
entries.push_back(clean);
entries.push_back(hello_cpp);
entries.push_back(hello_o);
entries.push_back(factorial_cpp);
entries.push_back(factorial_o);
entries.push_back(main_cpp);
entries.push_back(main_o);
entries.push_back(hello);
entries.push_back(all);
try
{
Make(entries, string("all"));
}
catch (exception)
{
cout << "error" << endl;
}
return 0;
}