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