# Copyright 2010 the V8 project authors. All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of Google Inc. nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import platform
import re
import sys
import os
from os.path import join, dirname, abspath
from types import DictType, StringTypes
root_dir = dirname(File('SConstruct').rfile().abspath)
sys.path.insert(0, join(root_dir, 'tools'))
import js2c, utils

# ANDROID_TOP is the top of the Android checkout, fetched from the environment
# variable 'TOP'.   You will also need to set the CXX, CC, AR and RANLIB
# environment variables to the cross-compiling tools.
ANDROID_TOP = os.environ.get('TOP')
if ANDROID_TOP is None:
  ANDROID_TOP=""

# ARM_TARGET_LIB is the path to the dynamic library to use on the target
# machine if cross-compiling to an arm machine. You will also need to set
# the additional cross-compiling environment variables to the cross compiler.
ARM_TARGET_LIB = os.environ.get('ARM_TARGET_LIB')
if ARM_TARGET_LIB:
  ARM_LINK_FLAGS = ['-Wl,-rpath=' + ARM_TARGET_LIB + '/lib:' +
                     ARM_TARGET_LIB + '/usr/lib',
                    '-Wl,--dynamic-linker=' + ARM_TARGET_LIB +
                    '/lib/ld-linux.so.3']
else:
  ARM_LINK_FLAGS = []

GCC_EXTRA_CCFLAGS = []
GCC_DTOA_EXTRA_CCFLAGS = []

ANDROID_FLAGS = ['-march=armv7-a',
                 '-mtune=cortex-a8',
                 '-mfloat-abi=softfp',
                 '-mfpu=vfp',
                 '-fpic',
                 '-mthumb-interwork',
                 '-funwind-tables',
                 '-fstack-protector',
                 '-fno-short-enums',
                 '-fmessage-length=0',
                 '-finline-functions',
                 '-fno-inline-functions-called-once',
                 '-fgcse-after-reload',
                 '-frerun-cse-after-loop',
                 '-frename-registers',
                 '-fomit-frame-pointer',
                 '-finline-limit=64',
                 '-DCAN_USE_VFP_INSTRUCTIONS=1',
                 '-DCAN_USE_ARMV7_INSTRUCTIONS=1',
                 '-DCAN_USE_UNALIGNED_ACCESSES=1',
                 '-MD']

ANDROID_INCLUDES = [ANDROID_TOP + '/bionic/libc/arch-arm/include',
                    ANDROID_TOP + '/bionic/libc/include',
                    ANDROID_TOP + '/bionic/libstdc++/include',
                    ANDROID_TOP + '/bionic/libc/kernel/common',
                    ANDROID_TOP + '/bionic/libc/kernel/arch-arm',
                    ANDROID_TOP + '/bionic/libm/include',
                    ANDROID_TOP + '/bionic/libm/include/arch/arm',
                    ANDROID_TOP + '/bionic/libthread_db/include',
                    ANDROID_TOP + '/frameworks/base/include',
                    ANDROID_TOP + '/system/core/include']

ANDROID_LINKFLAGS = ['-nostdlib',
                     '-Bdynamic',
                     '-Wl,-T,' + ANDROID_TOP + '/build/core/armelf.x',
                     '-Wl,-dynamic-linker,/system/bin/linker',
                     '-Wl,--gc-sections',
                     '-Wl,-z,nocopyreloc',
                     '-Wl,-rpath-link=' + ANDROID_TOP + '/out/target/product/generic/obj/lib',
                     ANDROID_TOP + '/out/target/product/generic/obj/lib/crtbegin_dynamic.o',
                     ANDROID_TOP + '/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/interwork/libgcc.a',
                     ANDROID_TOP + '/out/target/product/generic/obj/lib/crtend_android.o'];

LIBRARY_FLAGS = {
  'all': {
    'CPPPATH': [join(root_dir, 'src')],
    'regexp:interpreted': {
      'CPPDEFINES': ['V8_INTERPRETED_REGEXP']
    },
    'mode:debug': {
      'CPPDEFINES': ['V8_ENABLE_CHECKS', 'OBJECT_PRINT']
    },
    'vmstate:on': {
      'CPPDEFINES':   ['ENABLE_VMSTATE_TRACKING'],
    },
    'objectprint:on': {
      'CPPDEFINES':   ['OBJECT_PRINT'],
    },
    'protectheap:on': {
      'CPPDEFINES':   ['ENABLE_VMSTATE_TRACKING', 'ENABLE_HEAP_PROTECTION'],
    },
    'profilingsupport:on': {
      'CPPDEFINES':   ['ENABLE_VMSTATE_TRACKING', 'ENABLE_LOGGING_AND_PROFILING'],
    },
    'debuggersupport:on': {
      'CPPDEFINES':   ['ENABLE_DEBUGGER_SUPPORT'],
    },
    'inspector:on': {
      'CPPDEFINES':   ['INSPECTOR'],
    },
    'liveobjectlist:on': {
      'CPPDEFINES':   ['ENABLE_DEBUGGER_SUPPORT', 'INSPECTOR',
                       'LIVE_OBJECT_LIST', 'OBJECT_PRINT'],
    }
  },
  'gcc': {
    'all': {
      'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS'],
      'CXXFLAGS':     ['$CCFLAGS', '-fno-rtti', '-fno-exceptions'],
    },
    'visibility:hidden': {
      # Use visibility=default to disable this.
      'CXXFLAGS':     ['-fvisibility=hidden']
    },
    'mode:debug': {
      'CCFLAGS':      ['-g', '-O0'],
      'CPPDEFINES':   ['ENABLE_DISASSEMBLER', 'DEBUG'],
      'os:android': {
        'CCFLAGS':    ['-mthumb']
      }
    },
    'mode:release': {
      'CCFLAGS':      ['-O3', '-fomit-frame-pointer', '-fdata-sections',
                       '-ffunction-sections'],
      'os:android': {
        'CCFLAGS':    ['-mthumb', '-Os'],
        'CPPDEFINES': ['SK_RELEASE', 'NDEBUG']
      }
    },
    'os:linux': {
      'CCFLAGS':      ['-ansi'] + GCC_EXTRA_CCFLAGS,
      'library:shared': {
        'CPPDEFINES': ['V8_SHARED'],
        'LIBS': ['pthread']
      }
    },
    'os:macos': {
      'CCFLAGS':      ['-ansi', '-mmacosx-version-min=10.4'],
      'library:shared': {
        'CPPDEFINES': ['V8_SHARED']
      }
    },
    'os:freebsd': {
      'CPPPATH' : ['/usr/local/include'],
      'LIBPATH' : ['/usr/local/lib'],
      'CCFLAGS':      ['-ansi'],
    },
    'os:openbsd': {
      'CPPPATH' : ['/usr/local/include'],
      'LIBPATH' : ['/usr/local/lib'],
      'CCFLAGS':      ['-ansi'],
    },
    'os:solaris': {
      # On Solaris, to get isinf, INFINITY, fpclassify and other macros one
      # needs to define __C99FEATURES__.
      'CPPDEFINES': ['__C99FEATURES__'],
      'CPPPATH' : ['/usr/local/include'],
      'LIBPATH' : ['/usr/local/lib'],
      'CCFLAGS':      ['-ansi'],
    },
    'os:win32': {
      'CCFLAGS':      ['-DWIN32'],
      'CXXFLAGS':     ['-DWIN32'],
    },
    'os:android': {
      'CPPDEFINES':   ['ANDROID', '__ARM_ARCH_5__', '__ARM_ARCH_5T__',
                       '__ARM_ARCH_5E__', '__ARM_ARCH_5TE__'],
      'CCFLAGS':      ANDROID_FLAGS,
      'WARNINGFLAGS': ['-Wall', '-Wno-unused', '-Werror=return-type',
                       '-Wstrict-aliasing=2'],
      'CPPPATH':      ANDROID_INCLUDES,
    },
    'arch:ia32': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_IA32'],
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32']
    },
    'arch:arm': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_ARM'],
      'unalignedaccesses:on' : {
        'CPPDEFINES' : ['CAN_USE_UNALIGNED_ACCESSES=1']
      },
      'unalignedaccesses:off' : {
        'CPPDEFINES' : ['CAN_USE_UNALIGNED_ACCESSES=0']
      }
    },
    'simulator:arm': {
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32'],
    },
    'arch:mips': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_MIPS'],
      'simulator:none': {
        'CCFLAGS':      ['-EL', '-mips32r2', '-Wa,-mips32r2', '-fno-inline'],
        'LDFLAGS':      ['-EL']
      }
    },
    'simulator:mips': {
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32'],
    },
    'arch:x64': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_X64'],
      'CCFLAGS':      ['-m64'],
      'LINKFLAGS':    ['-m64'],
    },
    'prof:oprofile': {
      'CPPDEFINES':   ['ENABLE_OPROFILE_AGENT']
    },
    'gdbjit:on': {
      'CPPDEFINES':   ['ENABLE_GDB_JIT_INTERFACE']
    }
  },
  'msvc': {
    'all': {
      'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS'],
      'CXXFLAGS':     ['$CCFLAGS', '/GR-', '/Gy'],
      'CPPDEFINES':   ['WIN32'],
      'LINKFLAGS':    ['/INCREMENTAL:NO', '/NXCOMPAT', '/IGNORE:4221'],
      'CCPDBFLAGS':   ['/Zi']
    },
    'verbose:off': {
      'DIALECTFLAGS': ['/nologo'],
      'ARFLAGS':      ['/NOLOGO']
    },
    'arch:ia32': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_IA32', '_USE_32BIT_TIME_T'],
      'LINKFLAGS':    ['/MACHINE:X86'],
      'ARFLAGS':      ['/MACHINE:X86']
    },
    'arch:x64': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_X64'],
      'LINKFLAGS':    ['/MACHINE:X64'],
      'ARFLAGS':      ['/MACHINE:X64']
    },
    'mode:debug': {
      'CCFLAGS':      ['/Od', '/Gm'],
      'CPPDEFINES':   ['_DEBUG', 'ENABLE_DISASSEMBLER', 'DEBUG'],
      'LINKFLAGS':    ['/DEBUG'],
      'msvcrt:static': {
        'CCFLAGS': ['/MTd']
      },
      'msvcrt:shared': {
        'CCFLAGS': ['/MDd']
      }
    },
    'mode:release': {
      'CCFLAGS':      ['/O2'],
      'LINKFLAGS':    ['/OPT:REF', '/OPT:ICF'],
      'msvcrt:static': {
        'CCFLAGS': ['/MT']
      },
      'msvcrt:shared': {
        'CCFLAGS': ['/MD']
      },
      'msvcltcg:on': {
        'CCFLAGS':      ['/GL'],
        'ARFLAGS':      ['/LTCG'],
        'pgo:off': {
          'LINKFLAGS':    ['/LTCG'],
        },
        'pgo:instrument': {
          'LINKFLAGS':    ['/LTCG:PGI']
        },
        'pgo:optimize': {
          'LINKFLAGS':    ['/LTCG:PGO']
        }
      }
    }
  }
}


V8_EXTRA_FLAGS = {
  'gcc': {
    'all': {
      'WARNINGFLAGS': ['-Wall',
                       '-Werror',
                       '-W',
                       '-Wno-unused-parameter',
                       '-Wnon-virtual-dtor']
    },
    'os:win32': {
      'WARNINGFLAGS': ['-pedantic', '-Wno-long-long']
    },
    'os:linux': {
      'WARNINGFLAGS': ['-pedantic'],
      'library:shared': {
        'soname:on': {
          'LINKFLAGS': ['-Wl,-soname,${SONAME}']
        }
      }
    },
    'os:macos': {
      'WARNINGFLAGS': ['-pedantic']
    },
    'disassembler:on': {
      'CPPDEFINES':   ['ENABLE_DISASSEMBLER']
    }
  },
  'msvc': {
    'all': {
      'WARNINGFLAGS': ['/W3', '/WX', '/wd4351', '/wd4355', '/wd4800']
    },
    'library:shared': {
      'CPPDEFINES': ['BUILDING_V8_SHARED'],
      'LIBS': ['winmm', 'ws2_32']
    },
    'arch:arm': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_ARM'],
      # /wd4996 is to silence the warning about sscanf
      # used by the arm simulator.
      'WARNINGFLAGS': ['/wd4996']
    },
    'arch:mips': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_MIPS'],
    },
    'disassembler:on': {
      'CPPDEFINES':   ['ENABLE_DISASSEMBLER']
    }
  }
}


MKSNAPSHOT_EXTRA_FLAGS = {
  'gcc': {
    'os:linux': {
      'LIBS': ['pthread'],
    },
    'os:macos': {
      'LIBS': ['pthread'],
    },
    'os:freebsd': {
      'LIBS': ['execinfo', 'pthread']
    },
    'os:solaris': {
      'LIBS': ['m', 'pthread', 'socket', 'nsl', 'rt'],
      'LINKFLAGS': ['-mt']
    },
    'os:openbsd': {
      'LIBS': ['execinfo', 'pthread']
    },
    'os:win32': {
      'LIBS': ['winmm', 'ws2_32'],
    },
  },
  'msvc': {
    'all': {
      'CPPDEFINES': ['_HAS_EXCEPTIONS=0'],
      'LIBS': ['winmm', 'ws2_32']
    }
  }
}


DTOA_EXTRA_FLAGS = {
  'gcc': {
    'all': {
      'WARNINGFLAGS': ['-Werror', '-Wno-uninitialized'],
      'CCFLAGS': GCC_DTOA_EXTRA_CCFLAGS
    }
  },
  'msvc': {
    'all': {
      'WARNINGFLAGS': ['/WX', '/wd4018', '/wd4244']
    }
  }
}


CCTEST_EXTRA_FLAGS = {
  'all': {
    'CPPPATH': [join(root_dir, 'src')],
  },
  'gcc': {
    'all': {
      'LIBPATH': [abspath('.')]
    },
    'os:linux': {
      'LIBS':         ['pthread'],
    },
    'os:macos': {
      'LIBS':         ['pthread'],
    },
    'os:freebsd': {
      'LIBS':         ['execinfo', 'pthread']
    },
    'os:solaris': {
      'LIBS':         ['m', 'pthread', 'socket', 'nsl', 'rt'],
      'LINKFLAGS':    ['-mt']
    },
    'os:openbsd': {
      'LIBS':         ['execinfo', 'pthread']
    },
    'os:win32': {
      'LIBS': ['winmm', 'ws2_32']
    },
    'os:android': {
      'CPPDEFINES':   ['ANDROID', '__ARM_ARCH_5__', '__ARM_ARCH_5T__',
                       '__ARM_ARCH_5E__', '__ARM_ARCH_5TE__'],
      'CCFLAGS':      ANDROID_FLAGS,
      'CPPPATH':      ANDROID_INCLUDES,
      'LIBPATH':     [ANDROID_TOP + '/out/target/product/generic/obj/lib',
                      ANDROID_TOP + '/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/interwork'],
      'LINKFLAGS':    ANDROID_LINKFLAGS,
      'LIBS':         ['log', 'c', 'stdc++', 'm', 'gcc'],
      'mode:release': {
        'CPPDEFINES': ['SK_RELEASE', 'NDEBUG']
      }
    },
    'arch:arm': {
      'LINKFLAGS':   ARM_LINK_FLAGS
    },
  },
  'msvc': {
    'all': {
      'CPPDEFINES': ['_HAS_EXCEPTIONS=0'],
      'LIBS': ['winmm', 'ws2_32']
    },
    'library:shared': {
      'CPPDEFINES': ['USING_V8_SHARED']
    },
    'arch:ia32': {
      'CPPDEFINES': ['V8_TARGET_ARCH_IA32']
    },
    'arch:x64': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_X64'],
      'LINKFLAGS': ['/STACK:2091752']
    },
  }
}


SAMPLE_FLAGS = {
  'all': {
    'CPPPATH': [join(abspath('.'), 'include')],
  },
  'gcc': {
    'all': {
      'LIBPATH': ['.'],
      'CCFLAGS': ['-fno-rtti', '-fno-exceptions']
    },
    'os:linux': {
      'LIBS':         ['pthread'],
    },
    'os:macos': {
      'LIBS':         ['pthread'],
    },
    'os:freebsd': {
      'LIBPATH' : ['/usr/local/lib'],
      'LIBS':     ['execinfo', 'pthread']
    },
    'os:solaris': {
      'LIBPATH' : ['/usr/local/lib'],
      'LIBS':     ['m', 'pthread', 'socket', 'nsl', 'rt'],
      'LINKFLAGS': ['-mt']
    },
    'os:openbsd': {
      'LIBPATH' : ['/usr/local/lib'],
      'LIBS':     ['execinfo', 'pthread']
    },
    'os:win32': {
      'LIBS':         ['winmm', 'ws2_32']
    },
    'os:android': {
      'CPPDEFINES':   ['ANDROID', '__ARM_ARCH_5__', '__ARM_ARCH_5T__',
                       '__ARM_ARCH_5E__', '__ARM_ARCH_5TE__'],
      'CCFLAGS':      ANDROID_FLAGS,
      'CPPPATH':      ANDROID_INCLUDES,
      'LIBPATH':     [ANDROID_TOP + '/out/target/product/generic/obj/lib',
                      ANDROID_TOP + '/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/interwork'],
      'LINKFLAGS':    ANDROID_LINKFLAGS,
      'LIBS':         ['log', 'c', 'stdc++', 'm', 'gcc'],
      'mode:release': {
        'CPPDEFINES': ['SK_RELEASE', 'NDEBUG']
      }
    },
    'arch:arm': {
      'LINKFLAGS':   ARM_LINK_FLAGS
    },
    'arch:ia32': {
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32']
    },
    'arch:x64': {
      'CCFLAGS':      ['-m64'],
      'LINKFLAGS':    ['-m64']
    },
    'arch:mips': {
      'CPPDEFINES':   ['V8_TARGET_ARCH_MIPS'],
      'simulator:none': {
        'CCFLAGS':      ['-EL', '-mips32r2', '-Wa,-mips32r2', '-fno-inline'],
        'LINKFLAGS':    ['-EL'],
        'LDFLAGS':      ['-EL']
      }
    },
    'simulator:arm': {
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32']
    },
    'simulator:mips': {
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32']
    },
    'mode:release': {
      'CCFLAGS':      ['-O2']
    },
    'mode:debug': {
      'CCFLAGS':      ['-g', '-O0'],
      'CPPDEFINES':   ['DEBUG']
    },
    'prof:oprofile': {
      'LIBPATH': ['/usr/lib32', '/usr/lib32/oprofile'],
      'LIBS': ['opagent']
    }
  },
  'msvc': {
    'all': {
      'LIBS': ['winmm', 'ws2_32']
    },
    'verbose:off': {
      'CCFLAGS': ['/nologo'],
      'LINKFLAGS': ['/NOLOGO']
    },
    'verbose:on': {
      'LINKFLAGS': ['/VERBOSE']
    },
    'library:shared': {
      'CPPDEFINES': ['USING_V8_SHARED']
    },
    'prof:on': {
      'LINKFLAGS': ['/MAP']
    },
    'mode:release': {
      'CCFLAGS':   ['/O2'],
      'LINKFLAGS': ['/OPT:REF', '/OPT:ICF'],
      'msvcrt:static': {
        'CCFLAGS': ['/MT']
      },
      'msvcrt:shared': {
        'CCFLAGS': ['/MD']
      },
      'msvcltcg:on': {
        'CCFLAGS':      ['/GL'],
        'pgo:off': {
          'LINKFLAGS':    ['/LTCG'],
        },
      },
      'pgo:instrument': {
        'LINKFLAGS':    ['/LTCG:PGI']
      },
      'pgo:optimize': {
        'LINKFLAGS':    ['/LTCG:PGO']
      }
    },
    'arch:ia32': {
      'CPPDEFINES': ['V8_TARGET_ARCH_IA32'],
      'LINKFLAGS': ['/MACHINE:X86']
    },
    'arch:x64': {
      'CPPDEFINES': ['V8_TARGET_ARCH_X64'],
      'LINKFLAGS': ['/MACHINE:X64', '/STACK:2091752']
    },
    'mode:debug': {
      'CCFLAGS':    ['/Od'],
      'LINKFLAGS':  ['/DEBUG'],
      'CPPDEFINES': ['DEBUG'],
      'msvcrt:static': {
        'CCFLAGS':  ['/MTd']
      },
      'msvcrt:shared': {
        'CCFLAGS':  ['/MDd']
      }
    }
  }
}


D8_FLAGS = {
  'gcc': {
    'console:readline': {
      'LIBS': ['readline']
    },
    'os:linux': {
      'LIBS': ['pthread'],
    },
    'os:macos': {
      'LIBS': ['pthread'],
    },
    'os:freebsd': {
      'LIBS': ['pthread'],
    },
    'os:solaris': {
      'LIBS': ['m', 'pthread', 'socket', 'nsl', 'rt'],
      'LINKFLAGS': ['-mt']
    },
    'os:openbsd': {
      'LIBS': ['pthread'],
    },
    'os:android': {
      'LIBPATH':     [ANDROID_TOP + '/out/target/product/generic/obj/lib',
                      ANDROID_TOP + '/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/interwork'],
      'LINKFLAGS':    ANDROID_LINKFLAGS,
      'LIBS':         ['log', 'c', 'stdc++', 'm', 'gcc'],
    },
    'os:win32': {
      'LIBS': ['winmm', 'ws2_32'],
    },
    'arch:arm': {
      'LINKFLAGS':   ARM_LINK_FLAGS
    },
  },
  'msvc': {
    'all': {
      'LIBS': ['winmm', 'ws2_32']
    }
  }
}


SUFFIXES = {
  'release': '',
  'debug': '_g'
}


def Abort(message):
  print message
  sys.exit(1)


def GuessToolchain(os):
  tools = Environment()['TOOLS']
  if 'gcc' in tools:
    return 'gcc'
  elif 'msvc' in tools:
    return 'msvc'
  else:
    return None


def GuessVisibility(os, toolchain):
  if os == 'win32' and toolchain == 'gcc':
    # MinGW can't do it.
    return 'default'
  elif os == 'solaris':
    return 'default'
  else:
    return 'hidden'


OS_GUESS = utils.GuessOS()
TOOLCHAIN_GUESS = GuessToolchain(OS_GUESS)
ARCH_GUESS = utils.GuessArchitecture()
VISIBILITY_GUESS = GuessVisibility(OS_GUESS, TOOLCHAIN_GUESS)


SIMPLE_OPTIONS = {
  'toolchain': {
    'values': ['gcc', 'msvc'],
    'default': TOOLCHAIN_GUESS,
    'help': 'the toolchain to use (%s)' % TOOLCHAIN_GUESS
  },
  'os': {
    'values': ['freebsd', 'linux', 'macos', 'win32', 'android', 'openbsd', 'solaris'],
    'default': OS_GUESS,
    'help': 'the os to build for (%s)' % OS_GUESS
  },
  'arch': {
    'values':['arm', 'ia32', 'x64', 'mips'],
    'default': ARCH_GUESS,
    'help': 'the architecture to build for (%s)' % ARCH_GUESS
  },
  'regexp': {
    'values': ['native', 'interpreted'],
    'default': 'native',
    'help': 'Whether to use native or interpreted regexp implementation'
  },
  'snapshot': {
    'values': ['on', 'off', 'nobuild'],
    'default': 'off',
    'help': 'build using snapshots for faster start-up'
  },
  'prof': {
    'values': ['on', 'off', 'oprofile'],
    'default': 'off',
    'help': 'enable profiling of build target'
  },
  'gdbjit': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable GDB JIT interface'
  },
  'library': {
    'values': ['static', 'shared'],
    'default': 'static',
    'help': 'the type of library to produce'
  },
  'vmstate': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable VM state tracking'
  },
  'objectprint': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable object printing'
  },
  'protectheap': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable heap protection'
  },
  'profilingsupport': {
    'values': ['on', 'off'],
    'default': 'on',
    'help': 'enable profiling of JavaScript code'
  },
  'debuggersupport': {
    'values': ['on', 'off'],
    'default': 'on',
    'help': 'enable debugging of JavaScript code'
  },
  'inspector': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable inspector features'
  },
  'liveobjectlist': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable live object list features in the debugger'
  },
  'soname': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'turn on setting soname for Linux shared library'
  },
  'msvcrt': {
    'values': ['static', 'shared'],
    'default': 'static',
    'help': 'the type of Microsoft Visual C++ runtime library to use'
  },
  'msvcltcg': {
    'values': ['on', 'off'],
    'default': 'on',
    'help': 'use Microsoft Visual C++ link-time code generation'
  },
  'simulator': {
    'values': ['arm', 'mips', 'none'],
    'default': 'none',
    'help': 'build with simulator'
  },
  'unalignedaccesses': {
    'values': ['default', 'on', 'off'],
    'default': 'default',
    'help': 'set whether the ARM target supports unaligned accesses'
  },
  'disassembler': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable the disassembler to inspect generated code'
  },
  'sourcesignatures': {
    'values': ['MD5', 'timestamp'],
    'default': 'MD5',
    'help': 'set how the build system detects file changes'
  },
  'console': {
    'values': ['dumb', 'readline'],
    'default': 'dumb',
    'help': 'the console to use for the d8 shell'
  },
  'verbose': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'more output from compiler and linker'
  },
  'visibility': {
    'values': ['default', 'hidden'],
    'default': VISIBILITY_GUESS,
    'help': 'shared library symbol visibility (%s)' % VISIBILITY_GUESS
  },
  'pgo': {
    'values': ['off', 'instrument', 'optimize'],
    'default': 'off',
    'help': 'select profile guided optimization variant',
  }
}


def GetOptions():
  result = Options()
  result.Add('mode', 'compilation mode (debug, release)', 'release')
  result.Add('sample', 'build sample (shell, process, lineprocessor)', '')
  result.Add('cache', 'directory to use for scons build cache', '')
  result.Add('env', 'override environment settings (NAME0:value0,NAME1:value1,...)', '')
  result.Add('importenv', 'import environment settings (NAME0,NAME1,...)', '')
  for (name, option) in SIMPLE_OPTIONS.iteritems():
    help = '%s (%s)' % (name, ", ".join(option['values']))
    result.Add(name, help, option.get('default'))
  return result


def GetVersionComponents():
  MAJOR_VERSION_PATTERN = re.compile(r"#define\s+MAJOR_VERSION\s+(.*)")
  MINOR_VERSION_PATTERN = re.compile(r"#define\s+MINOR_VERSION\s+(.*)")
  BUILD_NUMBER_PATTERN = re.compile(r"#define\s+BUILD_NUMBER\s+(.*)")
  PATCH_LEVEL_PATTERN = re.compile(r"#define\s+PATCH_LEVEL\s+(.*)")

  patterns = [MAJOR_VERSION_PATTERN,
              MINOR_VERSION_PATTERN,
              BUILD_NUMBER_PATTERN,
              PATCH_LEVEL_PATTERN]

  source = open(join(root_dir, 'src', 'version.cc')).read()
  version_components = []
  for pattern in patterns:
    match = pattern.search(source)
    if match:
      version_components.append(match.group(1).strip())
    else:
      version_components.append('0')

  return version_components


def GetVersion():
  version_components = GetVersionComponents()

  if version_components[len(version_components) - 1] == '0':
    version_components.pop()
  return '.'.join(version_components)


def GetSpecificSONAME():
  SONAME_PATTERN = re.compile(r"#define\s+SONAME\s+\"(.*)\"")

  source = open(join(root_dir, 'src', 'version.cc')).read()
  match = SONAME_PATTERN.search(source)

  if match:
    return match.group(1).strip()
  else:
    return ''


def SplitList(str):
  return [ s for s in str.split(",") if len(s) > 0 ]


def IsLegal(env, option, values):
  str = env[option]
  for s in SplitList(str):
    if not s in values:
      Abort("Illegal value for option %s '%s'." % (option, s))
      return False
  return True


def VerifyOptions(env):
  if not IsLegal(env, 'mode', ['debug', 'release']):
    return False
  if not IsLegal(env, 'sample', ["shell", "process", "lineprocessor"]):
    return False
  if not IsLegal(env, 'regexp', ["native", "interpreted"]):
    return False
  if env['os'] == 'win32' and env['library'] == 'shared' and env['prof'] == 'on':
    Abort("Profiling on windows only supported for static library.")
  if env['gdbjit'] == 'on' and (env['os'] != 'linux' or (env['arch'] != 'ia32' and env['arch'] != 'x64')):
    Abort("GDBJIT interface is supported only for Intel-compatible (ia32 or x64) Linux target.")
  if env['prof'] == 'oprofile' and env['os'] != 'linux':
    Abort("OProfile is only supported on Linux.")
  if env['os'] == 'win32' and env['soname'] == 'on':
    Abort("Shared Object soname not applicable for Windows.")
  if env['soname'] == 'on' and env['library'] == 'static':
    Abort("Shared Object soname not applicable for static library.")
  if env['os'] != 'win32' and env['pgo'] != 'off':
    Abort("Profile guided optimization only supported on Windows.")
  if env['cache'] and not os.path.isdir(env['cache']):
    Abort("The specified cache directory does not exist.")
  if not (env['arch'] == 'arm' or env['simulator'] == 'arm') and ('unalignedaccesses' in ARGUMENTS):
    print env['arch']
    print env['simulator']
    Abort("Option unalignedaccesses only supported for the ARM architecture.")
  for (name, option) in SIMPLE_OPTIONS.iteritems():
    if (not option.get('default')) and (name not in ARGUMENTS):
      message = ("A value for option %s must be specified (%s)." %
          (name, ", ".join(option['values'])))
      Abort(message)
    if not env[name] in option['values']:
      message = ("Unknown %s value '%s'.  Possible values are (%s)." %
          (name, env[name], ", ".join(option['values'])))
      Abort(message)


class BuildContext(object):

  def __init__(self, options, env_overrides, samples):
    self.library_targets = []
    self.mksnapshot_targets = []
    self.cctest_targets = []
    self.sample_targets = []
    self.d8_targets = []
    self.options = options
    self.env_overrides = env_overrides
    self.samples = samples
    self.use_snapshot = (options['snapshot'] != 'off')
    self.build_snapshot = (options['snapshot'] == 'on')
    self.flags = None

  def AddRelevantFlags(self, initial, flags):
    result = initial.copy()
    toolchain = self.options['toolchain']
    if toolchain in flags:
      self.AppendFlags(result, flags[toolchain].get('all'))
      for option in sorted(self.options.keys()):
        value = self.options[option]
        self.AppendFlags(result, flags[toolchain].get(option + ':' + value))
    self.AppendFlags(result, flags.get('all'))
    return result

  def AddRelevantSubFlags(self, options, flags):
    self.AppendFlags(options, flags.get('all'))
    for option in sorted(self.options.keys()):
      value = self.options[option]
      self.AppendFlags(options, flags.get(option + ':' + value))

  def GetRelevantSources(self, source):
    result = []
    result += source.get('all', [])
    for (name, value) in self.options.iteritems():
      source_value = source.get(name + ':' + value, [])
      if type(source_value) == dict:
        result += self.GetRelevantSources(source_value)
      else:
        result += source_value
    return sorted(result)

  def AppendFlags(self, options, added):
    if not added:
      return
    for (key, value) in added.iteritems():
      if key.find(':') != -1:
        self.AddRelevantSubFlags(options, { key: value })
      else:
        if not key in options:
          options[key] = value
        else:
          prefix = options[key]
          if isinstance(prefix, StringTypes): prefix = prefix.split()
          options[key] = prefix + value

  def ConfigureObject(self, env, input, **kw):
    if (kw.has_key('CPPPATH') and env.has_key('CPPPATH')):
      kw['CPPPATH'] += env['CPPPATH']
    if self.options['library'] == 'static':
      return env.StaticObject(input, **kw)
    else:
      return env.SharedObject(input, **kw)

  def ApplyEnvOverrides(self, env):
    if not self.env_overrides:
      return
    if type(env['ENV']) == DictType:
      env['ENV'].update(**self.env_overrides)
    else:
      env['ENV'] = self.env_overrides


def PostprocessOptions(options, os):
  # Adjust architecture if the simulator option has been set
  if (options['simulator'] != 'none') and (options['arch'] != options['simulator']):
    if 'arch' in ARGUMENTS:
      # Print a warning if arch has explicitly been set
      print "Warning: forcing architecture to match simulator (%s)" % options['simulator']
    options['arch'] = options['simulator']
  if (options['prof'] != 'off') and (options['profilingsupport'] == 'off'):
    # Print a warning if profiling is enabled without profiling support
    print "Warning: forcing profilingsupport on when prof is on"
    options['profilingsupport'] = 'on'
  if os == 'win32' and options['pgo'] != 'off' and options['msvcltcg'] == 'off':
    if 'msvcltcg' in ARGUMENTS:
      print "Warning: forcing msvcltcg on as it is required for pgo (%s)" % options['pgo']
    options['msvcltcg'] = 'on'
  if options['arch'] == 'mips':
    if ('regexp' in ARGUMENTS) and options['regexp'] == 'native':
      # Print a warning if native regexp is specified for mips
      print "Warning: forcing regexp to interpreted for mips"
    options['regexp'] = 'interpreted'
  if options['liveobjectlist'] == 'on':
    if (options['debuggersupport'] != 'on') or (options['mode'] == 'release'):
      # Print a warning that liveobjectlist will implicitly enable the debugger
      print "Warning: forcing debuggersupport on for liveobjectlist"
    options['debuggersupport'] = 'on'
    options['inspector'] = 'on'
    options['objectprint'] = 'on'


def ParseEnvOverrides(arg, imports):
  # The environment overrides are in the format NAME0:value0,NAME1:value1,...
  # The environment imports are in the format NAME0,NAME1,...
  overrides = {}
  for var in imports.split(','):
    if var in os.environ:
      overrides[var] = os.environ[var]
  for override in arg.split(','):
    pos = override.find(':')
    if pos == -1:
      continue
    overrides[override[:pos].strip()] = override[pos+1:].strip()
  return overrides


def BuildSpecific(env, mode, env_overrides):
  options = {'mode': mode}
  for option in SIMPLE_OPTIONS:
    options[option] = env[option]
  PostprocessOptions(options, env['os'])

  context = BuildContext(options, env_overrides, samples=SplitList(env['sample']))

  # Remove variables which can't be imported from the user's external
  # environment into a construction environment.
  user_environ = os.environ.copy()
  try:
    del user_environ['ENV']
  except KeyError:
    pass

  library_flags = context.AddRelevantFlags(user_environ, LIBRARY_FLAGS)
  v8_flags = context.AddRelevantFlags(library_flags, V8_EXTRA_FLAGS)
  mksnapshot_flags = context.AddRelevantFlags(library_flags, MKSNAPSHOT_EXTRA_FLAGS)
  dtoa_flags = context.AddRelevantFlags(library_flags, DTOA_EXTRA_FLAGS)
  cctest_flags = context.AddRelevantFlags(v8_flags, CCTEST_EXTRA_FLAGS)
  sample_flags = context.AddRelevantFlags(user_environ, SAMPLE_FLAGS)
  d8_flags = context.AddRelevantFlags(library_flags, D8_FLAGS)

  context.flags = {
    'v8': v8_flags,
    'mksnapshot': mksnapshot_flags,
    'dtoa': dtoa_flags,
    'cctest': cctest_flags,
    'sample': sample_flags,
    'd8': d8_flags
  }

  # Generate library base name.
  target_id = mode
  suffix = SUFFIXES[target_id]
  library_name = 'v8' + suffix
  version = GetVersion()
  if context.options['soname'] == 'on':
    # When building shared object with SONAME version the library name.
    library_name += '-' + version

  # Generate library SONAME if required by the build.
  if context.options['soname'] == 'on':
    soname = GetSpecificSONAME()
    if soname == '':
      soname = 'lib' + library_name + '.so'
    env['SONAME'] = soname

  # Build the object files by invoking SCons recursively.
  (object_files, shell_files, mksnapshot) = env.SConscript(
    join('src', 'SConscript'),
    build_dir=join('obj', target_id),
    exports='context',
    duplicate=False
  )

  context.mksnapshot_targets.append(mksnapshot)

  # Link the object files into a library.
  env.Replace(**context.flags['v8'])

  context.ApplyEnvOverrides(env)
  if context.options['library'] == 'static':
    library = env.StaticLibrary(library_name, object_files)
  else:
    # There seems to be a glitch in the way scons decides where to put
    # PDB files when compiling using MSVC so we specify it manually.
    # This should not affect any other platforms.
    pdb_name = library_name + '.dll.pdb'
    library = env.SharedLibrary(library_name, object_files, PDB=pdb_name)
  context.library_targets.append(library)

  d8_env = Environment()
  d8_env.Replace(**context.flags['d8'])
  context.ApplyEnvOverrides(d8_env)
  shell = d8_env.Program('d8' + suffix, object_files + shell_files)
  context.d8_targets.append(shell)

  for sample in context.samples:
    sample_env = Environment()
    sample_env.Replace(**context.flags['sample'])
    sample_env.Prepend(LIBS=[library_name])
    context.ApplyEnvOverrides(sample_env)
    sample_object = sample_env.SConscript(
      join('samples', 'SConscript'),
      build_dir=join('obj', 'sample', sample, target_id),
      exports='sample context',
      duplicate=False
    )
    sample_name = sample + suffix
    sample_program = sample_env.Program(sample_name, sample_object)
    sample_env.Depends(sample_program, library)
    context.sample_targets.append(sample_program)

  cctest_env = env.Copy()
  cctest_env.Prepend(LIBS=[library_name])
  cctest_program = cctest_env.SConscript(
    join('test', 'cctest', 'SConscript'),
    build_dir=join('obj', 'test', target_id),
    exports='context object_files',
    duplicate=False
  )
  context.cctest_targets.append(cctest_program)

  return context


def Build():
  opts = GetOptions()
  env = Environment(options=opts)
  Help(opts.GenerateHelpText(env))
  VerifyOptions(env)
  env_overrides = ParseEnvOverrides(env['env'], env['importenv'])

  SourceSignatures(env['sourcesignatures'])

  libraries = []
  mksnapshots = []
  cctests = []
  samples = []
  d8s = []
  modes = SplitList(env['mode'])
  for mode in modes:
    context = BuildSpecific(env.Copy(), mode, env_overrides)
    libraries += context.library_targets
    mksnapshots += context.mksnapshot_targets
    cctests += context.cctest_targets
    samples += context.sample_targets
    d8s += context.d8_targets

  env.Alias('library', libraries)
  env.Alias('mksnapshot', mksnapshots)
  env.Alias('cctests', cctests)
  env.Alias('sample', samples)
  env.Alias('d8', d8s)

  if env['sample']:
    env.Default('sample')
  else:
    env.Default('library')

  if env['cache']:
    CacheDir(env['cache'])

# We disable deprecation warnings because we need to be able to use
# env.Copy without getting warnings for compatibility with older
# version of scons.  Also, there's a bug in some revisions that
# doesn't allow this flag to be set, so we swallow any exceptions.
# Lovely.
try:
  SetOption('warn', 'no-deprecated')
except:
  pass


Build()