【转载】openwrt框架分析

文章出处:http://blog.csdn.net/kingvenll/article/details/27545221linux

此次讲讲openwrt的结构.web

 

1. 代码上来看有几个重要目录package, target, build_root, bin, dl....vim

---build_dir/host目录是创建工具链时的临时目录网络

---build_dir/toolchain-<arch>*是对应硬件的工具链的目录app

---staging_dir/toolchain-<arch>* 则是工具链的安装位置工具

---target/linux/<platform>目录里面是各个平台(arch)的相关代码ui

---target/linux/<platform>/config-3.10文件就是配置文件了this

---dl目录是'download'的缩写, 在编译前期,须要从网络下载的数据包都会放在这个目录下,这些软件包的一个特色就是,会自动安装在所编译的固件中,也就是咱们make menuconfig的时候,为固件配置的一些软件包。若是咱们须要更改这些源码包,只须要将更改好的源码包打包成相同的名字放在这个目录下,而后开始编译便可。编译时,会将软件包解压到build_dir目录下。spa

---而在build_dir/目录下进行解压,编译和打补丁等。.net

---package目录里面包含了咱们在配置文件里设定的全部编译好的软件包。默认状况下,会有默认选择的软件包。在openwrt中ipk就是一切, 咱们可使用

$ ./scripts/feeds update来对软件包进行更新.

$ ./scripts/feeds search nmap 查找软件包'nmap'

 Search results in feed ’packages’: 
 nmap       Network exploration and/or security auditing utility 

 

$ ./scripts/feeds install nmap 安装'nmap'这个软件

$ make package/symlinks  //估计意思是更新软件源之类的

 

---bin目录下生成了不少bin文件,根据不一样的平台来区分。另外bin/<platform>/package目录,里面有不少ipk后缀的文件,都是package目录下的源码在build_dir目录下编译后的生成的结果。

 

2. 新建本身的packages
对于本身新建的package,而这个package又不须要随固件一块儿安装,换句话说,就是能够当作一个可选软件包的话。咱们能够利用咱们的SDK环境来单独编译,编译后会生成一个ipk的文件包。而后利用 opkg install xxx.ipk 来安装这个软件。

 

下面具体说下,如何编译一个helloword的软件包。
(1)首先,编写helloworld程序
编写helloworld.c
/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/
#include <stdio.h>
#include <unistd.h>
int main(void)
{
     printf("Hell! O' world, why won't my code compile?\n\n");
     return 0;
}


编写Makefile文件
# build helloworld executable when user executes "make"
helloworld: helloworld.o
        $(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
        $(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
        rm *.o helloworld
                                  
在这两个文件的目录下,执行make 应该能够生成helloworld的可执行文件。执行helloworld后,可以打印出“Hell!O' world, why won't my code compile?”。 这一步,主要保证咱们的源程序是能够正常编译的。

 

下面咱们将其移植到OpenWRT上。
(2)将OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2解压
tar –xvf OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2
(3)进入SDK
cd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1
能够看到里面的目录结构跟咱们以前source的目录结构基本相同,所须要编译的软件包,须要放置在package目录下
(4)在package目录下建立helloworld目录
cd package
mkdir helloworld
cd helloworld
(5)建立src目录,拷贝 helloworld文件
mkdir src
cp /home/wrt/test/helloworld.c src
cp /home/wrt/test/Makefile src
(6)在helloworld目录下建立Makefile文件
这个Makefile文件是给OpenWRT读的,而以前写的那个Makefile文件是针对helloworld给编译其读的。两个Makefile不在同一层目录下。


touch Makefile
vim Makefile

Makefile文件模板内容以下:############################################### OpenWrt Makefile for helloworld program### Most of the variables used here are defined in# the include directives below. We just need to# specify a basic description of the package,# where to build our program, where to find# the source files, and where to install the# compiled program on the router.## Be very careful of spacing in this file.# Indents should be tabs, not spaces, and# there should be no trailing whitespace in# lines that are not commented.###############################################include $(TOPDIR)/rules.mk# Name and release number of this packagePKG_NAME:=helloworldPKG_RELEASE:=1# 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 directoryPKG_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 belowdefine Package/helloworld        SECTION:=utils        CATEGORY:=Utilities        TITLE:=Helloworld -- prints a snarky messageendef# Uncomment portion below for Kamikaze and delete DESCRIPTION variable abovedefine 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))(7)返回到SDK的根目录执行make进行编译编译过程会在build_dir目录下完成编译结果会放在 bin/[yourtarget]/package目录下helloworld_1_bcm47xx.ipk(8)上传helloworld_1_bcm47xx.ipk上传helloworld_1_bcm47xx.ipk至路由器执行# opkg install helloworld_1_bcm47xx.ipk而后输入hello而后按Tab键,发现openwrt中已经有helloworld可执行命令。执行 helloworld命令来查看程序的效果。Hell! O' world, why won't my code compile?

相关文章
相关标签/搜索