How to create temp directory in CMake?

I need a CMake analogue of the mktemp command in linux. Which macro provides this?

+3
source share
3 answers

Looked for this too to evaluate the expressions suggested in the CMake Wiki . Wrote some macros and an example to generate temp file names and execute them:

#!/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)

Conclusion:

Hai
A: Test
B: Test

Please note that on Linux you can install this script in an executable file and run it with cmake -P. Useful for testing material.

+3
source

There is no direct analogue of CMake for "mktemp".

From inside a CMake script or CMakeLists.txt file, it is best to use

file(MAKE_DIRECTORY "/path/to/dir/name")

, , . file : http://cmake.org/cmake/help/cmake-2-8-docs.html#command:file

$ENV{TMP}

, temp.

CMake ,

cmake -E make_directory /path/to/dir/name

, . execute_process, cmake script CMakeLists . , , , mktemp. http://cmake.org/cmake/help/cmake-2-8-docs.html#command:execute_process

+2

:

#!/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
0

All Articles