Add non-cmake source library to project build?

User avatar
kamumu
Posts: 2
Joined: Fri Dec 13, 2019 4:30 am

Add non-cmake source library to project build?

Postby kamumu » Fri Dec 13, 2019 4:57 am

Howdy,
I'm trying to use libfixmath (a plain Makefile based library) in a simple project derived from the blink example, but not having much success in finding the correct incantations for CMakeLists.txt.

I have the libfixmath source tree in my project root directory (no idea if this is correct/conventional) and with this added to my project's CMakeLists:

Code: Select all

cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
include_directories(libfixmath/libfixmath)
add_library(fixmath STATIC libfixmath/libfixmath)
set_target_properties(fixmath PROPERTIES LINKER_LANGUAGE C)
target_link_libraries(fixmath)

project(blink)
...I can at least get fixmath to build, that is to say I find a libfixmath.a in the build directory when I build my project, but the project ultimately fails to build because fixmath is not getting linked in (a pile of unresolved symbol messages at link time).

I haven't done anything to the fixmath sources, it just has the makefile it came with, no CMakeLists of its own.

So a couple questions:
I doubt I'm doing this "right", so any tips on generally how to integrate foreign libraries into ESP-IDF projects is helpful. I'm not at all familiar with cmake in general and it's complex even before adding in the IDF toolchain.

I'm getting my library built, but despite passing in that link_libraries directive, it's very obviously not getting linked in. How do I tell cmake that I want to link it in?

CMake docs and blog posts seem to span from "very simple example project" to "massive multiplatform desktop app with 1000 deps", with not much covering the simple use case of integrating a fairly trivial 3rd party library.

User avatar
kamumu
Posts: 2
Joined: Fri Dec 13, 2019 4:30 am

Re: Add non-cmake source library to project build?

Postby kamumu » Sat Dec 14, 2019 7:18 am

Figured out a way to do this, posting to potentially save someone else a couple days. Solution was to pretty much ignore CMake on its own and present the library as an IDF component local to the project.

- Make a "components" directory in your project root.
- Put the library source tree in the components directory.
- Add a CMakeLists.txt similar to this to the "{proj root}/components/libfixmath" directory (fixmath sources are in "{proj root}/components/libfixmath/libfixmath"):

Code: Select all

idf_component_register(SRCS "libfixmath/fix16.c"
				"libfixmath/fix16_exp.c"
				"libfixmath/fix16_sqrt.c"
				"libfixmath/fix16_str.c"
				"libfixmath/fract32.c"
				"libfixmath/uint32.c"
				INCLUDE_DIRS "libfixmath")

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Add non-cmake source library to project build?

Postby ESP_Angus » Mon Dec 16, 2019 4:19 am

Hi kamumu,

Glad you found a solution. For simple libraries like this, making a wrapper component that ignores the third party library's build system entirely - like you have done - is the best solution.

CMake does have a feature called ExternalProject that can be used to run a separate build system as part of a project, ie call "make" to build the makefile. However you still need to manually make sure that the external project uses the right cross-compiler toolchain, passes required compiler flags, sees all header directories, etc.

It's usually simpler to do the thing that you've done, unless the external library is very complex or requires some special custom build steps.

mishafarms
Posts: 10
Joined: Mon Sep 11, 2017 10:36 pm

Re: Add non-cmake source library to project build?

Postby mishafarms » Sun Dec 22, 2019 5:45 pm

I tried to use the ExternalProject_Add, but I am having some issues.
When I use this CMakeLists.txt

Code: Select all

ExternalProject_Add(host_mkfatfs
    PREFIX ${COMPONENT_PATH}
    SOURCE_DIR ${COMPONENT_PATH}/src
    CONFIGURE_COMMAND ""
    BUILD_IN_SOURCE 1
    BUILD_COMMAND make
    INSTALL_COMMAND ""
    )

add_custom_target(mkfatfs ALL
    COMMAND echo "This is target 'mkfatfs', and it depends on an external project"
    # If the file exists, then commands related to that file won't be executed
    # DONOT let other target depends on the same OUTPUT as current target,
    #   or it may be bad when doing parallel make
    DEPENDS host_mkfatfs

    # to make quotes printable,for example
    VERBATIM
)
I get this error
Unknown CMake command "ExternalProject_Add".
So I added this to the top of the CMakeLists.txt

Code: Select all

cmake_minimum_required(VERSION 3.5)

include(ExternalProject)
and I get this error.
/home/myuser/.espressif/tools/cmake/3.13.4/share/cmake-3.13/Modules/ExternalProject.cmake:980
(define_property):

define_property command is not scriptable
Any advice on how to proceed?

mishafarms
Posts: 10
Joined: Mon Sep 11, 2017 10:36 pm

Re: Add non-cmake source library to project build?

Postby mishafarms » Thu Dec 26, 2019 11:00 pm

UPDATE.

My issue was not having the following line

Code: Select all

idf_component_register()
everything is working now.

Except that I want to have a new target call it flashfatfs. I want to build this when I want to. For a Makefile project I just added the
target and I could type "make flashfatfs" and make would run the commands.

I can add a new target, but running idf.py build flashfatfs gives me an error about Error: no such command flashfatfs. Is there a way to just build my target?

Who is online

Users browsing this forum: Okoshi Hiroaki and 47 guests