openwrt sdk 添加软件包 Makefile 写法

参考 https://openwrt.org/start?id=docs/guide-developer/packages ,英文稍好点的自己看吧,我写出来也就是方便,英文不好的人看。

软件包的来源,有几种来源。git , sourceforge.net , 或是某软件自己的网站下载。 也支持 svn 什么的,不常用的就不介绍了

1, git 下载

PKG_NAME:=dkjson
PKG_VERSION:=2.5
PKG_RELEASE:=3

PKG_SOURCE_URL:=https://github.com/LuaDist/dkjson.git
PKG_SOURCE_PROTO:=git
PKG_SOURCE_VERSION:=e72ba0c9f5d8b8746fc306f6189a819dbb5cd0be

首选是 git 地址 https://github.com/LuaDist/dkjson.git 然后是版本号 e72ba0c9f5d8b8746fc306f6189a819dbb5cd0be

大家都知道git 提交以后,会生成这个吧。 git log 可以查看到。

2, http https 下载

PKG_NAME:=mpg123
PKG_VERSION:=1.25.10
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2
PKG_SOURCE_URL:=@SF/mpg123
PKG_HASH:=6c1337aee2e4bf993299851c70b7db11faec785303cfca3a5c3eb5f329ba7023

表示从 SF 也就是 https://sourceforge.net/ 上面下载,把版本号拼上来下载地址就是 

https://downloads.sourceforge.net/mpg123/mpg123-1.25.10.tar.bz2

PKG_NAME:=mpd
PKG_VERSION:=0.20.20
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=http://www.musicpd.org/download/mpd/0.20/
PKG_HASH:=a9e458c6e07cdf62649de7722e1e5a7f13aa82eeb397bfbbebc07cf5cf273584

没有放到 sf.net 的 从自己的网站下载

PKG_HASH 是 sha256 

3, 配置说明信息

define Package/libev
  SECTION:=libs
  CATEGORY:=Libraries
  TITLE:=High-performance event loop
  URL:=http://software.schmorp.de/pkg/libev.html
  DEPENDS:=+zlib +libpthread 
endef

在 make menuconfig 哪个选择项目里面出现,以及说明信息。

4, configure 编译附加指令

CONFIGURE_ARGS += 
    --enable-shared 
    --enable-static 

编译出来 动态库, 静态库。

功能介绍

表示在哪个选项包里面
define Package/cJSON

描述
define Package/cJSON/description

编译配置
define Build/Configure

编译前
define Build/Prepare

编译
define Build/Compile

安装
define Package/cJSON/install

安装到 dev 一般是头文件和 静态库 动态库
define Build/InstallDev

有几个不是必须的,如果 不写就是用默认的

例:添加一个 cJSON 包

#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
# https://github.com/DaveGamble/cJSON.git
#
include $(TOPDIR)/rules.mk

PKG_NAME:=cJSON   #包名
PKG_VERSION:=1.7.7 #版本
PKG_RELEASE:=1
PKG_MAINTAINER:=DaveGamble #作者

PKG_SOURCE_PROTO:=git  #git
PKG_SOURCE_URL:=https://github.com/DaveGamble/cJSON.git #git 地址
PKG_MIRROR_HASH:=9fe484dd954f6c573fee367ecea5aadfb743bee0a9d6aa917c29528b73fa5fa3 #自动生成的不用管
PKG_SOURCE_VERSION:=787d651e8131c6394c6ff844f108e1a53012949f #git 提交记录
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz  #在 dl 中保存的文件名
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)

PKG_LICENSE:=MIT #许可证
PKG_LICENSE_FILES:=LICENSE #许可证文件

include $(INCLUDE_DIR)/package.mk

define Package/cJSON 
  SECTION:=net
  CATEGORY:=Network
  TITLE:=cJSON
  URL:=https://github.com/DaveGamble

endef

define Package/cJSON/description
    Ultralightweight JSON parser in ANSI C.
endef

define Build/InstallDev #安装头文件 静态库 动态库什么的
    $(INSTALL_DIR) $(1)/usr/include
    $(CP) $(PKG_BUILD_DIR)/cJSON.h $(1)/usr/include/
    $(INSTALL_DIR) $(1)/usr/lib
    $(CP) $(PKG_BUILD_DIR)/libcjson.{a,so*} $(1)/usr/lib/
endef

define Package/cJSON/install #安装到 固件中的 动态库 可执行程序
    $(INSTALL_DIR) $(1)/usr/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/cJSON_test $(1)/usr/bin
    $(INSTALL_DIR) $(1)/usr/lib
    $(CP) $(PKG_BUILD_DIR)/libcjson.so.* $(1)/usr/lib/
    $(CP) $(PKG_BUILD_DIR)/libcjson_utils.so.* $(1)/usr/lib/
endef

$(eval $(call BuildPackage,cJSON))

另外一个 cmake 版

#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
# https://github.com/DaveGamble/cJSON.git
#
include $(TOPDIR)/rules.mk

PKG_NAME:=cJSON
PKG_VERSION:=1.7.7
PKG_RELEASE:=1
PKG_MAINTAINER:=DaveGamble

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/DaveGamble/cJSON.git
PKG_MIRROR_HASH:=9fe484dd954f6c573fee367ecea5aadfb743bee0a9d6aa917c29528b73fa5fa3
PKG_SOURCE_VERSION:=787d651e8131c6394c6ff844f108e1a53012949f
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)

PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=LICENSE

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk  #添加 cmake

define Package/cJSON
  SECTION:=net
  CATEGORY:=Network
  TITLE:=cJSON
  URL:=https://github.com/DaveGamble
endef

define Package/cJSON/description
    Ultralightweight JSON parser in ANSI C.
endef

#cmake 参数 CMAKE_OPTIONS
+= -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TEST=On -DBUILD_SHARED_AND_STATIC_LIBS=On -DCMAKE_INSTALL_PREFIX=/usr define Build/InstallDev $(INSTALL_DIR) $(1)/usr/include $(CP) $(PKG_INSTALL_DIR)/usr/include/cjson $(1)/usr/include/ $(INSTALL_DIR) $(1)/usr/lib $(CP) $(PKG_INSTALL_DIR)/usr/lib/libcjson.{a,so*} $(1)/usr/lib/ endef define Package/cJSON/install $(INSTALL_DIR) $(1)/usr/bin $(INSTALL_BIN) $(PKG_BUILD_DIR)/cJSON_test $(1)/usr/bin $(INSTALL_DIR) $(1)/usr/lib $(CP) $(PKG_INSTALL_DIR)/usr/lib/libcjson.so.* $(1)/usr/lib/ $(CP) $(PKG_INSTALL_DIR)/usr/lib/libcjson_utils.so.* $(1)/usr/lib/ endef $(eval $(call BuildPackage,cJSON))

cmake 的编译还是比较简单的。

写了一个 测试 cjson 的小程序 

 1 #include <stdio.h>
 2 #include <cjson/cJSON.h>
 3 
 4 int main(int argc, char **argv)
 5 {
 6     cJSON *root;
 7     
 8     root = cJSON_CreateObject();
 9     cJSON_AddStringToObject(root, "VER",       "1.0");
10     cJSON_AddStringToObject(root, "IP",        "127.0.0.1");
11     cJSON_AddStringToObject(root, "DATE",      "2018");
12     
13     printf("cjson: %s", cJSON_PrintUnformatted(root));
14     cJSON_Delete(root);
15     return 0;
16 }

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:=cjson_hello
PKG_VERSION:=1.0
PKG_RELEASE:=1

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/cjson_hello
    SECTION:=examples
    CATEGORY:=Examples
    TITLE:=cjson_hello, World!
    DEPENDS:=+cJSON
endef

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

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    cp ./src/* $(PKG_BUILD_DIR)
    $(Build/Patch)
endef

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

$(eval $(call BuildPackage,cjson_hello))

只需要添加这行 引用 库 就可以了 DEPENDS:=+cJSON , openwrt 真的是太方便了,这点比 yocto 易用。

看测试截图

原文地址:https://www.cnblogs.com/ningci/p/9460524.html