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

Commit 76b3a84

Browse files
committed
Refactored modules to use new 'BOARD_ID' target property.
Added check for target's type where interface libraries can be used to use the 'INTERFACE' prefix for the 'BOARD_ID' property in case the target is indeed an interface library.
1 parent a946089 commit 76b3a84

File tree

17 files changed

+135
-93
lines changed

17 files changed

+135
-93
lines changed

cmake/Platform/Libraries/LibraryFlagsManager.cmake

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22
# Sets compiler and linker flags on the given library target.
33
# Changes are kept even outside the scope of the function since they apply on a target.
44
# _library_target - Name of the library target.
5-
# _board_id - Board ID associated with the library. Some flags require it.
65
#=============================================================================#
7-
function(set_library_flags _library_target _board_id)
6+
function(set_library_flags _library_target)
87

98
parse_scope_argument(scope "${ARGN}"
109
DEFAULT_SCOPE PUBLIC)
1110

12-
set_compiler_target_flags(${_library_target} ${_board_id} ${scope})
11+
# Infer target's type and act differently if it's an interface-library
12+
get_target_property(target_type ${_library_target} TYPE)
13+
14+
if ("${target_type}" STREQUAL "INTERFACE_LIBRARY")
15+
get_target_property(board_id ${_library_target} INTERFACE_BOARD_ID)
16+
else ()
17+
get_target_property(board_id ${_library_target} BOARD_ID)
18+
endif ()
19+
20+
set_target_compile_flags(${_library_target} ${scope})
1321

1422
# Set linker flags
15-
set_linker_flags(${_library_target} ${_board_id})
23+
set_linker_flags(${_library_target})
1624

1725
endfunction()

cmake/Platform/Other/TargetFlagsManager.cmake

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
#=============================================================================#
22
# Sets compiler flags on the given target using the given board ID, compiler language and scope.
33
# _target_name - Name of the target (Executable or Library) to set flags on.
4-
# _board_id - Target's bounded board ID.
54
# _language - Language for which flags are set (such as C/C++).
65
# _scope - Flags' scope relative to outer targets (targets using the given target).
76
#=============================================================================#
8-
function(_set_target_language_flags _target_name _board_id _language _scope)
7+
function(_set_target_language_flags _target_name _language _scope)
98

10-
parse_compiler_recipe_flags(${_board_id} compiler_recipe_flags
9+
# Infer target's type and act differently if it's an interface-library
10+
get_target_property(target_type ${_target_name} TYPE)
11+
12+
if ("${target_type}" STREQUAL "INTERFACE_LIBRARY")
13+
get_target_property(board_id ${_target_name} INTERFACE_BOARD_ID)
14+
else ()
15+
get_target_property(board_id ${_target_name} BOARD_ID)
16+
endif ()
17+
18+
parse_compiler_recipe_flags(${board_id} compiler_recipe_flags
1119
LANGUAGE "${_language}")
1220

1321
target_compile_options(${_target_name} ${_scope}
@@ -18,27 +26,26 @@ endfunction()
1826
#=============================================================================#
1927
# Sets compiler flags on the given target, according also to the given board ID.
2028
# _target_name - Name of the target (Executable or Library) to set flags on.
21-
# _board_id - Target's bounded board ID.
2229
#=============================================================================#
23-
function(set_compiler_target_flags _target_name _board_id)
30+
function(set_target_compile_flags _target_name)
2431

2532
cmake_parse_arguments(parsed_args "" "LANGUAGE" "" ${ARGN})
2633
parse_scope_argument(scope "${ARGN}"
2734
DEFAULT_SCOPE PUBLIC)
2835

2936
if (parsed_args_LANGUAGE)
30-
_set_target_language_flags(${_target_name} ${_board_id} ${parsed_args_LANGUAGE} ${scope})
37+
_set_target_language_flags(${_target_name} ${parsed_args_LANGUAGE} ${scope})
3138

3239
else () # No specific language requested - Use all
3340

3441
get_cmake_compliant_language_name(asm lang)
35-
_set_target_language_flags(${_target_name} ${_board_id} ${lang} ${scope})
42+
_set_target_language_flags(${_target_name} ${lang} ${scope})
3643

3744
get_cmake_compliant_language_name(c lang)
38-
_set_target_language_flags(${_target_name} ${_board_id} ${lang} ${scope})
45+
_set_target_language_flags(${_target_name} ${lang} ${scope})
3946

4047
get_cmake_compliant_language_name(cpp lang)
41-
_set_target_language_flags(${_target_name} ${_board_id} ${lang} ${scope})
48+
_set_target_language_flags(${_target_name} ${lang} ${scope})
4249

4350
endif ()
4451

@@ -47,11 +54,19 @@ endfunction()
4754
#=============================================================================#
4855
# Sets linker flags on the given target, according also to the given board ID.
4956
# _target_name - Name of the target (Executable or Library) to set flags on.
50-
# _board_id - Target's bounded board ID.
5157
#=============================================================================#
52-
function(set_linker_flags _target_name _board_id)
58+
function(set_linker_flags _target_name)
59+
60+
# Infer target's type and act differently if it's an interface-library
61+
get_target_property(target_type ${_target_name} TYPE)
62+
63+
if ("${target_type}" STREQUAL "INTERFACE_LIBRARY")
64+
get_target_property(board_id ${_target_name} INTERFACE_BOARD_ID)
65+
else ()
66+
get_target_property(board_id ${_target_name} BOARD_ID)
67+
endif ()
5368

54-
parse_linker_recpie_pattern("${_board_id}" linker_recipe_flags)
69+
parse_linker_recpie_pattern(${board_id} linker_recipe_flags)
5570

5671
string(REPLACE ";" " " cmake_compliant_linker_flags "${linker_recipe_flags}")
5772

@@ -63,12 +78,11 @@ endfunction()
6378
# Sets compiler and linker flags on the given Executable target,
6479
# according also to the given board ID.
6580
# _target_name - Name of the target (Executable) to set flags on.
66-
# _board_id - Target's bounded board ID.
6781
#=============================================================================#
68-
function(set_executable_target_flags _target_name _board_id)
82+
function(set_executable_target_flags _target_name)
6983

70-
set_compiler_target_flags(${_target_name} "${_board_id}")
71-
set_linker_flags(${_target_name} "${_board_id}")
84+
set_target_compile_flags(${_target_name})
85+
set_linker_flags(${_target_name})
7286

7387
target_link_libraries(${_target_name} PUBLIC m) # Add math library
7488

@@ -80,14 +94,13 @@ endfunction()
8094
#=============================================================================#
8195
# Sets upload/flash flags on the given target, according also to the given board ID.
8296
# _target_name - Name of the target (Executable) to set flags on.
83-
# _board_id - Target's bounded board ID.
8497
#=============================================================================#
85-
function(set_upload_target_flags _target_name _board_id _upload_port _return_var)
98+
function(set_upload_target_flags _target_name _upload_port _return_var)
8699

87-
set(upload_flags "")
100+
get_target_property(board_id ${_target_name} BOARD_ID)
88101

89102
# Parse and append recipe flags
90-
parse_upload_recipe_pattern("${_board_id}" "${_upload_port}" upload_recipe_flags)
103+
parse_upload_recipe_pattern(${board_id} "${_upload_port}" upload_recipe_flags)
91104
list(APPEND upload_flags "${upload_recipe_flags}")
92105

93106
set(target_binary_base_path "${CMAKE_CURRENT_BINARY_DIR}/${_target_name}")

cmake/Platform/Sketches/SketchHeadersManager.cmake

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# Resolves the header files included in a sketch by linking their appropriate library if necessary
33
# or by validating they're included by the sketch target.
44
# _target_name - Name of the target to add the sketch file to.
5-
# _board_id - ID of the board to bind to the target (Each target can have a single board).
65
# _sketch_file - Path to a sketch file to add to the target.
76
#=============================================================================#
8-
function(resolve_sketch_headers _target_name _board_id _sketch_file)
7+
function(resolve_sketch_headers _target_name _sketch_file)
98

109
get_source_file_included_headers("${_sketch_file}" sketch_headers)
1110

11+
get_target_property(board_id ${_target_name} BOARD_ID)
12+
1213
foreach (header ${sketch_headers})
1314

1415
# Header name without extension (such as '.h') can represent an Arduino/Platform library
@@ -21,12 +22,12 @@ function(resolve_sketch_headers _target_name _board_id _sketch_file)
2122

2223
string(TOLOWER ${header_we} header_we_lower)
2324

24-
link_platform_library(${_target_name} ${header_we_lower} ${_board_id})
25+
link_platform_library(${_target_name} ${header_we_lower})
2526

2627
else ()
2728

2829
# Pass the '3RD_PARTY' option to avoid name-conversion
29-
find_arduino_library(${header_we}_sketch_lib ${header_we} ${_board_id} 3RD_PARTY QUIET)
30+
find_arduino_library(${header_we}_sketch_lib ${header_we} ${board_id} 3RD_PARTY QUIET)
3031

3132
# If library isn't found, display a status since it might be a user library
3233
if (NOT TARGET ${header_we}_sketch_lib OR
@@ -38,7 +39,7 @@ function(resolve_sketch_headers _target_name _board_id _sketch_file)
3839
"you'd have to check this manually!")
3940

4041
else ()
41-
link_arduino_library(${_target_name} ${header_we}_sketch_lib ${_board_id})
42+
link_arduino_library(${_target_name} ${header_we}_sketch_lib)
4243
endif ()
4344

4445
endif ()

cmake/Platform/Sketches/SketchManager.cmake

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@ endfunction()
2424
# Each sketch is converted to a valid '.cpp' source file under the project's source directory.
2525
# The function also finds and links any libraries the sketch uses to the target.
2626
# _target_name - Name of the target to add the sketch file to.
27-
# _board_id - ID of the board to bind to the target (Each target can have a single board).
2827
# _sketch_file - Path to a sketch file to add to the target.
2928
#=============================================================================#
30-
function(add_sketch_to_target _target_name _board_id _sketch_file)
29+
function(add_sketch_to_target _target_name _sketch_file)
3130

3231
_get_converted_source_desired_path(${_sketch_file} sketch_converted_source_path)
3332

3433
# Only perform conversion if policy is set or if sketch hasn't been converted yet
3534
if (CONVERT_SKETCHES_IF_CONVERTED_SOURCES_EXISTS OR
3635
NOT EXISTS ${sketch_converted_source_path})
3736

38-
resolve_sketch_headers(${_target_name} ${_board_id} ${_sketch_file})
37+
resolve_sketch_headers(${_target_name} ${_sketch_file})
3938

4039
convert_sketch_to_source(${_sketch_file} ${sketch_converted_source_path})
4140

@@ -48,15 +47,14 @@ endfunction()
4847
#=============================================================================#
4948
# Adds a list of sketch files as converted sources to the given target.
5049
# _target_name - Name of the target to add the sketch file to.
51-
# _board_id - ID of the board to bind to the target (Each target can have a single board).
5250
# [Sketches] - List of paths to sketch files to add to the target.
5351
#=============================================================================#
54-
function(target_sketches _target_name _board_id)
52+
function(target_sketches _target_name)
5553

5654
parse_sources_arguments(parsed_sketches "" "" "" "${ARGN}")
5755

5856
foreach (sketch_file ${parsed_sketches})
59-
add_sketch_to_target(${_target_name} ${_board_id} ${sketch_file})
57+
add_sketch_to_target(${_target_name} ${sketch_file})
6058
endforeach ()
6159

6260
endfunction()

cmake/Platform/Targets/ArduinoCMakeLibraryTarget.cmake

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@ function(_add_arduino_cmake_library _target_name _board_id _sources)
1616

1717
add_library(${_target_name} INTERFACE)
1818
set(scope INTERFACE)
19-
19+
20+
set_property(TARGET ${_target_name} PROPERTY INTERFACE_BOARD_ID ${_board_id})
21+
2022
else ()
21-
22-
add_library(${_target_name} STATIC "${_sources}")
23+
24+
add_library(${_target_name} STATIC "${_sources}")
2325
set(scope PUBLIC)
24-
26+
27+
set_property(TARGET ${_target_name} PROPERTY BOARD_ID ${_board_id})
28+
2529
endif ()
2630

31+
2732
# Treat headers' parent directories as include directories of the target
2833
get_headers_parent_directories("${_sources}" include_dirs)
2934
target_include_directories(${_target_name} ${scope} ${include_dirs})
3035

31-
set_library_flags(${_target_name} ${_board_id} ${scope})
36+
set_library_flags(${_target_name} ${scope})
3237

33-
set_target_architecture_definition(${_target_name} ${scope} ${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE})
38+
set_target_architecture_definition(${_target_name} ${scope}
39+
${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE})
3440

3541
endfunction()
3642

cmake/Platform/Targets/ArduinoExampleTarget.cmake

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@
1111
function(add_arduino_example _target_name _board_id _example_name)
1212

1313
convert_string_to_pascal_case(${_example_name} arduino_compliant_example_name)
14+
1415
find_arduino_example_sources("${ARDUINO_SDK_EXAMPLES_PATH}"
1516
${arduino_compliant_example_name} example_sketches ${ARGN})
17+
1618
# First create the target (Without sources), then add sketches as converted sources
1719
add_arduino_executable(${_target_name} ${_board_id})
18-
target_sketches(${_target_name} ${_board_id} "${example_sketches}")
20+
21+
target_sketches(${_target_name} "${example_sketches}")
1922

2023
endfunction()
2124

2225
#=============================================================================#
2326
# Adds/Creates an Arduino-Library-Example executable target with the given name,
2427
# using the given board ID and library example's sources.
2528
# _target_name - Name of the target (Executable) to create.
26-
# _board_id - ID of the board to bind to the target (Each target can have a single board).
2729
# _library_target_name - Name of an already-existing library target.
2830
# This means the library should first be found by the user.
2931
# _library_name - Name of the library the example belongs to, such as 'Servo'.
@@ -32,8 +34,7 @@ endfunction()
3234
# e.g The 'Blink' example is under the '01.Basics' category.
3335
# Generally, all this does is improving search performance by narrowing.
3436
#=============================================================================#
35-
function(add_arduino_library_example _target_name _board_id
36-
_library_target_name _library_name _example_name)
37+
function(add_arduino_library_example _target_name _library_target_name _library_name _example_name)
3738

3839
convert_string_to_pascal_case(${_example_name} arduino_compliant_example_name)
3940
convert_string_to_pascal_case(${_library_name} arduino_compliant_library_name)
@@ -42,10 +43,15 @@ function(add_arduino_library_example _target_name _board_id
4243
message(SEND_ERROR "Library target doesn't exist - It must be created first!")
4344
endif ()
4445

46+
get_target_property(board_id ${_library_target_name} BOARD_ID)
47+
4548
find_arduino_library_example_sources("${ARDUINO_SDK_LIBRARIES_PATH}/${arduino_compliant_library_name}"
4649
${arduino_compliant_example_name} example_sketches ${ARGN})
47-
add_arduino_executable(${_target_name} ${_board_id})
48-
target_sketches(${_target_name} ${_board_id} "${example_sketches}")
49-
link_arduino_library(${_target_name} ${_library_target_name} ${_board_id})
50+
51+
add_arduino_executable(${_target_name} ${board_id})
52+
53+
target_sketches(${_target_name} "${example_sketches}")
54+
55+
link_arduino_library(${_target_name} ${_library_target_name})
5056

5157
endfunction()

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function(add_arduino_library _target_name _board_id)
3434
find_dependent_platform_libraries("${arch_resolved_sources}" lib_platform_libs)
3535

3636
foreach (platform_lib ${lib_platform_libs})
37-
link_platform_library(${_target_name} ${platform_lib} ${_board_id})
37+
link_platform_library(${_target_name} ${platform_lib})
3838
endforeach ()
3939

4040
endfunction()
@@ -57,23 +57,30 @@ endfunction()
5757
# it adds core lib's include directories to the libraries include directories.
5858
# _target_name - Name of the "executable" target.
5959
# _library_target_name - Name of the library target.
60-
# _board_id - Board ID associated with the linked Core Lib.
6160
# [HEADER_ONLY] - Whether library is a header-only library, i.e has no source files
6261
#=============================================================================#
63-
function(link_arduino_library _target_name _library_target_name _board_id)
62+
function(link_arduino_library _target_name _library_target_name)
6463

6564
cmake_parse_arguments(parsed_args "HEADER_ONLY" "" "" ${ARGN})
6665

67-
get_core_lib_target_name(${_board_id} core_lib_target)
68-
6966
if (NOT TARGET ${_target_name})
7067
message(FATAL_ERROR "Target doesn't exist - It must be created first!")
7168
elseif (NOT TARGET ${_library_target_name})
7269
message(FATAL_ERROR "Library target doesn't exist - It must be created first!")
73-
elseif (NOT TARGET ${core_lib_target})
74-
message(FATAL_ERROR "Core Library target doesn't exist. This is bad and should be reported")
7570
endif ()
7671

72+
# Retrieve 'board_id' property of targets
73+
get_target_property(board_id ${_target_name} BOARD_ID)
74+
75+
# Get the name of the Core-Lib target associated with the targets' 'board_id'
76+
get_core_lib_target_name(${board_id} core_lib_target)
77+
78+
if (NOT TARGET ${core_lib_target})
79+
message(FATAL_ERROR "Core Library target doesn't exist. "
80+
"This is bad and should be reported")
81+
endif ()
82+
83+
# Infer scope
7784
if (parsed_args_HEADER_ONLY)
7885
set(scope INTERFACE)
7986
else ()

0 commit comments

Comments
 (0)