def get_env(s):
    import os
    return os.environ[s] if s in os.environ else None


def get_build_dir():
    return 'build%d_%s' % (build_target.bits,
                           'debug' if build_target.is_debug() else 'release')


global_config(
    duplicated_source_action='error',
    test_timeout=600,  # In seconds
    glob_error_severity='error',
    legacy_public_targets=load_value('build/blade/legacy_public_targets.conf'),
)

cc_test_config(
    # `initial-exec` TLS does not work quite well on ppc64le if dynamic linking
    # is used.
    dynamic_link=not build_target.is_debug() and build_target.arch != 'ppc64le',
    # Doesn't work quite right on AArch64. (It does work on ppc64le, though.)
    #
    # Stacktrace suggests that it's related to global initialization order
    # fiasco.
    heap_check='strict' if build_target.arch != 'aarch64' else '',
    gperftools_libs='thirdparty/gperftools:tcmalloc',
    gtest_libs=['thirdparty/googletest:gtest', '#pthread'],
    gtest_main_libs=['thirdparty/googletest:gtest_main'],
    pprof_path='thirdparty/gperftools/bin/pprof',
)

cc_config(
    cppflags=[
        '-gdwarf-2',
        '-mcx16' if build_target.arch == 'x86_64' else '',
        '-fno-var-tracking',
        '-gno-column-info',
    ],
    cflags='-std=gnu11',
    cxxflags=[
        '-std=gnu++2a',

        # This option must be applied here. Options applied in `warnings` are
        # not applied to generated source files (e.g., those generated by
        # `protoc`.). In practice, `-Wpsabi` produces some diagnostics when
        # compiling `.pb.cc` on ppc64le, this option suppress those diagnostics.
        '-Wno-psabi' if build_target.arch == 'ppc64le' else '',
    ],

    # NOTE: Please keep all warnings in order in one same group.
    warnings=[
        '-Werror',
        '-Wall',
        '-Wextra',

        # other useful warnings
        '-Warray-bounds',
        '-Wchar-subscripts',
        '-Wcomment',
        '-Wendif-labels',
        '-Wformat=1',
        # '-Wframe-larger-than=69632', # A 64k buffer and other small vars
        '-Wmissing-include-dirs',
        '-Wmultichar',
        '-Wparentheses',
        '-Wpointer-arith',
        '-Wreturn-type',
        '-Wsequence-point',
        '-Wswitch',
        '-Wunused-function',
        '-Wunused-label',
        '-Wunused-result',
        '-Wunused-value',
        '-Wwrite-strings',

        # Suppress all warnings which we can't handle or are misreported.
        # Even if some them are sure useful, too many warnings also overwhelm
        # the true problems.
        '-Wno-format-truncation',
        '-Wno-float-equal',
        # '-Wno-incompatible-pointer-types',

        # Causes too many 'note's when it is disabled due to the size of the code
        '-Wno-misleading-indentation',
        '-Wno-missing-field-initializers',
        '-Wno-sign-compare',
        '-Wno-strict-aliasing',
        '-Wno-unused-but-set-variable',
        '-Wno-unused-local-typedefs',
        '-Wno-unused-parameter',
    ],
    # C++ only warning flags
    cxx_warnings=[
        '-Wnon-virtual-dtor',
        '-Woverloaded-virtual',

        # Suppress all warnings which we can't handle or are misreported.
        '-Wno-deprecated',
        '-Wno-invalid-offsetof',
    ],
    extra_incs=[
        # for all thirdparty packages
        'thirdparty/',
        '%s/thirdparty/' % get_build_dir(),
    ],
    linkflags=[
        # We need this in some platforms for `link_all_symbols=True` to work.
        '-Wl,--no-as-needed',
        '-lpthread',
    ],
    benchmark_libs=['//thirdparty/benchmark:benchmark'],
    benchmark_main_libs=['//thirdparty/benchmark:benchmark_main'],
    allowed_undeclared_hdrs=load_value('build/blade/allowed_undeclared_hdrs.conf'),
    # header_inclusion_dependencies = True,
    hdr_dep_missing_severity='error',
)

cc_binary_config(run_lib_paths=['//thirdparty/jdk/lib'],)

cc_library_config(
    generate_dynamic=True,
    arflags=['rcsDT'],
    prebuilt_libpath_pattern='lib${bits}',
    hdrs_missing_severity='error',
    hdrs_missing_suppress=load_value('build/blade/hdrs_missing_suppress.conf'),
)

proto_library_config(
    protoc='thirdparty/protobuf/bin/%s/protoc-3.4.1' % build_target.arch,
    protobuf_libs=['//thirdparty/protobuf:protobuf'],
    protobuf_path='thirdparty',
    protobuf_incs='thirdparty/protobuf-3.4.1/src',
    protoc_direct_dependencies=True,
    well_known_protos=[
        'google/protobuf/any.proto',
        'google/protobuf/api.proto',
        'google/protobuf/compiler/plugin.proto',
        'google/protobuf/descriptor.proto',
        'google/protobuf/duration.proto',
        'google/protobuf/empty.proto',
        'google/protobuf/field_mask.proto',
        'google/protobuf/source_context.proto',
        'google/protobuf/struct.proto',
        'google/protobuf/timestamp.proto',
        'google/protobuf/type.proto',
        'google/protobuf/wrappers.proto',
    ],
)
