# vim: set sts=2 ts=8 sw=2 tw=99 et ft=python:
import os

for cxx in builder.targets:
  arch = cxx.target.arch
  binary = SM.ExtLibrary(builder, cxx, 'dbi.pgsql.ext')
  compiler = binary.compiler
  compiler.cxxincludes += [
    os.path.join(SM.mms_root, 'core', 'sourcehook')
  ]
  if compiler.family == 'gcc' or compiler.family == 'clang':
    compiler.cxxflags += ['-fno-rtti']
  elif compiler.family == 'msvc':
    compiler.cxxflags += ['/GR-']

  if compiler.target.platform == 'linux' or compiler.target.platform == 'mac':
    compiler.postlink += [
      '-lz',
      '-lpthread',
      '-lm'
    ]
  if compiler.target.platform == 'linux':
    if arch == 'x86':
      path = os.path.join(builder.sourcePath, 'extensions', 'pgsql', 'lib_linux')
    elif arch == 'x86_64':
      path = os.path.join(builder.sourcePath, 'extensions', 'pgsql', 'lib_linux64')
    compiler.postlink += [
      os.path.join(path, 'libpq.a'),
      '-lrt'
    ]
  elif compiler.target.platform == 'windows':
    if arch == 'x86':
      path = os.path.join(builder.sourcePath, 'extensions', 'pgsql', 'lib_win')
    elif arch == 'x86_64':
      path = os.path.join(builder.sourcePath, 'extensions', 'pgsql', 'lib_win64')
    compiler.postlink += [
      os.path.join(path, 'libpq.lib'),
      'wsock32.lib',
      'ws2_32.lib',
      'secur32.lib'
    ]
  elif compiler.target.platform == 'mac':
    if arch == 'x86':
      path = os.path.join(builder.sourcePath, 'extensions', 'pgsql', 'lib_darwin')
    elif arch == 'x86_64':
      path = os.path.join(builder.sourcePath, 'extensions', 'pgsql', 'lib_darwin64')
    compiler.postlink += [
      os.path.join(path, 'libpq.a')
    ]

  compiler.cxxincludes += [path]

  binary.sources += [
    os.path.join('..', '..', 'public', 'smsdk_ext.cpp'),
    os.path.join('pgsql', 'PgBasicResults.cpp'),
    os.path.join('pgsql', 'PgDatabase.cpp'),
    os.path.join('pgsql', 'PgDriver.cpp'),
    os.path.join('pgsql', 'PgStatement.cpp'),
    'extension.cpp'
  ]
  SM.extensions += [builder.Add(binary)]

