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