]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Support adjusting the compression level through CCACHE_COMPRESS_LEVEL
authorHongli Lai (Phusion) <hongli@phusion.nl>
Sun, 19 Feb 2012 23:28:36 +0000 (00:28 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 20 Feb 2012 21:46:18 +0000 (22:46 +0100)
MANUAL.txt
ccache.c
ccache.h
util.c

index f15d6667e6e111bda3ec4eedcf9b1e8d48e292ca..b263c5a788405c55fb19434ee950ac8a386d5ba6 100644 (file)
@@ -244,6 +244,13 @@ WRAPPERS>>.
     cache; compressed and uncompressed results will still be usable regardless
     of this setting.
 
+*CCACHE_COMPRESS_LEVEL*::
+
+    This environment variable determines the level at which ccache will
+    compress object files. It only has effect if *CCACHE_COMPRESS* is also
+    set. The value defaults to 6, and must be no lower than 1 (fastest, worst
+    compression) and no higher than 9 (slowest, best compression).
+
 *CCACHE_CPP2*::
 
     If you set the environment variable *CCACHE_CPP2* then ccache will not use
index 2a956a5df7722825923817bc97b8399549b6413d..7942104734d3fbeecdc46b7c1fda9ba60b51cb62 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -163,10 +163,10 @@ static bool enable_unify;
 static bool enable_direct = true;
 
 /*
- * Whether to enable compression of files stored in the cache. (Manifest files
- * are always compressed.)
+ * If non-zero, enables compression of files stored in the cache, compressed
+ * at the given level. (Manifest files are always compressed.)
  */
-static bool enable_compression = false;
+static int enable_compression = 0;
 
 /* number of levels (1 <= nlevels <= 8) */
 static int nlevels = 2;
@@ -2097,7 +2097,11 @@ ccache(int argc, char *argv[])
 
        if (getenv("CCACHE_COMPRESS")) {
                cc_log("Compression enabled");
-               enable_compression = true;
+               if (getenv("CCACHE_COMPRESS_LEVEL")) {
+                       enable_compression = atoi(getenv("CCACHE_COMPRESS_LEVEL"));
+               } else {
+                       enable_compression = 6;
+               }
        }
 
        if ((env = getenv("CCACHE_NLEVELS"))) {
index 6a62753d39941cf3a4c76f70d63a780a6f032c22..b9505ac32061b2b50404bbb0319871df0584869f 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -102,10 +102,10 @@ void cc_log(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
 void cc_log_argv(const char *prefix, char **argv);
 void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
 void copy_fd(int fd_in, int fd_out);
-int copy_file(const char *src, const char *dest, int compress_dest);
-int move_file(const char *src, const char *dest, int compress_dest);
+int copy_file(const char *src, const char *dest, int compress_level);
+int move_file(const char *src, const char *dest, int compress_level);
 int move_uncompressed_file(const char *src, const char *dest,
-                           int compress_dest);
+                           int compress_level);
 bool file_is_compressed(const char *filename);
 int create_dir(const char *dir);
 int create_parent_dirs(const char *path);
diff --git a/util.c b/util.c
index e1f09bc5ff86d7b4a9c9a33dbffd4dc353dbab44..bca11bc553f4651631352fb9601e82ad943827fd 100644 (file)
--- a/util.c
+++ b/util.c
@@ -191,11 +191,11 @@ mkstemp(char *template)
 #endif
 
 /*
- * Copy src to dest, decompressing src if needed. compress_dest decides whether
- * dest will be compressed.
+ * Copy src to dest, decompressing src if needed. compress_level > 0 decides whether
+ * dest will be compressed, and with which compression level.
  */
 int
-copy_file(const char *src, const char *dest, int compress_dest)
+copy_file(const char *src, const char *dest, int compress_level)
 {
        int fd_in = -1, fd_out = -1;
        gzFile gz_in = NULL, gz_out = NULL;
@@ -210,7 +210,7 @@ copy_file(const char *src, const char *dest, int compress_dest)
 
        tmp_name = format("%s.%s.XXXXXX", dest, tmp_string());
        cc_log("Copying %s to %s via %s (%s)",
-              src, dest, tmp_name, compress_dest ? "compressed": "uncompressed");
+              src, dest, tmp_name, compress_level ? "compressed": "uncompressed");
 
        /* open source file */
        fd_in = open(src, O_RDONLY | O_BINARY);
@@ -233,7 +233,7 @@ copy_file(const char *src, const char *dest, int compress_dest)
                goto error;
        }
 
-       if (compress_dest) {
+       if (compress_level) {
                /*
                 * A gzip file occupies at least 20 bytes, so it will always
                 * occupy an entire filesystem block, even for empty files.
@@ -244,20 +244,21 @@ copy_file(const char *src, const char *dest, int compress_dest)
                        goto error;
                }
                if (file_size(&st) == 0) {
-                       compress_dest = 0;
+                       compress_level = 0;
                }
        }
 
-       if (compress_dest) {
+       if (compress_level) {
                gz_out = gzdopen(dup(fd_out), "wb");
                if (!gz_out) {
                        cc_log("gzdopen(dest) error: %s", strerror(errno));
                        goto error;
                }
+               gzsetparams(gz_out, compress_level, Z_DEFAULT_STRATEGY);
        }
 
        while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
-               if (compress_dest) {
+               if (compress_level) {
                        written = gzwrite(gz_out, buf, n);
                } else {
                        ssize_t count;
@@ -271,7 +272,7 @@ copy_file(const char *src, const char *dest, int compress_dest)
                        } while (written < n);
                }
                if (written != n) {
-                       if (compress_dest) {
+                       if (compress_level) {
                                cc_log("gzwrite error: %s (errno: %s)",
                                       gzerror(gz_in, &errnum),
                                       strerror(errno));
@@ -346,11 +347,11 @@ error:
 
 /* Run copy_file() and, if successful, delete the source file. */
 int
-move_file(const char *src, const char *dest, int compress_dest)
+move_file(const char *src, const char *dest, int compress_level)
 {
        int ret;
 
-       ret = copy_file(src, dest, compress_dest);
+       ret = copy_file(src, dest, compress_level);
        if (ret != -1) {
                x_unlink(src);
        }
@@ -362,10 +363,10 @@ move_file(const char *src, const char *dest, int compress_dest)
  * are on the same file system.
  */
 int
-move_uncompressed_file(const char *src, const char *dest, int compress_dest)
+move_uncompressed_file(const char *src, const char *dest, int compress_level)
 {
-       if (compress_dest) {
-               return move_file(src, dest, compress_dest);
+       if (compress_level) {
+               return move_file(src, dest, compress_level);
        } else {
                return x_rename(src, dest);
        }