66#=============================================================================#
77function (_set_library_flags _library_target _board_id)
88
9+ set (scope_options "PRIVATE" "PUBLIC" "INTERFACE" )
10+ cmake_parse_arguments (parsed_args "${scope_options} " "" "" ${ARGN} )
11+
12+ if (parsed_args_PRIVATE)
13+ set (scope PRIVATE )
14+ elseif (parsed_args_INTERFACE)
15+ set (scope INTERFACE )
16+ else ()
17+ set (scope PUBLIC )
18+ endif ()
19+
920 # Set C++ compiler flags
1021 get_cmake_compliant_language_name(cpp flags_language)
11- set_compiler_target_flags(${_library_target} "${_board_id} " PUBLIC LANGUAGE ${flags_language} )
22+ set_compiler_target_flags(${_library_target} "${_board_id} " ${scope} LANGUAGE ${flags_language} )
1223
1324 # Set linker flags
1425 set_linker_flags(${_library_target} "${_board_id} " )
@@ -22,37 +33,45 @@ endfunction()
2233# _target_name - Name of the library target to be created. Usually library's real name.
2334# _board_id - Board ID associated with the linked Core Lib.
2435# _sources - Source and header files to create library target from.
25- # [ARCH] - Optional library architecture (Such as 'avr', 'nrf52', etc.)
36+ # [ARCH] - Optional library architecture (Such as 'avr', 'nrf52', etc.).
37+ # [INTERFACE] - Whether the library should be created as an interface library (header-only).
2638#=============================================================================#
2739function (_add_arduino_cmake_library _target_name _board_id _sources)
2840
29- cmake_parse_arguments (library " " "ARCH" "" ${ARGN} )
41+ cmake_parse_arguments (parsed_args "INTERFACE " "ARCH" "" ${ARGN} )
3042
31- if (library_ARCH ) # Treat architecture-specific libraries differently
43+ if (parsed_args_ARCH ) # Treat architecture-specific libraries differently
3244 # Filter any sources that aren't supported by the platform's architecture
3345 list (LENGTH library_ARCH num_of_libs_archs)
3446 if (${num_of_libs_archs} GREATER 1)
3547 # Exclude all unsupported architectures, request filter in regex mode
36- _get_unsupported_architectures("${library_ARCH } " arch_filter REGEX )
48+ _get_unsupported_architectures("${parsed_args_ARCH } " arch_filter REGEX )
3749 set (filter_type EXCLUDE )
3850 else ()
39- set (arch_filter "src\\ /[^/]+\\ .|${library_ARCH } " )
51+ set (arch_filter "src\\ /[^/]+\\ .|${parsed_args_ARCH } " )
4052 set (filter_type INCLUDE )
4153 endif ()
4254 list (FILTER _sources ${filter_type} REGEX ${arch_filter} )
4355 endif ()
4456
45- add_library (${_target_name} STATIC "${_sources} " )
57+ if (parsed_args_INTERFACE)
58+ add_library (${_target_name} INTERFACE )
59+ set (scope INTERFACE )
60+ else ()
61+ add_library (${_target_name} STATIC "${_sources} " )
62+ set (scope PUBLIC )
63+ endif ()
64+
4665 # Treat headers' parent directories as include directories of the target
4766 get_headers_parent_directories("${_sources} " include_dirs)
48- target_include_directories (${_target_name} PUBLIC ${include_dirs} )
67+ target_include_directories (${_target_name} ${scope} ${include_dirs} )
4968
50- _set_library_flags(${_target_name} ${_board_id} )
69+ _set_library_flags(${_target_name} ${_board_id} ${scope} )
5170
52- if (library_ARCH )
53- string (TOUPPER ${library_ARCH } upper_arch)
71+ if (parsed_args_ARCH )
72+ string (TOUPPER ${parsed_args_ARCH } upper_arch)
5473 set (arch_definition "ARDUINO_ARCH_${upper_arch} " )
55- target_compile_definitions (${_target_name} PUBLIC ${arch_definition} )
74+ target_compile_definitions (${_target_name} ${scope} ${arch_definition} )
5675 endif ()
5776
5877endfunction ()
@@ -76,6 +95,15 @@ function(_link_arduino_cmake_library _target_name _library_name)
7695 set (scope_options "PRIVATE" "PUBLIC" "INTERFACE" )
7796 cmake_parse_arguments (link_library "${scope_options} " "BOARD_CORE_TARGET" "" ${ARGN} )
7897
98+ # Now, link library to executable
99+ if (link_library_PUBLIC)
100+ set (scope PUBLIC )
101+ elseif (link_library_INTERFACE)
102+ set (scope INTERFACE )
103+ else ()
104+ set (scope PRIVATE )
105+ endif ()
106+
79107 # First, include core lib's directories in library as well
80108 if (link_library_BOARD_CORE_TARGET)
81109 set (core_target ${link_library_BOARD_CORE_TARGET} )
@@ -84,17 +112,9 @@ function(_link_arduino_cmake_library _target_name _library_name)
84112 endif ()
85113
86114 get_target_property (core_lib_includes ${core_target} INCLUDE_DIRECTORIES )
87- target_include_directories (${_library_name} PUBLIC "${core_lib_includes} " )
88- target_link_libraries (${_library_name} PUBLIC ${core_target} )
115+ target_include_directories (${_library_name} ${scope} "${core_lib_includes} " )
116+ target_link_libraries (${_library_name} ${scope} ${core_target} )
89117
90- # Now, link library to executable
91- if (link_library_PUBLIC)
92- set (scope PUBLIC )
93- elseif (link_library_INTERFACE)
94- set (scope INTERFACE )
95- else ()
96- set (scope PRIVATE )
97- endif ()
98- target_link_libraries (${_target_name} ${scope} ${_library_name} )
118+ target_link_libraries (${_target_name} PRIVATE ${_library_name} )
99119
100120endfunction ()
0 commit comments