Alien-TinyCCx

 view release on metacpan or  search on metacpan

lib/Alien/TinyCCx.pm  view on Meta::CPAN

package Alien::TinyCCx;

use strict;
use warnings;

# Follow Golden's Version Rule: http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
our $VERSION = "0.12";
$VERSION = eval $VERSION;

use File::ShareDir;
use File::Spec;
use Env qw( @PATH );
use Carp;
use Config;

###################################
# Find the Distribution Directory #
###################################

# The prefix will depend on whether or not the thing was finally installed.
# The easiest way to know that is if *this* *module* *file* is in a blib
# directory or not:
my $dist_dir;

my $mod_path = $INC{'Alien/TinyCCx.pm'};
if ($mod_path =~ s/(.*)blib.*/$1share/) {
	$dist_dir = $mod_path;
	croak('Looks like Alien::TinyCCx is being invoked from blib, but I cannot find build-time sharedir!')
		unless -d $dist_dir;
}
else {
	$dist_dir = File::ShareDir::dist_dir('Alien-TinyCCx');
}

############################
# Path retrieval functions #
############################

# First we need to find a number of files of interest. Rather than guess
# at these files' locations, we can perform a simple search for them
# and store the locations that we find.
my %files_to_find = (
	bin => 'tcc',
	lib => 'libtcc.',
	inc => 'libtcc.h'
);
if ($^O =~ /MSWin/) {
	$files_to_find{bin} .= '.exe';
	$files_to_find{lib} .= 'dll';
}
else {
	$files_to_find{lib} .= 'a';
}
my %path_for_file;

# Otherwise go through all dirs and subdirs of dist_dir
my @dirs = $dist_dir;
while (@dirs) {
	# Pull the next directory off the list
	my $dir = shift @dirs;
	
	# Check if any of the unfound files are in this directory
	for my $file_type (keys %files_to_find) {
		if (-f File::Spec->catfile($dir, $files_to_find{$file_type})) {
			delete $files_to_find{$file_type};
			$path_for_file{$file_type} = $dir;
		}
	}
	
	# Otherwise, scan this directory for subdirectories
	opendir (my $dh, $dir) or next;
	CHILD: for my $child (readdir $dh) {
		next CHILD if $child =~ /^\./; # skip dot-files and dot-dirs
		my $full_path = File::Spec->catdir($dir, $child);
		push @dirs, $full_path if -d $full_path;
	}
	close $dh;
}

# Find the path to the tcc executable
sub path_to_tcc { $path_for_file{bin} }

# Modify the PATH environment variable to include tcc's directory
unshift @PATH, path_to_tcc();

# Find the path to the compiled libraries. Note that this is only applicable
# on Unixish systems; Windows simply uses the %PATH%, which was already
# appropriately set.
sub libtcc_library_path { $path_for_file{lib} }

# Add library path on Unixish:
if ($ENV{LD_LIBRARY_PATH}) {
	$ENV{LD_LIBRARY_PATH} = libtcc_library_path() . ':' . $ENV{LD_LIBRARY_PATH};
}
elsif ($^O !~ /MSWin/) {
	$ENV{LD_LIBRARY_PATH} = libtcc_library_path();
}

# Determine path for libtcc.h



( run in 0.644 second using v1.01-cache-2.11-cpan-e1769b4cff6 )