我需要在linux中使用CMake模拟mktemp命令。什么宏提供了这个?
答案 0 :(得分:3)
没有“mktemp”的直接CMake模拟。
从CMake脚本或CMakeLists.txt文件中,您最好的选择是使用
file(MAKE_DIRECTORY "/path/to/dir/name")
命令,并为其指定您知道自己具有写入权限的目录。有关文件命令的帮助,请访问:http://cmake.org/cmake/help/cmake-2-8-docs.html#command:file
你也可以简单地使用
$ENV{TMP}
如果有环境变量指向系统提供的临时目录。
如果您直接调用CMake,也可以使用
cmake -E make_directory /path/to/dir/name
最后,另请参阅execute_process命令,该命令允许您从cmake脚本或CMakeLists文件中调用任意命令行工具并捕获输出。如果您有另一个可以调用的工具可以为您提供mktemp功能,那么这可能会很有用。 http://cmake.org/cmake/help/cmake-2-8-docs.html#command:execute_process
答案 1 :(得分:3)
正在寻找这个来评估CMake Wiki中建议的表达式。写了一些宏和一个生成临时文件名并执行它们的例子:
#!/usr/bin/cmake -P
macro(temp_name fname)
if(${ARGC} GREATER 1) # Have to escape ARGC to correctly compare
set(_base ${ARGV1})
else(${ARGC} GREATER 1)
set(_base ".cmake-tmp")
endif(${ARGC} GREATER 1)
set(_counter 0)
while(EXISTS "${_base}${_counter}")
math(EXPR _counter "${_counter} + 1")
endwhile(EXISTS "${_base}${_counter}")
set(${fname} "${_base}${_counter}")
endmacro(temp_name)
# Evaluate expression
# Suggestion from the Wiki: http://cmake.org/Wiki/CMake/Language_Syntax
# Unfortunately, no built-in stuff for this: http://public.kitware.com/Bug/view.php?id=4034
macro(eval expr)
temp_name(_fname)
file(WRITE ${_fname} "${expr}")
include(${_fname})
file(REMOVE ${_fname})
endmacro(eval)
# Examples
eval("message(\"Hai\")")
set(funcs a;b)
macro(test_a arg)
message("A: ${arg}")
endmacro(test_a)
macro(test_b arg)
message("B: ${arg}")
endmacro(test_b)
foreach(func ${funcs})
set(func_name test_${func})
eval("${func_name}(\"Test\")")
endforeach(func)
输出:
Hai
A: Test
B: Test
请注意,在Linux中,您可以将此脚本设置为可执行文件并使用cmake -P
运行它。用于测试内容。
答案 2 :(得分:0)
我实现了以下宏:
#!/usr/bin/cmake -P
include(CMakeParseArguments)
function(MKTEMP)
set(options CREATE_FOLDER CREATE_FILE)
set(oneValueArgs PREFIX PARENT OUTPUT_VARIABLE)
cmake_parse_arguments(MKTEMP "${options}" "${oneValueArgs}" "" ${ARGN})
if(NOT DEFINED MKTEMP_CREATE_FOLDER)
set(MKTEMP_CREATE_FOLDER FALSE)
endif()
if(NOT DEFINED MKTEMP_CREATE_FILE)
set(MKTEMP_CREATE_FILE FALSE)
endif()
if(MKTEMP_CREATE_FOLDER AND MKTEMP_CREATE_FILE)
# Can not create folder and file with the same name
message(FATAL_ERROR "Both flags CREATE_FOLDER and CREATE_FILE are set")
endif()
if(NOT DEFINED MKTEMP_PREFIX)
set(MKTEMP_PREFIX "tmp")
endif()
if(NOT DEFINED MKTEMP_PARENT)
set(MKTEMP_PARENT "$ENV{TMP}")
endif()
set(_COUNTER 0)
while(EXISTS "${MKTEMP_PARENT}/${MKTEMP_PREFIX}${_COUNTER}")
math(EXPR _COUNTER "${_COUNTER} + 1")
endwhile()
set(_NAME "${MKTEMP_PARENT}/${MKTEMP_PREFIX}${_COUNTER}")
set(${MKTEMP_OUTPUT_VARIABLE} "${_NAME}" PARENT_SCOPE)
if(MKTEMP_CREATE_FOLDER)
file(MAKE_DIRECTORY "${_NAME}")
elseif(MKTEMP_CREATE_FILE)
file(WRITE "${_NAME}" "")
endif()
endfunction()
用法:
# only generate name - with default prefix ("tmp")
MKTEMP(OUTPUT_VARIABLE TMPONLYNAME)
message("TMPONLYNAME is ${TMPONLYNAME}")
# only generate name - with custom prefix ("myapp")
MKTEMP(PREFIX "myapp" OUTPUT_VARIABLE TMPONLYNAME)
message("TMPONLYNAME is ${TMPONLYNAME}")
# only generate name - use current folder as temp
MKTEMP(PARENT "." OUTPUT_VARIABLE TMPONLYNAME)
message("TMPONLYNAME is ${TMPONLYNAME}")
# create file
MKTEMP(PREFIX "myapp" OUTPUT_VARIABLE TMPFILE CREATE_FILE)
message("TMPFILE is ${TMPFILE}")
# ... work with file ...
file(REMOVE "${TMPFILE}")
# create folder
MKTEMP(PREFIX "myapp" OUTPUT_VARIABLE TMPFOLDER CREATE_FOLDER)
message("TMPFOLDER is ${TMPFOLDER}")
# ... work with folder ...
file(REMOVE_RECURSE "${TMPFOLDER}")
我的Windows环境中的输出示例(“myapp7”因删除而相同):
TMPONLYNAME is C:\Users\msuslov\AppData\Local\Temp\tmp1
TMPONLYNAME is C:\Users\msuslov\AppData\Local\Temp\myapp7
TMPONLYNAME is .\tmp0
TMPFILE is C:\Users\msuslov\AppData\Local\Temp\myapp7
TMPFOLDER is C:\Users\msuslov\AppData\Local\Temp\myapp7