|
| 1 | +cmake_minimum_required(VERSION 3.14...3.30) |
| 2 | + |
| 3 | +project(blink |
| 4 | + VERSION 1.0 |
| 5 | + DESCRIPTION "Blink on NuttX" |
| 6 | + LANGUAGES Swift |
| 7 | +) |
| 8 | + |
| 9 | +if("${CMAKE_Swift_COMPILER_VERSION}" VERSION_LESS 6.1) |
| 10 | + message(FATAL_ERROR "Swift 6.1 or later is required") |
| 11 | +endif() |
| 12 | + |
| 13 | +if(POLICY CMP0169) |
| 14 | + # allow to call FetchContent_Populate directly |
| 15 | + cmake_policy(SET CMP0169 OLD) |
| 16 | +endif() |
| 17 | + |
| 18 | +option(LIST_ALL_BOARDS "List all available boards" OFF) |
| 19 | +option(ENABLE_NUTTX_TRACE "Enable NuttX trace" OFF) |
| 20 | + |
| 21 | +if(ENABLE_NUTTX_TRACE) |
| 22 | + set(TRACEFLAG "--trace") |
| 23 | +else() |
| 24 | + set(TRACEFLAG "") |
| 25 | +endif() |
| 26 | + |
| 27 | +set(FETCHCONTENT_QUIET FALSE) |
| 28 | +include(FetchContent) |
| 29 | +FetchContent_Declare( |
| 30 | + apps |
| 31 | + GIT_REPOSITORY https://github.com/apache/nuttx-apps.git |
| 32 | + GIT_TAG nuttx-12.7.0 |
| 33 | + SOURCE_DIR ${CMAKE_BINARY_DIR}/apps |
| 34 | + FIND_PACKAGE_ARGS |
| 35 | +) |
| 36 | +FetchContent_GetProperties(apps) |
| 37 | +if(NOT apps_POPULATED) |
| 38 | + FetchContent_Populate(apps) |
| 39 | +endif() |
| 40 | + |
| 41 | +FetchContent_Declare( |
| 42 | + nuttx |
| 43 | + GIT_REPOSITORY https://github.com/apache/nuttx.git |
| 44 | + GIT_TAG nuttx-12.7.0 |
| 45 | + SOURCE_DIR ${CMAKE_BINARY_DIR}/nuttx |
| 46 | + FIND_PACKAGE_ARGS |
| 47 | +) |
| 48 | +FetchContent_GetProperties(nuttx) |
| 49 | +if(NOT nuttx_POPULATED) |
| 50 | + FetchContent_Populate(nuttx) |
| 51 | +endif() |
| 52 | + |
| 53 | +if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") |
| 54 | + set(SCRIPT_SUFFIX .bat) |
| 55 | +else() |
| 56 | + set(SCRIPT_SUFFIX .sh) |
| 57 | +endif() |
| 58 | + |
| 59 | +if(LIST_ALL_BOARDS) |
| 60 | + execute_process( |
| 61 | + COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR} |
| 62 | + ${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH} |
| 63 | + ${nuttx_SOURCE_DIR}/tools/configure${SCRIPT_SUFFIX} -L |
| 64 | + RESULT_VARIABLE result |
| 65 | + ) |
| 66 | + if(result) |
| 67 | + message(FATAL_ERROR "Failed to run tools/configure") |
| 68 | + endif() |
| 69 | +else() |
| 70 | + if(NOT DEFINED BOARD_CONFIG) |
| 71 | + message(FATAL_ERROR "Please define configuration with BOARD_CONFIG") |
| 72 | + else() |
| 73 | + message(STATUS "BOARD_CONFIG: ${BOARD_CONFIG}") |
| 74 | + endif() |
| 75 | + |
| 76 | + # Send swift-blinky example to nuttx-apps path |
| 77 | + file(COPY ${CMAKE_SOURCE_DIR}/leds_swift DESTINATION ${apps_SOURCE_DIR}/examples) |
| 78 | + file(COPY ${CMAKE_SOURCE_DIR}/defconfig DESTINATION ${nuttx_SOURCE_DIR}/boards/risc-v/qemu-rv/rv-virt/configs/leds_swift) |
| 79 | + |
| 80 | + add_custom_target(distclean |
| 81 | + COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR} |
| 82 | + ${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH} |
| 83 | + make distclean |
| 84 | + COMMENT "Clean NuttX" |
| 85 | + ) |
| 86 | + |
| 87 | + execute_process( |
| 88 | + COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR} |
| 89 | + ${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH} |
| 90 | + ${nuttx_SOURCE_DIR}/tools/configure${SCRIPT_SUFFIX} -l ${BOARD_CONFIG} |
| 91 | + RESULT_VARIABLE result |
| 92 | + ) |
| 93 | + if(result) |
| 94 | + message(FATAL_ERROR "Failed to run tools/configure") |
| 95 | + endif() |
| 96 | + |
| 97 | + add_custom_target(copy_swift_example |
| 98 | + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/leds_swift ${apps_SOURCE_DIR}/examples/leds_swift |
| 99 | + COMMENT "Copying leds_swift example to nuttx-apps" |
| 100 | + ) |
| 101 | + |
| 102 | + add_custom_target(build_nuttx ALL |
| 103 | + COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR} |
| 104 | + ${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH} |
| 105 | + make ${TRACEFLAG} -j ${JOB_POOLS} |
| 106 | + DEPENDS copy_swift_example |
| 107 | + COMMENT "Building NuttX" |
| 108 | + ) |
| 109 | + |
| 110 | + add_custom_command( |
| 111 | + TARGET build_nuttx |
| 112 | + POST_BUILD |
| 113 | + COMMAND ${CMAKE_COMMAND} -E copy ${nuttx_SOURCE_DIR}/nuttx ${CMAKE_BINARY_DIR}/nuttx.elf |
| 114 | + ) |
| 115 | + |
| 116 | + add_custom_target(export_nuttx |
| 117 | + COMMAND ${CMAKE_COMMAND} -E chdir ${nuttx_SOURCE_DIR} |
| 118 | + ${CMAKE_COMMAND} -E env PATH=${nuttx_SOURCE_DIR}/tools:$ENV{PATH} |
| 119 | + make export |
| 120 | + COMMENT "Exporting NuttX" |
| 121 | + ) |
| 122 | + |
| 123 | + add_custom_target(extract_nuttx_export |
| 124 | + COMMAND ${CMAKE_COMMAND} -E tar xzf ${nuttx_SOURCE_DIR}/nuttx-export-12.7.0.tar.gz |
| 125 | + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} |
| 126 | + COMMAND ${CMAKE_COMMAND} -E remove ${nuttx_SOURCE_DIR}/nuttx-export-12.7.0.tar.gz |
| 127 | + DEPENDS export_nuttx |
| 128 | + COMMENT "Extracting NuttX export" |
| 129 | + ) |
| 130 | + |
| 131 | + add_custom_target(nuttx-libs |
| 132 | + DEPENDS build_nuttx export_nuttx extract_nuttx_export |
| 133 | + ) |
| 134 | +endif() |
0 commit comments