Skip to content

Commit 0e4e25a

Browse files
authored
Rework build targets such that: (earlephilhower#1211)
1. Make sure Pico SDK libraries have the correct dependencies on other SDK libraries 2. Pico SDK libraries all have _headers variants to include the headers. This may facilitate building user STATIC libraries without pulling in SDK code, though care will still need to be taken w.r.t. values of #defines 3. Make sure the _headers versions also have the correct dependencies Note: There are a few exceptions to 1. for some non code libraries like pico_standard_link and pico_cxx_options
1 parent d717fe2 commit 0e4e25a

File tree

65 files changed

+318
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+318
-247
lines changed

src/CMakeLists.txt

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ function(pico_add_subdirectory subdir)
2727
pico_promote_common_scope_vars()
2828
endfunction()
2929

30+
# takes dependencies after the target
31+
function(pico_mirrored_target_link_libraries TARGET SCOPE)
32+
if (${ARGC} LESS 3)
33+
message(FATAL_ERROR "expected a target, scope and at least one dependency")
34+
endif()
35+
if (NOT TARGET ${TARGET}_headers)
36+
message(FATAL_ERROR "${TARGET} does not have headers")
37+
endif()
38+
# library should depend on its own header
39+
target_link_libraries(${TARGET} ${SCOPE} ${TARGET}_headers)
40+
foreach(DEPENDENCY IN LISTS ARGN)
41+
if (DEPENDENCY MATCHES ".*_headers")
42+
message(FATAL_ERROR "should not use headers with pico_mirrored_target_link_libraries")
43+
endif()
44+
# note, it would be nice to only add the dependency if it exists, but we do
45+
# not necessarily add libraries in reverse dependency order, so we do this
46+
# unconditionally, so this function should only be passed dependencies that
47+
# have headers, or link will fail with a missing library -lfoo_headers
48+
target_link_libraries(${TARGET}_headers ${SCOPE} ${DEPENDENCY}_headers)
49+
target_link_libraries(${TARGET} ${SCOPE} ${DEPENDENCY})
50+
endforeach()
51+
endfunction()
52+
3053
# add a link option to wrap the given function name; i.e. -Wl:wrap=FUNCNAME for gcc
3154
function(pico_wrap_function TARGET FUNCNAME)
3255
target_link_options(${TARGET} INTERFACE "LINKER:--wrap=${FUNCNAME}")
@@ -42,18 +65,34 @@ function(pico_add_map_output TARGET)
4265
endif ()
4366
endfunction()
4467

45-
# create a hardware_NAME_headers target (see pico_pico_simple_hardware_headers_target)
46-
# create a hardware_NAME target (see pico_pico_simple_hardware_target)
68+
# create a hardware_NAME_headers target (see pico_simple_hardware_headers_target)
69+
# create a hardware_NAME target (see pico_simple_hardware_target)
4770
macro(pico_simple_hardware_target NAME)
4871
pico_simple_hardware_headers_target(${NAME})
4972
pico_simple_hardware_impl_target(${NAME})
5073
endmacro()
5174

5275
# create an INTERFACE library named target, and define LIB_TARGET=1 (upper case) as a compile option
76+
# and make it dependent on a pre-existing corresponding _headers library
77+
# optional arg NOFLAG will skip the LIB_TARGET definition
5378
function(pico_add_impl_library target)
5479
add_library(${target} INTERFACE)
5580
string(TOUPPER ${target} TARGET_UPPER)
56-
target_compile_definitions(${target} INTERFACE LIB_${TARGET_UPPER}=1)
81+
if (${ARGC} GREATER 1)
82+
if (NOT "${ARGV1}" STREQUAL "NOFLAG")
83+
message(FATAL_ERROR "Unknown parameter ${ARGV1}")
84+
endif()
85+
else()
86+
target_compile_definitions(${target} INTERFACE LIB_${TARGET_UPPER}=1)
87+
endif()
88+
target_link_libraries(${target} INTERFACE ${target}_headers)
89+
endfunction()
90+
91+
# create an INTERFACE library named target along with associated header, and define LIB_TARGET=1 (upper case) as a compile option
92+
# optional arg NOFLAG will skip the LIB_TARGET definition
93+
function(pico_add_library target)
94+
add_library(${target}_headers INTERFACE)
95+
pico_add_impl_library(${target} ${ARGN})
5796
endfunction()
5897

5998
# create an INTERFACE library named hardware_NAME_headers INTERFACE library if it doesn't already exist,
@@ -66,7 +105,7 @@ macro(pico_simple_hardware_headers_target NAME)
66105
target_include_directories(hardware_${NAME}_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
67106
target_link_libraries(hardware_${NAME}_headers INTERFACE pico_base_headers)
68107
if (NOT PICO_NO_HARDWARE)
69-
target_link_libraries(hardware_${NAME}_headers INTERFACE hardware_structs hardware_claim)
108+
target_link_libraries(hardware_${NAME}_headers INTERFACE hardware_structs hardware_claim_headers)
70109
endif()
71110
endif()
72111
endmacro()
@@ -80,13 +119,17 @@ macro(pico_simple_hardware_headers_only_target NAME)
80119
# super interesting except to determine functionality as they are mostly passive accessors, however
81120
# they could be useful to determine if the header is available.
82121
# pico_add_sdk_impl_library(hardware_${NAME})
83-
add_library(hardware_${NAME} INTERFACE)
122+
add_library(hardware_${NAME}_headers INTERFACE)
84123

85-
target_include_directories(hardware_${NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
86-
target_link_libraries(hardware_${NAME} INTERFACE pico_base_headers)
124+
# a headers only target should still have an explicit _headers library for consistency
125+
target_include_directories(hardware_${NAME}_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
126+
target_link_libraries(hardware_${NAME}_headers INTERFACE pico_base_headers)
87127
if (NOT PICO_NO_HARDWARE)
88-
target_link_libraries(hardware_${NAME} INTERFACE hardware_structs)
128+
target_link_libraries(hardware_${NAME}_headers INTERFACE hardware_structs)
89129
endif()
130+
131+
add_library(hardware_${NAME} INTERFACE)
132+
target_link_libraries(hardware_${NAME} INTERFACE hardware_${NAME}_headers)
90133
endif()
91134
endmacro()
92135

@@ -104,7 +147,10 @@ macro(pico_simple_hardware_impl_target NAME)
104147
${CMAKE_CURRENT_LIST_DIR}/${NAME}.c
105148
)
106149

107-
target_link_libraries(hardware_${NAME} INTERFACE hardware_${NAME}_headers pico_platform)
150+
pico_mirrored_target_link_libraries(hardware_${NAME} INTERFACE pico_platform)
151+
if (NOT PICO_NO_HARDWARE)
152+
target_link_libraries(hardware_${NAME} INTERFACE hardware_claim)
153+
endif()
108154
endif()
109155
endmacro()
110156

src/common/pico_base/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
if (NOT TARGET pico_base_headers)
2-
add_library(pico_base_headers INTERFACE)
2+
pico_add_library(pico_base NOFLAG)
33
target_include_directories(pico_base_headers INTERFACE include ${CMAKE_BINARY_DIR}/generated/pico_base)
44

55
# PICO_BUILD_DEFINE: PICO_BOARD, Name of board, type=string, default=CMake PICO_BOARD variable, group=pico_base
@@ -10,4 +10,5 @@ if (NOT TARGET pico_base_headers)
1010

1111
list(APPEND PICO_SDK_POST_LIST_FILES ${CMAKE_CURRENT_LIST_DIR}/generate_config_header.cmake)
1212
pico_promote_common_scope_vars()
13-
endif()
13+
endif()
14+
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
if (NOT TARGET pico_stdlib_headers)
22
add_library(pico_stdlib_headers INTERFACE)
33
target_include_directories(pico_stdlib_headers INTERFACE include)
4-
target_link_libraries(pico_stdlib_headers INTERFACE
5-
hardware_gpio
6-
hardware_uart
7-
hardware_divider
8-
pico_time
9-
pico_util
10-
)
4+
# dependencies handled in implementation CMakeLists.txt
115
endif()
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
if (NOT TARGET pico_sync_headers)
22
add_library(pico_sync_headers INTERFACE)
3+
target_link_libraries(pico_sync_headers INTERFACE
4+
hardware_sync_headers
5+
pico_time_headers)
6+
endif()
7+
8+
if (NOT TARGET pico_sync)
9+
pico_add_impl_library(pico_sync)
310
target_include_directories(pico_sync_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
4-
target_link_libraries(pico_sync_headers INTERFACE hardware_sync pico_time)
11+
pico_mirrored_target_link_libraries(pico_sync INTERFACE pico_sync_sem pico_sync_mutex pico_sync_critical_section pico_time hardware_sync)
512
endif()
613

14+
715
if (NOT TARGET pico_sync_core)
8-
pico_add_impl_library(pico_sync_core)
16+
pico_add_library(pico_sync_core NOFLAG)
917
target_sources(pico_sync_core INTERFACE
1018
${CMAKE_CURRENT_LIST_DIR}/lock_core.c
1119
)
12-
target_link_libraries(pico_sync_core INTERFACE pico_sync_headers)
1320
endif()
1421

1522
if (NOT TARGET pico_sync_sem)
16-
pico_add_impl_library(pico_sync_sem)
23+
pico_add_library(pico_sync_sem)
1724
target_sources(pico_sync_sem INTERFACE
1825
${CMAKE_CURRENT_LIST_DIR}/sem.c
1926
)
20-
target_link_libraries(pico_sync_sem INTERFACE pico_sync_core pico_time)
27+
pico_mirrored_target_link_libraries(pico_sync_sem INTERFACE pico_sync_core)
2128
endif()
2229

2330
if (NOT TARGET pico_sync_mutex)
24-
pico_add_impl_library(pico_sync_mutex)
31+
pico_add_library(pico_sync_mutex)
2532
target_sources(pico_sync_mutex INTERFACE
2633
${CMAKE_CURRENT_LIST_DIR}/mutex.c
2734
)
28-
target_link_libraries(pico_sync_mutex INTERFACE pico_sync_core pico_time)
35+
pico_mirrored_target_link_libraries(pico_sync_mutex INTERFACE pico_sync_core)
2936
endif()
3037

3138
if (NOT TARGET pico_sync_critical_section)
32-
pico_add_impl_library(pico_sync_critical_section)
39+
pico_add_library(pico_sync_critical_section)
3340
target_sources(pico_sync_critical_section INTERFACE
3441
${CMAKE_CURRENT_LIST_DIR}/critical_section.c
3542
)
36-
target_link_libraries(pico_sync_critical_section INTERFACE pico_sync_core pico_time)
37-
endif()
38-
39-
if (NOT TARGET pico_sync)
40-
pico_add_impl_library(pico_sync)
41-
target_link_libraries(pico_sync INTERFACE pico_sync_sem pico_sync_mutex pico_sync_critical_section pico_sync_core)
43+
pico_mirrored_target_link_libraries(pico_sync_critical_section INTERFACE pico_sync_core)
4244
endif()
4345

4446

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
if (NOT TARGET pico_time_headers)
22
add_library(pico_time_headers INTERFACE)
3-
43
target_include_directories(pico_time_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
5-
6-
target_link_libraries(pico_time_headers INTERFACE hardware_timer)
4+
target_link_libraries(pico_time_headers INTERFACE hardware_timer_headers pico_sync_headers pico_util_headers)
75
endif()
86

97
if (NOT TARGET pico_time)
@@ -12,5 +10,5 @@ if (NOT TARGET pico_time)
1210
target_sources(pico_time INTERFACE
1311
${CMAKE_CURRENT_LIST_DIR}/time.c
1412
${CMAKE_CURRENT_LIST_DIR}/timeout_helper.c)
15-
target_link_libraries(pico_time INTERFACE pico_time_headers pico_sync pico_util)
13+
target_link_libraries(pico_time INTERFACE hardware_timer pico_sync pico_util)
1614
endif()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
add_library(pico_usb_reset_interface_headers INTERFACE)
1+
pico_add_library(pico_usb_reset_interface NOFLAG)
22
target_include_directories(pico_usb_reset_interface_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if (NOT TARGET pico_util_headers)
22
add_library(pico_util_headers INTERFACE)
33
target_include_directories(pico_util_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
4-
target_link_libraries(pico_util_headers INTERFACE pico_base_headers hardware_sync)
4+
target_link_libraries(pico_util_headers INTERFACE pico_base_headers hardware_sync_headers)
55
endif()
66

77
if (NOT TARGET pico_util)
@@ -11,5 +11,5 @@ if (NOT TARGET pico_util)
1111
${CMAKE_CURRENT_LIST_DIR}/pheap.c
1212
${CMAKE_CURRENT_LIST_DIR}/queue.c
1313
)
14-
target_link_libraries(pico_util INTERFACE pico_util_headers)
14+
pico_mirrored_target_link_libraries(pico_util INTERFACE pico_sync)
1515
endif()

src/host/hardware_sync/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ if (NOT TARGET hardware_sync)
77
${CMAKE_CURRENT_LIST_DIR}/sync_core0_only.c
88
)
99

10-
target_link_libraries(hardware_sync INTERFACE hardware_sync_headers pico_platform)
10+
pico_mirrored_target_link_libraries(hardware_sync INTERFACE pico_platform)
1111
endif()
1212

src/host/hardware_timer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pico_simple_hardware_target(timer)
22

3-
target_compile_definitions(hardware_timer INTERFACE
3+
target_compile_definitions(hardware_timer_headers INTERFACE
44
PICO_HARDWARE_TIMER_RESOLUTION_US=1000 # to loosen tests a little
55
)
66

src/host/pico_divider/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pico_add_impl_library(pico_divider)
33
target_sources(pico_divider INTERFACE
44
${CMAKE_CURRENT_LIST_DIR}/divider.c)
55

6-
target_link_libraries(pico_divider INTERFACE pico_divider_headers)
6+
pico_mirrored_target_link_libraries(pico_divider INTERFACE hardware_divider)
77

88
macro(pico_set_divider_implementation TARGET IMPL)
99
endmacro()

0 commit comments

Comments
 (0)