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