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