Unblock ocaml-camlimages, it *should* now build with compiler changes.
[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 final"
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";                        (* orphaned *)
27 ]
28 let blocked pkg = List.mem pkg blocked
29
30 (* These packages are treated as if they have been rebuilt. *)
31 let ignored = [
32   "ocaml-srpm-macros";             (* don't need to build this *)
33   "ocaml";                         (* rebuilt by hand *)
34   "whenjobs";                      (* obsolete *)
35 ]
36 let ignored pkg = List.mem pkg ignored
37
38 (* List of OCaml-related source package names. *)
39 let source_packages =
40   let dirs = shlines "cd %s && ls -1d ocaml*" fedora_dir in
41   dirs @ [ "alt-ergo"; "apron"; "brltty"; "coccinelle"; "coq";
42            "cduce"; "frama-c"; "gappalib-coq"; "graphviz"; "hevea"; "hivex";
43            "js-of-ocaml"; "llvm"; "plplot"; "virt-top"; "why3"; "xen";
44            "flocq" (* no OCaml code, but needs to be rebuilt after Coq *);
45            "guestfs-browser";
46            "virt-dmesg" ]
47
48 (* Dependencies of each package.  (pkg, [deps ...]) *)
49 let pkg_deps = dependencies branch source_packages
50
51 (* Remove blocked packages and packages which have a blocked package
52  * as a dependency (recursively).
53  *)
54 let source_packages =
55   let rec is_blocked pkg =
56     if blocked pkg then true
57     else (
58       let deps = List.assoc pkg pkg_deps in
59       List.exists is_blocked deps
60     )
61   in
62   List.filter (fun pkg -> not (is_blocked pkg)) source_packages
63
64 (* Short the dependencies lists so that the build order is stable
65  * each time it runs.
66  *)
67 let pkg_deps =
68   List.map (fun (pkg, deps) -> pkg, List.sort compare deps) pkg_deps
69
70 (* Sort the source packages so that the packages with the largest
71  * number of reverse dependencies [other packages that depend on it]
72  * appear earlier in the list, on the basis that building these
73  * packages first has the greatest advantage.
74  *)
75 let source_packages =
76   let rdeps pkg =
77     Utils.filter_map (
78       fun (rdep, deps) -> if List.mem pkg deps then Some rdep else None
79     ) pkg_deps
80   in
81   let cmp p1 p2 =
82     let r1 = rdeps p1 and r2 = rdeps p2 in
83     let n1 = List.length r1 and n2 = List.length r2 in
84     if n1 <> n2 then compare n2 n1 else compare p1 p2
85   in
86   List.sort cmp source_packages
87
88 let () =
89   printf "final list of source packages = %s\n%!"
90     (String.concat " " source_packages)
91
92 (* We could make this a goal, but it's cheap enough to run it unconditionally. *)
93 let install_build_dependencies pkg =
94   sh "sudo yum clean all --disablerepo=\\* --enablerepo=%s"
95     (quote yum_repo);
96   sh "sudo yum-builddep -y --disablerepo=\\* --enablerepo=%s %s"
97     (quote yum_repo) (fedora_specfile pkg branch)
98
99 (* Unset MAKEFLAGS so it doesn't affect local builds. *)
100 let () = Unix.putenv "MAKEFLAGS" ""
101
102 (* Goal: rebuild all packages. *)
103 let rec goal all () =
104   let n = List.length source_packages in
105   List.iteri (
106     fun i pkg ->
107       require (rebuild_started pkg);
108       printf "*** *** rebuilt %d/%d packages *** ***\n%!" (i+1) n
109   ) source_packages
110
111 (* Goal: That 'package' has been rebuilt and exists in Koji. *)
112 and rebuilt pkg =
113   let specfile = fedora_specfile pkg branch in
114
115   (* Note: verrel may change as we go along, so don't assign it to
116    * variable.
117    *)
118
119   (* Note the target must be both of these because the old verrel
120    * could exist as a koji build without it having been part of the
121    * rebuild.
122    *)
123   target (ignored pkg ||
124             (file_contains_string specfile rebuild_name &&
125                koji_build_state (fedora_verrel pkg branch) == `Complete));
126
127   (* Ignored packages are treated as if they have been rebuilt. *)
128   if not (ignored pkg) then (
129
130     (* Start the rebuild. *)
131     require (rebuild_started pkg);
132
133     (* Wait for the build state to reach a conclusion. *)
134     let rec loop () =
135       match koji_build_state (fedora_verrel pkg branch) with
136       | `No_such_build ->
137         failwith (sprintf "rebuild of package %s: no build found" pkg)
138       | `Building ->
139         sleep 60;
140         loop ()
141       | `Complete ->
142         ()
143       | `Deleted ->
144         failwith (sprintf "rebuild of package %s: deleted" pkg)
145       | `Failed ->
146         failwith (sprintf "rebuild of package %s: failed" pkg)
147       | `Canceled ->
148         failwith (sprintf "rebuild of package %s: canceled" pkg)
149     in
150     loop ();
151
152     (* Wait for the build to appear in Koji repo. *)
153     koji_wait_repo koji_target (fedora_verrel pkg branch)
154   )
155
156 (* Goal: The rebuild of the package has started, but we haven't waited
157  * for it to finish.
158  *)
159 and rebuild_started pkg =
160   let deps = List.assoc pkg pkg_deps in
161   let specfile = fedora_specfile pkg branch in
162
163   (* Note the target must be both of these because the old verrel
164    * could exist as a koji build without it having been part of the
165    * rebuild.
166    *)
167   target (ignored pkg ||
168             (file_contains_string specfile rebuild_name &&
169                (match koji_build_state (fedora_verrel pkg branch) with
170                | `Building | `Complete -> true
171                | `Deleted | `Failed | `Canceled | `No_such_build -> false)));
172
173   (* All dependent packages must have been fully rebuilt and in the
174    * repo first.
175    *)
176   List.iter (fun dep -> require (rebuilt dep)) deps;
177
178   (* Ignored packages are treated as if they have been rebuilt. *)
179   if not (ignored pkg) then (
180 (*
181     (* A local test build must succeed. *)
182     require (local_build_succeeded pkg);
183 *)
184     (* local_build_succeeded normally does this ... *)
185     require (specfile_updated pkg);
186
187     (* Rebuild the package in Koji.  Don't wait ... *)
188     koji_build ~wait:false pkg branch;
189
190     (* ... but the build doesn't appear in Koji (eg. in 'koji
191      * buildinfo') until the SRPM has been built.  This can take quite
192      * some time.  Loop here until the build appears.
193      *)
194     let rec loop () =
195       match koji_build_state (fedora_verrel pkg branch) with
196       | `No_such_build ->
197         sleep 60;
198         loop ();
199       | `Building | `Complete ->
200         ()
201       | `Deleted ->
202         failwith (sprintf "rebuild of package %s: deleted" pkg)
203       | `Failed ->
204         failwith (sprintf "rebuild of package %s: failed" pkg)
205       | `Canceled ->
206         failwith (sprintf "rebuild of package %s: canceled" pkg)
207     in
208     loop ()
209   )
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
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 (* - XXX why did we do this here?
249   install_build_dependencies pkg;
250 *)
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     fedpkg commit -c
267     fedpkg push
268   " repodir