Last active
March 8, 2026 03:37
-
-
Save scivision/2581385d2a5187426020dc17f111d9a2 to your computer and use it in GitHub Desktop.
CMake generate new temporary directory or temporary filename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main(void){ | |
| char name[L_tmpnam]; | |
| if(tmpnam(name) == NULL){ | |
| printf("Temporary file name: %s\n", name); | |
| return 0; | |
| } | |
| return 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdio> | |
| #include <iostream> | |
| int main(){ | |
| char name[L_tmpnam]; | |
| if(std::tmpnam(name)){ | |
| std::cout << "Temporary file name: " << name << std::endl; | |
| return 0; | |
| } | |
| return 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function(get_temp_dir ovar) | |
| find_program(mktemp NAMES mktemp) | |
| if(mktemp) | |
| execute_process(COMMAND mktemp -d OUTPUT_VARIABLE out OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE ret) | |
| if(ret EQUAL 0) | |
| set(${ovar} ${out} PARENT_SCOPE) | |
| return() | |
| endif() | |
| endif() | |
| find_program(pwsh NAMES pwsh) | |
| if(pwsh) | |
| execute_process(COMMAND pwsh -c "[System.IO.Path]::GetTempPath()" OUTPUT_VARIABLE out OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE ret) | |
| if(ret EQUAL 0) | |
| string(RANDOM LENGTH 12 _s) | |
| set(out ${out}${_s}) | |
| file(MAKE_DIRECTORY ${out}) | |
| set(${ovar} ${out} PARENT_SCOPE) | |
| return() | |
| endif() | |
| endif() | |
| message(FATAL_ERROR "Could not find mktemp or pwsh to make temporary directory") | |
| endfunction(get_temp_dir) | |
| # demo | |
| get_temp_dir(TEMPDIR) | |
| message(STATUS "temporary directory: ${TEMPDIR}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function(get_temp_file ovar) | |
| find_program(mktemp NAMES mktemp) | |
| if(mktemp) | |
| execute_process(COMMAND mktemp OUTPUT_VARIABLE out OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE ret) | |
| if(ret EQUAL 0) | |
| set(${ovar} ${out} PARENT_SCOPE) | |
| return() | |
| endif() | |
| endif() | |
| find_program(pwsh NAMES pwsh) | |
| if(pwsh) | |
| execute_process(COMMAND pwsh -c "New-TemporaryFile | Select -ExpandProperty FullName" OUTPUT_VARIABLE out OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE ret) | |
| if(ret EQUAL 0) | |
| set(${ovar} ${out} PARENT_SCOPE) | |
| return() | |
| endif() | |
| endif() | |
| message(FATAL_ERROR "Could not find mktemp or pwsh to make temporary file") | |
| endfunction(get_temp_file) | |
| # demo | |
| get_temp_file(TEMPFILE) | |
| message(STATUS "temporary file: ${TEMPFILE}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment