Skip to content

Instantly share code, notes, and snippets.

@mberends
Created February 2, 2009 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mberends/56913 to your computer and use it in GitHub Desktop.
Save mberends/56913 to your computer and use it in GitHub Desktop.
# Makefile creator in Perl 6, reads Makefile.in and writes Makefile.
# Use your values of /my/ to run either (always in current directory):
# '/my/perl6 Makefile.p6' or '/my/parrot /my/perl6.pbc Makefile.p6'
use v6;
# say %*VM<config>.perl;
# say "osname = {%*VM<config><osname>}";
# say "revision = {%*VM<config><revision>}";
# say "MAJOR.MINOR.PATCH = {%*VM<config><MAJOR>}.{%*VM<config><MINOR>}.{%*VM<config><PATCH>}";
# say "prefix = {%*VM<config><prefix>}";
# say "PATH = {%*ENV<PATH>}";
# say "_ = {%*ENV<_>}";
say "\nHi, this is Makefile.p6 preparing to make your Makefile.\n";
# Operating System
my $ps_command;
given %*VM<config><osname> {
when 'linux' { $ps_command = 'ps o args'; }
when 'darwin' { $ps_command = 'ps o args'; }
when 'win32' {
say "Sorry, currently not working on Windows";
say "Cannot continue.";
exit(1);
}
default {
say "Sorry, I don't know os '{%*VM<config><osname>}'";
say "Please report this. Cannot continue.";
exit(2);
}
}
say "operating system = {%*VM<config><osname>}";
# Current process executable and arguments
my @ps = qx($ps_command).split("\n"); # all procs and their args
my @makeproc = grep( { $_ ~~ / Makefile.p6 $ / }, @ps); # find Makefile
if @makeproc.elems == 0 {
say "Strange, cannot find myself in your process list.";
say "Here's what your '$ps_command' reported:";
say @ps.join("\n");
say "I give up.";
exit(3);
}
if @makeproc[0] !~~ / (.*) <.ws> Makefile.p6 $ / {
say "Cannot read command line. It says:";
say @makeproc[0];
exit(4);
}
my $perl6 = $0;
say "perl6 = $perl6";
# Read Makefile.in, perform edits, write Makefile
my $maketext = slurp( 'Makefile.in' );
$maketext .= subst( 'Makefile.in', 'Makefile' );
$maketext .= subst( 'To be read', 'Written' );
$maketext .= subst( '<PERL6>', $perl6 );
$maketext .= subst( '<BIN_DIR>', $perl6 );
squirt( 'Makefile', $maketext );
# Job done.
say "\nAll done, you can now use 'make', 'make help' and so on.\n";
# The opposite of slurp
sub squirt( Str $filename, Str $text ) {
my $handle = open( $filename, :w ); # should check for success
$handle.print: $text;
$handle.close;
}
# inefficient workaround - replace when Rakudo gets qx{} or `backtick`.
sub qx( $command ) {
my Str $tempfile = "/tmp/rakudo_svg_tiny_makefile_qx.tmp";
my Str $fullcommand = "$command >$tempfile";
run $fullcommand;
my Str $result = slurp( $tempfile );
unlink $tempfile;
return $result;
}
=begin pod
=head1 NAME
Makefile.p6 - convert Makefile.in to Makefile with local settings
=head1 AUTHOR
Martin Berends (mberends on CPAN github #perl6 and @flashmail.com).
=end pod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment