import os
import sys

from utils import *
from utils import _make_path_relative
import rtconfig

def TargetMakefile(env):
    project = ProjectInfo(env)

    make = open('config.mk', 'w')

    BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
    RTT_ROOT = os.path.abspath(env['RTT_ROOT'])

    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']

    path = ''
    paths = [_make_path_relative(BSP_ROOT, os.path.normpath(i)) for i in project['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')

    src = open('src.mk', 'w')
    files = [_make_path_relative(BSP_ROOT, os.path.normpath(i)) for i in Files]
    src.write('SRC_FILES :=\n')
    for item in files:
        src.write('SRC_FILES +=%s\n' % item)

    return

@@ -151,43 +151,8 @@
 v)%0A%0A
-    make = open('config.mk', 'w')%0A%0A
     
@@ -244,16 +244,138 @@
 OOT'%5D)%0A%0A
+    match_bsp = False%0A    if BSP_ROOT.startswith(RTT_ROOT): %0A        match_bsp = True%0A%0A    make = open('config.mk', 'w')%0A%0A
     make
@@ -1202,45 +1202,246 @@
 path
+s
  = 
-''%0A    paths = %5B_make_path_
+%5Bos.path.normpath(i) for i in project%5B'CPPPATH'%5D%5D%0A    CPPPATH = %5B%5D%0A    for path in paths:%0A        fn = os.path.normpath(path)%0A        if match_bsp:%0A            if fn.startswith(BSP_ROOT):%0A                fn = '$(BSP_ROOT)' + fn.
 re
+p
 la
-tiv
+c
 e(BS
@@ -1452,57 +1452,398 @@
 OT, 
-os.path.normpath(i)) for i in project%5B'
+'')%0A            elif fn.startswith(RTT_ROOT):%0A                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')%0A        else:%0A            if fn.startswith(RTT_ROOT):%0A                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')%0A            elif fn.startswith(BSP_ROOT):%0A                fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')%0A%0A        CPPPATH.append(fn)%0A%0A    path = ''%0A    paths = 
 CPPPATH
-'%5D%5D
 %0A   
@@ -2288,65 +2288,528 @@
     
-src = open('src.mk'
+files = Files%0A    Files = %5B%5D%0A    for file in files:%0A        fn = os.path.normpath(file)%0A        if match_bsp:%0A            if fn.startswith(BSP_ROOT):%0A                fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')%0A            elif fn.startswith(RTT_ROOT):%0A                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT
 , '
-w
 ')%0A    
+   
-files = %5B_make_path_
+ else:%0A            if fn.startswith(RTT_ROOT):%0A                fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')%0A            elif fn.startswith(BSP_ROOT):%0A                fn = '$(BSP_ROOT)' + fn.
 re
+p
 la
-tiv
+c
 e(BS
@@ -2820,44 +2820,106 @@
 OT, 
-os.path.normpath(i)) for i in
+'')%0A%0A        Files.append(fn)%0A        # print(fn)%0A%0A    src = open('src.mk', 'w')%0A    files =
  Files
-%5D
 %0A   

Add RTT_ROOT/BSP_ROOT for CPPPATH and source files