openwrt添加自定义package APP

宁良平
2023-12-01

openwrt 和 buildroot类似,添加自己的app应用程序,参照package里面的模板,建立自己的app

  • 在目录openwrt/package/下,建立app文件夹,存放自己的应用,可仿照openwrt/package/utils/fbtest
eric@eric-PC:~/Documents/work/mt7628/openwrt/package/utils$ tree fbtest/
fbtest/
├── Makefile
└── src
    ├── fbtest.c
    └── Makefile

1 directory, 3 files
  • 新建应用helloword
eric@eric-PC:~/Documents/work/mt7628/openwrt/package/app$ ls -l
总用量 4
drwxr-xr-x 3 eric eric 4096 7月  13 16:56 helloword
eric@eric-PC:~/Documents/work/mt7628/openwrt/package/app$ tree helloword/
helloword/
├── Makefile
└── src
    ├── helloword.c
    └── Makefile

1 directory, 3 files
  • 顶层Makefile 供openwrt调用
    Makefile:
#
# Copyright (C) 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=helloword
PKG_RELEASE:=1.0
# PKG_INSTALL:=1	#是否调用src中的install

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
  SECTION:=app
  CATEGORY:=app
  TITLE:=test app
  DEPENDS:= +libm +libpthread	##依赖库
endef

define Build/Configure
endef

define Package/$(PKG_NAME)/description
 This package for test.
endef

# 屏蔽此处,则调用src下面的makefile
# define Build/Compile
# 	echo $(PKG_BUILD_DIR)
# 	$(MAKE) -C $(PKG_BUILD_DIR) \
# 		CC="$(TARGET_CC)" \
# 		CFLAGS="$(TARGET_CFLAGS) -Wall" \
# 		LDFLAGS="$(TARGET_LDFLAGS)"
# endef

define Package/$(PKG_NAME)/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/bin/
endef

$(eval $(call BuildPackage,$(PKG_NAME)))


  • 生成文件链接index,并进行安装
./scripts/feeds update -i packages
./scripts/feeds install -a
  • 生成链接后,tmp/.packageinfo中应该有如下信息
Source-Makefile: package/app/helloword/Makefile

Package: helloword
Version: 1
Depends: +libc +GCC_LIBSSP:libssp +USE_GLIBC:librt +USE_GLIBC:libpthread 
Conflicts: 
Menu-Depends: 
Provides: 
Section: app
Category: app
Repository: base
Title: test app
Maintainer: 
Source: 
Type: ipkg
Description: test app

@@
  • src目录下,保存helloword源码文件及Makefile
    helloword.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>

int main (int argc,char **argv){
	printf("helloword\r\n");
	return 0;
}

Makefile:

######################################
# eric
######################################

## 指定编译工具
# CC	= mipsel-openwrt-linux-gcc
# CPP	= mipsel-openwrt-linux-g++
# CC ?= "gcc"
RM	= rm -rf

## 目标文件名称
TARGET	:= helloword

## 源文件路径(默认检索3层)
SRC_PATH	:= ./ #$(PKG_BUILD_DIR)
DIRS	:= $(shell find $(SRC_PATH) -maxdepth 3 -type d)

## 获取所有.c文件路径
SRCS	+= $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c)) 
SRCPPS	+= $(foreach dir, $(DIRS), $(wildcard $(dir)/*.cpp))

## 所有对应目标文件
OBJS	:= $(SRCS:.c=.o) $(SRCPPS:.cpp=.o)

## 所有用到的库
LIBS	:= pthread m 

## 指定头文件路径
INCLUDE_PATH	:= . $(DIRS) 

## 指定库文件路径
LIB_PATH	:= /lib /usr/local/lib 

## 编译参数初始化
CFLAGS		+= -g -O3 -pthread -fgnu89-inline #-Wall

## 加载头文件路径
CFLAGS		+= $(foreach dir, $(INCLUDE_PATH), -I$(dir))
# CFLAGS 		+=  `pkg-config --cflags --libs opencv` 

# CXXFLAGS=

## 加载库文件路径
LDFLAGS		+= $(foreach libdir, $(LIB_PATH), -L$(libdir))

## 加载库文件
LDFLAGS 	+= $(foreach lib, $(LIBS), -l$(lib))

.PHONY:all

all: $(TARGET) 
 
$(TARGET) :$(OBJS)
	@$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)
	@#$(RM) $(OBJS)
	@echo =============================
	@echo $(TARGET) ok

%.o	: %.c
	@echo $@...
	@$(CC) -c $(CFLAGS) $< -o $@

%.o	: %.cpp
	@echo $@...
	@$(CPP) -c $(CXXFLAGS) $< -o $@ 

# %.o	: %.S
#     $(CC) -c $(CFLAGS) $< -o $@
 
.PHONY:clean
clean:
	$(RM) $(OBJS) $(TARGET)

  • package单独编译helloword,顶层目录执行指令:
    make package/app/helloword/compile
make[3]: Entering directory '/home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword'
helloword.o...
=============================
helloword ok
make[3]: Leaving directory '/home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword'
touch /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.built
rm -rf /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.pkgdir/helloword.installed /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.pkgdir/helloword
mkdir -p /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.pkgdir/helloword
install -d -m0755 /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.pkgdir/helloword/usr/bin
install -m0755 /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/helloword /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.pkgdir/helloword/usr/bin/
touch /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.pkgdir/helloword.installed
mkdir -p /home/eric/Documents/openwrt_1907/staging_dir/target-mipsel_24kc_musl/root-ramips/stamp
SHELL= flock /home/eric/Documents/openwrt_1907/tmp/.root-copy.flock -c 'cp -fpR /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/.pkgdir/helloword/. /home/eric/Documents/openwrt_1907/staging_dir/target-mipsel_24kc_musl/root-ramips/'
touch /home/eric/Documents/openwrt_1907/staging_dir/target-mipsel_24kc_musl/root-ramips/stamp/.helloword_installed
mkdir -p /home/eric/Documents/openwrt_1907/bin/targets/ramips/mt76x8/packages /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword/CONTROL /home/eric/Documents/openwrt_1907/staging_dir/target-mipsel_24kc_musl/pkginfo
install -d -m0755 /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword/usr/bin
install -m0755 /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/helloword /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword/usr/bin/
find /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword -name 'CVS' -o -name '.svn' -o -name '.#*' -o -name '*~'| xargs -r rm -rf
export CROSS="mipsel-openwrt-linux-musl-"  NO_RENAME=1 ; NM="mipsel-openwrt-linux-musl-nm" STRIP="/home/eric/Documents/openwrt_1907/staging_dir/host/bin/sstrip" STRIP_KMOD="/home/eric/Documents/openwrt_1907/scripts/strip-kmod.sh" PATCHELF="/home/eric/Documents/openwrt_1907/staging_dir/host/bin/patchelf" /home/eric/Documents/openwrt_1907/scripts/rstrip.sh /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword
rstrip.sh: /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword/usr/bin/helloword: executable
(cd /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword/CONTROL; ( echo "$CONTROL"; printf "Description: "; echo "$DESCRIPTION" | sed -e 's,^[[:space:]]*, ,g'; ) > control; chmod 644 control; ( echo "#!/bin/sh"; echo "[ \"\${IPKG_NO_SCRIPT}\" = \"1\" ] && exit 0"; echo "[ -x "\${IPKG_INSTROOT}/lib/functions.sh" ] || exit 0"; echo ". \${IPKG_INSTROOT}/lib/functions.sh"; echo "default_postinst \$0 \$@"; ) > postinst; ( echo "#!/bin/sh"; echo "[ -x "\${IPKG_INSTROOT}/lib/functions.sh" ] || exit 0"; echo ". \${IPKG_INSTROOT}/lib/functions.sh"; echo "default_prerm \$0 \$@"; ) > prerm; chmod 0755 postinst prerm;  )
install -d -m0755 /home/eric/Documents/openwrt_1907/bin/packages/mipsel_24kc/base
/home/eric/Documents/openwrt_1907/scripts/ipkg-build -c -o 0 -g 0 /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword /home/eric/Documents/openwrt_1907/bin/packages/mipsel_24kc/base
Packaged contents of /home/eric/Documents/openwrt_1907/build_dir/target-mipsel_24kc_musl/helloword/ipkg-mipsel_24kc/helloword into /home/eric/Documents/openwrt_1907/bin/packages/mipsel_24kc/base/helloword_1.0_mipsel_24kc.ipk
echo "helloword" >> /home/eric/Documents/openwrt_1907/staging_dir/target-mipsel_24kc_musl/pkginfo/helloword.default.install
make[2]: Leaving directory '/home/eric/Documents/openwrt_1907/package/app/helloword'
  • 编译完成后,bin/packages/mipsel_24kc/base下可看到对应的ipk文件生成
eric@eric-PC:~/Documents/work/mt7628/openwrt/bin/packages/mipsel_24kc/base$ ls *helloword* -l
-rw-r--r-- 1 eric eric 2570 7月  16 11:16 helloword_1_mipsel_24kc.ipk
  • build_dir/target-mipsel_24kc_glibc/helloword目录下,也同样生成了可执行的helloword
eric@eric-PC:~/Documents/work/mt7628/openwrt/build_dir/target-mipsel_24kc_glibc/helloword$ tree
.
├── helloword
├── helloword.c
├── helloword.o
├── ipkg-mipsel_24kc
│   └── helloword
│       ├── CONTROL
│       │   ├── control
│       │   ├── postinst
│       │   └── prerm
│       └── usr
│           └── bin
│               └── helloword
└── Makefile

5 directories, 8 files

  • 下面是openwrt官方Makefile模板,参考即可
include $(TOPDIR)/rules.mk

# Name, version and release number
# The name and version of your package are used to define the variable to point to the build directory of your package: $(PKG_BUILD_DIR)
PKG_NAME:=helloworld
PKG_VERSION:=1.0
PKG_RELEASE:=1

# Source settings (i.e. where to find the source codes)
# This is a custom variable, used below
SOURCE_DIR:=/home/buildbot/helloworld

include $(INCLUDE_DIR)/package.mk

# Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/helloworld
  SECTION:=examples
  CATEGORY:=Examples
  TITLE:=Hello, World!
endef

# Package description; a more verbose description on what our package does
define Package/helloworld/description
  A simple "Hello, world!" -application.
endef

# Package preparation instructions; create the build directory and copy the source code. 
# The last command is necessary to ensure our preparation instructions remain compatible with the patching system.
define Build/Prepare
        mkdir -p $(PKG_BUILD_DIR)
        cp $(SOURCE_DIR)/* $(PKG_BUILD_DIR)
        $(Build/Patch)
endef

# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
define Build/Compile
        $(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/helloworld.o -c $(PKG_BUILD_DIR)/helloworld.c
        $(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/helloworld.o
endef

# Package install instructions; create a directory inside the package to hold our executable, and then copy the executable we built previously into the folder
define Package/helloworld/install
        $(INSTALL_DIR) $(1)/usr/bin
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/usr/bin
endef

# This command is always the last, it uses the definitions and variables we give above in order to get the job done
$(eval $(call BuildPackage,helloworld))

如果源码已经用交叉编译器在宿主机上编译完成,则可直接将生成的可执行文件,安装在系统中。
例如app目录下mqttuser/files/ali是编译好的可执行文件。

eric@eric-PC:~/Documents/work/mt7628/openwrt/package/app$ tree
.
├── helloword
│   ├── Makefile
│   └── src
│       ├── helloword.c
│       └── Makefile
└── mqttuser
    ├── files
    │   └── ali
    └── Makefile
    
4 directories, 5 files

其中,Makefile文件内容:

#
# Copyright (C) 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=mqttuser
PKG_RELEASE:=1
# PKG_INSTALL:=1

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
  SECTION:=app
  CATEGORY:=app
  TITLE:=mqtt user
  DEPENDS:=
endef

# define Build/Configure
# 	mkdir -p $(PKG_BUILD_DIR)
# 	$(CP) ./files/ali $(PKG_BUILD_DIR)
# endef

define Build/Compile
	$(CP) ./files/* $(PKG_BUILD_DIR)
# 	$(MAKE) -C $(PKG_BUILD_DIR) \
# 		CC="$(TARGET_CC)" \
# 		CFLAGS="$(TARGET_CFLAGS) -Wall" \
# 		LDFLAGS="$(TARGET_LDFLAGS)"
endef

define Package/$(PKG_NAME)/install
#     $(INSTALL_DIR) $(1)/usr/lib
# 	$(INSTALL_BIN) $(PKG_BUILD_DIR)/uhttpd_lua.so $(1)/usr/lib/
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ali $(1)/usr/bin
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

以上,编译完成后,可执行文件ali,则自动放在usr/bin目录下!


仅此记录

 类似资料: