aboutsummaryrefslogtreecommitdiff
path: root/src/cached.cpp
blob: 83950c82ba3b0230d88b42241d1a4a67ec702886 (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
gb_internal GB_COMPARE_PROC(cached_file_cmp) {
	String const &x = *(String *)a;
	String const &y = *(String *)b;
	return string_compare(x, y);
}

bool recursively_delete_directory(wchar_t *wpath_c) {
#if defined(GB_SYSTEM_WINDOWS)
	auto const is_dots_w = [](wchar_t const *str) -> bool {
		if (!str) {
			return false;
		}
		return wcscmp(str, L".") == 0 || wcscmp(str, L"..") == 0;
	};

	TEMPORARY_ALLOCATOR_GUARD();

	wchar_t dir_path[MAX_PATH] = {};
	wchar_t filename[MAX_PATH] = {};
	wcscpy(dir_path, wpath_c);
	wcscat(dir_path, L"\\*");

	wcscpy(filename, wpath_c);
	wcscat(filename, L"\\");


	WIN32_FIND_DATAW find_file_data = {};
	HANDLE hfind = FindFirstFileW(dir_path, &find_file_data);
	if (hfind == INVALID_HANDLE_VALUE) {
		return false;
	}
	defer (FindClose(hfind));

	wcscpy(dir_path, filename);

	for (;;) {
		if (FindNextFileW(hfind, &find_file_data)) {
			if (is_dots_w(find_file_data.cFileName)) {
				continue;
			}
			wcscat(filename, find_file_data.cFileName);

			if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
				if (!recursively_delete_directory(filename)) {
					return false;
				}
				RemoveDirectoryW(filename);
				wcscpy(filename, dir_path);
			} else {
				if (find_file_data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
					_wchmod(filename, _S_IWRITE);
				}
				if (!DeleteFileW(filename)) {
					return false;
				}
				wcscpy(filename, dir_path);
			}
		} else {
			if (GetLastError() == ERROR_NO_MORE_FILES) {
				break;
			}
			return false;
		}
	}


	return RemoveDirectoryW(wpath_c);
#else
	return false;
#endif
}

bool recursively_delete_directory(String const &path) {
#if defined(GB_SYSTEM_WINDOWS)
	String16 wpath = string_to_string16(permanent_allocator(), path);
	wchar_t *wpath_c = alloc_wstring(permanent_allocator(), wpath);
	return recursively_delete_directory(wpath_c);
#else
	return false;
#endif
}

int try_clear_cache(void) {
	bool ok = recursively_delete_directory(str_lit(".odin-cache"));
	return ok ? 0 : 1;
}


u64 crc64_with_seed(void const *data, isize len, u64 seed) {
	isize remaining;
	u64 result = ~seed;
	u8 const *c = cast(u8 const *)data;
	for (remaining = len; remaining--; c++) {
		result = (result >> 8) ^ (GB__CRC64_TABLE[(result ^ *c) & 0xff]);
	}
	return ~result;
}

bool check_if_exists_file_otherwise_create(String const &str) {
	char const *str_c = alloc_cstring(permanent_allocator(), str);
	if (!gb_file_exists(str_c)) {
		gbFile f = {};
		gb_file_create(&f, str_c);
		gb_file_close(&f);
		return true;
	}
	return false;
}


bool check_if_exists_directory_otherwise_create(String const &str) {
#if defined(GB_SYSTEM_WINDOWS)
	String16 wstr = string_to_string16(permanent_allocator(), str);
	wchar_t *wstr_c = alloc_wstring(permanent_allocator(), wstr);
	return CreateDirectoryW(wstr_c, nullptr);
#else
	char const *str_c = alloc_cstring(permanent_allocator(), str);
	if (!gb_file_exists(str_c)) {
		return false;
	}
	return false;
#endif
}
bool try_copy_executable_cache_internal(bool to_cache) {
	String exe_name = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Output]);
	defer (gb_free(heap_allocator(), exe_name.text));

	gbString cache_name = gb_string_make(heap_allocator(), "");
	defer (gb_string_free(cache_name));

	String cache_dir = build_context.build_cache_data.cache_dir;

	cache_name = gb_string_append_length(cache_name, cache_dir.text, cache_dir.len);
	cache_name = gb_string_appendc(cache_name, "/");

	cache_name = gb_string_appendc(cache_name, "cached-exe");
	if (selected_target_metrics) {
		cache_name = gb_string_appendc(cache_name, "-");
		cache_name = gb_string_append_length(cache_name, selected_target_metrics->name.text, selected_target_metrics->name.len);
	}
	if (selected_subtarget) {
		String st = subtarget_strings[selected_subtarget];
		cache_name = gb_string_appendc(cache_name, "-");
		cache_name = gb_string_append_length(cache_name, st.text, st.len);
	}
	cache_name = gb_string_appendc(cache_name, ".bin");

	if (to_cache) {
		return gb_file_copy(
			alloc_cstring(temporary_allocator(), exe_name),
			cache_name,
			false
		);
	} else {
		return gb_file_copy(
			cache_name,
			alloc_cstring(temporary_allocator(), exe_name),
			false
		);
	}
}



bool try_copy_executable_to_cache(void) {
	if (try_copy_executable_cache_internal(true)) {
		build_context.build_cache_data.copy_already_done = true;
		return true;
	}
	return false;
}

bool try_copy_executable_from_cache(void) {
	if (try_copy_executable_cache_internal(false)) {
		build_context.build_cache_data.copy_already_done = true;
		return true;
	}
	return false;
}




// returns false if different, true if it is the same
bool try_cached_build(Checker *c) {
	Parser *p = c->parser;

	auto files = array_make<String>(heap_allocator());
	for (AstPackage *pkg : p->packages) {
		for (AstFile *f : pkg->files) {
			array_add(&files, f->fullpath);
		}
	}

	for (auto const &entry : c->info.load_file_cache) {
		auto *cache = entry.value;
		if (!cache || !cache->exists) {
			continue;
		}
		array_add(&files, cache->path);
	}

	array_sort(files, cached_file_cmp);

	u64 crc = 0;
	for (String const &path : files) {
		crc = crc64_with_seed(path.text, path.len, crc);
	}

	String base_cache_dir = build_context.build_paths[BuildPath_Output].basename;
	base_cache_dir = concatenate_strings(permanent_allocator(), base_cache_dir, str_lit("/.odin-cache"));
	(void)check_if_exists_directory_otherwise_create(base_cache_dir);

	gbString crc_str = gb_string_make_reserve(permanent_allocator(), 16);
	crc_str = gb_string_append_fmt(crc_str, "%016llx", crc);
	String cache_dir = concatenate3_strings(permanent_allocator(), base_cache_dir, str_lit("/"), make_string_c(crc_str));
	String manifest_path = concatenate3_strings(permanent_allocator(), cache_dir, str_lit("/"), str_lit("odin.manifest"));

	build_context.build_cache_data.cache_dir = cache_dir;
	build_context.build_cache_data.manifest_path = manifest_path;

	if (check_if_exists_directory_otherwise_create(cache_dir)) {
		goto do_write_file;
	}

	if (check_if_exists_file_otherwise_create(manifest_path)) {
		goto do_write_file;
	} else {
		// exists already
		LoadedFile loaded_file = {};

		LoadedFileError file_err = load_file_32(
			alloc_cstring(temporary_allocator(), manifest_path),
			&loaded_file,
			false
		);
		if (file_err) {
			return false;
		}

		String data = {cast(u8 *)loaded_file.data, loaded_file.size};
		String_Iterator it = {data, 0};

		isize file_count = 0;

		for (; it.pos < data.len; file_count++) {
			String line = string_split_iterator(&it, '\n');
			if (line.len == 0) {
				break;
			}
			isize sep = string_index_byte(line, ' ');
			if (sep < 0) {
				goto do_write_file;
			}

			String timestamp_str = substring(line, 0, sep);
			String path_str = substring(line, sep+1, line.len);

			timestamp_str = string_trim_whitespace(timestamp_str);
			path_str = string_trim_whitespace(path_str);

			if (files[file_count] != path_str) {
				goto do_write_file;
			}

			u64 timestamp = exact_value_to_u64(exact_value_integer_from_string(timestamp_str));
			gbFileTime last_write_time = gb_file_last_write_time(alloc_cstring(temporary_allocator(), path_str));
			if (last_write_time != timestamp) {
				goto do_write_file;
			}
		}

		if (file_count != files.count) {
			goto do_write_file;
		}

		goto try_copy_executable;
	}

do_write_file:;
	{
		char const *manifest_path_c = alloc_cstring(temporary_allocator(), manifest_path);
		gb_file_remove(manifest_path_c);

		gbFile f = {};
		defer (gb_file_close(&f));
		gb_file_open_mode(&f, gbFileMode_Write, manifest_path_c);

		for (String const &path : files) {
			gbFileTime ft = gb_file_last_write_time(alloc_cstring(temporary_allocator(), path));
			gb_fprintf(&f, "%llu %.*s\n", cast(unsigned long long)ft, LIT(path));
		}
		return false;
	}

try_copy_executable:;
	return try_copy_executable_from_cache();
}