What is the naming convention for CMake scripts?
I know that CMake uses the standard name "CMakeLists.txt" and a function add_subdirectory
to call scripts directly during the build process.
I have CMake code that I am using to turn files into C ++ strings, which can then be baked into the program using directives #include
. The relevant code in my root CMakeLists file looks like this (oversimplified, of course):
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/path/to/example.json.txt
COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${STRING_PATH} -P BuildString.cmake
DEPENDS ${CMAKE_SOURCE_DIR}/path/to/example.json
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
add_custom_target(strings DEPENDS ${CMAKE_BINARY_DIR}/path/to/example.json.txt)
(In the actual code, I call add_custom_command
for each file I need to convert to a string, then pass all the output filenames as a list add_custom_target
.)
And here's BuildString.cmake:
set(DELIMITER "")
set(SOURCE ${CMAKE_ARGV1})
set(BUILD ${CMAKE_ARGV2})
set(PATH ${CMAKE_ARGV3})
file(READ ${SOURCE}/${PATH} STRING)
# add semicolons back into string
string(REGEX REPLACE ";" "\\\\;" STRING "${STRING}")
file(WRITE ${BUILD}/${PATH}.txt R\"${DELIMITER}\(${STRING}\)${DELIMITER}\")
As you can see, BuildString.cmake simply takes an input file and outputs the contents of that file (wrapped in a C ++ string literal expression using ${DELIMITER}
a delimiter) into the build directory.
What should I call BuildString.cmake? Is it a convention to use all lowercase letters with underscores (build_string.cmake), or perhaps camel lowercase (buildString.cmake)? Or should the name be a noun instead of a verb (StringBuilder.cmake)?
(As a side note, if you can see an unrelated way, I could improve on any of this code, that would be appreciated as well.)
source to share