Commit 104125b2 authored by Rusty Russell's avatar Rusty Russell

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.
parent e169f82b
......@@ -31,6 +31,7 @@
#include <ccan/talloc/talloc.h>
#include <ccan/opt/opt.h>
#include <ccan/foreach/foreach.h>
#include <ccan/grab_file/grab_file.h>
int verbose = 0;
static LIST_HEAD(compulsory_tests);
......@@ -41,6 +42,12 @@ static struct btree *cmdline_exclude;
static struct btree *info_exclude;
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
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";
}
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[])
{
bool summary = false;
......@@ -535,6 +628,7 @@ int main(int argc, char *argv[])
tools_verbose = true;
m = get_manifest(talloc_autofree_context(), dir);
read_config_header();
/* Create a symlink from temp dir back to src dir's test directory. */
if (symlink(talloc_asprintf(m, "%s/test", dir),
......
......@@ -221,4 +221,10 @@ extern bool safe_mode;
/* Where is the ccan dir? Available after first manifest. */
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 */
......@@ -37,8 +37,8 @@ static char *build_subdir_objs(struct manifest *m)
char *output;
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);
i->compiled = NULL;
return talloc_asprintf(m,
......
......@@ -57,7 +57,8 @@ static void check_includes_build(struct manifest *m,
err(1, "writing to temporary file %s", tmpsrc);
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->score = score->total;
} else {
......
......@@ -72,8 +72,8 @@ static void check_use_build(struct manifest *m,
err(1, "Failure writing to temporary file %s", tmpfile);
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),
&cmdout)) {
score->pass = true;
......
......@@ -38,8 +38,8 @@ static void check_objs_build(struct manifest *m,
char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
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);
score_file_error(score, i, 0,
"Compiling object files:\n%s",
......
......@@ -121,7 +121,8 @@ static bool compile(const void *ctx,
file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
if (!compile_and_link(ctx, file->fullname, ccan_dir,
obj_list(m, file),
"", lib_list(m), file->compiled, output)) {
compiler, cflags,
lib_list(m), file->compiled, output)) {
/* Don't keep failures. */
if (keep)
unlink(file->compiled);
......
......@@ -66,10 +66,12 @@ static bool compile(const void *ctx,
bool link_with_module,
bool keep, char **output)
{
char *f = talloc_asprintf(ctx, "%s%s",
fail ? "-DFAIL " : "", cflags);
file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
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)) {
talloc_free(file->compiled);
return false;
......
......@@ -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);
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)) {
score_file_error(score, i, 0, "%s", err);
talloc_free(i->cov_compiled);
......@@ -93,11 +93,12 @@ static char *cov_compile(const void *ctx,
bool keep)
{
char *output;
char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
if (!compile_and_link(ctx, file->fullname, ccan_dir,
obj_list(m, modobjs),
COVERAGE_CFLAGS,
compiler, f,
lib_list(m), file->cov_compiled, &output)) {
talloc_free(file->cov_compiled);
file->cov_compiled = NULL;
......
......@@ -27,7 +27,7 @@ static bool compile(struct manifest *m,
char **output)
{
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);
}
......
......@@ -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. */
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)
{
if (compile_verbose)
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.
* Returns false on failure. */
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)
{
if (compile_verbose)
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);
}
......@@ -62,7 +62,8 @@ static char *compile_info(const void *ctx, const char *dir)
*strrchr(ccandir, '/') = '\0';
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))
return compiled;
return NULL;
......
......@@ -58,11 +58,13 @@ char *link_objects(const void *ctx, const char *basename, bool in_pwd,
const char *objs, char **errmsg);
/* 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,
const char *extra_cflags,
const char *compiler,
const char *cflags,
const char *outfile, char **output);
/* Compile and link single C file, with object files, libs, etc. */
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);
/* If in_pwd is false, return a file int temp_dir, otherwise a local file. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment