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