Update branches for libguestfs 1.32.
[goaljobs-goals.git] / fedora_ocaml_rebuild.ml
1 (* Perform a complete Fedora OCaml rebuild, in build order. *)
2
3 open Unix
4 open Printf
5
6 open Goaljobs
7 open Config
8 open Git
9 open Fedora
10
11 let branch = "master"
12 let side_tag = Some "f24-ocaml"
13
14 let koji_target =
15   match side_tag with
16   | Some t -> t
17   | None -> "rawhide"
18
19 (* The name of the rebuild, and also the magic substring that must
20  * appear in the %changelog when the package has been rebuilt.
21  *)
22 let rebuild_name = "OCaml 4.02.3"
23
24 (* Local repository that contains build dependencies. *)
25 let yum_repo = "koji-rawhide"
26
27 (* Packages that have problems.  These block the packages and all
28  * dependent packages.
29  *)
30 let blocked = [
31   "ocaml-pa-do";                        (* orphaned *)
32   "ocaml-camlp5";                       (* not updated to 4.02.3 yet *)
33 ]
34 let blocked pkg = List.mem pkg blocked
35
36 (* These packages are treated as if they have been rebuilt. *)
37 let ignored = [
38   "ocaml-srpm-macros";             (* don't need to build this *)
39   "ocaml";                         (* rebuilt by hand *)
40   "whenjobs";                      (* obsolete *)
41 ]
42 let ignored pkg = List.mem pkg ignored
43
44 (* List of OCaml-related source package names. *)
45 let source_packages =
46   let dirs = shlines "cd %s && ls -1d ocaml*" fedora_dir in
47   dirs @ [ "alt-ergo"; "apron"; "brltty"; "coccinelle"; "coq";
48            "cduce"; "frama-c"; "gappalib-coq"; "graphviz"; "hevea"; "hivex";
49            "js-of-ocaml"; "plplot"; "virt-top"; "why3"; "z3";
50            "flocq" (* no OCaml code, but needs to be rebuilt after Coq *);
51            "guestfs-browser"; "libguestfs";
52            "virt-dmesg" ]
53
54 (* Dependencies of each package.  (pkg, [deps ...]) *)
55 let pkg_deps = dependencies branch source_packages
56
57 (* Remove blocked packages and packages which have a blocked package
58  * as a dependency (recursively).
59  *)
60 let source_packages =
61   let rec is_blocked pkg =
62     if blocked pkg then true
63     else (
64       let deps = List.assoc pkg pkg_deps in
65       List.exists is_blocked deps
66     )
67   in
68   List.filter (fun pkg -> not (is_blocked pkg)) source_packages
69
70 (* Short the dependencies lists so that the build order is stable
71  * each time it runs.
72  *)
73 let pkg_deps =
74   List.map (fun (pkg, deps) -> pkg, List.sort compare deps) pkg_deps
75
76 (* Sort the source packages so that the packages with the largest
77  * number of reverse dependencies [other packages that depend on it]
78  * appear earlier in the list, on the basis that building these
79  * packages first has the greatest advantage.
80  *)
81 let source_packages =
82   let rdeps pkg =
83     Utils.filter_map (
84       fun (rdep, deps) -> if List.mem pkg deps then Some rdep else None
85     ) pkg_deps
86   in
87   let cmp p1 p2 =
88     let r1 = rdeps p1 and r2 = rdeps p2 in
89     let n1 = List.length r1 and n2 = List.length r2 in
90     if n1 <> n2 then compare n2 n1 else compare p1 p2
91   in
92   List.sort cmp source_packages
93
94 let () =
95   printf "final list of source packages = %s\n%!"
96     (String.concat " " source_packages)
97
98 (*
99 (* We could make this a goal, but it's cheap enough to run it unconditionally. *)
100 let install_build_dependencies pkg =
101   sh "sudo yum clean all --disablerepo=\\* --enablerepo=%s"
102     (quote yum_repo);
103   sh "sudo yum-builddep -y --disablerepo=\\* --enablerepo=%s %s"
104     (quote yum_repo) (fedora_specfile pkg branch)
105  *)
106
107 (* Unset MAKEFLAGS so it doesn't affect local builds. *)
108 let () = Unix.putenv "MAKEFLAGS" ""
109
110 (* Goal: rebuild all packages. *)
111 let rec goal all () =
112   let n = List.length source_packages in
113   List.iteri (
114     fun i pkg ->
115       require (rebuild_started pkg);
116       printf "*** *** rebuilt %d/%d packages *** ***\n%!" (i+1) n
117   ) source_packages
118
119 (* Goal: That 'package' has been rebuilt and exists in Koji. *)
120 and rebuilt pkg =
121   let specfile = fedora_specfile pkg branch in
122
123   (* Note: verrel may change as we go along, so don't assign it to
124    * variable.
125    *)
126
127   (* Note the target must be both of these because the old verrel
128    * could exist as a koji build without it having been part of the
129    * rebuild.
130    *)
131   target (ignored pkg ||
132             (file_contains_string specfile rebuild_name &&
133                koji_build_state (fedora_verrel pkg branch) == `Complete));
134
135   (* Ignored packages are treated as if they have been rebuilt. *)
136   if not (ignored pkg) then (
137
138     (* Start the rebuild. *)
139     require (rebuild_started pkg);
140
141     (* Wait for the build state to reach a conclusion. *)
142     let rec loop () =
143       match koji_build_state (fedora_verrel pkg branch) with
144       | `No_such_build ->
145         failwith (sprintf "rebuild of package %s: no build found" pkg)
146       | `Building ->
147         sleep 60;
148         loop ()
149       | `Complete ->
150         ()
151       | `Deleted ->
152         failwith (sprintf "rebuild of package %s: deleted" pkg)
153       | `Failed ->
154         failwith (sprintf "rebuild of package %s: failed" pkg)
155       | `Canceled ->
156         failwith (sprintf "rebuild of package %s: canceled" pkg)
157     in
158     loop ();
159
160     (* Wait for the build to appear in Koji repo. *)
161     koji_wait_repo koji_target (fedora_verrel pkg branch)
162   )
163
164 (* Goal: The rebuild of the package has started, but we haven't waited
165  * for it to finish.
166  *)
167 and rebuild_started pkg =
168   let deps = List.assoc pkg pkg_deps in
169   let specfile = fedora_specfile pkg branch in
170
171   (* Note the target must be both of these because the old verrel
172    * could exist as a koji build without it having been part of the
173    * rebuild.
174    *)
175   target (ignored pkg ||
176             (file_contains_string specfile rebuild_name &&
177                (match koji_build_state (fedora_verrel pkg branch) with
178                | `Building | `Complete -> true
179                | `Deleted | `Failed | `Canceled | `No_such_build -> false)));
180
181   (* All dependent packages must have been fully rebuilt and in the
182    * repo first.
183    *)
184   List.iter (fun dep -> require (rebuilt dep)) deps;
185
186   (* Ignored packages are treated as if they have been rebuilt. *)
187   if not (ignored pkg) then (
188 (*
189     (* A local test build must succeed. *)
190     require (local_build_succeeded pkg);
191 *)
192     (* local_build_succeeded normally does this ... *)
193     require (specfile_updated pkg);
194
195     (* Rebuild the package in Koji.  Don't wait ... *)
196     koji_build ~wait:false ?side_tag pkg branch;
197
198     (* ... but the build doesn't appear in Koji (eg. in 'koji
199      * buildinfo') until the SRPM has been built.  This can take quite
200      * some time.  Loop here until the build appears.
201      *)
202     let rec loop () =
203       match koji_build_state (fedora_verrel pkg branch) with
204       | `No_such_build ->
205         sleep 60;
206         loop ();
207       | `Building | `Complete ->
208         ()
209       | `Deleted ->
210         failwith (sprintf "rebuild of package %s: deleted" pkg)
211       | `Failed ->
212         failwith (sprintf "rebuild of package %s: failed" pkg)
213       | `Canceled ->
214         failwith (sprintf "rebuild of package %s: canceled" pkg)
215     in
216     loop ()
217   )
218
219 (*
220 and local_build_succeeded pkg =
221   (* The specfile must have been updated. *)
222   require (specfile_updated pkg);
223
224   let key =
225     sprintf "fedora_ocaml_local_build_%s_%s" pkg (fedora_verrel pkg branch) in
226
227   target (memory_exists key);
228
229   install_build_dependencies pkg;
230
231  (* Do a local test build to ensure the Koji build will work. *)
232   sh "
233     cd %s
234      fedpkg local
235   " (fedora_repo pkg branch);
236
237   memory_set key "1"
238 *)
239
240 and specfile_updated pkg =
241   let repodir = fedora_repo pkg branch in
242   let specfile = fedora_specfile pkg branch in
243
244   sh "
245     cd %s
246     rm -rf x86_64 noarch *.src.rpm .build* clog
247     git fetch
248   " repodir;
249
250   if not (git_has_local_changes repodir) then
251     sh "
252       cd %s
253       git pull --rebase
254     " repodir;
255
256 (* - XXX why did we do this here?
257   install_build_dependencies pkg;
258 *)
259
260   (* For rationale behind always bumping the spec file, see comment
261    * in 'fedora.ml'.
262    *)
263   let title =
264     if not (file_contains_string specfile rebuild_name) then
265       rebuild_name ^ " rebuild."
266     else
267       "Bump release and rebuild." in
268   sh "rpmdev-bumpspec -c %s %s" (quote title) specfile;
269
270   (* XXX Automate common specfile fixes. *)
271
272   sh "
273     cd %s
274     fedpkg commit -c
275     fedpkg push
276   " repodir