Browse Source

Use a temporary directory for -use-separate-modules

Windows only currently
gingerBill 1 year ago
parent
commit
7dd4cccce7
2 changed files with 66 additions and 25 deletions
  1. 28 9
      src/llvm_backend.cpp
  2. 38 16
      src/string.cpp

+ 28 - 9
src/llvm_backend.cpp

@@ -2556,20 +2556,37 @@ gb_internal String lb_filepath_ll_for_module(lbModule *m) {
 
 	return path;
 }
+
 gb_internal String lb_filepath_obj_for_module(lbModule *m) {
-	String path = concatenate3_strings(permanent_allocator(),
-		build_context.build_paths[BuildPath_Output].basename,
-		STR_LIT("/"),
-		build_context.build_paths[BuildPath_Output].name
-	);
+	String basename = build_context.build_paths[BuildPath_Output].basename;
+	String name = build_context.build_paths[BuildPath_Output].name;
+
+	bool use_temporary_directory = false;
+	if (USE_SEPARATE_MODULES && build_context.build_mode == BuildMode_Executable) {
+		// NOTE(bill): use a temporary directory
+		String dir = temporary_directory(permanent_allocator());
+		if (dir.len != 0) {
+			basename = dir;
+			use_temporary_directory = true;
+		}
+	}
+
+	gbString path = gb_string_make_length(heap_allocator(), basename.text, basename.len);
+	path = gb_string_appendc(path, "/");
+	path = gb_string_append_length(path, name.text, name.len);
 
 	if (m->file) {
 		char buf[32] = {};
 		isize n = gb_snprintf(buf, gb_size_of(buf), "-%u", m->file->id);
 		String suffix = make_string((u8 *)buf, n-1);
-		path = concatenate_strings(permanent_allocator(), path, suffix);
+		path = gb_string_append_length(path, suffix.text, suffix.len);
 	} else if (m->pkg) {
-		path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name);
+		path = gb_string_appendc(path, "-");
+		path = gb_string_append_length(path, m->pkg->name.text, m->pkg->name.len);
+	}
+
+	if (use_temporary_directory) {
+		path = gb_string_append_fmt(path, "-%p", m);
 	}
 
 	String ext = {};
@@ -2607,7 +2624,10 @@ gb_internal String lb_filepath_obj_for_module(lbModule *m) {
 		}
 	}
 
-	return concatenate_strings(permanent_allocator(), path, ext);
+	path = gb_string_append_length(path, ext.text, ext.len);
+
+	return make_string(cast(u8 *)path, gb_string_length(path));
+
 }
 
 
@@ -2666,7 +2686,6 @@ gb_internal bool lb_llvm_object_generation(lbGenerator *gen, bool do_threading)
 
 			String filepath_ll = lb_filepath_ll_for_module(m);
 			String filepath_obj = lb_filepath_obj_for_module(m);
-			// gb_printf_err("%.*s\n", LIT(filepath_obj));
 			array_add(&gen->output_object_paths, filepath_obj);
 			array_add(&gen->output_temp_paths, filepath_ll);
 

+ 38 - 16
src/string.cpp

@@ -329,22 +329,22 @@ gb_internal bool string_contains_char(String const &s, u8 c) {
 }
 
 gb_internal bool string_contains_string(String const &haystack, String const &needle) {
-    if (needle.len == 0) return true;
-    if (needle.len > haystack.len) return false;
-
-    for (isize i = 0; i <= haystack.len - needle.len; i++) {
-        bool found = true;
-        for (isize j = 0; j < needle.len; j++) {
-            if (haystack[i + j] != needle[j]) {
-                found = false;
-                break;
-            }
-        }
-        if (found) {
-            return true;
-        }
-    }
-    return false;
+	if (needle.len == 0) return true;
+	if (needle.len > haystack.len) return false;
+
+	for (isize i = 0; i <= haystack.len - needle.len; i++) {
+		bool found = true;
+		for (isize j = 0; j < needle.len; j++) {
+			if (haystack[i + j] != needle[j]) {
+				found = false;
+				break;
+			}
+		}
+		if (found) {
+			return true;
+		}
+	}
+	return false;
 }
 
 gb_internal String filename_from_path(String s) {
@@ -543,6 +543,28 @@ gb_internal String string16_to_string(gbAllocator a, String16 s) {
 
 
 
+gb_internal String temporary_directory(gbAllocator allocator) {
+	String res = {};
+#if defined(GB_SYSTEM_WINDOWS)
+	DWORD n = GetTempPathW(0, nullptr);
+	if (n == 0) {
+		return res;
+	}
+	DWORD len = gb_max(MAX_PATH, n);
+	wchar_t *b = gb_alloc_array(heap_allocator(), wchar_t, len+1);
+	defer (gb_free(heap_allocator(), b));
+	n = GetTempPathW(len, b);
+	if (n == 3 && b[1] == ':' && b[2] == '\\') {
+
+	} else if (n > 0 && b[n-1] == '\\') {
+		n -= 1;
+	}
+	b[n] = 0;
+	String16 s = make_string16(b, n);
+	res = string16_to_string(allocator, s);
+#endif
+	return res;
+}