diff mbox series

[v2] gomod: extract license files for omitted modules

Message ID 20250719150105.2697443-1-jeroen@myspectrum.nl
State New
Headers show
Series [v2] gomod: extract license files for omitted modules | expand

Commit Message

Jeroen Hofstee July 19, 2025, 3:01 p.m. UTC
From: Jeroen Hofstee <jhofstee@victronenergy.com>

If a gomod is omitted with a PACKAGECONFIG option its license file
doesn't get extracted to the gomod cache dir and hence do_populate_lic
will complain that the license file isn't found. This adds a task
do_extract_lic after do_compile and before do_populate_lic to make
sure the license files are extracted in such a case.

Signed-off-by: Jeroen Hofstee <jhofstee@victronenergy.com>

---
v2:

- drop bb.utils.mkdirhier(os.path.dirname(cachefile)), extract
  will create it.
- fix typo in the commit msg, it is do_populate_lic
---
 meta/classes-recipe/go-mod.bbclass | 33 ++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
diff mbox series

Patch

diff --git a/meta/classes-recipe/go-mod.bbclass b/meta/classes-recipe/go-mod.bbclass
index a15dda8f0e..4c0496e1e7 100644
--- a/meta/classes-recipe/go-mod.bbclass
+++ b/meta/classes-recipe/go-mod.bbclass
@@ -29,6 +29,39 @@  do_unpack[cleandirs] += "${GOMODCACHE}"
 GO_WORKDIR ?= "${GO_IMPORT}"
 do_compile[dirs] += "${B}/src/${GO_WORKDIR}"
 
+python do_extract_lic() {
+    import zipfile
+
+    lics = d.getVar("LIC_FILES_CHKSUM")
+    cache = d.getVar("GOMODCACHE")
+    dldir = os.path.join(cache, "cache", "download")
+    prefix = "file://pkg/mod/"
+
+    for lic in lics.split():
+        if not lic.startswith(prefix):
+            continue
+
+        try:
+            src = lic[len(prefix):].split(";")[0]
+            url, suffix = src.split("@v")
+            version, _, file = suffix.partition(os.path.sep)
+        except:
+            continue
+
+        cachefile = os.path.join(cache, src)
+        zip = os.path.join(dldir, url, "@v", "v" + version) + ".zip"
+        if os.path.exists(cachefile) or not os.path.exists(zip):
+            continue
+
+        try:
+            bb.note(f"extract {src} from {zip}")
+            zipfile.ZipFile(zip).extract(src, cache)
+        except:
+            bb.warn(f"could not extract {src} from {zip}")
+}
+
 # Make go install unpack the module zip files in the module cache directory
 # before the license directory is polulated with license files.
+# Do make sure licenses get extracted for omitted modules.
+addtask do_extract_lic after do_compile before do_populate_lic
 addtask do_compile before do_populate_lic