-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.cmake
More file actions
107 lines (96 loc) · 4.56 KB
/
Copy pathpaths.cmake
File metadata and controls
107 lines (96 loc) · 4.56 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
107
# =============================================================================
# paths.cmake — path normalization and build dirs
# =============================================================================
## @brief Normalize a path for Windows workspaces.
## @param[out] out_var Name of the variable to set in the parent scope
## with the normalized path.
## @param[in] input_path Path to normalize. On WIN32 this will convert
## forward slashes to backslashes and remove surrounding
## quotes.
## @note On WIN32 this strips surrounding double quotes (if present)
## and replaces '/' with '\\' to produce a Windows-style path.
## On non-WIN32 platforms the input is returned unchanged.
## Use for values passed to cmd.exe / bat. For paths consumed by
## CMake itself, prefer normalize_cmake_path().
function(windows_path _out_path _input_path)
buildmaster_message(CORE LOWLEVEL "Entering windows_path")
if(NOT ARGC EQUAL 2)
buildmaster_message(CORE FATAL "windows_path requires output variable name and input path")
endif()
if(WIN32)
set(_p "${_input_path}")
string(REGEX REPLACE "^\"(.*)\"$" "\\1" _p "${_p}")
string(REPLACE "/" "\\" _out "${_p}")
set(${_out_path} "${_out}" PARENT_SCOPE)
buildmaster_message(CORE DEBUG "windows_path → ${_out}")
else()
set(${_out_path} "${_input_path}" PARENT_SCOPE)
endif()
buildmaster_message(CORE LOWLEVEL "Exiting windows_path")
endfunction()
## @brief Normalize a filesystem path for use inside CMake (forward slashes).
## @param[out] _out Name of the variable to set in the parent scope.
## @param[in] _input Path (may contain backslashes or surrounding quotes).
## @note Strips optional surrounding quotes, then applies file(TO_CMAKE_PATH).
## Use for ENV-derived paths (BUILDMASTER_DOWNLOADSDIR, cache dirs, etc.)
## so they are safe in toolchain.cmake and CMake string expansion.
function(normalize_cmake_path _out _input)
buildmaster_message(CORE LOWLEVEL "Entering normalize_cmake_path")
if(NOT ARGC EQUAL 2)
buildmaster_message(CORE FATAL "normalize_cmake_path requires output variable name and input path")
endif()
set(_p "${_input}")
string(REGEX REPLACE "^\"(.*)\"$" "\\1" _p "${_p}")
file(TO_CMAKE_PATH "${_p}" _p)
set(${_out} "${_p}" PARENT_SCOPE)
buildmaster_message(CORE LOWLEVEL "Exiting normalize_cmake_path")
endfunction()
## @brief Produce a filesystem-safe string from an arbitrary input.
## @param[out] _out Name of the variable to set in the parent scope
## with the sanitized string.
## @param[in] _input Input string to sanitize.
## @note Replaces any character not in [A-Za-z0-9._-] with '_',
## collapses repeated underscores to a single '_' and trims
## leading/trailing underscores.
function(sanitize_for_filename _out _input)
buildmaster_message(CORE LOWLEVEL "Entering sanitize_for_filename")
if(NOT ARGC EQUAL 2)
buildmaster_message(CORE FATAL "sanitize_for_filename requires output variable name and input string")
endif()
string(REGEX REPLACE "[^A-Za-z0-9._-]" "_" _output "${_input}")
string(REGEX REPLACE "_+" "_" _output "${_output}")
string(REGEX REPLACE "^_+|_+$" "" _output "${_output}")
set(${_out} "${_output}" PARENT_SCOPE)
buildmaster_message(CORE LOWLEVEL "Exiting sanitize_for_filename")
endfunction()
## @brief Ensure a per-component build directory exists and return its path.
## @param[out] _out Name of the variable to set in the parent scope with
## the created directory path.
## @param[in] _component Optional component name; when provided the
## directory will be `${CMAKE_CURRENT_BINARY_DIR}/build/<sanitized>/`
## where `<sanitized>` is produced by `sanitize_for_filename`.
## When omitted the directory is `${CMAKE_CURRENT_BINARY_DIR}/build`.
## @note Creates the directory with `file(MAKE_DIRECTORY ...)` if it
## does not already exist.
function(ensure_build_dir _out)
buildmaster_message(CORE LOWLEVEL "Entering ensure_build_dir")
if(ARGC LESS 1)
buildmaster_message(CORE FATAL "ensure_build_dir requires an output variable name and optional component name")
endif()
set(_out_var "${_out}")
if(ARGC EQUAL 2)
set(_component "${ARGV1}")
else()
set(_component "")
endif()
if("${_component}" STREQUAL "")
set(_sanitized "build")
else()
sanitize_for_filename(_sanitized "${_component}")
set(_sanitized "build/${_sanitized}")
endif()
set(_builddir "${CMAKE_CURRENT_BINARY_DIR}/${_sanitized}")
file(MAKE_DIRECTORY "${_builddir}")
set(${_out_var} "${_builddir}" PARENT_SCOPE)
buildmaster_message(CORE LOWLEVEL "Exiting ensure_build_dir")
endfunction()