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