Skip to content

Commit 48761bc

Browse files
committed
hal_nxp: Sync 25.06 cmake to hal_nxp
Signed-off-by: Zhaoxiang Jin <[email protected]>
1 parent f52fb60 commit 48761bc

File tree

2 files changed

+202
-39
lines changed

2 files changed

+202
-39
lines changed

mcux/mcux-sdk-ng/cmake/extension/basic_settings_lite.cmake

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
# SPDX-License-Identifier: Apache-2.0
22
#
3-
# Copyright 2024 NXP
3+
# Copyright 2025 NXP
44

55
include_guard(GLOBAL)
66

7+
include(${SdkRootDirPath}/cmake/extension/misc_function.cmake)
8+
set(READ_TOOL_VERSION_PY "${SdkRootDirPath}/scripts/misc/read_tool_versions.py")
9+
10+
_read_tool_versions(${READ_TOOL_VERSION_PY})
11+
12+
log_status("CMake version: ${CMAKE_VERSION}")
13+
if (CMAKE_VERSION VERSION_LESS ${CMAKE_MINIMUM_VERSION})
14+
message("warning: The system CMake version ${CMAKE_VERSION} is lower than the recommended version ${CMAKE_MINIMUM_VERSION} which may cause unexpected build failure especially for complicated project. Please upgrade CMake to version ${CMAKE_MINIMUM_VERSION} or above.")
15+
endif()
16+
717
# Source-less library that encapsulates all the global compiler options needed
818
# by all source files.
919
add_library(mcux_build_properties INTERFACE)
@@ -57,3 +67,8 @@ list(
5767
CONFIG_MCUX_TOOLCHAIN_MDK_CPU_IDENTIFIER
5868
CONFIG_MCUX_TOOLCHAIN_JLINK_CPU_IDENTIFIER
5969
CONFIG_MCUX_HW_SOC_MULTICORE_DEVICE)
70+
71+
list(
72+
APPEND
73+
USED_CONFIG_SYMBOLS
74+
CONFIG_TOOLCHAIN)

mcux/mcux-sdk-ng/cmake/extension/function.cmake

Lines changed: 186 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: BSD-3-Clause
44
function(mcux_add_source)
55
set(single_value BASE_PATH CONFIG PREINCLUDE EXCLUDE SCOPE)
6-
set(multi_value SOURCES ${MCUX_SOURCE_CONDITION})
6+
set(multi_value SOURCES PREINCLUDE_TYPE ${MCUX_SOURCE_CONDITION})
77
cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}"
88
${ARGN})
99

@@ -66,8 +66,13 @@ function(mcux_add_source)
6666
if (match)
6767
file(GLOB files "${directory}/${file}")
6868
else ()
69-
# add source_abs_path to list file
70-
list(APPEND files "${source_abs_path}")
69+
string(REGEX MATCH "\\*\\*" match "${file}")
70+
if (match)
71+
file(GLOB_RECURSE files "${directory}/*")
72+
else ()
73+
# add source_abs_path to list file
74+
list(APPEND files "${source_abs_path}")
75+
endif ()
7176
endif ()
7277
foreach(source_abs_path ${files})
7378
# process config files, project customized config files have higher priority
@@ -171,8 +176,17 @@ function(mcux_add_source)
171176
set(pre_flag "-include ${source_abs_path}")
172177
endif()
173178
if (pre_flag)
174-
mcux_append_single_compiler_flags_variable("CC" "${pre_flag}")
175-
mcux_append_single_compiler_flags_variable("CX" "${pre_flag}")
179+
if(__PREINCLUDE_TYPE)
180+
if("c_include" IN_LIST __PREINCLUDE_TYPE)
181+
mcux_append_single_compiler_flags_variable("CC" "${pre_flag}")
182+
endif()
183+
if("cpp_include" IN_LIST __PREINCLUDE_TYPE)
184+
mcux_append_single_compiler_flags_variable("CX" "${pre_flag}")
185+
endif()
186+
else()
187+
mcux_append_single_compiler_flags_variable("CC" "${pre_flag}")
188+
mcux_append_single_compiler_flags_variable("CX" "${pre_flag}")
189+
endif()
176190
endif()
177191
if(${CONFIG_TOOLCHAIN} STREQUAL "codewarrior")
178192
# don't set preinclude file for codewarrior assembler, otherwise build failed
@@ -184,16 +198,30 @@ function(mcux_add_source)
184198
# Error: ^
185199
# Unrecognized mnemonic: CONFIG_DBI_USE_MIPI_PANE
186200
elseif (as_pre_flag)
187-
mcux_append_single_compiler_flags_variable("AS" "${as_pre_flag}")
201+
if(__PREINCLUDE_TYPE)
202+
if("asm_include" IN_LIST __PREINCLUDE_TYPE)
203+
mcux_append_single_compiler_flags_variable("AS" "${as_pre_flag}")
204+
endif()
205+
else()
206+
mcux_append_single_compiler_flags_variable("AS" "${as_pre_flag}")
207+
endif()
188208
else ()
189-
mcux_append_single_compiler_flags_variable("AS" "${pre_flag}")
209+
if(__PREINCLUDE_TYPE)
210+
if("asm_include" IN_LIST __PREINCLUDE_TYPE)
211+
mcux_append_single_compiler_flags_variable("AS" "${pre_flag}")
212+
endif()
213+
else()
214+
mcux_append_single_compiler_flags_variable("AS" "${pre_flag}")
215+
endif()
190216
endif ()
191217
endif()
192218

193219
if (NOT __EXCLUDE)
194220
target_sources(${MCUX_SDK_PROJECT_NAME} ${source_scope} ${source_abs_path})
195221
log_debug("Add source ${source_abs_path} into project"
196222
${CMAKE_CURRENT_LIST_FILE})
223+
else()
224+
mcux_set_property(MCUX_EXCLUDE_FILE_LIST ${source_abs_path} APPEND)
197225
endif ()
198226

199227
# need to go through remove source list and remove the source from
@@ -480,6 +508,13 @@ function(mcux_set_variable name value)
480508
CACHE STRING "Selected ${name}: ${value}")
481509
endif()
482510

511+
if(${name} STREQUAL device)
512+
# for west cmd to find cached variable
513+
set(CACHED_DEVICE
514+
${value}
515+
CACHE STRING "Selected ${name}: ${value}")
516+
endif()
517+
483518
set(${name} ${value} CACHE STRING "The variable ${name}: ${value}" FORCE)
484519

485520
log_debug("Variable ${name} is set to ${value}" ${CMAKE_CURRENT_LIST_FILE})
@@ -505,7 +540,7 @@ function(post_build_process)
505540
endfunction()
506541

507542
function(mcux_convert_binary)
508-
set(single_value BINARY)
543+
set(single_value BINARY TARGET)
509544
set(multi_value TOOLCHAINS EXTRA_ARGS)
510545
cmake_parse_arguments(_ "${options}" "${single_value}" "${multi_value}"
511546
${ARGN})
@@ -524,6 +559,19 @@ function(mcux_convert_binary)
524559
if(NOT binary_name)
525560
return()
526561
endif()
562+
563+
if(__TARGET)
564+
set(target_name ${__TARGET})
565+
else()
566+
set(target_name ${MCUX_SDK_PROJECT_NAME})
567+
endif()
568+
569+
# Library can not be converted to binary file, only convert executable
570+
get_target_property(TARGET_TYPE ${target_name} TYPE)
571+
if(NOT TARGET_TYPE STREQUAL "EXECUTABLE")
572+
log_warn("${target_name} is library type, can't be converted to binary file by mcux_convert_binary" ${CMAKE_CURRENT_LIST_FILE})
573+
return()
574+
endif()
527575

528576
# Get the absolute folder path
529577
get_filename_component(FILE_PATH ${binary_name} DIRECTORY)
@@ -548,27 +596,33 @@ function(mcux_convert_binary)
548596
if(NOT __EXTRA_ARGS)
549597
set(__EXTRA_ARGS -gap-fill 0xff -flash-start-x 0x20000)
550598
endif()
551-
target_link_options(${MCUX_SDK_PROJECT_NAME} PRIVATE ${OBJDUMP_BIN_CMD} ${__EXTRA_ARGS})
599+
target_link_options(${target_name} PRIVATE ${OBJDUMP_BIN_CMD} ${__EXTRA_ARGS})
552600
return()
601+
else()
602+
if(__EXTRA_ARGS)
603+
string(REPLACE " " ";" extra_args "${__EXTRA_ARGS}")
604+
else()
605+
set(extra_args)
606+
endif()
553607
endif()
554608

555-
get_target_property(IMAGE_FILE_NAME ${MCUX_SDK_PROJECT_NAME} OUTPUT_NAME)
609+
get_target_property(IMAGE_FILE_NAME ${target_name} OUTPUT_NAME)
556610
if (${IMAGE_FILE_NAME} STREQUAL "IMAGE_FILE_NAME-NOTFOUND")
557-
set(IMAGE_FILE_NAME ${MCUX_SDK_PROJECT_NAME})
611+
set(IMAGE_FILE_NAME ${target_name})
558612
endif()
559613
add_custom_command(
560-
TARGET ${MCUX_SDK_PROJECT_NAME}
614+
TARGET ${target_name}
561615
POST_BUILD
562616
COMMAND
563-
${CMAKE_OBJCOPY} ${OBJDUMP_BIN_CMD}
617+
${CMAKE_OBJCOPY} ${OBJDUMP_BIN_CMD} ${extra_args}
564618
${CMAKE_CURRENT_BINARY_DIR}/${IMAGE_FILE_NAME}${CMAKE_EXECUTABLE_SUFFIX}
565619
${OBJDUMP_OUT_CMD} ${binary_name})
566620

567-
get_target_property(clean_files ${MCUX_SDK_PROJECT_NAME}
621+
get_target_property(clean_files ${target_name}
568622
ADDITIONAL_CLEAN_FILES)
569623
list(APPEND clean_files ${binary_name})
570624

571-
set_target_properties(${MCUX_SDK_PROJECT_NAME}
625+
set_target_properties(${target_name}
572626
PROPERTIES ADDITIONAL_CLEAN_FILES "${clean_files}")
573627
endfunction()
574628

@@ -1098,12 +1152,33 @@ endfunction()
10981152

10991153
function(mcux_add_cmakelists path)
11001154
set(add_cmakelist 0)
1101-
file(RELATIVE_PATH relative_path ${SdkRootDirPath} ${path})
11021155
set(cmakelist_path ${path}/CMakeLists.txt)
1156+
file(RELATIVE_PATH relative_path ${SdkRootDirPath} ${path})
1157+
1158+
string(FIND ${relative_path} "../" sdk_path_index)
1159+
if(${sdk_path_index} GREATER -1)
1160+
string(REGEX REPLACE "^([.][.]/)+" "custom/" relative_path ${relative_path})
1161+
endif()
11031162

11041163
if(ARGC EQUAL 1)
11051164
set(add_cmakelist 1)
1165+
get_filename_component(binary_path "${APPLICATION_BINARY_DIR}/${relative_path}" ABSOLUTE)
11061166
elseif(ARGC EQUAL 2)
1167+
string(TOUPPER ${ARGV1} ARGV1)
1168+
if(${ARGV1} STREQUAL OPTIONAL)
1169+
if(EXISTS ${cmakelist_path})
1170+
set(add_cmakelist 1)
1171+
get_filename_component(binary_path "${APPLICATION_BINARY_DIR}/${relative_path}" ABSOLUTE)
1172+
else()
1173+
log_debug("No CMakeLists.txt in ${path}" ${CMAKE_CURRENT_LIST_FILE})
1174+
endif()
1175+
else()
1176+
# If the second argument is not OPTIONAL, treat it as binary_path
1177+
set(add_cmakelist 1)
1178+
set(binary_path ${ARGV1})
1179+
endif()
1180+
elseif(ARGC EQUAL 3)
1181+
set(binary_path ${ARGV2})
11071182
string(TOUPPER ${ARGV1} ARGV1)
11081183
if(${ARGV1} STREQUAL OPTIONAL)
11091184
if(EXISTS ${cmakelist_path})
@@ -1116,6 +1191,10 @@ function(mcux_add_cmakelists path)
11161191
"Wrong argument in mcux_add_cmakelists, second argument only supports OPTIONAL"
11171192
${CMAKE_CURRENT_LIST_FILE})
11181193
endif()
1194+
else()
1195+
log_fatal(
1196+
"Wrong number of arguments in mcux_add_cmakelists, expected 1, 2, or 3 arguments"
1197+
${CMAKE_CURRENT_LIST_FILE})
11191198
endif()
11201199

11211200
if(${add_cmakelist} EQUAL 1)
@@ -1124,7 +1203,13 @@ function(mcux_add_cmakelists path)
11241203
else()
11251204
log_debug("Include CMakeLists.txt from ${path}"
11261205
${CMAKE_CURRENT_LIST_FILE})
1127-
add_subdirectory(${path} ${CMAKE_CURRENT_BINARY_DIR}/${relative_path})
1206+
get_property(ADDED_SUBDIRS GLOBAL PROPERTY ADDED_SUBDIRS)
1207+
get_filename_component(path "${path}" REALPATH)
1208+
list(FIND ADDED_SUBDIRS ${path} SUBDIR_FOUND)
1209+
if(SUBDIR_FOUND EQUAL -1)
1210+
add_subdirectory(${path} ${binary_path})
1211+
set_property(GLOBAL APPEND PROPERTY ADDED_SUBDIRS ${path})
1212+
endif()
11281213
endif()
11291214
endif()
11301215
endfunction()
@@ -1135,41 +1220,53 @@ function(mcux_load_project_ide_data)
11351220
foreach(f
11361221
${SdkRootDirPath}/devices/IDE.yml
11371222
${SdkRootDirPath}/devices/${soc_portfolio}/IDE.yml
1138-
${SdkRootDirPath}/devices/${soc_portfolio}/${soc_series}/IDE.yml
1139-
${SdkRootDirPath}/devices/${soc_portfolio}/${soc_series}/${device}/IDE.yml
1140-
${SdkRootDirPath}/examples/IDE.yml
1141-
${SdkRootDirPath}/examples/_boards/IDE.yml
1142-
${SdkRootDirPath}/examples/_boards/${board}/IDE.yml)
1223+
${SdkRootDirPath}/${device_root}/${soc_portfolio}/${soc_series}/IDE.yml
1224+
${SdkRootDirPath}/${device_root}/${soc_portfolio}/${soc_series}/${device}/IDE.yml)
11431225
if(EXISTS ${f})
11441226
list(APPEND IDE_yml_list ${f})
11451227
endif()
11461228
endforeach()
1229+
1230+
if (DEFINED board)
1231+
foreach(f
1232+
${SdkRootDirPath}/examples/IDE.yml
1233+
${SdkRootDirPath}/examples/_boards/IDE.yml
1234+
${SdkRootDirPath}/${board_root}/${board}/IDE.yml)
1235+
if(EXISTS ${f})
1236+
list(APPEND IDE_yml_list ${f})
1237+
endif()
1238+
endforeach()
1239+
endif()
11471240
else()
11481241
foreach(f
11491242
${SdkRootDirPath}/devices/IDE.yml
11501243
${SdkRootDirPath}/devices/${soc_portfolio}/IDE.yml
1151-
${SdkRootDirPath}/devices/${soc_portfolio}/${soc_series}/IDE.yml
1152-
${SdkRootDirPath}/devices/${soc_portfolio}/${soc_series}/${device}/IDE.yml
1153-
${SdkRootDirPath}/devices/${soc_portfolio}/${soc_series}/${device}/${core_id}/IDE.yml
1154-
${SdkRootDirPath}/examples/IDE.yml
1155-
${SdkRootDirPath}/examples/_boards/IDE.yml
1156-
${SdkRootDirPath}/examples/_boards/${board}/IDE.yml
1157-
${SdkRootDirPath}/examples/_boards/${board}/${core_id}/IDE.yml)
1244+
${SdkRootDirPath}/${device_root}/${soc_portfolio}/${soc_series}/IDE.yml
1245+
${SdkRootDirPath}/${device_root}/${soc_portfolio}/${soc_series}/${device}/IDE.yml
1246+
${SdkRootDirPath}/${device_root}/${soc_portfolio}/${soc_series}/${device}/${core_id}/IDE.yml)
11581247
if(EXISTS ${f})
11591248
list(APPEND IDE_yml_list ${f})
11601249
endif()
11611250
endforeach()
1251+
1252+
if (DEFINED board)
1253+
foreach(f
1254+
${SdkRootDirPath}/examples/IDE.yml
1255+
${SdkRootDirPath}/examples/_boards/IDE.yml
1256+
${SdkRootDirPath}/${board_root}/${board}/IDE.yml
1257+
${SdkRootDirPath}/${board_root}/${board}/${core_id}/IDE.yml)
1258+
if(EXISTS ${f})
1259+
list(APPEND IDE_yml_list ${f})
1260+
endif()
1261+
endforeach()
1262+
endif()
11621263
endif()
11631264

1164-
if (DEFINED project_board_port_path)
1165-
if (DEFINED INTERNAL_EXAMPLE)
1166-
get_target_source_in_sub_folders(${APPLICATION_SOURCE_DIR} ${INTERNAL_EXAMPLE_FOLDER} "IDE.yml")
1167-
else ()
1168-
get_target_source_in_sub_folders(${APPLICATION_SOURCE_DIR} "examples" "IDE.yml")
1169-
endif ()
1265+
if (DEFINED project_board_port_path OR DEFINED project_device_port_path)
1266+
get_target_source_in_sub_folders(${APPLICATION_SOURCE_DIR} ${EXAMPLE_FOLDER} "IDE.yml")
11701267
list(APPEND IDE_yml_list ${GET_TARGET_SOURCE_IN_SUB_FOLDERS_OUTPUT})
11711268

1172-
get_target_source_in_sub_folders(${full_project_board_port_path} "${board}" "IDE.yml")
1269+
get_target_source_in_sub_folders(${full_project_port_path} "${board_device_folder}" "IDE.yml")
11731270
list(APPEND IDE_yml_list ${GET_TARGET_SOURCE_IN_SUB_FOLDERS_OUTPUT})
11741271
else()
11751272
if (EXISTS ${APPLICATION_SOURCE_DIR}/IDE.yml)
@@ -2389,7 +2486,58 @@ function(reset_app_link_order)
23892486
target_link_libraries(app PRIVATE -Wl,--end-group)
23902487
endif()
23912488

2392-
# Workaround: there will a an extra "-Wl,--start-group" without the statement below
2393-
target_link_libraries(app PRIVATE McuxSDK)
2489+
# Workaround:
2490+
# Without the statement below, there are two errors:
2491+
# missing --end-group
2492+
# undefined reference to `_exit'
2493+
if(${CONFIG_TOOLCHAIN} STREQUAL "armgcc")
2494+
target_link_libraries(app PRIVATE -Wl,--start-group)
2495+
target_link_libraries(app PRIVATE "-lm")
2496+
target_link_libraries(app PRIVATE "-lc")
2497+
target_link_libraries(app PRIVATE "-lgcc")
2498+
target_link_libraries(app PRIVATE "-lnosys")
2499+
target_link_libraries(app PRIVATE -Wl,--end-group)
2500+
endif()
23942501
endfunction()
23952502

2503+
# Usage
2504+
# _get_subfolder_file(<output-var> <directory> <pattern> <level>)
2505+
#
2506+
# get designated file path from subfolder with designed deepth level
2507+
function(_get_subfolder_file OUTPUT_VAR CURRENT_DIR PATTERN LEVEL)
2508+
set(current_level 1)
2509+
set(dir_queue "${CURRENT_DIR}")
2510+
2511+
while( dir_queue AND (${current_level} LESS_EQUAL ${LEVEL}))
2512+
list(LENGTH dir_queue queue_length)
2513+
2514+
math(EXPR end_index "${queue_length} - 1")
2515+
foreach(index RANGE ${end_index})
2516+
list(GET dir_queue ${index} dir)
2517+
file(GLOB children RELATIVE "${dir}" "${dir}/*/")
2518+
2519+
foreach(child ${children})
2520+
if(IS_DIRECTORY "${dir}/${child}")
2521+
list(APPEND dir_queue "${dir}/${child}")
2522+
endif ()
2523+
endforeach()
2524+
2525+
endforeach()
2526+
2527+
math(EXPR end_index "${queue_length} - 1")
2528+
foreach(index RANGE ${end_index})
2529+
list(REMOVE_AT dir_queue 0)
2530+
endforeach ()
2531+
2532+
2533+
math(EXPR current_level "${current_level} + 1")
2534+
endwhile()
2535+
2536+
foreach (dir ${dir_queue})
2537+
if(EXISTS ${dir}/${PATTERN})
2538+
set(${OUTPUT_VAR} ${dir}/${PATTERN} PARENT_SCOPE)
2539+
break()
2540+
endif ()
2541+
endforeach ()
2542+
2543+
endfunction()

0 commit comments

Comments
 (0)