forked from bosagora/agora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.d
executable file
·194 lines (177 loc) · 6.31 KB
/
build.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
#!/usr/bin/env dub
/+
dub.json:
{
"name": "cpp_build"
}
+/
/*******************************************************************************
Build the SCP library
Copyright:
Copyright (c) 2019-2021 BOSAGORA Foundation
All rights reserved.
License:
MIT License. See LICENSE for details.
*******************************************************************************/
module build;
private:
import std.algorithm;
import std.datetime;
static import std.file;
import std.path;
import std.process;
import std.range;
import std.stdio;
immutable RootPath = __FILE_FULL_PATH__.dirName();
immutable SourcePath = RootPath;
immutable BuildPath = RootPath.buildPath("build");
immutable BuildTarget = BuildPath.buildPath("libscp.o");
/// Include path for C++ dependency - libsodium must be in the include path
/// (INCLUDE on Windows, and /usr/include/ or similar on POSIX)
immutable Includes = [
SourcePath,
SourcePath.buildPath("src"),
SourcePath.buildPath("lib"),
SourcePath.buildPath("lib", "xdrpp"),
];
version (Posix)
{
immutable ObjPattern = "*.o";
immutable CompilerIncludeFlag = "-I ";
immutable CppFlags = [
"-c",
"-g",
"-W",
"-Wall",
"-Wno-comment", // ignore warnings for multi-line "//" style comments
"-Wno-unused-parameter",
"-fPIC",
"-D_GLIBCXX_USE_CXX11_ABI=0",
"-std=c++17",
];
immutable CppCmd = [ "clang++" ] ~ CppFlags;
}
else version (Windows)
{
immutable ObjPattern = "*.obj";
immutable CompilerIncludeFlag = "/I ";
immutable CppFlags = [
"/JMC",
"/MP",
"/GS",
"/W4",
// https://docs.microsoft.com/en/cpp/error-messages/tool-errors/linker-tools-warning-lnk4099?view=vs-2019
// Remove warning : PDB file not found
"/wd\"4099\"",
// https://docs.microsoft.com/en/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4100?view=vs-2019
// Remove warning 'identifier' : unreferenced formal parameter
"/wd\"4100\"",
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4127?view=vs-2019
// Remove warning : conditional expression is constant
"/wd\"4127\"",
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4324?view=vs-2019
// Remove warning 'struct_name' : structure was padded due to __declspec(align())
"/wd\"4324\"",
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4408?view=vs-2019
// Remove warning : anonymous struct or union did not declare any data members
"/wd\"4408\"",
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4510?view=vs-2019
// Remove warning 'class' : default constructor could not be generated
"/wd\"4510\"",
// https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-4-c4512?view=vs-2019
// Remove warning 'class' : assignment operator could not be generated
"/Zc:wchar_t",
"/Zi",
"/Gm-",
"/Od",
"/sdl",
"/fp:precise",
"/D \"BUILD_TESTS\"",
"/D \"WIN32_LEAN_AND_MEAN\"",
"/D \"NOMINMAX\"",
"/D \"_WINSOCK_DEPRECATED_NO_WARNINGS\"",
"/D \"SODIUM_STATIC\"",
"/D \"_CRT_SECURE_NO_WARNINGS\"",
"/D \"_WIN32_WINNT=0x0601\"",
"/D \"WIN32\"",
"/D \"_MBCS\"",
"/D \"_CRT_NONSTDC_NO_DEPRECATE\"",
"/errorReport:prompt",
"/WX-",
"/Zc:forScope",
"/RTC1",
"/Gd",
"/std:c++17",
"/FC",
"/EHsc",
"/c"
];
immutable CppCmd = [ "cl" ] ~ CppFlags;
}
else
static assert(0, "Unsupported platform");
struct FileInfo
{
string path;
SysTime lastModified;
}
int main ()
{
// Make sure we're in the right directory
if (!std.file.exists(BuildPath) || !std.file.isDir(BuildPath))
std.file.mkdir(BuildPath);
std.file.chdir(BuildPath);
auto xdrfiles = std.file.dirEntries(
SourcePath, "*.x", std.file.SpanMode.depth);
auto headers = std.file.dirEntries(
SourcePath, "*.h*", std.file.SpanMode.depth);
// Need to array this because we might reuse it
auto sources = std.file.dirEntries(
SourcePath, "*.c*", std.file.SpanMode.depth).array;
auto objs = std.file.dirEntries(
BuildPath, ObjPattern, std.file.SpanMode.depth).array;
// If one of the obj file is older than one of the source file, rebuild
// That's a lesser approach than the dependency tracking Makefile do,
// but we don't expect those files to change very often.
// If the build script has changed, we also rebuild.
if (objs.length > sources.length)
{
writeln(objs.length - sources.length,
" sources files were deleted, cleaning up build directory and doing a full rebuild...");
std.file.chdir("../");
std.file.rmdirRecurse(BuildPath);
std.file.mkdir(BuildPath);
std.file.chdir(BuildPath);
}
else if (objs.length == sources.length)
{
auto buildTs = objs.map!((v) => FileInfo(v, std.file.timeLastModified(v)))
.minElement!((a) => a.lastModified);
auto lastModif = chain([__FILE_FULL_PATH__], xdrfiles, headers, sources.save)
.map!((v) => FileInfo(v, std.file.timeLastModified(v)))
.maxElement!((a) => a.lastModified);
if (lastModif.lastModified > buildTs.lastModified)
{
writeln("File ", lastModif.path, " is newer than ", buildTs.path,
" (", lastModif.lastModified, " > ", buildTs.lastModified,
"), doing a full rebuild...");
}
else
{
writeln("Target ", BuildTarget, " is up to date, nothing to do");
return 0;
}
}
else
writeln("First build / new source files added: Doing a full build...");
auto cmd = chain(CppCmd, Includes.map!((v) => CompilerIncludeFlag ~ v), sources);
auto strCmd = cmd.join(" ");
// writeln(strCmd);
auto pid = executeShell(strCmd);
if (pid.status != 0)
{
stderr.writeln("Build failed: ", pid.output);
return 1;
}
return 0;
}