openwrt添加自定义package APP

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
# PKG_INSTALL:=1

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

include $(INCLUDE_DIR)/package.mk

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

define Build/Configure
endef

define Build/Compile
	$(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)))

src目录下,保存helloword源码文件及Makefile
helloword.c

#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 

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

Makefile

CC = gcc
CFLAGS = -Wall
OBJS = helloword.o

all: helloword

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

helloword: $(OBJS)
	$(CC) -o $@ $(OBJS)

clean:
	rm -f rbcfg *.o

package单独编译helloword,顶层目录执行指令:
make package/app/helloword/compile

eric@eric-PC:~/Documents/work/mt7628/openwrt$ make package/app/helloword/compile
find: 'package/feeds/luci/luci-theme-argon/luci-theme-argon': Too many levels of symbolic links
WARNING: Makefile 'package/feeds/packages/apache/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/telephony/asterisk-16.x/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/avro/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/gnunet/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/gnunet/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/gnunet/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/jose/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/telephony/kamailio-5.x/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/telephony/kamailio-5.x/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/libsearpc/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/network/utils/nftables/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/samba4/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/seafile-ccnet/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/seafile-server/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/ulogd/Makefile' has a dependency on 'jansson', which does not exist
WARNING: Makefile 'package/feeds/packages/yara/Makefile' has a dependency on 'jansson', which does not exist
 make[1] package/app/helloword/compile
 make[2] -C package/libs/toolchain compile
 make[2] -C package/app/helloword compile

编译完成后,openwrt/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

mt7628/openwrt/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目录下!


仅此记录

你可能感兴趣的:(笔记收藏)