# Copyright (c) 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 utils

Import('env')

env = env.Clone()

# TODO: move all these builders out to site_scons or somesuch.

# Building .stab files, using M4FLAGS.

env.Replace(
    STAB = 'python $OPEN_DIR/tools/parse_stab.py',
    I18N_INPUTS_BASEDIR = '$OPEN_DIR/ui/generated',
    STABCOM = \
        '$STAB $M4FLAGS ${_M4INCFLAGS} $TARGET $SOURCE $I18N_INPUTS_BASEDIR',
)
stab_builder = Builder(action = '$STABCOM', src_suffix = '.stab')
env.Append(BUILDERS = {'Stab': stab_builder})


# Building .idl files.
# This is a total mess.  MIDL needs to be run from $OPEN_DIR because it's too
# stupid to apply its include paths to a relative path like "ui/ie/bla.idl"
# (it only looks in the current dir).  So we have to jump through hoops to fix
# up our relative include paths and output files.
env.Tool('midl')

if env['BROWSER'] == 'IE':
  env.Replace(
      SCONS_DIR = '../../../..',  # the scons dir relative to OPEN_DIR
      IDLINCPREFIX = '/I$SCONS_DIR/',
      IDLINCSUFFIX = '',
      _IDLINCFLAGS = ('${_concat(IDLINCPREFIX, CPPPATH, IDLINCSUFFIX, '
                     '__env__, RDirs, TARGET, SOURCE)}'),
      IDLDEFPREFIX = '/D',
      IDLDEFSUFFIX = '',
      _IDLDEFFLAGS = ('${_defines(IDLDEFPREFIX, CPPDEFINES, '
                     'IDLDEFSUFFIX, __env__)}'),
      MIDLCOM = (
          'cd $OPEN_DIR && '
          '$MIDL ${_IDLDEFFLAGS} ${_IDLINCFLAGS} -env win32 -Oicf '
          '/tlb $SCONS_DIR/${TARGET.base}.tlb '
          '/h $SCONS_DIR/${TARGET.base}.h '
          '/iid $SCONS_DIR/${TARGET.base}_i.c '
          '/proxy $SCONS_DIR/${TARGET.base}_p.c '
          '/dlldata $SCONS_DIR/${TARGET.base}_data.c '
          '$SCONS_DIR/$SOURCE'
      ),
  )
elif env['BROWSER'] in ['FF2', 'FF3']:
  env.Replace(
      GECKO_SDK = '$GECKO_BASE/$OS',
      GECKO_BIN = '$GECKO_SDK/gecko_sdk/bin',
      GECKO_LIB = '$GECKO_SDK/gecko_sdk/lib',
      MIDLCOM = (
          'xpidl -I $GECKO_SDK/gecko_sdk/idl -I $GECKO_BASE '
          '-m header -o ${TARGET.base} $SOURCE && '
          'xpidl -I $GECKO_SDK/gecko_sdk/idl -I $GECKO_BASE '
          '-m typelib -o ${TARGET.base} $SOURCE'
      ),
  )
  if env['BROWSER'] == 'FF2':
    env['GECKO_BASE'] = '$THIRD_PARTY_DIR/gecko_1.8'
  else:
    env['GECKO_BASE'] = '$THIRD_PARTY_DIR/gecko_1.9'

  env.PrependENVPath('PATH', env.Dir('#/$GECKO_BIN').abspath)

def MyIdlEmitter(target, source, env):
  """Produce the list of outputs generated by our IDL compiler.  Outputs
  differ depending on whether we're using xpidl (Firefox) or midl (IE)."""
  base = os.path.splitext(str(target[0]))[0]
  if 'xpidl' in env['MIDLCOM']:
    # Firefox's IDL compiler only generates .h and .xpt files. 
    target = [base + '.h', base + '.xpt']
  else:
    # MIDL generates the following.  Depending on the IDL, a .tlb is
    # generated, but since we can't know, we'll just ignore that.
    target = [base + '.h', base + '_i.c', base + '_p.c', base + '_data.c']
  return (target, source)
# Copy the builder object so we don't overwrite everyone's
# emitter with ours.
import copy
env['BUILDERS']['TypeLibrary'] = copy.copy(env['BUILDERS']['TypeLibrary'])
env['BUILDERS']['TypeLibrary'].emitter = MyIdlEmitter


# Building .xpt files.

if env['BROWSER'] in ['FF2', 'FF3']:
  env.Replace(
    XPTLINK = 'xpt_link',
    XPTLINKCOM = '$XPTLINK $TARGET $SOURCES',
  )
  xpt_builder = Builder(action = '$XPTLINKCOM', src_suffix = '.xpt')
  env.Append(BUILDERS = {'XptLink': xpt_builder})


# C++ defines

env.Prepend(
    CPPDEFINES = [
        'BROWSER_${BROWSER}=1',
# SpiderMonkey (the Firefox JS engine)'s JS_GET_CLASS macro in jsapi.h needs
# this defined to work with the gecko SDK that we've built.
# The definition of JS_THREADSAFE must be kept in sync with MOZJS_CPPFLAGS.
# TODO(mpcomplete): we only need this for FF and SF, I think.
        'JS_THREADSAFE',
    ],
)

if env['BROWSER'] in ['FF2', 'FF3']:
  env.Prepend(
      CPPDEFINES = [
# TODO(cprince): Update source files so we don't need this compatibility define?
        'BROWSER_FF=1',
        'MOZILLA_STRICT_API',
# SpiderMonkey (the Firefox JS engine)'s JS_GET_CLASS macro in jsapi.h needs
# this defined to work with the gecko SDK that we've built.
# The definition of JS_THREADSAFE must be kept in sync with MOZJS_CPPFLAGS.
        'JS_THREADSAFE',
      ],
      CPPPATH = [
        '$GECKO_BASE',
        '$GECKO_SDK',
        '$GECKO_SDK/gecko_sdk/include',
      ],
  )
elif env['BROWSER'] == 'NPAPI' and env['OS'] == 'win32':
  env.Prepend(
      CPPDEFINES = [
        'BROWSER_CHROME=1',
      ],
  )

env.Append(
    CPPPATH = [
#        '$LIBPNG_DIR',
#        '$SKIA_DIR/include',
#        '$SKIA_DIR/include/corecg',
#        '$SKIA_DIR/platform',
    ],
    LIBS = [
#        'base',
        'googleurl-gears',
#        env['ICU_LIBS'],  # TODO(sgk):  '$ICU_LIBS' when scons is fixed
#        'modp_b64',
        'png-gears',
        'sqlite-gears',
#        'skia',
        'zlib-gears',
    ],
)

# Add in libraries for in-development APIs for non-official builds.
if not env['OFFICIAL_BUILD']:
  if env['OS'] != 'wince':
    env.Append(LIBS = [
      'gd',
      'jpeg-gears'
    ])
  env.Append(LIBS = 'portaudio')

if env['BROWSER'] == 'IE':
  if env['OS'] == 'win32':
    env.Append(
        LIBS = [
            'kernel32.lib',
            'user32.lib',
            'gdi32.lib',
            'uuid.lib',
            'sensapi.lib',
            'shlwapi.lib',
            'shell32.lib',
            'advapi32.lib',
            'wininet.lib',
            'comdlg32.lib',
            'user32.lib',
        ],
    )
  else:  # OS=wince
    env.Append(
        LIBS = [
            'wininet.lib',
            'ceshell.lib',
            'coredll.lib',
            'corelibc.lib',
            'ole32.lib',
            'oleaut32.lib',
            'uuid.lib',
            'commctrl.lib',
            'atlosapis.lib',
            'piedocvw.lib',
            'cellcore.lib',
            'htmlview.lib',
            'imaging.lib',
            'toolhelp.lib',
            'aygshell.lib',
            'iphlpapi.lib',
            'gpsapi.lib',
        ],
    )
elif env['BROWSER'] in ['FF2', 'FF3']:
  env.Append(
      LIBPATH = '$GECKO_LIB',
      LIBS = [
          'xpcom',
          'xpcomglue_s',
      ],
  )
  if env['OS'] == 'win32':
    env.Append(
        LIBS = [
            'nspr4.lib',
            'js3250.lib',
            'ole32.lib',
            'shell32.lib',
            'shlwapi.lib',
            'advapi32.lib',
            'wininet.lib',
            'comdlg32.lib',
            'user32.lib',
        ],
    )
  elif env['OS'] == 'linux':
    # Although the 1.9 SDK contains libnspr4, it is better to link against
    # libxul, which in turn depends on libnspr4. In Ubuntu 8.04, libnspr4 was
    # not listed in /usr/lib, only libxul was.
    env.Append(LIBS = (env['BROWSER'] == 'FF2' and 'nspr4' or 'xul'))
elif env['BROWSER'] == 'NPAPI':
  env.Append(
      LIBS = [
          'delayimp.lib',
          'comdlg32.lib',
      ],
      LINKFLAGS = [
          '/DELAYLOAD:"comdlg32.dll"',
      ],
  )


# Building resources.
env_res = env.Clone()
env_res.Replace(
    CPPDEFINES = [
        '_UNICODE',
        'UNICODE',
        '$EXTRA_DEFINES',
    ],
    EXTRA_DEFINES = [
        'BROWSER_${BROWSER}=1',
    ],
    CPPPATH = [
        '$COMMON_GENFILES_DIR/..',
        '$GENFILES_DIR/..',
        '$OPEN_DIR',
        '$VC80_CPPPATH',
    ],
)

if env['MODE'] == 'dbg':
  env_res.Append(CPPDEFINES = 'DEBUG=1')
else:
  env_res.Append(CPPDEFINES = 'NDEBUG=1')

env_res.Append(RCFLAGS = [('/l', '0x409')])
if env['OS'] == 'win32':
  env_res.Append(
      CPPPATH = [
          '$PRIVATE_THIRD_PARTY_DIR/atlmfc_vc80/files/include',
      ],
  )
elif env['OS'] == 'wince':
  env_res.Append(
      CPPDEFINES = [
          'WINCE',
          '_WIN32',
          '_WIN32_WCE',
          'UNDER_CE',
      ],
      CPPPATH = [
          '$OPEN_DIR/..',
          '$PRIVATE_THIRD_PARTY_DIR/atlmfc_vc80ce/files/include',
      ],
      RCFLAGS = [
          '-N',
      ]
  )

if env['OS'] == 'win32':
  # TODO: move to breakpad
  env.Append(CPPFLAGS = ['/wd4018'])

# Input file lists

def NewInputs():
  """Returns a new dictionary of input file lists.

  Browser-specific inputs are referenced by the browser name.  All browsers
  include inputs from the 'all' list."""

  return {
      'all': [],
      'IE': [],
      'FF2': [],
      'FF3': [],
      'NPAPI': [],
      'SF': [],
  }

srcs = NewInputs()
m4srcs = NewInputs()
html_m4srcs = NewInputs()
i18n_m4srcs = NewInputs()
stabsrcs = NewInputs()
idlsrcs = NewInputs()

#-----------------------------------------------------------------------------
# third_party/breakpad

if env['OS'] == 'win32':
  srcs['NPAPI'] += [
      '$THIRD_PARTY_DIR/breakpad/src/client/exception_handler_stub.cc',
  ]

  srcs['IE'] += [
      '$THIRD_PARTY_DIR/breakpad/src/client/windows/handler/exception_handler.cc',
      '$THIRD_PARTY_DIR/breakpad/src/common/windows/guid_string.cc',
      '$OPEN_DIR/base/common/exception_handler_win32.cc',
  ]

  srcs['FF3'] += [
      '$THIRD_PARTY_DIR/breakpad/src/client/windows/handler/exception_handler.cc',
      '$THIRD_PARTY_DIR/breakpad/src/common/windows/guid_string.cc',
      '$OPEN_DIR/base/common/exception_handler_win32.cc',
  ]

#-----------------------------------------------------------------------------
# third_party/v8/bindings

srcs['NPAPI'] += [
    '$THIRD_PARTY_DIR/v8/bindings_local/npruntime.cc',
    '$THIRD_PARTY_DIR/v8/bindings_local/np_v8object.cc',
    '$THIRD_PARTY_DIR/v8/bindings_local/v8_helpers.cc',
    '$THIRD_PARTY_DIR/v8/bindings_local/v8_np_utils.cc',
    '$THIRD_PARTY_DIR/v8/bindings_local/v8_npobject.cc',
]

#-----------------------------------------------------------------------------
# third_party/convert_utf

srcs['all'] += [
    '$THIRD_PARTY_DIR/convert_utf/ConvertUTF.c'
]

#-----------------------------------------------------------------------------
# third_party/jsoncpp

srcs['all'] += [
    '$THIRD_PARTY_DIR/jsoncpp/json_reader.cc',
    '$THIRD_PARTY_DIR/jsoncpp/json_value.cc',
    '$THIRD_PARTY_DIR/jsoncpp/json_writer.cc',
]

#-----------------------------------------------------------------------------
# third_party/modp_b64

srcs['all'] += [
    '$THIRD_PARTY_DIR/modp_b64/modp_b64.cc',
]

#-----------------------------------------------------------------------------
# base/common

m4srcs['all'] = [
    '$OPEN_DIR/base/common/product_version.rc.m4'
]

srcs['all'] += [
    '$OPEN_DIR/base/common/async_router.cc',
    '$OPEN_DIR/base/common/base_class.cc',
    '$OPEN_DIR/base/common/base64.cc',
    '$OPEN_DIR/base/common/byte_store.cc',
    '$OPEN_DIR/base/common/byte_store_test.cc',
    '$OPEN_DIR/base/common/circular_buffer_test.cc',
    '$OPEN_DIR/base/common/database_name_table.cc',
    '$OPEN_DIR/base/common/event.cc',
    '$OPEN_DIR/base/common/event_test.cc',
    '$OPEN_DIR/base/common/file.cc',
    '$OPEN_DIR/base/common/file_test.cc',
    '$OPEN_DIR/base/common/html_event_monitor.cc',
    '$OPEN_DIR/base/common/http_utils.cc',
    '$OPEN_DIR/base/common/js_dom_element.cc',
    '$OPEN_DIR/base/common/js_marshal.cc',
    '$OPEN_DIR/base/common/js_runner_utils.cc',
    '$OPEN_DIR/base/common/js_types.cc',
    '$OPEN_DIR/base/common/leak_counter.cc',
    '$OPEN_DIR/base/common/memory_buffer.cc',
    '$OPEN_DIR/base/common/memory_buffer_test.cc',
    '$OPEN_DIR/base/common/message_queue.cc',
    '$OPEN_DIR/base/common/message_service.cc',
    '$OPEN_DIR/base/common/message_service_test.cc',
    '$OPEN_DIR/base/common/mime_detect.cc',
    '$OPEN_DIR/base/common/mutex.cc',
    '$OPEN_DIR/base/common/mutex_posix.cc',
    '$OPEN_DIR/base/common/mutex_test.cc',
    '$OPEN_DIR/base/common/mutex_win32.cc',
    '$OPEN_DIR/base/common/name_value_table.cc',
    '$OPEN_DIR/base/common/name_value_table_test.cc',
    '$OPEN_DIR/base/common/paths.cc',
    '$OPEN_DIR/base/common/permissions_db.cc',
    '$OPEN_DIR/base/common/permissions_db_test.cc',
    '$OPEN_DIR/base/common/permissions_manager.cc',
    '$OPEN_DIR/base/common/position_table.cc',
    '$OPEN_DIR/base/common/process_utils_win32.cc',
    '$OPEN_DIR/base/common/png_utils.cc',
    '$OPEN_DIR/base/common/scoped_refptr_test.cc',
    '$OPEN_DIR/base/common/security_model.cc',
    '$OPEN_DIR/base/common/security_model_test.cc',
    '$OPEN_DIR/base/common/serialization.cc',
    '$OPEN_DIR/base/common/serialization_test.cc',
    '$OPEN_DIR/base/common/shortcut_table.cc',
    '$OPEN_DIR/base/common/sqlite_wrapper.cc',
    '$OPEN_DIR/base/common/sqlite_wrapper_test.cc',
    '$OPEN_DIR/base/common/stopwatch.cc',
    '$OPEN_DIR/base/common/stopwatch_posix.cc',
    '$OPEN_DIR/base/common/stopwatch_win32.cc',
    '$OPEN_DIR/base/common/string16.cc',
    '$OPEN_DIR/base/common/string_utils.cc',
    '$OPEN_DIR/base/common/string_utils_osx.cc',
    '$OPEN_DIR/base/common/string_utils_test.cc',
    '$OPEN_DIR/base/common/thread.cc',
    '$OPEN_DIR/base/common/thread_locals.cc',
    '$OPEN_DIR/base/common/thread_posix.cc',
    '$OPEN_DIR/base/common/thread_win32.cc',
    '$OPEN_DIR/base/common/timed_call.cc',
    '$OPEN_DIR/base/common/timed_call_test.cc',
    '$OPEN_DIR/base/common/url_utils.cc',
    '$OPEN_DIR/base/common/url_utils_test.cc',
    '$OPEN_DIR/base/common/user_config.cc',
    '$OPEN_DIR/base/common/vista_utils.cc',
]

#-----------------------------------------------------------------------------
# base/firefox

m4srcs['FF3'] += [
    '$OPEN_DIR/base/firefox/install.rdf.m4',
]

idlsrcs['FF3'] += [
    '$OPEN_DIR/base/firefox/interfaces.idl',
]

srcs['FF3'] += [
    '$OPEN_DIR/base/common/file_posix.cc',
    '$OPEN_DIR/base/common/file_win32.cc',
    '$OPEN_DIR/base/common/html_event_monitor_ff.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_linux.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_null.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test_linux.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test_win32.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_win32.cc',
    '$OPEN_DIR/base/common/js_runner_ff.cc',
    '$OPEN_DIR/base/common/js_runner_ff_marshaling.cc',
    '$OPEN_DIR/base/common/message_queue_ff.cc',
    '$OPEN_DIR/base/common/paths_ff.cc',
    '$OPEN_DIR/base/common/time_utils_win32.cc',
    '$OPEN_DIR/base/firefox/dom_utils.cc',
    '$OPEN_DIR/base/firefox/module.cc',
    '$OPEN_DIR/base/firefox/ns_file_utils.cc',
    '$OPEN_DIR/base/firefox/xpcom_dynamic_load.cc',
]

#-----------------------------------------------------------------------------
# base/ie

m4srcs['IE'] += [
    '$OPEN_DIR/base/ie/bho.rgs.m4',
    '$OPEN_DIR/base/ie/interfaces.idl.m4',
    '$OPEN_DIR/base/ie/module.rgs.m4',
]

idlsrcs['IE'] += [
    '$GENFILES_DIR/interfaces.idl',
]

srcs['IE'] += [
    '$OPEN_DIR/base/ie/activex_utils.cc',
    '$OPEN_DIR/base/ie/bho.cc',
    '$OPEN_DIR/base/common/detect_version_collision_win32.cc',
    '$OPEN_DIR/base/ie/dispatcher_to_idispatch.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_null.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test_win32.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_win32.cc',
    '$OPEN_DIR/base/common/js_runner_ie.cc',
    '$OPEN_DIR/base/common/message_queue_ie.cc',
    '$OPEN_DIR/base/ie/module.cc',
    '$OPEN_DIR/base/ie/module_wrapper.cc',
    '$OPEN_DIR/base/common/paths_ie.cc',
    '$OPEN_DIR/base/common/time_utils_win32.cc',
]

if env['OS'] == 'win32':
  srcs['IE'] += [
      '$OPEN_DIR/base/common/file_win32.cc',
      '$OPEN_DIR/base/common/html_event_monitor_ie.cc',
      '$OPEN_DIR/base/ie/ie_version.cc',
  ]
elif env['OS'] == 'wince':
  srcs['IE'] += [
      '$OPEN_DIR/base/common/common_ie.cc',
      '$OPEN_DIR/base/common/file_wince.cc',
      '$OPEN_DIR/base/common/html_event_monitor_iemobile.cc',
      '$OPEN_DIR/base/common/wince_compatibility.cc',
  ]

#-----------------------------------------------------------------------------
# base/chrome + npapi

srcs['NPAPI'] += [
    '$OPEN_DIR/base/chrome/module_cr.cc',
    '$OPEN_DIR/base/common/js_runner_cr.cc',
    '$OPEN_DIR/base/common/html_event_monitor_np.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_linux.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_null.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test_linux.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_test_win32.cc',
    '$OPEN_DIR/base/common/ipc_message_queue_win32.cc',
    '$OPEN_DIR/base/common/paths_cr.cc',
    '$OPEN_DIR/base/npapi/browser_utils.cc',
    '$OPEN_DIR/base/npapi/module.cc',
    '$OPEN_DIR/base/npapi/np_utils.cc',
    '$OPEN_DIR/base/npapi/npn_bindings.cc',
    '$OPEN_DIR/base/npapi/npp_bindings.cc',
    '$OPEN_DIR/base/npapi/plugin.cc',
]

if env['OS'] == 'win32':
  srcs['NPAPI'] += [
      '$OPEN_DIR/base/common/detect_version_collision_win32.cc',
      '$OPEN_DIR/base/common/file_win32.cc',
      '$OPEN_DIR/base/common/message_queue_cr.cc',
      '$OPEN_DIR/base/common/time_utils_win32.cc',
      '$OPEN_DIR/base/ie/ie_version.cc',
  ]

#-----------------------------------------------------------------------------
# console

srcs['all'] += [
    '$OPEN_DIR/console/console.cc',
    '$OPEN_DIR/console/js_callback_logging_backend.cc',
]

#-----------------------------------------------------------------------------
# canvas

# The Canvas API is not yet enabled in official builds.
if not env['OFFICIAL_BUILD']  and env['OS'] in ['win32', 'osx']:
  srcs['all'] += [
      '$OPEN_DIR/canvas/blob_backed_skia_input_stream.cc',
      '$OPEN_DIR/canvas/blob_backed_skia_output_stream.cc',
      '$OPEN_DIR/canvas/canvas.cc',
      '$OPEN_DIR/canvas/canvas_rendering_context_2d.cc',
  ]

#-----------------------------------------------------------------------------
# database

srcs['all'] += [
    '$OPEN_DIR/database/database.cc',
    '$OPEN_DIR/database/database_utils.cc',
    '$OPEN_DIR/database/database_utils_test.cc',
    '$OPEN_DIR/database/result_set.cc',
]

#-----------------------------------------------------------------------------
# database2

srcs['all'] += [
    '$OPEN_DIR/database2/connection.cc',
    '$OPEN_DIR/database2/commands.cc',
    '$OPEN_DIR/database2/database2.cc',
    '$OPEN_DIR/database2/database2_common.cc',
    '$OPEN_DIR/database2/database2_metadata.cc',
    '$OPEN_DIR/database2/interpreter.cc',
    '$OPEN_DIR/database2/manager.cc',
    '$OPEN_DIR/database2/result_set2.cc',
    '$OPEN_DIR/database2/statement.cc',
    '$OPEN_DIR/database2/transaction.cc',
]

#-----------------------------------------------------------------------------
# desktop

srcs['all'] += [
    '$OPEN_DIR/desktop/desktop.cc',
    '$OPEN_DIR/desktop/desktop_linux.cc',
    '$OPEN_DIR/desktop/desktop_osx.cc',
    '$OPEN_DIR/desktop/desktop_test.cc',
    '$OPEN_DIR/desktop/desktop_win32.cc',
    '$OPEN_DIR/desktop/dll_data_wince.cc',
    '$OPEN_DIR/desktop/drag_and_drop_registry.cc',
    '$OPEN_DIR/desktop/notification_message_orderer.cc',
    '$OPEN_DIR/desktop/shortcut_utils_win32.cc',
]

srcs['NPAPI'] += [
    '$OPEN_DIR/desktop/desktop_cr.cc',
]

srcs['all'] += [
    '$OPEN_DIR/desktop/file_dialog.cc',
    '$OPEN_DIR/desktop/file_dialog_gtk.cc',
    '$OPEN_DIR/desktop/file_dialog_osx.cc',
    '$OPEN_DIR/desktop/file_dialog_win32.cc',
]

# The browser module also needs these files, to communicate with the notifier.
srcs['all'] += [
    '$OPEN_DIR/notifier/const_notifier.cc',
    '$OPEN_DIR/notifier/notifier_process_linux.cc',
    '$OPEN_DIR/notifier/notifier_process_posix.cc',
    '$OPEN_DIR/notifier/notifier_process_win32.cc',
    '$OPEN_DIR/notifier/notifier_proxy.cc',
    '$OPEN_DIR/notifier/notifier_utils_win32.cc',
    '$OPEN_DIR/notifier/notification.cc',
]

srcs['FF3'] += [
    '$OPEN_DIR/desktop/drop_target_ff.cc',
]

srcs['IE'] += [
    '$OPEN_DIR/desktop/drop_target_ie.cc',
]

#-----------------------------------------------------------------------------
# localserver/common

srcs['all'] += [
    '$OPEN_DIR/localserver/common/async_task_test.cc',
    '$OPEN_DIR/localserver/common/blob_store.cc',
    '$OPEN_DIR/localserver/common/capture_task.cc',
    '$OPEN_DIR/localserver/common/file_store.cc',
    '$OPEN_DIR/localserver/common/http_constants.cc',
    '$OPEN_DIR/localserver/common/localserver.cc',
    '$OPEN_DIR/localserver/common/localserver_db.cc',
    '$OPEN_DIR/localserver/common/localserver_perf_test.cc',
    '$OPEN_DIR/localserver/common/managed_resource_store.cc',
    '$OPEN_DIR/localserver/common/manifest.cc',
    '$OPEN_DIR/localserver/common/resource_store.cc',
    '$OPEN_DIR/localserver/common/update_task.cc',
    '$OPEN_DIR/localserver/common/update_task_single_process.cc',
    '$OPEN_DIR/localserver/file_submitter.cc',
    '$OPEN_DIR/localserver/localserver_module.cc',
    '$OPEN_DIR/localserver/managed_resource_store_module.cc',
    '$OPEN_DIR/localserver/resource_store_module.cc',
]

srcs['IE'] += [
    '$OPEN_DIR/localserver/common/http_cookies.cc',
]

srcs['FF3'] += [
    '$OPEN_DIR/localserver/common/http_cookies.cc',
]

#-----------------------------------------------------------------------------
# localserver/chrome + npapi
# TODO: safari

srcs['NPAPI'] += [
    '$OPEN_DIR/localserver/chrome/gears_protocol_handler.cc',
    '$OPEN_DIR/localserver/chrome/network_intercept_cr.cc',
    '$OPEN_DIR/localserver/chrome/http_cookies_cr.cc',
    '$OPEN_DIR/localserver/chrome/http_request_cr.cc',
    '$OPEN_DIR/localserver/chrome/update_task_cr.cc',
    '$OPEN_DIR/localserver/common/safe_http_request.cc',
    '$OPEN_DIR/localserver/npapi/async_task_np.cc',
]

#-----------------------------------------------------------------------------
# localserver/firefox

srcs['FF3'] += [
    '$OPEN_DIR/localserver/common/progress_event.cc',
    '$OPEN_DIR/localserver/common/safe_http_request.cc',
    '$OPEN_DIR/localserver/firefox/async_task_ff.cc',
    '$OPEN_DIR/localserver/firefox/cache_intercept.cc',
    '$OPEN_DIR/localserver/firefox/http_request_ff.cc',
    '$OPEN_DIR/localserver/firefox/progress_input_stream.cc',
]

#-----------------------------------------------------------------------------
# localserver/ie

srcs['IE'] += [
    '$OPEN_DIR/localserver/ie/async_task_ie.cc',
    '$OPEN_DIR/localserver/ie/file_submit_behavior.cc',
    '$OPEN_DIR/localserver/ie/http_handler_ie.cc',
    '$OPEN_DIR/localserver/ie/http_request_ie.cc',
    '$OPEN_DIR/localserver/common/progress_event.cc',
    '$OPEN_DIR/localserver/ie/progress_input_stream.cc',
    '$OPEN_DIR/localserver/ie/update_task_ie.cc',
    '$OPEN_DIR/localserver/ie/urlmon_utils.cc',
]

#-----------------------------------------------------------------------------
# dummy_module

srcs['all'] += [
    '$OPEN_DIR/dummy/dummy_module.cc',
]


#-----------------------------------------------------------------------------
# test

srcs['all'] += [
    '$OPEN_DIR/cctests/test.cc',
]

#-----------------------------------------------------------------------------
# ui/generated
#
# Anything with the _I18N suffix will be expanded for each language in
# I18N_LANGS

html_m4srcs['all'] += [
    '$OPEN_DIR/ui/common/permissions_dialog.html_m4',
    '$OPEN_DIR/ui/common/settings_dialog.html_m4',
    '$OPEN_DIR/ui/common/shortcuts_dialog.html_m4',
]

i18n_m4srcs['FF3'] += [
    '$OPEN_DIR/ui/generated/' + lang + '/i18n.dtd.m4'
    for lang in env['I18N_LANGS']
]

#TODO: $(IE_OUTDIR)/string_table.res

#-----------------------------------------------------------------------------
# ui/common (built for all browsers)

srcs['all'] += [
    '$OPEN_DIR/ui/common/html_dialog.cc',
    '$OPEN_DIR/ui/common/i18n_strings.cc',
    '$OPEN_DIR/ui/common/permissions_dialog.cc',
    '$OPEN_DIR/ui/common/window_utils.cc',
]

srcs['IE'] += [
    '$OPEN_DIR/ui/common/settings_dialog.cc',
]

srcs['FF3'] += [
    '$OPEN_DIR/ui/common/settings_dialog.cc',
]

stabsrcs['all'] = [
    '$OPEN_DIR/ui/common/permissions_dialog.js.stab',
    '$OPEN_DIR/ui/common/settings_dialog.js.stab',
    '$OPEN_DIR/ui/common/shortcuts_dialog.js.stab',
]

#-----------------------------------------------------------------------------
# ui/npapi
# TODO: safari

m4srcs['all'] += [
    '$OPEN_DIR/ui/ie/ui_resources.rc.m4',
]

#-----------------------------------------------------------------------------
# ui/chrome

srcs['NPAPI'] += [
    '$OPEN_DIR/ui/chrome/settings_dialog_cr.cc',
    '$OPEN_DIR/ui/chrome/html_dialog_cr.cc',
]

#-----------------------------------------------------------------------------
# ui/factory

m4srcs['FF3'] += [
    '$OPEN_DIR/ui/firefox/browser-overlay.js.m4',
    '$OPEN_DIR/ui/firefox/browser-overlay.xul.m4',
    '$OPEN_DIR/ui/firefox/chrome.manifest.m4',
]

idlsrcs['FF3'] += [
    '$OPEN_DIR/ui/firefox/ui_utils.idl',
]

srcs['FF3'] += [
    '$OPEN_DIR/ui/firefox/html_dialog_ff.cc',
    '$OPEN_DIR/ui/firefox/ui_utils.cc',
]

#-----------------------------------------------------------------------------
# ui/ie

m4srcs['IE'] += [
    '$OPEN_DIR/ui/ie/tools_menu_item.rgs.m4',
    '$OPEN_DIR/ui/ie/ui_resources.rc.m4',
]

idlsrcs['IE'] += [
    '$OPEN_DIR/ui/ie/html_dialog_host.idl',
]

srcs['IE'] += [
    '$OPEN_DIR/ui/ie/html_dialog_ie.cc',
    '$OPEN_DIR/ui/ie/tools_menu_item.cc',
]

stabsrcs['IE'] += [
    '$OPEN_DIR/ui/ie/string_table.rc.stab',
]

# Additional files specific to Win32 or WinCE.
if env['OS'] == 'win32':
  srcs['IE'] += [
      '$OPEN_DIR/ui/ie/html_dialog_host.cc',
  ]
elif env['OS'] == 'wince':
  m4srcs['IE'] += [
      '$OPEN_DIR/ui/ie/html_dialog_bridge_iemobile.rgs.m4',
  ]

  idlsrcs['IE'] += [
      '$OPEN_DIR/ui/ie/html_dialog_host_iemobile.idl',
      '$OPEN_DIR/ui/ie/html_dialog_bridge_iemobile.idl',
  ]

  srcs['IE'] += [
      '$OPEN_DIR/ui/ie/html_dialog_host_iemobile.cc',
      '$OPEN_DIR/ui/ie/html_dialog_bridge_iemobile.cc',
  ]

#-----------------------------------------------------------------------------
# workerpool/npapi
# TODO: safari

srcs['NPAPI'] += [
    '$OPEN_DIR/workerpool/common/workerpool_utils.cc',
    '$OPEN_DIR/workerpool/npapi/pool_threads_manager.cc',
    '$OPEN_DIR/workerpool/workerpool.cc',
]

#-----------------------------------------------------------------------------
# workerpool/firefox

srcs['FF3'] += [
    '$OPEN_DIR/workerpool/common/workerpool_utils.cc',
    '$OPEN_DIR/workerpool/firefox/pool_threads_manager.cc',
    '$OPEN_DIR/workerpool/workerpool.cc',
]

#-----------------------------------------------------------------------------
# workerpool/ie

srcs['IE'] += [
    '$OPEN_DIR/workerpool/common/workerpool_utils.cc',
    '$OPEN_DIR/workerpool/ie/pool_threads_manager.cc',
    '$OPEN_DIR/workerpool/workerpool.cc',
]

#-----------------------------------------------------------------------------
# timer

srcs['all'] += [
    '$OPEN_DIR/timer/timer.cc',
]

#-----------------------------------------------------------------------------
# httprequest

srcs['all'] += [
    '$OPEN_DIR/httprequest/httprequest.cc',
    '$OPEN_DIR/httprequest/httprequest_upload.cc',
]

#-----------------------------------------------------------------------------
# blob

srcs['all'] += [
    '$OPEN_DIR/blob/blob.cc',
    '$OPEN_DIR/blob/blob_builder.cc',
    '$OPEN_DIR/blob/blob_builder_module.cc',
    '$OPEN_DIR/blob/blob_interface.cc',
    '$OPEN_DIR/blob/blob_test.cc',
    '$OPEN_DIR/blob/blob_utils.cc',
    '$OPEN_DIR/blob/buffer_blob.cc',
    '$OPEN_DIR/blob/fail_blob.cc',
    '$OPEN_DIR/blob/file_blob.cc',
    '$OPEN_DIR/blob/join_blob.cc',
    '$OPEN_DIR/blob/slice_blob.cc',
]

# TODO(bpm): Make this cross-browser, not Firefox- or Safari-specific.
srcs['FF3'] += [
    '$OPEN_DIR/blob/blob_input_stream_ff.cc',
    '$OPEN_DIR/blob/blob_input_stream_ff_test.cc',
]

srcs['SF'] += [
    '$OPEN_DIR/blob/blob_input_stream_sf.mm',
    '$OPEN_DIR/blob/blob_input_stream_sf_test.mm',
]

#-----------------------------------------------------------------------------
# inspector

if not env['OFFICIAL_BUILD']:
  srcs['all'] += [
      '$OPEN_DIR/inspector/inspector_resources.cc',
  ]

#-----------------------------------------------------------------------------
# installer/wince

if env['OS'] == 'wince':
  srcs['IE'] += [
      '$OPEN_DIR/installer/iemobile/cab_updater.cc',
  ]

  m4srcs['IE'] += [
      '$OPEN_DIR/installer/iemobile/setup.rc.m4',
  ]

  wince_setup_srcs = [
      '$OPEN_DIR/installer/iemobile/process_restarter.cc',
      '$OPEN_DIR/installer/iemobile/setup.cc',
      env_res.RES('$GENFILES_DIR/setup.rc'),
  ]

#-----------------------------------------------------------------------------
# image

# The Image API is not yet enabled in official builds.
if not env['OFFICIAL_BUILD'] and env['OS'] != 'wince':
  srcs['all'] += [
      '$OPEN_DIR/image/backing_image.cc',
      '$OPEN_DIR/image/image.cc',
      '$OPEN_DIR/image/image_loader.cc',
  ]

#-----------------------------------------------------------------------------
# factory/npapi

srcs['NPAPI'] += [
    '$OPEN_DIR/factory/factory_impl.cc',
    '$OPEN_DIR/factory/factory_np.cc',
    '$OPEN_DIR/factory/factory_utils.cc',
]

#-----------------------------------------------------------------------------
# factory/firefox

srcs['FF3'] += [
    '$OPEN_DIR/factory/factory_impl.cc',
    '$OPEN_DIR/factory/factory_ff.cc',
    '$OPEN_DIR/factory/factory_utils.cc',
]

#-----------------------------------------------------------------------------
# factory/ie

m4srcs['IE'] += [
    '$OPEN_DIR/factory/factory_ie.rgs.m4',
]

srcs['IE'] += [
    '$OPEN_DIR/factory/factory_impl.cc',
    '$OPEN_DIR/factory/factory_ie.cc',
    '$OPEN_DIR/factory/factory_utils.cc',
]

#-----------------------------------------------------------------------------
# geolocation

srcs['all'] += [
    '$OPEN_DIR/geolocation/access_token_manager.cc',
    '$OPEN_DIR/geolocation/backoff_manager.cc',
    '$OPEN_DIR/geolocation/empty_device_data_provider.cc',
    '$OPEN_DIR/geolocation/geolocation.cc',
    '$OPEN_DIR/geolocation/geolocation_db.cc',
    '$OPEN_DIR/geolocation/geolocation_db_test.cc',
    '$OPEN_DIR/geolocation/geolocation_test.cc',
    '$OPEN_DIR/geolocation/gps_location_provider_wince.cc',
    '$OPEN_DIR/geolocation/location_provider.cc',
    '$OPEN_DIR/geolocation/location_provider_pool.cc',
    '$OPEN_DIR/geolocation/network_location_provider.cc',
    '$OPEN_DIR/geolocation/network_location_request.cc',
    '$OPEN_DIR/geolocation/radio_data_provider_wince.cc',
    '$OPEN_DIR/geolocation/reverse_geocoder.cc',
    '$OPEN_DIR/geolocation/timed_callback.cc',
    '$OPEN_DIR/geolocation/wifi_data_provider_android.cc',
    '$OPEN_DIR/geolocation/wifi_data_provider_common.cc',
    '$OPEN_DIR/geolocation/wifi_data_provider_linux.cc',
    '$OPEN_DIR/geolocation/wifi_data_provider_osx.cc',
    '$OPEN_DIR/geolocation/wifi_data_provider_win32.cc',
    '$OPEN_DIR/geolocation/wifi_data_provider_wince.cc',
    '$OPEN_DIR/geolocation/wifi_data_provider_windows_common.cc',
]

#-----------------------------------------------------------------------------
# media

# The Audio API has not been finalized for official builds.
if not env['OFFICIAL_BUILD']:
  srcs['all'] += [
      '$OPEN_DIR/media/audio.cc',
      '$OPEN_DIR/media/audio_recorder.cc',
      '$OPEN_DIR/media/audio_recorder_test.cc',
      '$OPEN_DIR/media/base_audio_recorder.cc',
      '$OPEN_DIR/media/media.cc',
      '$OPEN_DIR/media/media_data.cc',
      '$OPEN_DIR/media/mock_audio_recorder.cc',
      '$OPEN_DIR/media/pa_audio_recorder.cc',
      '$OPEN_DIR/media/time_ranges.cc',
  ]

#-----------------------------------------------------------------------------
# vista_broker

vista_broker_srcs = [
    '$OPEN_DIR/base/common/process_utils_win32.cc',
    '$OPEN_DIR/base/common/string16.cc',
    '$OPEN_DIR/base/common/string_utils.cc',
    '$OPEN_DIR/desktop/shortcut_utils_win32.cc',
    '$OPEN_DIR/vista_broker/vista_broker.cc',
]

vista_broker_srcs += [
    '$THIRD_PARTY_DIR/convert_utf/ConvertUTF.c'
]

#-----------------------------------------------------------------------------
# resources

dll_resources = []

if env['OS'] in ['win32', 'wince']:
  ui_res =  env_res.RES('$GENFILES_DIR/ui_resources.rc'),
  dll_resources += [ui_res]

  module_res = None
  if env['BROWSER'] == 'NPAPI':
    module_res = env_res.RES('$OPEN_DIR/base/npapi/module.rc')
    dll_resources += [module_res]
  elif env['BROWSER'] == 'IE':
    module_res = env_res.RES('$OPEN_DIR/base/ie/module.rc')
    string_res = env_res.RES('$GENFILES_DIR/string_table.rc')
    dll_resources += [module_res, string_res]

    if env['OS'] == 'wince':
      dll_resources += [env_res.RES('$GENFILES_DIR/setup.rc')]

  vista_broker_resources = env_res.RES(
      '$GENFILES_DIR/vista_broker_string_table.res',
      '$GENFILES_DIR/string_table.rc',
      EXTRA_DEFINES = 'VISTA_BROKER=1')
  vista_broker_resources += env_res.RES(
      '$OPEN_DIR/vista_broker/vista_broker.rc',
      EXTRA_DEFINES = 'VISTA_BROKER=1')

#-----------------------------------------------------------------------------
# libs

libs = []

if env['OS'] == 'win32':
  libs += [
      '$THIRD_PARTY_DIR/skia/skia-${MODE}-${OS}-${ARCH}.lib',
  ]

if env['BROWSER'] == 'NPAPI':
  libs += [
      '$THIRD_PARTY_DIR/v8/bin-${MODE}/libv8core.lib',
      '$THIRD_PARTY_DIR/v8/bin-${MODE}/no_snapshotv8.lib',
      '$THIRD_PARTY_DIR/v8/bin-${MODE}/libjscre.lib',
      '$OPEN_DIR/base/chrome/module.def'
  ]
elif env['BROWSER'] == 'IE':
  libs += ['$OPEN_DIR/tools/mscom.def']

# TODO: all the other ports, including third-party libs
# - SAFARI, android, symbian
# - breakpad[_osx]
# - glint
# - growl
# - spidermonkey
# - libspeex
# - libtremor
# TODO: other targets
# - installer (safari, android, ...)

# Now build the dependency tree.
def PatternRule(t, s): return utils.PatternRule(t, s, env)

# Add any hard-coded "FF3_" sources to the FF2_ sources.
srcs['FF2'] += srcs['FF3']
m4srcs['FF2'] += m4srcs['FF3']
html_m4srcs['FF2'] += html_m4srcs['FF3']
i18n_m4srcs['FF2'] += i18n_m4srcs['FF3']
stabsrcs['FF2'] += stabsrcs['FF3']
idlsrcs['FF2'] += idlsrcs['FF3']

# Add the target browser's inputs to the list files to build.
srcs['all'] += srcs[env['BROWSER']]
m4srcs['all'] += m4srcs[env['BROWSER']]
html_m4srcs['all'] += html_m4srcs[env['BROWSER']]
i18n_m4srcs['all'] += i18n_m4srcs[env['BROWSER']]
stabsrcs['all'] += stabsrcs[env['BROWSER']]
idlsrcs['all'] += idlsrcs[env['BROWSER']]

# Include the common targets generated by SConscript.common.
Import('common_targets')

# genfiles/%: %.m4
m4s = [env.M4(*PatternRule('$GENFILES_DIR/${SOURCE.filebase}', src))
        for src in m4srcs['all']]

# genfiles/%.html: %.html_m4
html_m4s = [env.M4(
                *PatternRule('$GENFILES_DIR/${SOURCE.filebase}.html', src))
            for src in html_m4srcs['all']]

# genfiles/i18n/%: %.m4
# This magic grabs the last *2* components from the path ("en-US/foo.m4")
def PathEnd(path, n):
  """Returns the last n components of the given path.
  Example: PathEnd('/foo/bar/baz', 2) == 'bar/baz'"""
  split_path = os.path.normpath(path).split(os.sep)
  return os.sep.join(split_path[-n:])
env['PathEnd'] = PathEnd
tmp_pattern = '$GENFILES_DIR/i18n/${PathEnd(str(SOURCE.base), 2)}'
i18n_m4s = [env.M4(*PatternRule(tmp_pattern, src))
            for src in i18n_m4srcs['all']]

# genfiles/%.js: %.js.stab
stabs = [env.Stab(
             *PatternRule('$GENFILES_DIR/${SOURCE.filebase}', src))
         for src in stabsrcs['all']]

# genfiles/%.html: %.js
for stab in stabs:
  env.Depends(*PatternRule('${SOURCE.base}.html', stab))

# genfiles/%.tlb: %.idl
xptsrcs = []
for src in idlsrcs['all']:
  idl = env.TypeLibrary(
            *PatternRule('$GENFILES_DIR/${SOURCE.filebase}.tlb', src))
  srcs['all'] += [x for x in idl if str(x).endswith('_i.c')]
  if env['BROWSER'] in ['FF2', 'FF3']:
    xptsrcs += [x for x in idl if str(x).endswith('.xpt')]

if env['BROWSER'] in ['FF2', 'FF3']:
  env.XptLink('gears.xpt', xptsrcs)

# Add common sources.
srcs['all'] += common_targets['src']

# TODO: figure out why the .rc scanner doesn't notice these dependencies.
if env['OS'] in ['win32', 'wince']:
  env.Depends(ui_res, html_m4s)
  env.Depends(module_res, m4s)

module = env.ChromeSharedLibrary('gears',
    srcs['all'] + libs + dll_resources)
env.Alias('gears', module)

if env['OS'] == 'wince':
  wince_setup = env.ChromeSharedLibrary('setup', wince_setup_srcs)

if env['OS'] == 'win32' and env['BROWSER'] == 'IE':
# Note: We use IE_OUTDIR so that relative path from gears.dll is same in
# development environment as deployment environment.
# Note: vista_broker.exe needs to stay in sync with name used in
# desktop_win32.cc.
# TODO(aa): This can move to common_outdir like crash_sender.exe
  vista_broker = env.Program('vista_broker',
      vista_broker_srcs + vista_broker_resources)
  env.Alias('gears', vista_broker)