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