From 4a5d2a3c22096c563608ce2e3603ea40810f030b Mon Sep 17 00:00:00 2001 From: davidovski Date: Sun, 30 Jan 2022 02:34:51 +0000 Subject: moved everything to bash again --- src/BuildOrder.pm | 110 -------------------------- src/BuildPackage.pm | 85 -------------------- src/XibUtil.pm | 52 ------------- src/build_order.pm | 131 +++++++++++++++++++++++++++++++ src/build_package.sh | 190 +++++++++++++++++++++++++++++++++++++++++++++ src/prepare_environment.sh | 19 +++++ src/xib.pl | 67 ---------------- 7 files changed, 340 insertions(+), 314 deletions(-) delete mode 100644 src/BuildOrder.pm delete mode 100755 src/BuildPackage.pm delete mode 100644 src/XibUtil.pm create mode 100755 src/build_order.pm create mode 100755 src/build_package.sh create mode 100755 src/prepare_environment.sh delete mode 100755 src/xib.pl diff --git a/src/BuildOrder.pm b/src/BuildOrder.pm deleted file mode 100644 index 0836209..0000000 --- a/src/BuildOrder.pm +++ /dev/null @@ -1,110 +0,0 @@ -package BuildOrder; -# tools for determining which order to build packages - -use strict; -use warnings; - -use File::Basename "basename"; -use Sort::TSort "tsort"; - -sub list_dependencies{ - my $file = $_; - my @deps = (); - - open (my $fh, "<", $file) or warn "Cannot open $file"; - - while (my $line = <$fh>) { - if ($line =~ /DEPS=\((.+)\)/) { - my @words = split(/ /, $1); - push(@deps, @words); - } - } - - return @deps; -} - -sub list_buildfiles{ - my @files = glob("$main::buildfiles/repo/*/*.xibuild"); - # ignore any meta packages during this stage, they can be added later - return grep(!/\/meta\//, @files); -} - -sub list_meta_pkgs{ - return map({basename($_, ".xibuild")} glob("$main::buildfiles/repo/meta/*.xibuild")); -} - -sub get_packages{ - my %pkgs = (); - - my @files = list_buildfiles(); - - foreach (@files) { - my $pkg_file = $_; - my $pkg_name = basename($pkg_file, ".xibuild"); - - my @deps = list_dependencies($pkg_file); - $pkgs{$pkg_name} = \@deps; - } - - return %pkgs; -} - -# Get a list of all the edges -sub get_edges{ - my %pkgs = @_; - - my @edges = (); - foreach (keys(%pkgs)) { - my $pkg = $_; - my @deps = @{$pkgs{$_}}; - foreach (@deps) { - my $dep = $_; - - my @edge = ($pkg, $dep); - push @edges, [ @edge ]; - - } - } - return @edges; -} - -# Determine which packages are depended on -sub get_depended_on{ - my @edges = @_; - - my %install = (); - foreach (@edges) { - my @edge = @{$_}; - $install{$edge[1]} = $edge[0]; - } - - return keys(%install); -} - -sub determine_build_order{ - my ($file) = @_; - my %pkgs = get_packages(); - - my @edges = get_edges(%pkgs); - - my @install = get_depended_on(@edges); - - # use tsort to determine the build order - my @sorted = reverse(@{tsort(\@edges)}); - - my @meta = list_meta_pkgs(); - push(@sorted, @meta); - - open (my $fh, ">", $file) or die "Cannot open $file"; - - foreach(@sorted) { - my $pkg = $_; - print($fh "$pkg"); - print($fh "+") if (grep(/^$pkg/, @install)); - print($fh "\n"); - } - - return $file; -} - -1; diff --git a/src/BuildPackage.pm b/src/BuildPackage.pm deleted file mode 100755 index 61087f2..0000000 --- a/src/BuildPackage.pm +++ /dev/null @@ -1,85 +0,0 @@ -package BuildPackage; - -use strict; -use warnings; - -use File::Basename; -use XibUtil qw/extract_from_file extract md5_sum/; - -sub extract_source{ - return XibUtil::extract_from_file(@_, qr/^SOURCE=(.+)$/); -} - -sub extract_branch{ - return XibUtil::extract_from_file(@_, qr/^BRANCH=(.+)$/); -} - -sub extract_version{ - return XibUtil::extract_from_file(@_, qr/^PKG_VER=(.+)$/); -} - -sub get_built_version{ - my ($build_file) = @_; - my @package_split = split(/\//, $build_file); - my $repo = $package_split[-2]; - my $name = basename($build_file, ".xibuild"); - my $dest = "$main::export/repo/$repo/$name"; - - my $pkg = "$dest.xipkg"; - my $info = "$dest.xipkg.info"; - my $used_build = "$dest.xibuild"; - - if (-e $pkg && -e $info) { - return md5_sum($used_build); - } -} - -sub clear_build_folder{ - rmtree("$main::chroot/build"); -} - -sub fetch_source{ - my ($build_file) = @_; - - mkdir("$main::chroot/build"); - mkdir("$main::chroot/build/source"); - chdir("$main::chroot/build/source"); - - my $source = extract_source($build_file); - my $branch = extract_branch($build_file); - my $PKG_VER = extract_version($build_file); - - if (XibUtil::is_git_repo($source, $branch)) { - print("Fetching git repo $source version $PKG_VER\n"); - system("git clone $source ."); - system("git checkout $branch"); - - } else { - print("downloading file $source\n"); - my $downloaded_file = basename($source); - system("curl $source $downloaded_file"); - extract("$downloaded_file"); - system("pwd; cp -r */* .") - - } - - # download source to $chroot/build/mysource.tgz - # extract source to $chroot/build/source -} - -sub build_package{ - my ($build_file) = @_; - - my $existing_version = get_built_version($build_file); - if (defined($existing_version) && $existing_version eq md5_sum($build_file)) { - # do not build - print("do not build\n"); - return - } - # build - fetch_source($build_file); - - - -} -1; diff --git a/src/XibUtil.pm b/src/XibUtil.pm deleted file mode 100644 index 1aa02a8..0000000 --- a/src/XibUtil.pm +++ /dev/null @@ -1,52 +0,0 @@ -package XibUtil; - -use v5.12; - -use strict; -use warnings; - -use File::Basename; - -sub md5_sum{ - my ($file) = @_; - - open(my $fh, "<", $file) or die "Cannot open $file: $!"; - binmode($fh); - - return Digest::MD5->new->addfile($fh)->hexdigest; -} - -sub extract_from_file{ - my ($file, $regex) = @_; - open (my $fh, "<", $file) or warn "Cannot open $file"; - while (my $line = <$fh>) { - if ($line =~ $regex) { - return $1; - } - } -} - -sub is_git_repo{ - return system("git ls-remote -q @_"); -} - -sub extract{ - my ($file) = @_; - my $ext = (fileparse($file, qr/\.[^.]*/))[2]; - print("$ext\n"); - - my $cmd = ""; - given($ext) { - $cmd = "tar -zxf $file" when ".gz"; - $cmd = "tar -xf $file" when ".xz"; - $cmd = "unzip $file" when ".zip"; - $cmd = "tar --lzip -xf $file" when ".lz"; - default { - $cmd = "tar -xf $file"; - } - } - - system($cmd); -} - -1; diff --git a/src/build_order.pm b/src/build_order.pm new file mode 100755 index 0000000..37beb64 --- /dev/null +++ b/src/build_order.pm @@ -0,0 +1,131 @@ +#!/usr/bin/env perl + +# TODO for building the whole system +# We first need to have a base system that we can build from +# (up to chapter 6 in lfs book, done by make_tools.sh) +# packages need to be build in order of importance +# need to some how find out which packages are the most important and build and install them to the chroot first +# build a package, install it if necessary +# copy the generated package/info to a safe place +# sign the package +# put the package in the export folder + +# TODO for building a single package: +# do all the preliminary checks (exists, deps are installed, etc) +# download source to $chroot/source +# download additional tools +# copy xibuild to the chroot +# create a "build.sh" script in the chroot +# - run the 3 stages of package building +# - create the pacakage in $chroot/$name.xipkg +# - add some info to package info +# - if requested, install to the chroot + +use strict; +use warnings; + +use Env; +use File::Basename; +use lib dirname (__FILE__); +use Sort::TSort "tsort"; + +our $buildfiles = $ENV{XIB_BUILDFILES}; + +sub list_dependencies{ + my $file = $_; + my @deps = (); + + open (my $fh, "<", $file) or warn "Cannot open $file"; + + while (my $line = <$fh>) { + if ($line =~ /DEPS=\((.+)\)/) { + my @words = split(/ /, $1); + push(@deps, @words); + } + } + + return @deps; +} + +sub list_buildfiles{ + my @files = glob("$buildfiles/repo/*/*.xibuild"); + # ignore any meta packages during this stage, they can be added later + return grep(!/\/meta\//, @files); +} + +sub list_meta_pkgs{ + return map({basename($_, ".xibuild")} glob("$buildfiles/repo/meta/*.xibuild")); +} + +sub get_packages{ + my %pkgs = (); + + my @files = list_buildfiles(); + + foreach (@files) { + my $pkg_file = $_; + my $pkg_name = basename($pkg_file, ".xibuild"); + + my @deps = list_dependencies($pkg_file); + $pkgs{$pkg_name} = \@deps; + } + + return %pkgs; +} + +# Get a list of all the edges +sub get_edges{ + my %pkgs = @_; + + my @edges = (); + foreach (keys(%pkgs)) { + my $pkg = $_; + my @deps = @{$pkgs{$_}}; + foreach (@deps) { + my $dep = $_; + + my @edge = ($pkg, $dep); + push @edges, [ @edge ]; + + } + } + return @edges; +} + +# Determine which packages are depended on +sub get_depended_on{ + my @edges = @_; + + my %install = (); + foreach (@edges) { + my @edge = @{$_}; + $install{$edge[1]} = $edge[0]; + } + + return keys(%install); +} + +sub determine_build_order{ + my %pkgs = get_packages(); + + my @edges = get_edges(%pkgs); + + my @install = get_depended_on(@edges); + + # use tsort to determine the build order + my @sorted = reverse(@{tsort(\@edges)}); + + my @meta = list_meta_pkgs(); + push(@sorted, @meta); + + foreach(@sorted) { + my $pkg = $_; + print("$pkg"); + print("+") if (grep(/^$pkg/, @install)); + print("\n"); + } +} + +unless (caller) { + determine_build_order(); +} diff --git a/src/build_package.sh b/src/build_package.sh new file mode 100755 index 0000000..0c37069 --- /dev/null +++ b/src/build_package.sh @@ -0,0 +1,190 @@ +#!/bin/bash + +BUILDFILE=$1 +REPO=$(echo $BUILDFILE | rev | cut -d/ -f2 | rev) +NAME=$(basename $BUILDFILE .xibuild) + +source $BUILDFILE + +extract () { + FILE=$1 + case "${FILE#*.}" in + "tar.gz" ) + tar -zxf $FILE + ;; + "tar.lz" ) + tar --lzip -xf "$FILE" + ;; + "zip" ) + unzip $FILE + ;; + * ) + tar -xf $FILE + ;; + esac +} + +package_exists () { + local exported="$XIB_EXPORT/repo/$REPO/$NAME" + local exported_pkg="$exported.xipkg" + local exported_pkg_info="$exported.xipkg.info" + local exported_pkg_build="$exported.xibuild" + + if [ -f $exported_pkg ] && [ -f $exported_pkg_info ] && [ -f $exported_pkg_build ]; then + local built_sum=$(md5sum $exported_pkg_build | cut -d" " -f1) + local current_sum=$(md5sum $BUILDFILE | cut -d" " -f1) + + if [ "$built_sum" = "$current_sum" ]; then + return 0 + fi + fi + + return 1 +} + +fetch_source () { + local src_dir="$XIB_CHROOT/build/source" + mkdir -pv $src_dir + + pushd $src_dir + if git ls-remote -q $SOURCE $BRANCH &> /dev/null; then + # The source is a git repo + git clone $SOURCE . + git checkout $BRANCH + else + # The source is a file + + local downloaded_file=$(basename $SOURCE) + curl -SsL $SOURCE > $downloaded_file + extract $downloaded_file + + # if the extracted file only had one directory + if [ "$(ls -l | wc -l)" = "3" ]; then + for file in */*; do + mv $file . + done; + fi + fi + popd +} + +clean_chroot () { + local export_dir="$XIB_CHROOT/export" + local build_dir="$XIB_CHROOT/build" + + rm -rf $export_dir + rm -rf $build_dir + + mkdir -pv $export_dir + mkdir -pv $build_dir + + mkdir -pv "$XIB_EXPORT/repo/$REPO/" +} + +make_buildscript () { + cat > "$XIB_CHROOT/build/build.sh" << "EOF" +#!/bin/bash + +export PKG_NAME=$(cat /build/name) +export PKG_DEST=/export + +prepare () { + echo "passing prepare" +} + +build () { + echo "passing build" +} + +check () { + echo "passing check" +} + +package () { + echo "passing package" +} + +cd /build +ls +source $PKG_NAME.xibuild +cd /build/source + +prepare || exit 1 +build || exit 1 +check +package || exit 1 + +if command -v postinstall > /dev/null; then + POSTINSTALL=$(type postinstall | sed '1,3d,$d') + if [${#POSTINSTALL} != 0]; then + POST_DIR=$PKG_DEST/var/lib/xipkg/postinstall + mkdir -p $POST_DIR + echo "#!/bin/sh" > $POST_DIR/$PKG_NAME.sh + echo $POSTINSTALL >> $POST_DIR/$PKG_NAME.sh + fi +fi +EOF + chmod 700 "$XIB_CHROOT/build/build.sh" +} + +package () { + local export_repo="$XIB_EXPORT/repo/$REPO" + local export_pkg="$XIB_EXPORT/repo/$REPO/$NAME.xipkg" + local pkg_dest="$XIB_CHROOT/export" + + pushd "$pkg_dest" + if [ "$(ls -1 | wc -l)" = "0" ]; then + echo "package is empty" + exit 1; + fi + tar -C $pkg_dest -cvzf $export_pkg ./ + popd +} + +create_info () { + local export_pkg="$XIB_EXPORT/repo/$REPO/$NAME.xipkg" + local pkg_info="$export_pkg.info" + + echo "" > $pkg_info + echo "NAME=$NAME" >> $pkg_info + echo "DESCRIPTION=$DESC" >> $pkg_info + echo "PKG_FILE=$NAME.xipkg" >> $pkg_info + echo "CHECKSUM=$(md5sum $export_pkg | awk '{ print $1 }')" >> $pkg_info + echo "SOURCE=$SOURCE" >> $pkg_info + echo "DATE=$(date)" >> $pkg_info + echo "DEPS=(${DEPS[*]})" >> $pkg_info +} + +sign () { + local export_pkg="$XIB_EXPORT/repo/$REPO/$NAME.xipkg" + local pkg_info="$export_pkg.info" + + echo "SIGNATURE=" >> $pkg_info + openssl dgst -sign $PRIV_KEY $export_pkg >> $pkg_info + +} + +build () { + clean_chroot + fetch_source + make_buildscript + + cp "$BUILDFILE" "$XIB_CHROOT/build/" + printf $NAME > "$XIB_CHROOT/build/name" + + local log_file="$XIB_EXPORT/repo/$REPO/$NAME.log" + xichroot $XIB_CHROOT /build/build.sh > $log_file + + package + create_info + + # TODO check if the key exists, if not, skip signing + sign + + cp "$BUILDFILE" "$XIB_EXPORT/repo/$REPO/" + +} + +[ -z "${XIB_CHROOT}" ] && echo "CRITICAL! No chroot env variable set!" && exit 1; + +package_exists || build diff --git a/src/prepare_environment.sh b/src/prepare_environment.sh new file mode 100755 index 0000000..73fca98 --- /dev/null +++ b/src/prepare_environment.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +export XIB_DIR="/var/lib/xib" +export XIB_BUILDFILES="$XIB_DIR/buildfiles" +export XIB_CHROOT="$XIB_DIR/chroot" +export XIB_EXPORT="$XIB_DIR/export" + +export PRIV_KEY="$HOME/.ssh/xi.pem" + +export BUILDFILES_GIT_REPO="https://xi.davidovski.xyz/git/buildfiles.git" + +mkdir -pv $XIB_DIR $XIB_BUILDFILES $XIB_CHROOT $XIB_EXPORT + +if [ -d $XIB_BUILDFILES/.git ]; then + cd $XIB_BUILDFILES + git pull +else + git clone $BUILDFILES_GIT_REPO $XIB_BUILDFILES +fi diff --git a/src/xib.pl b/src/xib.pl deleted file mode 100755 index fcdbeb2..0000000 --- a/src/xib.pl +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env perl - -# TODO for building the whole system -# We first need to have a base system that we can build from -# (up to chapter 6 in lfs book, done by make_tools.sh) -# packages need to be build in order of importance -# need to some how find out which packages are the most important and build and install them to the chroot first -# build a package, install it if necessary -# copy the generated package/info to a safe place -# sign the package -# put the package in the export folder - -# TODO for building a single package: -# do all the preliminary checks (exists, deps are installed, etc) -# download source to $chroot/source -# download additional tools -# copy xibuild to the chroot -# create a "build.sh" script in the chroot -# - run the 3 stages of package building -# - create the pacakage in $chroot/$name.xipkg -# - add some info to package info -# - if requested, install to the chroot - -use v5.12; - -use strict; -use warnings; -use Getopt::Long "HelpMessage"; - -use File::Basename; -use lib dirname (__FILE__); -use BuildOrder "determine_build_order"; -use XibUtil "extract"; -use BuildPackage; - -our $BUILDFILES_REPO = "https://xi.davidovski.xyz/git/buildfiles.git"; - -GetOptions( - "chroot:s" => \(our $chroot = "/var/lib/xib/chroot"), - "buildfiles" => \(our $buildfiles = "/var/lib/xib/buildfiles"), - "export:s" => \(our $export = "/var/lib/xib/export"), -) or HelpMessage(1); - -sub prepare_xib_environment{ - die "chroot environment at $chroot doesn't yet exist\n" unless ( -e $chroot); - - if (!-d $export) { - mkdir($export); - } - - pull_buildfiles(); -} - -sub pull_buildfiles{ - if (-d $buildfiles) { - system("cd $buildfiles && git pull"); - } else { - system("git clone $BUILDFILES_REPO $buildfiles"); - } -} - -unless (caller) { - prepare_xib_environment(); - my $file = "$chroot/buildorder"; - BuildOrder::determine_build_order($file); - BuildPackage::build_package("$buildfiles/repo/util/vim.xibuild"); -} -- cgit v1.2.1