2014年3月17日星期一

自动生成make file


1) 文件结构
SynPhrase包括两个文件夹:
a) synphrase/
collapseverb.cpp  collapseverb.h  expandtemplate.txt  expandverb.cpp  expandverb.h  main.cpp  nohup.out  train.cpp  train.h                                  

b) utility/
alignment.h  morphor.cpp  morphor.h  postagger.h  srl_sentence.h  tree.h  tsuruoka_maxent.h  utility.cpp  utility.h
lapos-0.1.2/
common.h  crf.cpp  crf.h  crfpos.cpp  lookahead.cpp  strdic.h  tokenize.cpp
maxent-3.0/
lbfgs.cpp  lbfgs.h  libtsuruoka_maxent.a  mathvec.h  maxent.cpp  maxent.h  owlqn.cpp  sgd.cpp

2)  运行autoscan
生成configure.scan, 重命名为configure.in, 里面的内容如下
-----------------------------------------------------------------------------------------------------------
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([utility/utility.cpp])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h sys/time.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([pow sqrt strchr strrchr strstr])

AC_OUTPUT
-----------------------------------------------------------------------------------------------------------
修改为
-----------------------------------------------------------------------------------------------------------
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT(syntheticphrase, 1.0)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([synphrase/main.cpp])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_LIBTOOL
AC_PROG_RANLIB

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h sys/time.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
AC_CHECK_FUNCS([pow sqrt strchr strrchr strstr])

# core stuff
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([utility/maxent-3.0/Makefile])
AC_CONFIG_FILES([utility/lapos-0.1.2/Makefile])
AC_CONFIG_FILES([utility/Makefile])
AC_CONFIG_FILES([synphrase/Makefile])

AC_OUTPUT
-----------------------------------------------------------------------------------------------------------
主要包括以下几个:
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([synphrase/main.cpp])
其中synphrase/main.cpp是主程序app

AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([utility/maxent-3.0/Makefile])
AC_CONFIG_FILES([utility/lapos-0.1.2/Makefile])
AC_CONFIG_FILES([utility/Makefile])
AC_CONFIG_FILES([synphrase/Makefile])
为每个子目录指定Makefile

3) 为每个目录添加Makefile.am文件
utility/lapos-0.1.2/Makefile.am
--------------------------------------------------
noinst_LIBRARIES = liblapos.a

liblapos_a_SOURCES = \
  common.h \
  crf.h \
  strdic.h \
  tokenize.cpp \
  lookahead.cpp \
  crfpos.cpp
 
AM_CPPFLAGS = -W -Wall
---------------------------------------------------

utility/maxent-3.0/Makefile.am
---------------------------------------------------------------------
noinst_LIBRARIES = libtsuruoka_maxent.a

libtsuruoka_maxent_a_SOURCES = \
  lbfgs.cpp \
  maxent.cpp \
  owlqn.cpp \
  sgd.cpp
 
AM_CPPFLAGS = -W -Wall
-----------------------------------------------------------------------

utility/maxent-3.0/Makefile.am
-----------------------------------------------------------------------
noinst_LIBRARIES = libtsuruoka_maxent.a

libtsuruoka_maxent_a_SOURCES = \
  lbfgs.cpp \
  maxent.cpp \
  owlqn.cpp \
  sgd.cpp
 
AM_CPPFLAGS = -W -Wall

-bash-4.1$ cat utility/Makefile.am
noinst_LIBRARIES = libutility.a

libutility_a_SOURCES = \
  alignment.h \
  srl_sentence.h \
  tree.h \
  utility.h \
  postagger.h \
  morphor.h \
  tsuruoka_maxent.h \
  argument_reorder_model.h \
  utility.cpp \
  morphor.cpp
 
AM_CPPFLAGS = -W -Wall -I$(top_srcdir)/utility/lapos-0.1.2 -I$(top_srcdir)/utility/maxent-3.0
-----------------------------------------------------------------------

synphrase/Makefile.am
-----------------------------------------------------------------------
bin_PROGRAMS = train

train_SOURCES = main.cpp
train_LDADD = libsynphrase.a ../utility/libutility.a ../utility/maxent-3.0/libtsuruoka_maxent.a ../utility/lapos-0.1.2/liblapos.a -lz

noinst_LIBRARIES = libsynphrase.a

libsynphrase_a_SOURCES = \
  collapseverb.h \
  expandverb.h \
  train.h \
  collapseverb.cpp \
  expandverb.cpp \
  train.cpp
 
AM_CPPFLAGS = -W -Wall -I$(top_srcdir) -I$(top_srcdir)/utility
-----------------------------------------------------------------------

Makefile.am
-----------------------------------------------------------------------
SUBDIRS = \
  utility/maxent-3.0 \
  utility/lapos-0.1.2 \
  utility \
  synphrase

UTOMAKE_OPTIONS = foreign
ACLOCAL_AMFLAGS = -I m4
AM_CPPFLAGS = -D_GLIBCXX_PARALLEL -march=native -mtune=native -O2 -pipe -fomit-frame-pointer -Wall
-----------------------------------------------------------------------

4) 运行 autoreconf
 运行前需要运行mkdir m4 && libtoolize && automake --add-missing
(运行automake --add-missing会提示NEWS, README等文件不存在, 先运行touch NEWS README AUTHORS ChangeLog)

5) 运行./configure

6) 运行make

这时在synphrase目录下生成可执行文件train