Autotools - 윈디하나의 솔라나라

목차

개요

설치

Hello World! 프로그램

Automake, Autoconf 를 사용해 Hello Woeld!출력하는 라이브러리와 어플리케이션을 설치하는 배포판을 만드는 예시를 들었다.
  1. 디렉토리 및 소스파일

    예제는 Hello World!를 출력하는 C프로그램이다. helloworld.c, helloworld.h, main.c 의 세개의 소스파일로 구성되어있다. helloworld.c, helloworld.h 으로 libhelloworld.so 를 빌드하고, main.c 에서 로드해 사용한다.
    main.c
    (102 바이트)
    #include "helloworld.h"
    
    int main(int argc, char *argv[]) {
    	print_helloworld();
    	print_offtsize();
    }
    
    helloworld.h
    (168 바이트)
    #ifndef helloworld_h
    #define helloworld_h
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void print_helloworld();
    void print_offtsize();
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    
    helloworld.c
    (307 바이트)
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include "helloworld.h"
    #include <stdio.h>
    
    void print_helloworld() {
    #ifdef EXTRAMESSAGE
    	printf("Hello, World! %s\n", EXTRAMESSAGE);
    #else
    	printf("Hello, World!\n");
    #endif
    }
    
    void print_offtsize() {
    	printf("off_t size is %d bytes.\n", sizeof(off_t));
    }
    
    windy@wl ~/autotools/src $ wget http://www.solanara.net/contents/includes/autotools/main.c
    windy@wl ~/autotools/src $ wget http://www.solanara.net/contents/includes/autotools/helloworld.h
    windy@wl ~/autotools/src $ wget http://www.solanara.net/contents/includes/autotools/helloworld.c
    
    빌드는 아래와 같이 할 수 있다. 테스트한 후 helloworld 는 삭제한다.
    windy@wl ~/autotools/src $ cc -o helloworld main.c helloworld.c
    windy@wl ~/autotools/src $ ./helloworld
    Hello, World!
    off_t size is 4 bytes.
    windy@wl ~/autotools/src $ rm helloworld
    
  2. Makefile.am 작성

    Automake 용 Makefile.am 을 만든다. Makefile 을 만들기 위함이다.
    windy@wl ~/autotools $ vi Makefile.am
    AUTOMAKE_OPTIONS = foreign
    SUBDIRS = src
    ACLOCAL_AMFLAGS = -I m4
    pkgconfigdir = $(libdir)/pkgconfig
    pkgconfig_DATA = helloworld.pc
    
    windy@wl ~/autotools $ vi src/Makefile.am
    ACLOCAL_AMFLAGS = -I m4
    AM_CFLAGS = -O3
    AM_LDFLAGS =
    bin_PROGRAMS = helloworld
    sbin_PROGRAMS = helloworld
    helloworld_SOURCES = main.c
    helloworld_LDADD = libhelloworld.la
    lib_LTLIBRARIES = libhelloworld.la
    libhelloworld_la_SOURCES = helloworld.c helloworld.h
    libhelloworld_la_LDFLAGS = -version-info $(LIBHELLOWORLD_LIBRARY_VERSION) -release $(LIBHELLOWORLD_RELEASE_VERSION)
    
    여기까지 하면 아래와 같이 파일이 있어야 한다.
    windy@wl ~/autotools $ find . | sort
    ./
    ./Makefile.am
    ./src
    ./src/helloworld.c
    ./src/main.c
    ./src/helloworld.h
    ./src/Makefile.am
    
  3. configure.ac 파일 생성

    Autoconf용 configure.ac 파일을 만든다. 이 파일에는 시스템에서 빌드를 하기 위해 어떤 사항을 검사해야 하는지, 빌드를 위한 옵션으로 어떤것이 있는지 명시한다. 즉 configure.acconfigure스크립트를 만들기 위해 필요한 매크로를 정의한다.

    예를 들어 시스템에서 컴파일러로 gcc(1)cc(1) 중 어떤 컴파일러를 사용하는지 알아내서 각각에 맞는 컴파일 플래그를 지정하는 매크로를 정의하거나, 컴파일에 반드시 필요한 라이브러리가 있는 경우 라이브러리가 미리 인스톨 되어있는지 검사할 수 있는 매크로를 정의할 수 있다. 자주 사용되는 매크로의 경우 미리 만들어 놓은 것도 있다. 예를 들어 시스템이 2GB이상 파일을 읽고 쓸 수 있는지 알려면 AC_SYS_LARGEFILE매크로를 넣어주기만 하면 된다. 미리 정의된 테스트의 목록은 Autoconf - 5 Existing Tests을 참고하자.

    아래는 Hello World! 프로그램에서 사용할 configure.ac 를 만드는 방법에 대해 소개한다.

    windy@wl ~/autotools $ autoscan 1)
    windy@wl ~/autotools $ cp configure.scan configure.ac
    windy@wl ~/autotools $ vi configure.ac
    AC_INIT(helloworld, 1.0, admin@solanara.net) 2)
    AC_CONFIG_FILES([helloworld.pc])
    AC_USE_SYSTEM_EXTENSIONS
    AC_SYS_LARGEFILE
    
    AM_INIT_AUTOMAKE
    LT_INIT
    AM_SILENT_RULES([yes])
    AH_TOP([#ifndef AUTO_CONFIG_H
    #define AUTO_CONFIG_H
    ])
    
    AH_BOTTOM([ 
    #endif /*AUTO CONFIG H */
    ])
    
    LIBHELLOWORLD_RELEASE_VERSION=1
    LIBHELLOWORLD_LIBRARY_VERSION=1:0:0
    AC_SUBST(LIBHELLOWORLD_RELEASE_VERSION)
    AC_SUBST(LIBHELLOWORLD_LIBRARY_VERSION)
    
    AX_LIB_SOCKET_NSL
    
    AC_ARG_ENABLE([32-bit], [AS_HELP_STRING([--enable-32-bit],[Build 32bit Library])], [CFLAGS="$CFLAGS -m32"; LDFLAGS="$LDFLAGS -m32"])
    AC_ARG_ENABLE([64-bit], [AS_HELP_STRING([--enable-64-bit],[Build 64bit Library])], [CFLAGS="$CFLAGS -m64"; LDFLAGS="$LDFLAGS -m64"])
    
    AC_ARG_WITH([extramessage],[AS_HELP_STRING([--with-extramessage],[Print with extra message])], [AC_DEFINE_UNQUOTED(EXTRAMESSAGE, ["$withval"], [Print Extra Message])])
    
    AC_CONFIG_MACRO_DIR([m4])
    root@wl ~/autotools # ifnames src/* 3)
    EXTRAMESSAGE src/helloworld.c
    HAVE_CONFIG_H src/helloworld.c
    __cplusplus src/helloworld.h
    helloworld_h src/helloworld.h
    
    1) configure.scan, autoscan.log 파일이 생성된다. autoscan.log 파일은 지워도 된다. autoscan을 하지 않고 configure.ac를 직접 만들어도 되지만, 이 예제에서는 autoscan에 의해 생성되는 스크립트는 제외하고 보여준다.
    2) AC_INIT 부분을 수정한다. 나머지 스크립트는 AC_OUTPUT 위의 적당한 위치에 추가한다. 스크립트중 AC_SYS_LARGEFILE, AC_USE_SYSTEM_EXTENSIONS, AX_LIB_SOCKET_NSL 은 helloworld 프로그램과는 관련 없다. 예제를 보이기 위해 넣었을 뿐이다. AX_LIB_SOCKET_NSL 은 Autoconf Archive를 설치해야 사용할 수 있다.
    3) 소스를 읽어 #if, #elif, #ifdef, #ifndef, ... 등에 사용된 지시자를 표시해준다. configure.ac 에 필요한 옵션들이 모두 등록했는지 확인하기 위함이다.
  4. helloworld.pc.in 파일 생성

    pkg-config(1)를 지원하려면, helloworld.pc를 생성해야 하며, 이를 생성하고 설치하려면 helloworld.pc.in 을 만들어야 한다. .pc 파일에 대해서는 pkg-config(1)을 참조하자. (Makefile.am, configure.ac 파일에 이미 pkg-config 용 설정이 추가되어있다)
    windy@wl ~/autotools $ vi helloworld.pc.in 1)
    prefix=@prefix@
    exec_prefix=@exec_prefix@
    includedir=@includedir@
    libdir=@libdir@
    
    Name: LibHelloWorld
    Description: Helloworld Library
    URL: http://www.solanara.net/solanara/autotools
    Version: @PACKAGE_VERSION@
    Cflags: -I${includedir}
    Libs: -L${libdir} -lhelloworld
    Libs.private:
    
    configure.ac
    여기까지 작업했다면 사실상 배포판을 위한 모든 작업을 마친 셈이다. 아래부터는 어느 프로젝트에서나 같은 명령을 사용한다. 여기까지 작업한 파일은 autotools.tar.gz에서 받을 수 있다.
  5. configure 파일 및 configure 에 필요한 파일 생성

    이제 configure 를 만들기 위한 마지막 단계다. 아래와 같이 프로그램을 차례대로 실행해주면 작업은 완료된다.
    windy@wl ~/autotools $ libtoolize 1)
    libtoolize: putting auxiliary files in `.'.
    libtoolize: linking file `./ltmain.sh'
    libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
    libtoolize: linking file `m4/libtool.m4'
    libtoolize: linking file `m4/ltoptions.m4'
    libtoolize: linking file `m4/ltsugar.m4'
    libtoolize: linking file `m4/ltversion.m4'
    libtoolize: linking file `m4/lt~obsolete.m4'
    windy@wl ~/autotools $ cp /usr/local/share/aclocal/ax_lib_socket_nsl.m4 ./m4/
    windy@wl ~/autotools $ aclocal 2)
    windy@wl ~/autotools $ autoheader 3)
    windy@wl ~/autotools $ automake --add-missing 4)
    configure.ac:8: installing './compile'
    configure.ac:12: installing './config.guess'
    configure.ac:12: installing './config.sub'
    configure.ac:11: installing './install-sh'
    configure.ac:11: installing './missing'
    src/Makefile.am: installing './depcomp'
    windy@wl ~/autotools $ autoconf 5)
    
    1) m4디렉토리를 생성하고, ltmain.sh파일을 링크한다.
    2) aclocal.m4 파일을 생성한다. autom4te.cache 디렉토리에도 몇개의 캐시 파일을 생성한다.
    3) config.h.in파일을 생성한다.
    4) Makefile.in 파일을 생성하고, compile, config.guess, config.sub, install-sh, missing, depcomp를 링크한다.
    5) configure 파일을 생성한다.

    autoreconf
    configure.ac 가 갱신되었다면 autoheader 부터 autoconf 까지의 명령어를 일일이 실행해야 하는데, autoreconf(1)는 이 과정을 한번에 해준다.

    gettext, gnulib
    gettext를 사용하는 경우 추가로 더 해줘야 할 것이 있다. gettext 의 autopoint(1)항목을 읽어보자. 윈디하나의 솔라나라: 유틸리티 규약, getopt, gettext - gettext항목을 읽어보는 것도 도움될 것이다. autotools와 같이 소개하곤 하는 GNU lib 역시 이식성을 좋게 만들기 위해 사용하지만 이 문서에서는 설명하지 않는다.
  6. 테스트

    configure 스크립트가 생성되었으면 아래와 같이 테스트해본다.
    windy@wl ~/autotools $ ./configure --prefix=/usr/local/hw --enable-64-bit --with-extramessage="SOLANARA"
    windy@wl ~/autotools $ make
    windy@wl ~/autotools $ sudo make install
    windy@wl ~/autotools $ cd /usr/local/hw
    windy@wl /usr/local/hw $ find .
    .
    ./lib
    ./lib/libhelloworld.so
    ./lib/libhelloworld.a
    ./lib/libhelloworld.la
    ./lib/pkgconfig
    ./lib/pkgconfig/helloworld.pc
    ./lib/libhelloworld-1.so.1.0.0
    ./lib/libhelloworld-1.so.1
    ./bin
    ./bin/helloworld
    ./sbin
    ./sbin/helloworld
    windy@wl /usr/local/hw $ ./bin/helloworld
    Hello, World! SOLANARA
    windy@wl /usr/local/hw $ ldd ./bin/helloworld
            libhelloworld-1.so.1 =>  /usr/local/hw/lib/libhelloworld-1.so.1
            libsocket.so.1 =>        /lib/64/libsocket.so.1
            libnsl.so.1 =>   /lib/64/libnsl.so.1
            libc.so.1 =>     /lib/64/libc.so.1
            libmp.so.2 =>    /lib/64/libmp.so.2
            libucrypto.so.1 =>       /lib/64/libucrypto.so.1
            libelf.so.1 =>   /lib/64/libelf.so.1
            libcryptoutil.so.1 =>    /lib/64/libcryptoutil.so.1
            libz.so.1 =>     /lib/64/libz.so.1
    
  7. 배포본 만들기

    아래와같이 배포본을 만들 수 있다. 이 작업을 하기 전에 configure 를 한번 실행시켜야 한다.
    root@wl ~/autotools # make dist
    root@wl ~/autotools # make dist-bzip2
    root@wl ~/autotools # make dist-lzip 1)
    root@wl ~/autotools # make dist-xz 1)
    root@wl ~/autotools # ls -alh *.tar.*
    -rw-r--r--   1 windy    staff       247K  6월 17일  00:00 helloworld-1.0.tar.bz2
    -rw-r--r--   1 windy    staff       304K  6월 17일  00:00 helloworld-1.0.tar.gz
    -rw-r--r--   1 windy    staff       204K  6월 17일  00:00 helloworld-1.0.tar.lz
    -rw-r--r--   1 windy    staff       204K  6월 17일  00:00 helloworld-1.0.tar.xz
    root@wl ~/autotools # 
    
    1) xz, lzip 파일이 필요하다. 설치방법은 윈디하나의 솔라나라: 아카이버 및 컴프레서를 읽어보자.
  8. 표준 타겟

    automake(1) 가 기본적으로 생성하는 make 의 타겟에 대해 설명한다. 흔히 사용하는건 all, install, clean이지만 이외의 타겟에 대해서도 설명한다. 더 자세한 사항은 7.2.6 Standard Targets for Users를 읽어보자.

Solaris용 Autoconf 테스트

diff, patch

GNU patch, GNU diff

RSS ATOM XHTML 5 CSS3