Autotools utoTrial - Drexel University

Post on 03-Feb-2022

6 views 1 download

Transcript of Autotools utoTrial - Drexel University

Autotools Tutorial

Mengke HU

ECE Department

Drexel University

ASPITRG Group Meeting

Outline

1 Introduction

2 GNU Coding standards

3 Autoconf

4 Automake

5 Libtools

6 Demonstration

The Basics of Autotools

1 The purpose of autotools

I It serves the needs of your users (checking platform and libraries;compiling and installing ).

I It makes your project incredibly portable�for di�erent systemplatforms.

2 Why should we use autotools:

I A lot of free softwares target Linux operating system.I Autotools allow your project to build successfully on future versions ordistributions with virtually no changes to the build scripts.

The Basics of Autotools

1 The purpose of autotools

I It serves the needs of your users (checking platform and libraries;compiling and installing ).

I It makes your project incredibly portable�for di�erent systemplatforms.

2 Why should we use autotools:

I A lot of free softwares target Linux operating system.I Autotools allow your project to build successfully on future versions ordistributions with virtually no changes to the build scripts.

The Basics of Autotools

1 3 GNU packages for GNU build system

I AutoconfGenerate a con�guration script for a project

I AutomakeSimplify the process of creating consistent and functional make�les

I LibtoolProvides an abstraction for the portable creation of shared libraries

2 Basic steps (commends) to build and install software

I tar -zxvf package_name-version.tar.gzI cd package_name-versionI ./con�gureI makeI sudo make install

The Basics of Autotools

1 3 GNU packages for GNU build system

I AutoconfGenerate a con�guration script for a project

I AutomakeSimplify the process of creating consistent and functional make�les

I LibtoolProvides an abstraction for the portable creation of shared libraries

2 Basic steps (commends) to build and install software

I tar -zxvf package_name-version.tar.gzI cd package_name-versionI ./con�gureI makeI sudo make install

Autoconf

1 Autoconf

I autoconfI autoheaderI autom4teI autoreconfI autoscanI autoupdate

Data Flow Diagram for Autoconf

Automake

1 automake

I automakeI aclocal

Data Flow Diagram for aclocal

Libtools

1 Libtools

I libtoolI libtoolize

Data Flow Diagram for automake and libtool

A simple project structure

jupiter1.0

Make�le

I ./src/

F Make�le

F main.c

Make�le

Make�le layoutvar1=val1var2=val2...target1 : t1_dep1 ... t1_depN<TAB> shell-command1a<TAB> shell-command1b...target2 : t2_dep1 ... t2_depN<TAB> shell-command2a<TAB> shell-command2b

...

Make�le ExampleFile: /home/mengke/Desktop/backup2/jupiter/src/Makefile Page 1 of 1

# src/Makefile. Generated from Makefile.in by configure.# Package-specific substitution variablespackage = Jupiterversion = 1.0tarname = jupiterdistdir = $(tarname)-$(version)# VPATH-related substitution variablessrcdir = .# Prefix-specific substitution variablesprefix = /usr/localexec_prefix = ${prefix}bindir = ${exec_prefix}/bin#CFLAGS = -g -O0

all: jupiterjupiter: main.c

gcc -g -O2 -o jupiter ../../src/main.c#$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ main.c

check: all./jupiter | grep "Hello from .*jupiter!"@echo "*** ALL TESTS PASSED ***"

install:install -d $(DESTDIR)$(bindir) #DESTDIR is to dsupport the staged installationinstall -m 0755 jupiter $(DESTDIR)$(bindir)cp jupiter /usr/binchown root:toot /usr/bin/jupiter #change file owner and groupchmod +x /usr/bin/jupiter

uninstall:-rm $(DESTDIR)$(bindir)/jupiter

Makefile: Makefile.in ../config.statuscd .. && ./config.status src/$@

../config.status: ../configurecd .. && ./config.status --recheck

clean: -rm jupiter

.PHONY: all clean check install uninstall

Autoconf Syntax

1 Autoconf

I Autoconf use M4 macro language.I M4 is for the purpose of macro language processor.

2 Syntax for con�gure.ac �leAC_INIT([PackageName], [version])AC_CONFIG_FILES([Make�le src/Make�le])

AC_OUTPUT

Check Datatype

1 Check for types for di�erent platforms

I AC_TYPE_UINTN_TI AC_TYPE_INTN_T

Where N = 8,16,32,64

2 How those macros work?

I Check if stdint.h or inttypes.h have UINTN_T or UINTN_TI If not, de�ne UINTN_T or UINTN_T.

Add automake functionality

1 Enabling Automake in con�gure.ac

I AC_INIT([Jupiter], [1.0], [jupiter-bugs@example.org])I AM_INIT_AUTOMAKEI AC_CONFIG_SRCDIR([src/main.c])

2 Make�le.am for compiling sources

I bin_PROGRAMS = jupiterI jupiter_SOURCES = main.cI jupiter_CPPFLAGS = -I$(top_srcdir)/commonI jupiter_LDADD = ../common/libjupcommon.a

Add convenience libraries (static library)

Make�le.am for convenience libraries

I noinst_LIBRARIES = libjupcommon.aI libjupcommon_a_SOURCES = jupcommon.h print.c

Shared Libraries

Shared libraries contain only a small table of functions that programsrequire.

Shared libraries make executable �le smaller.

It is easy to update shared libraries without recompiling projects.

Shared libraries use dynamic linking, which provides chunks ofexecutable code that the operating system can load into a program'saddress space and execute.

Libtools

1 How the libtool works?

I Libtool script is generated by con�gure script and ltmain.sh.I Libtool script then insulates the author of the build system from thenuances of building shared libraries on di�erent platforms.

2 Add following lines in con�gure.ac:

I AC_SEARCH_LIBS([dlopen], [dl])I AC_SEARCH_LIBS([lt_dlopen], [ltdl])

Demo1: Make�le for compiling?

Project_Name/src/main.cFile: /home/mengke/Research/autotoo…demo/demo1_makefile/src/main.c Page 1 of 1

#include <stdio.h>#include <stdlib.h>

int main(int argc, char * argv[]){ printf("Hello from %s!\n", argv[0]); return 0;}

Demo1: Make�le for compiling?

Project_Name/src/Make�leFile: /home/mengke/Research/autotoo…mo/demo1_makefile/src/Makefile Page 1 of 1

#TARGET1: refer to actions "HelloWorld" in TARGET2all: HelloWorld#TARGET2: depends on HelloWorld.o, # ultimately depends on TARTET3HelloWorld: HelloWorld.o

gcc -o HelloWorld HelloWorld.o#TARGET3: depends on main.cHelloWorld.o: main.c

cc -c -o HelloWorld.o main.c#TARGET4: delete HelloWorld and HelloWorld.oclean:

rm HelloWorld HelloWorld.o#TARGET5: refer to actions "all" and "clean".PHONY: all clean

Demo2:Make�le for Creating Source Distribution ArchiveProject_Name/Make�leFile: /home/mengke/Research/autotoo…emo/demo2_makePackage/Makefile Page 1 of 1

#TARGET1: refer to src/Makefile# 1. go into src folder# 2. run target "all" in src/Makefileall:

cd src && make#TARGET2: creates distribution, refers to TARGET3dist: HelloWorld-1.0.tar.gz#TARGET3: generate software package# ultimately depends on TARGET4 HelloWorld-1.0.tar.gz: HelloWorld-1.0

tar -zcvf HelloWorld-1.0.tar.gz HelloWorld-1.0rm -rf HelloWorld-1.0

#TARGET4: create package distribution directoryHelloWorld-1.0:

mkdir -p HelloWorld-1.0/srccp Makefile HelloWorld-1.0cp src/Makefile HelloWorld-1.0/srccp src/main.c HelloWorld-1.0/src

#TARGET5: check the all the functions for both Makefile and src/Makefiledistcheck: HelloWorld-1.0.tar.gz

tar -zxvf HelloWorld-1.0.tar.gz cd HelloWorld-1.0 && make allcd HelloWorld-1.0 && make clean rm -rf Helloworld-1.0 @echo "**** Package Helloworld-1.0.tar.gz is ready for distribution."

#TARGET6: refer to actions "all", "clean" and "dist".PHONY: all clean dist

Demo3: Autoconf for con�guring projectProject_Name/con�gure.ac

File: /home/mengke/Research/autotoo…mo3_configureFile/configure.ac Page 1 of 1

#pass 'HelloWorld' to variable "package" in Makefile.in#pass '1.0' to variable "version" in Makefile.inAC_INIT([HelloWorld],[1.0])#generate Makefile by running the script './configure'AC_CONFIG_FILES([Makefile src/Makefile])

AC_OUTPUT

Demo3: Autoconf for con�guring projectFile: /home/mengke/Research/autotoo…emo3_configureFile/Makefile.in Page 1 of 1

# Configure Inputpackage = @PACKAGE_NAME@version = @PACKAGE_VERSION@tarname = @PACKAGE_TARNAME@distdir = $(tarname)-$(version)

#TARGET1: refer to src/Makefile# 1. go into src folder# 2. run target "all" in src/Makefileall clean check HelloWorld install uninstall:

cd src && $(MAKE) $@#TARGET2: creates distribution, refers to TARGET3dist: $(distdir).tar.gz#TARGET3: generate software package# ultimately depends on TARGET4 $(distdir).tar.gz: $(distdir)

tar -zcvf $(distdir).tar.gz $(distdir)rm -rf $(distdir)

#TARGET4: create package distribution directory$(distdir):

mkdir -p $(distdir)/src# cp Makefile $(distdir)# cp src/Makefile $(distdir)/src

cp src/main.c $(distdir)/srccp configure.ac $(distdir)cp configure $(distdir)cp Makefile.in $(distdir)cp src/Makefile.in $(distdir)/src

#TARGET5: check the all the functions for both Makefile and src/Makefiledistcheck: $(distdir).tar.gz

gzip -cd $(distdir).tar.gz | tar xvf -# tar -zxvf $(distdir).tar.gz

cd $(distdir) && ./configurecd $(distdir) && $(MAKE) allcd $(distdir) && $(MAKE) checkcd $(distdir) && $(MAKE) installcd $(distdir) && $(MAKE) uninstallcd $(distdir) && $(MAKE) clean rm -rf $(distdir) @echo "**** Package Helloworld-1.0.tar.gz is ready for distribution."

#TARGET6: regenerate Makefile if its template (Makefile.in) has changedMakefile: Makefile.in config.status

./config.status $@

#TARGET7: rebuild config.status when configure changesconfig.status: configure

./config.status --recheck

#TARGET8: refer to actions "all", "clean", "dist", "install" and "uninstall".PHONY: all clean check dist distcheck install uninstall

Demo3: Autoconf for con�guring project

autoreconfGenerate con�gure script from con�gure.ac

./con�gureGenerate Make�le from Make�le.in

make distPackage the software project into tarball

Demo4: Autoconf for automatic Make�lescon�gure.ac

File: /home/mengke/Research/autotoo…/demo4_makefileam/configure.ac Page 1 of 1

#Initializes the Autoconf system#pass 'HelloWorld' to variable "package" in Makefile.in#pass '1.0' to variable "version" in Makefile.inAC_INIT([HelloWorld],[1.0])

#enable automakeAM_INIT_AUTOMAKE

#generate Makefile by running the script './configure'AC_CONFIG_FILES([Makefile src/Makefile])

#check compilersAC_PROG_CC([cc gcc])

#check data type UINT16_TAC_TYPE_UINT16_T

#checkAC_PROG_INSTALL

AC_OUTPUT

Demo4: Autoconf for automatic Make�lesProject_name/Make�le.am

File: /home/mengke/Research/autotoo…o/demo4_makefileam/Makefile.am Page 1 of 1

#specify the source file folderSUBDIRS = src

Project_name/src/Make�le.amFile: /home/mengke/Research/autotoo…mo4_makefileam/src/Makefile.am Page 1 of 1

#Specify the executable file namebin_PROGRAMS = HelloWorld#specify the source fileHelloWorld_SOURCES = main.c

Demo4: Autoconf for automatic Make�les

touch NEWS README AUTHORS ChangeLogGenerate empty �les �NEWS�, �README�, �AUTHORS�, and�ChangeLog�

autoreconf -iGenerate con�gure script and Make�le.in from con�gure.ac andmake�le.am

./con�gureGenerate Make�le from Make�le.in

makeCompile program

make distcheckCheck distribution

Summary

Design con�gure.ac and make�le.am according to your project.

autoreconf -i

I aclocal.m4I autom4te.cacheI con�g.h.inI con�gureI install-shI Make�le.in

Add macros according the messages from previous step; Search libltdllibrary if libtool is used

./con�gure

I It is to generate Make�le

Package your project

Calcote, JohnA practitioner's guide to GNU Autoconf, Automake, and Libtool.

GNU Autotools online