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