OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import contextlib |
| 7 import optparse |
| 8 import os |
| 9 import shutil |
| 10 import subprocess |
| 11 import sys |
| 12 import tempfile |
| 13 |
| 14 |
| 15 GN_ROOT = os.path.abspath(os.path.join( |
| 16 os.path.dirname(os.path.abspath(__file__)), '..')) |
| 17 |
| 18 SRC_ROOT = os.path.abspath(os.path.join( |
| 19 GN_ROOT, '..', '..')) |
| 20 |
| 21 BUILD_ROOT = os.path.abspath(os.path.join( |
| 22 SRC_ROOT, 'out', 'Release')) |
| 23 |
| 24 |
| 25 def is_linux(): |
| 26 return sys.platform.startswith('linux') |
| 27 |
| 28 |
| 29 @contextlib.contextmanager |
| 30 def scoped_tempdir(): |
| 31 path = tempfile.mkdtemp() |
| 32 try: |
| 33 yield path |
| 34 finally: |
| 35 shutil.rmtree(path) |
| 36 |
| 37 |
| 38 def main(argv): |
| 39 parser = optparse.OptionParser() |
| 40 parser.add_option('-o', '--output', |
| 41 help='place output in PATH', metavar='PATH') |
| 42 options, args = parser.parse_args(argv) |
| 43 |
| 44 if not options.output: |
| 45 parser.error('Please specify --output.') |
| 46 |
| 47 if args: |
| 48 parser.error('Unrecognized command line arguments: %s.' % ', '.join(args)) |
| 49 |
| 50 with scoped_tempdir() as tempdir: |
| 51 print 'Building gn manually...' |
| 52 write_ninja(os.path.join(tempdir, 'build.ninja')) |
| 53 subprocess.check_call(['ninja', '-C', tempdir, 'gn']) |
| 54 |
| 55 print 'Building gn using itself...' |
| 56 subprocess.check_call([ |
| 57 os.path.join(tempdir, 'gn'), |
| 58 '--args=is_debug=false', |
| 59 '--output=%s' % os.path.relpath(BUILD_ROOT, SRC_ROOT)]) |
| 60 subprocess.check_call(['ninja', '-C', BUILD_ROOT, 'gn']) |
| 61 subprocess.check_call(['strip', os.path.join(BUILD_ROOT, 'gn')]) |
| 62 |
| 63 # Preserve the executable permission bit. |
| 64 shutil.copy2(os.path.join(BUILD_ROOT, 'gn'), options.output) |
| 65 |
| 66 return 0 |
| 67 |
| 68 |
| 69 def write_ninja(path): |
| 70 cflags = os.environ.get('CFLAGS', '').split() |
| 71 ldflags = os.environ.get('LDFLAGS', '').split() |
| 72 include_dirs = [SRC_ROOT] |
| 73 libs = [] |
| 74 |
| 75 static_libraries = { |
| 76 'base': {'sources': [], 'tool': 'cxx'}, |
| 77 'dynamic_annotations': {'sources': [], 'tool': 'cc'}, |
| 78 'gn': {'sources': [], 'tool': 'cxx'}, |
| 79 } |
| 80 |
| 81 for name in os.listdir(GN_ROOT): |
| 82 if not name.endswith('.cc'): |
| 83 continue |
| 84 if name.endswith('_unittest.cc'): |
| 85 continue |
| 86 if name in ['generate_test_gn_data.cc']: |
| 87 continue |
| 88 full_path = os.path.join(GN_ROOT, name) |
| 89 static_libraries['gn']['sources'].append( |
| 90 os.path.relpath(full_path, SRC_ROOT)) |
| 91 |
| 92 static_libraries['dynamic_annotations']['sources'].extend([ |
| 93 'base/third_party/dynamic_annotations/dynamic_annotations.c', |
| 94 ]) |
| 95 static_libraries['base']['sources'].extend([ |
| 96 'base/at_exit.cc', |
| 97 'base/atomicops_internals_x86_gcc.cc', |
| 98 'base/base_paths.cc', |
| 99 'base/base_switches.cc', |
| 100 'base/callback_internal.cc', |
| 101 'base/command_line.cc', |
| 102 'base/debug/alias.cc', |
| 103 'base/debug/stack_trace.cc', |
| 104 'base/debug/trace_event_impl.cc', |
| 105 'base/debug/trace_event_impl_constants.cc', |
| 106 'base/debug/trace_event_memory.cc', |
| 107 'base/debug/trace_event_synthetic_delay.cc', |
| 108 'base/environment.cc', |
| 109 'base/file_util.cc', |
| 110 'base/files/file.cc', |
| 111 'base/files/file_enumerator.cc', |
| 112 'base/files/file_path.cc', |
| 113 'base/files/file_path_constants.cc', |
| 114 'base/json/json_parser.cc', |
| 115 'base/json/json_reader.cc', |
| 116 'base/json/json_string_value_serializer.cc', |
| 117 'base/json/json_writer.cc', |
| 118 'base/json/string_escape.cc', |
| 119 'base/lazy_instance.cc', |
| 120 'base/location.cc', |
| 121 'base/logging.cc', |
| 122 'base/memory/ref_counted.cc', |
| 123 'base/memory/ref_counted_memory.cc', |
| 124 'base/memory/singleton.cc', |
| 125 'base/memory/weak_ptr.cc', |
| 126 'base/message_loop/incoming_task_queue.cc', |
| 127 'base/message_loop/message_loop.cc', |
| 128 'base/message_loop/message_loop_proxy.cc', |
| 129 'base/message_loop/message_loop_proxy_impl.cc', |
| 130 'base/message_loop/message_pump.cc', |
| 131 'base/message_loop/message_pump_default.cc', |
| 132 'base/metrics/bucket_ranges.cc', |
| 133 'base/metrics/histogram.cc', |
| 134 'base/metrics/histogram_base.cc', |
| 135 'base/metrics/histogram_samples.cc', |
| 136 'base/metrics/sample_map.cc', |
| 137 'base/metrics/sample_vector.cc', |
| 138 'base/metrics/sparse_histogram.cc', |
| 139 'base/metrics/statistics_recorder.cc', |
| 140 'base/path_service.cc', |
| 141 'base/pending_task.cc', |
| 142 'base/pickle.cc', |
| 143 'base/process/kill.cc', |
| 144 'base/process/process_iterator.cc', |
| 145 'base/process/process_metrics.cc', |
| 146 'base/profiler/alternate_timer.cc', |
| 147 'base/profiler/tracked_time.cc', |
| 148 'base/run_loop.cc', |
| 149 'base/sequence_checker_impl.cc', |
| 150 'base/sequenced_task_runner.cc', |
| 151 'base/strings/string16.cc', |
| 152 'base/strings/string_number_conversions.cc', |
| 153 'base/strings/string_piece.cc', |
| 154 'base/strings/string_split.cc', |
| 155 'base/strings/string_util.cc', |
| 156 'base/strings/string_util_constants.cc', |
| 157 'base/strings/stringprintf.cc', |
| 158 'base/strings/utf_string_conversion_utils.cc', |
| 159 'base/strings/utf_string_conversions.cc', |
| 160 'base/synchronization/cancellation_flag.cc', |
| 161 'base/synchronization/lock.cc', |
| 162 'base/sys_info.cc', |
| 163 'base/task_runner.cc', |
| 164 'base/third_party/dmg_fp/dtoa_wrapper.cc', |
| 165 'base/third_party/dmg_fp/g_fmt.cc', |
| 166 'base/third_party/icu/icu_utf.cc', |
| 167 'base/third_party/nspr/prtime.cc', |
| 168 'base/thread_task_runner_handle.cc', |
| 169 'base/threading/non_thread_safe_impl.cc', |
| 170 'base/threading/post_task_and_reply_impl.cc', |
| 171 'base/threading/sequenced_worker_pool.cc', |
| 172 'base/threading/simple_thread.cc', |
| 173 'base/threading/thread_checker_impl.cc', |
| 174 'base/threading/thread_collision_warner.cc', |
| 175 'base/threading/thread_id_name_manager.cc', |
| 176 'base/threading/thread_local_storage.cc', |
| 177 'base/threading/thread_restrictions.cc', |
| 178 'base/time/time.cc', |
| 179 'base/timer/elapsed_timer.cc', |
| 180 'base/timer/timer.cc', |
| 181 'base/tracked_objects.cc', |
| 182 'base/tracking_info.cc', |
| 183 'base/values.cc', |
| 184 'base/vlog.cc', |
| 185 ]) |
| 186 |
| 187 if is_linux(): |
| 188 static_libraries['libevent'] = { |
| 189 'sources': [ |
| 190 'third_party/libevent/buffer.c', |
| 191 'third_party/libevent/epoll.c', |
| 192 'third_party/libevent/evbuffer.c', |
| 193 'third_party/libevent/evdns.c', |
| 194 'third_party/libevent/event.c', |
| 195 'third_party/libevent/event_tagging.c', |
| 196 'third_party/libevent/evrpc.c', |
| 197 'third_party/libevent/evutil.c', |
| 198 'third_party/libevent/http.c', |
| 199 'third_party/libevent/log.c', |
| 200 'third_party/libevent/poll.c', |
| 201 'third_party/libevent/select.c', |
| 202 'third_party/libevent/signal.c', |
| 203 'third_party/libevent/strlcpy.c', |
| 204 ], |
| 205 'tool': 'cc', |
| 206 'include_dirs': [ |
| 207 os.path.join(SRC_ROOT, 'third_party', 'libevent', 'linux') |
| 208 ], |
| 209 'cflags': cflags + ['-DHAVE_CONFIG_H'], |
| 210 } |
| 211 static_libraries['xdg_user_dirs'] = { |
| 212 'sources': [ |
| 213 'base/third_party/xdg_user_dirs/xdg_user_dir_lookup.cc', |
| 214 ], |
| 215 'tool': 'cxx', |
| 216 } |
| 217 static_libraries['base']['sources'].extend([ |
| 218 'base/base_paths_posix.cc', |
| 219 'base/debug/debugger_posix.cc', |
| 220 'base/debug/stack_trace_posix.cc', |
| 221 'base/file_util_posix.cc', |
| 222 'base/files/file_enumerator_posix.cc', |
| 223 'base/files/file_posix.cc', |
| 224 'base/message_loop/message_pump_glib.cc', |
| 225 'base/message_loop/message_pump_gtk.cc', |
| 226 'base/message_loop/message_pump_libevent.cc', |
| 227 'base/nix/xdg_util.cc', |
| 228 'base/posix/file_descriptor_shuffle.cc', |
| 229 'base/process/internal_linux.cc', |
| 230 'base/process/kill_posix.cc', |
| 231 'base/process/process_handle_linux.cc', |
| 232 'base/process/process_handle_posix.cc', |
| 233 'base/process/process_iterator_linux.cc', |
| 234 'base/process/process_linux.cc', |
| 235 'base/process/process_metrics_linux.cc', |
| 236 'base/process/process_metrics_posix.cc', |
| 237 'base/process/process_posix.cc', |
| 238 'base/safe_strerror_posix.cc', |
| 239 'base/strings/sys_string_conversions_posix.cc', |
| 240 'base/synchronization/condition_variable_posix.cc', |
| 241 'base/synchronization/lock_impl_posix.cc', |
| 242 'base/synchronization/waitable_event_posix.cc', |
| 243 'base/sys_info_linux.cc', |
| 244 'base/sys_info_posix.cc', |
| 245 'base/threading/platform_thread_linux.cc', |
| 246 'base/threading/platform_thread_posix.cc', |
| 247 'base/threading/thread_local_posix.cc', |
| 248 'base/threading/thread_local_storage_posix.cc', |
| 249 'base/time/time_posix.cc', |
| 250 ]) |
| 251 |
| 252 cflags.extend(['-O2', '-pthread', '-pipe']) |
| 253 |
| 254 static_libraries['base'].setdefault('cflags', []).extend( |
| 255 subprocess.check_output( |
| 256 ['pkg-config', 'gtk+-2.0', 'x11', '--cflags']).split()) |
| 257 ldflags.extend(['-pthread']) |
| 258 ldflags.extend(subprocess.check_output( |
| 259 ['pkg-config', 'gtk+-2.0', 'x11', |
| 260 '--libs-only-L', '--libs-only-other']).split()) |
| 261 libs.extend(subprocess.check_output( |
| 262 ['pkg-config', 'gtk+-2.0', 'x11', '--libs-only-l']).split()) |
| 263 |
| 264 with open(os.path.join(GN_ROOT, 'bootstrap', 'build.ninja.template')) as f: |
| 265 ninja_template = f.read() |
| 266 |
| 267 def src_to_obj(path): |
| 268 return '%s' % os.path.splitext(src_file)[0] + '.o' |
| 269 |
| 270 ninja_lines = [] |
| 271 for library, settings in static_libraries.iteritems(): |
| 272 for src_file in settings['sources']: |
| 273 ninja_lines.extend([ |
| 274 'build %s: %s %s' % (src_to_obj(src_file), |
| 275 settings['tool'], |
| 276 os.path.join(SRC_ROOT, src_file)), |
| 277 ' includes = %s' % ' '.join( |
| 278 ['-I' + dirname for dirname in |
| 279 include_dirs + settings.get('include_dirs', [])]), |
| 280 ' cflags = %s' % ' '.join(cflags + settings.get('cflags', [])), |
| 281 ]) |
| 282 |
| 283 ninja_lines.append('build %s.a: alink_thin %s' % ( |
| 284 library, |
| 285 ' '.join([src_to_obj(src_file) for src_file in settings['sources']]))) |
| 286 |
| 287 ninja_lines.extend([ |
| 288 'build gn: link %s' % ( |
| 289 ' '.join(['%s.a' % library for library in static_libraries])), |
| 290 ' ld = $ldxx', |
| 291 ' ldflags = %s' % ' '.join(ldflags), |
| 292 ' libs = %s' % ' '.join(libs), |
| 293 '', # Make sure the file ends with a newline. |
| 294 ]) |
| 295 |
| 296 with open(path, 'w') as f: |
| 297 f.write(ninja_template + '\n'.join(ninja_lines)) |
| 298 |
| 299 |
| 300 if __name__ == '__main__': |
| 301 sys.exit(main(sys.argv[1:])) |
OLD | NEW |