|
| 1 | +include(FetchContent) |
| 2 | + |
| 3 | +function(ensure_ctags CTAGS_PATH AUTODL_CTAGS) |
| 4 | + find_program(CTAGS ctags PATHS ${CTAGS_PATH}) |
| 5 | + if(EXISTS ${CTAGS}) |
| 6 | + return() |
| 7 | + endif() |
| 8 | + |
| 9 | + |
| 10 | + cmake_host_system_information( |
| 11 | + RESULT HOSTINFO |
| 12 | + QUERY OS_NAME OS_PLATFORM |
| 13 | + ) |
| 14 | + list(GET HOSTINFO 0 HOST_OS) |
| 15 | + list(GET HOSTINFO 1 HOST_ARCH) |
| 16 | + |
| 17 | + unset(CPUCODE) |
| 18 | + string(TOUPPER ${HOST_ARCH} HOST_ARCH) |
| 19 | + if (${HOST_ARCH} MATCHES "^(AMD64|X86_64|x64)$") |
| 20 | + set(CPUCODE "x86_64") |
| 21 | + elseif (${HOST_ARCH} MATCHES "^(ARM|ARM64)$") |
| 22 | + # not sure there, am I specific enough? |
| 23 | + set(CPUCODE "armv6") |
| 24 | + elseif (${HOST_ARCH} MATCHES "^(I386|IA32|x86|i686)$") |
| 25 | + set(CPUCODE "i686") |
| 26 | + endif() |
| 27 | + |
| 28 | + unset(OSCODE) |
| 29 | + unset(ARCHIVE_EXT) |
| 30 | + if (${HOST_OS} STREQUAL "Linux") |
| 31 | + if(${CPUCODE} STREQUAL "armv6") |
| 32 | + set(OSCODE "linux-gnueabihf") |
| 33 | + # ... I guess? Is there any further check to perform? |
| 34 | + else() |
| 35 | + set(OSCODE "pc-linux-gnu") |
| 36 | + endif() |
| 37 | + set(ARCHIVE_EXT ".tar.bz2") |
| 38 | + elseif (${HOST_OS} STREQUAL "Windows") |
| 39 | + if(${CPUCODE} MATCHES "i686|x86_64") |
| 40 | + # ctags supports only 32-bit for Windows |
| 41 | + set(CPUCODE "i686") |
| 42 | + set(OSCODE "mingw32") |
| 43 | + set(ARCHIVE_EXT ".zip") |
| 44 | + endif() |
| 45 | + elseif (${HOST_OS} STREQUAL "Darwin") |
| 46 | + if(${CPUCODE} STREQUAL "x86_64") |
| 47 | + set(OSCODE "apple-darwin") |
| 48 | + set(ARCHIVE_EXT ".zip") |
| 49 | + endif() |
| 50 | + endif() |
| 51 | + |
| 52 | + if (DEFINED OSCODE AND DEFINED CPUCODE AND DEFINED ARCHIVE_EXT) |
| 53 | + if (${AUTODL_CTAGS}) |
| 54 | + # the SHA512 file is of the form "hash_in_hexa filename" |
| 55 | + file(DOWNLOAD |
| 56 | + "https://github.com/arduino/ctags/releases/download/5.8-arduino11/ctags-5.8-arduino11-${CPUCODE}-${OSCODE}${ARCHIVE_EXT}.sha512" |
| 57 | + ${CMAKE_CURRENT_BINARY_DIR}/ctags_sha512.txt |
| 58 | + ) |
| 59 | + file(READ ${CMAKE_CURRENT_BINARY_DIR}/ctags_sha512.txt CHECKSUM_FULLTEXT) |
| 60 | + string(SUBSTRING "${CHECKSUM_FULLTEXT}" 0 128 CHECKSUM) # keep just the hash; 512 bits make 128 hex caracters |
| 61 | + |
| 62 | + FetchContent_Declare( |
| 63 | + ctags |
| 64 | + SOURCE_DIR ${CTAGS_PATH} |
| 65 | + URL "https://github.com/arduino/ctags/releases/download/5.8-arduino11/ctags-5.8-arduino11-${CPUCODE}-${OSCODE}${ARCHIVE_EXT}" |
| 66 | + URL_HASH SHA512=${CHECKSUM} |
| 67 | + ) |
| 68 | + if (NOT EXISTS ${CTAGS_PATH}) |
| 69 | + message(STATUS "Downloading ctags...") |
| 70 | + FetchContent_MakeAvailable(ctags) |
| 71 | + endif() |
| 72 | + else() # supported platform, no autodl |
| 73 | + message(WARNING |
| 74 | + " |
| 75 | + Could not find Arduino's ctags. This tool is used to convert sketches to proper C++. |
| 76 | + Please install it and make it available in your $PATH, or let me make my private install using the AUTODL_CTAGS option. |
| 77 | + (You can ignore this warning if you understand the implications regarding the structure of your sketch.) |
| 78 | + " |
| 79 | + ) |
| 80 | + endif() |
| 81 | + else() # unsupported platform |
| 82 | + message(WARNING |
| 83 | + " |
| 84 | + Could not find Arduino's ctags. This tool is used to convert sketches to proper C++. |
| 85 | + Please install it and make it available in your $PATH: https://github.com/arduino/ctags/releases/latest |
| 86 | + (You can ignore this warning if you understand the implications regarding the structure of your sketch.) |
| 87 | + " |
| 88 | + ) |
| 89 | + endif() |
| 90 | + |
| 91 | + find_program(CTAGS ctags PATHS ${CTAGS_PATH}) |
| 92 | +endfunction() |
0 commit comments