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