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