1# -*- python -*- 2 3# Copyright 2014 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7# This file is used to build the libchrome package for Chrome OS: 8# https://www.chromium.org/chromium-os/packages/libchrome 9 10import os 11 12env = Environment() 13 14BASE_VER = os.environ.get('BASE_VER', '0') 15PKG_CONFIG = os.environ.get('PKG_CONFIG', 'pkg-config') 16CHROME_INCLUDE_PATH = os.environ.get('CHROME_INCLUDE_PATH', '.') 17 18# This block will need updating whenever libchrome gets updated. The order of 19# the libs below doesn't matter (as scons will take care of building things in 20# the required order). The split between them is purely to reduce excess 21# linking of third-party libraries, i.e. 'core' should require only a minimal 22# set of libraries, and other third-party libraries should get a unique 'xxx' 23# name. 24base_name = 'base' 25base_libs = [ 26 { 27 'name' : 'core', 28 'sources' : """ 29 allocator/allocator_extension.cc 30 allocator/allocator_shim.cc 31 allocator/allocator_shim_default_dispatch_to_glibc.cc 32 at_exit.cc 33 base64.cc 34 base64url.cc 35 base_switches.cc 36 bind_helpers.cc 37 build_time.cc 38 callback_helpers.cc 39 callback_internal.cc 40 command_line.cc 41 cpu.cc 42 debug/alias.cc 43 debug/debugger.cc 44 debug/debugger_posix.cc 45 debug/stack_trace.cc 46 debug/stack_trace_posix.cc 47 debug/task_annotator.cc 48 environment.cc 49 files/file.cc 50 files/file_enumerator.cc 51 files/file_enumerator_posix.cc 52 files/file_path.cc 53 files/file_path_constants.cc 54 files/file_path_watcher.cc 55 files/file_path_watcher_linux.cc 56 files/file_posix.cc 57 files/file_tracing.cc 58 files/file_util.cc 59 files/file_util_linux.cc 60 files/file_util_posix.cc 61 files/important_file_writer.cc 62 files/memory_mapped_file.cc 63 files/memory_mapped_file_posix.cc 64 files/scoped_file.cc 65 files/scoped_temp_dir.cc 66 guid.cc 67 hash.cc 68 json/json_file_value_serializer.cc 69 json/json_parser.cc 70 json/json_reader.cc 71 json/json_string_value_serializer.cc 72 json/json_value_converter.cc 73 json/json_writer.cc 74 json/string_escape.cc 75 lazy_instance.cc 76 location.cc 77 logging.cc 78 md5.cc 79 memory/aligned_memory.cc 80 memory/ref_counted.cc 81 memory/ref_counted_memory.cc 82 memory/shared_memory_posix.cc 83 memory/singleton.cc 84 memory/weak_ptr.cc 85 message_loop/incoming_task_queue.cc 86 message_loop/message_loop.cc 87 message_loop/message_loop_task_runner.cc 88 message_loop/message_pump.cc 89 message_loop/message_pump_default.cc 90 message_loop/message_pump_glib.cc 91 message_loop/message_pump_libevent.cc 92 metrics/bucket_ranges.cc 93 metrics/field_trial.cc 94 metrics/metrics_hashes.cc 95 metrics/histogram_base.cc 96 metrics/histogram.cc 97 metrics/histogram_samples.cc 98 metrics/histogram_snapshot_manager.cc 99 metrics/persistent_histogram_allocator.cc 100 metrics/persistent_memory_allocator.cc 101 metrics/persistent_sample_map.cc 102 metrics/sample_map.cc 103 metrics/sample_vector.cc 104 metrics/sparse_histogram.cc 105 metrics/statistics_recorder.cc 106 pending_task.cc 107 pickle.cc 108 posix/file_descriptor_shuffle.cc 109 posix/global_descriptors.cc 110 posix/safe_strerror.cc 111 posix/unix_domain_socket_linux.cc 112 process/internal_linux.cc 113 process/kill.cc 114 process/kill_posix.cc 115 process/launch.cc 116 process/launch_posix.cc 117 process/process_handle_linux.cc 118 process/process_iterator.cc 119 process/process_iterator_linux.cc 120 process/process_handle_posix.cc 121 process/process_metrics.cc 122 process/process_metrics_linux.cc 123 process/process_metrics_posix.cc 124 process/process_posix.cc 125 profiler/scoped_profile.cc 126 profiler/scoped_tracker.cc 127 profiler/tracked_time.cc 128 rand_util.cc 129 rand_util_posix.cc 130 run_loop.cc 131 sequence_checker_impl.cc 132 sequenced_task_runner.cc 133 sha1_portable.cc 134 strings/pattern.cc 135 strings/safe_sprintf.cc 136 strings/string16.cc 137 strings/string_number_conversions.cc 138 strings/string_piece.cc 139 strings/stringprintf.cc 140 strings/string_split.cc 141 strings/string_util.cc 142 strings/string_util_constants.cc 143 strings/sys_string_conversions_posix.cc 144 strings/utf_string_conversions.cc 145 strings/utf_string_conversion_utils.cc 146 synchronization/cancellation_flag.cc 147 synchronization/condition_variable_posix.cc 148 synchronization/lock.cc 149 synchronization/lock_impl_posix.cc 150 synchronization/read_write_lock_posix.cc 151 synchronization/waitable_event_posix.cc 152 synchronization/waitable_event_watcher_posix.cc 153 sync_socket_posix.cc 154 sys_info.cc 155 sys_info_chromeos.cc 156 sys_info_linux.cc 157 sys_info_posix.cc 158 task_runner.cc 159 task/cancelable_task_tracker.cc 160 task_scheduler/scheduler_lock_impl.cc 161 task_scheduler/sequence.cc 162 task_scheduler/sequence_sort_key.cc 163 task_scheduler/task.cc 164 task_scheduler/task_traits.cc 165 third_party/icu/icu_utf.cc 166 third_party/nspr/prtime.cc 167 threading/non_thread_safe_impl.cc 168 threading/platform_thread_internal_posix.cc 169 threading/platform_thread_linux.cc 170 threading/platform_thread_posix.cc 171 threading/post_task_and_reply_impl.cc 172 threading/sequenced_task_runner_handle.cc 173 threading/sequenced_worker_pool.cc 174 threading/simple_thread.cc 175 threading/thread.cc 176 threading/thread_checker_impl.cc 177 threading/thread_collision_warner.cc 178 threading/thread_id_name_manager.cc 179 threading/thread_local_posix.cc 180 threading/thread_local_storage.cc 181 threading/thread_local_storage_posix.cc 182 threading/thread_restrictions.cc 183 threading/thread_task_runner_handle.cc 184 threading/worker_pool.cc 185 threading/worker_pool_posix.cc 186 timer/elapsed_timer.cc 187 timer/timer.cc 188 time/clock.cc 189 time/default_clock.cc 190 time/default_tick_clock.cc 191 time/tick_clock.cc 192 time/time.cc 193 time/time_posix.cc 194 trace_event/heap_profiler_allocation_context.cc 195 trace_event/heap_profiler_allocation_context_tracker.cc 196 trace_event/heap_profiler_allocation_register.cc 197 trace_event/heap_profiler_allocation_register_posix.cc 198 trace_event/heap_profiler_heap_dump_writer.cc 199 trace_event/heap_profiler_stack_frame_deduplicator.cc 200 trace_event/heap_profiler_type_name_deduplicator.cc 201 trace_event/malloc_dump_provider.cc 202 trace_event/memory_allocator_dump.cc 203 trace_event/memory_allocator_dump_guid.cc 204 trace_event/memory_dump_manager.cc 205 trace_event/memory_dump_request_args.cc 206 trace_event/memory_dump_session_state.cc 207 trace_event/memory_infra_background_whitelist.cc 208 trace_event/process_memory_dump.cc 209 trace_event/process_memory_maps.cc 210 trace_event/process_memory_totals.cc 211 trace_event/trace_buffer.cc 212 trace_event/trace_config.cc 213 trace_event/trace_event_argument.cc 214 trace_event/trace_event_impl.cc 215 trace_event/trace_event_memory_overhead.cc 216 trace_event/trace_event_synthetic_delay.cc 217 trace_event/trace_log.cc 218 trace_event/trace_log_constants.cc 219 trace_event/trace_sampling_thread.cc 220 tracked_objects.cc 221 tracking_info.cc 222 values.cc 223 version.cc 224 vlog.cc 225 """, 226 'prefix' : 'base', 227 'libs' : 'pthread rt libmodp_b64', 228 'pc_libs' : 'glib-2.0 libevent', 229 }, 230 { 231 'name' : 'dl', 232 'sources' : """ 233 native_library_posix.cc 234 """, 235 'prefix' : 'base', 236 'libs' : 'dl', 237 'pc_libs' : '', 238 }, 239 { 240 'name' : 'dbus', 241 'sources' : """ 242 bus.cc 243 dbus_statistics.cc 244 exported_object.cc 245 file_descriptor.cc 246 message.cc 247 object_manager.cc 248 object_path.cc 249 object_proxy.cc 250 property.cc 251 scoped_dbus_error.cc 252 string_util.cc 253 util.cc 254 values_util.cc 255 """, 256 'prefix' : 'dbus', 257 'libs' : '', 258 'pc_libs' : 'dbus-1 protobuf-lite', 259 }, 260 { 261 'name' : 'timers', 262 'sources' : """ 263 alarm_timer_chromeos.cc 264 """, 265 'prefix' : 'components/timers', 266 'libs' : '', 267 'pc_libs' : '', 268 }, 269 { 270 'name' : 'crypto', 271 'sources' : """ 272 hmac.cc 273 hmac_nss.cc 274 nss_key_util.cc 275 nss_util.cc 276 openssl_util.cc 277 p224.cc 278 p224_spake.cc 279 random.cc 280 rsa_private_key.cc 281 rsa_private_key_nss.cc 282 scoped_test_nss_db.cc 283 secure_hash.cc 284 secure_util.cc 285 sha2.cc 286 signature_creator_nss.cc 287 signature_verifier_nss.cc 288 symmetric_key_nss.cc 289 third_party/nss/rsawrapr.c 290 third_party/nss/sha512.cc 291 """, 292 'prefix' : 'crypto', 293 'libs' : '%s-dl-%s' % (base_name, BASE_VER), 294 'pc_libs' : 'nss openssl', 295 }, 296 { 297 'name' : 'sandbox', 298 'sources' : """ 299 linux/bpf_dsl/bpf_dsl.cc 300 linux/bpf_dsl/codegen.cc 301 linux/bpf_dsl/dump_bpf.cc 302 linux/bpf_dsl/policy.cc 303 linux/bpf_dsl/policy_compiler.cc 304 linux/bpf_dsl/syscall_set.cc 305 linux/bpf_dsl/verifier.cc 306 linux/seccomp-bpf/die.cc 307 linux/seccomp-bpf/sandbox_bpf.cc 308 linux/seccomp-bpf/syscall.cc 309 linux/seccomp-bpf/trap.cc 310 311 linux/seccomp-bpf-helpers/baseline_policy.cc 312 linux/seccomp-bpf-helpers/sigsys_handlers.cc 313 linux/seccomp-bpf-helpers/syscall_parameters_restrictions.cc 314 linux/seccomp-bpf-helpers/syscall_sets.cc 315 316 linux/services/init_process_reaper.cc 317 linux/services/proc_util.cc 318 linux/services/resource_limits.cc 319 linux/services/scoped_process.cc 320 linux/services/syscall_wrappers.cc 321 linux/services/thread_helpers.cc 322 linux/services/yama.cc 323 linux/syscall_broker/broker_channel.cc 324 linux/syscall_broker/broker_client.cc 325 linux/syscall_broker/broker_file_permission.cc 326 linux/syscall_broker/broker_host.cc 327 linux/syscall_broker/broker_policy.cc 328 linux/syscall_broker/broker_process.cc 329 330 linux/services/credentials.cc 331 linux/services/namespace_sandbox.cc 332 linux/services/namespace_utils.cc 333 """, 334 'prefix' : 'sandbox', 335 'libs' : '', 336 'pc_libs' : '', 337 }, 338] 339 340env.Append( 341 CPPPATH=['files'], 342 CCFLAGS=['-g'] 343) 344for key in Split('CC CXX AR RANLIB LD NM CFLAGS CXXFLAGS LDFLAGS'): 345 value = os.environ.get(key) 346 if value: 347 env[key] = Split(value) 348if os.environ.has_key('CPPFLAGS'): 349 env['CCFLAGS'] += Split(os.environ['CPPFLAGS']) 350 351env['CCFLAGS'] += ['-DOS_CHROMEOS', 352 '-DUSE_NSS_CERTS', 353 '-DUSE_SYSTEM_LIBEVENT', 354 '-DNO_TCMALLOC', 355 '-fPIC', 356 '-fno-exceptions', 357 '-Wall', 358 '-Werror', 359 '-Wno-deprecated-register', 360 '-Wno-narrowing', 361 '-Wno-psabi', 362 '-Wno-unused-local-typedefs', 363 # Various #defines are hardcoded near the top of 364 # build_config.h to ensure that they'll be set both when 365 # libchrome is built and when other packages include 366 # libchrome's headers. 367 '-I%s' % CHROME_INCLUDE_PATH] 368 369env.Append( 370 CXXFLAGS=['-std=c++11'] 371) 372 373# Flags for clang taken from build/common.gypi in the clang==1 section. 374CLANG_FLAGS = ( 375 '-Wno-char-subscripts', 376) 377 378env['CCFLAGS'] += ['-Xclang-only=%s' % x for x in CLANG_FLAGS] 379 380# Fix issue with scons not passing some vars through the environment. 381for key in Split('PKG_CONFIG SYSROOT'): 382 if os.environ.has_key(key): 383 env['ENV'][key] = os.environ[key] 384 385all_base_libs = [] 386all_pc_libs = '' 387all_libs = [] 388all_scons_libs = [] 389 390# Build all the shared libraries. 391for lib in base_libs: 392 pc_libs = lib['pc_libs'].replace('${bslot}', BASE_VER) 393 all_pc_libs += ' ' + pc_libs 394 395 libs = Split(lib['libs'].replace('${bslot}', BASE_VER)) 396 all_libs += libs 397 398 name = '%s-%s-%s' % (base_name, lib['name'], BASE_VER) 399 all_base_libs += [name] 400 corename = '%s-core-%s' % (base_name, BASE_VER) 401 # Automatically link the sub-libs against the main core lib. 402 # This is to keep from having to explicitly mention it in the 403 # table above (i.e. lazy). 404 if name != corename: 405 libs += [corename] 406 407 e = env.Clone() 408 e.Append( 409 LIBS = Split(libs), 410 LIBPATH = ['.'], 411 LINKFLAGS = ['-Wl,--as-needed', '-Wl,-z,defs', 412 '-Wl,-soname,lib%s.so' % name], 413 ) 414 if pc_libs: 415 e.ParseConfig(PKG_CONFIG + ' --cflags --libs %s' % pc_libs) 416 417 # Prepend prefix to source filenames. 418 sources = [os.path.join(lib['prefix'], x) for x in Split(lib['sources'])] 419 420 all_scons_libs += [ e.SharedLibrary(name, sources) ] 421 422 423# Build a static library of mocks for unittests to link against. 424# Being static allows us to mask this library out of the image. 425 426all_base_test_libs = [] 427all_test_pc_libs = '' 428all_test_libs = [] 429 430test_libs = [ 431 { 432 'name': 'base_test_support', 433 'sources': """ 434 simple_test_clock.cc 435 simple_test_tick_clock.cc 436 test_file_util.cc 437 test_file_util_linux.cc 438 test_switches.cc 439 test_timeouts.cc 440 """, 441 'prefix': 'base/test', 442 'libs': '', 443 'pc_libs': '', 444 }, 445 { 446 'name': 'dbus_test_support', 447 'sources': """ 448 mock_bus.cc 449 mock_exported_object.cc 450 mock_object_manager.cc 451 mock_object_proxy.cc 452 """, 453 'prefix': 'dbus', 454 'libs': '', # TODO(wiley) what should go here? 455 'pc_libs': 'dbus-1 protobuf-lite', 456 }, 457 { 458 'name': 'timer_test_support', 459 'sources': """ 460 mock_timer.cc 461 """, 462 'prefix': 'base/timer', 463 'libs': '', 464 'pc_libs': '', 465 }, 466] 467 468for lib in test_libs: 469 pc_libs = lib['pc_libs'].replace('${bslot}', BASE_VER) 470 all_test_pc_libs += ' ' + pc_libs 471 472 libs = Split(lib['libs'].replace('${bslot}', BASE_VER)) 473 all_test_libs += libs 474 475 name = '%s-%s-%s' % (base_name, lib['name'], BASE_VER) 476 all_base_test_libs += [name] 477 478 static_env = env.Clone() 479 if pc_libs: 480 static_env.ParseConfig(PKG_CONFIG + ' --cflags --libs %s' % pc_libs) 481 sources = [os.path.join(lib['prefix'], x) 482 for x in Split(lib['sources'])] 483 static_env.StaticLibrary(name, sources) 484 485# Build the random text files (pkg-config and linker script). 486 487def lib_list(libs): 488 return ' '.join(['-l' + l for l in libs]) 489 490prod_subst_dict = { 491 '@BSLOT@': BASE_VER, 492 '@PRIVATE_PC@': all_pc_libs, 493 '@BASE_LIBS@': lib_list(all_base_libs), 494 '@LIBS@': lib_list(all_libs), 495 '@NAME@': 'libchrome', 496 '@PKG_CFG_NAME@': 'libchrome-%s.pc' % BASE_VER, 497 '@LIB_NAME@': 'libbase-%s.so' % BASE_VER, 498 '@DESCRIPTION@': 'chrome base library', 499 # scons, in its infinite wisdom sees fit to expand this string if 500 # if we don't escape the $. 501 '@TARGET_LIB@': 'base-$${bslot}', 502} 503 504# Similarly, build text files related to the test libraries. 505test_subst_dict = { 506 '@BSLOT@': BASE_VER, 507 '@PRIVATE_PC@': all_test_pc_libs, 508 '@BASE_LIBS@': lib_list(all_base_test_libs), 509 '@LIBS@': lib_list(all_test_libs), 510 '@NAME@': 'libchrome-test', 511 '@PKG_CFG_NAME@': 'libchrome-test-%s.pc' % BASE_VER, 512 '@LIB_NAME@': 'libbase-test-%s.a' % BASE_VER, 513 '@DESCRIPTION@': 'chrome base test library', 514 # scons, in its infinite wisdom sees fit to expand this string if 515 # if we don't escape the $. 516 '@TARGET_LIB@': 'base-test-$${bslot}', 517} 518 519pc_file_contents = """ 520prefix=/usr 521includedir=${prefix}/include 522bslot=@BSLOT@ 523 524Name: @NAME@ 525Description: @DESCRIPTION@ 526Version: ${bslot} 527Requires: 528Requires.private: @PRIVATE_PC@ 529Libs: -l@TARGET_LIB@ 530Libs.private: @BASE_LIBS@ @LIBS@ 531Cflags: -I${includedir}/@TARGET_LIB@ -Wno-c++11-extensions -Wno-unused-local-typedefs -DBASE_VER=${bslot} 532""" 533 534# https://sourceware.org/binutils/docs/ld/Scripts.html 535so_file_contents = """GROUP ( AS_NEEDED ( @BASE_LIBS@ ) )""" 536 537for subst_dict in (test_subst_dict, prod_subst_dict): 538 env = Environment(tools=['textfile'], SUBST_DICT=subst_dict) 539 env.Substfile(subst_dict['@LIB_NAME@'], [Value(so_file_contents)]) 540 env.Substfile(subst_dict['@PKG_CFG_NAME@'], [Value(pc_file_contents)]) 541