Browse Source

tools: explicit find_ccan_dir()

Move ccan_dir determination out to its own function, rather than implying it
by the first time we get the manifest of a module.
Rusty Russell 14 years ago
parent
commit
20ea8a3708
7 changed files with 48 additions and 31 deletions
  1. 31 0
      tools/ccan_dir.c
  2. 1 0
      tools/ccanlint/Makefile
  3. 11 4
      tools/ccanlint/ccanlint.c
  4. 3 0
      tools/ccanlint/ccanlint.h
  5. 0 24
      tools/manifest.c
  6. 0 3
      tools/manifest.h
  7. 2 0
      tools/tools.h

+ 31 - 0
tools/ccan_dir.c

@@ -0,0 +1,31 @@
+#include <ccan/talloc/talloc.h>
+#include "tools.h"
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+/* Walk up to find /ccan/ => ccan directory. */
+static unsigned int ccan_dir_prefix(const char *fulldir)
+{
+	unsigned int i;
+
+	assert(fulldir[0] == '/');
+	for (i = strlen(fulldir) - 1; i > 0; i--) {
+		if (strncmp(fulldir+i, "/ccan", 5) != 0)
+			continue;
+		if (fulldir[i+5] != '\0' && fulldir[i+5] != '/')
+			continue;
+		return i + 1;
+	}
+	return 0;
+}
+
+const char *find_ccan_dir(const char *base)
+{
+	unsigned int prefix = ccan_dir_prefix(base);
+
+	if (!prefix)
+		return NULL;
+
+	return talloc_strndup(NULL, base, prefix);
+}

+ 1 - 0
tools/ccanlint/Makefile

@@ -27,6 +27,7 @@ CORE_OBJS := \
 	tools/ccanlint/ccanlint.o \
 	tools/ccanlint/file_analysis.o \
 	tools/ccanlint/licenses.o \
+	tools/ccan_dir.o \
 	tools/compile.o \
 	tools/depends.o \
 	tools/doc_extract-core.o \

+ 11 - 4
tools/ccanlint/ccanlint.c

@@ -52,6 +52,8 @@ const char *cflags = NULL;
 
 const char *config_header;
 
+const char *ccan_dir;
+
 #if 0
 static void indent_print(const char *string)
 {
@@ -777,16 +779,21 @@ int main(int argc, char *argv[])
 			dir[strlen(dir)-1] = '\0';
 
 	got_dir:
+		/* We assume there's a ccan/ in there somewhere... */
+		if (i == 1) {
+			ccan_dir = find_ccan_dir(dir);
+			if (!ccan_dir)
+				errx(1, "Cannot find ccan/ base directory in %s",
+				     dir);
+			read_config_header();
+		}
+
 		if (dir != base_dir)
 			prefix = talloc_append_string(talloc_basename(NULL,dir),
 						      ": ");
 
 		m = get_manifest(talloc_autofree_context(), dir);
 
-		/* FIXME: This has to come after we've got manifest. */
-		if (i == 1)
-			read_config_header();
-
 		/* Create a symlink from temp dir back to src dir's
 		 * test directory. */
 		unlink(testlink);

+ 3 - 0
tools/ccanlint/ccanlint.h

@@ -191,4 +191,7 @@ extern const char *compiler, *cflags;
 /* Contents of config.h (or NULL if not found) */
 extern const char *config_header;
 
+/* Where is the ccan dir?   */
+extern const char *ccan_dir;
+
 #endif /* CCAN_LINT_H */

+ 0 - 24
tools/manifest.c

@@ -23,8 +23,6 @@
 #include <stdarg.h>
 #include <assert.h>
 
-const char *ccan_dir;
-
 static size_t dir_hash(const char *name)
 {
 	return hash(name, strlen(name), 0);
@@ -196,22 +194,6 @@ static void sort_files(struct list_head *list)
 	talloc_free(files);
 }
 
-/* Walk up tp find /ccan/ => ccan directory. */
-static unsigned int ccan_dir_prefix(const char *fulldir)
-{
-	unsigned int i;
-
-	assert(fulldir[0] == '/');
-	for (i = strlen(fulldir) - 1; i > 0; i--) {
-		if (strncmp(fulldir+i, "/ccan", 5) != 0)
-			continue;
-		if (fulldir[i+5] != '\0' && fulldir[i+5] != '/')
-			continue;
-		return i + 1;
-	}
-	errx(1, "Could not find /ccan/ dir in %s", fulldir);
-}
-
 struct manifest *get_manifest(const void *ctx, const char *dir)
 {
 	struct manifest *m;
@@ -265,12 +247,6 @@ struct manifest *get_manifest(const void *ctx, const char *dir)
 		errx(1, "I don't expect to be run from the root directory");
 	m->basename++;
 
-	if (!ccan_dir) {
-		unsigned int prefix = ccan_dir_prefix(m->dir);
-
-		ccan_dir = talloc_strndup(NULL, m->dir, prefix);
-	}
-
 	add_files(m, "");
 
 	/* Nicer to run tests in a predictable order. */

+ 0 - 3
tools/manifest.h

@@ -89,7 +89,4 @@ const char *get_ccan_file_contents(struct ccan_file *f);
 /* Use this rather than accessing f->lines directly: loads on demand. */
 char **get_ccan_file_lines(struct ccan_file *f);
 
-/* Where is the ccan dir?  Available after first manifest. */
-extern const char *ccan_dir;
-
 #endif /* CCAN_TOOLS_MANIFEST_H */

+ 2 - 0
tools/tools.h

@@ -75,4 +75,6 @@ extern const unsigned int default_timeout_ms;
 /* Talloc destructor which unlinks file. */
 int unlink_file_destructor(char *filename);
 
+/* Get ccan/ top dir, given a directory within it. */
+const char *find_ccan_dir(const char *base);
 #endif /* CCAN_TOOLS_H */