OpenWRT添加模块(二)configure.in和Makefile.am

前一章实现了一个模块的添加和编译,本章介绍基于autoconf/automake的生成机制将menuconfig中的设置传递到编译系统(Makefile和source code)

对menuconfig设置后,Package/$(PKG_NAME) 和 config/Config.in中定义的变量将获取到对应项的值

package makefile

此时,在Package Makefile中添加如下语句,可以将值传递到./configure(一般软件安装执行make之前的脚本文件)

ifeq ($(CONFIG_FIRST_FEATURE),y)                                               这里的CONFIG_FIRST_FEATURE对应Package目录config/Config.in的FIRST_FEATURE

  CONFIGURE_ARGS += \                                                                   ./configure会以CONFIGURE_ARG作为参数执行
      --enable-first-feature \                                                                     这个标记可以作为一个特性的总控开关                                                           
      --enable-teddy-library="-L$(STAGING_DIR)/src" \                     标记本Package的库文件目录,STAGING_DIR可以看成是一个自动变量
      --enable-teddy-include="-I$(STAGING_DIR)/include"              标记本Package的头文件目录,STAGING_DIR可以看成是一个自动变量

endif
如果成功的话,编译输出./configure时会自动添加--enable-first-macro选项

另外,为了让其他模块能够引用本模块的头文件和库文件,可以增加

define Build/InstallDev
      $(INSTALL_DIR) $(1)/usr/{lib,include}
      $(CP) $(PKG_BUILD_DIR)/src/include/* $(1)/usr/include       将头文件拷贝到指定路径

      mkdir -p $(1)/usr/lib
      $(CP) $(PKG_BUILD_DIR)/src/libteddy.a $(1)/usr/lib               将库文件拷贝到指定路径
endef

source configure.in

为后续Makefile准备一个宏控制开关,需要在configure.in中添加

AC_ARG_ENABLE(first-feature,                                                               不知道干什么,跟--enable-first-feature对应就好
              AS_HELP_STRING([--enable-first-feature],                 
                             [information could show up when compiling]),        提示信息,随便写
   [
      if test "$enableval" = yes; then
        AC_DEFINE([INCLUDE_FIRST_FEATURE], [1],[Include FNSR modifications])           定义一个宏,可以在.config中找到
        AM_CONDITIONAL(INCLUDE_FIRST_FEATURE, true)                                                    使这个宏可以被Makefile使用
         echo Enable first feature of Teddy                                                                                         提示信息,随便
      else
         echo Disable first feature of Teddy                                            
      fi
   ],
   [
      echo Disable first feature of Teddy
   ]
)

头文件和库文件路径也需要传递给Makefile,所以

DEFAULT_TEDDY_INCLUDE_PATH=''
AC_ARG_ENABLE(teddy-include,
   AS_HELP_STRING([--enable-teddy-include=-I/path/to/your/lib_TEDDY/src/include],[set the lib_TEDDY include path]),
   [
    if test -n "$enableval"; then
       echo Set the lib_TEDDY include path to $enableval
       AC_SUBST([TEDDY_INCLUDE_PATH],[$enableval])
    else
       echo Set the lib_TEDDY include path to $DEFAULT_TEDDY_INCLUDE_PATH
       AC_SUBST([TEDDY_INCLUDE_PATH],[$DEFAULT_TEDDY_INCLUDE_PATH])
    fi
   ],
   [
      echo Set the lib_TEDDY include path to $DEFAULT_TEDDY_INCLUDE_PATH
      AC_SUBST([TEDDY_INCLUDE_PATH],[$DEFAULT_TEDDY_INCLUDE_PATH])
   ]
)

DEFAULT_TEDDY_LIBRARY_PATH=''
AC_ARG_ENABLE(teddy-library,
   AS_HELP_STRING([--enable-teddy-library=-L/path/to/your/lib_teddy/src],[set the lib_teddy library path]),
   [
    if test -n "$enableval"; then
       echo Set the lib_TEDDY library path to $enableval
       AC_SUBST([TEDDY_LIBRARY_PATH],[$enableval])
    else
       echo Set the lib_TEDDY library path to $DEFAULT_TEDDY_LIBRARY_PATH
       AC_SUBST([TEDDY_LIBRARY_PATH],[$DEFAULT_TEDDY_LIBRARY_PATH])
    fi
   ],
   [
      echo Set the lib_teddy library path to $DEFAULT_TEDDY_LIBRARY_PATH
      AC_SUBST([TEDDY_LIBRARY_PATH],[$DEFAULT_TEDDY_LIBRARY_PATH])
   ]
)

source Makefile.am

为了让其他模块引用teddy库,在相关模块的Makefile.am中添加(相关模块的configure.in也要参照上节内容进行修改)

if INCLUDE_FIRST_FEATURE

    libomciapi_library += -lteddy

    AM_CFLAGS   += @TEDDY_INCLUDE_PATH@

    AM_LDFLAGS += @TEDDY_LIBRARY_PATH@

endif

如果希望在文件编译过程增加宏定义,还可以添加

    AM_CFLAGS   +=-DTEDDY

你可能感兴趣的:(OpenWRT添加模块(二)configure.in和Makefile.am)