christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1 | # Copyright 2008 Google Inc. All rights reserved. |
| 2 | # Redistribution and use in source and binary forms, with or without |
| 3 | # modification, are permitted provided that the following conditions are |
| 4 | # met: |
| 5 | # |
| 6 | # * Redistributions of source code must retain the above copyright |
| 7 | # notice, this list of conditions and the following disclaimer. |
| 8 | # * Redistributions in binary form must reproduce the above |
| 9 | # copyright notice, this list of conditions and the following |
| 10 | # disclaimer in the documentation and/or other materials provided |
| 11 | # with the distribution. |
| 12 | # * Neither the name of Google Inc. nor the names of its |
| 13 | # contributors may be used to endorse or promote products derived |
| 14 | # from this software without specific prior written permission. |
| 15 | # |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | import platform |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 29 | import re |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 30 | import sys |
| 31 | from os.path import join, dirname, abspath |
| 32 | root_dir = dirname(File('SConstruct').rfile().abspath) |
| 33 | sys.path.append(join(root_dir, 'tools')) |
| 34 | import js2c |
| 35 | |
| 36 | |
| 37 | def Abort(message): |
| 38 | print message |
| 39 | sys.exit(1) |
| 40 | |
| 41 | |
| 42 | def GuessOS(): |
| 43 | id = platform.system() |
| 44 | if id == 'Linux': |
| 45 | return 'linux' |
| 46 | elif id == 'Darwin': |
| 47 | return 'macos' |
| 48 | elif id == 'Windows': |
| 49 | return 'win32' |
| 50 | else: |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 51 | return '<none>' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def GuessProcessor(): |
| 55 | id = platform.machine() |
| 56 | if id.startswith('arm'): |
| 57 | return 'arm' |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 58 | elif (not id) or (not re.match('(x|i[3-6])86', id) is None): |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | return 'ia32' |
| 60 | else: |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 61 | return '<none>' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | def GuessToolchain(os): |
| 65 | tools = Environment()['TOOLS'] |
| 66 | if 'gcc' in tools: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 67 | return 'gcc' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | elif 'msvc' in tools: |
| 69 | return 'msvc' |
| 70 | else: |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 71 | return '<none>' |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def GetOptions(): |
| 75 | result = Options() |
| 76 | os_guess = GuessOS() |
| 77 | toolchain_guess = GuessToolchain(os_guess) |
| 78 | processor_guess = GuessProcessor() |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 79 | result.Add('mode', 'compilation mode (debug, release)', 'release') |
| 80 | result.Add('toolchain', 'the toolchain to use (gcc, msvc)', toolchain_guess) |
| 81 | result.Add('os', 'the os to build for (linux, macos, win32)', os_guess) |
| 82 | result.Add('processor', 'the processor to build for (arm, ia32)', processor_guess) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | result.Add('snapshot', 'build using snapshots for faster start-up (on, off)', 'off') |
| 84 | result.Add('library', 'which type of library to produce (static, shared, default)', 'default') |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 85 | result.Add('sample', 'build sample (process, shell)', '') |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 86 | return result |
| 87 | |
| 88 | |
| 89 | def VerifyOptions(env): |
| 90 | if not env['mode'] in ['debug', 'release']: |
| 91 | Abort("Unknown build mode '%s'." % env['mode']) |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 92 | if not env['toolchain'] in ['gcc', 'msvc']: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 93 | Abort("Unknown toolchain '%s'." % env['toolchain']) |
| 94 | if not env['os'] in ['linux', 'macos', 'win32']: |
| 95 | Abort("Unknown os '%s'." % env['os']) |
| 96 | if not env['processor'] in ['arm', 'ia32']: |
| 97 | Abort("Unknown processor '%s'." % env['processor']) |
| 98 | if not env['snapshot'] in ['on', 'off']: |
| 99 | Abort("Illegal value for option snapshot: '%s'." % env['snapshot']) |
| 100 | if not env['library'] in ['static', 'shared', 'default']: |
| 101 | Abort("Illegal value for option library: '%s'." % env['library']) |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 102 | if not env['sample'] in ['', 'process', 'shell']: |
| 103 | Abort("Illegal value for option sample: '%s'." % env['sample']) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | |
| 105 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 106 | def Build(): |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | opts = GetOptions() |
| 108 | env = Environment(options=opts) |
| 109 | Help(opts.GenerateHelpText(env)) |
| 110 | VerifyOptions(env) |
| 111 | |
| 112 | os = env['os'] |
| 113 | arch = env['processor'] |
| 114 | toolchain = env['toolchain'] |
| 115 | mode = env['mode'] |
| 116 | use_snapshot = (env['snapshot'] == 'on') |
| 117 | library_type = env['library'] |
| 118 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 119 | # Build the object files by invoking SCons recursively. |
| 120 | object_files = env.SConscript( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 121 | join('src', 'SConscript'), |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 122 | build_dir='build', |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | exports='toolchain arch os mode use_snapshot library_type', |
| 124 | duplicate=False |
| 125 | ) |
| 126 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 127 | # Link the object files into a library. |
| 128 | if library_type == 'static': |
| 129 | library = env.StaticLibrary('v8', object_files) |
| 130 | elif library_type == 'shared': |
| 131 | # There seems to be a glitch in the way scons decides where to put |
| 132 | # PDB files when compiling using MSVC so we specify it manually. |
| 133 | # This should not affect any other platforms. |
| 134 | library = env.SharedLibrary('v8', object_files, PDB='v8.dll.pdb') |
| 135 | else: |
| 136 | library = env.Library('v8', object_files) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame^] | 138 | # Bail out if we're not building any sample. |
| 139 | sample = env['sample'] |
| 140 | if not sample: return |
| 141 | |
| 142 | # Build the sample. |
| 143 | env.Replace(CPPPATH='public') |
| 144 | object_path = join('build', 'samples', sample) |
| 145 | source_path = join('samples', sample + '.cc') |
| 146 | object = env.Object(object_path, source_path) |
| 147 | if toolchain == 'gcc': |
| 148 | env.Program(sample, [object, library], LIBS='pthread') |
| 149 | else: |
| 150 | env.Program(sample, [object, library], LIBS='WS2_32') |
| 151 | |
| 152 | |
| 153 | Build() |