1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/tal/tal.h>
3 #include <ccan/tal/str/str.h>
4 #include <ccan/tal/path/path.h>
14 #include <ccan/str/str.h>
15 #include <ccan/take/take.h>
17 /* We might need more ../ for nested modules. */
18 static const char *link_prefix(struct manifest *m)
20 char *prefix = tal_strdup(m, "../../");
23 for (i = 0; i < strcount(m->modname, "/"); i++)
24 prefix = tal_strcat(m, take(prefix), "../");
26 return tal_strcat(m, take(prefix), "licenses/");
29 static const char *expected_link(const tal_t *ctx,
30 const char *prefix, enum license license)
32 const char *shortname;
35 case LICENSE_LGPLv2_PLUS:
37 shortname = "LGPL-2.1";
44 case LICENSE_GPLv2_PLUS:
55 shortname = "BSD-3CLAUSE";
59 shortname = "BSD-MIT";
70 return tal_strcat(ctx, prefix, shortname);
73 static void handle_license_link(struct manifest *m, struct score *score)
75 struct doc_section *d = find_license_tag(m);
76 const char *prefix = link_prefix(m);
77 const char *link = path_join(m, m->dir, "LICENSE");
78 const char *ldest = expected_link(score, prefix, m->license);
82 "Most modules want a copy of their license, so usually we create a\n"
83 "LICENSE symlink into %s to avoid too many copies.\n", prefix);
85 /* FIXME: make ask printf-like */
86 q = tal_fmt(m, "Set up link to %s (license is %s)?",
89 if (symlink(ldest, link) != 0)
90 err(1, "Creating symlink %s -> %s", link, ldest);
94 extern struct ccanlint license_exists;
96 static void check_has_license(struct manifest *m,
97 unsigned int *timeleft, struct score *score)
101 char *license = path_join(m, m->dir, "LICENSE");
102 const char *expected;
103 struct doc_section *d;
104 const char *prefix = link_prefix(m);
106 d = find_license_tag(m);
108 score->error = tal_strdup(score, "No License: tag in _info");
112 m->license = which_license(d);
113 if (m->license == LICENSE_UNKNOWN) {
114 score_file_error(score, m->info_file, d->srcline,
115 "WARNING: unknown License: in _info: %s",
117 /* FIXME: For historical reasons, don't fail here. */
122 /* If they have a license tag at all, we pass. */
125 expected = expected_link(m, prefix, m->license);
127 len = readlink(license, buf, sizeof(buf));
129 /* Could be a real file... OK if not a standard license. */
130 if (errno == EINVAL) {
132 score->score = score->total;
137 "License in _info is '%s',"
138 " expect LICENSE symlink '%s'",
139 d->lines[0], expected);
142 if (errno == ENOENT) {
143 /* Public domain doesn't really need a file. */
144 if (m->license == LICENSE_PUBLIC_DOMAIN) {
145 score->score = score->total;
148 score->error = tal_strdup(score,
149 "LICENSE does not exist");
151 license_exists.handle = handle_license_link;
154 err(1, "readlink on %s", license);
156 if (len >= sizeof(buf))
157 errx(1, "Reading symlink %s gave huge result", license);
161 if (!strstarts(buf, prefix)) {
162 score->error = tal_fmt(score,
163 "Expected symlink into %s not %s",
169 score->error = tal_fmt(score,
170 "License in _info is unknown '%s',"
171 " but LICENSE symlink is '%s'",
176 if (!streq(buf, expected)) {
177 score->error = tal_fmt(score,
178 "Expected symlink to %s not %s",
183 score->score = score->total;
186 struct ccanlint license_exists = {
187 .key = "license_exists",
188 .name = "Module has License: entry in _info, and LICENSE symlink/file",
189 .check = check_has_license,
190 .needs = "info_exists"
192 REGISTER_TEST(license_exists);