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