1

I'm writing a custom command in CMake to convert a file to binary format at build time. However, the tool I'm using requires directory for a file to be pre-existed; it doesn't create a directory automatically.

Currently my CMake macro looks like this:

MACRO(ConvertToBinary _name)
    ADD_CUSTOM_COMMAND(
        OUTPUT ${CMAKE_BINARY_DIR}/${_name}.bin
        COMMAND ${EXE_DIR}/toBinary -i ${CMAKE_CURRENT_SOURCE_DIR}/${_name} -o ${CMAKE_BINARY_DIR}/${_name}.bin
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_name}
    )
ENDMACRO(ConvertToBinary)

This macro doesn't work for _name parameter equal, e.g., to "test/sample.txt", because the tool is run with test/ folder under ${CMAKE_BINARY_DIR} being not existed.

How to create the directory before the tool, generating the file, is run?


I have tried to overcome the problem by pre-creating the file (and directory) with the help of CMake. So the tool will be run with the directory created, and can override the output file. I have tried the following:

MACRO(ConvertToBinary _name)
    ADD_CUSTOM_COMMAND(
        OUTPUT ${CMAKE_BINARY_DIR}/${_name}.bin
        FILE(WRITE ${CMAKE_BINARY_DIR}/${_name}.bin "Dummy")
        COMMAND ${EXE_DIR}/toBinary -i ${CMAKE_CURRENT_SOURCE_DIR}/${_name} -o ${CMAKE_BINARY_DIR}/${_name}.bin
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_name}
    )
ENDMACRO(ConvertToBinary)

But it did not work. I can't find information on how to create file at build time with CMake, so any help is appreciated.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Cyril
  • 559
  • 5
  • 17
  • 1
    Use CMake's [command line tool mode](https://cmake.org/cmake/help/latest/manual/cmake.1.html#command-line-tool-mode). – Florian Apr 17 '18 at 10:27
  • @Florian: Referenced question describes how to create a directory, but doesn't describe **how to extract directory component from the file**. (Well, I know it is simple with [get_filename_component](https://cmake.org/cmake/help/v3.7/command/get_filename_component.html)). But as tools not creating directory for output file are quite common, it would be usefull to have a question/answer(s) about complete resolution for that problem. (Another approach could be adding corresponded answer for the related question, but without changing that question itself it seems to be not efficient). – Tsyvarev Apr 17 '18 at 10:56
  • @Cyril: I have slightly rearrange your question post for separate your **main problem** (creating a directory for the file) from your attempt for overcome that (creating the file itself). Feel free to more improve the question, so future readers will find it useful. – Tsyvarev Apr 17 '18 at 17:54

1 Answers1

2

In CMake you may extract directory component of the file and create this directory.

Below code can be used for a tool, which generates a file but doesn't create directory for it.

# Assume some tool generates file '${file}', but don't create directory for it.
#
# Extract directory component for a file
get_filename_component(_dir ${file} DIRECTORY)
add_custom_command(OUTPUT ${file}
    COMMAND ${CMAKE_COMMAND} -E make_directory ${_dir} # Create output directory
    COMMAND <tool-which-generates-file> ... ${file}
)

See also that question about ways for create a directory in CMake.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153