import os
import sys
import glob
import SCons
import shutil

def CheckX86(context):
	return context.TryCompile("#ifndef __i386__\n#error\n#endif\nint main(){}", '.c')

def CheckX64(context):
	return context.TryCompile("#ifndef __amd64__\n#error\n#endif\nint main(){}", '.c')

Import('env flags_force32 platform client server')


#DETECT ARCHITECTURE
conf = Configure(env)
if int(flags_force32) or CheckX86(conf):
	print 'cpu: X86'
	env.Append(CXXFLAGS = ['-D__X86__', '-DLIB_ARCH=\"\\"X86\\"\"'])
	env.Append(CCFLAGS = ['-D__X86__', '-DLIB_ARCH=\"\\"X86\\"\"'])
	arch='X86'
elif CheckX64(conf):
	print 'cpu: X64'
	env.Append(CXXFLAGS = ['-D__X64__', '-DLIB_ARCH=\"\\"X64\\"\"'])
	env.Append(CCFLAGS = ['-D__X64__', '-DLIB_ARCH=\"\\"X64\\"\"'])
	arch='X64'
else:
	print 'cpu: Unknown'
	exit(-1)
env.Append(CXXFLAGS = '-DSERVER_HOST=\"\\"' + server + '\\"\"')


#Set Flags
if platform == 'linux':
    #libname = libname + "." + versionMajor + "." + versionMinor
    env.Append(CCFLAGS = '-DLINUX')
    env.Append(CXXFLAGS = '-DLINUX')
elif platform == 'win32':
    env.Append(CCFLAGS = '-DWIN32')
    env.Append(CXXFLAGS = '-DWIN32')
    if arg_ltcg == '1':
      env.Append(CCFLAGS = '/GL')
      env.Append(CXXFLAGS = '/GL')
      env.Append(LINKFLAGS = '/LTCG')


env.Append(CCFLAGS = '-DLIB_CLIENT="\\"' + client + '\\""')
env.Append(CXXFLAGS = '-DLIB_CLIENT="\\"' + client + '\\""')


#OPTIMIZATION FLAGS
if platform == 'linux':
    env.Append(CCFLAGS = '-O3')
    env.Append(CXXFLAGS = '-O3')
elif platform == 'win32':
    env.Append(CCFLAGS = '/O2 /GL')
    env.Append(CXXFLAGS = '/O2 /GL')

#BUILD LIB OBJECTS
Export('env')
libutf = []

libutf = SConscript('utf/SConscript')

CCFLAGS = ''

#Build The Lib
flags_unittest = 0
Export('env libutf CCFLAGS platform arch flags_unittest')
obj = []

obj += SConscript('utf/SConscript')
obj += SConscript('cpuinfo/SConscript')
obj += SConscript('meminfo/SConscript')
obj += SConscript('protocol/SConscript')
obj += SConscript('fsinfo/SConscript')
obj += SConscript('osinfo/SConscript')

if platform == 'win32':
    obj += env.SharedObject("windriver.cpp")
else:
    obj += env.SharedObject("lindriver.cpp")

bin = env.Library(obj)
Return('bin')
