Openwrt development

From WRTnode Wiki Site
Jump to: navigation, search
This page contains changes which are not marked for translation.

Other languages:
English 100% • ‎中文(中国大陆)‎ 100%

1,Setting up the OpenWrt build environment is described in detail in Compiling WRTnode Firmware .Next, it is described how to build a development environment based on OpenWrt.

2、In the source /trunk directory, execute command:

$make menuconfig

Openwrt development1.jpg

Select option [*] Build the OpenWrt SDK, save, exit and make:

$make V=s

3、After compilation completed, in directory /trunk/bin/ramips we should find file OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 . This is the OpenWrt SDK we just compiled. This will be used to develop packages for OpenWrt.

Let's parse the file name and understand the meaning of the SDK: target system is ramips, host system is Linux-x86_64, tools and libraries are compiled with gcc-4.8-linaro_uClibc-0.9.33.2 4、Unzip and navigate SDK

$tar -jxvf OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2
$ cd OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2
$ ls

Openwrt development2.png

Next step is to work on packages inside of this directory.

5、Developing a package named helloworld

(1)Into the package directory:

$cd package

(2)Creating /helloworld directory in the package directory and enter /helloworld directory:

$mkdir helloworld
$cd helloworld

Openwrt development3.png

(3)In /helloworld directory, create a new directory named /src. Then, in directory /src, create Makefile which will be used to compile our hellowrold.c in the /src directory.

$mkdir src
$cd src
$touch helloworld.c
$touch Makefile

Openwrt development4.png

(4)Edit helloworld.c code (file is in /src directory): $vim helloworld.c

#include <stdio.h>
#include <unistd.h>
int main(void){   
 printf("a hellowrold ipk for openwrt !!! \n"); 
 return 0;
 }

(5)Edit Makefile

$vim Makefile
# 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

(6)Now we have to edit another Makefile in the parent directory, /helloworld directory, this Mkefile is used by OpenWRT to generate the package (in the previous step we edited Makefile in /src directory in order to compile helloworld.c)

$ cd ../
$ touch Makefile

Note: ../ represents the parent directory

Openwrt development5.png

$ vim Makefile
###############################################
#OpenWrt Makefile for helloworld program
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_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 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))

(5)Here is the translation of a return to the top of the SDK, which is the following directories:

Openwrt6.png

$make V=s

Compilation process will be completed in the /build_dir catalog generating an ipk package, helloworld_1_ramips_24kec.ipk, check the current directory /bin/ramips/packages.

(7) The helloworld_1_ramips_24kec.ipk has to be uploaded into WRTnode - for Linux systems use scp command:

$Scp xxx / helloworld_1_ramips_24kec.ipk root@192.168.8.1: / usr / bin 

xxx/ fill in the directory where your helloworld_1_ramips_24kec.ipk and 192.168.8.1 is WRTnode's IP.

For Windows systems, use Winscp software to upload ipk package to WRTnode.

(8) Install newly created package on WRTnode development board

$cd /usr/bin$ opkg install helloworld_1_ramips_24kec.ipk

View helloworld program execution results.

$helloworld

The Print Information:

a helloworld ipk for openwrt  !!! 

There is an available OpenWrt SDK

There is an available helloworld package

Create a package using Makefile - consult details at openwrt wiki

References