1 #include <tools/ccanlint/ccanlint.h>
11 #include <ccan/talloc/talloc.h>
12 #include <ccan/str/str.h>
14 struct ccanlint has_license;
16 static struct doc_section *find_license(const struct manifest *m)
18 struct doc_section *d;
20 list_for_each(m->info_file->doc_sections, d, list) {
21 if (!streq(d->function, m->basename))
23 if (streq(d->type, "license"))
29 static const char *expected_link(const struct manifest *m,
30 struct doc_section *d)
32 if (streq(d->lines[0], "GPL")
33 || streq(d->lines[0], "GPLv3")
34 || streq(d->lines[0], "GPLv3 or later")
35 || streq(d->lines[0], "GPLv3 (or later)")
36 || streq(d->lines[0], "GPL (3 or any later version)"))
37 return "../../licenses/GPL-3";
38 if (streq(d->lines[0], "GPLv2")
39 || streq(d->lines[0], "GPLv2 or later")
40 || streq(d->lines[0], "GPLv2 (or later)")
41 || streq(d->lines[0], "GPL (2 or any later version)"))
42 return "../../licenses/GPL-3";
43 if (streq(d->lines[0], "LGPL")
44 || streq(d->lines[0], "LGPLv3")
45 || streq(d->lines[0], "LGPLv3 or later")
46 || streq(d->lines[0], "LGPLv3 (or later)")
47 || streq(d->lines[0], "LGPL (3 or any later version)"))
48 return "../../licenses/LGPL-3";
49 if (streq(d->lines[0], "LGPLv2")
50 || streq(d->lines[0], "LGPLv2 or later")
51 || streq(d->lines[0], "LGPLv2 (or later)")
52 || streq(d->lines[0], "LGPL (2 or any later version)"))
53 return "../../licenses/LGPL-2.1";
54 if (streq(d->lines[0], "BSD")
55 || streq(d->lines[0], "BSD-MIT")
56 || streq(d->lines[0], "MIT"))
57 return "../../licenses/BSD-MIT";
61 static void handle_license_link(struct manifest *m, struct score *score)
63 const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
64 struct doc_section *d = find_license(m);
65 const char *ldest = expected_link(m, d);
69 "Most modules want a copy of their license, so usually we create a\n"
70 "LICENSE symlink into ../../licenses to avoid too many copies.\n");
72 /* FIXME: make ask printf-like */
73 q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
76 if (symlink(ldest, link) != 0)
77 err(1, "Creating symlink %s -> %s", link, ldest);
81 static void check_has_license(struct manifest *m,
83 unsigned int *timeleft, struct score *score)
87 char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
89 struct doc_section *d;
93 score->error = talloc_strdup(score, "No License: tag in _info");
96 expected = expected_link(m, d);
98 len = readlink(license, buf, sizeof(buf));
100 /* Could be a real file... OK if not a standard license. */
101 if (errno == EINVAL) {
107 = talloc_asprintf(score,
108 "License in _info is '%s',"
109 " expect LICENSE symlink '%s'",
110 d->lines[0], expected);
113 if (errno == ENOENT) {
114 score->error = talloc_strdup(score,
115 "LICENSE does not exist");
117 has_license.handle = handle_license_link;
120 err(1, "readlink on %s", license);
122 if (len >= sizeof(buf))
123 errx(1, "Reading symlink %s gave huge result", license);
127 if (!strstarts(buf, "../../licenses/")) {
128 score->error = talloc_asprintf(score,
129 "Expected symlink to"
130 " ../../licenses/..."
136 score->error = talloc_asprintf(score,
137 "License in _info is unknown '%s',"
138 " but LICENSE symlink is '%s'",
143 if (!streq(buf, expected)) {
144 score->error = talloc_asprintf(score,
145 "Expected symlink to %s not %s",
150 score->score = score->total;
153 struct ccanlint license_exists = {
154 .key = "license_exists",
155 .name = "Module has License: entry in _info, and LICENSE symlink/file",
156 .check = check_has_license,
157 .needs = "info_exists"
160 REGISTER_TEST(license_exists);