Add virt-top to the list of packages to 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 koji_target = "rawhide"
13
14 (* The name of the rebuild, and also the magic substring that must
15  * appear in the %changelog when the package has been rebuilt.
16  *)
17 let rebuild_name = "ocaml-4.02.0-0.8.git10e45753.fc22"
18
19 (* Local repository that contains build dependencies. *)
20 let yum_repo = "koji-rawhide"
21
22 (* Packages that have problems.  These block the packages and all
23  * dependent packages.
24  *)
25 let blocked = [
26   "ocaml-mikmatch";                     (* build failure on 4.02.0 *)
27   "ocaml-omake";                        (* build failure on 4.02.0 with hevea *)
28   "ocaml-p3l";                          (* build failure on 4.02.0 -warn-error A *)
29   "ocaml-pa-do";                        (* build failure, complex *)
30   "ocaml-lwt";                          (* build failure on 4.02.0 *)
31   "ocaml-preludeml";                    (* build failure *)
32   "cduce";                              (* broken again with latest 4.02 *)
33   "frama-c";                            (* build failure *)
34   "gappalib-coq";                       (* build failure in configure script *)
35 ]
36 let blocked pkg = List.mem pkg blocked
37
38 (* These packages are treated as if they have been rebuilt. *)
39 let ignored = [
40   "ocaml-srpm-macros";             (* don't need to build this *)
41   "ocaml";                         (* rebuilt by hand *)
42   "whenjobs";                      (* obsolete *)
43   "libguestfs";                    (* rebuilt by hand *)
44   "graphviz";                      (* rebuilt by hand *)
45   "xen";                           (* already done *)
46   "plplot";                        (* already done *)
47 ]
48 let ignored pkg = List.mem pkg ignored
49
50 (* List of OCaml-related source package names. *)
51 let source_packages =
52   let dirs = shlines "cd %s && ls -1d ocaml*" fedora_dir in
53   dirs @ [ "alt-ergo"; "apron"; "brltty"; "coccinelle"; "coq";
54            "cduce"; "frama-c"; "gappalib-coq"; "graphviz"; "hivex";
55            "js-of-ocaml"; "llvm"; "plplot"; "why3"; "xen"; "virt-top" ]
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 (* 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 (* Unset MAKEFLAGS so it doesn't affect local builds. *)
109 let () = Unix.putenv "MAKEFLAGS" ""
110
111 (* Goal: rebuild all packages. *)
112 let rec goal all () =
113   let n = List.length source_packages in
114   List.iteri (
115     fun i pkg ->
116       require (rebuild_started pkg);
117       printf "*** *** rebuilt %d/%d packages *** ***\n%!" (i+1) n
118   ) source_packages
119
120 (* Goal: That 'package' has been rebuilt and exists in Koji. *)
121 and rebuilt pkg =
122   let specfile = fedora_specfile pkg branch in
123
124   (* Note: verrel may change as we go along, so don't assign it to
125    * variable.
126    *)
127
128   (* Note the target must be both of these because the old verrel
129    * could exist as a koji build without it having been part of the
130    * rebuild.
131    *)
132   target (ignored pkg ||
133             (file_contains_string specfile rebuild_name &&
134                koji_build_state (fedora_verrel pkg branch) == `Complete));
135
136   (* Ignored packages are treated as if they have been rebuilt. *)
137   if not (ignored pkg) then (
138
139     (* Start the rebuild. *)
140     require (rebuild_started pkg);
141
142     (* Wait for the build state to reach a conclusion. *)
143     let rec loop () =
144       match koji_build_state (fedora_verrel pkg branch) with
145       | `No_such_build ->
146         failwith (sprintf "rebuild of package %s: no build found" pkg)
147       | `Building ->
148         sleep 60;
149         loop ()
150       | `Complete ->
151         ()
152       | `Deleted ->
153         failwith (sprintf "rebuild of package %s: deleted" pkg)
154       | `Failed ->
155         failwith (sprintf "rebuild of package %s: failed" pkg)
156       | `Canceled ->
157         failwith (sprintf "rebuild of package %s: canceled" pkg)
158     in
159     loop ();
160
161     (* Wait for the build to appear in Koji repo. *)
162     koji_wait_repo koji_target (fedora_verrel pkg branch)
163   )
164
165 (* Goal: The rebuild of the package has started, but we haven't waited
166  * for it to finish.
167  *)
168 and rebuild_started pkg =
169   let deps = List.assoc pkg pkg_deps in
170   let specfile = fedora_specfile pkg branch in
171
172   (* Note the target must be both of these because the old verrel
173    * could exist as a koji build without it having been part of the
174    * rebuild.
175    *)
176   target (ignored pkg ||
177             (file_contains_string specfile rebuild_name &&
178                (match koji_build_state (fedora_verrel pkg branch) with
179                | `Building | `Complete -> true
180                | `Deleted | `Failed | `Canceled | `No_such_build -> false)));
181
182   (* All dependent packages must have been fully rebuilt and in the
183    * repo first.
184    *)
185   List.iter (fun dep -> require (rebuilt dep)) deps;
186
187   (* Ignored packages are treated as if they have been rebuilt. *)
188   if not (ignored pkg) then (
189     (* A local test build must succeed. *)
190     require (local_build_succeeded pkg);
191
192     (* Rebuild the package in Koji.  Don't wait ... *)
193     koji_build ~wait:false pkg branch;
194
195     (* ... but the build doesn't appear in Koji (eg. in 'koji
196      * buildinfo') until the SRPM has been built.  This can take quite
197      * some time.  Loop here until the build appears.
198      *)
199     let rec loop () =
200       match koji_build_state (fedora_verrel pkg branch) with
201       | `No_such_build ->
202         sleep 60;
203         loop ();
204       | `Building | `Complete ->
205         ()
206       | `Deleted ->
207         failwith (sprintf "rebuild of package %s: deleted" pkg)
208       | `Failed ->
209         failwith (sprintf "rebuild of package %s: failed" pkg)
210       | `Canceled ->
211         failwith (sprintf "rebuild of package %s: canceled" pkg)
212     in
213     loop ()
214   )
215
216 and local_build_succeeded pkg =
217   (* The specfile must have been updated. *)
218   require (specfile_updated pkg);
219
220   let key =
221     sprintf "fedora_ocaml_local_build_%s_%s" pkg (fedora_verrel pkg branch) in
222
223   target (memory_exists key);
224
225   install_build_dependencies pkg;
226
227  (* Do a local test build to ensure the Koji build will work. *)
228   sh "
229     cd %s
230      fedpkg local
231   " (fedora_repo pkg branch);
232
233   memory_set key "1"
234
235 and specfile_updated pkg =
236   let repodir = fedora_repo pkg branch in
237   let specfile = fedora_specfile pkg branch in
238
239   sh "
240     cd %s
241     rm -rf x86_64 noarch *.src.rpm .build* clog
242     git fetch
243   " repodir;
244
245   if not (git_has_local_changes repodir) then
246     sh "
247       cd %s
248       git pull --rebase
249     " repodir;
250
251   install_build_dependencies pkg;
252
253   (* For rationale behind always bumping the spec file, see comment
254    * in 'fedora.ml'.
255    *)
256   let title =
257     if not (file_contains_string specfile rebuild_name) then
258       rebuild_name ^ " rebuild."
259     else
260       "Bump release and rebuild." in
261   sh "rpmdev-bumpspec -c %s %s" (quote title) specfile;
262
263   (* XXX Automate common specfile fixes. *)
264
265   sh "
266     cd %s
267     echo 'Please make further changes as required to the spec file %s.spec'
268     echo '(Press return key)'
269     read
270     emacs -nw %s
271     echo 'OK to commit this change? (press ^C if not)'
272     read
273     fedpkg commit -c
274     echo 'OK to push this change? (press ^C if not)'
275     read
276     fedpkg push
277   " repodir
278     pkg
279     specfile