buildsystems and what the heck for we ... - installfest.cz · libraries do not use go for...

37
Buildsystems and what the heck for we actually use the autotools Tom´ s Chv´ atal SUSE Packagers team 2013/07/19

Upload: others

Post on 27-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Buildsystems and what the heck for weactually use the autotools

Tomas ChvatalSUSE Packagers team

2013/07/19

Page 2: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Introduction

Page 3: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Who the hell is Tomas Chvatal

• SUSE Employee since 2011 - Team lead of packagers team

• Packager of Libreoffice and various other stuff for openSUSE

• openSUSE promoter and volunteer

• Gentoo developer since fall 2008

3 of 37

Page 4: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Autotools process

Page 5: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Complete autotools process

5 of 37

Page 6: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Make

Page 7: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Why not just a sh script?

Always recompiling everything is a waste of time and CPU power

7 of 37

Page 8: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Plain makefile example

CC ?= @CC@CFLAGS ?= @CFLAGS@PROGRAM = e x a m p l e b i n a r yOBJ = main . o p a r s e r . o output . o$ (PROGRRAM) : $ (OBJ)

$ (CC) $ (LDFLAGS) −o $@ $ˆ

main . o : main . c common . hp a r s e r . o : p a r s e r . c common . houtput . o : output . c common . h s e t u p . h

i n s t a l l : $ (PROGRAM)# You have to use t a b s h e r e

$ ( INSTALL ) $ (PROGRAM) $ ( BINDIR )c l e a n :

$ (RM) $ (OBJ)8 of 37

Page 9: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Variables in Makefiles

• Variables expanded using $(), ie $(VAR)

• Variables are assigned like in sh, ie VAR=value

• $@ current target

• $<the first dependent file

• $ˆall dependent files

9 of 37

Page 10: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Well nice, but why autotools then

• Makefiles can get complex fast (really unreadable)

• Lots of details to keep in mind when writing, small mistakeshappen fast

• Does not make dependencies between targets really easier

• Automake gives you automatic tarball creation (make distcheck)

10 of 37

Page 11: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Autotools

Page 12: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Simplified autotools process

12 of 37

Page 13: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Autoconf/configure sample

AC INIT ( example , 0 . 1 , bugs@example . com )AC CONFIG HEADER ( [ c o n f i g . h ] )

AC PROG CAC PROG CPPAC PROG INSTALL

AC HEADER STDCAC CHECK HEADERS ( [ s t r i n g . h u n i s t d . h l i m i t s . h ] )

AC CONFIG FILES ( [ M a k e f i l edoc / M a k e f i l es r c / M a k e f i l e ] )

AC OUTPUT

13 of 37

Page 14: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Autoconf syntax

• The M4 syntax is quite weird on the first read

• It is not interpreted, it is text substitution machine

• Lots of quoting is needed, if in doubt add more []

• Everything that does or might contain whitespace or commas hasto be quoted

• Custom autoconf M4 macros are almost unreadable

14 of 37

Page 15: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Automake

bin PROGRAMS = e x a m p l e b i n a r y

examplebinary SOURCES = \s r c / main . c \s r c / p a r s e r . c \s r c / output . c \s r c / s e t u p . c

noinst HEADERS = s r c /common . h s r c / s e t u p . h

15 of 37

Page 16: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Basic rules

• Always use just one Makefile.am in root folder

• All files that are to be distributed must be added to relevant partsor EXTRA DIST

• Always run make distcheck to verify your package really works

• Use check BINARIES/etc. . . to have test phase

16 of 37

Page 17: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Variables for automake - SUFFIXES

• PROGRAMS

• LIBRARIES DO NOT USE go for LTLIBRARIES

• SCRIPTS

• SOURCES

• HEADERS

• OBJECTS

• DATA

• LDADD

17 of 37

Page 18: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Variables for automake - PREFIXES

• bin will be installed to bindir

• sbin will be installed to sbindir

• lib will be installed to libdir

• noinst will not be installed

• EXTRA will be packaged upon make dist

• check used only for make check

18 of 37

Page 19: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Libtool

Page 20: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Libtool versioning

• Start with version information of ‘0:0:0’ for each libtool library

• If the library source code has changed at all since the last update,then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)

• If any interfaces have been added, removed, or changed since thelast update, increment current, and set revision to 0

• If any interfaces have been added since the last public release, thenincrement age

• If any interfaces have been removed or changed since the lastpublic release, then set age to 0

20 of 37

Page 21: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

configure.ac changes

LT VERSION=m4 esyscmd ( [ . / v e r s i o n . sh −v ] )LT INIT ( [ d i s a b l e−s t a t i c p i c−o n l y ] )AC PROG LIBTOOL

21 of 37

Page 22: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Makefile.am changes

l ib LTLIBRARIES = l i b e x a m p l e . l al ibexample la SOURCES = \

s r c / someth ing . c \s r c / s o m e t h i n g e l s e . c \s r c / whatever . c

l i b e x a m p l e l a C F L A G S = \$ (MYEXTERNALPACKAGE CFLAGS)

l ibexample la LDFLAGS = \$ (MYEXTERNALPACKAGE LIBS) \−v e r s i o n− i n f o $ ( LT VERSION ) \−e x p o r t−symbols−r e g e x ’ˆ f o o ’

22 of 37

Page 23: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Autotools and windows

Page 24: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Initial thoughts

• Well for multiplatform support you can count on autotools on anyUNIX-ish system

• On windows you have to use cygwin/mingw

• Per above you will spent bit of time getting that running

• You have to write yourself the .rc or rc.in file to be processed bycmake (see librevenge/etc.)

24 of 37

Page 25: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Changes for configure.ac

AC MSG CHECKING ( [ f o r n a t i v e Win32 ] )AS CASE ( [ $hos t ] ,

[∗−∗−mingw∗ ] , [n a t i v e w i n 32=yesBINARY WIN32 RESOURCE=b ina r y−win32 r e s . l oAC CHECK TOOL(WINDRES, w ind r e s )

] , [n a t i v e w i n 32=noBINARY WIN32 RESOURCE=

])# Ensure compat wi th MSVCAS IF ( [ t e s t ” x$na t i v e w i n32 ” = ” xye s ” ] , [

AC CHECK TOOL(WINDRES, w ind r e s )AS IF ( [ t e s t x”$GCC” = xye s ] , [

AC MSG CHECKING ( [ how to get MSVC−compa t i b l e s t r u c t pack ing ] )AS IF ( [ t e s t −z ” $ac cv prog CC ” ] , [

ou r g c c=”$CC”] , [

ou r g c c=”$ac cv prog CC ”] )AS IF ( [ $ou r gcc −v −−he l p 2>/dev/ n u l l | grep ms−b i t f i e l d s >/dev/ n u l l ] , [

m s n a t i v e s t r u c t=”−mms−b i t f i e l d s ”CFLAGS=”$CFLAGS $msn a t i v e s t r u c t ”CXXFLAGS=”$CXXFLAGS $msn a t i v e s t r u c t ”AC MSG RESULT ( [ ${msn a t i v e s t r u c t } ] )

] , [AC MSG RESULT ( [ no way ] )AC MSG WARN( [ produced l i b r a r i e s might be i n c ompa t i b l e w i th MSVC−comp i l ed code ] )

] )] )

] )

25 of 37

Page 26: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Changes for Makefile.am

bin PROGRAMS = examp l eb i na r y

examplebinary SOURCES = \s r c /main . c \s r c / p a r s e r . c \s r c / output . c \s r c / s e tup . c

examplebinary LDADD = \$ (OTHER LIBS) \@BINARY WIN32 RESOURCE@

noinst HEADERS = s r c /common . h s r c / s e tup . h

i f OS WIN32@BINARY WIN32 RESOURCE@ : examp l eb i na r y . r c $ ( examplebinary OBJECTS )

chmod +x $ ( t o p s r c d i r )/ b u i l d /∗ compi le−r e s o u r c e && \WINDRES=@WINDRES@ $ ( t o p s r c d i r )/ b u i l d / l t−compi le−r e s o u r c e examp l eb i na r y . r c @BINARY WIN32 RESOURCE@

e n d i f

26 of 37

Page 27: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Additional points for Makefile.am

• Always pass -avoid-version to libtool

• Remember to add the resource file to DEPENDENCIES

• Script to compile the .lo fileshttps://github.com/AbiWord/enchant/blob/master/lt-compile-resource

27 of 37

Page 28: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Autotools usability

• Not hard as people are led to believe -¿ you can deploy it unlessyour files are too messy

• It, because of mingw, produces slower binaries than MSVC

• Most people are fine with it, but if not use Visual Studio projectfile and be done

• For .rc files you usualy have to use some shellscript as libtool hasno clue

28 of 37

Page 29: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

CMake

Page 30: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

What are the benefits?

• No libtool!

• Multiplatform generator for free Mac/Win/Linux...

• Can swap make for ninja

30 of 37

Page 31: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Any disadvantages?

• FindBLA.cmake are sometimes pretty crappy

• If you rely on just .pc files you loose multiplatformity

• Can get unreadable fast

• Conflicting guides online, fine when you have someone to ask

• Distribution archive generator using CPack confuse many people

31 of 37

Page 32: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

CMake example

cmake min imum requ i red (VERSION 2 . 8 )p r o j e c t ( example C)s e t ( Example VERSION MAJOR 0)s e t ( Example VERSION MINOR 1)

s e t ( src EXAMPLEs r c / main . cs r c / p a r s e r . cs r c / output . cs r c / s e t u p . cs r c /common . hs r c / s e t u p . h

)a d d e x e c u t a b l e ( e x a m p l e b i n a r y ${src EXAMPLE})i n s t a l l (TARGETS e x a m p l e b i n a r y DESTINATION b i n )

32 of 37

Page 33: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

CPack example

i n c l u d e ( I n s t a l l R e q u i r e d S y s t e m L i b r a r i e s )s e t ( CPACK RESOURCE FILE LICENSE

”${CMAKE CURRENT SOURCE DIR}/LICENSE ”)s e t (CPACK PACKAGE VERSION MAJOR ”${Tutorial VERSION MAJOR }”)s e t (CPACK PACKAGE VERSION MINOR ”${Tutorial VERSION MINOR }”)i n c l u d e ( CPack )

33 of 37

Page 34: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Reading

Page 35: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Reading

35 of 37

Page 36: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Endnote

Page 37: Buildsystems and what the heck for we ... - installfest.cz · LIBRARIES DO NOT USE go for LTLIBRARIES SCRIPTS SOURCES HEADERS OBJECTS DATA LDADD 17 of 37. Variables for automake -

Thanks

Thank you for your attention.

37 of 37