Beispiel #1
0
def createMakefile(workdir='.', libroot='../../../Library/', outdir='output', target='main.ihx', options=''):

  # print message to console  
  sys.stdout.write('creating Makefile ... ')
  sys.stdout.flush()

  # change to specified working directory
  os.chdir(workdir)

  # set project paths
  LIB_ROOT = libroot
  PRJ_ROOT = workdir
  OBJDIR   = outdir
  TARGET   = target

  # set command for creating dependencies and set search paths 
  CC       = 'sdcc '
  CFLAGS   = '-mstm8 --std-sdcc99 --opt-code-speed '+options+' '
  #CFLAGS   = '-mstm8 --std-sdcc99 --debug -DDEBUG '+options+' '
  LFLAGS   = '-mstm8 -lstm8 --out-fmt-ihx '
  DEPEND   = '-MM '
  INCLUDE  = '-I. '
  for dir in misc.listSubdirs(PRJ_ROOT):
    dir = dir.replace('\\','/')
    INCLUDE += '-I' + dir + ' '
  for dir in misc.listSubdirs(LIB_ROOT):
    dir = dir.replace('\\', '/')          # convert Windows path to POSIX for Makefile
    if os.path.basename(dir) == 'inc':    # include all 'inc' folders
      INCLUDE += '-I' + dir + ' '

  # get set of .c files in project folder incl. subdirectories
  source_todo = misc.listFiles(PRJ_ROOT,".c")
  source_done = set()
  header_done = set()
  object_done = set()

  # generate generic Makefile header
  Makefile = open('Makefile', 'wb')
  Makefile.write(('OBJDIR   = '+OBJDIR+'\n').encode())    # Python 3.x requires bytes, not string 
  Makefile.write(('TARGET   = '+TARGET+'\n\n').encode())
  Makefile.write(('.PHONY: clean all default objects\n\n').encode())
  Makefile.write(('.PRECIOUS: $(TARGET)\n\n').encode())
  Makefile.write(('default: $(OBJDIR) $(OBJDIR)/$(TARGET)\n\n').encode())
  Makefile.write(('all: default\n\n').encode())
  Makefile.write(('# create output folder\n').encode())
  Makefile.write(('$(OBJDIR):\n').encode())
  if platform.system() == 'Windows':
    Makefile.write(('	mkdir $(OBJDIR)\n').encode())
  else:
    Makefile.write(('	mkdir -p $(OBJDIR)\n').encode())
  Makefile.write(('\n').encode())

  # iteratively add project sources to Makefile
  Makefile.write(('# compile sources\n').encode())
  while (len(source_todo) > 0):

    # get next pending source and mark as done
    source = source_todo.pop()
    source_done.add(source)

    # convert Windows path to POSIX for Makefile
    source = source.replace('\\','/')

    # use compiler generate dependency list
    cmd = CC+DEPEND+CFLAGS+INCLUDE+source
    #print cmd
    exitcode, out, err = misc.executeCmd(cmd, verbose=1)
    if (exitcode != 0):
      misc.Exit(exitcode)
    
    # for Python 3.x need to explicitely convert bytes to str
    if (sys.version_info.major == 3):
      out = str(out, sys.stdout.encoding)

    # append .c file with dependency and compile instruction to Makefile
    Makefile.write(('$(OBJDIR)/'+out).encode())
    #print(out)
    Makefile.write(('\t'+CC+CFLAGS+INCLUDE+'-c $< -o $@\n\n').encode())
    
    # extract file list including object[0], source[1] and headers[2..N]
    out = out.replace(':', '')
    out = out.replace('\\', '')
    out = out.replace('\n', '')
    out = out.split()
    #print out

    # for all files returned by compiler...
    for next in out:
      
      # append object files for linker
      if next.endswith('.rel'):
        object_done.add(next)

      # if corresponding source to header exists, add to pending sources
      if next.endswith('.h'):
        if next not in header_done:           # not yet in list
          header_done.add(next)                 # add to treated headers
          next = (next[::-1].replace("/inc/"[::-1], "/src/"[::-1], 1))[::-1]  # replace last /inc/ by /src/ (see https://stackoverflow.com/questions/2556108/rreplace-how-to-replace-the-last-occurrence-of-an-expression-in-a-string)        
          if (os.path.isfile(next[:-1]+'c')):   # if corresponding .c exists, add to todo list
            source_todo.add(next[:-1]+'c')


  # link project object files
  Makefile.write(('# link sources\n').encode())
  Makefile.write(('$(OBJDIR)/$(TARGET): ').encode())
  for next in object_done:
    Makefile.write(('$(OBJDIR)/'+next+' ').encode())
  Makefile.write(('\n').encode())
  Makefile.write(('\t'+CC+LFLAGS).encode())
  for next in object_done:
    Makefile.write(('$(OBJDIR)/'+next+' ').encode())
  Makefile.write((' -o $@\n').encode())

  # close Makefile.dep
  Makefile.close()

  # print message to console  
  sys.stdout.write('done\n')
  sys.stdout.flush()
#!/usr/bin/python3
'''
 Find and execute all clean.py beneath this script
'''

# required modules
import sys
import os
sys.path.append('../Tools')
import misc

# execute all 'clean.py' below the current folder
oldDir = os.getcwd()
for file in misc.listFiles('.', "clean.py"):

    # change working directory
    os.chdir(os.path.dirname(file))

    # print progress
    print(os.path.dirname(file))

    # execute clean.py
    os.system('python clean.py')

    # restore working directory
    os.chdir(oldDir)

# optional prompt for return, then close window
if False:
    if (sys.version_info.major == 3):
        input("\npress return to exit ... ")
#!/usr/bin/python3
'''
 Find and execute all clean.py beneath this script
'''

# required modules
import sys
import os
import platform
sys.path.insert(0, '../Tools')
import misc

# execute all 'clean.py' below the current folder
oldDir = os.getcwd()
for file in misc.listFiles('.', "build_upload.py"):

    # change working directory
    os.chdir(os.path.dirname(file))

    # show progress
    sys.stdout.write('compile ' + os.path.dirname(file) + ' ... ')
    sys.stdout.flush()

    # build w/o upload
    if platform.system() == 'Windows':
        err = os.system(
            'python build_upload.py --skipupload --skipterminal --skippause > NUL'
        )
    else:
        err = os.system(
            'python build_upload.py --skipupload --skipterminal --skippause > /dev/null'