#!/usr/bin/env python
import os
import re

TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
CHAL_DIR = os.path.join(os.path.dirname(TOOLS_DIR), 'cqe-challenges')

#reads in old makefile from folder
#parses for compiler arguments 
#creates cmake lists file with parsed arguments as parent-scope variables
def readAndMake(folder):
    inStream = open(folder+"/Makefile", "r")
    oldMake = inStream.readlines()
    inStream.close()

    makeVars = {'CFLAGS': '-fno-builtin -Wno-int-to-pointer-cast -Wno-writable-strings -nostdinc '}
    for line in oldMake:
        line = re.sub('[\r\n\t]', '', line)
        if "=" in line:
            var, val = line.split('=', 1)
            var = var.strip()
            val = val.strip()
            if var == "CFLAGS":
                makeVars[var] += val.replace('-Werror', '')
            else:
                makeVars[var] = val

    newMake = ""
    for var, val in makeVars.iteritems():
        newMake += 'set( {} "{}" )\n'.format(var, val)
    newMake += 'buildCB(${CFLAGS})'

    outStream = open(folder + "/CMakeLists.txt", "w")
    outStream.write(newMake)
    outStream.close()

#write makeFiles for all folders in path
def doAll(path):
    dirs = os.listdir(path)
    for folder in dirs:
        folder = path + "/" + folder
        #print folder
        if "00" in folder:
            print folder
            readAndMake(folder)

if __name__ == '__main__':
    doAll(CHAL_DIR)   ##path should be folder containing multiple challenge binaries and nothing else.

@@ -15,16 +15,28 @@
  python%0A
+import glob%0A
 import o
@@ -178,241 +178,264 @@
 ')%0A%0A
-#reads in old makefile from folder%0A#parses for compiler arguments %0A#creates cm
+%0Adef generate_cmake(path):%0A    # Path to the new CM
 ake
- 
 lists
- file with parsed arguments as parent-scope variables%0Adef readAndMake(folder):%0A    inStream = open(folder+%22/Makefile%22, %22r%22)%0A
+.txt%0A    cmake_path = os.path.join(os.path.dirname(path), 'CMakeLists.txt')%0A    print('Generating: %7B%7D'.format(cmake_path))%0A%0A    # Read in the Makefile%0A    with open(path) as f:%0A 
     
+   
 old
-M
+_m
 ake = 
-inStream
+f
 .rea
@@ -447,29 +447,40 @@
 s()%0A
+%0A
     
-inStream.close()%0A
+# Default values for CFLAGS
 %0A   
@@ -484,17 +484,18 @@
     make
-V
+_v
 ars = %7B'
@@ -596,17 +596,18 @@
 e in old
-M
+_m
 ake:%0A   
@@ -646,16 +646,67 @@
 ', line)
+%0A%0A        # Parse out any variables in the Makefile
 %0A       
@@ -815,32 +815,90 @@
 al = val.strip()
+%0A%0A            # Keep the CFLAGS that have already been set
 %0A            if 
@@ -926,33 +926,34 @@
             make
-V
+_v
 ars%5Bvar%5D += val.
@@ -1013,17 +1013,18 @@
     make
-V
+_v
 ars%5Bvar%5D
@@ -1031,28 +1031,56 @@
  = val%0A%0A    
-newM
+# Generate the CMake data%0A    cm
 ake = %22%22%0A   
@@ -1104,9 +1104,10 @@
 make
-V
+_v
 ars.
@@ -1123,28 +1123,26 @@
 ():%0A        
-newM
+cm
 ake += 'set(
@@ -1176,20 +1176,18 @@
 al)%0A    
-newM
+cm
 ake += '
@@ -1215,490 +1215,243 @@
     
-outStream = open(folder + %22/
+# Write the 
 CMake
-L
+l
 ists
-.txt%22, %22w%22)%0A    outStream.write(newMake)%0A    outStream.close()%0A%0A#write makeFiles for all folders in path%0Adef doAll(path):%0A    dirs = os.listdir(path)%0A    for folder in dirs:%0A        folder = path + %22/%22 + folder%0A        #print folder%0A        if %2200%22 in folder:%0A            print folder%0A            readAndMake(folder)%0A%0Aif __name__ == '__main__':%0A    doAll(CHAL_DIR)   ##path should be folder containing multiple challenge binaries and nothing else.
+%0A    with open(cmake_path, 'w') as f:%0A        f.write(cmake)%0A%0A%0Adef main():%0A    makefiles = glob.glob(os.path.join(CHAL_DIR, '*', 'Makefile'))%0A    map(generate_cmake, makefiles)%0A%0A%0Aif __name__ == '__main__':%0A    main()
 %0A

Refactor makefiles.py