Include libfyaml as libary with cmake #44144
Replies: 3 comments 4 replies
-
I am aware this issue is old, but I am having the exact same problem. I've also tried the |
Beta Was this translation helpful? Give feedback.
-
If you get stuck at this step you first need to understand CMake. This is not a Zephyr problem, but rather a CMake problem. And yes, it is hard to link a Zephyr app with a random library from the internet, but if you do it correctly and go step by step, you might have a chance as long as the library can even be used on your target system. |
Beta Was this translation helpful? Give feedback.
-
This is a complete minimal working example of how to make libfyaml a Zephyr module. This example consists of configuration files for libfyaml Zephyr module, The module and a sample app in a diff format.diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..bda441e
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: Apache-2.0
+
+cmake_minimum_required(VERSION 3.20.0)
+
+set(MODULE_EXT_ROOT ${CMAKE_CURRENT_LIST_DIR})
+
+find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
+project(libfyaml-sample)
+
+target_sources(app PRIVATE src/main.c)
diff --git a/modules/libfyaml/CMakeLists.txt b/modules/libfyaml/CMakeLists.txt
new file mode 100644
index 0000000..32533d5
--- /dev/null
+++ b/modules/libfyaml/CMakeLists.txt
@@ -0,0 +1,30 @@
+if(CONFIG_LIBFYAML)
+ zephyr_interface_library_named(libfyaml)
+ target_include_directories(libfyaml INTERFACE ${ZEPHYR_CURRENT_MODULE_DIR}/include)
+
+ zephyr_library()
+ zephyr_library_include_directories(
+ ${ZEPHYR_CURRENT_MODULE_DIR}/src
+ ${ZEPHYR_CURRENT_MODULE_DIR}/src/lib
+ ${ZEPHYR_CURRENT_MODULE_DIR}/src/xxhash
+ ${ZEPHYR_CURRENT_MODULE_DIR}/src/util
+ )
+ zephyr_library_compile_definitions(VERSION="0.8")
+ zephyr_library_compile_definitions(
+ _GNU_SOURCE=1
+ NDEBUG=1
+ FY_DEVMODE=1
+ )
+ zephyr_library_compile_options(
+ -Wno-pointer-arith
+ -Wno-return-type
+ -std=gnu99
+ )
+
+ zephyr_library_sources(
+ ${ZEPHYR_CURRENT_MODULE_DIR}/src/lib/fy-parse.c
+ )
+
+ zephyr_library_link_libraries(libfyaml)
+
+endif()
diff --git a/modules/libfyaml/Kconfig b/modules/libfyaml/Kconfig
new file mode 100644
index 0000000..95e6fd3
--- /dev/null
+++ b/modules/libfyaml/Kconfig
@@ -0,0 +1,10 @@
+config LIBFYAML
+ bool "libfyaml"
+
+if LIBFYAML
+
+config APP_LINK_WITH_LIBFYAML
+ bool "Link 'app' with libfyaml"
+ default y
+
+endif
diff --git a/modules/modules.cmake b/modules/modules.cmake
new file mode 100644
index 0000000..bc138c9
--- /dev/null
+++ b/modules/modules.cmake
@@ -0,0 +1,2 @@
+set(ZEPHYR_LIBFYAML_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR}/libfyaml)
+set(ZEPHYR_LIBFYAML_KCONFIG ${CMAKE_CURRENT_LIST_DIR}/libfyaml/Kconfig)
diff --git a/prj.conf b/prj.conf
new file mode 100644
index 0000000..2afdb01
--- /dev/null
+++ b/prj.conf
@@ -0,0 +1,2 @@
+CONFIG_LIBFYAML=y
+CONFIG_PICOLIBC=y
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..a0ef54b
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,8 @@
+#include <zephyr/kernel.h>
+
+#include <libfyaml.h>
+
+void main(void)
+{
+ printk("Hello World! %s\n", fy_library_version());
+}
diff --git a/west.yml b/west.yml
new file mode 100644
index 0000000..18109df
--- /dev/null
+++ b/west.yml
@@ -0,0 +1,18 @@
+manifest:
+
+ projects:
+ - name: zephyr
+ url: https://github.com/zephyrproject-rtos/zephyr
+ revision: main
+ path: zephyr
+ west-commands: scripts/west-commands.yml
+ import:
+ name-allowlist:
+ - cmsis
+ - hal_stm32
+ - picolibc
+
+ - name: libfyaml
+ url: /tmp/ws/repos/libfyaml
+ revision: zephyr
+ path: modules/lib/libfyaml To use it you need a modified version of libfyaml repository. A modification must be made on libfyaml to have zephyr/module.yml in diff format.diff --git a/zephyr/module.yml b/zephyr/module.yml
new file mode 100644
index 0000000..ab1e2b0
--- /dev/null
+++ b/zephyr/module.yml
@@ -0,0 +1,3 @@
+build:
+ cmake-ext: True
+ kconfig-ext: True This example is minimal because it only compiles A few commented out lines diff --git a/src/lib/fy-parse.c b/src/lib/fy-parse.c
index f53362a..ae3ce7a 100644
--- a/src/lib/fy-parse.c
+++ b/src/lib/fy-parse.c
@@ -11,10 +11,10 @@
#include <stdio.h>
#include <string.h>
-#include <sys/mman.h>
+//#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
-#include <sys/ioctl.h>
+//#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
diff --git a/src/lib/fy-types.c b/src/lib/fy-types.c
index 1649e04..ed1a225 100644
--- a/src/lib/fy-types.c
+++ b/src/lib/fy-types.c
@@ -11,7 +11,6 @@
#include <stdio.h>
#include <string.h>
-#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
diff --git a/src/util/fy-utils.h b/src/util/fy-utils.h
index 8644898..dd6598b 100644
--- a/src/util/fy-utils.h
+++ b/src/util/fy-utils.h
@@ -16,7 +16,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
-#include <termios.h>
+//#include <termios.h>
#if defined(__APPLE__) && (_POSIX_C_SOURCE < 200809L)
FILE *open_memstream(char **ptr, size_t *sizeloc);
@@ -52,8 +52,8 @@ int fy_tag_scan(const char *data, size_t len, struct fy_tag_scan_info *info);
#define FY_UNUSED /* nothing */
#endif
-int fy_term_set_raw(int fd, struct termios *oldt);
-int fy_term_restore(int fd, const struct termios *oldt);
+//int fy_term_set_raw(int fd, struct termios *oldt);
+//int fy_term_restore(int fd, const struct termios *oldt);
ssize_t fy_term_write(int fd, const void *data, size_t count);
int fy_term_safe_write(int fd, const void *data, size_t count);
ssize_t fy_term_read(int fd, void *data, size_t count, int timeout_us); To make complete libyaml Zephyr module, you need to
Last but not least, libfyaml is GPL 2.0. Make sure you know what that means. Happy Hacking, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi Folks,
I am struggling since some time to include an external libary to my project, in this case it is libfyaml for parasing YAML files from SD card.
The libfyaml also comes with a CMakeList.txt for building itself.
The project folder structure is:
-- CMakeList.txt
-- src/
-- -- main.c
-- libfyaml/
-- -- CMakeList.txt
-- -- include/
-- -- -- libfyaml.h
-- -- src/../
-- -- -- loadsoffiles.c
My global CMakeList.txt looks like that:
When building it throws an error:
CMake Error: CMake can not determine linker language for target: libfyaml
And I don't really know what I can do about it.Another approach:
When I replace
add_libary()
andtarget_link_libraries()
withadd_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/libfyaml")
it cannot findsys/termios.h
, which tells me that something like the zephyr base is not included while it was compiled.Anyone has a suggestion how to proceed? Which approach should I take and is there a way to get arround those erroros?
I have seen, that there are other threads with a similar topics, which helped, but it's still not successfull.
Thanks for your help, I really appreciate it!
Beta Was this translation helpful? Give feedback.
All reactions