| 1 | package MegaDistro::RpmMaker::Build; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Exporter; | ||
| 7 | our @ISA = qw(Exporter); | ||
| 8 | our @EXPORT_OK = qw(have_rpm build_rpm); | ||
| 9 | |||
| 10 | use lib '../MegaDistro'; | ||
| 11 | |||
| 12 | use MegaDistro::Config qw(:default $DEVNULL); | ||
| 13 | use MegaDistro::RpmMaker::Config qw(:default :build); | ||
| 14 | use MegaDistro::RpmMaker::SpecFile qw(make_specfile); | ||
| 15 | |||
| 16 | use File::Spec::Functions qw(:ALL); | ||
| 17 | use File::Find; | ||
| 18 | use Archive::Tar; | ||
| 19 | |||
| 20 | no warnings 'File::Find'; | ||
| 21 | |||
| 22 | sub have_rpm { | ||
| 23 | `rpmbuild --help > /dev/null 2>&1`; | ||
| 24 | if ( $? == 0 ) { | ||
| 25 | return 1; | ||
| 26 | } | ||
| 27 | else { | ||
| 28 | return 0; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | sub make_macrosfile { | ||
| 33 | open( MACROSFILE, ">$buildtree{'macrosfile'}" ) || die "Cannot open file: $!"; | ||
| 34 | print MACROSFILE '%_topdir' . "\t" . $buildtree{'prefixdir'} . "\n"; | ||
| 35 | print MACROSFILE '%_sourcedir' . "\t" . '%{_topdir}/SOURCES' . "\n"; | ||
| 36 | print MACROSFILE '%_specdir' . "\t" . '%{_topdir}/SPECS' . "\n"; | ||
| 37 | print MACROSFILE '%_tmppath' . "\t" . '%{_topdir}/.tmp' . "\n"; | ||
| 38 | print MACROSFILE '%_builddir' . "\t" . '%{_topdir}/BUILD' . "\n"; | ||
| 39 | print MACROSFILE '%_buildroot' . "\t" . '%{_tmppath}/%{name}-%{version}' . "\n"; | ||
| 40 | print MACROSFILE '%_rpmdir' . "\t" . '%{_topdir}/RPMS' . "\n"; | ||
| 41 | print MACROSFILE '%_srpmdir' . "\t" . '%{_topdir}/SRPMS' . "\n"; | ||
| 42 | print MACROSFILE '%_rpmfilename' . "\t" . '%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm' . "\n"; | ||
| 43 | print MACROSFILE '%_enable_debug_packages' . "\t" . '0' . "\n"; | ||
| 44 | print MACROSFILE '%debug_package' . "\t" . '%{nil}' . "\n"; | ||
| 45 | print MACROSFILE '%_unpackaged_files_terminate_build' . "\t" . '0' . "\n"; | ||
| 46 | close( MACROSFILE ); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub make_rcfile { | ||
| 50 | my $macrofiles = `rpm --showrc | grep 'macrofiles'`; chomp $macrofiles; | ||
| 51 | open( RCFILE, ">$buildtree{'rcfile'}" ) || die "Cannot open file: $!"; | ||
| 52 | # print RCFILE 'include:' . "\t\t" . '/usr/lib/rpm/rpmrc' . "\n"; | ||
| 53 | print RCFILE $macrofiles . ':' . $buildtree{'macrosfile'} . "\n"; | ||
| 54 | close( RCFILE ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub tarball_rpm { | ||
| 58 | if ( $args{'trace'} ) { | ||
| 59 | print 'MegaDistro::RpmMaker::Build : Executing sub-routine: tarball_rpm' . "\n"; | ||
| 60 | } | ||
| 61 | #create src tarball | ||
| 62 | my $TARBALL = $metadata{'name'} . '-' . $metadata{'version'} . '.tar.gz'; | ||
| 63 | |||
| 64 | my @files; | ||
| 65 | find(sub{push@files,abs2rel($File::Find::name,$Conf{'builddir'})},$Conf{'builddir'}); | ||
| 66 | shift @files if !$files[0]; | ||
| 67 | chdir $Conf{'builddir'}; | ||
| 68 | my $tar = Archive::Tar->create_archive(catdir($buildtree{'SOURCES'},$TARBALL),9,@files); | ||
| 69 | |||
| 70 | # #check for tarball and copy to SOURCES directory | ||
| 71 | # if ( -s "$Conf{'rootdir'}/$TARBALL" ) { | ||
| 72 | # system( "cp $Conf{'rootdir'}/$TARBALL $buildtree{'SOURCES'}/$TARBALL" ); | ||
| 73 | # if ( $args{'debug'} ) { | ||
| 74 | # print "\t" . 'Tarball successfully created - name is: ' . $TARBALL . "\n"; | ||
| 75 | # } | ||
| 76 | # } | ||
| 77 | # else { | ||
| 78 | # die 'Error copying tarball!' . "\n"; | ||
| 79 | # } | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub build_rpm { | ||
| 84 | MegaDistro::RpmMaker::Config::_init_globals(); | ||
| 85 | &build_pre; #initialize the build directory tree | ||
| 86 | &tarball_rpm; | ||
| 87 | &make_macrosfile; | ||
| 88 | &make_rcfile; | ||
| 89 | &make_specfile; | ||
| 90 | if ( $args{'trace'} ) { | ||
| 91 | print 'MegaDistro::RpmMaker::Build : Executing sub-routine: build_rpm' . "\n"; | ||
| 92 | } | ||
| 93 | |||
| 94 | my $addopts; | ||
| 95 | if ( $args{'verbose'} ) { | ||
| 96 | $addopts = '-v'; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $addopts = '--quiet'; | ||
| 100 | } | ||
| 101 | |||
| 102 | #check for specfile, and package. | ||
| 103 | if ( -e "$buildtree{'specfile'}" ) { | ||
| 104 | if ( !$args{'verbose'} ) { | ||
| 105 | system( "rpmbuild --rcfile $buildtree{'drcfile'}:$buildtree{'rcfile'} -bb $addopts $buildtree{'specfile'} > $DEVNULL" ); | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | system( "rpmbuild --rcfile $buildtree{'drcfile'}:$buildtree{'rcfile'} -bb $addopts $buildtree{'specfile'}" ); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | die 'SPEC file not found!' . "\n"; | ||
| 113 | } | ||
| 114 | |||
| 115 | my $RPM = $metadata{'name'} . '-' . $metadata{'version'} . '-' . $metadata{'release'} . '.' . $metadata{'buildarch'} . '.rpm'; | ||
| 116 | #copy the package, to $Conf{'rootdir'} | ||
| 117 | if ( -e "$buildtree{'RPMS'}/$RPM" ) { | ||
| 118 | system( "cp $buildtree{'RPMS'}/$RPM $Conf{'rootdir'}/$RPM" ); | ||
| 119 | if ( $args{'debug'} ) { | ||
| 120 | print "\t" . 'RPM successfully created - name is: ' . $RPM . "\n"; #make this return? | ||
| 121 | #(output from caller) | ||
| 122 | } | ||
| 123 | |||
| 124 | } | ||
| 125 | else { | ||
| 126 | die 'Error copying RPM!' . "\n"; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; |