|
1 | 1 | cmake_minimum_required(VERSION 3.1.0)
|
2 | 2 | project(MSTelemetry)
|
3 | 3 |
|
| 4 | +# Set installation prefix for macOS and Linux |
| 5 | +if(UNIX AND NOT DEFINED CMAKE_INSTALL_PREFIX) |
| 6 | + set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Installation directory prefix" FORCE) |
| 7 | +endif() |
| 8 | + |
| 9 | + |
| 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() |
| 45 | +endif() |
| 46 | + |
| 47 | + |
| 48 | + |
4 | 49 | set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
|
5 | 50 | set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
|
6 | 51 | set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
|
|
0 commit comments