@@ -27,6 +27,29 @@ function(pico_add_subdirectory subdir)
27
27
pico_promote_common_scope_vars()
28
28
endfunction ()
29
29
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
+
30
53
# add a link option to wrap the given function name; i.e. -Wl:wrap=FUNCNAME for gcc
31
54
function (pico_wrap_function TARGET FUNCNAME)
32
55
target_link_options (${TARGET} INTERFACE "LINKER:--wrap=${FUNCNAME} " )
@@ -42,18 +65,34 @@ function(pico_add_map_output TARGET)
42
65
endif ()
43
66
endfunction ()
44
67
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 )
47
70
macro (pico_simple_hardware_target NAME )
48
71
pico_simple_hardware_headers_target(${NAME} )
49
72
pico_simple_hardware_impl_target(${NAME} )
50
73
endmacro ()
51
74
52
75
# 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
53
78
function (pico_add_impl_library target )
54
79
add_library (${target} INTERFACE )
55
80
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} )
57
96
endfunction ()
58
97
59
98
# 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)
66
105
target_include_directories (hardware_${NAME} _headers INTERFACE ${CMAKE_CURRENT_LIST_DIR} /include )
67
106
target_link_libraries (hardware_${NAME} _headers INTERFACE pico_base_headers)
68
107
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 )
70
109
endif ()
71
110
endif ()
72
111
endmacro ()
@@ -80,13 +119,17 @@ macro(pico_simple_hardware_headers_only_target NAME)
80
119
# super interesting except to determine functionality as they are mostly passive accessors, however
81
120
# they could be useful to determine if the header is available.
82
121
# pico_add_sdk_impl_library(hardware_${NAME})
83
- add_library (hardware_${NAME} INTERFACE )
122
+ add_library (hardware_${NAME} _headers INTERFACE )
84
123
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)
87
127
if (NOT PICO_NO_HARDWARE)
88
- target_link_libraries (hardware_${NAME} INTERFACE hardware_structs)
128
+ target_link_libraries (hardware_${NAME} _headers INTERFACE hardware_structs)
89
129
endif ()
130
+
131
+ add_library (hardware_${NAME} INTERFACE )
132
+ target_link_libraries (hardware_${NAME} INTERFACE hardware_${NAME} _headers)
90
133
endif ()
91
134
endmacro ()
92
135
@@ -104,7 +147,10 @@ macro(pico_simple_hardware_impl_target NAME)
104
147
${CMAKE_CURRENT_LIST_DIR} /${NAME} .c
105
148
)
106
149
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 ()
108
154
endif ()
109
155
endmacro ()
110
156
0 commit comments