# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os

Import(['env'])

env_res = env.Clone()
env_test = env.Clone()
env = env.Clone()

install_targets = []

env.Prepend(
    CPPPATH = [
        'app',
        '$CHROME_DIR/third_party/wtl/include',
    ],
)


# TODO(port)
if env_res.Bit('windows'):
  env_res.Append(
      CPPPATH = [
          '.',
          '$CHROME_SRC_DIR',
          # For app/chrome_dll.res to #include installer_util_strings.rc.
          '$CHROME_DIR/installer/util',
      ],
      RCFLAGS = [
          ['/l', '0x409'],
      ],
  )

  browser_res = env_res.RES(
      '$TARGET_ROOT/grit_derived_sources/browser_resources.rc')
  chrome_exe_res = env_res.RES('app/chrome_exe.rc'),
  chrome_dll_res = env_res.RES('app/chrome_dll.rc')
  env_res.Depends(chrome_dll_res, 'installer/util/installer_util_strings.rc')
  common_res = env_res.RES(
      '$TARGET_ROOT/grit_derived_sources/common_resources.rc')
  debugger_res = env_res.RES(
      '$TARGET_ROOT/grit_derived_sources/debugger_resources.rc')
  renderer_res = env_res.RES(
      '$TARGET_ROOT/grit_derived_sources/renderer_resources.rc')
  test_data_res = env_res.RES('test/data/resource.rc')

  net_res = '$TARGET_ROOT/grit_derived_sources/net_resources.res'
  webkit_res = '$TARGET_ROOT/grit_derived_sources/webkit_resources.res'

  dll_resources = [
      browser_res,
      chrome_dll_res,
      common_res,
      debugger_res,
      net_res,
      renderer_res,
      webkit_res,
  ]

##############################################################################
# TODO(sgk):  move to separate .scons file for chrome.dll ####################

env_dll = env.Clone()

env_dll.ApplySConscript([
    '$CHROME_SRC_DIR/build/using_v8.scons',
    '$ICU38_DIR/using_icu38.scons',
])

env_dll.Prepend(
    CPPPATH = [
        "..",
    ],
    CPPDEFINES = [
        'U_STATIC_IMPLEMENTATION',
        'PNG_USER_CONFIG',
        'CHROME_PNG_WRITE_SUPPORT',
        'LIBXSLT_STATIC',
        'LIBXML_STATIC',
    ],
)

env_dll.Append(
    CPPPATH = [
        '$CHROME_DIR/app',
        '$LIBPNG_DIR',
        '$SKIA_DIR/include',
        '$SKIA_DIR/include/corecg',
        '$SKIA_DIR/platform',
        '$LIBXSL_DIR',
        '$LIBXML_DIR/DerivedSources/include',
        '$LIBXML_DIR/include',
        '$BREAKPAD_DIR/src',
    ],
    LIBS = [
        'base',
        'base_gfx',
        #'breakpad_handler',
        'googleurl',
        'media',
        'printing',
        'net',
        'skia',
        'bzip2',
        'libjpeg',
        'libpng',
        'libxml',
        'libxslt',
        'modp_b64',
        'zlib',

        'WTF',
        'V8Bindings',
        'WebCore',
        'WebKit',
        'default_plugin',
        'glue',
        'JavaScriptCore_pcre',
        'port',

        'browser',
        'common',
        'debugger',
        'util',
        'renderer',
        'hunspell',
        'sdch',
        'sqlite',
        'v8_snapshot',

        'X11',
        'Xrender',
        'Xext',
    ],
)

if env.Bit('windows'):
  # These libraries don't work on non-Windows yet.
  env_dll.Append(
      LIBS = [
          'plugin',
          'worker',
      ]
  )

if env_dll.Bit('windows'):
  env_dll.Append(
      CCFLAGS = [
          '/TP',
          '/Wp64',
      ],
      CPPDEFINES = [
          '_WINDLL',
          'CHROME_DLL',
          'BROWSER_DLL',
          'RENDERER_DLL',
          'PLUGIN_DLL',
      ],
      LIBS = [
          'activex_shim',
          'browser_views',
          'views',

          #'comctl32.lib',
          #'dwmapi.lib',
          #'rpcrt4.lib',
          #'shlwapi.lib',
          #'winmm.lib',
          #'wsock32.lib',

          'Urlmon',
      ],
      LINKFLAGS = [
          '/INCREMENTAL',
          '/DEBUG',

          '/DELAYLOAD:"comdlg32.dll"',
          '/DELAYLOAD:"crypt32.dll"',
          '/DELAYLOAD:"cryptui.dll"',
          '/DELAYLOAD:"dwmapi.dll"',
          '/DELAYLOAD:"imagehlp.dll"',
          '/DELAYLOAD:"imm32.dll"',
          '/DELAYLOAD:"oleacc.dll"',
          '/DELAYLOAD:"oleaut32.dll"',
          '/DELAYLOAD:"psapi.dll"',
          '/DELAYLOAD:"urlmon.dll"',
          '/DELAYLOAD:"uxtheme.dll"',
          '/DELAYLOAD:"winhttp.dll"',
          '/DELAYLOAD:"wininet.dll"',
          '/DELAYLOAD:"winspool.drv"',
          '/DELAYLOAD:"ws2_32.dll"',
          '/DELAYLOAD:"wsock32.dll"',

          '/SUBSYSTEM:WINDOWS',
          '/BASE:"0x01000000"',
          '/MACHINE:X86',
          '/FIXED:No',

          '/safeseh',
          '/dynamicbase',
          '/ignore:4199',
          '/ignore:4221',
          '/nxcompat',

          '/PDB:${TARGETS[1]}',
      ],
  )

input_files = ChromeFileList([
  # TODO(sgk):  violate standard indentation so we don't have to
  # reindent too much when we remove the explicit MSVSFilter() calls
  # in favor of generating the hierarchy to reflect the file system.
  MSVSFilter('resources', [
    '$WEBKIT_DIR/glue/resources/aliasb.cur',
    Derived(env_dll.File('$TARGET_ROOT/'
                         + 'grit_derived_sources/browser_resources.rc')),
    '$WEBKIT_DIR/glue/resources/cell.cur',
    'app/check_dependents.bat',
    'app/chrome.dll.deps',
    'app/chrome_dll.rc',
    'app/chrome_dll_resource.h',
    'app/chrome_dll_version.rc.version',
    '$WEBKIT_DIR/glue/resources/col_resize.cur',
    'common/common_resources.rc',
    '$WEBKIT_DIR/glue/resources/copy.cur',
    'browser/debugger/resources/debugger_resources.rc',
    Derived(env_dll.File('$TARGET_ROOT/grit_derived_sources/net_resources.rc')),
    'renderer/renderer_resources.rc',
    '$WEBKIT_DIR/glue/resources/row_resize.cur',
    '$WEBKIT_DIR/glue/resources/vertical_text.cur',
    Derived(env_dll.File('$TARGET_ROOT/'
                         + 'grit_derived_sources/webkit_resources.rc')),
    '$WEBKIT_DIR/glue/resources/zoom_in.cur',
    '$WEBKIT_DIR/glue/resources/zoom_out.cur',
  ]),
    'app/chrome_dll_main.cc',
])

if env_dll.Bit('windows'):
  additional = [
      '$V8_DIR/snapshot-empty$OBJSUFFIX'
  ]
  dll_targets = env_dll.ChromeSharedLibrary('app/chrome_dll/chrome',
                                            #dll_resources + input_files,
                                            input_files + additional,
                                            PDB='chrome_dll.pdb')
  install_targets.extend(dll_targets)

  chrome_exe_version_rc = env.ChromeVersionRC(
      'chrome_exe_version.rc',
      'app/chrome_exe_version.rc.version'
  )

  chrome_dll_version_rc = env.ChromeVersionRC(
      'chrome_dll_version.rc',
      'app/chrome_dll_version.rc.version',
  )

  # TODO(sgk):  explicit dependencies => scanned implicit dependencies
  Depends(chrome_exe_res, chrome_exe_version_rc)
  Depends(chrome_dll_res, chrome_dll_version_rc)

  chrome_exe_version_res = env_res.RES(chrome_exe_version_rc)
  chrome_dll_version_res = env_res.RES(chrome_dll_version_rc)

  install_targets.extend(chrome_exe_version_rc)
  install_targets.extend(chrome_exe_version_res)

  install_targets.extend(chrome_dll_version_rc)
  install_targets.extend(chrome_dll_version_res)

if env_dll.Bit('linux'):
  input_files = [
      'app/chrome_dll_main.cc',
      'app/chrome_exe_main_gtk.cc',
  ]

  env_dll.Append(
    LIBS = [
      'event',
    ]
  )
  chrome = env_dll.ChromeProgram('app/chrome', input_files)


p = env.ChromeMSVSProject('app/chrome_dll.vcproj',
                          dest='$CHROME_SRC_DIR/chrome/app/chrome_dll.vcproj',
                          guid='{C0A7EE2C-2A6D-45BE-BA78-6D006FDF52D9}',
                          keyword='Win32Proj',
                          dependencies = [
                              '$BASE_DIR/build/base.vcproj',
                              '$WEBKIT_DIR/build/WebCore/WebCore.vcproj',
                              '$CHROME_DIR/plugin/plugin.vcproj',
                              '$CHROME_DIR/worker/worker.vcproj',
                              '$LIBJPEG_DIR/libjpeg.vcproj',
                              '$BZIP2_DIR/bzip2.vcproj',
                              '$NET_DIR/build/net.vcproj',
                              ('$WEBKIT_DIR/build/JavaScriptCore/' +
                                   'JavaScriptCore_pcre.vcproj'),
                              '$WEBKIT_DIR/build/port/port.vcproj',
                              '$CHROME_DIR/browser/debugger/debugger.vcproj',
                              '$WEBKIT_DIR/default_plugin/default_plugin.vcproj',
                              '$CHROME_DIR/browser/browser.vcproj',
                              '$WEBKIT_DIR/build/V8Bindings/V8Bindings.vcproj',
                              '$SQLITE_DIR/sqlite.vcproj',
                              '$CHROME_DIR/views/views.vcproj',
                              '$MODP_B64_DIR/modp_b64.vcproj',
                              '$ZLIB_DIR/zlib.vcproj',
                              '$CHROME_DIR/common/common.vcproj',
                              '$ICU38_DIR/build/icu.vcproj',
                              '$CHROME_DIR/renderer/renderer.vcproj',
                              '$BASE_DIR/build/base_gfx.vcproj',
                              '$WEBKIT_DIR/build/JavaScriptCore/WTF.vcproj',
                              ('$CHROME_DIR/browser/'
                               + 'resources/browser_resources.vcproj'),
                              '$V8_DIR/tools/visual_studio/v8_snapshot.vcproj',
                              '$LIBPNG_DIR/libpng.vcproj',
                              '$WEBKIT_DIR/build/glue/glue.vcproj',
                              '$SKIA_DIR/skia.vcproj',
                              '$CHROME_DIR/third_party/hunspell/hunspell.vcproj',
                              '$GEARS_DIR/gears.vcproj',
                              '$GOOGLEURL_DIR/build/googleurl.vcproj',
                              '$CHROME_DIR/installer/util/util.vcproj',
                              '$WEBKIT_DIR/activex_shim/activex_shim.vcproj',
                              '$SDCH_DIR/sdch.vcproj',
                              '$CHROME_DIR/browser/views/browser_views.vcproj',
                              '$LIBXSLT_DIR/build/libxslt.vcproj',
                          ],
                          # TODO(sgk):  when we can intuit the hierarchy
                          # from the built targets.
                          #buildtargets=TODO,
                          files=input_files,
                          local_directory_prefix='./',
                          relative_path_substitutions = [
                              ('../../..', '$(OutDir)'),
                          ],
                          ConfigurationType='2')

p.AddToolFile('tools/build/win/version.rules')

p.AddConfig('Debug|Win32',
            InheritedPropertySheets=[
                '$(SolutionDir)../build/debug.vsprops',
                './chrome_dll.vsprops',
                '../installer/util/using_util.vsprops',
            ],
            tools=[
                'VCPreBuildEventTool',
                'VCCustomBuildTool',
                'Version',
                'VCXMLDataGeneratorTool',
                'VCWebServiceProxyGeneratorTool',
                'VCMIDLTool',
                'VCCLCompilerTool',
                'VCManagedResourceCompilerTool',
                'VCResourceCompilerTool',
                'VCPreLinkEventTool',
                MSVSTool('VCLinkerTool',
                         OutputFile='$(IntDir)/chrome.dll',
                         ImportLibrary='$(OutDir)/lib/$(ProjectName).lib'),
                'VCALinkTool',
                MSVSTool('VCManifestTool',
                         AdditionalManifestFiles=('$(SolutionDir)/app/'
                                                  + 'chrome.dll.manifest')),
                'VCXDCMakeTool',
                'VCBscMakeTool',
                'VCFxCopTool',
                'VCAppVerifierTool',
                'VCWebDeploymentTool',
                MSVSTool('VCPostBuildEventTool',
                         CommandLine=('call '
                                      + '$(SolutionDir)/tools/build/'
                                      + 'win/hardlink_failsafe.bat '
                                      + '"$(TargetPath)" '
                                      + '"$(OutDir)/$(TargetFileName)"')),
            ])

p.AddConfig('Release|Win32',
            InheritedPropertySheets=[
                '$(SolutionDir)../build/release.vsprops',
                './chrome_dll.vsprops',
                '../installer/util/using_util.vsprops',
            ],
            tools=[
                'VCPreBuildEventTool',
                'VCCustomBuildTool',
                'Version',
                'VCXMLDataGeneratorTool',
                'VCWebServiceProxyGeneratorTool',
                'VCMIDLTool',
                'VCCLCompilerTool',
                'VCManagedResourceCompilerTool',
                'VCResourceCompilerTool',
                'VCPreLinkEventTool',
                MSVSTool('VCLinkerTool',
                         OutputFile='$(OutDir)/chrome.dll',
                         ImportLibrary='$(OutDir)/lib/$(ProjectName).lib'),
                'VCALinkTool',
                MSVSTool('VCManifestTool',
                         AdditionalManifestFiles=('$(SolutionDir)/app/'
                                                  + 'chrome.dll.manifest')),
                'VCXDCMakeTool',
                'VCBscMakeTool',
                'VCFxCopTool',
                'VCAppVerifierTool',
                'VCWebDeploymentTool',
                MSVSTool('VCPostBuildEventTool',
                         CommandLine=('check_dependents.bat '
                                      + '$(TargetPath) chrome.dll.deps')),
            ])


##############################################################################
# TODO(sgk):  move to separate .scons file for chrome.exe ####################

env_exe = env.Clone()

env_exe.Prepend(
    CPPPATH = [
        '..',
        '$BREAKPAD_DIR/src',
    ],
    LIBS = [
        'base',
        'breakpad_handler',
        'chrome',
        'common',
        'icu',
        'sandbox',
        'util',
    ],
)

env_exe.Append(
    LINKFLAGS = [
        '/INCREMENTAL',

        '/DELAYLOAD:"dwmapi.dll"',
        '/DELAYLOAD:"uxtheme.dll"',

        '/SUBSYSTEM:WINDOWS',
        '/MACHINE:X86',
        '/FIXED:No',

        '/safeseh',
        '/dynamicbase',
        '/ignore:4199',
        '/nxcompat',

        '/PDB:${TARGETS[1]}',
        #'/IMPLIB:${TARGETS[2]}',
    ],
)

input_files = ChromeFileList([
  # TODO(sgk):  violate standard indentation so we don't have to
  # reindent too much when we remove the explicit MSVSFilter() calls
  # in favor of generating the hierarchy to reflect the file system.
  MSVSFilter('resources', [
    'app/check_dependents.bat',
    'app/chrome.exe.deps',
    'app/chrome_exe.rc',
    'app/chrome_exe_resource.h',
    'app/chrome_exe_version.rc.version',
  ]),
    'app/breakpad.cc',
    'app/breakpad.h',
    'app/chrome_exe_main.cc',
    'common/chrome_switches$OBJSUFFIX',
    'app/client_util.cc',
    'app/client_util.h',
    'common/env_vars$OBJSUFFIX',
    'common/env_vars.h',
    'app/google_update_client.cc',
    'app/google_update_client.h',
    'common/result_codes.h',
])

# We only use a separate exe/dll on Windows.
if env.Bit('windows'):
  chrome_exe = env_exe.ChromeProgram('chrome_exe/chrome', input_files)

  install_targets.append(chrome_exe[0])

  env.Requires('$DESTINATION_ROOT/chrome.exe',
               ['$DESTINATION_ROOT/chrome.dll',
                '$DESTINATION_ROOT/icudt38.dll',
                '$DESTINATION_ROOT/rlz.dll',
                '$DESTINATION_ROOT/First Run',
                '$DESTINATION_ROOT/themes',
                Alias('chrome_locales'),
               ])

  env.Install('$DESTINATION_ROOT', '$CHROME_DIR/app/FirstRun')

  # For release we want to run dependencies.py, may look something like:
  #env.AddPostAction('$TARGET_ROOT/chrome.exe',
  #                  ('$PYTHON tools/build/win/dependencies.py ' +
  #                       '$(TargetPath) chrome.exe.deps'))

p = env.ChromeMSVSProject('app/chrome_exe.vcproj',
                          dest='$CHROME_SRC_DIR/chrome/app/chrome_exe.vcproj',
                          guid='{7B219FAA-E360-43C8-B341-804A94EEFFAC}',
                          keyword='Win32Proj',
                          dependencies = [
                              '$BASE_DIR/build/base.vcproj',
                              '$SANDBOX_DIR/src/sandbox.vcproj',
                              ('$CHROME_DIR/tools/'
                               + 'crash_service/crash_service.vcproj'),
                              '$BREAKPAD_DIR/breakpad_handler.vcproj',
                              '$CHROME_DIR/app/chrome_dll.vcproj',
                              '$CHROME_DIR/app/locales/en-US.vcproj',
                              '$CHROME_DIR/installer/util/util.vcproj',
                              '$CHROME_DIR/app/theme/theme_dll.vcproj',
                          ],
                          # TODO(sgk):  when we can intuit the hierarchy
                          # from the built targets.
                          #buildtargets=TODO,
                          files=input_files,
                          local_directory_prefix='./',
                          ConfigurationType='1')

p.AddToolFile('tools/build/win/version.rules')

p.AddConfig('Debug|Win32',
            InheritedPropertySheets=[
                './chrome_exe.vsprops',
                '$(SolutionDir)../build/debug.vsprops',
            ],
            tools=[
                'VCPreBuildEventTool',
                'VCCustomBuildTool',
                'Version',
                'VCXMLDataGeneratorTool',
                'VCWebServiceProxyGeneratorTool',
                'VCMIDLTool',
                MSVSTool('VCCLCompilerTool',
                         BasicRuntimeChecks='0',
                         BufferSecurityCheck='false'),
                'VCManagedResourceCompilerTool',
                'VCResourceCompilerTool',
                'VCPreLinkEventTool',
                MSVSTool('VCLinkerTool',
                         ImportLibrary='$(OutDir)/lib/$(ProjectName).lib'),
                'VCALinkTool',
                MSVSTool('VCManifestTool',
                         AdditionalManifestFiles=('$(SolutionDir)/app/'
                                                  + 'chrome.exe.manifest')),
                'VCXDCMakeTool',
                'VCBscMakeTool',
                'VCFxCopTool',
                'VCAppVerifierTool',
                'VCWebDeploymentTool',
                MSVSTool('VCPostBuildEventTool',
                         CommandLine=('copy '
                                      + '$(ProjectDir)/FirstRun '
                                      + '$(OutDir)/"First Run"'),
                         Description='Copy first run complete sentinel file'),
            ])

p.AddConfig('Release|Win32',
            InheritedPropertySheets=[
                './chrome_exe.vsprops',
                '$(SolutionDir)../build/release.vsprops',
            ],
            tools=[
                'VCPreBuildEventTool',
                'VCCustomBuildTool',
                'Version',
                'VCXMLDataGeneratorTool',
                'VCWebServiceProxyGeneratorTool',
                'VCMIDLTool',
                'VCCLCompilerTool',
                'VCManagedResourceCompilerTool',
                'VCResourceCompilerTool',
                'VCPreLinkEventTool',
                MSVSTool('VCLinkerTool',
                         ImportLibrary='$(OutDir)/lib/$(ProjectName).lib'),
                'VCALinkTool',
                MSVSTool('VCManifestTool',
                         AdditionalManifestFiles=('$(SolutionDir)/app/'
                                                  + 'chrome.exe.manifest')),
                'VCXDCMakeTool',
                'VCBscMakeTool',
                'VCFxCopTool',
                'VCAppVerifierTool',
                'VCWebDeploymentTool',
                MSVSTool('VCPostBuildEventTool',
                         # TODO(sgk): figure out how to get \r\n through
                         # Python's XML generation?
                         #CommandLine=('copy '
                         #             + '$(ProjectDir)/FirstRun '
                         #             + '$(OutDir)/"First Run"\r\n'
                         #             + 'check_dependents.bat '
                         #             + '$(TargetPath) '
                         #             + 'chrome.exe.deps\r\n'),
                         CommandLine=('copy '
                                      + '$(ProjectDir)/FirstRun '
                                      + '$(OutDir)/"First Run" '
                                      + '&& '
                                      + 'check_dependents.bat '
                                      + '$(TargetPath) '
                                      + 'chrome.exe.deps'),
                         Description='Copy first run complete sentinel file'),
            ])


##############################################################################
# TODO(sgk): Port these to browser_views.scons and views.scons.

sconscript_files = [
    'browser/views/SConscript',
    'views/SConscript',
]

if not env.WantSystemLib('hunspell'):
  sconscript_files.append('third_party/hunspell/SConscript')

env.SConscript(sconscript_files, exports=['env', 'env_res', 'env_test'])

# TODO(port)
if env.Bit('windows'):
  env.InstallAs('$LIBS_DIR/${LIBPREFIX}jscre${LIBSUFFIX}',
                '$WEBKIT_DIR/JavaScriptCore_pcre.lib')


env.Install('$DESTINATION_ROOT', install_targets)


gears_plugins = [
    '$GEARS_DIR/binaries/gears.dll',
    '$GEARS_DIR/binaries/gears.pdb',
]

# TODO(port)
if env.Bit('windows'):
  env.Install('$DESTINATION_ROOT/plugins/gears', gears_plugins)

env.Command('$DESTINATION_ROOT/resources/inspector',
            '$CHROME_SRC_DIR/third_party/WebKit/WebCore/inspector/front-end',
            Copy('$TARGET', '$SOURCE'),
            source_scanner=DirScanner)