Page 1 of 1

Migrating from legacy to CMake build

Posted: Wed Dec 18, 2019 6:34 pm
by PeterR
Have ended up playing 'toolchain' pinball; you know you want to add a feature but your toolchain has an issue & so you have to switch to another toolchain (all I have to do is...) and then you find a whole new world of pain :)

I have a legacy GNU makefile build and now need to migrate to CMake (ESP-IDF 4.1 legacy build seems C++11 broken).
I have not found a simple legacy GNU to CMake 'translation' tutorial.
The problem is that the examples are single instance e.g. examples show single extra directory:

Code: Select all

set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
but I need to convert:

Code: Select all

EXTRA_COMPONENT_DIRS += $(PROJECT_PATH)/components/WebApp/image
EXTRA_COMPONENT_DIRS += $(PROJECT_PATH)/components/NMEA/Application
In other words I need to understand how to list directories or add to the appropriate variable.

The makefile commands I need to translate are (above)+:
Add a pre-built library:

Code: Select all

# The following three lines link in the pre-built NMEA library.
LIBS := nmea
COMPONENT_ADD_LDFLAGS += -L$(COMPONENT_PATH)/lib \
                          $(addprefix -l,$(LIBS))


Add multiple include paths

Code: Select all

COMPONENT_PRIV_INCLUDEDIRS := include  ../../../database/include ../../../third-party/include

Re: Migrating from legacy to CMake build

Posted: Wed Dec 18, 2019 8:34 pm
by chegewara

Re: Migrating from legacy to CMake build

Posted: Wed Dec 18, 2019 8:37 pm
by PeterR
Awesume! Will try tommorow. Was not looking forwards to that hastle.
Santa will visit you this year ;)

Re: Migrating from legacy to CMake build

Posted: Thu Dec 19, 2019 4:12 am
by ESP_Angus
Hi Peter,

The automatic conversion script and the docs page chegewara linked are good places to start.

But also:
PeterR wrote:
Wed Dec 18, 2019 6:34 pm
(ESP-IDF 4.1 legacy build seems C++11 broken).
We still support the legacy build system. If you open a github issue or something to report exactly what's broken then we will fix it.

EDIT: Just scrolled a bit further and saw this, guess the same thing. Will get someone to look into it.

Re: Migrating from legacy to CMake build

Posted: Thu Dec 19, 2019 11:33 am
by PeterR
Cool, I could do with some help!

Needless to say the conversion tool just sulks for a while before crashing!

Code: Select all

$ ./esp-idf/tools/cmake/convert_to_cmake.py ./Application
cygpath: can't convert empty path
Converting ./Application...
Converted C:\msys32\home\Pete\esp\Application\components\NMEA\Target/CMakeLists.txt
Traceback (most recent call last):
  File "./esp-idf/tools/cmake/convert_to_cmake.py", line 195, in <module>
    main()
  File "./esp-idf/tools/cmake/convert_to_cmake.py", line 191, in main
    convert_project(args.project)
  File "./esp-idf/tools/cmake/convert_to_cmake.py", line 123, in convert_project
    p = subprocess.check_output(cmd).strip()
  File "C:/msys32/mingw32/lib/python2.7/subprocess.py", line 224, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['cygpath', '-w', u'']' returned non-zero exit status 1

Re: Migrating from legacy to CMake build

Posted: Thu Dec 19, 2019 10:56 pm
by ESP_Angus
Hi PeterR,

Looks like it's tripping up on some whitespace in the COMPONENT_PATHS list generated by Make, there must be two spaces between one of the entries.

Could you please try applying the following patch and see if it starts working as expected?

Code: Select all

diff --git a/tools/cmake/convert_to_cmake.py b/tools/cmake/convert_to_cmake.py
index 0ba7fa62f5..2ac147006a 100755
--- a/tools/cmake/convert_to_cmake.py
+++ b/tools/cmake/convert_to_cmake.py
@@ -78,14 +78,14 @@ def get_component_variables(project_path, component_path):
             return None
 
         srcs = []
-        for obj in make_vars["COMPONENT_OBJS"].split(" "):
+        for obj in make_vars["COMPONENT_OBJS"].split():
             src = find_src(obj)
             if src is not None:
                 srcs.append(src)
         make_vars["COMPONENT_SRCS"] = " ".join(srcs)
     else:
         component_srcs = list()
-        for component_srcdir in make_vars.get("COMPONENT_SRCDIRS", ".").split(" "):
+        for component_srcdir in make_vars.get("COMPONENT_SRCDIRS", ".").split():
             component_srcdir_path = os.path.abspath(os.path.join(component_path, component_srcdir))
 
             srcs = list()
@@ -114,7 +114,7 @@ def convert_project(project_path):
     if "PROJECT_NAME" not in project_vars:
         raise RuntimeError("PROJECT_NAME does not appear to be defined in IDF project Makefile at %s" % project_path)
 
-    component_paths = project_vars["COMPONENT_PATHS"].split(" ")
+    component_paths = project_vars["COMPONENT_PATHS"].split()
 
     # Convert components as needed
     for p in component_paths:
BTW, something which isn't mentioned about the conversion tool but isn't is that it doesn't determine the "requirements" of a particular component (ie the dependency tree). If you have a component that uses headers from other components, the new component CMakeLists.txt files will need to be manually edited to add these dependencies or the headers won't be found when building. More about how to do that, here:
https://docs.espressif.com/projects/esp ... quirements

Will edit the build docs to mention this.

EDIT: Changed the patch

Re: Migrating from legacy to CMake build

Posted: Fri Dec 20, 2019 10:55 am
by PeterR
Thanks. The patch did not want to apply but manually removing the " " seems to work.
I am sure that I will have some detailed question latter which I will raise in another post.

Re: Migrating from legacy to CMake build

Posted: Fri Jun 18, 2021 11:46 pm
by bradcla
When I run "convert_to_cmake.py", I get a "Python was not found" message. However, when I type "py" from the same directory, I see Python startup and produce a "Python 3.9.5." message. Any ideas what could be going wrong?

Re: Migrating from legacy to CMake build

Posted: Sat Feb 12, 2022 1:58 pm
by nottledim
The `convert_to_cmake.py` program does not exist in latest esp-idf so I've copied it from github. Unfortunately it doesn't work:

Code: Select all

Converting /home/dick/src/esp/projects/fouterlec-lcd...
Traceback (most recent call last):
  File "/home/dick/src/esp/esp-idf/tools/cmake/convert_to_cmake.py", line 205, in <module>
    main()
  File "/home/dick/src/esp/esp-idf/tools/cmake/convert_to_cmake.py", line 201, in main
    convert_project(args.project)
  File "/home/dick/src/esp/esp-idf/tools/cmake/convert_to_cmake.py", line 117, in convert_project
    component_paths = project_vars['COMPONENT_PATHS'].split()
KeyError: 'COMPONENT_PATHS'
And line 117 of the program says:

Code: Select all

   
component_paths = project_vars['COMPONENT_PATHS'].split()
and that's the only reference to COMPONENT_PATHS in the program. I've no idea where it is supposed to come from! Is there a quick fix I could use?

So what's the best way of converting a legacy Make project to the latest idf? Is there a document which gives instructions as to what to do?