-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakefile2.cpp
150 lines (121 loc) · 3.35 KB
/
makefile2.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
// {"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 a function to build a 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)
{
// 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);
Make(all);
return 0;
}