1 #include <tools/ccanlint/ccanlint.h>
11 #include <ccan/talloc/talloc.h>
12 #include <ccan/str/str.h>
13 #include <ccan/str_talloc/str_talloc.h>
15 static struct doc_section *find_license_tag(const struct manifest *m)
17 struct doc_section *d;
19 list_for_each(m->info_file->doc_sections, d, list) {
20 if (!streq(d->function, m->basename))
22 if (streq(d->type, "license"))
28 static enum license which_license(struct doc_section *d)
30 if (strstarts(d->lines[0], "GPL")) {
31 if (strchr(d->lines[0], '3'))
33 else if (strchr(d->lines[0], '2')) {
34 if (strreg(NULL, d->lines[0], "or (any )?later", NULL))
35 return LICENSE_GPLv2_PLUS;
42 if (strstarts(d->lines[0], "LGPL")) {
43 if (strchr(d->lines[0], '3'))
44 return LICENSE_LGPLv3;
45 else if (strchr(d->lines[0], '2')) {
46 if (strreg(NULL, d->lines[0], "or (any )?later", NULL))
47 return LICENSE_LGPLv2_PLUS;
49 return LICENSE_LGPLv2;
53 if (streq(d->lines[0], "BSD-MIT")
54 || streq(d->lines[0], "MIT"))
56 if (streq(d->lines[0], "BSD (3 clause)"))
58 if (strreg(NULL, d->lines[0], "[Pp]ublic [Dd]omain"))
59 return LICENSE_PUBLIC_DOMAIN;
61 return LICENSE_UNKNOWN;
64 static const char *expected_link(enum license license)
67 case LICENSE_LGPLv2_PLUS:
69 return "../../licenses/LGPL-2.1";
72 return "../../licenses/LGPL-3";
74 case LICENSE_GPLv2_PLUS:
76 return "../../licenses/GPL-2";
80 return "../../licenses/GPL-3";
83 return "../../licenses/BSD-3CLAUSE";
86 return "../../licenses/BSD-MIT";
93 static void handle_license_link(struct manifest *m, struct score *score)
95 struct doc_section *d = find_license_tag(m);
96 const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
97 const char *ldest = expected_link(m->license);
101 "Most modules want a copy of their license, so usually we create a\n"
102 "LICENSE symlink into ../../licenses to avoid too many copies.\n");
104 /* FIXME: make ask printf-like */
105 q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
108 if (symlink(ldest, link) != 0)
109 err(1, "Creating symlink %s -> %s", link, ldest);
113 extern struct ccanlint license_exists;
115 static void check_has_license(struct manifest *m,
117 unsigned int *timeleft, struct score *score)
121 char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
122 const char *expected;
123 struct doc_section *d;
125 d = find_license_tag(m);
127 score->error = talloc_strdup(score, "No License: tag in _info");
131 m->license = which_license(d);
133 /* If they have a license tag at all, we pass. */
136 expected = expected_link(m->license);
138 len = readlink(license, buf, sizeof(buf));
140 /* Could be a real file... OK if not a standard license. */
141 if (errno == EINVAL) {
143 score->score = score->total;
147 = talloc_asprintf(score,
148 "License in _info is '%s',"
149 " expect LICENSE symlink '%s'",
150 d->lines[0], expected);
153 if (errno == ENOENT) {
154 /* Public domain doesn't really need a file. */
155 if (m->license == LICENSE_PUBLIC_DOMAIN) {
156 score->score = score->total;
159 score->error = talloc_strdup(score,
160 "LICENSE does not exist");
162 license_exists.handle = handle_license_link;
165 err(1, "readlink on %s", license);
167 if (len >= sizeof(buf))
168 errx(1, "Reading symlink %s gave huge result", license);
172 if (!strstarts(buf, "../../licenses/")) {
173 score->error = talloc_asprintf(score,
174 "Expected symlink to"
175 " ../../licenses/..."
181 score->error = talloc_asprintf(score,
182 "License in _info is unknown '%s',"
183 " but LICENSE symlink is '%s'",
188 if (!streq(buf, expected)) {
189 score->error = talloc_asprintf(score,
190 "Expected symlink to %s not %s",
195 score->score = score->total;
198 struct ccanlint license_exists = {
199 .key = "license_exists",
200 .name = "Module has License: entry in _info, and LICENSE symlink/file",
201 .check = check_has_license,
202 .needs = "info_exists"
204 REGISTER_TEST(license_exists);