From 3e246caa1c672a5d42fb1db6620b16644a7ec58c Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Wed, 7 Aug 2024 11:28:17 +0800 Subject: [PATCH] CMake: Generator re-write and bug fixes. Updated CMakeLists.txt generator to handle private macro definitions for source groups. Individual source groups are added as OBJECT libraries, which does not generate actual archive but will be linked together at final application linking stage. Source groups without source files are added as INTERFACE libraries, which provides library dependencies to the final application. Signed-off-by: Yilin Sun --- tools/cmake.py | 129 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 111 insertions(+), 18 deletions(-) diff --git a/tools/cmake.py b/tools/cmake.py index 5e6765f25c..c0bb87160f 100644 --- a/tools/cmake.py +++ b/tools/cmake.py @@ -172,32 +172,125 @@ def GenerateCFiles(env, project, project_name): cm_file.write("\t-D" + i + "\n") cm_file.write(")\n\n") - cm_file.write("SET(PROJECT_SOURCES\n") + libgroups = [] + interfacelibgroups = [] for group in project: + if group['name'] == 'Applications': + continue + + # When a group is provided without sources, add it to the library list + if len(group['src']) == 0: + interfacelibgroups.append(group) + else: + libgroups.append(group) + + cm_file.write("# Library source files\n") + for group in project: + cm_file.write("SET(RT_{:s}_SOURCES\n".format(group['name'].upper())) for f in group['src']: # use relative path path = _make_path_relative(os.getcwd(), os.path.normpath(f.rfile().abspath)) cm_file.write( "\t" + path.replace("\\", "/") + "\n" ) + cm_file.write(")\n\n") + + cm_file.write("# Library search paths\n") + for group in libgroups + interfacelibgroups: + if not 'LIBPATH' in group.keys(): + continue + + if len(group['LIBPATH']) == 0: + continue + + cm_file.write("SET(RT_{:s}_LINK_DIRS\n".format(group['name'].upper())) + for f in group['LIBPATH']: + cm_file.write("\t"+ f.replace("\\", "/") + "\n" ) + cm_file.write(")\n\n") + + cm_file.write("# Library local macro definitions\n") + for group in libgroups: + if not 'LOCAL_CPPDEFINES' in group.keys(): + continue + + if len(group['LOCAL_CPPDEFINES']) == 0: + continue + + cm_file.write("SET(RT_{:s}_DEFINES\n".format(group['name'].upper())) + for f in group['LOCAL_CPPDEFINES']: + cm_file.write("\t"+ f.replace("\\", "/") + "\n" ) + cm_file.write(")\n\n") + + cm_file.write("# Library dependencies\n") + for group in libgroups + interfacelibgroups: + if not 'LIBS' in group.keys(): + continue + + if len(group['LIBS']) == 0: + continue + + cm_file.write("SET(RT_{:s}_LIBS\n".format(group['name'].upper())) + for f in group['LIBS']: + cm_file.write("\t"+ "{}\n".format(f.replace("\\", "/"))) + cm_file.write(")\n\n") + + cm_file.write("# Libraries\n") + for group in libgroups: + cm_file.write("ADD_LIBRARY(rtt_{:s} OBJECT ${{RT_{:s}_SOURCES}})\n" + .format(group['name'], group['name'].upper())) + + cm_file.write("\n") + + cm_file.write("# Interface libraries\n") + for group in interfacelibgroups: + cm_file.write("ADD_LIBRARY(rtt_{:s} INTERFACE)\n".format(group['name'])) + + cm_file.write("\n") + + cm_file.write("# Private macros\n") + for group in libgroups: + if not 'LOCAL_CPPDEFINES' in group.keys(): + continue + + if len(group['LOCAL_CPPDEFINES']) == 0: + continue + + cm_file.write("TARGET_COMPILE_DEFINITIONS(rtt_{:s} PRIVATE ${{RT_{:s}_DEFINES}})\n" + .format(group['name'], group['name'].upper())) + + cm_file.write("\n") + + cm_file.write("# Interface library search paths\n") + if rtconfig.PLATFORM in ['gcc']: + for group in libgroups: + if not 'LIBPATH' in group.keys(): + continue + + if len(group['LIBPATH']) == 0: + continue + + cm_file.write("TARGET_LINK_DIRECTORIES(rtt_{:s} INTERFACE ${{RT_{:s}_LINK_DIRS}})\n" + .format(group['name'], group['name'].upper())) + + for group in libgroups: + if not 'LIBS' in group.keys(): + continue + + if len(group['LIBS']) == 0: + continue + + cm_file.write("TARGET_LINK_LIBRARIES(rtt_{:s} INTERFACE ${{RT_{:s}_LIBS}})\n" + .format(group['name'], group['name'].upper())) + + cm_file.write("\n") + + cm_file.write("ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}.elf ${RT_APPLICATIONS_SOURCES})\n") + + cm_file.write("TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME}.elf\n") + for group in libgroups + interfacelibgroups: + cm_file.write("\trtt_{:s}\n".format(group['name'])) cm_file.write(")\n\n") - if rtconfig.PLATFORM in ['gcc']: - cm_file.write("LINK_DIRECTORIES(\n") - for group in project: - if 'LIBPATH' in group.keys(): - for f in group['LIBPATH']: - cm_file.write( "\t"+ f.replace("\\", "/") + "\n" ) - cm_file.write(")\n\n") - - cm_file.write("LINK_LIBRARIES(\n") - for group in project: - if 'LIBS' in group.keys(): - for f in group['LIBS']: - cm_file.write( "\t"+ "{}\n".format(f.replace("\\", "/"))) - cm_file.write(")\n\n") - - cm_file.write("ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}.elf ${PROJECT_SOURCES})\n") cm_file.write("ADD_CUSTOM_COMMAND(TARGET ${CMAKE_PROJECT_NAME}.elf POST_BUILD \n" + POST_ACTION + '\n)\n') - + # auto inclue `custom.cmake` for user custom settings custom_cmake = \ '''