1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/tal/tal.h>
3 #include <ccan/tal/str/str.h>
13 #include <ccan/str/str.h>
14 #include <ccan/take/take.h>
16 /* We might need more ../ for nested modules. */
17 static const char *link_prefix(struct manifest *m)
19 char *prefix = tal_strdup(m, "../../");
22 for (i = 0; i < strcount(m->modname, "/"); i++)
23 prefix = tal_strcat(m, take(prefix), "../");
25 return tal_strcat(m, take(prefix), "licenses/");
28 static const char *expected_link(const tal_t *ctx,
29 const char *prefix, enum license license)
31 const char *shortname;
34 case LICENSE_LGPLv2_PLUS:
36 shortname = "LGPL-2.1";
43 case LICENSE_GPLv2_PLUS:
54 shortname = "BSD-3CLAUSE";
58 shortname = "BSD-MIT";
69 return tal_strcat(ctx, prefix, shortname);
72 static void handle_license_link(struct manifest *m, struct score *score)
74 struct doc_section *d = find_license_tag(m);
75 const char *prefix = link_prefix(m);
76 const char *link = tal_fmt(m, "%s/LICENSE", m->dir);
77 const char *ldest = expected_link(score, prefix, m->license);
81 "Most modules want a copy of their license, so usually we create a\n"
82 "LICENSE symlink into %s to avoid too many copies.\n", prefix);
84 /* FIXME: make ask printf-like */
85 q = tal_fmt(m, "Set up link to %s (license is %s)?",
88 if (symlink(ldest, link) != 0)
89 err(1, "Creating symlink %s -> %s", link, ldest);
93 extern struct ccanlint license_exists;
95 static void check_has_license(struct manifest *m,
96 unsigned int *timeleft, struct score *score)
100 char *license = tal_fmt(m, "%s/LICENSE", m->dir);
101 const char *expected;
102 struct doc_section *d;
103 const char *prefix = link_prefix(m);
105 d = find_license_tag(m);
107 score->error = tal_strdup(score, "No License: tag in _info");
111 m->license = which_license(d);
112 if (m->license == LICENSE_UNKNOWN) {
113 score_file_error(score, m->info_file, d->srcline,
114 "WARNING: unknown License: in _info: %s",
116 /* FIXME: For historical reasons, don't fail here. */
121 /* If they have a license tag at all, we pass. */
124 expected = expected_link(m, prefix, m->license);
126 len = readlink(license, buf, sizeof(buf));
128 /* Could be a real file... OK if not a standard license. */
129 if (errno == EINVAL) {
131 score->score = score->total;
136 "License in _info is '%s',"
137 " expect LICENSE symlink '%s'",
138 d->lines[0], expected);
141 if (errno == ENOENT) {
142 /* Public domain doesn't really need a file. */
143 if (m->license == LICENSE_PUBLIC_DOMAIN) {
144 score->score = score->total;
147 score->error = tal_strdup(score,
148 "LICENSE does not exist");
150 license_exists.handle = handle_license_link;
153 err(1, "readlink on %s", license);
155 if (len >= sizeof(buf))
156 errx(1, "Reading symlink %s gave huge result", license);
160 if (!strstarts(buf, prefix)) {
161 score->error = tal_fmt(score,
162 "Expected symlink into %s not %s",
168 score->error = tal_fmt(score,
169 "License in _info is unknown '%s',"
170 " but LICENSE symlink is '%s'",
175 if (!streq(buf, expected)) {
176 score->error = tal_fmt(score,
177 "Expected symlink to %s not %s",
182 score->score = score->total;
185 struct ccanlint license_exists = {
186 .key = "license_exists",
187 .name = "Module has License: entry in _info, and LICENSE symlink/file",
188 .check = check_has_license,
189 .needs = "info_exists"
191 REGISTER_TEST(license_exists);