Skip to content

Commit

Permalink
ports/psoc6: Added script to retrieve mtb build and link flags.
Browse files Browse the repository at this point in the history
Signed-off-by: enriquezgarc <enriquezgarcia.external@infineon.com>
  • Loading branch information
jaenrig-ifx committed Aug 30, 2023
1 parent 7732a3e commit 6b57257
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 26 deletions.
31 changes: 5 additions & 26 deletions ports/psoc6/mtb-libs/makefile_mtb.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ OPENOCD_HOME ?= $(MTB_HOME)/tools_3.0/openocd
# $(info MPY_DIR_OF_MTB_ADAPTER_MAKEFILE : $(MPY_DIR_OF_MTB_ADAPTER_MAKEFILE))

# $(info MPY_MTB_MAIN_MAKEFILE : $(MPY_MTB_MAIN_MAKEFILE))
$(info MPY_MTB_TARGET : $(MPY_MTB_TARGET))
# $(info MPY_MTB_TARGET : $(MPY_MTB_TARGET))
# $(info MPY_MTB_CONFIG : $(MPY_MTB_CONFIG))
# $(info MTB_LIB_NAME : $(MPY_MTB_LIB_NAME))

$(info MPY_MTB_BOARD_BUILD_DIR : $(MPY_MTB_BOARD_BUILD_DIR))
$(info MPY_MTB_BOARD_BUILD_OUTPUT_DIR : $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR))
# $(info MPY_MTB_BOARD_BUILD_DIR : $(MPY_MTB_BOARD_BUILD_DIR))
# $(info MPY_MTB_BOARD_BUILD_OUTPUT_DIR : $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR))

mtb_init: mtb_add_bsp mtb_set_bsp mtb_get_libs
$(info )
Expand Down Expand Up @@ -106,30 +106,9 @@ mtb_get_build_flags: mtb_build
$(eval INC += -I$(BOARD_DIR))
$(eval MPY_MTB_LIBRARIES = $(file < $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR)/liblist.rsp))
$(eval LIBS += $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR)/$(MPY_MTB_LIB_NAME))
# $(eval LIBS += $(MPY_MTB_LIBRARIES) $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR)/$(MPY_MTB_LIB_NAME))
# TODO: hardcode this flags in a board a separate board folder or in the general board folder
# python mtb_build_info.py ccxxflags build/APP_CY8CPROTO-062-4343W/Debug/.cycompiler
# $(eval CFLAGS_HARCODED = -mcpu=cortex-m4 --specs=nano.specs -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -mthumb -ffunction-sections -fdata-sections -ffat-lto-objects -g -Wall -pipe -DCOMPONENT_4343W -DCOMPONENT_APP_CY8CPROTO_062_4343W -DCOMPONENT_CAT1 -DCOMPONENT_CAT1A -DCOMPONENT_CM0P_SLEEP -DCOMPONENT_CM4 -DCOMPONENT_CM4_0 -DCOMPONENT_Debug -DCOMPONENT_GCC_ARM -DCOMPONENT_HCI_UART -DCOMPONENT_MURATA_1DX -DCOMPONENT_MW_ABSTRACTION_RTOS -DCOMPONENT_MW_CAT1CM0P -DCOMPONENT_MW_CLIB_SUPPORT -DCOMPONENT_MW_CMSIS -DCOMPONENT_MW_CONNECTIVITY_UTILITIES -DCOMPONENT_MW_CORE_LIB -DCOMPONENT_MW_CORE_MAKE -DCOMPONENT_MW_CY_MBEDTLS_ACCELERATION -DCOMPONENT_MW_FREERTOS -DCOMPONENT_MW_LWIP -DCOMPONENT_MW_LWIP_FREERTOS_INTEGRATION -DCOMPONENT_MW_LWIP_NETWORK_INTERFACE_INTEGRATION -DCOMPONENT_MW_MBEDTLS -DCOMPONENT_MW_MTB_HAL_CAT1 -DCOMPONENT_MW_MTB_PDL_CAT1 -DCOMPONENT_MW_RECIPE_MAKE_CAT1A -DCOMPONENT_MW_RETARGET_IO -DCOMPONENT_MW_SECURE_SOCKETS -DCOMPONENT_MW_SERIAL_FLASH -DCOMPONENT_MW_WHD_BSP_INTEGRATION -DCOMPONENT_MW_WIFI_CONNECTION_MANAGER -DCOMPONENT_MW_WIFI_CORE_FREERTOS_LWIP_MBEDTLS -DCOMPONENT_MW_WIFI_HOST_DRIVER -DCOMPONENT_MW_WPA3_EXTERNAL_SUPPLICANT -DCOMPONENT_PSOC6_02 -DCOMPONENT_SOFTFP -DCOMPONENT_WIFI_INTERFACE_SDIO -DCORE_NAME_CM4_0=1 -DCY8C624ABZI_S2D44 -DCYBSP_WIFI_CAPABLE -DCY_APPNAME_mtb_example_wifi_scan -DCY_RETARGET_IO_CONVERT_LF_TO_CRLF -DCY_RTOS_AWARE -DCY_SUPPORTS_DEVICE_VALIDATION -DCY_TARGET_BOARD=APP_CY8CPROTO_062_4343W -DCY_USING_HAL -DCY_WIFI_HOST_WAKE_SW_FORCE=0 -DDEBUG -DMBEDTLS_USER_CONFIG_FILE=mbedtls_user_config.h -DTARGET_APP_CY8CPROTO_062_4343W -DCOMPONENT_FREERTOS -DCOMPONENT_LWIP -DCOMPONENT_MBEDTLS)

# Mismatch between flags (CY8CPROTO-062-4343W):
# harcoded:
# (+)-DCOMPONENT_MW_WIFI_CORE_FREERTOS_LWIP_MBEDTLS
# (+)-DCY_APPNAME_mtb_example_wifi_scan
# (+)-DMBEDTLS_USER_CONFIG_FILE=mbedtls_user_config.h
# from script:
# (+)-DCY_LIBNAME_libmtb
# (+)-DMBEDTLS_USER_CONFIG_FILE=mbedtls_config.h
$(eval CFLAGS_PYTHON += $(shell $(PYTHON) $(MTB_BASE_EXAMPLE_MAKEFILE_DIR)/mtb_build_info.py ccxxflags $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR)/.cycompiler ))
$(eval CFLAGS += $(CFLAGS_PYTHON))
# $(info CFLAGS ::: $(CFLAGS_HARCODED))
$(info CFLAGS_ ::: $(CFLAGS_PYTHON))
$(eval CFLAGS += $(shell $(PYTHON) $(MTB_BASE_EXAMPLE_MAKEFILE_DIR)/mtb_build_info.py ccxxflags $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR)/.cycompiler ))
$(eval CXXFLAGS += $(CFLAGS))
# python mtb_build_info.py ldflags build/APP_CY8CPROTO-062-4343W/Debug/.cylinker
$(eval LDFLAGS_PY = $(shell $(PYTHON) $(MTB_BASE_EXAMPLE_MAKEFILE_DIR)/mtb_build_info.py ldflags $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR)/.cylinker $(MTB_BASE_EXAMPLE_MAKEFILE_DIR)))
$(info LDFLAGS_PY ::: $(LDFLAGS_PY))
# $(eval LDFLAGS += -mcpu=cortex-m4 --specs=nano.specs -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -mthumb -ffunction-sections -fdata-sections -ffat-lto-objects -g -Wall -pipe -Wl,--gc-sections -T$(MTB_BASE_EXAMPLE_MAKEFILE_DIR)/bsps/TARGET_APP_CY8CPROTO-062-4343W/COMPONENT_CM4/TOOLCHAIN_GCC_ARM/linker.ld -Wl,-Map,$(BUILD)/firmware.map -Wl,--start-group -Wl,--end-group -Wl,--print-memory-usage)
$(eval LDFLAGS += $(LDFLAGS_PY))
# $(eval LDFLAGS += -Wl,-Map,$(BUILD)/firmware.map -Wl,--start-group -Wl,--end-group -Wl,--print-memory-usage)
$(eval LDFLAGS += $(shell $(PYTHON) $(MTB_BASE_EXAMPLE_MAKEFILE_DIR)/mtb_build_info.py ldflags $(MPY_MTB_BOARD_BUILD_OUTPUT_DIR)/.cylinker $(MTB_BASE_EXAMPLE_MAKEFILE_DIR)))
$(eval QSTR_GEN_CFLAGS += $(INC) $(CFLAGS))

attached_devs:
Expand Down
92 changes: 92 additions & 0 deletions ports/psoc6/mtb-libs/mtb_build_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# This script will parse the build output files to extract build flags from the ModusToolbox
# build command that needs to be used as well in the MicroPython src builds.
# Ideally, this flags would be retievable directly from a Makefile command or variable of
# the ModusToolbox Makefile

import argparse, json, os


def get_file_content(file):
with open(file, "r") as f:
f_content = f.read()

return f_content


def get_ccxx_build_flags(cycompiler_file):
# Extract the flags located between '-c' option and
# the first (.rsp) response file
# Warning: is the response file a proper delimiter (?)
def find_flags_start(build_cmd_list):
return build_cmd_list.index("-c") + 1 # next is the first element

def find_flags_end(build_cmd_list):
file_opt = [item for item in build_cmd_list if item.startswith("@")]
return build_cmd_list.index(file_opt[0])

build_cmd = get_file_content(cycompiler_file)
build_cmd_list = build_cmd.split()

start_idx = find_flags_start(build_cmd_list)
end_idx = find_flags_end(build_cmd_list)

ccxx_flags = build_cmd_list[start_idx:end_idx]
print(*ccxx_flags)


def get_ld_linker_flags(cylinker_file, mtb_libs_path):
# Get flags only until the linker script after the compiler argument -T
def find_flags_start(link_cmd_list):
gcc_cmd = [item for item in link_cmd_list if item.endswith("arm-none-eabi-g++")]
return link_cmd_list.index(gcc_cmd[0]) + 1

def find_flags_end(link_cmd_list):
link_script_file_param = [item for item in link_cmd_list if item.startswith("-T")]
return link_cmd_list.index(link_script_file_param[0]) + 1

def set_path_of_linker_script(link_cmd_list, inker_script_param_index, mtb_libs_path):
link_cmd_list[inker_script_param_index] = (
"-T" + str(mtb_libs_path) + "/" + link_cmd_list[inker_script_param_index][len("-T") :]
)

link_cmd = get_file_content(cylinker_file)
link_cmd_list = link_cmd.split()

start_idx = find_flags_start(link_cmd_list)
end_idx = find_flags_end(link_cmd_list)
set_path_of_linker_script(link_cmd_list, end_idx - 1, mtb_libs_path)

ld_flags = link_cmd_list[start_idx:end_idx]
print(*ld_flags)


def parser():
def main_parser_func(args):
parser.print_help()

def parser_get_ccxxflags_func(args):
get_ccxx_build_flags(args.cycompiler_file)

def parser_get_ldflags_func(args):
get_ld_linker_flags(args.cylinker_file, args.mtb_libs_path)

parser = argparse.ArgumentParser(description="Utility to retrieve ModusToolbox build info")
subparser = parser.add_subparsers()
parser.set_defaults(func=main_parser_func)

parser_ccxx = subparser.add_parser("ccxxflags", description="Get C and CXX build flags")
parser_ccxx.add_argument("cycompiler_file", type=str, help=".cycompiler file")
parser_ccxx.set_defaults(func=parser_get_ccxxflags_func)

parser_ld = subparser.add_parser("ldflags", description="Get linker flags")
parser_ld.add_argument("cylinker_file", type=str, help=".cylinker file")
parser_ld.add_argument("mtb_libs_path", type=str, help="Path to mtb-libs folder")
parser_ld.set_defaults(func=parser_get_ldflags_func)

# Parser call
args = parser.parse_args()
args.func(args)


if __name__ == "__main__":
parser()

0 comments on commit 6b57257

Please sign in to comment.