由于minizip除了依賴(lài)于文件一些操作函數(shù)外并不依賴(lài)于其他庫(kù),所以個(gè)人直接編譯運(yùn)行;另外本次移植的是使用的xmake完成移植。
移植記錄
1、選擇合適的移植庫(kù),xmake提供了一些可以跨平臺(tái)移植的庫(kù),這里我選擇了minizip來(lái)進(jìn)行移植。
2、編寫(xiě)xmake.lua配置ToolChains并且引入minizip依賴(lài)
add_rules("mode.debug", "mode.release")
toolchain("aarch64-linux-musleabi")
set_kind("standalone")
set_sdkdir("$(projectdir)/../../tools/gnu_gcc/aarch64-linux-musleabi_for_x86_64-pc-linux-gnu")
on_load(function(toolchain)
os.setenv("PROJ_DIR", os.projectdir()) --For lua embed build script
toolchain:load_cross_toolchain()
toolchain:set("toolset", "cxx", "aarch64-linux-musleabi-g++")
toolchain:set("toolset", "cc", "aarch64-linux-musleabi-gcc")
-- add flags for aarch64
toolchain:add("cxflags", "-march=armv8-a -D__RTTHREAD__ -Wall -n --static -DHAVE_CCONFIG_H", {force = true})
toolchain:add("ldflags", "-march=armv8-a -D__RTTHREAD__ -Wall -n --static", {force = true})
toolchain:add("ldflags", "-T $(projectdir)/../../linker_scripts/aarch64/link.lds", {force = true})
if not is_config("pkg_searchdirs", "dropbear") then
toolchain:add("ldflags", "-L$(projectdir)/../../sdk/rt-thread/lib/aarch64/cortex-a -Wl,--whole-archive -lrtthread -Wl,--no-whole-archive", {force = true})
end
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/include", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../", {force = true})
toolchain:add("includedirs", "$(projectdir)", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/dfs", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/drivers", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/finsh", {force = true})
toolchain:add("includedirs", "$(projectdir)/../../sdk/rt-thread/components/net", {force = true})
toolchain:add("linkdirs", "$(projectdir)/../../sdk/rt-thread/lib/aarch64", {force = true})
if is_config("kind", "debug") then
toolchain:add("cxflags", "-g -gdwarf-2", {force = true})
else
toolchain:add("cxflags", "-O2", {force = true})
end
end)
toolchain_end()
add_requires("minizip")
target("minizip")
set_toolchains("aarch64-linux-musleabi")
set_kind("binary")
add_files("src/minizip.c")
add_packages("minizip")
target("miniunz")
set_toolchains("aarch64-linux-musleabi")
set_kind("binary")
add_files("src/miniunz.c")
add_packages("minizip")
target("minizip_test")
set_toolchains("aarch64-linux-musleabi")
set_kind("binary")
add_files("src/main.c")
add_packages("minizip")
配置cconfig.h,這個(gè)文件如果用scons會(huì)自動(dòng)生成,但是在xmake工程當(dāng)中不會(huì)自動(dòng)生成,所以需要自己實(shí)現(xiàn)
#ifndef CCONFIG_H__
#define CCONFIG_H__
/* Automatically generated file; DO NOT EDIT. */
/* compiler configure file for RT-Thread in GCC/MUSL */
#define HAVE_SYS_SIGNAL_H 1
#define HAVE_SYS_SELECT_H 1
#define HAVE_PTHREAD_H 1
#define HAVE_FDSET 1
#define HAVE_SIGACTION 1
#define HAVE_SIGEVENT 1
#define HAVE_SIGINFO 1
#define HAVE_SIGVAL 1
#endif
3、編寫(xiě)src/main.c程序,這里我用minizip實(shí)現(xiàn)了壓縮example.txt到example.zip
/*
Copyright (c) 2006-2018, RT-Thread Development Team
SPDX-License-Identifier: GPL-2.0
-
Change Logs:
Date Author Notes
2023-05-17 wcx1024979076 The first version
*/
#include "stdio.h"
#include "zip.h"
int main()
{
// 文件名
const char *zipfile = "example.zip";
// 需要壓縮的文件
const char *file = "example.txt";
zipFile zf = zipOpen(zipfile, APPEND_STATUS_CREATE);
if(zf == NULL)
{
printf("Error creating %s \\n", zipfile);
return 1;
}
// 壓縮文件
int err = zipOpenNewFileInZip(zf, file, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION);
if(err != ZIP_OK)
{
printf("Error adding %s to %s \\n", file, zipfile);
return 1;
}
// 讀取文件并壓縮
FILE *f = fopen(file, "rb");
char buf[1024];
int len;
while((len = fread(buf, 1, sizeof(buf), f)) > 0)
{
zipWriteInFileInZip(zf, buf, len);
}
fclose(f);
zipCloseFileInZip(zf);
zipClose(zf, NULL);
printf("Successfully created %s \\n", zipfile);
return 0;
}
4、xmake編譯鏈接生成mininet可執(zhí)行文件,打包進(jìn)入 sd.bin
這里我使用的mcopy來(lái)實(shí)現(xiàn)的(用的是Codespace來(lái)寫(xiě)的代碼,無(wú)root權(quán)限,不能使用mount掛載),具體命令為
mcopy -i sd.bin /path/of/the/minizip ::
5、用qemu虛擬機(jī)運(yùn)行即可
運(yùn)行結(jié)果:
-
虛擬機(jī)
+關(guān)注
關(guān)注
1文章
914瀏覽量
28155 -
GNU
+關(guān)注
關(guān)注
0文章
143瀏覽量
17492 -
SRC算法
+關(guān)注
關(guān)注
0文章
5瀏覽量
7426 -
RTThread
+關(guān)注
關(guān)注
8文章
132瀏覽量
40858
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論