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