Merge branch 'master' of gitorious.org:kakapo/kakapo into austin
[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         register_step_before('build', Setup::Step::copy_templates);
150         register_step_before('clean', Setup::Step::clean_templates);
151         
152         #install_build_libs();
153         install_substep('build', Setup::Step::build_libs, :before('_build_pbc_pir'));
154         install_substep('build', Setup::Step::strip_annotations, :before('_build_pbc_pir'));
155         register_step_before('clean', Setup::Step::clean_libs);
156         
157         register_step('release', Setup::Step::make_release);
158         
159         pir::shift(@argv);
160         setup_(@argv, %kakapo);
161 }
162
163 sub change_ext($file, :$from!, :$to!) {
164         my $len := pir::length__IS($file) - pir::length__IS($from);
165         
166         unless pir::substr__SSI($file, $len) eq $from {
167                 pir::die("No matching extension '$from' on file: $file");
168         }
169         
170         my $base := pir::substr__SSI($file, 0, $len);
171         $base ~ $to;    
172 }
173
174 sub get_args() {
175         my $interp := pir::getinterp__P();
176         $interp[2];
177 }
178
179 sub install_substep($step, &func, :$before, :$after) {
180         my $target := $before // $after;
181         my @insert;
182         @insert.push(&func);
183         
184         our %step;
185         my $index := ?$after;
186         for %step{$step} {
187                 if ~ $_ eq $target {
188                         pir::splice__vppii(%step{$step}, @insert, $index, 0);
189                         return 0;
190                 }
191                 
192                 $index++;
193         }
194         
195         pir::die("Unable to insert substep &func into $step - could not find $target");
196 }
197
198
199 # distutils functions are not in a namespace, so Step:: methods can't address them 
200 # directly.
201
202 sub needs_update($src, $dst, :$verbose) {
203         
204         ! file_exists($dst) ||newer(~$src, $dst, :verbose($verbose));
205 }
206
207 sub setup_(@steps, %config) {
208         Q:PIR {
209                 $P0 = find_lex '@steps'
210                 $P1 = find_lex '%config'
211                 'setup'($P0 :flat, $P1 :flat :named)
212         };
213 }
214
215 module Setup::Step;
216
217 sub build_libs(*%config) {
218         my $output_file;
219         my @inputs;
220         my $needs_update;
221         my $command;
222         
223         for %config<build_libs> {
224                 $output_file := ~ $_;
225                 @inputs := %config<build_libs>{$output_file};
226                 $needs_update := 0;
227                 
228                 unless newer(~$output_file, @inputs) {
229                         $command := 'cat ' 
230                                 ~ pir::join(' ', @inputs)
231                                 ~ ' > '
232                                 ~ $output_file;
233                         system($command, :verbose(1));
234                 }
235         }
236 }
237
238 sub clean_libs(*%config) {
239         clean_key(%config<build_libs>);
240 }
241
242 sub clean_templates(*%config) {
243         clean_key(%config<copy_templates>);
244 }
245         
246 sub copy_templates(*%config) {
247         my %cfg := %config<copy_templates>;
248         for %cfg {
249                 my $src := %cfg{$_};
250                 my $dst := ~ $_;
251                 
252                 unless newer(~$dst, ~$src) {
253                         cp($src, $dst, :verbose(1));
254                 }
255         }
256 }
257
258 sub make_release(*%config) {
259         my $release_id := %config<release_id>;
260         unless $release_id {
261                 pir::die("Cannot release: <release_id> config not set");
262         }
263         
264         my @args;
265         @args.push( $release_id );
266         
267         my $release_dir := pir::sprintf__SSP(%config<release_dir_format>, @args);
268
269         if file_exists($release_dir) {
270                 pir::die("Cannot release: '$release_dir' already exists.");
271         }
272         
273         my $vdd := %config<vdd_file>;
274         
275         unless $vdd && file_exists($vdd) {
276                 pir::die("Cannot release: no Version Description Document provided. Set <vdd_file> config.");
277         }
278         
279         my $fh := pir::open__PSS($vdd, 'r');
280         my $leader := $fh.readline;
281         $fh.close;
282         
283         if pir::index__ISS($leader, $release_id) < 0 {
284                 pir::die("Cannot release: VDD file '$vdd' does not contain '$release_id' in line 1");
285         }
286         
287         my %cfg := %config<release_files>; 
288         my $installed_vdd := 0;
289         
290         for %cfg {
291                 install(%cfg{~$_}, $release_dir ~ '/' ~$_, :verbose(1));
292                 
293                 if %cfg{~$_} eq $vdd {
294                         $installed_vdd++;
295                 }
296         }
297
298         unless $installed_vdd {
299                 install($vdd, $release_dir ~ '/VERSION', :verbose(1));
300         }
301         
302         unlink($vdd, :verbose(1));
303 }
304
305 sub strip_annotations(*%config) {
306         for %config<strip_annotations> {
307                 if file_exists(~$_) {
308                         my $fh := pir::open__PSS(~$_, 'r');
309                         my $body := $fh.readall();
310                         $fh.close;
311
312                         if pir::index__ISS($body, "\n.annotate ") >= 0 {
313                                 say("Removing  annotations from $_");
314                                 $body.replace("\n.annotate ", "\n# .annotate ");
315                                 $fh := pir::open__PSS(~$_, 'w');
316                                 $fh.puts($body);
317                                 $fh.close;
318                         }                               
319                 }
320         }
321 }