Barry
Barry

Reputation: 302643

Dealing with lots and lots of escaped characters in add_custom_command

I have a file that contains a bunch of data. I want to turn it into a C++ string literal, because I need to compile this data into the binary - I cannot read it from disk.

One way of doing this is to just generate a C++ source file that declares a string literal with a known name. The CMake code to do this is straightforward, if somewhat awful:

function(make_literal_from_file dest_file source_file literal_name)              
    add_custom_command(                                                          
        OUTPUT ${dest_file}                                                      
        COMMAND printf \'char const* ${literal_name} = R\"\#\(\' > ${dest_file}  
        COMMAND cat ${source_file} >> ${dest_file}                               
        COMMAND printf \'\)\#\"\;\' >> ${dest_file}                              
        DEPENDS ${source_file})                                                  
endfunction()

This works and does what I want (printf is necessary to avoid a new line after the raw string introducer). However, the amount of escaping going on here makes it very difficult to see what's going on. Is there a way to write this function such that it's actually readable?


Note that I cannot use a file(READ ...)/configure_file(...) combo here because source_file could be something that is generated by CMake at build time and so may not be present at configuration time.

Upvotes: 3

Views: 498

Answers (1)

Justin
Justin

Reputation: 25277

I would recommend writing a script to do this. You could write it in CMake, but I personally prefer a better language such as Python:

# Untested, just to show roughly how to do it
import sys

dest_file, source_file, literal_name = sys.argv[1:]

with open(dest_file) as dest, open(source_file) as source:
    literal_contents = source.read()
    dest.write(f'char const* {literal_name} = R"({literal_contents})";\n')

Corresponding CMake code:

# String interpolation came in Python 3.6, thus the requirement on 3.6.
# If using CMake < 3.12, use find_package(PythonInterp) instead.
find_package(Python3 3.6 COMPONENTS Interpreter)

# Make sure this resolves correctly. ${CMAKE_CURRENT_LIST_DIR} is helpful;
# it's the directory containing the current file (this cmake file)
set(make_literal_from_file_script "path/to/make_literal_from_file.py")

function(make_literal_from_file dest_file source_file literal_name)              
    add_custom_command(                                                          
        OUTPUT "${dest_file}"
        COMMAND 
            "${Python3_EXECUTABLE}" "${make_literal_from_file_script}"
            "${dest_file}"
            "${source_file}"
            "${literal_name}"
        DEPENDS "${source_file}")                                                  
endfunction()

If you don't want the dependency on Python, you could use C++ (only the CMake code shown):

add_executable(make_literal_from_file_exe
    path/to/cpp/file.cpp
)

function(make_literal_from_file dest_file source_file literal_name)              
    add_custom_command(                                                          
        OUTPUT "${dest_file}"
        COMMAND 
            make_literal_from_file_exe
            "${dest_file}"
            "${source_file}"
            "${literal_name}"
        DEPENDS "${source_file}")
endfunction()

Upvotes: 2

Related Questions