diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/README-oe-go-mod-autogen.txt | 81 | ||||
-rwxr-xr-x | scripts/oe-go-mod-autogen.py | 100 |
2 files changed, 178 insertions, 3 deletions
diff --git a/scripts/README-oe-go-mod-autogen.txt b/scripts/README-oe-go-mod-autogen.txt new file mode 100644 index 00000000..e5c11d90 --- /dev/null +++ b/scripts/README-oe-go-mod-autogen.txt | |||
@@ -0,0 +1,81 @@ | |||
1 | Description: | ||
2 | |||
3 | oe-go-mod-autogen.py is a helper script for go mod recipes. It is based | ||
4 | on intiial efforts to use only the git fetcher versus golangs module | ||
5 | infrastructure. | ||
6 | |||
7 | Example: | ||
8 | |||
9 | cmd: <path_to>/meta-virtualization/scripts/oe-go-mod-autogen.py \ | ||
10 | --repo https://github.com/docker/compose --rev v2.20.3 | ||
11 | output: src_uri.inc, relocation.inc, modules.txt | ||
12 | |||
13 | Copy these three generated files to replace the original ones, | ||
14 | then we only need update PV and SRCREV, and docker-compose is upgraded. | ||
15 | |||
16 | See --help for more explanations and examples. | ||
17 | |||
18 | Below are some technical details. | ||
19 | |||
20 | * get module's repo from module name | ||
21 | |||
22 | This script checks the following two URLs to determine the module's repo. | ||
23 | 1. https://<module_name_tweaked>?=go-get=1 | ||
24 | 2. https://pkg.go.dev/<module_name_tweaked> | ||
25 | |||
26 | The module_name_tweaked is derived from module_name, with the last components | ||
27 | removed one by one. Let me use two examples to explain this. | ||
28 | |||
29 | For module_name sigs.k8s.io/json, the sigs.k8s.io/json is first used as | ||
30 | module_name_tweaked for searching. And we can correctly get the repo URL, so | ||
31 | the search stops. | ||
32 | |||
33 | For module_name github.com/k3s-io/etcd/api/v3, the following ones are used | ||
34 | as module_name_tweaked: | ||
35 | github.com/k3s-io/etcd/api/v3 | ||
36 | github.com/k3s-io/etcd/api | ||
37 | github.com/k3s-io/etcd | ||
38 | And when searching 'github.com/k3s-io/etcd', we get the repo URL, so the search | ||
39 | stops. | ||
40 | |||
41 | * determine the srcdir:destdir mapping in 'vendor' creation | ||
42 | |||
43 | To correctly form the 'vendor' directory, the mapping is critical. | ||
44 | This script makes use of tag matching and path matching to determine | ||
45 | the subpath in the repo for the module. | ||
46 | |||
47 | * avoid subpath being overriden by parent path | ||
48 | |||
49 | We need to avoid subpath being overriden by parent path. This is needed | ||
50 | for both SRC_URI ordering in src_uri.inc and the sites mapping ordering | ||
51 | in relocation.inc. This script simply uses the length as the ordering key, | ||
52 | simply for the reason that if a path is a subpath of another path, it must | ||
53 | be longer. | ||
54 | |||
55 | * the .git suffix is removed to sync with each other | ||
56 | |||
57 | Unlike normal recipes, go mod recipe usually have many SRC_URIs. This script | ||
58 | remove the '.git' suffix from repo URL so that the repo URLs are in sync | ||
59 | with each. | ||
60 | |||
61 | * basic directory hierarchy and caching mechanism | ||
62 | |||
63 | <cwd>/repos: hold the repos downloaded and checked | ||
64 | <cwd>/wget-contents: hold the contents to determine the module's repo | ||
65 | <cwd>/wget-contents/<module_name>.repo_url.cache: the repo value cache | ||
66 | This is to avoid unnecessary URL fetching and repo cloning. | ||
67 | |||
68 | * the ERROR_OUT_ON_FETCH_AND_CHECKOUT_FAILURE switch in script | ||
69 | |||
70 | The script must get the correct repo_url, fullsrc_rev and subpath for | ||
71 | each required module in go.mod to correctly generate the src_uri.inc and | ||
72 | relocation.inc files. If this process fails for any required module, this | ||
73 | script stop immediately, as I deliberately set ERROR_OUT_ON_FETCH_AND_CHECKOUT_FAILURE | ||
74 | to True in this script. The purpose is to encourage people to report | ||
75 | problems to meta-virt so that we can improve this script according to | ||
76 | these feedbacks. But this variable can set to False, then the script | ||
77 | only records the failed modules in self.modules_unhandled with reasons | ||
78 | added, people can modify the generated src_uri.inc and relocation.inc | ||
79 | to manually handle these unhandled modules if they are urgent to | ||
80 | add/upgrade some go mod recipes. | ||
81 | |||
diff --git a/scripts/oe-go-mod-autogen.py b/scripts/oe-go-mod-autogen.py index aa155f0f..2970847e 100755 --- a/scripts/oe-go-mod-autogen.py +++ b/scripts/oe-go-mod-autogen.py | |||
@@ -644,17 +644,111 @@ def main(): | |||
644 | epilog=textwrap.dedent('''\ | 644 | epilog=textwrap.dedent('''\ |
645 | 645 | ||
646 | Overview: | 646 | Overview: |
647 | ========= | ||
647 | 648 | ||
648 | go-mod-oe is a tool for processing go dependencies to generate | 649 | go-mod-oe is a tool for processing go dependencies to generate |
649 | dependencies suitable for OE fetcher consumption. | 650 | dependencies suitable for OE fetcher consumption. |
650 | 651 | ||
651 | In particular, it creates a build structure suitable for | 652 | In particular, it creates a build structure suitable for |
652 | '-mod="vendor"' go builds. All dependencies are in the vendor/ | 653 | '-mod="vendor"' go builds. Once complete all go mod dependencies |
653 | directory, so no golang specific fetching or network access happens | 654 | are in the vendor/ directory, so no golang specific fetching or |
654 | during the build. | 655 | network access happens during the build. |
655 | 656 | ||
656 | The files src_uri.inc, relocation.inc and modules.txt are generated | 657 | The files src_uri.inc, relocation.inc and modules.txt are generated |
657 | and suitable for recipe inclusion. | 658 | and suitable for recipe inclusion. |
659 | |||
660 | A recipe build can then use these files to leverage the git fetcher | ||
661 | and related functionality (mirrors, sstate, etc). | ||
662 | |||
663 | Note 1: --rev does not have to be a tag, if you want to track the tip of | ||
664 | a branch specify the latest git has on that branch, and it will | ||
665 | be used. | ||
666 | |||
667 | Note 2: This script does not generate an entire recipe, the way the | ||
668 | the outputs are used can be modified as required. | ||
669 | |||
670 | Note 3: if a go.mod has a bad revision, or needs to be manually updated | ||
671 | to fetch fixes: go.mod in the main repository (see the repos/ | ||
672 | directory). If go.mod is edited, modules.txt also has to be | ||
673 | updated to match the revision information. | ||
674 | |||
675 | How to use in a recipe: | ||
676 | ======================= | ||
677 | |||
678 | There are examples in meta-virtualization of recipes that use this | ||
679 | script and stragegy for builds: docker-compose, nerdcli, k3s | ||
680 | |||
681 | 1) The recipe should set the master repository SRCREV details, and then include | ||
682 | the src_uri.inc file: | ||
683 | |||
684 | SRCREV_nerdcli = "e084a2df4a8861eb5f0b0d32df0643ef24b81093" | ||
685 | SRC_URI = "git://github.com/containerd/nerdctl.git;name=nerdcli;branch=master;protocol=https" | ||
686 | |||
687 | include src_uri.inc | ||
688 | |||
689 | This results in the SRC_URI being fully populated with the main | ||
690 | repository and all dependencies. | ||
691 | |||
692 | 2) The recipe should either copy, or include the relocation.inc file. It sets | ||
693 | a variable "sites" that is a list of source locations (where the src_uri.inc | ||
694 | fetches) and destination in a vendor directory, it also has a do_compile:prepend() | ||
695 | that contains a loop which relocates the fetches into a vendor.copy directory. | ||
696 | |||
697 | It is expected to be processed as follows, before compilation starts: | ||
698 | |||
699 | # sets the "sites" variable and copies files | ||
700 | include relocation.inc | ||
701 | |||
702 | The do_compile:prepend, contains the following loop: | ||
703 | |||
704 | cd ${S}/src/import | ||
705 | # this moves all the fetches into the proper vendor structure | ||
706 | # expected for build | ||
707 | for s in ${sites}; do | ||
708 | site_dest=$(echo $s | cut -d: -f1) | ||
709 | site_source=$(echo $s | cut -d: -f2) | ||
710 | force_flag=$(echo $s | cut -d: -f3) | ||
711 | mkdir -p vendor.copy/$site_dest | ||
712 | if [ -n "$force_flag" ]; then | ||
713 | echo "[INFO] $site_dest: force copying .go files" | ||
714 | rm -rf vendor.copy/$site_dest | ||
715 | rsync -a --exclude='vendor/' --exclude='.git/' vendor.fetch/$site_source/ vendor.copy/$site_dest | ||
716 | else | ||
717 | [ -n "$(ls -A vendor.copy/$site_dest/*.go 2> /dev/null)" ] && { echo "[INFO] vendor.fetch/$site_source -> $site_dest: go copy skipped (files present)" ; true ; } || { echo "[INFO] $site_dest: copying .go files" ; rsync -a --exclude='vendor/' --exclude='.git/' vendor.fetch/$site_source/ vendor.copy/$site_dest ; } | ||
718 | fi | ||
719 | done | ||
720 | |||
721 | The main compile() function, should set the appropriate GO variables, | ||
722 | copy modules.txt and build the appripriate target: | ||
723 | |||
724 | # our copied .go files are to be used for the build | ||
725 | ln -sf vendor.copy vendor | ||
726 | |||
727 | 3) The modules.txt file should be copied into the recipe directory, included | ||
728 | on the SRC_URI and copied into place after the relocation has been | ||
729 | processed. | ||
730 | |||
731 | # patches and config | ||
732 | SRC_URI += "file://0001-Makefile-allow-external-specification-of-build-setti.patch \\ | ||
733 | file://modules.txt \ | ||
734 | " | ||
735 | |||
736 | ..... | ||
737 | |||
738 | cp ${WORKDIR}/modules.txt vendor/ | ||
739 | |||
740 | Example: Updating the K3S recipe | ||
741 | ================================ | ||
742 | |||
743 | % cd meta-virtualization/recipe-containers/k3s/ | ||
744 | # produces src_uri.inc, relocation.inc and modules.txt in the current directory | ||
745 | % ../../scripts/oe-go-mod-autogen.py --repo https://github.com/rancher/k3s.git --rev v1.27.5+k3s1 | ||
746 | |||
747 | % cp modules.txt k3s/ | ||
748 | |||
749 | ... add and commit files. | ||
750 | |||
751 | |||
658 | ''')) | 752 | ''')) |
659 | parser.add_argument("--repo", help = "Repo for the recipe.", required=True) | 753 | parser.add_argument("--repo", help = "Repo for the recipe.", required=True) |
660 | parser.add_argument("--rev", help = "Revision for the recipe.", required=True) | 754 | parser.add_argument("--rev", help = "Revision for the recipe.", required=True) |