[ccache] [PATCH] Running the real compiler when HOME is not set

Francois Marier francois at debian.org
Mon Nov 13 23:42:50 GMT 2006


Hello,

I have written the following patch to make ccache run the real compiler
instead of exiting when HOME is not set.

This is a problem for example when using ccache with the scons build system
(see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=396350).  Arguably,
ccache should not be used with scons since scons does its own caching, but
it might be enabled by default (through a symlink) on a machine building
mostly non-scons projects (such as the Debian autobuilders).  Therefore, I
think that the best way to handle this case is to do as if CCACHE_DISABLE
was set.

Now, I realize that leaving cache_dir as NULL is not the safest thing since
I had to add a bunch of checks.  Would anybody have a better/cleaner
solution?

Francois
-------------- next part --------------
--- ccache-2.4.orig/ccache.c
+++ ccache-2.4/ccache.c
@@ -836,6 +836,13 @@
 {
 	/* find the real compiler */
 	find_compiler(argc, argv);
+
+	/* use the real compiler if HOME is not set */
+	if (!cache_dir) {
+		cc_log("Unable to determine home directory\n");
+		cc_log("ccache is disabled\n");
+		failed();
+	}
 	
 	/* we might be disabled */
 	if (getenv("CCACHE_DISABLE")) {
@@ -895,6 +902,13 @@
 	printf("-V                      print version number\n");
 }
 
+static void check_cache_dir(void)
+{
+	if (!cache_dir) {
+		fatal("Unable to determine home directory");
+	}
+}
+
 /* the main program when not doing a compile */
 static int ccache_main(int argc, char *argv[])
 {
@@ -914,31 +928,37 @@
 			exit(0);
 			
 		case 's':
+			check_cache_dir();
 			stats_summary();
 			break;
 
 		case 'c':
+			check_cache_dir();
 			cleanup_all(cache_dir);
 			printf("Cleaned cache\n");
 			break;
 
 		case 'C':
+			check_cache_dir();
 			wipe_all(cache_dir);
 			printf("Cleared cache\n");
 			break;
 
 		case 'z':
+			check_cache_dir();
 			stats_zero();
 			printf("Statistics cleared\n");
 			break;
 
 		case 'F':
+			check_cache_dir();
 			v = atoi(optarg);
 			stats_set_limits(v, -1);
 			printf("Set cache file limit to %u\n", (unsigned)v);
 			break;
 
 		case 'M':
+			check_cache_dir();
 			v = value_units(optarg);
 			stats_set_limits(-1, v);
 			printf("Set cache size limit to %uk\n", (unsigned)v);
@@ -983,7 +1003,10 @@
 
 	cache_dir = getenv("CCACHE_DIR");
 	if (!cache_dir) {
-		x_asprintf(&cache_dir, "%s/.ccache", get_home_directory());
+		const char *home_directory = get_home_directory();
+		if (home_directory) {
+			x_asprintf(&cache_dir, "%s/.ccache", home_directory);
+		}
 	}
 
 	temp_dir = getenv("CCACHE_TEMPDIR");
@@ -1023,7 +1046,7 @@
 	}
 
 	/* make sure the cache dir exists */
-	if (create_dir(cache_dir) != 0) {
+	if (cache_dir && (create_dir(cache_dir) != 0)) {
 		fprintf(stderr,"ccache: failed to create %s (%s)\n", 
 			cache_dir, strerror(errno));
 		exit(1);
--- ccache-2.4.orig/util.c
+++ ccache-2.4/util.c
@@ -448,7 +448,7 @@
 		}
 	}
 #endif
-	fatal("Unable to determine home directory");
+	cc_log("Unable to determine home directory");
 	return NULL;
 }
 


More information about the ccache mailing list