#!/usr/bin/python
import sys,os
MOZBUILD = sys.argv[1]
CXX = os.getenv("CXX")
if CXX == None:
	CXX = ""
else:
	CXX="CXX='%s'" % CXX
cpu_count = 4
ext = "o"
cmd = "find %s -name \\*.%s" % (MOZBUILD, ext)
ofiles = os.popen(cmd).read().rstrip().split("\n")
print cmd
ifiles = [file[0:len(file) - len(ext)] + "i" for file in ofiles]
idirs = {}
print ofiles
for file in ifiles:
  (dir, name) = os.path.split(file)
  if not idirs.has_key(dir):
    idirs[dir] = [name]
  else:
    idirs[dir].append(name)
for directory, files in idirs.iteritems():
  os.system("make %s -j %i -C %s %s" % (CXX, cpu_count, directory, " ".join(files)))
