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