CMake脚本的命名约定是什么? [英] What is the naming convention for CMake scripts?

查看:131
本文介绍了CMake脚本的命名约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道CMake使用标准名称"CMakeLists.txt"和add_subdirectory函数在构建过程中直接调用脚本.

I know that CMake makes use of the standard name "CMakeLists.txt" and the add_subdirectory function for calling scripts directly in the build process.

我有一些CMake代码,可用于将文件转换为C ++字符串,然后可以使用#include指令将其烘焙到程序中.我的CMakeLists根文件中的相关代码如下所示(当然,已大大简化了):

I have some CMake code that I use to turn files into C++ strings that can then be baked into the program using #include directives. The relevant code in my root CMakeLists file looks like this (greatly simplified, 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)

(在实际代码中,我为需要转换为字符串的每个文件调用add_custom_command,然后将所有输出文件名作为列表传递给add_custom_target.)

(In the actual code, I call add_custom_command for each file I need to turn into a string, then pass all the output filenames as a list to add_custom_target.)

这是BuildString.cmake:

And here is 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}\")

如您所见,BuildString.cmake只是获取一个输入文件,并将该文件的内容(使用${DELIMITER}作为分隔符的C ++原始字符串文字符号包装)输出到构建目录中.

As you can see, BuildString.cmake just takes an input file and outputs the contents of that file (wrapped in C++ raw string literal notation using ${DELIMITER} as the delimiter) into the build directory.

我应该怎么称呼BuildString.cmake?是约定将所有小写字母都使用下划线(build_string.cmake),还是使用小写的驼峰字母(buildString.cmake)?还是名称应该是名词而不是动词(StringBuilder.cmake)?

What should I call BuildString.cmake? Is the convention to use all lowercase with underscores (build_string.cmake), or maybe lower camel case (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 any of this code, that would also be appreciated.)

推荐答案

CMake模块有一个约定:在CamelCase FunctionOrMacro.cmake文件中实现了蛇形保护套function_or_macro().因此,如有疑问,请使用CamelCase.

There's a convention for CMake modules: snake-case function_or_macro() is implemented in CamelCase FunctionOrMacro.cmake file. So when in doubt, use CamelCase.

并且使用动词,名词用于类.

And use verbs, nouns are for classes.

这篇关于CMake脚本的命名约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆