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