Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 08f8702

Browse files
committed
Refactored the 'ArduinoCMakeLibraryTarget' module.
Separated target architecture definition to a function under the 'TargetFlagsManager' module.
1 parent 641cc97 commit 08f8702

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

cmake/Platform/Other/TargetFlagsManager.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,18 @@ function(set_upload_target_flags _target_name _board_id _upload_port _return_var
8181

8282
endfunction()
8383

84+
#=============================================================================#
85+
# Adds a compiler definition (#define) for the given architecture to the target.
86+
# The affecting scope of the definition is controlled by the _scope argument.
87+
# _target - Name of the target (Executable) to set flags on.
88+
# _scope - PUBLIC|INTERFACE|PRIVATE. Affects outer scope - How other targets see it.
89+
# _architecture - Architecture to define, e.g. 'avr'
90+
#=============================================================================#
91+
function(set_target_architecture_definition _target _scope _architecture)
92+
93+
string(TOUPPER ${_architecture} upper_arch)
94+
set(arch_definition "ARDUINO_ARCH_${upper_arch}")
95+
96+
target_compile_definitions(${_target} ${_scope} ${arch_definition})
97+
98+
endfunction()

cmake/Platform/Targets/ArduinoCMakeLibraryTarget.cmake

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ function(_add_arduino_cmake_library _target_name _board_id _sources)
2626

2727
set_library_flags(${_target_name} ${_board_id} ${scope})
2828

29-
string(TOUPPER ${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE} upper_arch)
30-
set(arch_definition "ARDUINO_ARCH_${upper_arch}")
31-
target_compile_definitions(${_target_name} ${scope} ${arch_definition})
29+
set_target_architecture_definition(${_target_name} ${scope}
30+
${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE})
3231

3332
endfunction()
3433

@@ -38,7 +37,7 @@ endfunction()
3837
# then links it to the library.
3938
# _target_name - Name of the target to link against.
4039
# _library_name - Name of the library target to link.
41-
# [PRIVATE|PUBLIC|INTERFACE] - Optional link scope.
40+
# [PRIVATE|PUBLIC|INTERFACE] - Optional link scope for the internally linked Core-Lib.
4241
# [BOARD_CORE_TARGET] - Optional target name of the Core Lib to use.
4342
# Use when the target is a library.
4443
#=============================================================================#
@@ -52,25 +51,33 @@ function(_link_arduino_cmake_library _target_name _library_name)
5251
cmake_parse_arguments(link_library "${scope_options}" "BOARD_CORE_TARGET" "" ${ARGN})
5352

5453
# Now, link library to executable
55-
if (link_library_PUBLIC)
56-
set(scope PUBLIC)
54+
if (link_library_PRIVATE)
55+
set(scope PRIVATE)
5756
elseif (link_library_INTERFACE)
5857
set(scope INTERFACE)
5958
else ()
60-
set(scope PRIVATE)
59+
set(scope PUBLIC)
6160
endif ()
6261

63-
# First, include core lib's directories in library as well
62+
# Resolve Core-Lib's target
6463
if (link_library_BOARD_CORE_TARGET)
6564
set(core_target ${link_library_BOARD_CORE_TARGET})
6665
else ()
6766
set(core_target ${${_target_name}_CORE_LIB_TARGET})
6867
endif ()
6968

7069
get_target_property(core_lib_includes ${core_target} INCLUDE_DIRECTORIES)
70+
71+
# Include core lib's include directories in library target, then link to it
7172
target_include_directories(${_library_name} ${scope} "${core_lib_includes}")
7273
target_link_libraries(${_library_name} ${scope} ${core_target})
7374

74-
target_link_libraries(${_target_name} PRIVATE ${_library_name})
75+
# Link library target to linked-to target
76+
if (link_library_PRIVATE)
77+
target_link_libraries(${_target_name} PRIVATE ${_library_name})
78+
else ()
79+
# Link 'INTERFACE' targets publicly, otherwise code won't compile
80+
target_link_libraries(${_target_name} PUBLIC ${_library_name})
81+
endif ()
7582

7683
endfunction()

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ function(link_arduino_library _target_name _library_target_name _board_id)
5656
endif ()
5757

5858
if (parsed_args_HEADER_ONLY)
59-
_link_arduino_cmake_library(${_target_name} ${_library_target_name}
60-
INTERFACE
61-
BOARD_CORE_TARGET ${core_lib_target})
59+
set(scope INTERFACE)
6260
else ()
63-
_link_arduino_cmake_library(${_target_name} ${_library_target_name}
64-
PUBLIC
65-
BOARD_CORE_TARGET ${core_lib_target})
61+
set(scope PUBLIC)
6662
endif ()
6763

64+
_link_arduino_cmake_library(${_target_name} ${_library_target_name}
65+
${scope}
66+
BOARD_CORE_TARGET ${core_lib_target})
67+
6868
endfunction()

0 commit comments

Comments
 (0)