Home | History | Annotate | Download | only in libchrome
      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 
     10 import os
     11 
     12 env = Environment()
     13 
     14 BASE_VER = os.environ.get('BASE_VER', '0')
     15 PKG_CONFIG = os.environ.get('PKG_CONFIG', 'pkg-config')
     16 CHROME_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.
     24 base_name = 'base'
     25 base_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                 nss_key_util.cc
    274                 nss_util.cc
    275                 openssl_util.cc
    276                 p224.cc
    277                 p224_spake.cc
    278                 random.cc
    279                 rsa_private_key.cc
    280                 rsa_private_key_nss.cc
    281                 scoped_test_nss_db.cc
    282                 secure_hash.cc
    283                 secure_util.cc
    284                 sha2.cc
    285                 signature_creator.cc
    286                 signature_verifier.cc
    287                 symmetric_key.cc
    288                 third_party/nss/rsawrapr.c
    289                 third_party/nss/sha512.cc
    290                 """,
    291     'prefix' : 'crypto',
    292     'libs' : '%s-dl-%s' % (base_name, BASE_VER),
    293     'pc_libs' : 'nss openssl',
    294   },
    295 ]
    296 
    297 env.Append(
    298   CPPPATH=['files'],
    299   CCFLAGS=['-g']
    300 )
    301 for key in Split('CC CXX AR RANLIB LD NM CFLAGS CXXFLAGS LDFLAGS'):
    302   value = os.environ.get(key)
    303   if value:
    304     env[key] = Split(value)
    305 if os.environ.has_key('CPPFLAGS'):
    306   env['CCFLAGS'] += Split(os.environ['CPPFLAGS'])
    307 
    308 env['CCFLAGS'] += ['-DOS_CHROMEOS',
    309                    '-DUSE_NSS_CERTS',
    310                    '-DUSE_SYSTEM_LIBEVENT',
    311                    '-DNO_TCMALLOC',
    312                    '-fPIC',
    313                    '-fno-exceptions',
    314                    '-Wall',
    315                    '-Werror',
    316                    '-Wno-deprecated-register',
    317                    '-Wno-narrowing',
    318                    '-Wno-psabi',
    319                    '-Wno-unused-local-typedefs',
    320                    # Various #defines are hardcoded near the top of
    321                    # build_config.h to ensure that they'll be set both when
    322                    # libchrome is built and when other packages include
    323                    # libchrome's headers.
    324                    '-I%s' % CHROME_INCLUDE_PATH]
    325 
    326 env.Append(
    327   CXXFLAGS=['-std=c++11']
    328 )
    329 
    330 # Flags for clang taken from build/common.gypi in the clang==1 section.
    331 CLANG_FLAGS = (
    332   '-Wno-char-subscripts',
    333 )
    334 
    335 env['CCFLAGS'] += ['-Xclang-only=%s' % x for x in CLANG_FLAGS]
    336 
    337 # Fix issue with scons not passing some vars through the environment.
    338 for key in Split('PKG_CONFIG SYSROOT'):
    339   if os.environ.has_key(key):
    340     env['ENV'][key] = os.environ[key]
    341 
    342 all_base_libs = []
    343 all_pc_libs = ''
    344 all_libs = []
    345 all_scons_libs = []
    346 
    347 # Build all the shared libraries.
    348 for lib in base_libs:
    349   pc_libs = lib['pc_libs'].replace('${bslot}', BASE_VER)
    350   all_pc_libs += ' ' + pc_libs
    351 
    352   libs = Split(lib['libs'].replace('${bslot}', BASE_VER))
    353   all_libs += libs
    354 
    355   name = '%s-%s-%s' % (base_name, lib['name'], BASE_VER)
    356   all_base_libs += [name]
    357   corename = '%s-core-%s' % (base_name, BASE_VER)
    358   # Automatically link the sub-libs against the main core lib.
    359   # This is to keep from having to explicitly mention it in the
    360   # table above (i.e. lazy).
    361   if name != corename:
    362     libs += [corename]
    363 
    364   e = env.Clone()
    365   e.Append(
    366     LIBS = Split(libs),
    367     LIBPATH = ['.'],
    368     LINKFLAGS = ['-Wl,--as-needed', '-Wl,-z,defs',
    369                  '-Wl,-soname,lib%s.so' % name],
    370   )
    371   if pc_libs:
    372     e.ParseConfig(PKG_CONFIG + ' --cflags --libs %s' % pc_libs)
    373 
    374   # Prepend prefix to source filenames.
    375   sources = [os.path.join(lib['prefix'], x) for x in Split(lib['sources'])]
    376 
    377   all_scons_libs += [ e.SharedLibrary(name, sources) ]
    378 
    379 
    380 # Build a static library of mocks for unittests to link against.
    381 # Being static allows us to mask this library out of the image.
    382 
    383 all_base_test_libs = []
    384 all_test_pc_libs = ''
    385 all_test_libs = []
    386 
    387 test_libs = [
    388   {
    389     'name': 'base_test_support',
    390     'sources': """
    391                simple_test_clock.cc
    392                simple_test_tick_clock.cc
    393                test_file_util.cc
    394                test_file_util_linux.cc
    395                test_switches.cc
    396                test_timeouts.cc
    397                """,
    398     'prefix': 'base/test',
    399     'libs': '',
    400     'pc_libs': '',
    401   },
    402   {
    403     'name': 'dbus_test_support',
    404     'sources': """
    405                mock_bus.cc
    406                mock_exported_object.cc
    407                mock_object_manager.cc
    408                mock_object_proxy.cc
    409                """,
    410     'prefix': 'dbus',
    411     'libs': '',  # TODO(wiley) what should go here?
    412     'pc_libs': 'dbus-1 protobuf-lite',
    413   },
    414   {
    415     'name': 'timer_test_support',
    416     'sources': """
    417                mock_timer.cc
    418                """,
    419     'prefix': 'base/timer',
    420     'libs': '',
    421     'pc_libs': '',
    422   },
    423 ]
    424 
    425 for lib in test_libs:
    426   pc_libs = lib['pc_libs'].replace('${bslot}', BASE_VER)
    427   all_test_pc_libs += ' ' + pc_libs
    428 
    429   libs = Split(lib['libs'].replace('${bslot}', BASE_VER))
    430   all_test_libs += libs
    431 
    432   name = '%s-%s-%s' % (base_name, lib['name'], BASE_VER)
    433   all_base_test_libs += [name]
    434 
    435   static_env = env.Clone()
    436   if pc_libs:
    437     static_env.ParseConfig(PKG_CONFIG + ' --cflags --libs %s' % pc_libs)
    438   sources = [os.path.join(lib['prefix'], x)
    439              for x in Split(lib['sources'])]
    440   static_env.StaticLibrary(name, sources)
    441 
    442 # Build the random text files (pkg-config and linker script).
    443 
    444 def lib_list(libs):
    445   return ' '.join(['-l' + l for l in libs])
    446 
    447 prod_subst_dict = {
    448   '@BSLOT@': BASE_VER,
    449   '@PRIVATE_PC@': all_pc_libs,
    450   '@BASE_LIBS@': lib_list(all_base_libs),
    451   '@LIBS@': lib_list(all_libs),
    452   '@NAME@': 'libchrome',
    453   '@PKG_CFG_NAME@': 'libchrome-%s.pc' % BASE_VER,
    454   '@LIB_NAME@': 'libbase-%s.so' % BASE_VER,
    455   '@DESCRIPTION@': 'chrome base library',
    456   # scons, in its infinite wisdom sees fit to expand this string if
    457   # if we don't escape the $.
    458   '@TARGET_LIB@': 'base-$${bslot}',
    459 }
    460 
    461 # Similarly, build text files related to the test libraries.
    462 test_subst_dict = {
    463   '@BSLOT@': BASE_VER,
    464   '@PRIVATE_PC@': all_test_pc_libs,
    465   '@BASE_LIBS@': lib_list(all_base_test_libs),
    466   '@LIBS@': lib_list(all_test_libs),
    467   '@NAME@': 'libchrome-test',
    468   '@PKG_CFG_NAME@': 'libchrome-test-%s.pc' % BASE_VER,
    469   '@LIB_NAME@': 'libbase-test-%s.a' % BASE_VER,
    470   '@DESCRIPTION@': 'chrome base test library',
    471   # scons, in its infinite wisdom sees fit to expand this string if
    472   # if we don't escape the $.
    473   '@TARGET_LIB@': 'base-test-$${bslot}',
    474 }
    475 
    476 pc_file_contents = """
    477 prefix=/usr
    478 includedir=${prefix}/include
    479 bslot=@BSLOT@
    480 
    481 Name: @NAME@
    482 Description: @DESCRIPTION@
    483 Version: ${bslot}
    484 Requires:
    485 Requires.private: @PRIVATE_PC@
    486 Libs: -l@TARGET_LIB@
    487 Libs.private: @BASE_LIBS@ @LIBS@
    488 Cflags: -I${includedir}/@TARGET_LIB@ -Wno-c++11-extensions -Wno-unused-local-typedefs -DBASE_VER=${bslot}
    489 """
    490 
    491 # https://sourceware.org/binutils/docs/ld/Scripts.html
    492 so_file_contents = """GROUP ( AS_NEEDED ( @BASE_LIBS@ ) )"""
    493 
    494 for subst_dict in (test_subst_dict, prod_subst_dict):
    495   env = Environment(tools=['textfile'], SUBST_DICT=subst_dict)
    496   env.Substfile(subst_dict['@LIB_NAME@'], [Value(so_file_contents)])
    497   env.Substfile(subst_dict['@PKG_CFG_NAME@'], [Value(pc_file_contents)])
    498