1 #include <tools/ccanlint/ccanlint.h>
11 #include <ccan/talloc/talloc.h>
12 #include <ccan/str/str.h>
14 static struct doc_section *find_license(const struct manifest *m)
16 struct doc_section *d;
18 list_for_each(m->info_file->doc_sections, d, list) {
19 if (!streq(d->function, m->basename))
21 if (streq(d->type, "license"))
27 static const char *expected_link(const struct manifest *m,
28 struct doc_section *d)
30 if (streq(d->lines[0], "GPL")
31 || streq(d->lines[0], "GPLv3")
32 || streq(d->lines[0], "GPLv3 or later")
33 || streq(d->lines[0], "GPLv3 (or later)")
34 || streq(d->lines[0], "GPL (3 or any later version)"))
35 return "../../licenses/GPL-3";
36 if (streq(d->lines[0], "GPLv2")
37 || streq(d->lines[0], "GPLv2 or later")
38 || streq(d->lines[0], "GPLv2 (or later)")
39 || streq(d->lines[0], "GPL (2 or any later version)"))
40 return "../../licenses/GPL-3";
41 if (streq(d->lines[0], "LGPL")
42 || streq(d->lines[0], "LGPLv3")
43 || streq(d->lines[0], "LGPLv3 or later")
44 || streq(d->lines[0], "LGPLv3 (or later)")
45 || streq(d->lines[0], "LGPL (3 or any later version)"))
46 return "../../licenses/LGPL-3";
47 if (streq(d->lines[0], "LGPLv2")
48 || streq(d->lines[0], "LGPLv2 or later")
49 || streq(d->lines[0], "LGPLv2 (or later)")
50 || streq(d->lines[0], "LGPL (2 or any later version)"))
51 return "../../licenses/LGPL-2.1";
52 if (streq(d->lines[0], "BSD")
53 || streq(d->lines[0], "MIT"))
54 return "../../licenses/BSD-MIT";
58 static void handle_license_link(struct manifest *m, struct score *score)
60 const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
61 struct doc_section *d = find_license(m);
62 const char *ldest = expected_link(m, d);
66 "Most modules want a copy of their license, so usually we create a\n"
67 "LICENSE symlink into ../../licenses to avoid too many copies.\n");
69 /* FIXME: make ask printf-like */
70 q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
73 if (symlink(ldest, link) != 0)
74 err(1, "Creating symlink %s -> %s", link, ldest);
78 static void check_has_license(struct manifest *m,
80 unsigned int *timeleft, struct score *score)
84 char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
86 struct doc_section *d;
90 score->error = "No License: tag in _info";
93 expected = expected_link(m, d);
95 len = readlink(license, buf, sizeof(buf));
97 /* Could be a real file... OK if not a standard license. */
98 if (errno == EINVAL) {
104 = talloc_asprintf(score,
105 "License in _info is '%s',"
106 " expect LICENSE symlink '%s'",
107 d->lines[0], expected);
110 if (errno == ENOENT) {
111 score->error = "LICENSE does not exist";
113 has_license.handle = handle_license_link;
116 err(1, "readlink on %s", license);
118 if (len >= sizeof(buf))
119 errx(1, "Reading symlink %s gave huge result", license);
123 if (!strstarts(buf, "../../licenses/")) {
124 score->error = talloc_asprintf(score,
125 "Expected symlink to"
126 " ../../licenses/..."
132 score->error = talloc_asprintf(score,
133 "License in _info is unknown '%s',"
134 " but LICENSE symlink is '%s'",
139 if (!streq(buf, expected)) {
140 score->error = talloc_asprintf(score,
141 "Expected symlink to %s not %s",
146 score->score = score->total;
149 struct ccanlint has_license = {
150 .key = "has-license",
151 .name = "Module has license",
152 .check = check_has_license,
155 REGISTER_TEST(has_license, &has_info, NULL);