- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildfile.cpp
More file actions
Latest commit
144 lines (132 loc) · 4.56 KB
/
Copy pathBuildfile.cpp
File metadata and controls
144 lines (132 loc) · 4.56 KB
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
#include"Buildfile.h"
#include"Directory.h"
#include<regex>
#include<iostream>
usingnamespacestd;
usingnamespacestd::placeholders;
constchar* const compilecmd_c = "COMPILECMD:";
constint compilecmd_len_c = 11;
constchar* const linkcmd_c = "LINKCMD:";
constint linkcmd_len_c = 8;
constchar* const postlinkcmd_c = "POSTCMD:";
Buildfile::Buildfile() : MemoryMappedFile("Buildfile") {
auto it = begin();
while(!strncmp(it, compilecmd_c, compilecmd_len_c) || !strncmp(it, linkcmd_c, linkcmd_len_c))
it = skip_past(it, end()); //skip lines that begin with compile or link command keywords
if(it == end())
throwruntime_error("No executable name found!");
while(it != end() && *it != ':') //read from beginning of line until ':'
executable_name += *it++;
it = first_alnum_char_past_colon(it, end());
string regex_str;
while(it != end() && *it != '\n') {
if(isspace(*it)) {
cpp_regexs.emplace_back(move(regex_str));
regex_str.clear();
}
else
regex_str += *it;
++it;
}
if(!regex_str.empty())
cpp_regexs.emplace_back(regex_str);
Directory current_dir;
cpps = current_dir.matching_filenames(bind(&Buildfile::filter, this, _1));
transform(cpps.begin(), cpps.end(), back_inserter(objects), &Buildfile::cpp_to_object);
it = first_alnum_char_past(postlinkcmd_c);
if(it == end())
return;
it = first_alnum_char_past_colon(it, end());
while(*it != '\n')
postlink_cmd += *it++;
}
autoBuildfile::first_alnum_char_past_colon(iterator it, iterator end) const -> iterator {
while(it != end && (isspace(*it) || *it == ':'))
++it; //skip until first alnum char that isn't ':'
return it;
}
boolBuildfile::filter(const string& filename) {
for(auto& r : cpp_regexs)
if(regex_match(filename, r))
returntrue;
returnfalse;
}
const vector<string>& Buildfile::get_compile_command(const string& source, const string& object) {
if(!compile_cmd.empty()) {
compile_cmd[source_index] = source;
compile_cmd[object_index] = object;
return compile_cmd;
}
//first time loading this, so need to fill compile_cmd
auto it = first_alnum_char_past(compilecmd_c);
string arg;
while(it != end() && *it != '\n') {
//read each word delimited by whitespace. Could conver to stringstream
//but that would be expensive. Eaiser to just do this
if(isspace(*it)) {
add_compile_arg(move(arg), source, object);
arg.clear();
} else
arg += *it;
++it;
}
if(!arg.empty())
add_compile_arg(move(arg), source, object);
return compile_cmd;
}
const string& Buildfile::get_link_command(const vector<string>& object_names) {
if(!linkcmd.empty())
return linkcmd;
auto it = first_alnum_char_past(linkcmd_c);
string word;
while(it != end() && *it != '\n') {
if(isspace(*it)) {
add_link_arg(move(word), object_names);
word.clear();
} else
word += *it;
++it;
}
if(!word.empty())
add_link_arg(move(word), object_names);
return linkcmd;
}
voidBuildfile::add_link_arg(string&& arg, const vector<string>& object_names) {
if(arg == "OBJECTS") {
for(auto& obj : object_names) {
linkcmd += obj;
linkcmd += '';
}
} elseif(arg == "EXE") {
linkcmd += executable_name;
linkcmd += '';
} else {
linkcmd += arg;
linkcmd += '';
}
}
voidBuildfile::add_compile_arg(string&& arg, const string& source, const string& object) {
if(arg == "SOURCE") {
source_index = compile_cmd.size();
compile_cmd.emplace_back(source);
} elseif(arg == "OBJECT") {
object_index = compile_cmd.size();
compile_cmd.emplace_back(object);
} else
compile_cmd.emplace_back(move(arg));
}
string Buildfile::cpp_to_object(const string& cpp_filename) {
size_t cpp_index = cpp_filename.find(".cpp");
string object_filename{cpp_filename.begin(), cpp_filename.begin() + cpp_index};
object_filename += ".o";
return object_filename;
}
constchar* Buildfile::first_alnum_char_past(const string& phrase) const {
auto it = begin();
while(strncmp(it, phrase.c_str(), phrase.size()))
it = skip_past(it, end()); //move it to begin at line of phrase
it = skip_past(it, end(), ':');
while(it != end() && isspace(*it) && *it != '\n')
++it; //skip all whitespace that is not the end of the line
return it;
}