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

project = builder.LibraryProject('game.cstrike.ext')
project.sources += [
  'extension.cpp',
  'natives.cpp',
  'RegNatives.cpp',
  'timeleft.cpp',
  'forwards.cpp',
  'util_cstrike.cpp',
  '../../public/smsdk_ext.cpp',
  '../../public/CDetour/detours.cpp',
  '../../public/asm/asm.c',
  '../../public/libudis86/decode.c',
  '../../public/libudis86/itab.c',
  '../../public/libudis86/syn-att.c',
  '../../public/libudis86/syn-intel.c',
  '../../public/libudis86/syn.c',
  '../../public/libudis86/udis86.c',
]

for sdk_name in ['css', 'csgo']:
  if sdk_name not in SM.sdks:
    continue
  sdk = SM.sdks[sdk_name]

  if sdk_name == 'csgo':
    project.sources += ['rulesfix.cpp']
  
  for cxx in builder.targets:
    if not cxx.target.arch in sdk.platformSpec[cxx.target.platform]:
      continue

    cxx.defines += ['HAVE_STRING_H']
    binary = SM.HL2ExtConfig(project, builder, cxx, 'game.cstrike.ext.' + sdk.ext, sdk)
    if sdk_name == 'csgo':
      compiler = binary.compiler
      compiler.cxxincludes += [os.path.join(sdk.path, 'public', 'steam')]
      compiler.defines += ['VERSION_SAFE_STEAM_API_INTERFACES']

      library_name = ''
      if cxx.target.platform == 'windows':
        compiler.linkflags += [os.path.join(sdk.path, 'lib', 'public', 'steam_api.lib')]
      elif cxx.target.platform == 'linux':
        library_name = 'libsteam_api.so'
        if cxx.target.arch == 'x86_64':
          source_path = os.path.join(sdk.path, 'lib', 'linux64')
        else:
          source_path = os.path.join(sdk.path, 'lib', 'linux')
      elif cxx.target.platform == 'mac':
        library_name = 'libsteam_api.dylib'
        if cxx.target.arch == 'x86_64':
          source_path = os.path.join(sdk.path, 'lib', 'osx64')
        else:
          source_path = os.path.join(sdk.path, 'lib', 'mac')

      if library_name:
        source_path = os.path.join(source_path, library_name)
        output_path = os.path.join(binary.localFolder, library_name)

        # Ensure the output path exists.
        builder.AddFolder(binary.localFolder)
        output = builder.AddSymlink(source_path, output_path)

        compiler.weaklinkdeps += [output]
        compiler.linkflags[0:0] = [library_name]

SM.extensions += builder.Add(project)
