# Path: rawe/utils/codegen.py
# def makeJobs():
# def memoizeFiles(genfiles,prefix=''):
#         def doAssertions(blah):
# def writeDifferentFiles(path,gfs):
#             def writeFile():
# def _getExportPath(genfiles,prefix):
# def flattenFileDict(fd,prefix=''):
# def directoryToDict(top):
# def withTempdir(command):
# def writeCCode(f, name):
#     def callme(tmpdir):
# 
# Path: rawe/utils/subprocess_tee.py
# def call(args, cwd = '.'):


import ctypes
import os
import rtModelExport
import rtIntegratorInterface
from multiprocessing import Process, Queue
from ..utils import codegen, subprocess_tee
\t@echo CXX $@: $(CXX) $(CXXFLAGS) -c $< -o $@
\t@$(CXX) $(CXXFLAGS) -c $< -o $@

%%.so : $(OBJ)
\t@echo LD $@: $(CXX) -shared -o $@ $(OBJ) $(LDFLAGS)
\t@$(CXX) -shared -o $@ $(OBJ) $(LDFLAGS)

integrator.a : $(OBJ)
\t@echo AR $@ : ar r $@ $?
\t@ar r $@ $?

integrator.o : $(OBJ)
\t@echo ld $@ : ld -r $? -o $@
\t@ld -r $? -o $@

clean :
\trm -f *.o *.so
""" % {'cfiles':' '.join(cfiles), 'cxxfiles':' '.join(cxxfiles),
       'cc': cgOptions['CC'], 'cxx': cgOptions['CXX'],
       'cflags': cgOptions['CFLAGS'], 'cxxflags': cgOptions['CXXFLAGS']
       }


def writeRtIntegrator(dae, options, measurements):
    # write the exporter file
    files = {'export_integrator.cpp': rtIntegratorInterface.phase1src(dae, options, measurements),
             'Makefile': rtIntegratorInterface.phase1makefile()}
    interfaceDir = codegen.memoizeFiles(files, prefix='rt_integrator_phase1__')

    # call make to make sure shared lib is build

# Path: rawe/utils/codegen.py
# def makeJobs():
# def memoizeFiles(genfiles,prefix=''):
#         def doAssertions(blah):
# def writeDifferentFiles(path,gfs):
#             def writeFile():
# def _getExportPath(genfiles,prefix):
# def flattenFileDict(fd,prefix=''):
# def directoryToDict(top):
# def withTempdir(command):
# def writeCCode(f, name):
#     def callme(tmpdir):
# 
# Path: rawe/utils/subprocess_tee.py
# def call(args, cwd = '.'):


# Path: rawe/rtIntegrator/rtIntegratorExport.py
import ctypes
import os
import rtModelExport
import rtIntegratorInterface
from multiprocessing import Process, Queue
from ..utils import codegen, subprocess_tee

\t@echo CXX $@: $(CXX) $(CXXFLAGS) -c $< -o $@
\t@$(CXX) $(CXXFLAGS) -c $< -o $@

%%.so : $(OBJ)
\t@echo LD $@: $(CXX) -shared -o $@ $(OBJ) $(LDFLAGS)
\t@$(CXX) -shared -o $@ $(OBJ) $(LDFLAGS)

integrator.a : $(OBJ)
\t@echo AR $@ : ar r $@ $?
\t@ar r $@ $?

integrator.o : $(OBJ)
\t@echo ld $@ : ld -r $? -o $@
\t@ld -r $? -o $@

clean :
\trm -f *.o *.so
""" % {'cfiles':' '.join(cfiles), 'cxxfiles':' '.join(cxxfiles),
       'cc': cgOptions['CC'], 'cxx': cgOptions['CXX'],
       'cflags': cgOptions['CFLAGS'], 'cxxflags': cgOptions['CXXFLAGS']
       }


def writeRtIntegrator(dae, options, measurements):
    # write the exporter file
    files = {'export_integrator.cpp': rtIntegratorInterface.phase1src(dae, options, measurements),
             'Makefile': rtIntegratorInterface.phase1makefile()}
    interfaceDir = codegen.memoizeFiles(files, prefix='rt_integrator_phase1__')

    # call make to make sure shared lib is build

    (ret, msgs) = subprocess_tee.call(['make',codegen.makeJobs()], cwd=interfaceDir)