Browse Source

ccanlint: read config.h to get compilation flags at runtime.

This means you don't have to recompile ccanlint to get the new flags;
it's a small step towards making ccanlint useful outside the ccan repo.
Rusty Russell 15 years ago
parent
commit
104125b2dd

+ 94 - 0
tools/ccanlint/ccanlint.c

@@ -31,6 +31,7 @@
 #include <ccan/talloc/talloc.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/opt/opt.h>
 #include <ccan/opt/opt.h>
 #include <ccan/foreach/foreach.h>
 #include <ccan/foreach/foreach.h>
+#include <ccan/grab_file/grab_file.h>
 
 
 int verbose = 0;
 int verbose = 0;
 static LIST_HEAD(compulsory_tests);
 static LIST_HEAD(compulsory_tests);
@@ -41,6 +42,12 @@ static struct btree *cmdline_exclude;
 static struct btree *info_exclude;
 static struct btree *info_exclude;
 static unsigned int timeout;
 static unsigned int timeout;
 
 
+/* These are overridden at runtime if we can find config.h */
+const char *compiler = CCAN_COMPILER;
+const char *cflags = CCAN_CFLAGS;
+
+const char *config_header;
+
 #if 0
 #if 0
 static void indent_print(const char *string)
 static void indent_print(const char *string)
 {
 {
@@ -475,6 +482,92 @@ static void skip_unrelated_tests(struct ccanlint *target)
 				i->skip = "not relevant to target";
 				i->skip = "not relevant to target";
 }
 }
 
 
+static char *demangle_string(char *string)
+{
+	unsigned int i;
+	const char mapfrom[] = "abfnrtv";
+	const char mapto[] = "\a\b\f\n\r\t\v";
+
+	if (!strchr(string, '"'))
+		return NULL;
+	string = strchr(string, '"') + 1;
+	if (!strrchr(string, '"'))
+		return NULL;
+	*strrchr(string, '"') = '\0';
+
+	for (i = 0; i < strlen(string); i++) {
+		if (string[i] == '\\') {
+			char repl;
+			unsigned len = 0;
+			char *p = strchr(mapfrom, string[i+1]);
+			if (p) {
+				repl = mapto[p - mapfrom];
+				len = 1;
+			} else if (strlen(string+i+1) >= 3) {
+				if (string[i+1] == 'x') {
+					repl = (string[i+2]-'0')*16
+						+ string[i+3]-'0';
+					len = 3;
+				} else if (isdigit(string[i+1])) {
+					repl = (string[i+2]-'0')*8*8
+						+ (string[i+3]-'0')*8
+						+ (string[i+4]-'0');
+					len = 3;
+				}
+			}
+			if (len == 0) {
+				repl = string[i+1];
+				len = 1;
+			}
+
+			string[i] = repl;
+			memmove(string + i + 1, string + i + len + 1,
+				strlen(string + i + len + 1) + 1);
+		}
+	}
+
+	return string;
+}
+
+
+static void read_config_header(void)
+{
+	char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
+	char **lines;
+	unsigned int i;
+
+	config_header = grab_file(NULL, fname, NULL);
+	if (!config_header) {
+		talloc_free(fname);
+		return;
+	}
+
+	lines = strsplit(config_header, config_header, "\n");
+	for (i = 0; i < talloc_array_length(lines) - 1; i++) {
+		char *sym;
+		const char **line = (const char **)&lines[i];
+
+		if (!get_token(line, "#"))
+			continue;
+		if (!get_token(line, "define"))
+			continue;
+		sym = get_symbol_token(lines, line);
+		if (streq(sym, "CCAN_COMPILER")) {
+			compiler = demangle_string(lines[i]);
+			if (verbose > 1)
+				printf("%s: compiler set to '%s'\n",
+				       fname, compiler);
+		} else if (streq(sym, "CCAN_CFLAGS")) {
+			cflags = demangle_string(lines[i]);
+			if (verbose > 1)
+				printf("%s: compiler flags set to '%s'\n",
+				       fname, cflags);
+		}
+	}
+	if (!compiler || !cflags)
+		errx(1, "Problem parsing %s", fname);
+}
+
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
 	bool summary = false;
 	bool summary = false;
@@ -535,6 +628,7 @@ int main(int argc, char *argv[])
 		tools_verbose = true;
 		tools_verbose = true;
 
 
 	m = get_manifest(talloc_autofree_context(), dir);
 	m = get_manifest(talloc_autofree_context(), dir);
+	read_config_header();
 
 
 	/* Create a symlink from temp dir back to src dir's test directory. */
 	/* Create a symlink from temp dir back to src dir's test directory. */
 	if (symlink(talloc_asprintf(m, "%s/test", dir),
 	if (symlink(talloc_asprintf(m, "%s/test", dir),

+ 6 - 0
tools/ccanlint/ccanlint.h

@@ -221,4 +221,10 @@ extern bool safe_mode;
 /* Where is the ccan dir?  Available after first manifest. */
 /* Where is the ccan dir?  Available after first manifest. */
 extern const char *ccan_dir;
 extern const char *ccan_dir;
 
 
+/* Compiler and CFLAGS, from config.h if available. */
+extern const char *compiler, *cflags;
+
+/* Contents of config.h (or NULL if not found) */
+extern const char *config_header;
+
 #endif /* CCAN_LINT_H */
 #endif /* CCAN_LINT_H */

+ 2 - 2
tools/ccanlint/compulsory_tests/depends_build.c

@@ -37,8 +37,8 @@ static char *build_subdir_objs(struct manifest *m)
 		char *output;
 		char *output;
 
 
 		i->compiled = maybe_temp_file(m, "", false, fullfile);
 		i->compiled = maybe_temp_file(m, "", false, fullfile);
-		if (!compile_object(m, fullfile, ccan_dir, "", i->compiled,
-				    &output)) {
+		if (!compile_object(m, fullfile, ccan_dir, compiler, cflags,
+				    i->compiled, &output)) {
 			talloc_free(i->compiled);
 			talloc_free(i->compiled);
 			i->compiled = NULL;
 			i->compiled = NULL;
 			return talloc_asprintf(m,
 			return talloc_asprintf(m,

+ 2 - 1
tools/ccanlint/compulsory_tests/main_header_compiles.c

@@ -57,7 +57,8 @@ static void check_includes_build(struct manifest *m,
 		err(1, "writing to temporary file %s", tmpsrc);
 		err(1, "writing to temporary file %s", tmpsrc);
 	close(fd);
 	close(fd);
 
 
-	if (compile_object(score, tmpsrc, ccan_dir, "", tmpobj, &cmdout)) {
+	if (compile_object(score, tmpsrc, ccan_dir, compiler, cflags,
+			   tmpobj, &cmdout)) {
 		score->pass = true;
 		score->pass = true;
 		score->score = score->total;
 		score->score = score->total;
 	} else {
 	} else {

+ 2 - 2
tools/ccanlint/compulsory_tests/module_links.c

@@ -72,8 +72,8 @@ static void check_use_build(struct manifest *m,
 		err(1, "Failure writing to temporary file %s", tmpfile);
 		err(1, "Failure writing to temporary file %s", tmpfile);
 	close(fd);
 	close(fd);
 
 
-	if (compile_and_link(score, tmpfile, ccan_dir, obj_list(m), "",
-			     lib_list(m),
+	if (compile_and_link(score, tmpfile, ccan_dir, obj_list(m),
+			     compiler, cflags, lib_list(m),
 			     maybe_temp_file(m, "", keep, tmpfile),
 			     maybe_temp_file(m, "", keep, tmpfile),
 			     &cmdout)) {
 			     &cmdout)) {
 		score->pass = true;
 		score->pass = true;

+ 2 - 2
tools/ccanlint/compulsory_tests/objects_build.c

@@ -38,8 +38,8 @@ static void check_objs_build(struct manifest *m,
 		char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
 		char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
 
 
 		i->compiled = maybe_temp_file(m, "", keep, fullfile);
 		i->compiled = maybe_temp_file(m, "", keep, fullfile);
-		if (!compile_object(score, fullfile, ccan_dir, "", i->compiled,
-				    &output)) {
+		if (!compile_object(score, fullfile, ccan_dir, compiler, cflags,
+				    i->compiled, &output)) {
 			talloc_free(i->compiled);
 			talloc_free(i->compiled);
 			score_file_error(score, i, 0,
 			score_file_error(score, i, 0,
 					 "Compiling object files:\n%s",
 					 "Compiling object files:\n%s",

+ 2 - 1
tools/ccanlint/tests/examples_compile.c

@@ -121,7 +121,8 @@ static bool compile(const void *ctx,
 	file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
 	file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
 	if (!compile_and_link(ctx, file->fullname, ccan_dir,
 	if (!compile_and_link(ctx, file->fullname, ccan_dir,
 			      obj_list(m, file),
 			      obj_list(m, file),
-			      "", lib_list(m), file->compiled, output)) {
+			      compiler, cflags,
+			      lib_list(m), file->compiled, output)) {
 		/* Don't keep failures. */
 		/* Don't keep failures. */
 		if (keep)
 		if (keep)
 			unlink(file->compiled);
 			unlink(file->compiled);

+ 4 - 2
tools/ccanlint/tests/tests_compile.c

@@ -66,10 +66,12 @@ static bool compile(const void *ctx,
 		    bool link_with_module,
 		    bool link_with_module,
 		    bool keep, char **output)
 		    bool keep, char **output)
 {
 {
+	char *f = talloc_asprintf(ctx, "%s%s",
+				  fail ? "-DFAIL " : "", cflags);
+
 	file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
 	file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
 	if (!compile_and_link(ctx, file->fullname, ccan_dir,
 	if (!compile_and_link(ctx, file->fullname, ccan_dir,
-			      obj_list(m, link_with_module),
-			      fail ? "-DFAIL" : "",
+			      obj_list(m, link_with_module), compiler, f,
 			      lib_list(m), file->compiled, output)) {
 			      lib_list(m), file->compiled, output)) {
 		talloc_free(file->compiled);
 		talloc_free(file->compiled);
 		return false;
 		return false;

+ 3 - 2
tools/ccanlint/tests/tests_compile_coverage.c

@@ -38,7 +38,7 @@ static bool build_module_objs_with_coverage(struct manifest *m, bool keep,
 		char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
 		char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
 
 
 		i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
 		i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
-		if (!compile_object(m, fullfile, ccan_dir, "",
+		if (!compile_object(m, fullfile, ccan_dir, compiler, cflags,
 				    i->cov_compiled, &err)) {
 				    i->cov_compiled, &err)) {
 			score_file_error(score, i, 0, "%s", err);
 			score_file_error(score, i, 0, "%s", err);
 			talloc_free(i->cov_compiled);
 			talloc_free(i->cov_compiled);
@@ -93,11 +93,12 @@ static char *cov_compile(const void *ctx,
 			 bool keep)
 			 bool keep)
 {
 {
 	char *output;
 	char *output;
+	char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
 
 
 	file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
 	file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
 	if (!compile_and_link(ctx, file->fullname, ccan_dir,
 	if (!compile_and_link(ctx, file->fullname, ccan_dir,
 			      obj_list(m, modobjs),
 			      obj_list(m, modobjs),
-			      COVERAGE_CFLAGS,
+			      compiler, f,
 			      lib_list(m), file->cov_compiled, &output)) {
 			      lib_list(m), file->cov_compiled, &output)) {
 		talloc_free(file->cov_compiled);
 		talloc_free(file->cov_compiled);
 		file->cov_compiled = NULL;
 		file->cov_compiled = NULL;

+ 1 - 1
tools/ccanlint/tests/tests_helpers_compile.c

@@ -27,7 +27,7 @@ static bool compile(struct manifest *m,
 		    char **output)
 		    char **output)
 {
 {
 	cfile->compiled = maybe_temp_file(m, ".o", keep, cfile->fullname);
 	cfile->compiled = maybe_temp_file(m, ".o", keep, cfile->fullname);
-	return compile_object(m, cfile->fullname, ccan_dir, "",
+	return compile_object(m, cfile->fullname, ccan_dir, compiler, cflags,
 			      cfile->compiled, output);
 			      cfile->compiled, output);
 }
 }
 
 

+ 11 - 8
tools/compile.c

@@ -22,25 +22,28 @@ char *link_objects(const void *ctx, const char *basename, bool in_pwd,
 
 
 /* Compile a single C file to an object file. */
 /* Compile a single C file to an object file. */
 bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
 bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
-		    const char *extra_cflags,
+		    const char *compiler,
+		    const char *cflags,
 		    const char *outfile, char **output)
 		    const char *outfile, char **output)
 {
 {
 	if (compile_verbose)
 	if (compile_verbose)
 		printf("Compiling %s\n", outfile);
 		printf("Compiling %s\n", outfile);
-	return run_command(ctx, NULL, output, CCAN_COMPILER " " CCAN_CFLAGS
-			   " -I%s %s -c -o %s %s",
-			   ccandir, extra_cflags, outfile, cfile);
+	return run_command(ctx, NULL, output,
+			   "%s %s -I%s -c -o %s %s",
+			   compiler, cflags, ccandir, outfile, cfile);
 }
 }
 
 
 /* Compile and link single C file, with object files.
 /* Compile and link single C file, with object files.
  * Returns false on failure. */
  * Returns false on failure. */
 bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
 bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
-		      const char *objs, const char *extra_cflags,
+		      const char *objs, const char *compiler,
+		      const char *cflags,
 		      const char *libs, const char *outfile, char **output)
 		      const char *libs, const char *outfile, char **output)
 {
 {
 	if (compile_verbose)
 	if (compile_verbose)
 		printf("Compiling and linking %s\n", outfile);
 		printf("Compiling and linking %s\n", outfile);
-	return run_command(ctx, NULL, output, CCAN_COMPILER " " CCAN_CFLAGS
-			   " -I%s %s -o %s %s %s %s",
-			   ccandir, extra_cflags, outfile, cfile, objs, libs);
+	return run_command(ctx, NULL, output,
+			   "%s %s -I%s -o %s %s %s %s",
+			   compiler, cflags,
+			   ccandir, outfile, cfile, objs, libs);
 }
 }

+ 2 - 1
tools/depends.c

@@ -62,7 +62,8 @@ static char *compile_info(const void *ctx, const char *dir)
 	*strrchr(ccandir, '/') = '\0';
 	*strrchr(ccandir, '/') = '\0';
 
 
 	compiled = maybe_temp_file(ctx, "", false, "info");
 	compiled = maybe_temp_file(ctx, "", false, "info");
-	if (compile_and_link(ctx, info_c_file, ccandir, "", "", "",
+	if (compile_and_link(ctx, info_c_file, ccandir, "",
+			     CCAN_COMPILER, CCAN_CFLAGS, "",
 			     compiled, &output))
 			     compiled, &output))
 		return compiled;
 		return compiled;
 	return NULL;
 	return NULL;

+ 4 - 2
tools/tools.h

@@ -58,11 +58,13 @@ char *link_objects(const void *ctx, const char *basename, bool in_pwd,
 		   const char *objs, char **errmsg);
 		   const char *objs, char **errmsg);
 /* Compile a single C file to an object file.  Returns false if fails. */
 /* Compile a single C file to an object file.  Returns false if fails. */
 bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
 bool compile_object(const void *ctx, const char *cfile, const char *ccandir,
-		    const char *extra_cflags,
+		    const char *compiler,
+		    const char *cflags,
 		    const char *outfile, char **output);
 		    const char *outfile, char **output);
 /* Compile and link single C file, with object files, libs, etc. */
 /* Compile and link single C file, with object files, libs, etc. */
 bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
 bool compile_and_link(const void *ctx, const char *cfile, const char *ccandir,
-		      const char *objs, const char *extra_cflags,
+		      const char *objs,
+		      const char *compiler, const char *cflags,
 		      const char *libs, const char *outfile, char **output);
 		      const char *libs, const char *outfile, char **output);
 
 
 /* If in_pwd is false, return a file int temp_dir, otherwise a local file. */
 /* If in_pwd is false, return a file int temp_dir, otherwise a local file. */