Skip to content

Commit 5f30094

Browse files
author
Jamie Smith
authored
Enable disabling target files for custom targets (ARMmbed#107)
* Enable disabling target files for custom targets * Fix unittests * Fix unittests again
1 parent 9d9ba58 commit 5f30094

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ target_include_directories(mbed-core-flags
246246
# mbed_set_post_build().
247247
add_subdirectory(targets)
248248

249+
if((NOT MBED_IS_NATIVE_BUILD) AND (NOT TARGET ${MBED_TARGET_CMAKE_NAME}))
250+
message(FATAL_ERROR "CMake target ${MBED_TARGET_CMAKE_NAME} was not found after scanning the mbed-os/targets \
251+
directory. If this is a custom target, you need to define a target called ${MBED_TARGET_CMAKE_NAME} before doing: \
252+
add_subdirectory(mbed-os)")
253+
endif()
254+
255+
if(NOT MBED_IS_NATIVE_BUILD)
256+
# Disable any requested files from the targets/ directory.
257+
mbed_apply_mcu_target_file_disables()
258+
endif()
259+
249260
add_subdirectory(cmsis)
250261
add_subdirectory(drivers)
251262
add_subdirectory(hal)

tools/cmake/mbed_target_functions.cmake

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
# File containing various functions for operating on library and executable targets.
55

6+
include_guard(GLOBAL)
7+
68
#
79
# Converts output file of `target` to binary file and to Intel HEX file.
810
#
@@ -189,4 +191,67 @@ endfunction()
189191
#
190192
function(mbed_finalize_build)
191193
mbed_finalize_ide_debug_configurations()
192-
endfunction(mbed_finalize_build)
194+
endfunction(mbed_finalize_build)
195+
196+
# Lists that mbed_disable_mcu_target_file stores data in
197+
set(MBED_DISABLE_MCU_TARGET_FILE_TARGETS "" CACHE INTERNAL "" FORCE)
198+
set(MBED_DISABLE_MCU_TARGET_FILE_FILES "" CACHE INTERNAL "" FORCE)
199+
200+
# Use this function to disable a source file from one of the MCU targets in the targets/ directory.
201+
# This allows you to override this file from a custom target.
202+
# This function may only be used with a target in the mbed-os/targets directory, and may only be
203+
# called after including app.cmake and before adding mbed-os as a subdirectory.
204+
function(mbed_disable_mcu_target_file TARGET FILENAME)
205+
206+
# Record this file for later disablement
207+
set(MBED_DISABLE_MCU_TARGET_FILE_TARGETS ${MBED_DISABLE_MCU_TARGET_FILE_TARGETS} ${TARGET} CACHE INTERNAL "" FORCE)
208+
set(MBED_DISABLE_MCU_TARGET_FILE_FILES ${MBED_DISABLE_MCU_TARGET_FILE_FILES} ${FILENAME} CACHE INTERNAL "" FORCE)
209+
210+
endfunction(mbed_disable_mcu_target_file)
211+
212+
# Called later, midway through the Mbed configure, to apply the file disables that were recorded earlier.
213+
function(mbed_apply_mcu_target_file_disables)
214+
215+
# Iterate through each disable request
216+
list(LENGTH MBED_DISABLE_MCU_TARGET_FILE_TARGETS NUM_DISABLES)
217+
set(DISABLE_IDX 0)
218+
while(DISABLE_IDX LESS NUM_DISABLES)
219+
220+
# Get the target and the file
221+
list(GET MBED_DISABLE_MCU_TARGET_FILE_TARGETS ${DISABLE_IDX} CURR_TARGET)
222+
list(GET MBED_DISABLE_MCU_TARGET_FILE_FILES ${DISABLE_IDX} CURR_FILE)
223+
224+
if(TARGET ${CURR_TARGET})
225+
get_property(CURR_TARGET_TYPE TARGET ${CURR_TARGET} PROPERTY TYPE)
226+
if("${CURR_TARGET_TYPE}" STREQUAL "INTERFACE_LIBRARY")
227+
228+
# Iterate through the list of sources and remove the target one
229+
get_property(CURR_TARGET_IFACE_SOURCES TARGET ${CURR_TARGET} PROPERTY INTERFACE_SOURCES)
230+
set(FOUND FALSE)
231+
foreach(SOURCE_FILE ${CURR_TARGET_IFACE_SOURCES})
232+
get_filename_component(SOURCE_FILE_NAME ${SOURCE_FILE} NAME)
233+
if("${SOURCE_FILE_NAME}" STREQUAL CURR_FILE)
234+
set(FOUND TRUE)
235+
list(REMOVE_ITEM CURR_TARGET_IFACE_SOURCES "${SOURCE_FILE}")
236+
endif()
237+
endforeach()
238+
239+
if(FOUND)
240+
message(STATUS "Disabled file '${CURR_FILE}' in target '${CURR_TARGET}'")
241+
else()
242+
message(WARNING "mbed_disable_mcu_target_file(): File '${CURR_FILE}' not found in target '${CURR_TARGET}'")
243+
endif()
244+
245+
set_property(TARGET ${CURR_TARGET} PROPERTY INTERFACE_SOURCES ${CURR_TARGET_IFACE_SOURCES})
246+
247+
else()
248+
message(FATAL_ERROR "mbed_disable_mcu_target_file(): Target '${CURR_TARGET}' is not an interface target.")
249+
endif()
250+
else()
251+
message(FATAL_ERROR "mbed_disable_mcu_target_file(): Failed to find target '${CURR_TARGET}'.")
252+
endif()
253+
254+
math(EXPR DISABLE_IDX "${DISABLE_IDX} + 1")
255+
endwhile()
256+
257+
endfunction(mbed_apply_mcu_target_file_disables)

0 commit comments

Comments
 (0)