]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add hash_command_output() and hash_multicommand_output()
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 14 Aug 2010 13:33:11 +0000 (15:33 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 14 Aug 2010 19:14:08 +0000 (21:14 +0200)
hashutil.c
hashutil.h
test/test_hashutil.c [new file with mode: 0644]

index 0d30fb2031886c18caff02983110ad0bda18d612..11c6cddadb6c8405817bbc47447666b811835167 100644 (file)
 #include "hashutil.h"
 #include "murmurhashneutral2.h"
 
-#include <string.h>
+#include <errno.h>
 #include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
 #include <time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 unsigned
 hash_from_string(void *str)
@@ -224,3 +228,76 @@ hash_source_code_file(struct mdfour *hash, const char *path)
        x_munmap(data, st.st_size);
        return result;
 }
+
+int
+hash_command_output(struct mdfour *hash, const char *command,
+                    const char *compiler)
+{
+       pid_t pid;
+       int pipefd[2];
+
+       if (pipe(pipefd) == -1) {
+               fatal("pipe failed");
+       }
+       pid = fork();
+       if (pid == -1) {
+               fatal("fork failed");
+       }
+
+       if (pid == 0) {
+               /* Child. */
+               struct args *args = args_init_from_string(command);
+               int i;
+               for (i = 0; i < args->argc; i++) {
+                       if (str_eq(args->argv[i], "%compiler%")) {
+                               args_set(args, i, compiler);
+                       }
+               }
+               cc_log_argv("Executing compiler check command ", args->argv);
+               close(pipefd[0]);
+               close(0);
+               dup2(pipefd[1], 1);
+               dup2(pipefd[1], 2);
+               _exit(execvp(args->argv[0], args->argv));
+               return 0; /* Never reached. */
+       } else {
+               /* Parent. */
+               int status, ok;
+               close(pipefd[1]);
+               ok = hash_fd(hash, pipefd[0]);
+               if (!ok) {
+                       cc_log("Error hashing compiler check command output: %s", strerror(errno));
+                       stats_update(STATS_COMPCHECK);
+               }
+               close(pipefd[0]);
+               if (waitpid(pid, &status, 0) != pid) {
+                       cc_log("waitpid failed");
+                       return 0;
+               }
+               if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+                       cc_log("Compiler check command returned %d", WEXITSTATUS(status));
+                       stats_update(STATS_COMPCHECK);
+                       return 0;
+               }
+               return ok;
+       }
+}
+
+int
+hash_multicommand_output(struct mdfour *hash, const char *commands,
+                         const char *compiler)
+{
+       char *command_string, *command, *p;
+       int ok = 1;
+
+       command_string = x_strdup(commands);
+       p = command_string;
+       while ((command = strtok(p, ";"))) {
+               if (!hash_command_output(hash, command, compiler)) {
+                       ok = 0;
+               }
+               p = NULL;
+       }
+       free(command_string);
+       return ok;
+}
index 57c7b0ac599f8b791356941e54edca715f49a1f0..43453ee4ecc1a84b33664b784b5d3ae37994f60f 100644 (file)
@@ -23,5 +23,9 @@ int file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2);
 int hash_source_code_string(
        struct mdfour *hash, const char *str, size_t len, const char *path);
 int hash_source_code_file(struct mdfour *hash, const char *path);
+int hash_command_output(struct mdfour *hash, const char *command,
+                        const char *compiler);
+int hash_multicommand_output(struct mdfour *hash, const char *command,
+                             const char *compiler);
 
 #endif
diff --git a/test/test_hashutil.c b/test/test_hashutil.c
new file mode 100644 (file)
index 0000000..2106871
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2010 Joel Rosdahl
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/*
+ * This file contains tests for functions in hashutil.c.
+ */
+
+#include "ccache.h"
+#include "hashutil.h"
+#include "test/framework.h"
+#include "test/util.h"
+
+TEST_SUITE(hashutil)
+
+TEST(hash_command_output_simple)
+{
+       struct mdfour h1, h2;
+       hash_start(&h1);
+       hash_start(&h2);
+       CHECK(hash_command_output(&h1, "echo", "not used"));
+       CHECK(hash_command_output(&h2, "echo", "not used"));
+       CHECK(hash_equal(&h1, &h2));
+}
+
+TEST(hash_command_output_space_removal)
+{
+       struct mdfour h1, h2;
+       hash_start(&h1);
+       hash_start(&h2);
+       CHECK(hash_command_output(&h1, "echo", "not used"));
+       CHECK(hash_command_output(&h2, " echo ", "not used"));
+       CHECK(hash_equal(&h1, &h2));
+}
+
+TEST(hash_command_output_hash_inequality)
+{
+       struct mdfour h1, h2;
+       hash_start(&h1);
+       hash_start(&h2);
+       CHECK(hash_command_output(&h1, "echo foo", "not used"));
+       CHECK(hash_command_output(&h2, "echo bar", "not used"));
+       CHECK(!hash_equal(&h1, &h2));
+}
+
+TEST(hash_command_output_compiler_substitution)
+{
+       struct mdfour h1, h2;
+       hash_start(&h1);
+       hash_start(&h2);
+       CHECK(hash_command_output(&h1, "echo foo", "not used"));
+       CHECK(hash_command_output(&h2, "%compiler% foo", "echo"));
+       CHECK(hash_equal(&h1, &h2));
+}
+
+TEST(hash_command_output_stdout_versus_stderr)
+{
+       struct mdfour h1, h2;
+       hash_start(&h1);
+       hash_start(&h2);
+       create_file("stderr.sh", "#!/bin/sh\necho foo >&2\n");
+       chmod("stderr.sh", 0555);
+       CHECK(hash_command_output(&h1, "echo foo", "not used"));
+       CHECK(hash_command_output(&h2, "./stderr.sh", "not used"));
+       CHECK(hash_equal(&h1, &h2));
+}
+
+TEST(hash_multicommand_output)
+{
+       struct mdfour h1, h2;
+       hash_start(&h1);
+       hash_start(&h2);
+       CHECK(hash_multicommand_output(&h1, "echo -n foo", "not used"));
+       CHECK(hash_multicommand_output(&h2, "echo -n f; echo -n oo", "not used"));
+       CHECK(hash_equal(&h1, &h2));
+}
+
+TEST(hash_multicommand_output_error_handling)
+{
+       struct mdfour h1, h2;
+       hash_start(&h1);
+       hash_start(&h2);
+       CHECK(!hash_multicommand_output(&h2, "false; true", "not used"));
+}
+
+TEST_SUITE_END