[IDFGH-2941] ELF file is in wrong place after running cmake --build .

skibbidy
Posts: 8
Joined: Thu Mar 19, 2020 5:06 am

[IDFGH-2941] ELF file is in wrong place after running cmake --build .

Postby skibbidy » Thu Mar 19, 2020 5:32 am

I am using the idf as a lib in my cmake project (idf.cmake). It seems the majority of the build runs through, but during the end of `cmake --build .` I get an error my ELF cannot be found, even when it exists down build/main/MYPROJECT.ELF. It appears either my ELF is in the wrong place or what generates the build/main/CMakeFiles/gen_project_binary.dir/build.make is incorrect.

Code: Select all

[100%] Generating binary image from built executable
esptool.py v2.8
Traceback (most recent call last):
  File "/home/myuser/esp/esp-idf/components/esptool_py/esptool/esptool.py", line 3201, in <module>
    _main()
  File "/home/myuser/esp/esp-idf/components/esptool_py/esptool/esptool.py", line 3194, in _main
    main()
  File "/home/myuser/esp/esp-idf/components/esptool_py/esptool/esptool.py", line 2965, in main
    operation_func(args)
  File "/home/myuser/esp/esp-idf/components/esptool_py/esptool/esptool.py", line 2466, in elf2image
    e = ELFFile(args.input)
  File "/home/myuser/esp/esp-idf/components/esptool_py/esptool/esptool.py", line 1951, in __init__
    with open(self.name, 'rb') as f:
[b]IOError: [Errno 2] No such file or directory: 'MYPROJECT.elf'
[/b]main/CMakeFiles/gen_project_binary.dir/build.make:60: recipe for target '.bin_timestamp' failed
make[2]: *** [.bin_timestamp] Error 1
CMakeFiles/Makefile2:99: recipe for target 'main/CMakeFiles/gen_project_binary.dir/all' failed
make[1]: *** [main/CMakeFiles/gen_project_binary.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
build/main/CMakeFiles/gen_project_binary.dir/build.make looks like this originally

Code: Select all

.bin_timestamp: main/MYPROJECT.elf
        @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --blue --bold --progress-dir=/home/myuser/work/MYPROJECT/build/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Generating binary image from built executable"
[b]        python /home/myuser/esp/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 elf2image --flash_mode dio --flash_freq 40m --flash_size 2MB --elf-sha256-offset 0xb0 -o /home/myuser/work/MYPROJECT/build/MYPROJECT.bin MYPROJECT.elf
[/b]        /usr/bin/cmake -E echo "Generated /home/myuser/work/MYPROJECT/build/MYPROJECT.bin"
        /usr/bin/cmake -E md5sum /home/myuser/work/MYPROJECT/build/MYPROJECT.bin > /home/myuser/work/MYPROJECT/build/.bin_timestamp
but when I change the MYPROJECT.ELF to main/MYPROJECT.ELF at the end of the python call, as below, and run `cmake --build .` it works correctly

Code: Select all

        
python /home/scarlson/esp/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 elf2image --flash_mode dio --flash_freq 40m --flash_size 2MB --elf-sha256-offset 0xb0 -o /home/scarlson/work/SimPill/build/SIMPILL.bin main/SIMPILL.elf


My file structure is:

- components/
--comp1/
---CMakeLisst.txt
---comp1_main.c
-main/
--CMakeLists.txt
--main.c
-build/
sdkconfig
CMakeLists.txt

the top level cmake is simple
CMakeLists.txt:

Code: Select all

cmake_minimum_required(VERSION 3.5)

project(MYPROJECT C)
set(elf_file ${CMAKE_PROJECT_NAME}.elf)

set(INCLUDE_DIRS ${CMAKE_HOME_DIRECTORY}/include)
# You could in theory have more than one subdirectory for 
# piecemeal builds
add_subdirectory(main)
The Cmake nested in main is
main/CMakeLists.txt:

Code: Select all

cmake_minimum_required(VERSION 3.5)
set(SUPPORTED_TARGETS esp32)
include($ENV{IDF_PATH}/tools/cmake/idf.cmake)
idf_build_component(${CMAKE_HOME_DIRECTORY}/components/comp1)

idf_build_process(esp32
	COMPONENTS esp32 freertos esptool_py comp1
	SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig
	BUILD_DIR ${CMAKE_BINARY_DIR})	    

add_executable(${elf_file} main.c)
target_link_libraries(${elf_file} idf::esp32 idf::freertos idf::spi_flash)
idf_build_executable(${elf_file})

set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
to build I run this in the build directory:

Code: Select all

cmake .. -DCMAKE_TOOLCHAIN_FILE=$IDF_PATH/tools/cmake/toolchain-esp32.cmake -DTARGET=esp32 -G 'Unix Makefiles'
cmake --build .
Also my build directory looks like this, note the MYPROJECT.elf at in the main/ dir, not in the top level build dir

Code: Select all

myuser@ubuntu:~/work/MYPROJECT/build$ ls main/
bootloader-prefix  CMakeFiles  cmake_install.cmake  config.env  Makefile  mconf-idf-prefix  MYPROJECT.elf
myuser@ubuntu:~/work/MYPROJECT/build$ ls
bootloader      CMakeFiles           compile_commands.json  esp-idf         flash_bootloader_args  flash_partition_table_args  kconfig_bin      ldgen_libraries.in  Makefile         sdkconfig
CMakeCache.txt  cmake_install.cmake  config                 flash_app_args  flasher_args.json      flash_project_args          ldgen_libraries  main                partition_table
Why might the build.make be incorrectly generated or my ELF placed in the wrong place?

TIA

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: ELF file is in wrong place after running cmake --build .

Postby chegewara » Thu Mar 19, 2020 5:47 pm

Usually elf file ends up in build folder next to bin file, and as you can see there is no path to main in this command:
set(elf_file ${CMAKE_PROJECT_NAME}.elf)
Thats why you need to do this if your elf file is in main subfolder:
but when I change the MYPROJECT.ELF to main/MYPROJECT.ELF at the end of the python call, as below, and run `cmake --build .` it works correctly

skibbidy
Posts: 8
Joined: Thu Mar 19, 2020 5:06 am

Re: ELF file is in wrong place after running cmake --build .

Postby skibbidy » Thu Mar 19, 2020 8:02 pm

chegewara wrote:
Thu Mar 19, 2020 5:47 pm
Usually elf file ends up in build folder next to bin file, and as you can see there is no path to main in this command:
set(elf_file ${CMAKE_PROJECT_NAME}.elf)
Thats why you need to do this if your elf file is in main subfolder:
but when I change the MYPROJECT.ELF to main/MYPROJECT.ELF at the end of the python call, as below, and run `cmake --build .` it works correctly
I understand what you are saying, but when I change it to

Code: Select all

set(elf_file main/${CMAKE_PROJECT_NAME}.elf)
it complains the tart name is invalid

Code: Select all

CMake Error at main/CMakeLists.txt:48 (add_executable):
  The target name "main/SIMPILL.elf" is reserved or not valid for certain
  CMake features, such as generator expressions, and may result in undefined
  behavior.
is it not possible to have a main directory (or src, whatever)? I thought it was

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: ELF file is in wrong place after running cmake --build .

Postby chegewara » Sat Mar 21, 2020 9:05 pm

I think that @Angus can help more in here.

skibbidy
Posts: 8
Joined: Thu Mar 19, 2020 5:06 am

Re: ELF file is in wrong place after running cmake --build .

Postby skibbidy » Sun Mar 22, 2020 7:47 pm

chegewara wrote:
Sat Mar 21, 2020 9:05 pm
I think that @Angus can help more in here.
@chegewara Thanks for the help.

For now I have a workaround at least (move main.c). However this does not follow normal cmake repo structure, I would like to keep this project consistent since the esp32 is not my only target board...

@Angus Any help is appreciated.

ESP_Alvin
Posts: 211
Joined: Thu May 17, 2018 2:26 am

Re: [IDFGH-2941] ELF file is in wrong place after running cmake --build .

Postby ESP_Alvin » Mon Mar 23, 2020 3:44 am

Moderator's note: edit the topic title for issue tracking, thanks.

ESP_renz
Posts: 18
Joined: Tue May 14, 2019 2:41 am

Re: [IDFGH-2941] ELF file is in wrong place after running cmake --build .

Postby ESP_renz » Mon Mar 30, 2020 1:08 am

Why might the build.make be incorrectly generated or my ELF placed in the wrong place?
It's not that the ELF file is in the wrong place, but rather the process that creates a bin out of the elf file wrongly assumes it to be in the top level build directory. Can you create an issue for this in the ESP-IDF GitHub repo so the fix can be tracked?

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 205 guests