summaryrefslogtreecommitdiff
path: root/test/suite/test.cc
blob: b9a6cc30183fa907fd836fd0162fd3032e162edf (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include "../../src/util.cc"
#include "../../src/pointerlist.cc"
#include "../../src/execute.cc"

#include <iostream>
#include <string>
#include <filesystem>
#include <thread>
#include <chrono>
#include <sstream>

std::vector<std::string> tokenize(const std::string& str)
{
	std::vector<std::string> tokens;
	std::stringstream ss(str);
	std::string token;
	 while(getline(ss, token, ' '))
	 {
		 tokens.push_back(token);
	 }
	 return tokens;
}

int fail(int value = 1,
         const std::source_location location = std::source_location::current())
{
	std::cout << "*** Failure at line " << location.line() << '\n';
	return value;
}

int main()
{
	using namespace std::chrono_literals;
	ctor::settings settings{};
	settings.verbose = 2;
	auto paths = get_paths();

	std::string CXX = "g++";
	get_env("CXX", CXX);
	std::string CTORDIR = "../../build";
	get_env("CTORDIR", CTORDIR);
	std::string BUILDDIR = "build";
	get_env("BUILDDIR", BUILDDIR);
	std::string CXXFLAGS;
	get_env("CXXFLAGS", CXXFLAGS);
	std::string LDFLAGS;
	get_env("LDFLAGS", LDFLAGS);

	auto cxx_prog = locate(CXX, paths);

	// Wipe the board
	std::filesystem::remove_all(BUILDDIR);
	std::filesystem::remove("configuration.cc");
	std::filesystem::remove("config.h");
	std::filesystem::remove("ctor");

	//////////////////////////////////////////////////////////////////////////////
	std::cout << "** ctor_files/ctor.cc.base\n";
	std::filesystem::copy("ctor_files/ctor.cc.base", "ctor.cc",
	                      std::filesystem::copy_options::overwrite_existing);

	// Compile bootstrap binary
	std::vector<std::string> args =
		{"-pthread", "-std=c++20", "-L", CTORDIR, "-lctor", "-I", "../../src",
		 "ctor.cc", "-o", "ctor"};

	// TODO: add support for quoted strings with spaces
	if(!CXXFLAGS.empty())
	{
		auto tokens = tokenize(CXXFLAGS);
		for(const auto& token : tokens)
		{
			args.push_back(token);
		}
	}
	if(!LDFLAGS.empty())
	{
		auto tokens = tokenize(LDFLAGS);
		for(const auto& token : tokens)
		{
			args.push_back(token);
		}
	}

	auto ret = execute(settings, cxx_prog, args);
	if(ret != 0)
	{
		return fail(ret);
	}

	// No build files should have been created yet
	if(std::filesystem::exists(BUILDDIR))
	{
		return fail();
	}

	// capture ctor binary before configure is called
	auto ctor_bin = readFile("ctor");
	args = {"configure", "--ctor-includedir", "../../src",
	        "--ctor-libdir="+CTORDIR, "--build-dir="+BUILDDIR};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	// ctor should be rebuilt at this point, so binary should have changed
	auto ctor_bin2 = readFile("ctor");
	if(ctor_bin == ctor_bin2)
	{
		return fail();
	}

	// configuration.cc should have been generated now
	if(!std::filesystem::exists("configuration.cc"))
	{
		return fail();
	}
	if(!std::filesystem::exists("config.h"))
	{
		return fail();
	}

	// Shouldn't compile anything yet - only configure
	if(std::filesystem::exists(BUILDDIR+"/hello-hello_cc.o"))
	{
		return fail();
	}

	ctor_bin = readFile("ctor");

	// Run normally to build project
	args = {"-v"};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	// Compiled object should now exist
	if(!std::filesystem::exists(BUILDDIR+"/hello-hello_cc.o"))
	{
		return fail();
	}

	// ctor should not have been rebuilt, so binary should be the same
	ctor_bin2 = readFile("ctor");
	if(ctor_bin != ctor_bin2)
	{
		return fail();
	}

	std::this_thread::sleep_for(1100ms);

	auto time = std::filesystem::last_write_time(BUILDDIR+"/hello-hello_cc.o");
	std::filesystem::last_write_time("hello.cc", time + 1s);
	std::this_thread::sleep_for(1100ms);

	// Run normally to rebuild hello.cc
	args = {"-v"};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	// Object file should have been recompiled
	auto time2 = std::filesystem::last_write_time(BUILDDIR+"/hello-hello_cc.o");
	if(time == time2)
	{
		return fail();
	}

	//////////////////////////////////////////////////////////////////////////////
	// Replace -DFOO with -DBAR in foo external.cxxflags
	std::cout << "** ctor_files/ctor.cc.bar\n";
	std::filesystem::copy("ctor_files/ctor.cc.bar", "ctor.cc",
	                      std::filesystem::copy_options::overwrite_existing);

	auto configuration_cc_bin = readFile("configuration.cc");
	ctor_bin = readFile("ctor");
	time = std::filesystem::last_write_time(BUILDDIR+"/hello-hello_cc.o");
	std::this_thread::sleep_for(1100ms);

	// Run normally to reconfigure, rebuild ctor and rebuild hello.cc
	args = {"-v"};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	time2 = std::filesystem::last_write_time(BUILDDIR+"/hello-hello_cc.o");
	if(time == time2)
	{
		return fail();
	}

	auto configuration_cc_bin2 = readFile("configuration.cc");
	if(configuration_cc_bin == configuration_cc_bin2)
	{
		return fail();
	}

	ctor_bin2 = readFile("ctor");
	if(ctor_bin == ctor_bin2)
	{
		return fail();
	}

	//////////////////////////////////////////////////////////////////////////////
	std::cout << "** ctor_files/ctor.cc.multi\n";
	std::filesystem::copy("ctor_files/ctor.cc.multi", "ctor.cc",
	                      std::filesystem::copy_options::overwrite_existing);

	configuration_cc_bin = readFile("configuration.cc");
	ctor_bin = readFile("ctor");
	time = std::filesystem::last_write_time(BUILDDIR+"/hello-hello_cc.o");
	std::this_thread::sleep_for(1100ms);

	// Run normally to reconfigure, rebuild ctor and rebuild hello.cc
	args = {"-v"};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	time2 = std::filesystem::last_write_time(BUILDDIR+"/hello-hello_cc.o");
	if(time == time2)
	{
		return fail();
	}

	configuration_cc_bin2 = readFile("configuration.cc");
	if(configuration_cc_bin == configuration_cc_bin2)
	{
		return fail();
	}

	ctor_bin2 = readFile("ctor");
	if(ctor_bin == ctor_bin2)
	{
		return fail();
	}

	// now touching foobar.h, should retrigger re-configuration
	time = std::filesystem::last_write_time("ctor");
	std::filesystem::last_write_time("foobar.h", time + 1s);
	time = std::filesystem::last_write_time("ctor");
	std::this_thread::sleep_for(1100ms);

	// Run normally to reconfigure, rebuild ctor and rebuild hello.cc
	args = {"-v"};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	time2 = std::filesystem::last_write_time("ctor");
	if(time == time2)
	{
		return fail();
	}

	//////////////////////////////////////////////////////////////////////////////
	std::cout << "** ctor_files/ctor.cc.generated\n";
	std::filesystem::copy("ctor_files/ctor.cc.generated", "ctor.cc",
	                      std::filesystem::copy_options::overwrite_existing);

	std::filesystem::remove(BUILDDIR+"/world.cc");
	std::filesystem::remove(BUILDDIR+"/foo.cc");

	configuration_cc_bin = readFile("configuration.cc");
	ctor_bin = readFile("ctor");
	std::this_thread::sleep_for(1100ms);

	// Run normally to reconfigure, rebuild ctor and build world.cc
	args = {"-v", "world"};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	configuration_cc_bin2 = readFile("configuration.cc");
	if(configuration_cc_bin == configuration_cc_bin2)
	{
		return fail();
	}

	ctor_bin2 = readFile("ctor");
	if(ctor_bin == ctor_bin2)
	{
		return fail();
	}

	// foo.cc should not be generated at this point
	if(std::filesystem::exists(BUILDDIR+"/foo.cc"))
	{
		return fail();
	}

	auto time_w = std::filesystem::last_write_time(BUILDDIR+"/world.cc");
	auto time_wo = std::filesystem::last_write_time(BUILDDIR+"/"+BUILDDIR+"/world-world_cc.o");
	std::this_thread::sleep_for(1100ms);

	// now touching hello.cc, should trigger regeneration of world.cc and rebuild
	std::filesystem::last_write_time("hello.cc", time_w + 1s);
	args = {"-v", "world"};
	ret = execute(settings, "./ctor", args);
	if(ret != 0)
	{
		return fail(ret);
	}

	auto time_w2 = std::filesystem::last_write_time(BUILDDIR+"/world.cc");
	auto time_wo2 = std::filesystem::last_write_time(BUILDDIR+"/"+BUILDDIR+"/world-world_cc.o");
	if(time_w == time_w2)
	{
		return fail();
	}
	if(time_wo == time_wo2)
	{
		return fail();
	}

	return 0;
}