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

import platform
29
import re
30
import sys
31
import os
32
from os.path import join, dirname, abspath
33
from types import DictType
34 35
root_dir = dirname(File('SConstruct').rfile().abspath)
sys.path.append(join(root_dir, 'tools'))
36
import js2c, utils
37 38


39 40 41 42 43 44 45
LIBRARY_FLAGS = {
  'all': {
    'CPPDEFINES':   ['ENABLE_LOGGING_AND_PROFILING']
  },
  'gcc': {
    'all': {
      'DIALECTFLAGS': ['-ansi'],
46 47
      'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS',
          '-fno-strict-aliasing'],
48
      'CXXFLAGS':     ['$CCFLAGS', '-fno-rtti', '-fno-exceptions'],
49 50 51 52 53 54 55 56 57
      'LIBS':         ['pthread']
    },
    'mode:debug': {
      'CCFLAGS':      ['-g', '-O0'],
      'CPPDEFINES':   ['ENABLE_DISASSEMBLER', 'DEBUG']
    },
    'mode:release': {
      'CCFLAGS':      ['-O2']
    },
58
    'wordsize:64': {
59 60
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32']
61
    }
62 63 64 65 66 67
  },
  'msvc': {
    'all': {
      'DIALECTFLAGS': ['/nologo'],
      'WARNINGFLAGS': ['/W3', '/WX', '/wd4355', '/wd4800'],
      'CCFLAGS':      ['$DIALECTFLAGS', '$WARNINGFLAGS'],
68
      'CXXFLAGS':     ['$CCFLAGS', '/GR-', '/Gy'],
69
      'CPPDEFINES':   ['WIN32', '_USE_32BIT_TIME_T', 'PCRE_STATIC'],
70 71 72 73 74 75 76 77 78 79 80 81
      'LINKFLAGS':    ['/NOLOGO', '/MACHINE:X86', '/INCREMENTAL:NO',
          '/NXCOMPAT', '/IGNORE:4221'],
      'ARFLAGS':      ['/NOLOGO'],
      'CCPDBFLAGS':   ['/Zi']
    },
    'mode:debug': {
      'CCFLAGS':      ['/Od', '/Gm', '/MTd'],
      'CPPDEFINES':   ['_DEBUG', 'ENABLE_DISASSEMBLER', 'DEBUG'],
      'LINKFLAGS':    ['/DEBUG']
    },
    'mode:release': {
      'CCFLAGS':      ['/Ox', '/MT', '/Ob2', '/Oi', '/Oy'],
82
      'LINKFLAGS':    ['/OPT:REF', '/OPT:ICF']
83 84 85 86 87 88 89 90
    }
  }
}


V8_EXTRA_FLAGS = {
  'gcc': {
    'all': {
91
      'CXXFLAGS':     [], #['-fvisibility=hidden'],
92 93 94
      'WARNINGFLAGS': ['-pedantic', '-Wall', '-Werror', '-W',
          '-Wno-unused-parameter']
    },
95 96 97
    'arch:arm': {
      'CPPDEFINES':   ['ARM']
    },
98 99 100
    'disassembler:on': {
      'CPPDEFINES':   ['ENABLE_DISASSEMBLER']
    }
101 102 103 104 105 106 107 108
  },
  'msvc': {
    'all': {
      'WARNINGFLAGS': ['/W3', '/WX', '/wd4355', '/wd4800']
    },
    'library:shared': {
      'CPPDEFINES':   ['BUILDING_V8_SHARED']
    },
109 110 111
    'arch:arm': {
      'CPPDEFINES':   ['ARM']
    },
112 113 114
    'disassembler:on': {
      'CPPDEFINES':   ['ENABLE_DISASSEMBLER']
    }
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  }
}


JSCRE_EXTRA_FLAGS = {
  'gcc': {
    'all': {
      'CPPDEFINES':   ['SUPPORT_UTF8', 'NO_RECURSE', 'SUPPORT_UCP'],
      'WARNINGFLAGS': ['-w']
    },
  },
  'msvc': {
    'all': {
      'CPPDEFINES':   ['SUPPORT_UTF8', 'NO_RECURSE', 'SUPPORT_UCP'],
      'WARNINGFLAGS': ['/W3', '/WX', '/wd4355', '/wd4800']
    },
    'library:shared': {
      'CPPDEFINES':   ['BUILDING_V8_SHARED']
    }
  }
}


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


CCTEST_EXTRA_FLAGS = {
  'all': {
    'CPPPATH': [join(root_dir, 'src')],
    'LIBS': ['$LIBRARY']
  },
  'gcc': {
    'all': {
      'LIBPATH': [abspath('.')]
160 161 162 163 164
    },
    'wordsize:64': {
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32']
    },
165 166
  },
  'msvc': {
167 168 169
    'all': {
      'CPPDEFINES': ['_HAS_EXCEPTIONS=0']
    },
170 171 172 173 174 175 176 177 178
    'library:shared': {
      'CPPDEFINES': ['USING_V8_SHARED']
    }
  }
}


SAMPLE_FLAGS = {
  'all': {
179
    'CPPPATH': [join(abspath('.'), 'include')],
180 181 182 183 184 185 186
    'LIBS': ['$LIBRARY'],
  },
  'gcc': {
    'all': {
      'LIBS': ['pthread'],
      'LIBPATH': ['.']
    },
187 188 189 190
    'wordsize:64': {
      'CCFLAGS':      ['-m32'],
      'LINKFLAGS':    ['-m32']
    },
191 192 193
    'mode:debug': {
      'CCFLAGS':      ['-g', '-O0']
    }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  },
  'msvc': {
    'all': {
      'CCFLAGS': ['/nologo'],
    },
    'library:shared': {
      'CPPDEFINES': ['USING_V8_SHARED']
    },
    'mode:release': {
      'CCFLAGS': ['/MT'],
    },
    'mode:debug': {
      'CCFLAGS': ['/MTd']
    }
  }
}


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


218 219 220 221 222 223 224 225 226 227 228 229 230 231
def Abort(message):
  print message
  sys.exit(1)


def GuessOS():
  id = platform.system()
  if id == 'Linux':
    return 'linux'
  elif id == 'Darwin':
    return 'macos'
  elif id == 'Windows':
    return 'win32'
  else:
232
    return None
233 234


235 236 237 238 239
def GuessWordsize():
  if '64' in platform.machine():
    return '64'
  else:
    return '32'
240 241 242 243 244


def GuessToolchain(os):
  tools = Environment()['TOOLS']
  if 'gcc' in tools:
245
    return 'gcc'
246 247 248
  elif 'msvc' in tools:
    return 'msvc'
  else:
249 250 251 252 253
    return None


OS_GUESS = GuessOS()
TOOLCHAIN_GUESS = GuessToolchain(OS_GUESS)
254
ARCH_GUESS = utils.GuessArchitecture()
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
WORDSIZE_GUESS = GuessWordsize()


SIMPLE_OPTIONS = {
  'toolchain': {
    'values': ['gcc', 'msvc'],
    'default': TOOLCHAIN_GUESS,
    'help': 'the toolchain to use'
  },
  'os': {
    'values': ['linux', 'macos', 'win32'],
    'default': OS_GUESS,
    'help': 'the os to build for'
  },
  'arch': {
    'values':['arm', 'ia32'],
    'default': ARCH_GUESS,
    'help': 'the architecture to build for'
  },
  'snapshot': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'build using snapshots for faster start-up'
  },
  'library': {
280 281
    'values': ['static', 'shared'],
    'default': 'static',
282 283 284 285 286 287 288 289 290 291 292
    'help': 'the type of library to produce'
  },
  'wordsize': {
    'values': ['64', '32'],
    'default': WORDSIZE_GUESS,
    'help': 'the word size'
  },
  'simulator': {
    'values': ['arm', 'none'],
    'default': 'none',
    'help': 'build with simulator'
293 294 295 296 297
  },
  'disassembler': {
    'values': ['on', 'off'],
    'default': 'off',
    'help': 'enable the disassembler to inspect generated code'
298 299 300 301 302
  },
  'sourcesignatures': {
    'values': ['MD5', 'timestamp'],
    'default': 'MD5',
    'help': 'set how the build system detects file changes'
303 304
  }
}
305 306 307 308


def GetOptions():
  result = Options()
309
  result.Add('mode', 'compilation mode (debug, release)', 'release')
310
  result.Add('sample', 'build sample (shell, process)', '')
311
  result.Add('env', 'override environment settings (NAME1:value1,NAME2:value2)', '')
312 313 314
  for (name, option) in SIMPLE_OPTIONS.items():
    help = '%s (%s)' % (name, ", ".join(option['values']))
    result.Add(name, help, option.get('default'))
315 316 317
  return result


318 319 320 321 322 323 324 325 326 327 328 329 330
def SplitList(str):
  return [ s for s in str.split(",") if len(s) > 0 ]


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


331
def VerifyOptions(env):
332 333 334 335
  if not IsLegal(env, 'mode', ['debug', 'release']):
    return False
  if not IsLegal(env, 'sample', ["shell", "process"]):
    return False
336 337 338 339 340 341 342 343 344
  for (name, option) in SIMPLE_OPTIONS.items():
    if (not option.get('default')) and (name not in ARGUMENTS):
      message = ("A value for option %s must be specified (%s)." %
          (name, ", ".join(option['values'])))
      Abort(message)
    if not env[name] in option['values']:
      message = ("Unknown %s value '%s'.  Possible values are (%s)." %
          (name, env[name], ", ".join(option['values'])))
      Abort(message)
345 346


347 348
class BuildContext(object):

349
  def __init__(self, options, env_overrides, samples):
350 351 352
    self.library_targets = []
    self.cctest_targets = []
    self.sample_targets = []
353
    self.options = options
354
    self.env_overrides = env_overrides
355
    self.samples = samples
356
    self.use_snapshot = (options['snapshot'] == 'on')
357 358 359 360 361
    self.flags = None
  
  def AddRelevantFlags(self, initial, flags):
    result = initial.copy()
    self.AppendFlags(result, flags.get('all'))
362 363 364 365 366
    toolchain = self.options['toolchain']
    self.AppendFlags(result, flags[toolchain].get('all'))
    for option in sorted(self.options.keys()):
      value = self.options[option]
      self.AppendFlags(result, flags[toolchain].get(option + ':' + value))
367 368 369 370 371
    return result
  
  def GetRelevantSources(self, source):
    result = []
    result += source.get('all', [])
372 373 374
    for (name, value) in self.options.items():
      result += source.get(name + ':' + value, [])
    return sorted(result)
375 376 377 378 379 380 381 382 383

  def AppendFlags(self, options, added):
    if not added:
      return
    for (key, value) in added.items():
      if not key in options:
        options[key] = value
      else:
        options[key] = options[key] + value
384

385
  def ConfigureObject(self, env, input, **kw):
386
    if self.options['library'] == 'static':
387 388
      return env.StaticObject(input, **kw)
    else:
389
      return env.SharedObject(input, **kw)
390

391 392 393 394 395 396 397 398
  def ApplyEnvOverrides(self, env):
    if not self.env_overrides:
      return
    if type(env['ENV']) == DictType:
      env['ENV'].update(**self.env_overrides)
    else:
      env['ENV'] = self.env_overrides

399

400 401 402 403 404 405 406 407 408
def PostprocessOptions(options):
  # Adjust architecture if the simulator option has been set
  if (options['simulator'] != 'none') and (options['arch'] != options['simulator']):
    if 'arch' in ARGUMENTS:
      # Print a warning if arch has explicitly been set
      print "Warning: forcing architecture to match simulator (%s)" % options['simulator']
    options['arch'] = options['simulator']


409 410 411 412 413 414 415 416 417 418 419 420
def ParseEnvOverrides(arg):
  # The environment overrides are in the format NAME1:value1,NAME2:value2
  overrides = {}
  for override in arg.split(','):
    pos = override.find(':')
    if pos == -1:
      continue
    overrides[override[:pos].strip()] = override[pos+1:].strip()
  return overrides


def BuildSpecific(env, mode, env_overrides):
421 422 423 424 425
  options = {'mode': mode}
  for option in SIMPLE_OPTIONS:
    options[option] = env[option]
  PostprocessOptions(options)

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

428
  library_flags = context.AddRelevantFlags(os.environ, LIBRARY_FLAGS)
429 430 431 432
  v8_flags = context.AddRelevantFlags(library_flags, V8_EXTRA_FLAGS)
  jscre_flags = context.AddRelevantFlags(library_flags, JSCRE_EXTRA_FLAGS)
  dtoa_flags = context.AddRelevantFlags(library_flags, DTOA_EXTRA_FLAGS)
  cctest_flags = context.AddRelevantFlags(v8_flags, CCTEST_EXTRA_FLAGS)  
433
  sample_flags = context.AddRelevantFlags(os.environ, SAMPLE_FLAGS)
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

  context.flags = {
    'v8': v8_flags,
    'jscre': jscre_flags,
    'dtoa': dtoa_flags,
    'cctest': cctest_flags,
    'sample': sample_flags
  }
  
  target_id = mode
  suffix = SUFFIXES[target_id]
  library_name = 'v8' + suffix
  env['LIBRARY'] = library_name

  # Build the object files by invoking SCons recursively.  
449
  object_files = env.SConscript(
450
    join('src', 'SConscript'),
451 452
    build_dir=join('obj', target_id),
    exports='context',
453 454
    duplicate=False
  )
455
  
456
  # Link the object files into a library.
457
  env.Replace(**context.flags['v8'])
458
  context.ApplyEnvOverrides(env)
459
  if context.options['library'] == 'static':
460
    library = env.StaticLibrary(library_name, object_files)
461
  else:
462 463 464
    # There seems to be a glitch in the way scons decides where to put
    # PDB files when compiling using MSVC so we specify it manually.
    # This should not affect any other platforms.
465 466 467 468 469 470 471
    pdb_name = library_name + '.dll.pdb'
    library = env.SharedLibrary(library_name, object_files, PDB=pdb_name)
  context.library_targets.append(library)
  
  for sample in context.samples:
    sample_env = Environment(LIBRARY=library_name)
    sample_env.Replace(**context.flags['sample'])
472
    context.ApplyEnvOverrides(sample_env)
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    sample_object = sample_env.SConscript(
      join('samples', 'SConscript'),
      build_dir=join('obj', 'sample', sample, target_id),
      exports='sample context',
      duplicate=False
    )
    sample_name = sample + suffix
    sample_program = sample_env.Program(sample_name, sample_object)
    sample_env.Depends(sample_program, library)
    context.sample_targets.append(sample_program)
  
  cctest_program = env.SConscript(
    join('test', 'cctest', 'SConscript'),
    build_dir=join('obj', 'test', target_id),
    exports='context object_files',
    duplicate=False
  )
  context.cctest_targets.append(cctest_program)
  
  return context


def Build():
  opts = GetOptions()
  env = Environment(options=opts)
  Help(opts.GenerateHelpText(env))
  VerifyOptions(env)
500
  env_overrides = ParseEnvOverrides(env['env'])
501
  
502 503
  SourceSignatures(env['sourcesignatures'])
  
504 505 506 507 508
  libraries = []
  cctests = []
  samples = []
  modes = SplitList(env['mode'])
  for mode in modes:
509
    context = BuildSpecific(env.Copy(), mode, env_overrides)
510 511 512 513 514 515 516 517 518 519
    libraries += context.library_targets
    cctests += context.cctest_targets
    samples += context.sample_targets

  env.Alias('library', libraries)
  env.Alias('cctests', cctests)
  env.Alias('sample', samples)
  
  if env['sample']:
    env.Default('sample')
520
  else:
521
    env.Default('library')
522

523

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


535
Build()