]> git.lyx.org Git - lyx.git/blobdiff - development/scons/SConstruct
Scons: ICONV_CONST bug fix, and some code clean-up
[lyx.git] / development / scons / SConstruct
index 4bdcfbbab3537ba5840e2b8f48f83c8468f1acc4..c80babbe6a4072b23eefdbd47a23a0834b4a909c 100644 (file)
@@ -71,7 +71,7 @@
 #   features.
 #
 
-import os, sys, copy, cPickle
+import os, sys, copy, cPickle, glob
 
 # config/scons_utils.py defines a few utility function
 sys.path.append('config')
@@ -81,8 +81,8 @@ import scons_utils as utils
 # Required runtime environment
 #----------------------------------------------------------
 
-# FIXME: I remember lyx requires higher version of python?
-EnsurePythonVersion(1, 5)
+# scons asks for 1.5.2, lyx requires 2.2
+EnsurePythonVersion(2, 2)
 # Please use at least 0.96.91 (not 0.96.1)
 EnsureSConsVersion(0, 96)
 
@@ -105,6 +105,7 @@ else:
 PACKAGE_VERSION = '1.5.0svn'
 DEVEL_VERSION = True
 default_build_mode = 'debug'
+lyx_ext = '*.C'
 
 PACKAGE = 'lyx'
 PACKAGE_BUGREPORT = 'lyx-devel@lists.lyx.org'
@@ -133,7 +134,6 @@ if os.name == 'nt':
   # If not specified, boost tries to figure out by itself, but it may fail.
   boost_posix = False
   packaging_method = 'windows'
-  add_suffix_to_executables = False
   default_prefix = 'c:/program files/lyx'
   share_dir = 'Resources'
   man_dir = 'Resources/man/man1'
@@ -148,7 +148,6 @@ elif os.name == 'posix' and sys.platform != 'cygwin':
   default_with_x = True
   boost_posix = True
   packaging_method = 'posix'
-  add_suffix_to_executables = True
   default_prefix = '/usr/local/'
   share_dir = 'share/lyx'
   man_dir = 'man/man1'
@@ -163,7 +162,6 @@ elif os.name == 'posix' and sys.platform == 'cygwin':
   default_with_x = True
   boost_posix = True
   packaging_method = 'posix'
-  add_suffix_to_executables = True
   default_prefix = '/usr/local/'
   share_dir = 'share/lyx'
   man_dir = 'man/man1'
@@ -178,7 +176,6 @@ elif os.name == 'darwin':
   default_with_x = False
   boost_posix = True
   packaging_method = 'mac'
-  add_suffix_to_executables = True
   # FIXME: where to install?
   default_prefix = '/usr/local/'
   share_dir = 'Resources'
@@ -194,7 +191,6 @@ else:  # unsupported system
   default_with_x = True
   boost_posix = False
   packaging_method = 'posix'
-  add_suffix_to_executables = True
   default_prefix = '/usr/local/'
   share_dir = 'share/lyx'
   man_dir = 'man/man1'
@@ -240,6 +236,8 @@ opts.AddOptions(
     allowed_values = ('aspell', 'pspell', 'ispell', 'auto') ),
   #
   BoolOption('fast_start', 'Whether or not use cached tests and keep current config.h', True),
+  #
+  BoolOption('load_option', 'load option from previous scons run', True),
   # FIXME: I do not know how pch is working. Ignore this option now.
   BoolOption('pch', '(NA) Whether or not use pch', default_pch_opt),
   # enable assertion, (config.h has ENABLE_ASSERTIOS
@@ -256,7 +254,9 @@ opts.AddOptions(
   BoolOption('std_debug', '(NA) Whether or not turn on stdlib debug', False),
   # using x11?
   BoolOption('X11', 'Use x11 windows system', default_with_x),
-  # 
+  # use MS VC++ to build lyx
+  BoolOption('use_vc', 'Use MS VC++ to build lyx', False),
+  #
   PathOption('qt_dir', 'Path to qt directory', None),
   #
   PathOption('qt_include_path', 'Path to qt include directory', None),
@@ -272,8 +272,12 @@ opts.AddOptions(
   PathOption('extra_inc_path1', 'Extra include path', None),
   #
   PathOption('extra_lib_path1', 'Extra library path', None),
+  # rebuild only specifed, comma separated targets
+  ('rebuild', 'rebuild only specifed, comma separated targets', None),
   # can be set to a non-existing directory
   ('prefix', 'install architecture-independent files in PREFIX', None),
+  # will install to dest_dir if defined. Otherwise, prefix will be used.
+  ('dest_dir', 'install to dest_dir', None),
   # version suffix
   ('version_suffix', 'install lyx as lyx-suffix', ''),
   #
@@ -293,11 +297,62 @@ opts.AddOptions(
   ('LDFLAGS', '$LDFLAGS', None),
 )
 
+# whether or not use current config.h, and cached tests
+if (not ARGUMENTS.has_key('fast_start') or \
+  ARGUMENTS['fast_start'] in ['y', 'yes', 't', 'true', '1', 'all']) \
+  and os.path.isfile(env_cache_file):
+  fast_start = True
+  cache_file = open(env_cache_file)
+  env_cache = cPickle.load(cache_file)
+  cache_file.close()
+  print '------------ fast_start mode --------------------'
+  print '  Use cached test results and current config.h'
+  print '  use fast_start=no to override'
+  print
+else:
+  fast_start = False
+  env_cache = {}
+
+if (not ARGUMENTS.has_key('load_option') or \
+  ARGUMENTS['load_option'] in ['y', 'yes', 't', 'true', '1', 'all']) \
+  and os.path.isfile(env_cache_file):
+  cache_file = open(env_cache_file)
+  opt_cache = cPickle.load(cache_file)['arg_cache']
+  cache_file.close()
+  # import cached options, but we should ignore qt_dir when frontend changes
+  if ARGUMENTS.has_key('frontend') and opt_cache.has_key('frontend') \
+    and ARGUMENTS['frontend'] != opt_cache['frontend'] \
+    and opt_cache.has_key('qt_dir'):
+    opt_cache.pop('qt_dir')
+  # some options will require full rebuild
+  # these are in general things that will change src/config.h
+  for arg in ['version_suffix', 'nls']:
+    if ARGUMENTS.has_key(arg) and ((not opt_cache.has_key(arg)) or \
+      ARGUMENTS[arg] != opt_cache[arg]):
+      if fast_start:
+        print "  ** fast_start is disabled because of the change of option", arg
+        print
+        fast_start = False
+  # and we do not cache some options
+  for arg in ['fast_start']:
+    if opt_cache.has_key(arg):
+      opt_cache.pop(arg)
+  for key in opt_cache.keys():
+    if not ARGUMENTS.has_key(key):
+      ARGUMENTS[key] = opt_cache[key]
+      print "Restoring cached option  %s=%s" % (key, ARGUMENTS[key])
+  print
+
+# save arguments
+env_cache['arg_cache'] = ARGUMENTS
+
 
 #---------------------------------------------------------
 # Setting up environment
 #---------------------------------------------------------
 
+# Note that I do not really like ENV=os.environ, but you may 
+# add it here if you experience some environment related problem
 env = Environment(options = opts)
 
 # Determine the frontend to use, which may be loaded
@@ -305,53 +360,68 @@ env = Environment(options = opts)
 frontend = env.get('frontend', default_frontend)
 # make sure the key exists
 env['frontend'] = frontend
+env['LYX_EXT'] = lyx_ext
 #
 use_X11 = env.get('X11', default_with_x)
+use_vc = env.get('use_vc', False)
+# use it only once for s scons-bug, will remove it later.
+env['USE_VC'] = use_vc
 
-# whether or not use current config.h, and cached tests
-if env['fast_start'] and os.path.isfile(env_cache_file):
-  fast_start = True
-  SetOption('implicit_cache', 1)
-  cache_file = open(env_cache_file)
-  env_cache = cPickle.load(cache_file)
-  cache_file.close()
-  print '------------ fast_start mode --------------------'
-  print '  Use cached test results and current config.h'
-  print '  use fast_start=no to override'
-  print '-------------------------------------------------'
-else:
-  fast_start = False
-  SetOption('implicit_cache', 0)
-  env_cache = {}
 
 # set individual variables since I do not really like ENV = os.environ
 env['ENV']['PATH'] = os.environ.get('PATH')
 env['ENV']['HOME'] = os.environ.get('HOME')
-env['TOP_SRC_DIR'] = TOP_SRC_DIR
+# these are defined for MSVC
+env['ENV']['LIB'] = os.environ.get('LIB')
+env['ENV']['INCLUDE'] = os.environ.get('INCLUDE')
+if use_vc:
+  # full path name is used to build msvs project files
+  env['TOP_SRC_DIR'] = Dir(TOP_SRC_DIR).abspath
+else:
+  env['TOP_SRC_DIR'] = TOP_SRC_DIR
 env['SCONS_DIR'] = SCONS_DIR
 # install to default_prefix by default
 env['PREFIX'] = env.get('prefix', default_prefix)
-if env.has_key('exec_prefix'):
-  env['BIN_DIR'] = env['exec_prefix']
-else:
-  env['BIN_DIR'] = os.path.join(env['PREFIX'], 'bin')
 # program suffix
 if env.has_key('version_suffix'):
-  env['PROGRAM_SUFFIX'] = env['version_suffix']
+  if env['version_suffix'] in ['y', 'yes', 't', 'true', '1', 'all']:
+    env['PROGRAM_SUFFIX'] = PACKAGE_VERSION
+  else:
+    env['PROGRAM_SUFFIX'] = env['version_suffix']
 else:
   env['PROGRAM_SUFFIX'] = ''
-env['ADD_SUFFIX_TO_EXECUTABLES'] = add_suffix_to_executables
-env['SHARE_DIR'] = os.path.join(env['PREFIX'], share_dir + env['PROGRAM_SUFFIX'])
-env['MAN_DIR'] = os.path.join(env['PREFIX'], man_dir)
+
+# whether or not add suffix to file and directory names
+env['ADD_SUFFIX'] = packaging_method != 'windows'
+if env['ADD_SUFFIX']:
+  env['SHARE_DIR'] = os.path.join(env['PREFIX'], share_dir + env['PROGRAM_SUFFIX'])
+else:  
+  env['SHARE_DIR'] = os.path.join(env['PREFIX'], share_dir)
 env['LOCALE_DIR'] = os.path.join(env['PREFIX'], locale_dir)
+#
+# if dest_dir is different from prefix.
+env['DEST_DIR'] = env.get('dest_dir', env['PREFIX'])
+if env.has_key('exec_prefix'):
+  env['BIN_DEST_DIR'] = env['exec_prefix']
+else:
+  env['BIN_DEST_DIR'] = os.path.join(env['DEST_DIR'], 'bin')
+if env['ADD_SUFFIX']:
+  env['SHARE_DEST_DIR'] = os.path.join(env['DEST_DIR'], share_dir + env['PROGRAM_SUFFIX'])
+else:
+  env['SHARE_DEST_DIR'] = os.path.join(env['DEST_DIR'], share_dir)
+env['MAN_DEST_DIR'] = os.path.join(env['DEST_DIR'], man_dir)
+env['LOCALE_DEST_DIR'] = os.path.join(env['DEST_DIR'], locale_dir)
 
 #
 # this is a bit out of place (after auto-configration) but 
 # it is required to do the tests. Since Tool('mingw') will 
-# reset CCFLAGS etc, this should be before getEnvVariable
-if platform_name == 'win32':
+# reset CCFLAGS etc, this should be done before getEnvVariable
+if platform_name == 'win32' and not use_vc:
   env.Tool('mingw')
   env.AppendUnique(CPPPATH = ['#c:/MinGW/include'])
+elif use_vc:
+  env.Tool('msvc')
+  env.Tool('mslink')
 
 
 # speed up source file processing
@@ -402,6 +472,8 @@ else:
 # all built libraries will go to build_dir/libs
 # (This is different from the make file approach)
 env['LOCALLIBPATH'] = '$BUILDDIR/libs'
+# to make use of local cached parameters, this one has to be '.'
+env['MSVSPATH'] = '.'
 env.AppendUnique(LIBPATH = ['$LOCALLIBPATH'])
 
 #
@@ -456,14 +528,24 @@ if env.has_key('aikasurus_path') and env['aikasurus_path']:
 # Note that this step has to be after env.Tool('mingw') step
 # since env.Tool('mingw') will set env['CC'] etc.
 #
-# save the old c compiler
-env['C_COMPILER'] = env['CC']
-if env.has_key('CXX') and env['CXX']:
-  env['CC'] = env['CXX']
-  env['LINK'] = env['CXX']
-else:
-  env['CC'] = 'g++'
-  env['LINK'] = 'g++'
+# save the old c compiler and CCFLAGS (used by libintl)
+env['C_COMPILER'] = env.subst('$CC')
+env['C_CCFLAGS'] = env.subst('$CCFLAGS').split()
+# if we use ms vc, the commands are fine (cl.exe and link.exe)
+if not use_vc:
+  if env.has_key('CXX') and env['CXX']:
+    env['CC'] = env.subst('$CXX')
+    env['LINK'] = env.subst('$CXX')
+  else:
+    env['CC'] = 'g++'
+    env['LINK'] = 'g++'
+else: 
+  # /TP treat all source code as C++
+  # C4819: The file contains a character that cannot be represented 
+  #   in the current code page (number)
+  # C4996: foo was decleared deprecated
+  env.Append(CCFLAGS=['/TP', '/EHsc', '/wd4819', '/wd4996'])
+  env.Append(C_CCFLAGS=['/Dinline#', '/D__attribute__(x)#', '/Duintmax_t=UINT_MAX'])
 
 
 #----------------------------------------------------------
@@ -478,9 +560,13 @@ conf = Configure(env,
     'CheckSelectArgType' : utils.checkSelectArgType,
     'CheckBoostLibraries' : utils.checkBoostLibraries,
     'CheckCommand' : utils.checkCommand,
+    'CheckCXXGlobalCstd' : utils.checkCXXGlobalCstd,
+    'CheckLC_MESSAGES' : utils.checkLC_MESSAGES,
+    'CheckIconvConst' : utils.checkIconvConst,
   }
 )
 
+
 # pkg-config? (if not, we use hard-coded options)
 if not fast_start:
   if conf.CheckPkgConfig('0.15.0'):
@@ -493,8 +579,10 @@ else:
   env['HAS_PKG_CONFIG'] = env_cache['HAS_PKG_CONFIG']
 
 # zlib? This is required. (fast_start assumes the existance of zlib)
-if not fast_start and not conf.CheckLibWithHeader('z', 'zlib.h', 'C'):
-    print 'Did not find libz or zlib.h, exiting!'
+if not fast_start:
+  if (not use_vc and not conf.CheckLibWithHeader('z', 'zlib.h', 'C')) \
+    or (use_vc and not conf.CheckLibWithHeader('zdll', 'zlib.h', 'C')):
+    print 'Did not find zdll.lib or zlib.h, exiting!'
     Exit(1)
 
 # qt libraries?
@@ -533,6 +621,12 @@ if not fast_start:
       print 'Did not find qt libraries, exiting!'
       Exit(1)
 
+# now, if msvc2005 is used, we will need that QT_LIB_PATH/QT_LIB.manifest file
+if use_vc:
+  # glob file xxx.dll.manifest (msvc 2003 may not have it)
+  manifests = glob.glob(os.path.join(env.subst('$QT_LIB_PATH'), '*.dll.manifest'))
+  if manifests != []:
+    env['LINKCOM'] = [env['LINKCOM'], 'mt.exe /MANIFEST %s /outputresource:$TARGET;1' % manifests[0]]
 
 # check socket libs
 if not fast_start:
@@ -586,8 +680,7 @@ else:
   env['INCLUDED_BOOST'] = env_cache['INCLUDED_BOOST']
 
 
-if not env.has_key('nls') or env['nls']:
-  env['ENABLE_NLS'] = True
+env['ENABLE_NLS'] = not env.has_key('nls') or env['nls']
 
 if not fast_start:
   if not env['ENABLE_NLS']:
@@ -614,9 +707,6 @@ if not fast_start:
       # we do not need to set LIBPATH now.
       env['INCLUDED_GETTEXT'] = True
       env['INTL_LIBS'] = ['included_intl']
-    if platform_name == 'win32':
-      # for functions AddFontResouceA, RemoveFontResourceA
-      env['INTL_LIBS'].append('gdi32')
   env_cache['INCLUDED_GETTEXT'] = env['INCLUDED_GETTEXT']
   env_cache['INTL_LIBS'] = env['INTL_LIBS']
 else:
@@ -642,7 +732,7 @@ if not fast_start:
   # of *used-by-lyx* macros compiled by Abdelrazak Younes <younes.a@free.fr>
   #
   # Note: addToConfig etc are defined in scons_util
-  utils.startConfigH(TOP_SRC_DIR)
+  utils.startConfigH()
 
   # HAVE_IO_H
   # HAVE_LIMITS_H
@@ -656,10 +746,26 @@ if not fast_start:
   # HAVE_SYS_UTIME_H
   # HAVE_UNISTD_H
   # HAVE_UTIME_H
+  # HAVE_STRINGS_H
+  # HAVE_DIRECT_H
   # HAVE_ISTREAM
   # HAVE_OSTREAM
   # HAVE_IOS
 
+  # for libintl % grep HAVE * | grep _H | cut -d: -f2 | sort -u
+  
+  # 
+  # HAVE_INTTYPES_H
+  # HAVE_STDINT_H
+  # HAVE_ALLOCA_H
+  # HAVE_STDLIB_H
+  # HAVE_STRING_H
+  # HAVE_STDDEF_H
+  # HAVE_LIMITS_H
+  # HAVE_ARGZ_H
+  # HAVE_UNISTD_H
+  # HAVE_SYS_PARAM_H
+
   # Check header files
   headers = [
     ('io.h', 'HAVE_IO_H', 'c'),
@@ -676,18 +782,27 @@ if not fast_start:
     ('unistd.h', 'HAVE_UNISTD_H', 'c'),
     ('inttypes.h', 'HAVE_INTTYPES_H', 'c'),
     ('utime.h', 'HAVE_UTIME_H', 'c'),
+    ('string.h', 'HAVE_STRING_H', 'c'),
+    ('strings.h', 'HAVE_STRINGS_H', 'c'),
+    ('direct.h', 'HAVE_DIRECT_H', 'c'),
     ('istream', 'HAVE_ISTREAM', 'cxx'),
     ('ostream', 'HAVE_OSTREAM', 'cxx'),
-    ('ios', 'HAVE_IOS', 'cxx')
+    ('ios', 'HAVE_IOS', 'cxx'),
+    ('argz.h', 'HAVE_ARGZ_H', 'c'),
+    ('limits.h', 'HAVE_LIMITS_H', 'c'),
+    ('alloca.h', 'HAVE_ALLOCA_H', 'c'),
+    ('stddef.h', 'HAVE_STDDEF_H', 'c'),
+    ('stdint.h', 'HAVE_STDINT_H', 'c'),
+    ('sys/param.h', 'HAVE_SYS_PARAM_H', 'c')
   ]
 
   for header in headers:
-    utils.addToConfig("/* Define to 1 if you have the <%s> header file. */" % header[0], TOP_SRC_DIR, newline=1)
+    description = "Define to 1 if you have the <%s> header file." % header[0]
     if (header[2] == 'c' and conf.CheckCHeader(header[0])) or \
       (header[2] == 'cxx' and conf.CheckCXXHeader(header[0])):
-      utils.addToConfig('#define %s 1' % header[1], TOP_SRC_DIR)
+      utils.addToConfig('#define %s 1' % header[1], desc = description)
     else:
-      utils.addToConfig('/* #undef %s */' % header[1], TOP_SRC_DIR)
+      utils.addToConfig('/* #undef %s */' % header[1], desc = description)
 
   # HAVE_OPEN
   # HAVE_CLOSE
@@ -707,6 +822,25 @@ if not fast_start:
   # HAVE_STRERROR
   # HAVE_STD_COUNT
   # HAVE_GETCWD
+  # HAVE_STRCPY
+  # HAVE_STRCASECMP 
+  # HAVE_STRDUP
+  # HAVE_STRTOUL
+  # HAVE_WCSLEN
+  # HAVE_MMAP ?
+  # HAVE_ALLOCA
+  # HAVE___FSETLOCKING
+  # HAVE_MEMPCPY
+  # HAVE_STRCASECMP
+  # HAVE___ARGZ_COUNT
+  # HAVE___ARGZ_NEXT
+  # HAVE___ARGZ_STRINGIFY
+  # HAVE___FSETLOCKING
+  # HAVE_GETCWD 
+  # HAVE_STRTOUL  
+  # HAVE_STRCASECMP
+  # HAVE_STRDUP
+  # HAVE_TSEARCH 
 
   # Check functions
   functions = [
@@ -735,20 +869,37 @@ int count()
 }
 '''),
     ('getcwd', 'HAVE_GETCWD', None),
+    ('stpcpy', 'HAVE_STPCPY', None),
+    ('strcasecmp', 'HAVE_STRCASECMP', None),
+    ('strdup', 'HAVE_STRDUP', None),
+    ('strtoul', 'HAVE_STRTOUL', None),
+    ('alloca', 'HAVE_ALLOCA', None),
+    ('__fsetlocking', 'HAVE___FSETLOCKING', None),
+    ('mempcpy', 'HAVE_MEMPCPY', None),
+    ('__argz_count', 'HAVE___ARGZ_COUNT', None),
+    ('__argz_next', 'HAVE___ARGZ_NEXT', None),
+    ('__argz_stringify', 'HAVE___ARGZ_STRINGIFY', None),
+    ('setlocale', 'HAVE_SETLOCALE', None),
+    ('tsearch', 'HAVE_TSEARCH', None),
+    ('getegid', 'HAVE_GETEGID', None),
+    ('getgid', 'HAVE_GETGID', None),
+    ('getuid', 'HAVE_GETUID', None),
+    ('wcslen', 'HAVE_WCSLEN', None)
   ]
 
+  for func in functions:
+    description = "Define to 1 if you have the `%s' function." % func[0]
+    if conf.CheckFunc(func[0], header=func[2]):
+      utils.addToConfig('#define %s 1' % func[1], desc = description)
+    else:
+      utils.addToConfig('/* #undef %s */' % func[1], desc = description)
+
+
   # HAVE_ASPRINTF
   # HAVE_WPRINTF
   # HAVE_SNPRINTF
   # HAVE_POSIX_PRINTF
   # HAVE_FCNTL
-  
-  for func in functions:
-    utils.addToConfig("/* Define to 1 if you have the `%s' function. */" % func[0], TOP_SRC_DIR, newline=1)
-    if conf.CheckFunc(func[0], header=func[2]):
-      utils.addToConfig('#define %s 1' % func[1], TOP_SRC_DIR)
-    else:
-      utils.addToConfig('/* #undef %s */' % func[1], TOP_SRC_DIR)
 
   env_functions = [
     ('asprintf', 'HAVE_ASPRINTF'),
@@ -759,45 +910,120 @@ int count()
   ]
 
   for func in env_functions:
-    utils.addToConfig("/* Define to 1 if you have the `%s' function. */" % func[0], TOP_SRC_DIR, newline=1)
+    description = "Define to 1 if you have the `%s' function." % func[0]
     if conf.CheckFunc(func[0]):
-      utils.addToConfig('#define %s 1' % func[1], TOP_SRC_DIR)
+      utils.addToConfig('#define %s 1' % func[1], desc = description)
       env[func[1]] = 1
     else:
-      utils.addToConfig('/* #undef %s */' % func[1], TOP_SRC_DIR)
+      utils.addToConfig('/* #undef %s */' % func[1], desc = description)
       env[func[1]] = 0
 
   # HAVE_INTMAX_T
-  # HAVE_INTTYPES_H_WITH_UINTMAX 
   # HAVE_DECL_ISTREAMBUF_ITERATOR
-  utils.addToConfig("/* Define to 1 if you have the `intmax_t' type. */", TOP_SRC_DIR, newline=1)
+  description = "Define to 1 if you have the `intmax_t' type."
   if conf.CheckType('intmax_t', includes='#include <stdint.h>') or \
     conf.CheckType('intmax_t', includes='#include <inttypes.h>'):
-    utils.addToConfig('#define HAVE_INTMAX_T 1', TOP_SRC_DIR)
+    utils.addToConfig('#define HAVE_INTMAX_T 1', desc = description)
   else:
-    utils.addToConfig('/* #undef HAVE_INTMAX_T */', TOP_SRC_DIR)
+    utils.addToConfig('/* #undef HAVE_INTMAX_T */',desc = description)
+
+  # HAVE_INTMAX_T
+  # HAVE_LONG_DOUBLE
+  # HAVE_LONG_LONG
+  # HAVE_WCHAR_T
+  # HAVE_WINT_T
+  # HAVE_INTTYPES_H_WITH_UINTMAX 
+  # HAVE_STDINT_H_WITH_UINTMAX
+
+  types = [
+    ('intmax_t', 'HAVE_INTMAX_T', None),
+    ('long double', 'HAVE_LONG_DOUBLE', None),
+    ('long long', 'HAVE_LONG_LONG', None),
+    ('wchar_t', 'HAVE_WCHAR_T', None),
+    ('wint_t', 'HAVE_WINT_T', None),
+    ('uintmax_t', 'HAVE_INTTYPES_H_WITH_UINTMAX', '#include <inttypes.h>'),
+    ('uintmax_t', 'HAVE_STDINT_H_WITH_UINTMAX', '#include <stdint.h>'),
+    ('std::istreambuf_iterator<std::istream>', 'HAVE_DECL_ISTREAMBUF_ITERATOR',
+      '#include <streambuf>\n#include <istream>')
+  ]
+  for t in types: 
+    description = "Define to 1 if you have the `%s' type." % t[0]
+    if conf.CheckType(t[0], includes=t[2]):
+      utils.addToConfig('#define %s 1' % t[1], desc = description)
+    else:
+      utils.addToConfig('/* #undef %s */' % t[1],  desc = description)
 
-  utils.addToConfig("/* Define to 1 if you have the `uintmax_t' type. */", TOP_SRC_DIR, newline=1)
-  if conf.CheckType('uintmax_t', includes='#include <inttypes.h>'):
-    utils.addToConfig('#define HAVE_INTTYPES_H_WITH_UINTMAX  1', TOP_SRC_DIR)
+  # windows/msvc sys/types.h does not have pid_t
+  # FIXME: #include <windows.h> is the right way?
+  if not conf.CheckType('pid_t', includes='#include <sys/types.h>'):
+    utils.addToConfig('#define pid_t int', desc = 'Define is sys/types.h does not have pid_t')
+
+  # determine the use of std::tolower or tolower
+  description = 'Define if your C++ compiler puts C library functions in the global namespace'
+  if conf.CheckCXXGlobalCstd():
+    utils.addToConfig('#define CXX_GLOBAL_CSTD 1', desc = description)
   else:
-    utils.addToConfig('/* #undef HAVE_INTTYPES_H_WITH_UINTMAX  */', TOP_SRC_DIR)
-    
-  utils.addToConfig("/* Define to 1 if you have the `istreambug_iterator' type. */", TOP_SRC_DIR, newline=1)
-  if conf.CheckType('std::istreambuf_iterator<std::istream>', 
-    includes='#include <streambuf>\n#include <istream>'):
-    utils.addToConfig('#define HAVE_DECL_ISTREAMBUF_ITERATOR 1', TOP_SRC_DIR)
+    utils.addToConfig('/* #undef CXX_GLOBAL_CSTD */', desc = description)
+
+  # HAVE_LIBGDI32
+  # HAVE_ICONV
+  # HAVE_LIBC
+  # HAVE_LIBAIKSAURUS
+  libs = [
+    ('gdi32', 'HAVE_LIBGDI32'),
+    ('iconv', 'HAVE_ICONV'),
+    ('c', 'HAVE_LIBC'),
+    ('Aiksaurus', 'HAVE_LIBAIKSAURUS'),
+  ]
+  for lib in libs:
+    description = "Define to 1 if you have the `%s' library (-l%s)." % (lib[0], lib[0])
+    if conf.CheckLib(lib[0]):
+      utils.addToConfig('#define %s 1' % lib[1], desc = description)
+      env[lib[1]] = True
+    else:
+      utils.addToConfig('/* #undef %s */' % lib[1], desc = description)
+      env[lib[1]] = False
+
+  # HAVE_LC_MESSAGES
+  description = 'Define if your <locale.h> file defines LC_MESSAGES.'
+  if conf.CheckLC_MESSAGES():
+    utils.addToConfig('#define HAVE_LC_MESSAGES 1', desc = description)
   else:
-    utils.addToConfig('/* #undef HAVE_DECL_ISTREAMBUF_ITERATOR */', TOP_SRC_DIR)
-      
-  
+    utils.addToConfig('/* #undef HAVE_LC_MESSAGES */', desc = description)
+
+  # ICONV_CONST
+  description = 'Define as const if the declaration of iconv() needs const.'
+  if conf.CheckIconvConst():
+    utils.addToConfig('#define ICONV_CONST', desc = description)
+  else:
+    utils.addToConfig('/* #undef ICONV_CONST */', desc = description)
+
   # PACKAGE
   # PACKAGE_VERSION
+  # PACKAGE_BUGREPORT
+  # PACKAGE_NAME
+  # PACKAGE_STRING
   # DEVEL_VERSION
-  utils.addToConfig('#define PACKAGE "%s%s"' % (PACKAGE, env['PROGRAM_SUFFIX']), TOP_SRC_DIR)
-  utils.addToConfig('#define PACKAGE_VERSION "%s"' % PACKAGE_VERSION, TOP_SRC_DIR)
+  utils.addToConfig('#define PACKAGE "%s%s"' % (PACKAGE, env['PROGRAM_SUFFIX']),
+    desc = "Name of package")
+
+  utils.addToConfig('#define PACKAGE_BUGREPORT "%s"' % PACKAGE_BUGREPORT,
+    desc = 'Define to the address where bug reports for this package should be sent.')
+
+  utils.addToConfig('#define PACKAGE_NAME "%s"' % PACKAGE_NAME,
+      desc = 'Define to the full name of this package.')
+
+  utils.addToConfig('#define PACKAGE_STRING "%s"' % PACKAGE_STRING,
+      desc = "Define to the full name and version of this package.")
+
+  utils.addToConfig('#define PACKAGE_TARNAME "%s"' % PACKAGE_TARNAME,
+      desc = "Define to the one symbol short name of this package.")
+
+  utils.addToConfig('#define PACKAGE_VERSION "%s"' % PACKAGE_VERSION,
+    desc = "Define to the version of this package.")
+
   if DEVEL_VERSION:
-    utils.addToConfig('#define DEVEL_VERSION 1', TOP_SRC_DIR)
+    utils.addToConfig('#define DEVEL_VERSION 1')
 
   # ENABLE_ASSERTIONS
   # ENABLE_NLS
@@ -806,32 +1032,32 @@ int count()
 
   # items are (ENV, ARGUMENTS)
   values = [
-    ('ENABLE_ASSERTIONS', 'assertions'),
-    ('ENABLE_NLS', 'nls'),
-    ('WITH_WARNINGS', 'warnings'),
-    ('_GLIBCXX_CONCEPT_CHECKS', 'concept_checks'),
+    ('ENABLE_ASSERTIONS', 'assertions', 'Define if you want assertions to be enabled in the code'),
+    ('ENABLE_NLS', 'nls', "Define to 1 if translation of program messages to the user's native anguage is requested."),
+    ('WITH_WARNINGS', 'warnings', 'Define this if you want to see the warning directives put here and there by the developpers to get attention'),
+    ('_GLIBCXX_CONCEPT_CHECKS', 'concept_checks', 'libstdc++ concept checking'),
   ]
 
   for val in values:
     if (env.has_key(val[0]) and env[val[0]]) or \
-        ARGUMENTS.get(val[1]):
-      utils.addToConfig('#define %s 1' % val[0], TOP_SRC_DIR)
+       (env.has_key(val[1]) and env[val[1]]):
+      utils.addToConfig('#define %s 1' % val[0], desc = val[2])
     else:
-      utils.addToConfig('/* #undef %s */' % val[0], TOP_SRC_DIR)
+      utils.addToConfig('/* #undef %s */' % val[0], desc = val[2])
 
+  # disable automatic linking of boost libraries.
+  # This is an interesting feature that is supposed to be useful under
+  # windows but since I can not find a way to use it on all platforms, 
+  # I disable it for now.
+  utils.addToConfig('#define BOOST_ALL_NO_LIB 1')
 
-  env['EXTRA_LIBS'] = []
-  # HAVE_LIBAIKSAURUS
   # AIKSAURUS_H_LOCATION
-  if conf.CheckLib('Aiksaurus'):
-    utils.addToConfig("#define HAVE_LIBAIKSAURUS 1", TOP_SRC_DIR)
-    if (conf.CheckCXXHeader("Aiksaurus.h")):
-      utils.addToConfig("#define AIKSAURUS_H_LOCATION <Aiksaurus.h>", TOP_SRC_DIR)
-    elif (conf.CheckCXXHeader("Aiksaurus/Aiksaurus.h")):
-      utils.addToConfig("#define AIKSAURUS_H_LOCATION <Aiksaurus/Aiksaurus.h>", TOP_SRC_DIR)
-    else:
-      utils.addToConfig("#define AIKSAURUS_H_LOCATION", TOP_SRC_DIR)
-    env['EXTRA_LIBS'].append('Aiksaurus')
+  if (conf.CheckCXXHeader("Aiksaurus.h")):
+    utils.addToConfig("#define AIKSAURUS_H_LOCATION <Aiksaurus.h>")
+  elif (conf.CheckCXXHeader("Aiksaurus/Aiksaurus.h")):
+    utils.addToConfig("#define AIKSAURUS_H_LOCATION <Aiksaurus/Aiksaurus.h>")
+  else:
+    utils.addToConfig("#define AIKSAURUS_H_LOCATION")
 
   # USE_ASPELL
   # USE_PSPELL
@@ -842,27 +1068,24 @@ int count()
   spell_detected = False
   if spell_engine in ['auto', 'aspell'] and \
     conf.CheckLib('aspell'):
-    utils.addToConfig('#define USE_ASPELL 1', TOP_SRC_DIR)
+    utils.addToConfig('#define USE_ASPELL 1', desc = 'Define as 1 to use the aspell library')
     env['USE_ASPELL'] = True
     env['USE_PSPELL'] = False
     env['USE_ISPELL'] = False
-    env['EXTRA_LIBS'].append('aspell')
     spell_detected = True
   elif spell_engine in ['auto', 'pspell'] and \
     conf.CheckLib('pspell'):
-    utils.addToConfig('#define USE_PSPELL 1', TOP_SRC_DIR)
+    utils.addToConfig('#define USE_PSPELL 1', desc = 'Define as 1 to use the pspell library')
     env['USE_ASPELL'] = False
     env['USE_PSPELL'] = True
     env['USE_ISPELL'] = False
-    env['EXTRA_LIBS'].append('pspell')
     spell_detected = True
   elif spell_engine in ['auto', 'ispell'] and \
     conf.CheckLib('ispell'):
-    utils.addToConfig('#define USE_ISPELL 1', TOP_SRC_DIR)
+    utils.addToConfig('#define USE_ISPELL 1', desc = 'Define as 1 to use the ispell library')
     env['USE_ASPELL'] = False
     env['USE_PSPELL'] = False
     env['USE_ISPELL'] = True
-    env['EXTRA_LIBS'].append('ispell')
     spell_detected = True
 
   if not spell_detected:
@@ -879,49 +1102,45 @@ int count()
   # USE_MACOSX_PACKAGING
   # USE_WINDOWS_PACKAGING
   if packaging_method == 'windows':
-    utils.addToConfig('#define USE_WINDOWS_PACKAGING 1', TOP_SRC_DIR)
+    utils.addToConfig('#define USE_WINDOWS_PACKAGING 1')
   elif packaging_method == 'posix':
-    utils.addToConfig('#define USE_POSIX_PACKAGING 1', TOP_SRC_DIR)
+    utils.addToConfig('#define USE_POSIX_PACKAGING 1')
   elif packaging_method == 'mac':
-    utils.addToConfig('#define USE_MACOSX_PACKAGING 1', TOP_SRC_DIR)
+    utils.addToConfig('#define USE_MACOSX_PACKAGING 1')
 
   # BOOST_POSIX
   if boost_posix:
-    utils.addToConfig('#define BOOST_POSIX 1', TOP_SRC_DIR)
+    utils.addToConfig('#define BOOST_POSIX 1')
   else:
-    utils.addToConfig('/* #undef BOOST_POSIX */', TOP_SRC_DIR)
+    utils.addToConfig('/* #undef BOOST_POSIX */')
 
   # MKDIR_TAKES_ONE_ARG
+  description = 'Define if mkdir takes only one argument.'
   if conf.CheckMkdirOneArg():
-    utils.addToConfig('#define MKDIR_TAKES_ONE_ARG 1', TOP_SRC_DIR)
+    utils.addToConfig('#define MKDIR_TAKES_ONE_ARG 1', desc = description)
   else:
-    utils.addToConfig('/* #undef MKDIR_TAKES_ONE_ARG */', TOP_SRC_DIR)
+    utils.addToConfig('/* #undef MKDIR_TAKES_ONE_ARG */', desc = description)
 
   # SELECT_TYPE_ARG1
   # SELECT_TYPE_ARG234
   # SELECT_TYPE_ARG5
   (arg1, arg234, arg5) = conf.CheckSelectArgType()
-  utils.addToConfig('#define SELECT_TYPE_ARG1 %s' % arg1, TOP_SRC_DIR)
-  utils.addToConfig('#define SELECT_TYPE_ARG234 %s' % arg234, TOP_SRC_DIR)
-  utils.addToConfig('#define SELECT_TYPE_ARG5 %s' % arg5, TOP_SRC_DIR)
+  utils.addToConfig('#define SELECT_TYPE_ARG1 %s' % arg1,
+    desc = "Define to the type of arg 1 for `select'.")
+  utils.addToConfig('#define SELECT_TYPE_ARG234 %s' % arg234,
+    desc = "Define to the type of arg 2, 3, 4 for `select'.")
+  utils.addToConfig('#define SELECT_TYPE_ARG5 %s' % arg5,
+    desc = "Define to the type of arg 5 for `select'.")
 
   # mkstemp
   # USE_BOOST_FORMAT
   # WANT_GETFILEATTRIBUTESEX_WRAPPER
   utils.endConfigH(TOP_SRC_DIR)
 
-  # env['EXTRA_LIBS'] will be modified later, so a unique copy is needed
-  # NOTE that we do *not* save qt_libs in environment.
-  env_cache['EXTRA_LIBS'] = copy.copy(env['EXTRA_LIBS'])
-  env_cache['USE_ASPELL'] = env['USE_ASPELL']
-  env_cache['USE_PSPELL'] = env['USE_PSPELL']
-  env_cache['USE_ISPELL'] = env['USE_ISPELL']
-  env_cache['HAVE_ASPRINTF'] = env['HAVE_ASPRINTF']
-  env_cache['HAVE_WPRINTF'] = env['HAVE_WPRINTF']
-  env_cache['HAVE_SNPRINTF'] = env['HAVE_SNPRINTF']
-  env_cache['HAVE_POSIX_PRINTF'] = env['HAVE_POSIX_PRINTF']
-  env_cache['HAVE_FCNTL'] = env['HAVE_FCNTL']
-
+  for key in ['USE_ASPELL', 'USE_PSPELL', 'USE_ISPELL', 'HAVE_ASPRINTF', \
+    'HAVE_WPRINTF', 'HAVE_SNPRINTF', 'HAVE_POSIX_PRINTF', 'HAVE_FCNTL', \
+    'HAVE_ICONV', 'HAVE_LIBGDI32', 'HAVE_LIBC', 'HAVE_LIBAIKSAURUS']:
+    env_cache[key] = env[key]
 else:
   #
   # this comes as a big surprise, without this line 
@@ -930,15 +1149,11 @@ else:
   # Note that the exact header file to check does not matter
   conf.CheckCHeader('io.h')
   # only a few variables need to be rescanned
-  env['EXTRA_LIBS'] = copy.copy(env_cache['EXTRA_LIBS'])
-  env['USE_ASPELL'] = env_cache['USE_ASPELL']
-  env['USE_PSPELL'] = env_cache['USE_PSPELL']
-  env['USE_ISPELL'] = env_cache['USE_ISPELL']
-  env['HAVE_ASPRINTF'] = env_cache['HAVE_ASPRINTF']
-  env['HAVE_WPRINTF'] = env_cache['HAVE_WPRINTF']
-  env['HAVE_SNPRINTF'] = env_cache['HAVE_SNPRINTF']
-  env['HAVE_POSIX_PRINTF'] = env_cache['HAVE_POSIX_PRINTF']
-  env['HAVE_FCNTL'] = env_cache['HAVE_FCNTL']
+  for key in ['USE_ASPELL', 'USE_PSPELL', 'USE_ISPELL', 'HAVE_ASPRINTF', \
+    'HAVE_WPRINTF', 'HAVE_SNPRINTF', 'HAVE_POSIX_PRINTF', 'HAVE_FCNTL', \
+    'HAVE_ICONV', 'HAVE_LIBGDI32', 'HAVE_LIBC', 'HAVE_LIBAIKSAURUS']:
+    env[key] = env_cache[key]
+
 
 #
 # Finish auto-configuration
@@ -949,7 +1164,7 @@ env = conf.Finish()
 #----------------------------------------------------------
 
 #
-# QT_LIB etc (EXTRA_LIBS holds lib for each frontend)
+# QT_LIB
 #
 # NOTE: Tool('qt') or Tool('qt4') will be loaded later
 # in their respective directory and specialized env.
@@ -957,18 +1172,15 @@ try:
   if frontend == 'qt3':
     # note: env.Tool('qt') my set QT_LIB to qt
     env['QT_LIB'] = 'qt-mt'
-    env['EXTRA_LIBS'].append('qt-mt')
+    env['FRONTEND_LIBS'] = ['qt-mt']
     if platform_name == 'cygwin' and use_X11:
-      env['EXTRA_LIBS'].extend(['GL',  'Xmu', 'Xi', 'Xrender', 'Xrandr', 'Xcursor',
-        'Xft', 'freetype', 'fontconfig', 'Xext', 'X11', 'SM', 'ICE', 'resolv',
-        'pthread'])
       env.AppendUnique(LIBPATH = ['/usr/X11R6/lib'])
   elif frontend == 'qt4':
     if platform_name == "win32":
       env['QT_LIB'] = ['QtCore4', 'QtGui4']
     else:
       env['QT_LIB'] = ['QtCore', 'QtGui']
-    env['EXTRA_LIBS'] += env['QT_LIB']
+    env['FRONTEND_LIBS'] = env['QT_LIB']
 except:
   print "Can not locate qt tools"
   print "What I get is "
@@ -978,10 +1190,30 @@ except:
 if platform_name in ['win32', 'cygwin']:
   # the final link step needs stdc++ to succeed under mingw
   # FIXME: shouldn't g++ automatically link to stdc++?
-  env['SYSTEM_LIBS'] = ['shlwapi', 'z', 'stdc++']
+  if use_vc:
+    env['SYSTEM_LIBS'] = ['shlwapi', 'shell32', 'advapi32', 'zdll']
+  else:
+    env['SYSTEM_LIBS'] = ['shlwapi', 'stdc++', 'z']
+elif platform_name == 'cygwin' and use_X11:
+  env['SYSTEM_LIBS'] = ['GL',  'Xmu', 'Xi', 'Xrender', 'Xrandr', 'Xcursor',
+    'Xft', 'freetype', 'fontconfig', 'Xext', 'X11', 'SM', 'ICE', 'resolv',
+    'pthread', 'z']
 else:
   env['SYSTEM_LIBS'] = ['z']
 
+libs = [
+  ('HAVE_ICONV', 'iconv'),
+  ('HAVE_LIBGDI32', 'gdi32'),
+  ('HAVE_LIBAIKSAURUS', 'Aiksaurus'),
+  ('USE_ASPELL', 'aspell'),
+  ('USE_ISPELL', 'ispell'),
+  ('USE_PSPELL', 'pspell'),
+]
+
+for lib in libs:
+  if env[lib[0]]:
+    env['SYSTEM_LIBS'].append(lib[1])
+
 #
 # Build parameters CPPPATH etc
 #
@@ -991,12 +1223,18 @@ else:
 env['CPPPATH'].remove(env['QT_INC_PATH'])
 env['CPPPATH'] += ['$TOP_SRC_DIR/boost', '$TOP_SRC_DIR/src']
 
-# TODO: add (more) appropriate compiling options (-DNDEBUG etc)
+# add appropriate compiling options (-DNDEBUG etc)
 # for debug/release mode
 if ARGUMENTS.get('mode', default_build_mode) == 'debug':
-  env.AppendUnique(CCFLAGS = [])
+  if use_vc:
+    env.AppendUnique(CCFLAGS = [])
+  else:
+    env.AppendUnique(CCFLAGS = ['-Wall', '-g'])
 else:
-  env.AppendUnique(CCFLAGS = [])
+  if use_vc:
+    env.AppendUnique(CCFLAGS = ['/O2'])
+  else:
+    env.AppendUnique(CCFLAGS = ['-Wall', '-O2'])
 
 #
 # Customized builders
@@ -1021,10 +1259,11 @@ if platform_name == 'cygwin':
 #
 # src/support/package.C.in needs the following to replace
 #  LYX_ABS_INSTALLED_DATADIR (e.g. /usr/local/lyx/share/lyx)
-env['LYX_DIR'] = env['SHARE_DIR']
+env['LYX_DIR'] = Dir(env['SHARE_DIR']).abspath
 #  LYX_ABS_INSTALLED_LOCALEDIR
-env['LOCALEDIR'] = env['LOCALE_DIR']
-env['TOP_SRCDIR'] = env['TOP_SRC_DIR']
+env['LOCALEDIR'] = Dir(env['LOCALE_DIR']).abspath
+# during variable substitution, absolute path is needed.
+env['TOP_SRCDIR'] = Dir(env['TOP_SRC_DIR']).abspath
 # needed by src/version.C.in => src/version.C
 env['PACKAGE_VERSION'] = PACKAGE_VERSION
 # fill in the version info
@@ -1043,7 +1282,7 @@ Build info:
   Local library directory:        %s
   Libraries pathes:               %s
   Boost libraries:                %s
-  Extra libraries:                %s
+  Frontend libraries:             %s
   System libraries:               %s
   include search path:            %s
 Frontend:
@@ -1060,9 +1299,9 @@ Frontend:
   env.subst('$LINKFLAGS'), env.subst('$LINKFLAGS'),
   env.subst('$BUILDDIR'), env.subst('$LOCALLIBPATH'),
   str(env['LIBPATH']), str(env['BOOST_LIBRARIES']),
-  str(env['EXTRA_LIBS']), str(env['SYSTEM_LIBS']), str(env['CPPPATH']),
+  str(env['FRONTEND_LIBS']), str(env['SYSTEM_LIBS']), str(env['CPPPATH']),
   env['frontend'], packaging_method,
-  env['PREFIX'], env['BIN_DIR'], env['SHARE_DIR'])
+  env['PREFIX'], env['BIN_DEST_DIR'], env['SHARE_DIR'])
 
 if env['frontend'] in ['qt3', 'qt4']:
   env['VERSION_INFO'] += '''  include dir:                    %s
@@ -1114,6 +1353,10 @@ env.SConsignFile(os.path.join(Dir(env['BUILDDIR']).abspath, '.sconsign'))
 #env.CacheDir('%s/Cache/%s' % (env['BUILDDIR'], frontend))
 
 env['BUILD_TARGETS'] = BUILD_TARGETS
+if env.has_key('rebuild'):
+  env['REBUILD_TARGETS'] = env['rebuild'].split(',')
+else:
+  env['REBUILD_TARGETS'] = None
 
 print "Building all targets recursively"