Fixed up some leftover git merge dingleberries.
[kakapo:kakapo.git] / setup.nqp
1 #! /usr/bin/env parrot-nqp
2 # Copyright 2009-2010, Austin Hastings. See accompanying LICENSE file, or
3 # http://www.opensource.org/licenses/artistic-license-2.0.php for license.
4
5 # =head1 NAME
6 #
7 # setup.nqp - Configure and build the Kakapo library.
8 #
9 # =head1 USAGE
10 #
11 #     $ parrot-nqp setup.nqp build
12 #     $ parrot-nqp setup.nqp test
13 #     $ sudo parrot-nqp setup.nqp install
14
15 INIT {
16         pir::load_language('parrot');
17         pir::load_bytecode('distutils.pbc');
18
19         pir::load_bytecode('dumper.pir');
20 }
21
22 MAIN(get_args());
23
24 sub new_hash(*%hash) {
25         %hash;
26 }
27
28 sub MAIN(@argv) {
29
30         my %kakapo := new_hash(
31                 :name(          'Kakapo' ),
32                 :abstract(              'Run-time library for NQP programs on the Parrot VM' ),
33                 :authority(             'http://gitorious.org/austin' ),
34                 :copyright_holder(      'Austin Hastings' ),
35                 :doc_files(             <README CREDITS> ),
36                 :keywords(              < library  nqp  parrot  runtime  stand alone  xunit
37                                         unit testing  matcher  pmc  methods  >),
38                 :license_type(  'Artistic License 2.0' ),
39                 :license_uri(           'http://www.perlfoundation.org/artistic_license_2_0' ),
40                 :checkout_uri(  'git://gitorious.org/kakapo/kakapo.git' ),
41                 :browser_uri(   'http://code.google.com/p/kakapo-parrot/' ),
42                 :project_uri(           'git://gitorious.org/kakapo/kakapo.git' ),
43
44                 :harness_files(         pir::join(' ', <
45                         !t/Pmc
46                         !t/Structure
47                         t
48                 >) ),
49
50                 :release_id(                    'release-9' ),
51                 :release_dir_format(    'released/%s'),
52                 :vdd_file(                      'vdd.txt' ),
53         );
54
55         %kakapo<copy_templates><library/krt0.pir>                       := <src/Internals/krt0.pir_tmpl>;
56         %kakapo<copy_templates><src/Internals/kakapo_bottom.pir> := <src/Internals/kakapo_bottom.pir_tmpl>;
57         %kakapo<copy_templates><src/Internals/kakapo_top.pir>   := <src/Internals/kakapo_top.pir_tmpl>;
58
59         %kakapo<pir_nqp-rx><src/Internals/Base.pir>     := <src/Internals/Base.nqp>;
60         %kakapo<pir_nqp-rx><src/Internals/Full.pir>     := <src/Internals/Full.nqp>;
61
62         my @base_pir_files := <
63                 src/Global.pir
64                 src/Syntax.pir
65
66                 src/Internals/Kakapo.pir
67
68                 src/Parrot/Opcode.pir
69                 src/Parrot/Parrot.pir
70                 src/Parrot/Pir.pir
71
72                 src/Pmc/Array.pir
73                 src/Pmc/Class.pir
74                 src/Pmc/common-methods.pir
75                 src/Pmc/Exception.pir
76                         src/Exceptions.pir
77                 src/Pmc/File.pir
78                 src/Pmc/Key.pir
79                 src/Pmc/Hash.pir
80                 src/Pmc/Namespace.pir
81                 src/Pmc/String.pir
82                 src/Pmc/Sub.pir
83                 src/Pmc/Undef.pir
84         >;
85
86         for @base_pir_files {
87                 %kakapo<pir_nqp-rx>{~ $_} := change_ext(~$_, :from('.pir'), :to('.nqp'));
88         }
89
90         %kakapo<build_libs><library/kakapo_base.pir> := <
91                 src/Internals/kakapo_top.pir
92                 src/Internals/Base.pir
93         >;
94         %kakapo<build_libs><library/kakapo_base.pir>.append( @base_pir_files );
95         %kakapo<build_libs><library/kakapo_base.pir>.push( <src/Internals/kakapo_bottom.pir>);
96
97         my @full_pir_files := <
98                 src/CallSignature.pir
99
100                 src/Classes/P6object.pir
101                 src/Classes/P6metaclass.pir
102
103                 src/ComponentMarshaller.pir
104                 src/DependencyQueue.pir
105                 src/Library.pir
106
107                 src/Matchers/Boolean.pir
108                 src/Matchers/CallSig.pir
109                 src/Matchers/Factory.pir
110                 src/Matchers/InstanceOf.pir
111                 src/Matchers/Matcher.pir
112                 src/Matchers/Not.pir
113                 src/Matchers/Null.pir
114                 src/Matchers/PassFail.pir
115
116                 src/Cuculinae/Antiphon.pir
117                 src/Cuculinae/Cuculus.pir
118                 src/Cuculinae/Cuckoo.pir
119                 src/Cuculinae/Ovum.pir
120                 src/Cuculinae/SigMatcher.pir
121                 src/Cuculinae/Verifier.pir
122
123                 src/Program.pir
124
125                 src/UnitTest/Assertions.pir
126                 src/UnitTest/Listeners.pir
127                 src/UnitTest/Loader.pir
128                 src/UnitTest/Result.pir
129                 src/UnitTest/Standalone.pir
130                 src/UnitTest/Suite.pir
131                 src/UnitTest/Testcase.pir
132         >;
133
134         for @full_pir_files {
135                 %kakapo<pir_nqp-rx>{~ $_} := change_ext(~$_, :from('.pir'), :to('.nqp'));
136         }
137
138         %kakapo<build_libs><library/kakapo_full.pir> := <
139                 src/Internals/kakapo_top.pir
140                 src/Internals/Full.pir
141         >;
142         %kakapo<build_libs><library/kakapo_full.pir>.append( @base_pir_files );
143         %kakapo<build_libs><library/kakapo_full.pir>.append( @full_pir_files );
144         %kakapo<build_libs><library/kakapo_full.pir>.push( <src/Internals/kakapo_bottom.pir>);
145
146         %kakapo<strip_annotations> := %kakapo<build_libs>;
147
148         %kakapo<pbc_pir><library/kakapo_base.pbc>       := <library/kakapo_base.pir>;
149         %kakapo<pbc_pir><library/kakapo_full.pbc>       := <library/kakapo_full.pir>;
150         %kakapo<pbc_pir><library/krt0.pbc>              := <library/krt0.pir>;
151
152         %kakapo<release_files><kakapo_base.pir> := <library/kakapo_base.pir>;
153         %kakapo<release_files><kakapo_base.pbc> := <library/kakapo_base.pbc>;
154         %kakapo<release_files><kakapo_full.pir> := <library/kakapo_full.pir>;
155         %kakapo<release_files><kakapo_full.pbc> := <library/kakapo_full.pbc>;
156         %kakapo<release_files><krt0.pir>                        := <library/krt0.pir>;
157         %kakapo<release_files><krt0.pbc>                := <library/krt0.pbc>;
158
159         # This test file needs compiling so the others can load it.
160         %kakapo<pir_nqp-rx><t/Pmc/common-methods.pir> := <t/Pmc/common-methods.nqp>;
161
162         %kakapo<inst_lib> := <
163                 library/kakapo_base.pbc
164                 library/kakapo_full.pbc
165                 library/krt0.pbc
166         >;
167
168         register_step_before('build', Setup::Step::copy_templates);
169         register_step_before('clean', Setup::Step::clean_templates);
170
171         #install_build_libs();
172         install_substep('build', Setup::Step::build_libs, :before('_build_pbc_pir'));
173         install_substep('build', Setup::Step::strip_annotations, :before('_build_pbc_pir'));
174         register_step_before('clean', Setup::Step::clean_libs);
175
176         register_step('release', Setup::Step::make_release);
177
178         pir::shift(@argv);
179         setup_(@argv, %kakapo);
180 }
181
182 sub change_ext($file, :$from!, :$to!) {
183         my $len := pir::length__IS($file) - pir::length__IS($from);
184
185         unless pir::substr__SSI($file, $len) eq $from {
186                 pir::die("No matching extension '$from' on file: $file");
187         }
188
189         my $base := pir::substr__SSI($file, 0, $len);
190         $base ~ $to;
191 }
192
193 sub get_args() {
194         my $interp := pir::getinterp__P();
195         $interp[2];
196 }
197
198 sub install_substep($step, &func, :$before, :$after) {
199         my $target := $before // $after;
200         my @insert;
201         @insert.push(&func);
202
203         our %step;
204         my $index := ?$after;
205         for %step{$step} {
206                 if ~ $_ eq $target {
207                         pir::splice__vppii(%step{$step}, @insert, $index, 0);
208                         return 0;
209                 }
210
211                 $index++;
212         }
213
214         pir::die("Unable to insert substep &func into $step - could not find $target");
215 }
216
217
218 # distutils functions are not in a namespace, so Step:: methods can't address them
219 # directly.
220
221 sub needs_update($src, $dst, :$verbose) {
222
223         ! file_exists($dst) ||newer(~$src, $dst, :verbose($verbose));
224 }
225
226 sub setup_(@steps, %config) {
227         Q:PIR {
228                 $P0 = find_lex '@steps'
229                 $P1 = find_lex '%config'
230                 'setup'($P0 :flat, $P1 :flat :named)
231         };
232 }
233
234 module Setup::Step;
235
236 sub build_libs(*%config) {
237         my $output_file;
238         my @inputs;
239         my $needs_update;
240         my $command;
241
242         for %config<build_libs> {
243                 $output_file := ~ $_;
244                 @inputs := %config<build_libs>{$output_file};
245                 $needs_update := 0;
246
247                 unless newer(~$output_file, @inputs) {
248                         $command := 'cat '
249                                 ~ pir::join(' ', @inputs)
250                                 ~ ' > '
251                                 ~ $output_file;
252                         system($command, :verbose(1));
253                 }
254         }
255 }
256
257 sub clean_libs(*%config) {
258         clean_key(%config<build_libs>);
259 }
260
261 sub clean_templates(*%config) {
262         clean_key(%config<copy_templates>);
263 }
264
265 sub copy_templates(*%config) {
266         my %cfg := %config<copy_templates>;
267         for %cfg {
268                 my $src := %cfg{$_};
269                 my $dst := ~ $_;
270
271                 unless newer(~$dst, ~$src) {
272                         cp($src, $dst, :verbose(1));
273                 }
274         }
275 }
276
277 sub make_release(*%config) {
278         my $release_id := %config<release_id>;
279         unless $release_id {
280                 pir::die("Cannot release: <release_id> config not set");
281         }
282
283         my @args;
284         @args.push( $release_id );
285
286         my $release_dir := pir::sprintf__SSP(%config<release_dir_format>, @args);
287
288         if file_exists($release_dir) {
289                 pir::die("Cannot release: '$release_dir' already exists.");
290         }
291
292         my $vdd := %config<vdd_file>;
293
294         unless $vdd && file_exists($vdd) {
295                 pir::die("Cannot release: no Version Description Document provided. Set <vdd_file> config.");
296         }
297
298         my $fh := pir::open__PSS($vdd, 'r');
299         my $leader := $fh.readline;
300         $fh.close;
301
302         if pir::index__ISS($leader, $release_id) < 0 {
303                 pir::die("Cannot release: VDD file '$vdd' does not contain '$release_id' in line 1");
304         }
305
306         my %cfg := %config<release_files>;
307         my $installed_vdd := 0;
308
309         for %cfg {
310                 install(%cfg{~$_}, $release_dir ~ '/' ~$_, :verbose(1));
311
312                 if %cfg{~$_} eq $vdd {
313                         $installed_vdd++;
314                 }
315         }
316
317         unless $installed_vdd {
318                 install($vdd, $release_dir ~ '/VERSION', :verbose(1));
319         }
320
321         unlink($vdd, :verbose(1));
322 }
323
324 sub strip_annotations(*%config) {
325         for %config<strip_annotations> {
326                 if file_exists(~$_) {
327                         my $fh := pir::open__PSS(~$_, 'r');
328                         my $body := $fh.readall();
329                         $fh.close;
330
331                         if pir::index__ISS($body, "\n.annotate ") >= 0 {
332                                 say("Removing annotations from $_");
333                                 $body.replace("\n.annotate ", "\n# .annotate ");
334                                 $fh := pir::open__PSS(~$_, 'w');
335                                 $fh.puts($body);
336                                 $fh.close;
337                         }
338                 }
339         }
340 }