Skip to content

Instantly share code, notes, and snippets.

@jberger
Created April 28, 2012 21:03
Show Gist options
  • Save jberger/2521993 to your computer and use it in GitHub Desktop.
Save jberger/2521993 to your computer and use it in GitHub Desktop.
Contributed by JTPalmer via #alien
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
use File::chdir;
use File::Spec;
use File::Copy qw/copy/;
use Config;
use ExtUtils::CBuilder 0.2703;
my $config_file = 'config';
my $obj_file = 'libdontpanic.o';
my $lib_file = 'libdontpanic.' . $Config{so};
my $ldflags;
$ldflags = "-shared -soname $lib_file" if $Config{osname} eq 'linux';
$ldflags = '-dynamiclib' if $Config{osname} eq 'darwin';
die unless $ldflags;
my %clean = (
src => [$lib_file, $obj_file],
'.' => [$config_file],
);
my %install = (
lib => [$lib_file],
include => [qw/libdontpanic.h/],
);
my $prefix = $CWD;
GetOptions(
'prefix=s' => \$prefix,
);
my $action = shift || 'build';
my $config = _load_options();
$config->{prefix} ||= $prefix;
my $sub = __PACKAGE__->can($action) or die "Unknown action: $action";
$sub->();
sub build {
local $CWD = 'src';
system('gcc -fPIC -c libdontpanic.c');
die $! if $?;
system("gcc $ldflags -o $lib_file libdontpanic.o");
die $! if $?;
}
sub configure {
_store_options( { prefix => $prefix } );
}
sub install {
my %files = do {
local $CWD = 'src';
map {
my $folder = $_;
my @files = map {File::Spec->rel2abs($_)} @{$install{$folder}};
($folder, \@files)
} keys %install;
};
_check_mkdir($config->{prefix});
local $CWD = $config->{prefix};
foreach my $folder (keys %files) {
_check_mkdir($folder);
local $CWD = $folder;
foreach my $file (@{$files{$folder}}) {
copy $file, $CWD or die "Could not copy file $file";
}
_write_pc() if $folder eq 'lib';
}
}
sub clean {
foreach my $folder (keys %clean) {
local $CWD = $folder;
unlink @{$clean{$folder}};
}
}
sub _load_options {
return {} unless -e $config_file;
my $config = do $config_file;
return $config;
}
sub _store_options {
my $opts = shift;
open my $fh, '>', $config_file;
print $fh Dumper $opts;
}
sub _write_pc {
open my $fh, '>', 'dontpanic.pc';
my $prefix = $config->{prefix};
print $fh <<END_PC;
prefix=$prefix
libdir=$prefix/lib
includedir=$prefix/include
Name: DontPanic
Description: Test Library for Alien::Base
Version: 1.02
Libs: -L$prefix/lib -ldontpanic
Cflags: -I$prefix/include
END_PC
}
sub _check_mkdir {
my $folder = shift;
unless (-d $folder) {
mkdir $folder or die "Could not create folder: $folder";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment