/
Module.d
163 lines (121 loc) · 3.04 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
module xfbuild.Module;
private {
import xfbuild.GlobalParams;
import xfbuild.Misc;
import Path = tango.io.Path;
import tango.io.UnicodeFile;
import tango.text.convert.UnicodeBom;
import tango.text.Util;
import tango.io.stream.Lines;
import tango.text.Regex;
import tango.text.convert.Format;
// TODO: better logging
import tango.io.Stdout;
}
public {
import TextUtil = tango.text.Util;
}
/+private {
Regex moduleHeaderRegex;
}
static this() {
moduleHeaderRegex = Regex(`module\s+([a-zA-Z0-9._]+)`);
}+/
class Module
{
const(char)[] name;
const(char)[] path;
bool isHeader()
{
assert (path.length > 0, name);
return path[$ - 1] == 'i';
}
const(char)[] lastName()
{
auto dotPos = TextUtil.locatePrior(name, '.');
if(dotPos == name.length) dotPos = 0;
else ++dotPos;
return name[dotPos .. $];
}
const(char)[] objFileInFolder()
{
auto dotPos = TextUtil.locatePrior(path, '.');
assert(dotPos != path.length, name);
return path[0 .. dotPos] ~ globalParams.objExt;
}
const(char)[][] depNames;
Module[] deps; // only direct deps
long timeDep;
long timeModified;
bool wasCompiled;
bool needRecompile;
private const(char)[] objFile_;
const(char)[] objFile()
{
if(objFile_)
return objFile_;
return objFile_ =
globalParams.objPath
~ globalParams.pathSep
~ (globalParams.useOQ ? name : TextUtil.replace(name.dup, '.', '-'))
~ globalParams.objExt;
}
bool modified() { return timeModified > timeDep; }
override
immutable(char)[] toString() { return name.idup; }
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(const(char)[] path) {
path = path.dup;
auto m = new Module;
m.path = path;
m.timeModified = Path.modified(m.path).ticks;
auto fileData = (new UnicodeFile!(char)(m.path, Encoding.Unknown)).read();
foreach(line; splitLines(fileData))
{
line = TextUtil.trim(line);
//if(moduleHeaderRegex.test(line))
if (auto arr = line.decomposeString(`module`, ` `, null, `;`))
{
//m.name = moduleHeaderRegex[1].dup;
m.name = arr[0].dup;
if(globalParams.verbose)
Stdout.formatln("module name for file '{}': {}", path, m.name);
break;
}
}
if(!m.name)
throw new Exception(Format("module '{}' needs module header", path).idup);
return m;
}
}
bool isIgnored(const(char)[] name)
{
foreach(m; globalParams.ignore)
{
if(name.length >= m.length && name[0 .. m.length] == m)
return true;
}
return false;
}