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 UNNEEDED,
102 char *license = path_join(m, m->dir, "LICENSE");
103 const char *expected;
104 struct doc_section *d;
105 const char *prefix = link_prefix(m);
107 d = find_license_tag(m);
109 score->error = tal_strdup(score, "No License: tag in _info");
113 m->license = which_license(d);
114 if (m->license == LICENSE_UNKNOWN) {
115 score_file_error(score, m->info_file, d->srcline,
116 "WARNING: unknown License: in _info: %s",
118 /* FIXME: For historical reasons, don't fail here. */
123 /* If they have a license tag at all, we pass. */
126 expected = expected_link(m, prefix, m->license);
128 len = readlink(license, buf, sizeof(buf));
130 /* Could be a real file... OK if not a standard license. */
131 if (errno == EINVAL) {
133 score->score = score->total;
138 "License in _info is '%s',"
139 " expect LICENSE symlink '%s'",
140 d->lines[0], expected);
143 if (errno == ENOENT) {
144 /* Public domain doesn't really need a file. */
145 if (m->license == LICENSE_PUBLIC_DOMAIN) {
146 score->score = score->total;
149 score->error = tal_strdup(score,
150 "LICENSE does not exist");
152 license_exists.handle = handle_license_link;
155 err(1, "readlink on %s", license);
157 if (len >= sizeof(buf))
158 errx(1, "Reading symlink %s gave huge result", license);
162 if (!strstarts(buf, prefix)) {
163 score->error = tal_fmt(score,
164 "Expected symlink into %s not %s",
170 score->error = tal_fmt(score,
171 "License in _info is unknown '%s',"
172 " but LICENSE symlink is '%s'",
177 if (!streq(buf, expected)) {
178 score->error = tal_fmt(score,
179 "Expected symlink to %s not %s",
184 score->score = score->total;
187 struct ccanlint license_exists = {
188 .key = "license_exists",
189 .name = "Module has License: entry in _info, and LICENSE symlink/file",
190 .check = check_has_license,
191 .needs = "info_exists"
193 REGISTER_TEST(license_exists);