App-AutoBuild
view release on metacpan or search on metacpan
lib/App/AutoBuild.pm view on Meta::CPAN
'cflags'=>[qw(-O2 -Isrc/ -Wall -Wextra), shell_config('sdl-config', '--cflags')],
'ldflags'=>[qw(-lSDL_image -lm), shell_config('sdl-config', '--libs')],
'programs'=>{
# each output executable goes here. you can list several.
'main'=>'src/main.c',
},
});
src/main.c
#include "stuff.h"
int main(int argc, char** argv)
{
stuff_function();
return 0;
}
src/stuff.c
#include "stuff.h"
void stuff_function()
{
// hai
}
src/stuff.h
#pragma once
void stuff_function();
Note you don't need to put stuff.c into your build.pl-- it Just Works(tm).
An even shorter example-- instead of a build.pl:
build.sh
#!/bin/sh
export CC="clang"
export CFLAGS="-std=c99 -pedantic -Wall -Wextra -O3"
perl -MApp::AutoBuild -e 'build("main.c");' -- $@
=head1 COMMAND LINE
usage: ./build.pl [-h|--help] [-v] [clean]
-h, --help: dump help
-v: increase verbosity (-vv or more -v's)
0 (default) only show compile/link actions, in shortened form
1 show compile/link actions with full command lines, and at the end a time summary (also shows App::AutoBuild overhead)
2 shows debugging for job numbers (not useful really yet)
-d: increase debugosity (-dd or more -d's)
0 (default) nothing!
1 show which dependencies caused a recompile
2 show stat() calls
-q: be more quiet
--cc=(compiler path): pick a compiler other than gcc
--program=(program): don't compile all targets, just this one
clean: unlink output files/meta file (.autobuild_meta)
=head1 DESCRIPTION
After writing a makefile for my 30th C project, I decided this was dumb and it (the computer) should figure out which object files should be linked in or recompiled. The idea behind this module is you create a build.pl that uses App::AutoBuild and c...
App::AutoBuild will figure out all the object files your C file depends on. A list of included header files (.h) will be computed by GCC and remembered in a cache. At build time, stat() is called on each header file included by your .c file. If an...
This tool isn't supposed to be a make replacement-- there are plenty of those, and at least one great one already in Perl. The idea is that the build system should know enough about the source code to do what you want for you. This replaces all the...
=head1 CAVEATS
For this to work properly, you must have a scheme and follow it. Every .c/.cpp file must have an .h/.hpp file that matches (with the exception of the .c/.cpp file with main()). For now, the .c and .h files must be in the same directory (but this ma...
If you have a .h file and an unrelated .c file with the same name (as in, headers.h and headers.c) in the same folder, the .c file will be compiled and linked in automatically. If this doesn't work well for you, put the .h files without .c files int...
A .autobuild_meta file is created in the current directory so it can remember modification times and dependency lists of files. This will definitely be configurable in a future version!
=head1 SUBROUTINES/METHODS
These are exported by default.
=head2 build()
Pass this function a hashref of parameters for how to build your project. Keys are as follows:
=head3 cflags
An arrayref of cflags
=head3 ldflags
An arrayref of ldflags
=head3 programs
A hashref with binaries as keys, and start-point C files as values.
=head3 rules
An arrayref of rule hashrefs. See L</RULES>.
=head2 shell_config()
This is a helper function that takes a shell command + args (as an array) to pass to system() and splits the STDOUT into an array by whitespace.
I do all this as arrays because the CFLAGS/LDFLAGS can be added or removed per-file with rules.
=head1 RULES
Rules let you have custom build options per-file. For now it only supports adding/removing cflags for a given .c file, or adding/removing ldflags for a given output binary.
{'file'=>'contrib/GLee/GLee.c', 'del_cflags'=>['-pedantic']},
{'file'=>'configtest', 'add_ldflags'=>['-lyaml'], 'del_ldflags'=>['-lGL', '-lGLU', shell_config('sdl-config', '--libs')]},
These definitely need some more work!
=head1 AUTHOR
Joel Jensen, C<< <yobert at gmail.com> >>
=head1 BUGS/TODO
Please email me if it doesn't work! I've only tested with GCC and clang.
Job paralellizing would be sweet. Patches welcome.
lib/App/AutoBuild.pm view on Meta::CPAN
}
sub DESTROY
{
my($self) = @_;
if($self->{'clean'})
{
$self->unlink_file($meta_file);
}else
{
store($self->{'meta'}, $meta_file);
}
return;
}
sub args
{
my($self, $argv) = @_;
for(@$argv)
{
if($_ =~ m/^-(v+)$/)
{
$self->{'verbose'} += length($1);
}
elsif($_ =~ m/^-(d+)$/)
{
$self->{'debug'} += length($1);
}
elsif($_ eq '-q')
{
$self->{'quiet'} = 1;
}
elsif($_ eq 'clean')
{
$self->{'clean'} = 1;
}
elsif($_ =~ m/^--cc=(.+)$/)
{
$self->{'cc'} = $1;
}
elsif($_ =~ m/^--program=(.+)$/)
{
$self->{'default'}{$1} = 1;
}
elsif($_ eq '-h' || $_ eq '--help')
{
print <<"zap";
usage: $0 [-h|--help] [-v] [clean]
-h, --help: dump help
-v: increase verbosity (-vv or more -v's)
0 (default) only show compile/link actions, in shortened form
1 show compile/link actions with full command lines, and at the end a time summary (also shows App::AutoBuild overhead)
2 shows debugging for job numbers (not useful really yet)
-d: increase debugosity (-dd or more -d's)
0 (default) nothing!
1 show which dependencies caused a recompile
2 show stat() calls
clean: unlink output files/meta file ($meta_file)
zap
exit(1);
}
else
{
warn "ignoring unknown commandline option $_";
}
}
}
sub cflags
{
my($self, $v) = @_;
if($v)
{
$self->{'cflags'} = $v;
}
return $self->{'cflags'};
}
sub ldflags
{
my($self, $v) = @_;
if($v)
{
$self->{'ldflags'} = $v;
}
return $self->{'ldflags'};
}
sub program
{
my($self, $cfile, $out) = @_;
my %job = (
'out'=>$out,
'task'=>'ld',
);
my $jid = $self->add_job(\%job);
$job{'needs'} = [$self->add_build_job($cfile, $jid)];
}
sub add_build_job
{
my($self, $cfile) = @_;
my $cpp = 0;
if(substr($cfile, -4, 4) eq '.cpp')
{
$self->{'cpp_ld'} = 1;
$cpp = 1;
}
# otherwise assume C instead of C++
my $ofile = replace_ext($cfile, $self->{'osuffix'});
$ofile =~ s/([^\/]+)$/\.$1/;
lib/App/AutoBuild.pm view on Meta::CPAN
sub replace_ext
{
my($in, $ext) = @_;
$in =~ s/\.[^.]{1,3}$//;
$in .= '.'.$ext;
return $in;
}
sub file_changed
{
my($self, $file) = @_;
if(defined $self->{'changed_this_run'}{$file})
{
return $self->{'changed_this_run'}{$file};
}
if(-e $file)
{
my $mtime = $self->mtime($file);
#my $md5 = $self->md5($file);
if($self->{'meta'}{$file} && $self->{'meta'}{$file}{'mtime'} && $self->{'meta'}{$file}{'mtime'} == $mtime)
{
$self->{'changed_this_run'}{$file} = 0;
return 0;
}
#if($self->{'meta'}{$file} && $self->{'meta'}{$file}{'md5'} && $self->{'meta'}{$file}{'md5'} eq $md5)
#{
# $self->{'changed_this_run'}{$file} = 0;
# return 0;
#}
$self->{'meta'}{$file}{'mtime'} = $mtime;
#$self->{'meta'}{$file}{'md5'} = $md5;
}
$self->{'changed_this_run'}{$file} = 1;
return 1;
}
sub file_update
{
my($self, $file) = @_;
delete $self->{'mtime_this_run'}{$file};
delete $self->{'md5_this_run'}{$file};
delete $self->{'changed_this_run'}{$file};
return $self->file_changed($file);
}
sub mtime
{
my($self, $file) = @_;
if($self->{'mtime_this_run'}{$file})
{
return $self->{'mtime_this_run'}{$file};
}
if($self->{'debug'} > 1)
{
print "stat('$file');\n";
}
my @s = stat($file);
$self->{'mtime_this_run'}{$file} = $s[9];
return $s[9];
}
sub md5
{
my($self, $file) = @_;
if($self->{'md5_this_run'}{$file})
{
return $self->{'md5_this_run'}{$file};
}
if($self->{'debug'} > 1)
{
print " md5('$file');\n";
}
open(my $dat, '<', $file) || die "cannot open file for md5: $!";
my $md5 = Digest::MD5->new();
$md5->addfile($dat);
close($dat);
my $sum = $md5->hexdigest();
$self->{'md5_this_run'}{$file} = $sum;
return $sum;
}
sub unlink_file
{
my($self, $file) = @_;
if(-e $file)
{
if($self->{'verbose'})
{
print "unlink('$file');\n";
}
unlink($file) || die "could not unlink $file: $!";
}
}
1;
( run in 0.549 second using v1.01-cache-2.11-cpan-39bf76dae61 )