From 6ac93f0a5bb7c8a389fea3834be7feb3239551eb Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Sat, 8 Aug 2026 09:49:25 +0200 Subject: [PATCH] make compilation better --- CMakeLists.txt | 77 ++++++++++++---------------------- Makefile | 4 +- cmake/platform_Linux.cmake | 23 ++++++++++ cmake/platform_Linux_pre.cmake | 1 + cmake/platform_Pico.cmake | 31 ++++++++++++++ cmake/platform_Pico_pre.cmake | 4 ++ src/hal/hal_pico.cpp | 2 + 7 files changed, 90 insertions(+), 52 deletions(-) create mode 100644 cmake/platform_Linux.cmake create mode 100644 cmake/platform_Linux_pre.cmake create mode 100644 cmake/platform_Pico.cmake create mode 100644 cmake/platform_Pico_pre.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c230d1..5f0ba81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,13 @@ cmake_minimum_required(VERSION 3.13) -option(BUILD_FOR_PICO "Build for Raspberry Pi Pico" ON) +set(BUILD_PLATFORM "Linux" CACHE STRING "Target platform: Pico or Linux") +set_property(CACHE BUILD_PLATFORM PROPERTY STRINGS Pico Linux) + +if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform_${BUILD_PLATFORM}_pre.cmake") + message(FATAL_ERROR "Unknown BUILD_PLATFORM '${BUILD_PLATFORM}' (no cmake/platform_${BUILD_PLATFORM}_pre.cmake found)") +endif() + +string(TOLOWER ${BUILD_PLATFORM} BUILD_PLATFORM_LOWER) set(GEOGRAPHICLIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/geographiclib) @@ -10,55 +17,25 @@ set(BUILD_DOCUMENTATION OFF CACHE BOOL "" FORCE) set(BUILD_BOTH_LIBRARIES OFF CACHE BOOL "" FORCE) set(PACKAGE_DEBIAN OFF CACHE BOOL "" FORCE) -# Automatically collect core sources and platform-specific HAL files +# Pre-project platform setup (e.g. Pico's pico_sdk_import.cmake must run +# before project(), and it must set PROJECT_LANGUAGES). +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform_${BUILD_PLATFORM}_pre.cmake) + +project(p-rad ${PROJECT_LANGUAGES}) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# NOTE: don't use add_link_options/add_compile_options here - they apply to +# every target including pico-sdk's bs2_default, whose binary gets fully +# garbage-collected. Scope them to the p-rad target instead (see platform files). + +# Automatically collect core sources and platform-specific HAL files. +# HAL files are matched by suffix convention: src/hal/*_.cpp file(GLOB CORE_SOURCES CONFIGURE_DEPENDS "src/core/*.cpp" "src/core/*.c") -file(GLOB HAL_PICO_SOURCES CONFIGURE_DEPENDS "src/hal/*_pico.cpp") -file(GLOB HAL_LINUX_SOURCES CONFIGURE_DEPENDS "src/hal/*_linux.cpp") +file(GLOB HAL_SOURCES CONFIGURE_DEPENDS "src/hal/*_${BUILD_PLATFORM_LOWER}.cpp") -if(BUILD_FOR_PICO) - set(PICO_BOARD pico_w CACHE STRING "Board type") - include(pico_sdk_import.cmake) - project(p-rad C CXX ASM) +add_subdirectory(${GEOGRAPHICLIB_DIR} EXCLUDE_FROM_ALL) - set(CMAKE_CXX_STANDARD 23) - set(CMAKE_CXX_STANDARD_REQUIRED ON) - - pico_sdk_init() - - add_subdirectory(${GEOGRAPHICLIB_DIR} EXCLUDE_FROM_ALL) - target_compile_options(GeographicLib INTERFACE -fexceptions -frtti) - - add_executable(p-rad - src/main.cpp - ${CORE_SOURCES} - ${HAL_PICO_SOURCES} - ) - - target_compile_definitions(p-rad PRIVATE PICO_BUILD) - target_include_directories(p-rad PRIVATE src) - target_link_libraries(p-rad pico_stdlib pico_cyw43_arch_lwip_threadsafe_background hardware_rtc GeographicLib) - - pico_enable_stdio_usb(p-rad 1) - pico_enable_stdio_uart(p-rad 0) - - pico_add_extra_outputs(p-rad) -else() - project(p-rad CXX) - - set(CMAKE_CXX_STANDARD 23) - set(CMAKE_CXX_STANDARD_REQUIRED ON) - - add_subdirectory(${GEOGRAPHICLIB_DIR} EXCLUDE_FROM_ALL) - - add_executable(p-rad - src/main.cpp - ${CORE_SOURCES} - ${HAL_LINUX_SOURCES} - ) - - target_include_directories(p-rad PRIVATE src) - find_package(Threads REQUIRED) - find_package(SDL3 REQUIRED CONFIG) - find_package(CURL REQUIRED) - target_link_libraries(p-rad Threads::Threads SDL3::SDL3 CURL::libcurl GeographicLib) -endif() \ No newline at end of file +# Post-project platform setup: add_executable, link libraries, platform-specific calls. +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform_${BUILD_PLATFORM}.cmake) diff --git a/Makefile b/Makefile index cde754f..2e26b2d 100644 --- a/Makefile +++ b/Makefile @@ -12,11 +12,11 @@ all: linux pico linux: $(AUTOHEADER) mkdir -p build-linux - (cd build-linux && cmake .. -DBUILD_FOR_PICO=OFF && make -j$(nproc)) + (cd build-linux && cmake .. -DBUILD_PLATFORM=Linux && make -j$(nproc)) pico: $(AUTOHEADER) mkdir -p build-pico - (cd build-pico && cmake .. -DBUILD_FOR_PICO=ON && make -j$(nproc)) + (cd build-pico && cmake .. -DBUILD_PLATFORM=Pico && make -j$(nproc)) # --------------------------------------------------------------------------- # kconfig targets diff --git a/cmake/platform_Linux.cmake b/cmake/platform_Linux.cmake new file mode 100644 index 0000000..2ada38c --- /dev/null +++ b/cmake/platform_Linux.cmake @@ -0,0 +1,23 @@ +add_executable(p-rad + src/main.cpp + ${CORE_SOURCES} + ${HAL_SOURCES} +) +target_compile_options(p-rad PRIVATE -ffunction-sections -fdata-sections) +target_link_options(p-rad PRIVATE -Wl,--gc-sections) +if(CMAKE_BUILD_TYPE EQUAL Release) +add_compile_options(-O3) +endif() + +target_include_directories(p-rad PRIVATE src) + +find_package(Threads REQUIRED) +find_package(SDL3 REQUIRED CONFIG) +find_package(CURL REQUIRED) + +target_link_libraries(p-rad + Threads::Threads + SDL3::SDL3 + CURL::libcurl + GeographicLib +) diff --git a/cmake/platform_Linux_pre.cmake b/cmake/platform_Linux_pre.cmake new file mode 100644 index 0000000..de81202 --- /dev/null +++ b/cmake/platform_Linux_pre.cmake @@ -0,0 +1 @@ +set(PROJECT_LANGUAGES CXX) diff --git a/cmake/platform_Pico.cmake b/cmake/platform_Pico.cmake new file mode 100644 index 0000000..956c572 --- /dev/null +++ b/cmake/platform_Pico.cmake @@ -0,0 +1,31 @@ +pico_sdk_init() + +target_compile_options(GeographicLib INTERFACE -fexceptions -frtti) + +add_executable(p-rad + src/main.cpp + ${CORE_SOURCES} + ${HAL_SOURCES} +) + +target_compile_options(p-rad PRIVATE -ffunction-sections -fdata-sections) +target_link_options(p-rad PRIVATE -Wl,--gc-sections) + +if(CMAKE_BUILD_TYPE EQUAL Release) + add_compile_options(-Os) +endif() + + +target_compile_definitions(p-rad PRIVATE PICO_BUILD) +target_include_directories(p-rad PRIVATE src) +target_link_libraries(p-rad + pico_stdlib + pico_cyw43_arch_lwip_threadsafe_background + hardware_rtc + GeographicLib +) + +pico_enable_stdio_usb(p-rad 1) +pico_enable_stdio_uart(p-rad 0) + +pico_add_extra_outputs(p-rad) diff --git a/cmake/platform_Pico_pre.cmake b/cmake/platform_Pico_pre.cmake new file mode 100644 index 0000000..5e7c597 --- /dev/null +++ b/cmake/platform_Pico_pre.cmake @@ -0,0 +1,4 @@ +set(PROJECT_LANGUAGES C CXX ASM) + +set(PICO_BOARD pico_w CACHE STRING "Board type") +include(${CMAKE_SOURCE_DIR}/pico_sdk_import.cmake) diff --git a/src/hal/hal_pico.cpp b/src/hal/hal_pico.cpp index 27b85b4..3e6ee06 100644 --- a/src/hal/hal_pico.cpp +++ b/src/hal/hal_pico.cpp @@ -54,6 +54,8 @@ void circle(const uint16_t x_center, const uint16_t y_center,const uint16_t radi } void rhombus(const uint16_t x_center, const uint16_t y_center, const uint16_t radius, const color_t color) {} void line(const uint16_t x1, const uint16_t y1, const uint16_t x2, const uint16_t y2, const color_t color) {} +void arc(uint16_t start_x, uint16_t start_y, float start_angle, float speed /*in pps*/,float angular_speed, uint16_t duration, color_t color) {} + } // namespace draw namespace net {