More progress on Matchers
[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/UnitTest
48                         t
49                 >) ),
50                 
51                 :release_id(            'release-7' ),
52                 :release_dir_format(    'released/%s'),
53                 :vdd_file(              'vdd.txt' ),
54         );
55
56         %kakapo<copy_templates><library/krt0.pir>                               := <src/Internals/krt0.pir_tmpl>;
57         %kakapo<copy_templates><src/Internals/kakapo_bottom.pir>        := <src/Internals/kakapo_bottom.pir_tmpl>;
58         %kakapo<copy_templates><src/Internals/kakapo_top.pir>           := <src/Internals/kakapo_top.pir_tmpl>;
59         
60
61         %kakapo<pir_nqp-rx><src/Internals/Base.pir>     := <src/Internals/Base.nqp>;
62         %kakapo<pir_nqp-rx><src/Internals/Full.pir>     := <src/Internals/Full.nqp>;
63
64         my @base_pir_files := <
65                 src/Global.pir
66                 src/Syntax.pir
67
68                 src/Internals/Kakapo.pir 
69                 
70                 src/Parrot/Opcode.pir
71                 src/Parrot/Parrot.pir
72                 src/Parrot/Pir.pir
73                 
74                 src/Pmc/Array.pir
75                 src/Pmc/common-methods.pir
76                 src/Pmc/Exception.pir
77                         src/Exceptions.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/Classes/P6object.pir
99                 src/Classes/P6metaclass.pir
100
101                 src/DependencyQueue.pir
102                 src/Library.pir
103
104                 src/Matchers/Boolean.pir
105                 src/Matchers/Factory.pir
106                 src/Matchers/InstanceOf.pir
107                 src/Matchers/Matcher.pir
108                 src/Matchers/Not.pir
109                 src/Matchers/Null.pir
110                 
111                 src/Mimidae/Generis.pir
112                 src/Mimidae/Maker.pir
113                 
114                 src/Program.pir
115                 
116                 src/UnitTest/Assertions.pir
117                 src/UnitTest/Listeners.pir
118                 src/UnitTest/Loader.pir
119                 src/UnitTest/Result.pir
120                 src/UnitTest/Suite.pir
121                 src/UnitTest/Testcase.pir
122         >;
123         
124         for @full_pir_files {
125                 %kakapo<pir_nqp-rx>{~ $_} := change_ext(~$_, :from('.pir'), :to('.nqp'));
126         }
127
128         %kakapo<build_libs><library/kakapo_full.pir> := <
129                 src/Internals/kakapo_top.pir
130                 src/Internals/Full.pir
131         >;
132         %kakapo<build_libs><library/kakapo_full.pir>.append( @base_pir_files );
133         %kakapo<build_libs><library/kakapo_full.pir>.append( @full_pir_files );
134         %kakapo<build_libs><library/kakapo_full.pir>.push( <src/Internals/kakapo_bottom.pir>);
135
136         %kakapo<strip_annotations> := %kakapo<build_libs>;
137         
138         %kakapo<pbc_pir><library/kakapo_base.pbc>       := <library/kakapo_base.pir>;
139         %kakapo<pbc_pir><library/kakapo_full.pbc>       := <library/kakapo_full.pir>;
140         %kakapo<pbc_pir><library/krt0.pbc>              := <library/krt0.pir>;
141
142         %kakapo<release_files><kakapo_base.pir> := <library/kakapo_base.pir>;
143         %kakapo<release_files><kakapo_base.pbc> := <library/kakapo_base.pbc>;
144         %kakapo<release_files><kakapo_full.pir> := <library/kakapo_full.pir>;
145         %kakapo<release_files><kakapo_full.pbc> := <library/kakapo_full.pbc>;
146         %kakapo<release_files><krt0.pir>                := <library/krt0.pir>;
147         %kakapo<release_files><krt0.pbc>                := <library/krt0.pbc>;
148
149         # This test file needs compiling so the others can load it.
150         %kakapo<pir_nqp-rx><t/Pmc/common-methods.pir> := <t/Pmc/common-methods.nqp>;
151         
152         register_step_before('build', Setup::Step::copy_templates);
153         register_step_before('clean', Setup::Step::clean_templates);
154         
155         #install_build_libs();
156         install_substep('build', Setup::Step::build_libs, :before('_build_pbc_pir'));
157         install_substep('build', Setup::Step::strip_annotations, :before('_build_pbc_pir'));
158         register_step_before('clean', Setup::Step::clean_libs);
159         
160         register_step('release', Setup::Step::make_release);
161         
162         pir::shift(@argv);
163         setup_(@argv, %kakapo);
164 }
165
166 sub change_ext($file, :$from!, :$to!) {
167         my $len := pir::length__IS($file) - pir::length__IS($from);
168         
169         unless pir::substr__SSI($file, $len) eq $from {
170                 pir::die("No matching extension '$from' on file: $file");
171         }
172         
173         my $base := pir::substr__SSI($file, 0, $len);
174         $base ~ $to;    
175 }
176
177 sub get_args() {
178         my $interp := pir::getinterp__P();
179         $interp[2];
180 }
181
182 sub install_substep($step, &func, :$before, :$after) {
183         my $target := $before // $after;
184         my @insert;
185         @insert.push(&func);
186         
187         our %step;
188         my $index := ?$after;
189         for %step{$step} {
190                 if ~ $_ eq $target {
191                         pir::splice__vppii(%step{$step}, @insert, $index, 0);
192                         return 0;
193                 }
194                 
195                 $index++;
196         }
197         
198         pir::die("Unable to insert substep &func into $step - could not find $target");
199 }
200
201
202 # distutils functions are not in a namespace, so Step:: methods can't address them 
203 # directly.
204
205 sub needs_update($src, $dst, :$verbose) {
206         
207         ! file_exists($dst) ||newer(~$src, $dst, :verbose($verbose));
208 }
209
210 sub setup_(@steps, %config) {
211         Q:PIR {
212                 $P0 = find_lex '@steps'
213                 $P1 = find_lex '%config'
214                 'setup'($P0 :flat, $P1 :flat :named)
215         };
216 }
217
218 module Setup::Step;
219
220 sub build_libs(*%config) {
221         my $output_file;
222         my @inputs;
223         my $needs_update;
224         my $command;
225         
226         for %config<build_libs> {
227                 $output_file := ~ $_;
228                 @inputs := %config<build_libs>{$output_file};
229                 $needs_update := 0;
230                 
231                 unless newer(~$output_file, @inputs) {
232                         $command := 'cat ' 
233                                 ~ pir::join(' ', @inputs)
234                                 ~ ' > '
235                                 ~ $output_file;
236                         system($command, :verbose(1));
237                 }
238         }
239 }
240
241 sub clean_libs(*%config) {
242         clean_key(%config<build_libs>);
243 }
244
245 sub clean_templates(*%config) {
246         clean_key(%config<copy_templates>);
247 }
248         
249 sub copy_templates(*%config) {
250         my %cfg := %config<copy_templates>;
251         for %cfg {
252                 my $src := %cfg{$_};
253                 my $dst := ~ $_;
254                 
255                 unless newer(~$dst, ~$src) {
256                         cp($src, $dst, :verbose(1));
257                 }
258         }
259 }
260
261 sub make_release(*%config) {
262         my $release_id := %config<release_id>;
263         unless $release_id {
264                 pir::die("Cannot release: <release_id> config not set");
265         }
266         
267         my @args;
268         @args.push( $release_id );
269         
270         my $release_dir := pir::sprintf__SSP(%config<release_dir_format>, @args);
271
272         if file_exists($release_dir) {
273                 pir::die("Cannot release: '$release_dir' already exists.");
274         }
275         
276         my $vdd := %config<vdd_file>;
277         
278         unless $vdd && file_exists($vdd) {
279                 pir::die("Cannot release: no Version Description Document provided. Set <vdd_file> config.");
280         }
281         
282         my $fh := pir::open__PSS($vdd, 'r');
283         my $leader := $fh.readline;
284         $fh.close;
285         
286         if pir::index__ISS($leader, $release_id) < 0 {
287                 pir::die("Cannot release: VDD file '$vdd' does not contain '$release_id' in line 1");
288         }
289         
290         my %cfg := %config<release_files>; 
291         my $installed_vdd := 0;
292         
293         for %cfg {
294                 install(%cfg{~$_}, $release_dir ~ '/' ~$_, :verbose(1));
295                 
296                 if %cfg{~$_} eq $vdd {
297                         $installed_vdd++;
298                 }
299         }
300
301         unless $installed_vdd {
302                 install($vdd, $release_dir ~ '/VERSION', :verbose(1));
303         }
304         
305         unlink($vdd, :verbose(1));
306 }
307
308 sub strip_annotations(*%config) {
309         for %config<strip_annotations> {
310                 if file_exists(~$_) {
311                         my $fh := pir::open__PSS(~$_, 'r');
312                         my $body := $fh.readall();
313                         $fh.close;
314
315                         if pir::index__ISS($body, "\n.annotate ") >= 0 {
316                                 say("Removing  annotations from $_");
317                                 $body.replace("\n.annotate ", "\n# .annotate ");
318                                 $fh := pir::open__PSS(~$_, 'w');
319                                 $fh.puts($body);
320                                 $fh.close;
321                         }                               
322                 }
323         }
324 }