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