0

I reviewed the response for using the add_custom_command for cmake and was not able to get it to work in android studio. I am having trouble finding the target when run.

add_custom_command( TARGET b.c.o
                    POST_BUILD
                    file(RENAME ${CMAKE_HOME_DIRECTORY}/newfolder/customer.txt ${CMAKE_HOME_DIRECTORY}/newfolder/used_customer.txt)
                  )

I get the following:

CMake Error at CMakeLists.txt:73 (add_custom_command):
  No TARGET ')' has been created in this directory.
-- Configuring incomplete, errors occurred!
See also "C:/Documents/Android_files/local_app/BApp/.externalNativeBuild/cmake/debug/x86_64/CMakeFiles/CMakeOutput.log".

FAILURE: Build failed with an exception.

What went wrong:

A problem occurred configuring project ':BApp'.
    > executing external native build for cmake C:\Documents\Android_files\local_app\BApp\CMakeLists.txt

Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Get more help at https://help.gradle.org

BUILD FAILED in 32s

It appears to not be able to find the target file each time I run it. Can you provide some help with this matter?

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

0

You can't nest cmake commands (file is a cmake command) in add_custom_command. The command from add_custom_command is executed in terminal, not from inside the cmake script. You can use mv in linux to rename a file:

add_custom_command(TARGET b.c.o POST_BUILD COMMAND mv ${CMAKE_HOME_DIRECTORY}/newfolder/customer.txt ${CMAKE_HOME_DIRECTORY}/newfolder/used_customer.txt)

You can use rename in windows to rename a file:

add_custom_command(TARGET b.c.o POST_BUILD COMMAND rename ${CMAKE_HOME_DIRECTORY}/newfolder/customer.txt ${CMAKE_HOME_DIRECTORY}/newfolder/used_customer.txt)

You can use cmake command mode (probably the best option):

add_custom_command(TARGET b.c.o POST_BUILD COMMAND ${CMAKE_COMMAND} -E rename ${CMAKE_HOME_DIRECTORY}/newfolder/customer.txt ${CMAKE_HOME_DIRECTORY}/newfolder/used_customer.txt)

Or you can put the cmake command:

  file(RENAME ${CMAKE_HOME_DIRECTORY}/newfolder/customer.txt ${CMAKE_HOME_DIRECTORY}/newfolder/used_customer.txt)

in a separate file called custom_command.cmake and do:

   add_custom_command(TARGET b.c.o POST_BUILD COMMAND ${CMAKE_COMMAND} -P custom_command.cmake -D CMAKE_HOME_DIRECTORY=${CMAKE_HOME_DIRECTORY} )
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks Kamil. I am still having trouble with finding the target. The problem with cmake is that comprehensive documentation does not exist. Or at least I haven't see any. It would be nice to get access to a real user manual. My target is in a cmake object folder but using the path name does not solve the problem. How do I tell cmake how to find the target? – Tony Skyviper May 07 '18 at 17:26
  • You can find cmake documentation online [here](https://cmake.org/cmake/help/v3.11/command/add_custom_command.html) (remember to choose correct cmake version up left). – KamilCuk May 08 '18 at 08:44
  • cmake object folder? You mean CMAKE_CURRENT_BINARY_DIR? Check out [cmake usefull variables](https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Useful-Variables). – KamilCuk May 08 '18 at 08:48
  • When I run the rename command, it creates a new file rather than overwrite the existing one. I assume this is because the build is not done using the file and blocks deleting the original. If this is the case, how can I tell it to delete after all of the builds are complete? – Tony Skyviper May 09 '18 at 00:02
  • POST_BUILD commands are run lastly, after all have been built. Maybe try with `add_dependencies(b.c.o. )`. To delete the file, you may just add [del](https://www.computerhope.com/delhlp.htm) a second `COMMAND del ` after the first command, or try using [move](https://www.computerhope.com/movehlp.htm) command. – KamilCuk May 09 '18 at 08:43