Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding, building and linking Lua in a project #45482

Closed
oscarbaselga opened this issue May 9, 2022 · 1 comment
Closed

Adding, building and linking Lua in a project #45482

oscarbaselga opened this issue May 9, 2022 · 1 comment
Labels
bug The issue is a bug, or the PR is fixing a bug

Comments

@oscarbaselga
Copy link

Bug description
My project has to integrate the Lua scripting language to implement user services. For that purpose, I first included its source code to the project, then coded cmake to build this code and to link the library to the final target, and finally invoked the C API of Lua from the main function. This does not work, it failed at compile time and run time.

My environment:

  • NXP FRDM K64F
  • Zephyr v2.7.0
  • West v0.12.0
  • Zephyr SDK v0.13.1
  • Cmake v3.22.3
  • Linux host system

To reproduce the problem

  1. Project tree
tfm
├── CMakeLists.txt
├── lib
│   └── lua-5.4.4
│       ├── lapi.c
│       ├── lapi.h
│       ├── lauxlib.c
│       ├── lauxlib.h
│       ├── lbaselib.c
│       ├── lcode.c
│       ├── lcode.h
│       ├── lcorolib.c
│       ├── lctype.c
│       ├── lctype.h
│       ├── ldblib.c
│       ├── ldebug.c
│       ├── ldebug.h
│       ├── ldo.c
│       ├── ldo.h
│       ├── ldump.c
│       ├── lfunc.c
│       ├── lfunc.h
│       ├── lgc.c
│       ├── lgc.h
│       ├── linit.c
│       ├── liolib.c
│       ├── ljumptab.h
│       ├── llex.c
│       ├── llex.h
│       ├── llimits.h
│       ├── lmathlib.c
│       ├── lmem.c
│       ├── lmem.h
│       ├── loadlib.c
│       ├── lobject.c
│       ├── lobject.h
│       ├── lopcodes.c
│       ├── lopcodes.h
│       ├── lopnames.h
│       ├── loslib.c
│       ├── lparser.c
│       ├── lparser.h
│       ├── lprefix.h
│       ├── lstate.c
│       ├── lstate.h
│       ├── lstring.c
│       ├── lstring.h
│       ├── lstrlib.c
│       ├── ltable.c
│       ├── ltable.h
│       ├── ltablib.c
│       ├── ltm.c
│       ├── ltm.h
│       ├── luaconf.h
│       ├── lua.h
│       ├── lua.hpp
│       ├── lualib.h
│       ├── lundump.c
│       ├── lundump.h
│       ├── lutf8lib.c
│       ├── lvm.c
│       ├── lvm.h
│       ├── lzio.c
│       └── lzio.h
├── prj.conf
├── README.rst
├── sample.yaml
└── src
    └── main.c
  1. Project code
    3.1. main.c
#include <zephyr.h>
#include <fs/fs.h>

#include <stdlib.h>

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

void main(void)
{
	lua_State *L = luaL_newstate();
  	luaL_openlibs(L);
  	luaL_dostring(L, "print('Hello from Lua')");

	printk("Hello with Lua!\n");

	lua_close(L);
}

2.2. prj.conf

CONFIG_NEWLIB_LIBC=y
CONFIG_FILE_SYSTEM=y
CONFIG_MAIN_STACK_SIZE=8192

2.3. CMakeLists.txt

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(tfm)

# Application source
target_sources(app PRIVATE src/main.c)

file(GLOB lua_sources lib/lua-5.4.4/*.c)
file(GLOB lua_headers lib/lua-5.4.4/*.h)

target_sources(app PRIVATE ${lua_sources} ${lua_headers})

target_compile_options(app PRIVATE -std=gnu99
    -O2 -Wall -Wextra -DLUA_COMPAT_5_3)
target_link_options(app PUBLIC -lm)
target_include_directories(app PRIVATE lib/lua-5.4.4)
  1. Compilation command: west build -b frdm_k64f sample/tfm

Expected behavior
I expect the project compiles and, when flashing and monitoring, the following output is displayed:

Hello from Lua
Hello with Lua!

Impact
It is absolutely necessary that this works, otherwise my project is useless.

Logs and console output
When building the project, the error shown is:

[3/9] Linking C executable zephyr/zephyr_prebuilt.elf
FAILED: zephyr/zephyr_prebuilt.elf zephyr/zephyr_prebuilt.map /home/oscar/zephyr_home/zephyr/build/zephyr/zephyr_prebuilt.map 
: && ccache /home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc  -lm zephyr/CMakeFiles/zephyr_prebuilt.dir/misc/empty_file.c.obj -o zephyr/zephyr_prebuilt.elf  zephyr/CMakeFiles/offsets.dir/./arch/arm/core/offsets/offsets.c.obj  -fuse-ld=bfd  -Wl,-T  zephyr/linker_zephyr_prebuilt.cmd  -Wl,-Map=/home/oscar/zephyr_home/zephyr/build/zephyr/zephyr_prebuilt.map  -Wl,--whole-archive  app/libapp.a  zephyr/libzephyr.a  zephyr/arch/common/libarch__common.a  zephyr/arch/arch/arm/core/aarch32/libarch__arm__core__aarch32.a  zephyr/arch/arch/arm/core/aarch32/cortex_m/libarch__arm__core__aarch32__cortex_m.a  zephyr/arch/arch/arm/core/aarch32/mpu/libarch__arm__core__aarch32__mpu.a  zephyr/lib/libc/newlib/liblib__libc__newlib.a  zephyr/lib/posix/liblib__posix.a  zephyr/boards/arm/frdm_k64f/libboards__arm__frdm_k64f.a  zephyr/subsys/fs/libsubsys__fs.a  zephyr/drivers/clock_control/libdrivers__clock_control.a  zephyr/drivers/console/libdrivers__console.a  zephyr/drivers/gpio/libdrivers__gpio.a  zephyr/drivers/serial/libdrivers__serial.a  zephyr/drivers/timer/libdrivers__timer.a  modules/hal_nxp/hal_nxp/lib..__modules__hal__nxp.a  -Wl,--no-whole-archive  zephyr/kernel/libkernel.a  -L"/home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/thumb/v7e-m/nofp"  -L/home/oscar/zephyr_home/zephyr/build/zephyr  -lgcc  zephyr/arch/common/libisr_tables.a  -mcpu=cortex-m4  -mthumb  -mabi=aapcs  -mfp16-format=ieee  -Wl,--gc-sections  -Wl,--build-id=none  -Wl,--sort-common=descending  -Wl,--sort-section=alignment  -Wl,-u,_OffsetAbsSyms  -Wl,-u,_ConfigAbsSyms  -nostdlib  -static  -no-pie  -Wl,-X  -Wl,-N  -Wl,--orphan-handling=warn  -lm  -Wl,-lc  -L"/home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/arm-zephyr-eabi"/lib/thumb/v7e-m/nofp  -Wl,-lgcc  -lc  -specs=nano.specs && cd /home/oscar/zephyr_home/zephyr/build/zephyr && /opt/cmake-3.22.3-linux-x86_64/bin/cmake -E echo
/home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/../../../../arm-zephyr-eabi/bin/ld.bfd: /home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/arm-zephyr-eabi/lib/thumb/v7e-m/nofp/libc_nano.a(lib_a-unlinkr.o): in function `_unlink_r':
unlinkr.c:(.text._unlink_r+0xc): undefined reference to `_unlink'
/home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/../../../../arm-zephyr-eabi/bin/ld.bfd: /home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/arm-zephyr-eabi/lib/thumb/v7e-m/nofp/libc_nano.a(lib_a-linkr.o): in function `_link_r':
linkr.c:(.text._link_r+0xe): undefined reference to `_link'
/home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/../../../../arm-zephyr-eabi/bin/ld.bfd: /home/oscar/zephyr-sdk-0.13.1/arm-zephyr-eabi/arm-zephyr-eabi/lib/thumb/v7e-m/nofp/libc_nano.a(lib_a-timesr.o): in function `_times_r':
timesr.c:(.text._times_r+0x2): undefined reference to `_times'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/local/bin/cmake --build /home/oscar/zephyr_home/zephyr/build

It seems that zephyr is not able to find the functions _unlink, _link and _times, so if I add a simple implementation in the main.c it works:

clock_t _times(struct tms* tms)
{
	errno = ENOSYS;
	return (clock_t) -1;
}

int _unlink(const char *pathname)
{
	return fs_unlink(pathname);
}

int _link(const char *oldpath, const char *newpath)
{
	errno = ENOSYS;
	return -1;
}

However, once the binary is flashed, a run time error appears:

[00:00:00.009,000] <err> os: ***** BUS FAULT *****
[00:00:00.009,000] <err> os:   Stacking error
[00:00:00.009,000] <err> os:   Imprecise data bus error
[00:00:00.009,000] <err> os:   NXP MPU error, port 3
[00:00:00.009,000] <err> os:     Mode: Supervisor, Data Address: 0x20000a08
[00:00:00.009,000] <err> os:     Type: Write, Master: 0, Regions: 0x8200
[00:00:00.009,000] <err> os: r0/a1:  0xaaaaaaaa  r1/a2:  0xaaaaaaaa  r2/a3:  0x00027556
[00:00:00.009,000] <err> os: r3/a4:  0x01000000 r12/ip:  0x20000084 r14/lr:  0x20000088
[00:00:00.009,000] <err> os:  xpsr:  0x0001b800
[00:00:00.009,000] <err> os: Faulting instruction address (r15/pc): 0x200004c8
[00:00:00.009,000] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
[00:00:00.009,000] <err> os: Current thread: 0x20000490 (unknown)
[00:00:00.091,000] <err> os: Halting system

Additional context
In this repository: https://github.com/Mohren/zephyrlua/tree/master/hello_world_lua5_3_5, the author says that the compilation error is caused by the os and io Lua libs, so he remove them, but this is not a good practice.

@oscarbaselga oscarbaselga added the bug The issue is a bug, or the PR is fixing a bug label May 9, 2022
@stephanosio
Copy link
Member

This is not a Zephyr bug, but a problem/bug with something you are working on.

@zephyrproject-rtos zephyrproject-rtos locked and limited conversation to collaborators May 9, 2022
@stephanosio stephanosio converted this issue into discussion #45485 May 9, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

No branches or pull requests

2 participants