Skip to content

Commit 0c7ff51

Browse files
committed
more cleaner check
1 parent 7dac26f commit 0c7ff51

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

CMakeLists.txt

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,41 @@ if(UNIX AND NOT DEFINED CMAKE_INSTALL_PREFIX)
77
endif()
88

99

10-
if(UNIX)
11-
set(TEST_FILE "${CMAKE_INSTALL_PREFIX}/.cmake_write_test")
12-
13-
# Attempt to write a test file
14-
file(WRITE "${TEST_FILE}" "" OUTPUT_VARIABLE WRITE_RESULT ERROR_VARIABLE WRITE_ERROR)
15-
16-
if(EXISTS "${TEST_FILE}")
17-
message(STATUS "Write access to ${CMAKE_INSTALL_PREFIX} confirmed.")
18-
file(REMOVE "${TEST_FILE}") # Clean up the test file
19-
else()
20-
# Fallback to a writable directory (e.g., user's home)
21-
set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/mst_telemetry" CACHE PATH "Fallback installation directory prefix" FORCE)
22-
message(STATUS "No write access to ${CMAKE_INSTALL_PREFIX}, using $ENV{HOME}/mst_telemetry instead.")
23-
endif()
10+
# Set installation prefix for Unix systems (macOS and Linux)
11+
if(UNIX AND NOT DEFINED CMAKE_INSTALL_PREFIX)
12+
# First try /usr/local
13+
set(TEST_FILE "/usr/local/.ci_write_test")
14+
15+
# Try to create a test file
16+
execute_process(
17+
COMMAND ${CMAKE_COMMAND} -E touch "${TEST_FILE}"
18+
RESULT_VARIABLE WRITE_RESULT
19+
)
20+
21+
# Check if write was successful
22+
if(WRITE_RESULT EQUAL 0)
23+
# We have write access to /usr/local
24+
set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Installation directory prefix" FORCE)
25+
message(STATUS "Using /usr/local as installation prefix (write access confirmed)")
26+
# Clean up test file
27+
execute_process(COMMAND ${CMAKE_COMMAND} -E remove "${TEST_FILE}")
28+
else()
29+
# No write access, fall back to HOME directory
30+
set(FALLBACK_DIR "$ENV{HOME}/mst_telemetry")
31+
32+
# Create the mst_telemetry directory
33+
execute_process(
34+
COMMAND ${CMAKE_COMMAND} -E make_directory "${FALLBACK_DIR}"
35+
RESULT_VARIABLE CREATE_DIR_RESULT
36+
)
37+
38+
if(NOT CREATE_DIR_RESULT EQUAL 0)
39+
message(FATAL_ERROR "Failed to create directory: ${FALLBACK_DIR}")
40+
endif()
41+
42+
set(CMAKE_INSTALL_PREFIX "${FALLBACK_DIR}" CACHE PATH "Installation directory prefix" FORCE)
43+
message(STATUS "No write access to /usr/local, created and using ${FALLBACK_DIR} instead")
44+
endif()
2445
endif()
2546

2647

0 commit comments

Comments
 (0)