devlinux
Trong các bài viết trước, chúng ta đã cùng tìm hiểu cách tạo một layer đơn giản trên Yocto và chạy giả lập trên QEMU. Tiếp nối chuỗi bài viết về Yocto Project, hôm nay chúng ta sẽ khám phá Devtool – một công cụ hỗ trợ mạnh mẽ giúp các nhà phát triển dễ dàng tạo, chỉnh sửa và kiểm thử các recipe trong Yocto.
Devtool được thiết kế để đơn giản hóa quy trình làm việc với Yocto. Thay vì phải xử lý thủ công, Devtool cho phép bạn nhanh chóng tạo, sửa đổi và cập nhật các recipe. Ngoài ra, Devtool cũng hỗ trợ build và kiểm thử các package phần mềm trước khi tích hợp vào image cuối cùng.
Devtool là một công cụ mạnh mẽ trong Yocto, được sử dụng để:
Để sử dụng Devtool, trước tiên bạn cần thiết lập môi trường Yocto bằng lệnh sau:
~/yocto/poky$ source oe-init-build-env
Sau đó, dùng lệnh dưới đây để xem chi tiết các subcommand mà Devtool hỗ trợ:
Sao chép
~/yocto/poky/build$ devtool -h
Kết quả trả về sẽ liệt kê các subcommand phổ biến, bao gồm:
Lệnh add giúp tạo một recipe mới từ một source code có sẵn (local hoặc trên server). Sau khi chạy lệnh này, một thư mục workspace sẽ được tạo ra để lưu trữ thông tin về source code và recipe.
Trong ví dụ này, chúng ta sẽ tạo một recipe có tên hello-devtool từ source code trên GitHub:
~/yocto/poky/build$ devtool add hello-devtool https://github.com/DevLinux097/Yocto-For-Dummies.git --srcbranch custom-devtool
Sau khi chạy xong, một recipe mới sẽ được tạo trong thư mục poky/build/workspace/recipes/.... Nội dung source code sẽ được lưu trong thư mục workspace/sources:
~/yocto/poky/build/workspace$ tree -L 1 workspace/
workspace/
├── appends # Lưu các thay đổi khi modify hoặc upgrade
├── conf # Lưu các file cấu hình
├── README
├── recipes # Lưu các recipe mới được tạo
└── sources # Lưu source code
Đọc nội dung mã nguồn được tải về từ git:
~/yocto/poky/build$ cat workspace/sources/hello-devtool/hello-devtool.c
#include <stdio.h>
int main()
{
printf("Devlinux: hello devtool!\n");
return 0;
}
Nội dung file hello-devtool_git.bb sẽ được tự động tạo như sau:
~/yocto/poky/build/workspace/recipes/hello-devtool$ cat hello-devtool_git.bb
# Recipe created by recipetool
# This is the basis of a recipe and may need further editing in order to be fully functional.
# (Feel free to remove these comments when editing.)
# Unable to find any files that looked like license statements. Check the accompanying
# documentation and source headers and set LICENSE and LIC_FILES_CHKSUM accordingly.
#
# NOTE: LICENSE is being set to "CLOSED" to allow you to at least start building - if
# this is not accurate with respect to the licensing of the software being built (it
# will not be in most cases) you must specify the correct value before using this
# recipe for anything other than initial testing/development!
LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""
SRC_URI = "git://github.com/DevLinux097/Yocto-For-Dummies.git;protocol=https;branch=custom-devtool"
# Modify these as desired
PV = "1.0+git${SRCPV}"
SRCREV = "9684263baeb96af50c937da46a63065c42703963"
S = "${WORKDIR}/git"
# NOTE: no Makefile found, unable to determine what needs to be done
do_configure () {
# Specify any needed configure commands here
:
}
do_compile () {
# Specify compilation commands here
:
}
do_install () {
# Specify install commands here
:
}
Lệnh build được sử dụng để build recipe và kiểm thử xem package phần mềm có hoạt động chính xác không.
Chỉnh sửa file hello-devtool_git.bb.
LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""
SRC_URI = "git://github.com/DevLinux097/Yocto-For-Dummies.git;protocol=https;branch=custom-devtool"
# Modify these as desired
PV = "1.0+git${SRCPV}"
SRCREV = "9684263baeb96af50c937da46a63065c42703963"
S = "${WORKDIR}/git"
do_compile () {
# Specify compilation commands here
${CC} ${S}/hello-devtool.c ${LDFLAGS} -o ${S}/hello-devtool
}
do_install () {
# Specify install commands here
install -d ${D}${bindir}
install -m 0755 ${S}/hello-devtool ${D}${bindir}
}
Sau khi chỉnh sửa chúng ta thực hiện build recipe bằng lệnh:
~/yocto/poky/build$ devtool build hello-devtool
Kết quả sẽ hiển thị trạng thái build thành công:
Sstate summary: Wanted 0 Found 0 Missed 0 Current 124 (0% match, 100% complete)
NOTE: Executing Tasks
NOTE: hello-devtool: compiling from external source tree /home/aosp/Working/yocto/sources/poky/build/workspace/sources/hello-devtool
NOTE: Tasks Summary: Attempted 531 tasks of which 523 didn't need to be rerun and all succeeded.
Để kiểm tra ứng dụng trên máy target, bạn có thể thêm recipe vào image bằng cách chỉnh sửa file local.conf:
IMAGE_INSTALL_append = " hello-devtool"
Sau đó, build lại image và chạy trên QEMU:
bitbake core-image-minimal
runqemu qemux86-64 nographic
Trên QEMU, kiểm tra ứng dụng:
root@arm:~# /usr/bin/hello-devtool
Devlinux: hello devtool!
Ở phần trước, chúng ta đã sử dụng phương pháp build lại toàn bộ image để kiểm tra ứng dụng trên QEMU. Tuy nhiên, nếu bạn chỉ muốn kiểm tra nhanh phần mềm của một recipe mà không cần build lại cả image, lệnh devtool deploy sẽ là lựa chọn tối ưu.
Lệnh deploy cho phép bạn triển khai nhanh một recipe đã build lên máy target (ví dụ: QEMU hoặc thiết bị thực). Điều kiện tiên quyết là từ máy local, bạn phải có khả năng SSH vào máy target.
Trong ví dụ này, chúng ta sẽ triển khai recipe hello-devtool lên QEMU.
Đầu tiên, cần thêm openssh vào image để hỗ trợ SSH từ máy local đến QEMU. Mở file conf/local.conf và thêm dòng sau:
IMAGE_INSTALL_append = " openssh"
Sau đó, build lại image để áp dụng thay đổi:
bitbake core-image-minimal
Khởi chạy QEMU với image vừa build:
runqemu qemux86-64 nographic
Sau khi đăng nhập vào QEMU, kiểm tra xem file hello-devtool đã có trong thư mục /usr/bin hay chưa:
root@arm:~# ls -l /usr/bin/ | grep hello
-rwxr-xr-x 1 root root 14296 Mar 9 2018 helloworld
root@arm:~#
Như bạn thấy, file hello-devtool chưa tồn tại vì chúng ta chưa deploy nó.
Từ máy local, sử dụng lệnh devtool deploy-target để triển khai recipe hello-devtool lên QEMU. Giả sử địa chỉ IP của QEMU là 192.168.7.2, chạy lệnh sau:
~/yocto/poky/build$ devtool deploy-target hello-devtool root@192.168.7.2
Sau khi deploy thành công, chạy ứng dụng để kiểm tra kết quả:
root@arm:~# /usr/bin/hello-devtool
Devlinux: hello devtool!
Sau khi hoàn thành việc build và kiểm thử, bạn có thể thêm recipe vào một layer cụ thể bằng lệnh finish:
~/yocto/poky/build$ devtool finish -f hello-devtool ../meta-devlinux/recipes-apps/
Sau khi thêm thành công sẽ có 1 recipe xuất hiện trong meta-devlinux:
### Trước khi thêm
~/yocto/poky$ tree meta-devlinux/recipes-apps
meta-devlinux/recipes-apps
└── hello-world
├── files
│ └── hello-world.c
└── hello-world.bb
### Sau khi thêm
~/yocto/poky$ tree meta-devlinux/recipes-apps
meta-devlinux/recipes-apps
├── hello-devtool
│ └── hello-devtool_git.bb
└── hello-world
├── files
│ └── hello-world.c
└── hello-world.bb
Lệnh modify cho phép bạn chỉnh sửa source code của một recipe hiện có trong một layer.
Dùng lệnh modify để đưa source code của recipe vào workspace:
~/yocto/poky/build$ devtool modify hello-devtool
Chỉnh sửa file hello-devtool.c:
int main()
{
printf("Devlinux: hello devtool!\n");
printf("This line edit in devtool modify.\n");
return 0;
}
Build lại và kiểm tra trên QEMU như các bước trên:
root@qemux86-64:~# hello-devtool
Devlinux: hello devtool!
This line edit in devtool modify.
root@qemux86-64:~#
Nếu bạn muốn hủy bỏ mọi thay đổi trong quá trình modify, dùng lệnh:
devtool reset hello-devtool
Lệnh update-recipe giúp cập nhật recipe khi có thay đổi trong source code.
Chỉnh sửa code:
int main()
{
printf("Devlinux: hello devtool!\n");
printf("This line edit in devtool modify.\n");
printf("This line is changed in devtool update-recipe\n");
return 0;
}
Sau khi chỉnh sửa source code, commit thay đổi và update recipe:
~/yocto/poky/build/workspace/sources/hello-devtool$ git add hello-devtool.c
~/yocto/poky/build/workspace/sources/hello-devtool$ git commit -m "Demo devtool update-recipe"
~/yocto/poky/build$ devtool update-recipe hello-devtool
Sau khi chạy lệnh devtool update-recipe, bạn có thể kiểm tra cấu trúc thư mục recipe trong layer (meta-devlinux) bằng lệnh tree:
~/yocto/poky/meta-devlinux/recipes-apps/hello-devtool$ tree
.
├── hello-devtool
│ └── 0001-Demo-devtool-update-recipe.patch
└── hello-devtool_git.bb
Bạn có thể kiểm tra nội dung của file patch để xem chi tiết các thay đổi đã được ghi nhận. File patch sẽ bao gồm các thay đổi như thêm, sửa hoặc xóa dòng trong mã nguồn.
Devtool là một công cụ mạnh mẽ và linh hoạt, giúp đơn giản hóa quy trình phát triển trên Yocto. Với Devtool, bạn có thể nhanh chóng tạo, chỉnh sửa, kiểm thử và triển khai các recipe, từ đó tối ưu hóa thời gian và công sức khi làm việc với Yocto Project.
devlinux
0 Bình luận