您的位置:首页 > 运维架构

openwrt 编译相关

2016-03-26 10:56 507 查看

定制一个ipk

从helloworld开始

在package目录放入helloworld目录,helloworld目录的结构如下
vmuser@vmuser-desktop:~/wtrb-env/openwrt-r32683/trunk$ find package/helloworld/
package/helloworld/
package/helloworld/src
package/helloworld/src/helloworld.c
package/helloworld/src/Makefile
package/helloworld/Makefile

总共3个文件,src目录下的两个文件Makefile,helloworld.c和外层的Makefile文件

helloworld.c
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("a hellowrold ipk for OpenWrt !!! \n");
return 0;
}
Makefile,用于编译helloworld.c
#build a Makefile for helloworld.c
helloworld: helloworld.o
$(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
$(CC) $(CFLAGS) -c helloworld.c
clean:
rm *.o helloworld


外面的Makefile 是openwrt定制的Makefile文件
###############################################
#OpenWrt Makefile for helloworld program
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1.2
# This specifies the directory where we're going to build the program.
# The root build directory, $(BUILD_DIR), is by default the  build_mipsel
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Helloworld -- prints a snarky message
endef
# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
If you can't figure out what this program does, you're probably
brain-dead and need immediate medical attention.
endef
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as   this one
# Specify where and how to install the program. Since we only have one  file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to build a package.
$(eval $(call BuildPackage,helloworld))


make menuconfig 里面选中

Utilities  --->  <*> helloworld......................... Helloworld -- prints a snarky message

在根目录下执行
vmuser@vmuser-desktop:~/wtrb-env/openwrt-r32683/trunk$ make  package/helloworld/compile
make[1] package/helloworld/compile
make[2] -C package/toolchain compile
make[2] -C package/helloworld compile


会生成

bin/ar71xx/packages/helloworld_1.2_ar71xx.ipk

并把bin文件考到rootfs

staging_dir/target-mips_r2_uClibc-0.9.33.2/root-ar71xx/bin/helloworld

在源码根目录下执行make的时候,
make 的时候会把m选项的package,比如helloworld

拷贝到bin/ar71xx/packages/helloworld_1.2_ar71xx.ipk

拷贝到staging_dir/target-mips_r2_uClibc-0.9.33.2/root-ar71xx/bin/helloworld

不会拷贝到build_dir/target-mips_r2_uClibc-0.9.33.2/root-ar71xx/bin/helloworld

而y选项的package会拷贝在这3个目录下

定制一个内核模块

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: