-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModule.d
194 lines (152 loc) · 3.96 KB
/
Module.d
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
190
191
192
193
194
/+
+ Copyright Andrej Mitrovic 2011.
+ Copyright Tomasz Stachowiak 2009 - 2011.
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+/
module xfbuild.Module;
import xfbuild.Exception;
import xfbuild.GlobalParams;
import xfbuild.Misc;
import std.string : lastIndexOf, splitLines, strip;
import std.algorithm : min;
import std.file;
import std.stdio;
import std.array : replace;
alias strip trim;
// todo: not sure if correct
size_t locatePrior(string source, string match, size_t start = size_t.max)
{
return lastIndexOf(source[0 .. min(start, $)], match);
}
class Module
{
string name;
string path;
bool isHeader()
{
assert(path.length > 0, name);
return path[$ - 1] == 'i';
}
string lastName()
{
auto dotPos = locatePrior(name, ".");
if (dotPos == name.length)
dotPos = 0;
else
++dotPos;
return name[dotPos .. $];
}
string objFileInFolder()
{
auto dotPos = locatePrior(path, ".");
assert(dotPos != path.length, name);
return path[0 .. dotPos] ~ globalParams.objExt;
}
string[] depNames;
Module[] deps; // only direct deps
long timeDep;
long timeModified;
bool wasCompiled;
bool needRecompile;
private string objFile_;
string objFile()
{
if (objFile_)
return objFile_;
objFile_ =
globalParams.objPath
~ globalParams.pathSep
~ (globalParams.useOQ ? name : replace(name, ".", "-"))
~globalParams.objExt;
return objFile;
}
bool modified()
{
return timeModified > timeDep;
}
override string toString()
{
return name;
}
override hash_t toHash()
{
return typeid(typeof(path)).getHash(cast(void*)&path);
}
override int opCmp(Object rhs_)
{
auto rhs = cast(Module)rhs_;
if (rhs is this)
return 0;
if (this.path > rhs.path)
return 1;
if (this.path < rhs.path)
return -1;
return 0;
}
void addDep(Module mod)
{
if (!hasDep(mod))
{
deps ~= mod;
}
}
bool hasDep(Module mod)
{
foreach (d; deps)
{
if (d.name == mod.name)
{
return true;
}
}
return false;
}
static Module fromFile(string path)
{
auto m = new Module;
m.path = path;
m.timeModified = timeLastModified(m.path).stdTime;
// @BUG@ Workaround: Phobos has issues reading empty files
if (std.file.getSize(m.path) == 0)
{
throw new ModuleException(format("module '%s' is empty", path), __FILE__, __LINE__);
}
auto file = File(m.path, "r");
foreach (aLine; file.byLine)
{
string line = trim(aLine).idup;
//if(moduleHeaderRegex.test(line))
if (auto arr = line.decomposeString(`module`, ` `, null, `;`))
{
//m.name = moduleHeaderRegex[1].dup;
m.name = arr[0];
if (globalParams.verbose)
writefln("Module name for file '%s': %s", path, m.name);
break;
}
}
if (!m.name)
throw new ModuleException(format("module '%s' needs module header", path));
return m;
}
}
bool isIgnored(string name)
{
foreach (m; globalParams.ignore)
{
if (name.length >= m.length && name[0 .. m.length] == m)
return true;
}
return false;
}
bool isPathIgnored(string name)
{
foreach (m; globalParams.ignorePaths)
{
if (name.length >= m.length && name[0 .. m.length] == m)
return true;
}
return false;
}