|
| 1 | +# Copyright 2024 - 2025 Khalil Estell and the libhal contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +cmake_minimum_required(VERSION 3.25) |
| 16 | + |
| 17 | +project(benchmark-error-handling LANGUAGES CXX) |
| 18 | + |
| 19 | +# Always check that the $ENV{LIBHAL_PLATFORM_LIBRARY} & $ENV{LIBHAL_PLATFORM} |
| 20 | +# environment variables are set by the profile. |
| 21 | +if("$ENV{LIBHAL_PLATFORM_LIBRARY}" STREQUAL "") |
| 22 | + message(FATAL_ERROR |
| 23 | + "Build environment variable LIBHAL_PLATFORM_LIBRARY is required for " "this project.") |
| 24 | +endif() |
| 25 | +if("$ENV{LIBHAL_PLATFORM}" STREQUAL "") |
| 26 | + message(FATAL_ERROR |
| 27 | + "Build environment variable LIBHAL_PLATFORM is required for " |
| 28 | + "this project.") |
| 29 | +endif() |
| 30 | + |
| 31 | +find_package(libhal-$ENV{LIBHAL_PLATFORM_LIBRARY} REQUIRED) |
| 32 | +find_package(prebuilt-picolibc QUIET) |
| 33 | + |
| 34 | +if(${CMAKE_CROSSCOMPILING}) |
| 35 | + find_package(${PACKAGE} REQUIRED) |
| 36 | +endif() |
| 37 | + |
| 38 | +set(startup_source_files main.cpp filler.cpp) |
| 39 | + |
| 40 | +# Makes the platform/<platform_name>.cpp file optional |
| 41 | +if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/platforms/$ENV{LIBHAL_PLATFORM}.cpp") |
| 42 | + list(APPEND startup_source_files platforms/$ENV{LIBHAL_PLATFORM}.cpp) |
| 43 | +endif() |
| 44 | + |
| 45 | +add_library(startup_code ${startup_source_files}) |
| 46 | + |
| 47 | +target_include_directories(startup_code PUBLIC .) |
| 48 | +target_compile_options(startup_code PRIVATE |
| 49 | + -g |
| 50 | + -Werror |
| 51 | + -Wall |
| 52 | + -Wextra |
| 53 | + -Wshadow |
| 54 | + -fno-threadsafe-statics |
| 55 | + $<$<COMPILE_LANGUAGE:CXX>:-fexceptions -fno-rtti> |
| 56 | +) |
| 57 | + |
| 58 | +target_link_libraries(startup_code PRIVATE |
| 59 | + libhal::$ENV{LIBHAL_PLATFORM_LIBRARY} |
| 60 | +) |
| 61 | + |
| 62 | +if(prebuilt-picolibc_FOUND) |
| 63 | + target_link_libraries(startup_code PRIVATE picolibc) |
| 64 | + message(STATUS "Found picolibc, linking it in!") |
| 65 | +endif() |
| 66 | + |
| 67 | +# Glob all the files in the directory 'generated_tests' |
| 68 | +file(GLOB GENERATED_TEST "${CMAKE_CURRENT_SOURCE_DIR}/generated_tests/*.cpp") |
| 69 | + |
| 70 | +foreach(test_path ${GENERATED_TEST}) |
| 71 | + get_filename_component(test_file_name ${test_path} NAME) |
| 72 | + set(elf ${test_file_name}.elf) |
| 73 | + message(STATUS "Generating Demo for \"${elf}\"") |
| 74 | + add_executable(${elf} ${test_path}) |
| 75 | + |
| 76 | + target_compile_options(${elf} PRIVATE |
| 77 | + -g |
| 78 | + -Werror |
| 79 | + -Wall |
| 80 | + -Wextra |
| 81 | + -Wshadow |
| 82 | + -fno-threadsafe-statics |
| 83 | + $<$<COMPILE_LANGUAGE:CXX>:-fexceptions -fno-rtti> |
| 84 | + ) |
| 85 | + |
| 86 | + target_link_libraries(${elf} PRIVATE |
| 87 | + startup_code |
| 88 | + libhal::$ENV{LIBHAL_PLATFORM_LIBRARY} |
| 89 | + ) |
| 90 | + |
| 91 | + target_link_options(${elf} PRIVATE -fno-threadsafe-statics) |
| 92 | + |
| 93 | + if(prebuilt-picolibc_FOUND) |
| 94 | + target_link_libraries(${elf} PRIVATE picolibc) |
| 95 | + endif() |
| 96 | + |
| 97 | + if(${CMAKE_CROSSCOMPILING}) |
| 98 | + # Convert elf into .bin, .hex and other formats needed for programming |
| 99 | + # devices. |
| 100 | + libhal_post_build(${elf}) |
| 101 | + libhal_disassemble(${elf}) |
| 102 | + endif() |
| 103 | +endforeach() |
0 commit comments