Building LFS (rev.6.2) with pacman.

Prerequisite:
This hint requires sufficient knowledge of pacman. Before reading this hint, please see manual

Why I chose pacman.

From my point of view pacman is the most suitable package manager for LFS. Let’s see shortly how pacman compiles and installs a program package on standalone Arch Linux. First of all, you should put the source package into /var/cache/pacman/src/ directory. Then you must have a script called PKGBUILD in an isolated directory (as well as patches for this package if they exist). The third step is to enter to the directory with PKGBUILD and launch makepkg script with –i option. And here we go. There is nothing unusual, except PKGBUILD script that can be created based on LFS-book text. For example, let’s take a look at LFS text (#6.23.1. Installation of Readline):


Upstream developers have fixed several issues since the initial release of Readline-5.1. Apply those fixes: 
	patch -Np1 -i ../readline-5.1-fixes-3.patch
Reinstalling Readline will cause the old libraries to be moved to < libraryname >.old. While this is
normally not a problem, in some cases it can trigger a linking bug in ldconfig. This can be avoided
by issuing the following two seds: 
	sed -i '/MV.*old/d' Makefile.in
	sed -i '/{OLDSUFF}/c:' support/shlib-install
Prepare Readline for compilation: 
	./configure --prefix=/usr --libdir=/lib
Compile the package: 
	make SHLIB_LIBS=-lncurses
The meaning of the make option: 
	SHLIB_LIBS=-lncurses 
This option forces Readline to link against the libncurses (really, libncursesw) library.
This package does not come with a test suite. 
Install the package: 
	make install
Give Readline's dynamic libraries more appropriate permissions: 
	chmod -v 755 /lib/lib{readline,history}.so*
Now move the static libraries to a more appropriate location: 
	mv -v /lib/lib{readline,history}.a /usr/lib
Next, remove the .so files in /lib and relink them into /usr/lib: 
	rm -v /lib/lib{readline,history}.so
	ln -sfv ../../lib/libreadline.so.5 /usr/lib/libreadline.so
	ln -sfv ../../lib/libhistory.so.5 /usr/lib/libhistory.so

Indeed, it can be transformed into BUILDPKG script very easily.


pkgname=readline
pkgver=5.1
pkgrel=1
pkgdesc="GNU readline library"
arch=(i686)
depends=('glibc' 'ncurses')
source=(ftp://ftp.cwru.edu/pub/bash/readline-5.1.tar.gz \
	readline-5.1-fixes-3.patch )
md5sums=( '7ee5a692db88b30ca48927a13fd60e46' )

build() {
  cd ${startdir}/src/${pkgname}-5.1
  patch -Np1 -i ../readline-5.1-fixes-3.patch || return 1

  sed -i '/MV.*old/d' Makefile.in
  sed -i '/{OLDSUFF}/c:' support/shlib-install
  
  ./configure --prefix=/usr --libdir=/lib
  make SHLIB_LIBS=-lncurses || return 1
  make DESTDIR=${startdir}/pkg install
  
  cd ${startdir}/pkg/lib
  chmod 755 libreadline.so.* libhistory.so.*
  
  #Now move the static libraries to a more appropriate location:
  mkdir -pv ${startdir}/pkg/usr/lib
  mv -v lib{readline,history}.a ${startdir}/pkg/usr/lib
  # Next, remove the .so files in /lib and relink them into /usr/lib:
  rm -v libreadline.so
  rm -v libhistory.so
  cd ${startdir}/pkg/usr/lib
  ln -sfv ../../lib/libreadline.so.5 libreadline.so
  ln -sfv ../../lib/libhistory.so.5 libhistory.so
}

The first part of the script is a place where you set the variables with info parameters of the current package. The second part is a function build, which compiles and installs package components into a special directory DESTDIR, whereas the LFS script uses "direct" installation. Thus if we have installed pacman and BUILDPKG-scripts for some packages we will be able build and install these packages on any Linux-like system.

How to build LFS with pacman.

In order to build LFS with pacman you need:
1.   Download libtar,    + patch and pacman
2.   Download lfsbs-6.2.tar.gz and copy it to $LFS/sources catalog. This tarball contains BUILDPKG scripts for packages used in LFS (#6.7-#6.57)
3.   Build temporary system according to LFS book (#5.1 - #5.30).
4.   Add zlib, libtar and pacman packages to the temporary toolchain.


	zlib-1.2.3 
	./configure --prefix=/tools
	make
	make install

	libtar-1.2.11
	patch -Np1 -i ../patches/libtar-1.2.11-2.patch
	./configure
	make
	make install

	pacman-2.9.8 
	./configure
	make
	cp -v pacman scripts/makepkg /tools/bin
	cp -v etc/{makepkg,pacman}.conf  /tools/etc
	
  You can change following setting in /tools/etc/makepkg.conf:  
    CFLAGS/CXXFLAGS - your optimization compiler flags
    KEEPDOCS		- if you want keep docs in /usr/share/doc and /usr/share/info.
    PACKAGER		- your name and e-mail
5.   Complete the chapter 5.
6.   Create $LFS/temp directory for temporary files.

IMPORTANT NOTICE:
Now you have two options to continue building:
    A) you can use makepkg command for each package manually.
    B) you can use prepared scripts to build system in packet mode.
We assume that we have $LFS/tools and $LFS/sources directories.

Option A

7.   Create /var/cache/pacman/src directory and copy sources packages to this directory.
8.   Perform #6.1-#6.6 from LFS book including enter to the chroot environment.
9.   Unzip and untar lfsbs-6.2.tar.gz file into directory for your choice (e.i. /usr/lfsbs).
At this point, you have set of directories each of them contains BUILDPKG file and other essential stuff.
10.   Compile sources and install packages (#6.7-#6.57) using following commands:

     cd NAME_OF_PACKAGE
     makepkg --clean --nodeps -w NAME_OF_CATALOG_FOR_BIN_PACKAGES &&
     pacman --upgrade --force --nodeps  NAME_OF_CATALOG_FOR_PACKAGES/PKG_NAME
11.   Install libtar and packman as in above item.
12.   Change setting in /etc/makepkg.conf as it described in item 4.
13.   Complete the chapter 6.

Option B

7.   Extract a1_chroot.sh, a2_mkdir.sh, a3_init_bs.sh scripts from lfsbs-6.2.tar.gz archive and put them to the $LFS/temp directory.
8.   Launch a1_chroot.sh script to enter to the chroot mode (#6.1- #6.4).
9.   Launch a2_mkdir.sh script to create directories, symbol links and etc. (#6.5 - #6.6).
10.   Launch a3_init_bs.sh scripts to initialize build framework. Now we can start building our system.
11.   Launch the scripts in the following sequence:
    b1_build.sh
    b2_glibc_post.sh
    b3_gcc.sh
    b4_build.sh
    b5_shadow_post.sh
    b6_build.sh
    b7_pacman.sh
Perform a sanity check after b2_glibc_post.sh and b3_gcc.sh as it is described in LFS book.
12.   Change setting in /etc/makepkg.conf as it is described in item 4.
13.   Complete the chapter 6.
[whohit]lfsbs[/whohit]