build bzip2 for current host

step0、参考

step1、安装依赖

所有时Terminal + Shell + GNU CoreUtils
下载时cURL
解压时tar + gzip
编译时gmakegcc / GCC | Clang / LLVM
运行时glibc ( libc.sold-linux-x86-64.so )

step2、使用curl命令下载bzip2源码包 (

curl -LO https://sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz

step3、使用tar解压bzip2源码包

tar vxf bzip2-1.0.8.tar.gz

step4、进入bzip2-1.0.8目录

cd bzip2-1.0.8

step5、查看bzip2-1.0.8目录中的内容

step6、将Makefile替换为如下内容

CC ?= cc
AR ?= ar

override CPPFLAGS += -D_FILE_OFFSET_BITS=64
LDFLAGS=

PREFIX=/usr/local

LIB_STATIC_FILE_EXT=a
LIB_SHARED_FILE_EXT=so

LIB_STATIC_FILE_NAME=libbz2.$(LIB_STATIC_FILE_EXT)
LIB_SHARED_FILE_NAME=libbz2.$(LIB_SHARED_FILE_EXT)

LIB_OBJS = blocksort.o  \
      huffman.o    \
      crctable.o   \
      randtable.o  \
      compress.o   \
      decompress.o \
      bzlib.o

build: bzip2 bzip2recover $(LIB_SHARED_FILE_NAME)

bzip2: $(LIB_STATIC_FILE_NAME) bzip2.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

bzip2recover: bzip2recover.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

$(LIB_SHARED_FILE_NAME): $(LIB_OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^

$(LIB_STATIC_FILE_NAME): $(LIB_OBJS)
	$(AR) crs $@ $^

install-include: bzlib.h
	install -d        $(PREFIX)/include
	install -m 644 $^ $(PREFIX)/include

install-bin: bzip2 bzip2recover bzgrep bzmore bzdiff
	install -d        $(PREFIX)/bin
	install -m 755 $^ $(PREFIX)/bin
	cd $(PREFIX)/bin && \
	ln -s -f bzip2 bunzip2 && \
	ln -s -f bzip2 bzcat && \
	ln -s -f bzgrep bzegrep && \
	ln -s -f bzgrep bzfgrep && \
	ln -s -f bzmore bzless && \
	ln -s -f bzdiff bzcmp

install-lib: $(LIB_STATIC_FILE_NAME) $(LIB_SHARED_FILE_NAME)
	install -d                             $(PREFIX)/lib
	install -m 644 $(LIB_STATIC_FILE_NAME) $(PREFIX)/lib
	install -m 755 $(LIB_SHARED_FILE_NAME) $(PREFIX)/lib

install-man: bzip2.1 bzgrep.1 bzmore.1 bzdiff.1
	install -d                 $(PREFIX)/share/man/man1
	install -m 644 $^          $(PREFIX)/share/man/man1
	echo ".so man1/bzgrep.1" > $(PREFIX)/share/man/man1/bzegrep.1
	echo ".so man1/bzgrep.1" > $(PREFIX)/share/man/man1/bzfgrep.1
	echo ".so man1/bzmore.1" > $(PREFIX)/share/man/man1/bzless.1
	echo ".so man1/bzdiff.1" > $(PREFIX)/share/man/man1/bzcmp.1

install: build install-include install-bin install-lib install-man

test: bzip2
	@cat words1
	./bzip2 -1  < sample1.ref > sample1.rb2
	./bzip2 -2  < sample2.ref > sample2.rb2
	./bzip2 -3  < sample3.ref > sample3.rb2
	./bzip2 -d  < sample1.bz2 > sample1.tst
	./bzip2 -d  < sample2.bz2 > sample2.tst
	./bzip2 -ds < sample3.bz2 > sample3.tst
	cmp sample1.bz2 sample1.rb2 
	cmp sample2.bz2 sample2.rb2
	cmp sample3.bz2 sample3.rb2
	cmp sample1.tst sample1.ref
	cmp sample2.tst sample2.ref
	cmp sample3.tst sample3.ref
	@cat words3

check: test

.PHONY: clean
clean: 
	rm -f *.o libbz2.a libbz2.so libbz2.dylib bzip2 bzip2recover \
	sample1.rb2 sample2.rb2 sample3.rb2 \
	sample1.tst sample2.tst sample3.tst

distclean: clean
	rm -f manual.ps manual.html manual.pdf

DISTNAME=bzip2-1.0.8
dist: check manual
	rm -f $(DISTNAME)
	ln -s -f . $(DISTNAME)
	tar cvf $(DISTNAME).tar \
	   $(DISTNAME)/blocksort.c \
	   $(DISTNAME)/huffman.c \
	   $(DISTNAME)/crctable.c \
	   $(DISTNAME)/randtable.c \
	   $(DISTNAME)/compress.c \
	   $(DISTNAME)/decompress.c \
	   $(DISTNAME)/bzlib.c \
	   $(DISTNAME)/bzip2.c \
	   $(DISTNAME)/bzip2recover.c \
	   $(DISTNAME)/bzlib.h \
	   $(DISTNAME)/bzlib_private.h \
	   $(DISTNAME)/Makefile \
	   $(DISTNAME)/LICENSE \
	   $(DISTNAME)/bzip2.1 \
	   $(DISTNAME)/bzip2.1.preformatted \
	   $(DISTNAME)/bzip2.txt \
	   $(DISTNAME)/words0 \
	   $(DISTNAME)/words1 \
	   $(DISTNAME)/words2 \
	   $(DISTNAME)/words3 \
	   $(DISTNAME)/sample1.ref \
	   $(DISTNAME)/sample2.ref \
	   $(DISTNAME)/sample3.ref \
	   $(DISTNAME)/sample1.bz2 \
	   $(DISTNAME)/sample2.bz2 \
	   $(DISTNAME)/sample3.bz2 \
	   $(DISTNAME)/dlltest.c \
	   $(DISTNAME)/manual.html \
	   $(DISTNAME)/manual.pdf \
	   $(DISTNAME)/manual.ps \
	   $(DISTNAME)/README \
	   $(DISTNAME)/README.COMPILATION.PROBLEMS \
	   $(DISTNAME)/README.XML.STUFF \
	   $(DISTNAME)/CHANGES \
	   $(DISTNAME)/libbz2.def \
	   $(DISTNAME)/libbz2.dsp \
	   $(DISTNAME)/dlltest.dsp \
	   $(DISTNAME)/makefile.msc \
	   $(DISTNAME)/unzcrash.c \
	   $(DISTNAME)/spewG.c \
	   $(DISTNAME)/mk251.c \
	   $(DISTNAME)/bzdiff \
	   $(DISTNAME)/bzdiff.1 \
	   $(DISTNAME)/bzmore \
	   $(DISTNAME)/bzmore.1 \
	   $(DISTNAME)/bzgrep \
	   $(DISTNAME)/bzgrep.1 \
	   $(DISTNAME)/Makefile-libbz2_so \
	   $(DISTNAME)/bz-common.xsl \
	   $(DISTNAME)/bz-fo.xsl \
	   $(DISTNAME)/bz-html.xsl \
	   $(DISTNAME)/bzip.css \
	   $(DISTNAME)/entities.xml \
	   $(DISTNAME)/manual.xml \
	   $(DISTNAME)/format.pl \
	   $(DISTNAME)/xmlproc.sh
	gzip -v $(DISTNAME).tar

# For rebuilding the manual from sources on my SuSE 9.1 box

MANUAL_SRCS= 	bz-common.xsl bz-fo.xsl bz-html.xsl bzip.css \
		entities.xml manual.xml 

manual: manual.html manual.ps manual.pdf

manual.ps: $(MANUAL_SRCS)
	./xmlproc.sh -ps manual.xml

manual.pdf: $(MANUAL_SRCS)
	./xmlproc.sh -pdf manual.xml

manual.html: $(MANUAL_SRCS)
	./xmlproc.sh -html manual.xml

step7、使用make命令进行编译、安装

make [option]... [VAR=VALUE]... [TARGET]...
VARVALUE示例说明
ARar指定静态库生成器
CCgcc |  clang指定C编译器
CFLAGS-O2 -v指定C编译器的参数
CPPFLAGS-I<includeDIR>指定C预处理器的参数
LDFLAGS-L<libDIR>指定C链接器的参数
PREFIX/usr/local指定安装目录

示例:

make  CFLAGS="-O2 -v" && sudo make install PREFIX=/usr