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