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], "BSD-MIT")
54 || streq(d->lines[0], "MIT"))
55 return "../../licenses/BSD-MIT";
59 static void handle_license_link(struct manifest *m, struct score *score)
61 const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
62 struct doc_section *d = find_license(m);
63 const char *ldest = expected_link(m, d);
67 "Most modules want a copy of their license, so usually we create a\n"
68 "LICENSE symlink into ../../licenses to avoid too many copies.\n");
70 /* FIXME: make ask printf-like */
71 q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
74 if (symlink(ldest, link) != 0)
75 err(1, "Creating symlink %s -> %s", link, ldest);
79 static void check_has_license(struct manifest *m,
81 unsigned int *timeleft, struct score *score)
85 char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
87 struct doc_section *d;
91 score->error = "No License: tag in _info";
94 expected = expected_link(m, d);
96 len = readlink(license, buf, sizeof(buf));
98 /* Could be a real file... OK if not a standard license. */
99 if (errno == EINVAL) {
105 = talloc_asprintf(score,
106 "License in _info is '%s',"
107 " expect LICENSE symlink '%s'",
108 d->lines[0], expected);
111 if (errno == ENOENT) {
112 score->error = "LICENSE does not exist";
114 has_license.handle = handle_license_link;
117 err(1, "readlink on %s", license);
119 if (len >= sizeof(buf))
120 errx(1, "Reading symlink %s gave huge result", license);
124 if (!strstarts(buf, "../../licenses/")) {
125 score->error = talloc_asprintf(score,
126 "Expected symlink to"
127 " ../../licenses/..."
133 score->error = talloc_asprintf(score,
134 "License in _info is unknown '%s',"
135 " but LICENSE symlink is '%s'",
140 if (!streq(buf, expected)) {
141 score->error = talloc_asprintf(score,
142 "Expected symlink to %s not %s",
147 score->score = score->total;
150 struct ccanlint has_license = {
151 .key = "has-license",
152 .name = "Module has license",
153 .check = check_has_license,
156 REGISTER_TEST(has_license, &has_info, NULL);