Yocto新增一个hello程序

文章目录

        • 1.编写源代码和Makefile
        • 2.新增配方/recipes
        • 3.尝试编译
        • 4.运行

 刚接触yocto,在这里记录一下怎么向yocto添加一个自己的helloworld并编译到rootfs中去。

1.编写源代码和Makefile

既然是新增一个helloworld,那就和linux中的helloworld一样,需要源码以及编译这个源码的Makefile.
hello.c

#include "stdio.h"
#include "stdlib.h"

int main()
{
	while(1)
	{
		fprintf(stderr, "HelloWorld");
		sleep(1);
	}
	return 0;
}

以及标准的HelloWorld Makefile

hello:hello.o
	$(CC) -o hello hello.o

hello.o: hello.c
	$(CC) -c hello.c

.PHONY : clean
clearn:
	-rm -rf *.o

2.新增配方/recipes

 Yocto编译时是bitbake去读取配方中的配置来进行获取源文件,编译,以及将编译生成的目标文件install到rootfs中去的。
 因此如果我需要让bitbake能编译到我的源码,我就需要让新增一个recipes用于描述这个新增的hello模块该怎么编译,并让bitbake在编译时读取到我新增的recipes.
 要让bitbake能够读取到hello的recipes,我们有多种办法,最简单的就是在已有的layer文件夹下.
首先看一下那些layer会被读取到

zrh@zrh-PC:~/hdd2/poky$ cat ./build/conf/bblayers.conf 
# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
POKY_BBLAYERS_CONF_VERSION = "2"

BBPATH = "${TOPDIR}"
BBFILES ?= ""

BBLAYERS ?= " \
  /home/zrh/hdd2/poky/meta \
  /home/zrh/hdd2/poky/meta-poky \
  /home/zrh/hdd2/poky/meta-yocto-bsp \
  "

所以我选择meta-poky这个layer,在这个layer里面已经存在一个recipes-core的文件夹,新增一个recipes-test用来存放hello的源码和recipes,在recipes-test里面新建hello文件夹,并新增hello.bb
hello.bb

DESCRIPTION = "Hello World Test"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://hello.c;md5=c887acb828cf8aa8512e94eed3e729f0"
PV = "1"
PR = "r0"

SRC_URI = "file://hello.c \
           file://Makefile \ 
          "

S = "${WORKDIR}"

do_compile () {
    make
}

do_install() {
	install -d ${D}${bindir}/
        install -m 0755 ${S}/hello ${D}${bindir}/
}

FILES_${PN} = "${bindir}/hello"

以下是参数说明

  • DESCRIPTION:这个包的描述
  • LIC_FILES_CHKSUM:文件的md5校验值,可以用md5sum hello.c来获取
  • PV:recipe的版本号
  • PR:recipe的修订版本号
  • SRC_URI:源文件列表
  • S:
  • do_compile : 重写了bitbake的函数,是bitbake进行compiletask的时候做的事情
  • do_install:同上
  • bindir:是一个路径变量,可以在总的conf,以及各个packge的conf中找到定义,大部分情况下是 /usr/bin/
  • file:// : 从本地获取文件,会从配方(*.bb)或者附加文件(.bbappend)的子目录中去寻找

再把c文件和Makefile文件添加进去就可以完成这个recipces了,添加完成之后的目录结构是这样的

zrh@zrh-PC:~/hdd2/poky/meta-poky/recipes-test$ tree hello/
hello/
|-- hello
|   |-- Makefile
|   `-- hello.c
`-- hello.bb

1 directory, 3 files

3.尝试编译

在完成上面这些步骤之后,我们就可以单独的编译这个包了。
先查看一下bitbake能不能读取到我们新增的recipes

zrh@zrh-PC:~/hdd2/poky/meta-poky/recipes-test$ bitbake -s | grep hello
go-helloworld                                         :0.1-r0                          
hello                                                   :1-r0  

看起来是能读到
那就可以执行bitbake hello单编一下这个recipes试试了

zrh@zrh-PC:~/hdd2/poky/meta-poky/recipes-test$ bitbake hello
WARNING: Host distribution "deepin-20-Beta" has not been validated with this version of the build system; you may possibly experience unexpected failures. It is recommended that you use a tested distribution.
Loading cache: 100% |#####################################################################################################################################################| Time: 0:00:00
Loaded 1309 entries from dependency cache.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION           = "1.46.0"
BUILD_SYS            = "x86_64-linux"
NATIVELSBSTRING      = "universal"
TARGET_SYS           = "x86_64-poky-linux"
MACHINE              = "qemux86-64"
DISTRO               = "poky"
DISTRO_VERSION       = "3.1.1"
TUNE_FEATURES        = "m64 core2"
TARGET_FPU           = ""
meta                 
meta-poky            
meta-yocto-bsp       = "my-yocto-3.1.1:febbe2944c0c4a04b85fa98fdc261186115954d8"

Initialising tasks: 100% |################################################################################################################################################| Time: 0:00:00
Sstate summary: Wanted 0 Found 0 Missed 0 Current 135 (0% match, 100% complete)
NOTE: Executing Tasks
NOTE: Tasks Summary: Attempted 542 tasks of which 542 didn't need to be rerun and all succeeded.

Summary: There was 1 WARNING message shown.

此时虽然能单编通过,但是我们在rootfs中并没有找到hello,这是因为我们没有将hello这个package添加到编译目标的recipes中,也就是说编译整个Linux发行版的时候是不会编到hello的。
以官方Yocto Project Quick Build.为例,
执行 bitbake core-image-sato 进行编译时,首先是读取的 core-image-sato.bb,所以我们只需要在这个recipes中添加我们的包就可以了,如下所示

diff --git a/meta/recipes-sato/images/core-image-sato.bb b/meta/recipes-sato/images/core-image-sato.bb
index 673106eb6d..c5d01c1ce0 100644
--- a/meta/recipes-sato/images/core-image-sato.bb
+++ b/meta/recipes-sato/images/core-image-sato.bb
@@ -13,3 +13,7 @@ TOOLCHAIN_HOST_TASK_remove_task-populate-sdk-ext = " nativesdk-intltool nativesd
 
 QB_MEM = '${@bb.utils.contains("DISTRO_FEATURES", "opengl", "-m 512", "-m 256", d)}'
 QB_MEM_qemumips = "-m 256"
+
+IMAGE_INSTALL_append += "hello"
+
+

4.运行

执行runqemu qemux86-64启动虚拟机
启动之后点开终端,然后输入hello,回车,会发现hello能被执行,并且不断地在当前终端打印helloworld,表示hello成功的添加到了yocto中

你可能感兴趣的:(Linux杂项,linux)