2011-06-16 48 views

回答

2

沒有直接的CMake模擬「mktemp」。

從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

+0

感謝這一點。但我怎麼能生成名稱,所以我會確保沒有同名的目錄存在? – krvladislav 2011-06-20 14:01:43

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運行它。用於測試東西。

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