Parcourir la source

Merge pull request #2502 from BernardXiong/branch_tools

[Tools] Add Makefile generation and Eclipse target
Bernard Xiong il y a 6 ans
Parent
commit
b1a98ebe46
5 fichiers modifiés avec 548 ajouts et 1 suppressions
  1. 13 0
      bsp/qemu-vexpress-a9/Makefile
  2. 7 1
      tools/building.py
  3. 299 0
      tools/eclipse.py
  4. 107 0
      tools/makefile.py
  5. 122 0
      tools/rtthread.mk

+ 13 - 0
bsp/qemu-vexpress-a9/Makefile

@@ -0,0 +1,13 @@
+phony := all
+all:
+
+include config.mk
+
+ifneq ($(MAKE_LIB),1)
+TARGET := rtthread.elf
+include src.mk
+endif
+
+$(if $(strip $(RTT_ROOT)),,$(error RTT_ROOT not defined))
+
+include $(RTT_ROOT)/tools/rtthread.mk

+ 7 - 1
tools/building.py

@@ -187,7 +187,7 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
     AddOption('--target',
                       dest = 'target',
                       type = 'string',
-                      help = 'set target project: mdk/mdk4/mdk5/iar/vs/vsc/ua/cdk/ses')
+                      help = 'set target project: mdk/mdk4/mdk5/iar/vs/vsc/ua/cdk/ses/makefile/eclipse')
     AddOption('--genconfig',
                 dest = 'genconfig',
                 action = 'store_true',
@@ -228,6 +228,8 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [
                 'cb':('keil', 'armcc'),
                 'ua':('gcc', 'gcc'),
                 'cdk':('gcc', 'gcc'),
+                'makefile':('gcc', 'gcc'),
+                'eclipse':('gcc', 'gcc'),
                 'ses' : ('gcc', 'gcc')}
     tgt_name = GetOption('target')
 
@@ -824,6 +826,10 @@ def GenTargetProject(program = None):
         from ses import SESProject
         SESProject(Env)
 
+    if GetOption('target') == 'makefile':
+        from makefile import TargetMakefile
+        TargetMakefile(Env)
+
 def EndBuilding(target, program = None):
     import rtconfig
 

+ 299 - 0
tools/eclipse.py

@@ -0,0 +1,299 @@
+import os
+import sys
+import glob
+
+from utils import *
+from utils import _make_path_relative
+from utils import xml_indent
+
+import xml.etree.ElementTree as etree
+from xml.etree.ElementTree import SubElement
+
+source_pattern = ['*.c', '*.cpp', '*.cxx', '*.s', '*.S', '*.asm']
+
+def OSPath(path):
+    import platform
+
+    if type(path) == type('str'):
+        if platform.system() == 'Windows':
+            return path.replace('/', '\\')
+        else:
+            return path.replace('\\', '/')
+    else:
+        if platform.system() == 'Windows':
+            return [item.replace('/', '\\') for item in path]
+        else:
+            return [item.replace('\\', '/') for item in path]
+
+def CollectPaths(paths):
+    all_paths = []
+
+    def ParentPaths(path):
+        ret = os.path.dirname(path)
+        if ret == path or ret == '':
+            return []
+
+        return [ret] + ParentPaths(ret)
+
+    for path in paths:
+        # path = os.path.abspath(path)
+        path = path.replace('\\', '/')
+        all_paths = all_paths + [path] + ParentPaths(path)
+
+    all_paths = list(set(all_paths))
+    return sorted(all_paths)
+
+'''
+Collect all of files under paths
+'''
+def CollectFiles(paths, pattern):
+    files = []
+    for path in paths:
+        if type(pattern) == type(''):
+            files = files + glob.glob(path + '/' + pattern)
+        else:
+            for item in pattern:
+                # print('--> %s' % (path + '/' + item))
+                files = files + glob.glob(path + '/' + item)
+
+    return sorted(files)
+
+def CollectAllFilesinPath(path, pattern):
+    files = []
+
+    for item in pattern:
+        files += glob.glob(path + '/' + item)
+
+    list = os.listdir(path)
+    if len(list):
+        for item in list:
+            if item.startswith('.'):
+                continue
+            if item == 'bsp':
+                continue
+
+            if os.path.isdir(os.path.join(path, item)):
+                files = files + CollectAllFilesinPath(os.path.join(path, item), pattern)
+    return files
+
+'''
+Exclude files from infiles
+'''
+def ExcludeFiles(infiles, files):
+    in_files  = set([OSPath(file) for file in infiles])
+    exl_files = set([OSPath(file) for file in files])
+
+    exl_files = in_files - exl_files
+
+    return exl_files
+
+def ExcludePaths(filepath, paths):
+    ret = []
+
+    files = os.listdir(filepath)
+    for file in files:
+        if file.startswith('.'):
+            continue
+
+        fullname = os.path.join(filepath, file)
+
+        if os.path.isdir(fullname):
+            # print(fullname)
+            if not fullname in paths:
+                ret = ret + [fullname]
+            else:
+                ret = ret + ExcludePaths(fullname, paths)
+
+    return ret
+
+def HandleToolOption(tools, env):
+    project = ProjectInfo(env)
+    BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
+
+    CPPDEFINES = project['CPPDEFINES']
+    paths = ['${ProjDirPath}/' + _make_path_relative(BSP_ROOT, os.path.normpath(i)).replace('\\', '/') for i in project['CPPPATH']]
+
+    for tool in tools:
+        if tool.get('id').find('c.compile') != 1:
+            options = tool.findall('option')
+            for option in options:
+                if option.get('id').find('c.compiler.include.paths') != -1:
+                    # find all of paths in this project
+                    include_paths = option.findall('listOptionValue')
+                    project_paths = []
+                    for item in include_paths:
+                        project_paths += [item.get('value')]
+
+                    if len(project_paths) > 0:
+                        cproject_paths = set(paths) - set(project_paths)
+                    else:
+                        cproject_paths = paths
+
+                    # print('c.compiler.include.paths')
+                    cproject_paths = sorted(cproject_paths)
+                    for item in cproject_paths:
+                        SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': item})
+
+                if option.get('id').find('c.compiler.defs') != -1:
+                    defs = option.findall('listOptionValue')
+                    project_defs = []
+                    for item in defs:
+                        project_defs += [item.get('value')]
+                    if len(project_defs) > 0:
+                        cproject_defs = set(CPPDEFINES) - set(project_defs)
+                    else:
+                        cproject_defs = CPPDEFINES
+
+                    # print('c.compiler.defs')
+                    cproject_defs = sorted(cproject_defs)
+                    for item in cproject_defs:
+                        SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': item})
+
+        if tool.get('id').find('c.linker') != -1:
+            options = tool.findall('option')
+            for option in options:
+                if option.get('id').find('c.linker.scriptfile') != -1:
+                    linker_script = 'link.lds'
+                    items = env['LINKFLAGS'].split(' ')
+                    if '-T' in items:
+                        linker_script = items[items.index('-T') + 1]
+                        linker_script = '${ProjDirPath}/' + linker_script
+
+                    # print('c.linker.scriptfile')
+                    listOptionValue = option.find('listOptionValue')
+                    if listOptionValue != None:
+                        listOptionValue.set('value', linker_script)
+                    else:
+                        SubElement(option, 'listOptionValue', {'builtIn': 'false', 'value': linker_script})
+
+                if option.get('id').find('c.linker.nostart') != -1:
+                    if env['LINKFLAGS'].find('-nostartfiles') != -1:
+                        option.set('value', 'true')
+                    else:
+                        option.set('value', 'false')
+
+    return
+
+def HandleRTTRoot(env):
+    bsp_root = env['BSP_ROOT']
+    rtt_root = env['RTT_ROOT']
+
+    if not rtt_root.startswith(bsp_root):
+        to_SubElement = True
+        # print('handle virtual root')
+
+        # always use '/' path separator
+        rtt_root = rtt_root.replace('\\', '/')
+
+        project = etree.parse('.project')
+        root = project.getroot()
+
+        linkedResources = root.find('linkedResources')
+        if linkedResources == None:
+            # add linkedResources
+            linkedResources = SubElement(root, 'linkedResources')
+            # print('add linkedResources')
+        else:
+            links = linkedResources.findall('link')
+            # search exist 'rt-thread' virtual folder
+            for link in links:
+                if link.find('name').text == 'rt-thread':
+                    # handle location
+                    to_SubElement = False
+                    location = link.find('location')
+                    location.text = rtt_root
+
+        if to_SubElement:
+            # print('to subelement for virtual folder')
+            link = SubElement(linkedResources, 'link')
+            name = SubElement(link, 'name')
+            name.text = 'rt-thread'
+            type = SubElement(link, 'type')
+            type.text = '2'
+            location = SubElement(link, 'location')
+            location.text = rtt_root
+
+        out = open('.project', 'w')
+        out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
+        xml_indent(root)
+        out.write(etree.tostring(root, encoding='utf-8'))
+        out.close()
+
+    return
+
+def TargetEclipse(env):
+    global source_pattern
+
+    print('Update eclipse setting...')
+
+    if not os.path.exists('.cproject'):
+        print('no eclipse CDT project found!')
+        return
+
+    HandleRTTRoot(env)
+
+    project = ProjectInfo(env)
+
+    all_paths = [OSPath(path) for path in CollectPaths(project['DIRS'])]
+    # print(all_paths)
+    bsp_root = os.path.abspath(env['BSP_ROOT'])
+
+    exclude_paths = ExcludePaths(bsp_root, all_paths)
+    paths = exclude_paths
+    exclude_paths = []
+    for path in paths:
+        # add bsp and libcpu folder and not collect source files (too more files)
+        if path.endswith('rt-thread\\bsp') or path.endswith('rt-thread\\libcpu'):
+            exclude_paths += [path]
+            continue
+
+        set = CollectAllFilesinPath(path, source_pattern)
+        if len(set):
+            exclude_paths += [path]
+
+    exclude_paths = [_make_path_relative(bsp_root, path).replace('\\', '/') for path in exclude_paths]
+    env['ExPaths'] = exclude_paths
+
+    all_files = CollectFiles(all_paths, source_pattern)
+    src_files = project['FILES']
+
+    exclude_files = ExcludeFiles(all_files, src_files)
+    exclude_files = [_make_path_relative(bsp_root, file).replace('\\', '/') for file in exclude_files]
+    env['ExFiles'] = exclude_files
+
+    cproject = etree.parse('.cproject')
+
+    root = cproject.getroot()
+    cconfigurations = root.findall('storageModule/cconfiguration')
+    for cconfiguration in cconfigurations:
+        tools = cconfiguration.findall('storageModule/configuration/folderInfo/toolChain/tool')
+        HandleToolOption(tools, env)
+
+        sourceEntries = cconfiguration.find('storageModule/configuration/sourceEntries')
+        entry = sourceEntries.find('entry')
+        if entry != None:
+            sourceEntries.remove(entry)
+
+        excluding = exclude_paths + exclude_files
+        excluding = sorted(excluding)
+        value = ''
+        for item in excluding:
+            if value == '':
+                value = item
+            else:
+                value += '|' + item
+        excluding = value
+
+        SubElement(sourceEntries, 'entry', {'excluding': excluding, 'flags': 'VALUE_WORKSPACE_PATH|RESOLVED', 'kind':'sourcePath', 'name':""})
+
+    # write back to .cproject
+    out = open('.cproject', 'w')
+    out.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
+    out.write('<?fileVersion 4.0.0?>')
+    xml_indent(root)
+    out.write(etree.tostring(root, encoding='utf-8'))
+    out.close()
+
+    print('done!')
+
+    return

+ 107 - 0
tools/makefile.py

@@ -0,0 +1,107 @@
+import os
+import sys
+
+from utils import *
+from utils import _make_path_relative
+import rtconfig
+
+def TargetMakefile(env):
+    project = ProjectInfo(env)
+
+    BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
+    RTT_ROOT = os.path.abspath(env['RTT_ROOT'])
+
+    match_bsp = False
+    if BSP_ROOT.startswith(RTT_ROOT): 
+        match_bsp = True
+
+    make = open('config.mk', 'w')
+
+    make.write('BSP_ROOT ?= %s\n' % BSP_ROOT.replace('\\', '\\\\'))
+    make.write('RTT_ROOT ?= %s\n' % RTT_ROOT.replace('\\', '\\\\'))
+    make.write('\n')
+
+    cross = os.path.abspath(rtconfig.EXEC_PATH)
+    cross = os.path.join(cross, rtconfig.PREFIX)
+    make.write('CROSS_COMPILE ?=%s' % cross.replace('\\', '\\\\'))
+    make.write('\n')
+    make.write('\n')
+
+    make.write('CFLAGS :=%s' % (rtconfig.CFLAGS))
+    make.write('\n')
+    make.write('AFLAGS :=%s' % (rtconfig.AFLAGS))
+    make.write('\n')
+    make.write('LFLAGS :=%s' % (rtconfig.LFLAGS))
+    make.write('\n')
+    if 'CXXFLAGS' in dir(rtconfig):
+        make.write('CXXFLAGS :=%s' % (rtconfig.CXXFLAGS))
+        make.write('\n')
+
+    make.write('\n')
+
+    Files   = project['FILES']
+    Headers = project['HEADERS']
+    CPPDEFINES = project['CPPDEFINES']
+
+    paths = [os.path.normpath(i) for i in project['CPPPATH']]
+    CPPPATH = []
+    for path in paths:
+        fn = os.path.normpath(path)
+        if match_bsp:
+            if fn.startswith(BSP_ROOT):
+                fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
+            elif fn.startswith(RTT_ROOT):
+                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
+        else:
+            if fn.startswith(RTT_ROOT):
+                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
+            elif fn.startswith(BSP_ROOT):
+                fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
+
+        CPPPATH.append(fn)
+
+    path = ''
+    paths = CPPPATH
+    for item in paths:
+        path += '\t-I%s \\\n' % item
+
+    make.write('CPPPATHS :=')
+    if path[0] == '\t': path = path[1:]
+    length = len(path)
+    if path[length - 2] == '\\': path = path[:length - 2]
+    make.write(path)
+    make.write('\n')
+    make.write('\n')
+
+    defines = ''
+    for item in project['CPPDEFINES']:
+        defines += ' -D%s' % item
+    make.write('DEFINES :=')
+    make.write(defines)
+    make.write('\n')
+
+    files = Files
+    Files = []
+    for file in files:
+        fn = os.path.normpath(file)
+        if match_bsp:
+            if fn.startswith(BSP_ROOT):
+                fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
+            elif fn.startswith(RTT_ROOT):
+                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
+        else:
+            if fn.startswith(RTT_ROOT):
+                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
+            elif fn.startswith(BSP_ROOT):
+                fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
+
+        Files.append(fn)
+        # print(fn)
+
+    src = open('src.mk', 'w')
+    files = Files
+    src.write('SRC_FILES :=\n')
+    for item in files:
+        src.write('SRC_FILES +=%s\n' % item)
+
+    return

+ 122 - 0
tools/rtthread.mk

@@ -0,0 +1,122 @@
+$(if $(strip $(TARGET)),,$(error TARGET not defined))
+$(if $(strip $(SRC_FILES)),,$(error No source files))
+$(if $(strip $(BSP_ROOT)),,$(error BSP_ROOT not defined))
+
+ifneq ($(MAKE_LIB),1)
+BUILD_DIR := $(BSP_ROOT)/build
+endif
+
+$(if $(strip $(BUILD_DIR)),,$(error BUILD_DIR not defined))
+
+RTT_BUILD_DIR := .
+BSP_BUILD_DIR := bsp
+
+#################
+
+define add_c_file
+$(eval C_SRC := $(1:$(BSP_ROOT)/%=%)) \
+$(eval C_SRC := $(C_SRC:$(RTT_ROOT)/%=%)) \
+$(eval COBJ := $(1:%.c=%.o)) \
+$(eval COBJ := $(COBJ:$(BSP_ROOT)/%=$(BSP_BUILD_DIR)/%)) \
+$(eval COBJ := $(COBJ:$(RTT_ROOT)/%=$(RTT_BUILD_DIR)/%)) \
+$(eval LOCALC := $(addprefix $(BUILD_DIR)/,$(COBJ))) \
+$(eval OBJS += $(LOCALC)) \
+$(if $(strip $(LOCALC)),$(eval $(LOCALC): $(C_SRC)
+	@if [ ! -d $$(@D) ]; then mkdir -p $$(@D); fi
+	@echo cc $$<
+	@$(CROSS_COMPILE)gcc $$(CFLAGS) -c $$< -o $$@))
+endef
+
+define add_cxx_file
+$(eval CXX_SRC := $(1:$(BSP_ROOT)/%=%)) \
+$(eval CXX_SRC := $(CXX_SRC:$(RTT_ROOT)/%=%)) \
+$(eval CXXOBJ := $(1:%.cpp=%.o)) \
+$(eval CXXOBJ := $(CXXOBJ:$(BSP_ROOT)/%=$(BSP_BUILD_DIR)/%)) \
+$(eval CXXOBJ := $(CXXOBJ:$(RTT_ROOT)/%=$(RTT_BUILD_DIR)/%)) \
+$(eval LOCALCXX := $(addprefix $(BUILD_DIR)/,$(CXXOBJ))) \
+$(eval OBJS += $(LOCALCXX)) \
+$(if $(strip $(LOCALCXX)),$(eval $(LOCALCXX): $(CXX_SRC)
+	@if [ ! -d $$(@D) ]; then mkdir -p $$(@D); fi
+	@echo cc $$<
+	@$(CROSS_COMPILE)g++ $$(CXXFLAGS) -c $$< -o $$@))
+endef
+
+define add_S_file
+$(eval S_SRC := $(1:$(BSP_ROOT)/%=%)) \
+$(eval S_SRC := $(S_SRC:$(RTT_ROOT)/%=%)) \
+$(eval SOBJ := $(1:%.S=%.o)) \
+$(eval SOBJ := $(SOBJ:$(BSP_ROOT)/%=$(BSP_BUILD_DIR)/%)) \
+$(eval SOBJ := $(SOBJ:$(RTT_ROOT)/%=$(RTT_BUILD_DIR)/%)) \
+$(eval LOCALS := $(addprefix $(BUILD_DIR)/,$(SOBJ))) \
+$(eval OBJS += $(LOCALS)) \
+$(if $(strip $(LOCALS)),$(eval $(LOCALS): $(S_SRC)
+	@if [ ! -d $$(@D) ]; then mkdir -p $$(@D); fi
+	@echo cc $$<
+	@$(CROSS_COMPILE)gcc $$(AFLAGS) -c $$< -o $$@))
+endef
+
+add_flg = $(eval CFLAGS += $1) \
+          $(eval AFLAGS += $1) \
+          $(eval CXXFLAGS += $1)
+
+add_inc = $(eval CFLAGS += -I$1) \
+          $(eval AFLAGS += -I$1) \
+          $(eval CXXFLAGS += -I$1)
+
+add_def = $(eval CFLAGS += -D$1) \
+          $(eval AFLAGS += -D$1) \
+          $(eval CXXFLAGS += -D$1)
+
+OBJS := 
+#VPATH := $(BSP_ROOT) $(RTT_ROOT)
+VPATH := $(RTT_ROOT)
+
+CONFIG_FLG := $(strip $(EXTERN_FLAGS))
+$(if $(CONFIG_FLG),$(foreach f,$(CONFIG_FLG),$(call add_flg,$(f))))
+
+CONFIG_DEF := $(strip $(PROJECT_DEFS))
+$(if $(CONFIG_DEF),$(foreach d,$(CONFIG_DEF),$(call add_def,$(d))))
+
+CONFIG_INC := $(strip $(INCLUDE_PATH))
+$(if $(CONFIG_INC),$(foreach i,$(CONFIG_INC),$(call add_inc,$(i))))
+
+SRCS := $(strip $(filter %.c,$(SRC_FILES)))
+$(if $(SRCS),$(foreach f,$(SRCS),$(call add_c_file,$(f))))
+
+SRCS := $(strip $(filter %.cpp,$(SRC_FILES)))
+$(if $(SRCS),$(foreach f,$(SRCS),$(call add_cxx_file,$(f))))
+
+SRCS := $(strip $(filter %.S,$(SRC_FILES)))
+$(if $(SRCS),$(foreach f,$(SRCS),$(call add_S_file,$(f))))
+
+CFLAGS += $(CPPPATHS)
+CXXFLAGS += $(CPPPATHS)
+AFLAGS += $(CPPPATHS)
+
+CFLAGS += $(DEFINES)
+CXXFLAGS += $(DEFINES)
+AFLAGS += $(DEFINES)
+
+all: $(TARGET)
+
+ifeq ($(MAKE_LIB),1)
+$(TARGET): $(OBJS)
+	@echo ------------------------------------------------
+	@echo ar $(TARGET)
+	@$(CROSS_COMPILE)ar -rv $@ $(OBJS)
+else
+$(TARGET): $(OBJS) $(EXTERN_LIB)
+	@echo ------------------------------------------------
+	@echo link $(TARGET)
+	@$(CROSS_COMPILE)g++ -o $@ $(LFLAGS) $(OBJS) $(EXTERN_LIB) -lc -lm
+	@echo ------------------------------------------------
+	@$(CROSS_COMPILE)objcopy -O binary $@ rtthread.bin
+	@$(CROSS_COMPILE)size $@
+endif
+
+phony += clean
+clean:
+	@echo clean
+	@rm -rf $(TARGET) $(BUILD_DIR)
+
+.PHONY: $(phony)