summaryrefslogtreecommitdiff
path: root/CMakeLists.txt
blob: f13226b5218b2889b24fd6be856b971108090507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# CMakeLists.txt - Main per-platform build script for sbc-harness project
#
# Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later

cmake_minimum_required(VERSION 3.30)

if (NOT PICO_PLATFORM)
        message(FATAL_ERROR "PICO_PLATFORM must be specified; use the GNUmakefile to set this")
endif()

set(PICO_SDK_PATH "${CMAKE_SOURCE_DIR}/3rd-party/pico-sdk")
include("${PICO_SDK_PATH}/external/pico_sdk_import.cmake")

project(sbc_harness)

pico_sdk_init()

if ((PICO_PLATFORM STREQUAL "host") AND (NOT PICO_NO_GC_SECTIONS))
	# On non-host builds, this is done by `pico_standard_link`.
	add_compile_options(-ffunction-sections -fdata-sections)
	add_link_options("LINKER:--gc-sections")
endif()

add_compile_options(-Wall -Wextra -Wswitch-enum -Werror)

string(TOUPPER "${CMAKE_BUILD_TYPE}" _upper_cmake_build_type)
string(REPLACE " " ";" _build_type_flags "${CMAKE_C_FLAGS_${_upper_cmake_build_type}}")
if ("-DNDEBUG" IN_LIST _build_type_flags)
	add_compile_options(-Wno-unused-variable -Wno-unused-parameter -Wno-unused-but-set-variable)
	target_compile_definitions(pico_printf INTERFACE PICO_PRINTF_ALWAYS_INCLUDED=1)
endif()

function(_suppress_tinyusb_warnings)
        __suppress_tinyusb_warnings()
	set_source_files_properties(
		${PICO_TINYUSB_PATH}/src/device/usbd.c
		PROPERTIES
		COMPILE_OPTIONS "-Wno-switch-enum")
endfunction()

function(target_embed_sources arg_compile_target arg_link_target arg_hdrname)
	set(embed_objs)
	foreach(embed_src IN LISTS ARGN)
		add_custom_command(
			OUTPUT            "${embed_src}.o"
			WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
			COMMAND           mkdir -p -- "$<PATH:GET_PARENT_PATH,${CMAKE_CURRENT_BINARY_DIR}/${embed_src}>" &&
				${CMAKE_LINKER} -r -b binary -o "${CMAKE_CURRENT_BINARY_DIR}/${embed_src}.o" "${embed_src}" &&
				${CMAKE_OBJCOPY} --rename-section .data=.rodata,alloc,load,readonly,data,contents "${CMAKE_CURRENT_BINARY_DIR}/${embed_src}.o" "${CMAKE_CURRENT_BINARY_DIR}/${embed_src}.o"
			DEPENDS           "${embed_src}"
		)
		list(APPEND embed_objs "${embed_src}.o")
	endforeach()
	set_source_files_properties("${embed_objs}" PROPERTIES
		EXTERNAL_OBJECT true
		GENERATED true
	)

	set_source_files_properties("${arg_hdrname}" PROPERTIES
		GENERATED true
	)
	add_custom_command(
		OUTPUT  "${arg_hdrname}"
		COMMAND "${CMAKE_SOURCE_DIR}/build-aux/embed-sources.h.gen" "${embed_objs}" >"${arg_hdrname}"
		COMMAND_EXPAND_LISTS
		DEPENDS "${embed_objs}" "${CMAKE_SOURCE_DIR}/build-aux/embed-sources.h.gen"
	)

	target_sources("${arg_compile_target}" PRIVATE "${arg_hdrname}")
	target_sources("${arg_link_target}" PRIVATE "${embed_objs}")
endfunction()

function(add_stack_analysis arg_outfile arg_objlib_target)
	target_compile_options("${arg_objlib_target}" PUBLIC "-fcallgraph-info=su,da")
	set_source_files_properties("${arg_outfile}" PROPERTIES
		GENERATED true
	)
	add_custom_command(
		OUTPUT  "${arg_outfile}"
		COMMAND "${CMAKE_SOURCE_DIR}/build-aux/stack.c.gen" "${PICO_PLATFORM}" "${CMAKE_SOURCE_DIR}" "$<TARGET_OBJECTS:${arg_objlib_target}>" >"${arg_outfile}"
		COMMAND_EXPAND_LISTS
		DEPENDS "$<TARGET_OBJECTS:${arg_objlib_target}>" "${CMAKE_SOURCE_DIR}/build-aux/stack.c.gen"
		COMMENT "Calculating ${arg_objlib_target} required stack sizes"
	)
endfunction()

include(CTest)
if (NOT DEFINED ENABLE_TESTS)
	if (PICO_PLATFORM STREQUAL "host")
		set(ENABLE_TESTS 1)
	else()
		set(ENABLE_TESTS 0)
	endif()
endif()

function(add_lib_test arg_libname arg_testname)
	if (ENABLE_TESTS)
		add_executable("${arg_testname}" "tests/${arg_testname}.c")
		target_link_libraries("${arg_testname}" "${arg_libname}")
		add_test(
			NAME    "${arg_libname}/${arg_testname}"
			COMMAND valgrind --error-exitcode=2 "./${arg_testname}"
		)
	endif()
endfunction()

function(_apply_matrix_helper _m_depth _m_assignments)
	list(LENGTH _m_arg_matrix _m_dimensions)
	math(EXPR _m_dimensions ${_m_dimensions}/2)
	if("${_m_depth}" EQUAL "${_m_dimensions}")
		cmake_language(CALL "${_m_arg_action}" "${_m_n}" "${_m_assignments}")
		math(EXPR _m_n "${_m_n}+1")
		set(_m_n "${_m_n}" PARENT_SCOPE)
	else()
		math(EXPR _m_ik "${_m_depth}*2")
		list(GET _m_arg_matrix "${_m_ik}" _m_tmp_key)

		math(EXPR _m_iv "${_m_ik}+1")
		list(GET _m_arg_matrix "${_m_iv}" _m_tmp_vals)
		string(REGEX REPLACE "^\\[(.*)\\]$" "\\1" _m_tmp_vals "${_m_tmp_vals}")

		foreach(_m_tmp_val IN LISTS _m_tmp_vals)
			math(EXPR _m_tmp_depth "${_m_depth}+1")
			set(_m_tmp_assignments "${_m_assignments}")
			list(APPEND _m_tmp_assignments "${_m_tmp_key}=${_m_tmp_val}")
			_apply_matrix_helper("${_m_tmp_depth}" "${_m_tmp_assignments}")
			set(_m_n "${_m_n}" PARENT_SCOPE)
		endforeach()
	endif()
endfunction()
function(apply_matrix _m_arg_action _m_arg_matrix)
	set(_m_n 0)
	_apply_matrix_helper(0 "")
endfunction()

add_subdirectory(libmisc)
add_subdirectory(libobj)
add_subdirectory(libcr)
add_subdirectory(libcr_ipc)
add_subdirectory(libhw_generic)
add_subdirectory(libhw)
add_subdirectory(libdhcp)
add_subdirectory(libusb)
add_subdirectory(lib9p)
add_subdirectory(lib9p_util)

add_subdirectory(cmd/sbc_harness)