-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathcurrent_user.cmake
More file actions
106 lines (97 loc) · 2.97 KB
/
current_user.cmake
File metadata and controls
106 lines (97 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# wolfboot/cmake/current_user.cmake
#
# Copyright (C) 2025 wolfSSL Inc.
#
# This file is part of wolfBoot.
#
# wolfBoot is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# wolfBoot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
#
# get_current_user(<OUT_VAR>)
# Sets <OUT_VAR> to the best guess of the current user across Windows, Linux, macOS, and WSL.
# Example usage
# get_current_user(CURRENT_USER)
# message(STATUS "Current user detected: ${CURRENT_USER}")
# Ensure this file is only included and initialized once
if(CMAKE_VERSION VERSION_LESS 3.10)
# Fallback path for older CMake, and anything else that wants to detect is loaded
if(DEFINED CURRENT_USER_CMAKE_INCLUDED)
return()
endif()
else()
include_guard(GLOBAL)
endif()
function(get_current_user OUT_VAR)
set(_user "")
# Fast path from environment
foreach(var USER USERNAME LOGNAME)
if(DEFINED ENV{${var}} AND NOT "$ENV{${var}}" STREQUAL "")
set(_user "$ENV{${var}}")
break()
endif()
endforeach()
# Windows specific fallbacks (native Win or WSL)
if(_user STREQUAL "")
if(WIN32 OR DEFINED ENV{WSL_DISTRO_NAME})
# Try PowerShell first
execute_process(
COMMAND powershell -NoProfile -Command "$env:USERNAME"
OUTPUT_VARIABLE _user
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(_user STREQUAL "")
# Fallback to cmd.exe
execute_process(
COMMAND cmd.exe /c echo %USERNAME%
OUTPUT_VARIABLE _user
ERROR_QUIET
)
string(REPLACE "\r" "" _user "${_user}")
string(STRIP "${_user}" _user)
endif()
endif()
endif()
# POSIX fallbacks
if(_user STREQUAL "")
execute_process(
COMMAND id -un
OUTPUT_VARIABLE _user
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()
if(_user STREQUAL "")
execute_process(
COMMAND whoami
OUTPUT_VARIABLE _user
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()
# Last resort: CI hints or placeholder
if(_user STREQUAL "")
foreach(var GITHUB_ACTOR BUILD_USER USERNAME USER LOGNAME)
if(DEFINED ENV{${var}} AND NOT "$ENV{${var}}" STREQUAL "")
set(_user "$ENV{${var}}")
break()
endif()
endforeach()
endif()
if(_user STREQUAL "")
set(_user "unknown")
endif()
set(${OUT_VAR} "${_user}" PARENT_SCOPE)
endfunction()
set(CURRENT_USER_CMAKE_INCLUDED true)