Skip to content
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

Linking external lib against POSIX API #28124

Closed
pablogs9 opened this issue Sep 7, 2020 · 8 comments
Closed

Linking external lib against POSIX API #28124

pablogs9 opened this issue Sep 7, 2020 · 8 comments
Assignees
Labels
area: Build System area: POSIX POSIX API Library bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug Stale

Comments

@pablogs9
Copy link
Contributor

pablogs9 commented Sep 7, 2020

Hello, I'm trying to use Zephyr POSIX API inside an external lib. In order to do that for example I test the sample/application_development/external_lib/ with the following changes:

diff --git a/samples/application_development/external_lib/mylib/src/mylib.c b/samples/application_development/external_lib/mylib/src/mylib.c
index 3f18508c35..7d6b851fe6 100644
--- a/samples/application_development/external_lib/mylib/src/mylib.c
+++ b/samples/application_development/external_lib/mylib/src/mylib.c
@@ -6,9 +6,13 @@
 
 #include "mylib.h"
 #include <stdio.h>
+#include <time.h>
 
 int mylib_hello_world(void)
 {
+       struct timespec tp;
+       clock_gettime(CLOCK_MONOTONIC, &tp);
+
        printf("mylib says: Hello World!\n");
        return 0;
 }
diff --git a/samples/application_development/external_lib/prj.conf b/samples/application_development/external_lib/prj.conf
index 8b042db540..f274e81052 100644
--- a/samples/application_development/external_lib/prj.conf
+++ b/samples/application_development/external_lib/prj.conf
@@ -1 +1,4 @@
 CONFIG_STDOUT_CONSOLE=y
+
+CONFIG_POSIX_API=y
+CONFIG_APP_LINK_WITH_POSIX_SUBSYS=y

And I get the following error:

src/mylib.c: In function 'mylib_hello_world':
src/mylib.c:14:2: warning: implicit declaration of function 'clock_gettime' [-Wimplicit-function-declaration]
   14 |  clock_gettime(CLOCK_MONOTONIC, &tp);
      |  ^~~~~~~~~~~~~
src/mylib.c:14:16: error: 'CLOCK_MONOTONIC' undeclared (first use in this function)
   14 |  clock_gettime(CLOCK_MONOTONIC, &tp);
      |                ^~~~~~~~~~~~~~~

If I put the same couple of lines inside the main.c of the app it builds. Can you provide me some help with the required configuration for this requirement?

Update:
I'm building with this command: west build -b olimex_stm32_e407 -p auto zephyr/samples/application_development/external_lib/

@pablogs9 pablogs9 changed the title Linking POSIX API against external lib Linking external lib against POSIX API Sep 7, 2020
@pabigot
Copy link
Collaborator

pabigot commented Sep 7, 2020

Zephyr's POSIX infrastructure doesn't support the clock_gettime() API, and does not have the various CLOCK_* sources. There was a vision for an enhancement that could support that (#19030) but it didn't go anywhere.

@pabigot
Copy link
Collaborator

pabigot commented Sep 7, 2020

Zephyr's POSIX infrastructure doesn't support the clock_gettime() API, and does not have the various CLOCK_* sources.

No, I'm wrong about that.

I applied the patch you give above to the external library function and am able to build for that platform. This may be a recent fix due to #27939. Please check the current master branch, or 2.4.0-rc1.

@pablogs9
Copy link
Contributor Author

pablogs9 commented Sep 8, 2020

I guess that I have yesterday's branch of Zephyr. Here you have a list of commands for replicating it:

mkdir test_zephyr
cd test_zephyr
west init .
west update
pip3 install --user -r zephyr/scripts/requirements.txt

cd zephyr
git fetch; git checkout master; git pull;
git apply patch.txt # Find patch file attached below
cd ..

export ZEPHYR_TOOLCHAIN_VARIANT=zephyr
export ZEPHYR_SDK_INSTALL_DIR=[YOUR SDK]
west build -b olimex_stm32_e407 -p auto zephyr/samples/application_development/external_lib/

My complete output:

[1/112] Generating linker.cmd
[2/112] Performing build step for 'mylib_project'
FAILED: mylib/src/mylib_project-stamp/mylib_project-build mylib/lib/libmylib.a 
cd /workspaces/foxy_ws/test_zephyr2/zephyr/samples/application_development/external_lib/mylib && make PREFIX=/workspaces/foxy_ws/test_zephyr2/build/mylib CC=/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc AR=/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/arm-zephyr-eabi-ar "CFLAGS=-I/workspaces/foxy_ws/test_zephyr2/zephyr/include -I/workspaces/foxy_ws/test_zephyr2/build/zephyr/include/generated -I/workspaces/foxy_ws/test_zephyr2/zephyr/soc/arm/st_stm32/stm32f4 -I/workspaces/foxy_ws/test_zephyr2/zephyr/lib/libc/minimal/include -I/workspaces/foxy_ws/test_zephyr2/zephyr/drivers -I/workspaces/foxy_ws/test_zephyr2/zephyr/soc/arm/st_stm32/common -I/workspaces/foxy_ws/test_zephyr2/modules/hal/cmsis/CMSIS/Core/Include -I/workspaces/foxy_ws/test_zephyr2/modules/hal/stm32/stm32cube/stm32f4xx/soc -I/workspaces/foxy_ws/test_zephyr2/modules/hal/stm32/stm32cube/stm32f4xx/drivers/include -I/workspaces/foxy_ws/test_zephyr2/modules/hal/stm32/stm32cube/stm32f4xx/drivers/include/Legacy -I/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include -I/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include-fixed -DKERNEL -D__ZEPHYR__=1 -D_FORTIFY_SOURCE=2 -DBUILD_VERSION=v2.4.0-rc1-8-g27e1fd69f987 -D__PROGRAM_START -DSTM32F407xx -DUSE_HAL_DRIVER -DUSE_FULL_LL_DRIVER -DCORE_CM4 -DHSE_VALUE=12000000  -Os -imacros /workspaces/foxy_ws/test_zephyr2/build/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -mcpu=cortex-m4 -mthumb -mabi=aapcs -imacros /workspaces/foxy_ws/test_zephyr2/zephyr/include/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wno-address-of-packed-member -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-strict-overflow -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=/workspaces/foxy_ws/test_zephyr2/zephyr/samples/application_development/external_lib=CMAKE_SOURCE_DIR -fmacro-prefix-map=/workspaces/foxy_ws/test_zephyr2/zephyr=ZEPHYR_BASE -fmacro-prefix-map=/workspaces/foxy_ws/test_zephyr2=WEST_TOPDIR -ffunction-sections -fdata-sections -std=c99 -nostdinc -isystem/workspaces/foxy_ws/test_zephyr2/zephyr/lib/libc/minimal/include -isystem/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include -isystem/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include-fixed" && /usr/bin/cmake -E touch /workspaces/foxy_ws/test_zephyr2/build/mylib/src/mylib_project-stamp/mylib_project-build
mkdir -p /workspaces/foxy_ws/test_zephyr2/build/mylib/obj /workspaces/foxy_ws/test_zephyr2/build/mylib/lib
/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -c -I/workspaces/foxy_ws/test_zephyr2/zephyr/include -I/workspaces/foxy_ws/test_zephyr2/build/zephyr/include/generated -I/workspaces/foxy_ws/test_zephyr2/zephyr/soc/arm/st_stm32/stm32f4 -I/workspaces/foxy_ws/test_zephyr2/zephyr/lib/libc/minimal/include -I/workspaces/foxy_ws/test_zephyr2/zephyr/drivers -I/workspaces/foxy_ws/test_zephyr2/zephyr/soc/arm/st_stm32/common -I/workspaces/foxy_ws/test_zephyr2/modules/hal/cmsis/CMSIS/Core/Include -I/workspaces/foxy_ws/test_zephyr2/modules/hal/stm32/stm32cube/stm32f4xx/soc -I/workspaces/foxy_ws/test_zephyr2/modules/hal/stm32/stm32cube/stm32f4xx/drivers/include -I/workspaces/foxy_ws/test_zephyr2/modules/hal/stm32/stm32cube/stm32f4xx/drivers/include/Legacy -I/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include -I/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include-fixed -DKERNEL -D__ZEPHYR__=1 -D_FORTIFY_SOURCE=2 -DBUILD_VERSION=v2.4.0-rc1-8-g27e1fd69f987 -D__PROGRAM_START -DSTM32F407xx -DUSE_HAL_DRIVER -DUSE_FULL_LL_DRIVER -DCORE_CM4 -DHSE_VALUE=12000000  -Os -imacros /workspaces/foxy_ws/test_zephyr2/build/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -mcpu=cortex-m4 -mthumb -mabi=aapcs -imacros /workspaces/foxy_ws/test_zephyr2/zephyr/include/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wno-address-of-packed-member -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-strict-overflow -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=/workspaces/foxy_ws/test_zephyr2/zephyr/samples/application_development/external_lib=CMAKE_SOURCE_DIR -fmacro-prefix-map=/workspaces/foxy_ws/test_zephyr2/zephyr=ZEPHYR_BASE -fmacro-prefix-map=/workspaces/foxy_ws/test_zephyr2=WEST_TOPDIR -ffunction-sections -fdata-sections -std=c99 -nostdinc -isystem/workspaces/foxy_ws/test_zephyr2/zephyr/lib/libc/minimal/include -isystem/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include -isystem/workspaces/foxy_ws/test_zephyr/toolchain/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/9.2.0/include-fixed -MD -Iinclude src/mylib.c -o /workspaces/foxy_ws/test_zephyr2/build/mylib/obj/mylib.o
src/mylib.c: In function 'mylib_hello_world':
src/mylib.c:14:5: warning: implicit declaration of function 'clock_gettime' [-Wimplicit-function-declaration]
   14 |     clock_gettime(CLOCK_MONOTONIC, &tp);
      |     ^~~~~~~~~~~~~
src/mylib.c:14:19: error: 'CLOCK_MONOTONIC' undeclared (first use in this function)
   14 |     clock_gettime(CLOCK_MONOTONIC, &tp);
      |                   ^~~~~~~~~~~~~~~
src/mylib.c:14:19: note: each undeclared identifier is reported only once for each function it appears in
make: *** [Makefile:13: all] Error 1
[3/112] Building ASM object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/cpu_idle.S.obj
[4/112] Building ASM object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/swap_helper.S.obj
[5/112] Building ASM object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/nmi_on_reset.S.obj
[6/112] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/nmi.c.obj
[7/112] Building C object zephyr/arch/common/CMakeFiles/arch__common.dir/sw_isr_common.c.obj
[8/112] Building C object zephyr/arch/common/CMakeFiles/isr_tables.dir/isr_tables.c.obj
[9/112] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/swap.c.obj
[10/112] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/timer/sys_clock_init.c.obj
[11/112] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/fatal.c.obj
[12/112] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/thread.c.obj
[13/112] Building C object zephyr/arch/arch/arm/core/aarch32/CMakeFiles/arch__arm__core__aarch32.dir/irq_manage.c.obj
[14/112] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/pinmux/stm32/pinmux_stm32.c.obj
[15/112] Building C object zephyr/CMakeFiles/zephyr.dir/drivers/timer/cortex_m_systick.c.obj
ninja: build stopped: subcommand failed.
FATAL ERROR: command exited with status 1: /usr/bin/cmake --build /workspaces/foxy_ws/test_zephyr2/build

patch.txt

@pabigot
Copy link
Collaborator

pabigot commented Sep 8, 2020

Thanks; apparently I had patched the version in my personal development area then built in my nordic development. I've reproduced the problem now.

Based on the description in the documentation this should probably work, but it doesn't.

The problem is that <time.h> when building the external library resolves to the toolchain <time.h>. It needs to resolve to ${ZEPHYR_BASE}/include/posix/time.h. This can be done by modifying the source to include <posix/time.h> instead.

To do it without modifying the source you'd have to somehow communicate the posix_sys interface library flags down into the externally built library. That can be done with:

--- a/samples/application_development/external_lib/CMakeLists.txt
+++ b/samples/application_development/external_lib/CMakeLists.txt
@@ -20,7 +20,7 @@ zephyr_get_compile_definitions_for_lang_as_string(       C definitions)
 zephyr_get_compile_options_for_lang_as_string(           C options)
 
 set(external_project_cflags
-  "${includes} ${definitions} ${options} ${system_includes}"
+  "${includes} -I${ZEPHYR_BASE}/include/posix ${definitions} ${options} ${system_includes}"
   )
 
 include(ExternalProject)

but this doesn't work because Zephyr's <posix/time.h> includes <kernel.h> which includes a generated header for kernel system calls that isn't available when the external library is being built.

To make progress, change the source to include <posix/time.h>.

Fixing the larger issue of making the Zephyr POSIX subsystem able to be used by an external library is likely to be a major effort. I'm going to mark this a bug so we can discuss it in triage today, but it'll probably get converted to an enhancement.

@pabigot pabigot added bug The issue is a bug, or the PR is fixing a bug and removed question labels Sep 8, 2020
@pablogs9
Copy link
Contributor Author

pablogs9 commented Sep 8, 2020

Ok, thank you very much @pabigot

@MaureenHelm MaureenHelm added the priority: low Low impact/importance bug label Sep 8, 2020
@pabigot pabigot removed their assignment Sep 8, 2020
@github-actions
Copy link

github-actions bot commented Nov 8, 2020

This issue has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this issue will automatically be closed in 14 days. Note, that you can always re-open a closed issue at any time.

@pablogs9
Copy link
Contributor Author

pablogs9 commented Feb 2, 2021

@pabigot @MaureenHelm any update on this?

@pabigot
Copy link
Collaborator

pabigot commented Feb 2, 2021

Not to my knowledge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Build System area: POSIX POSIX API Library bug The issue is a bug, or the PR is fixing a bug priority: low Low impact/importance bug Stale
Projects
None yet
Development

No branches or pull requests

5 participants