Swiftgram/tools/buck_utils.bzl
2019-06-06 00:17:51 +01:00

97 lines
3.3 KiB
Python

OTHER_LINKER_FLAGS_KEY = 'OTHER_LDFLAGS'
# Either appends or assigns `other_linker_flags` to `config` under `config_key`.
# Params:
# - config: A dictionary of config names and their values
# - additional_linker_flags: A string-representable value of additional linker flags
# - config_key: The key to which to append or assign the additional linker flags
def config_with_updated_linker_flags(config, other_linker_flags, config_key=OTHER_LINKER_FLAGS_KEY):
new_config = { }
config_key_found = False
for key in config:
if key == config_key:
new_config[key] = config[key] + (" %s" % other_linker_flags)
config_key_found = True
else:
new_config[key] = config[key]
if config_key_found == False:
# If `config` does not currently contain `config_key`, add it. Inherit for good measure.
new_config[config_key] = '$(inherited) ' + other_linker_flags
return new_config
# Creates a dictionary where the top level keys are the supported build configurations and the value of each key is `config`.
def configs_with_config(config):
return {
"Debug": config,
"Profile": config,
"Release": config,
}
def subdir_glob(glob_specs, exclude = None, prefix = ""):
"""Returns a dict of sub-directory relative paths to full paths.
The subdir_glob() function is useful for defining header maps for C/C++
libraries which should be relative the given sub-directory.
Given a list of tuples, the form of (relative-sub-directory, glob-pattern),
it returns a dict of sub-directory relative paths to full paths.
Please refer to native.glob() for explanations and examples of the pattern.
Args:
glob_specs: The array of tuples in form of
(relative-sub-directory, glob-pattern inside relative-sub-directory).
type: List[Tuple[str, str]]
exclude: A list of patterns to identify files that should be removed
from the set specified by the first argument. Defaults to [].
type: Optional[List[str]]
prefix: If is not None, prepends it to each key in the dictionary.
Defaults to None.
type: Optional[str]
Returns:
A dict of sub-directory relative paths to full paths.
"""
if exclude == None:
exclude = []
results = []
for dirpath, glob_pattern in glob_specs:
results.append(
_single_subdir_glob(dirpath, glob_pattern, exclude, prefix),
)
return _merge_maps(*results)
def _merge_maps(*file_maps):
result = {}
for file_map in file_maps:
for key in file_map:
if key in result and result[key] != file_map[key]:
fail(
"Conflicting files in file search paths. " +
"\"%s\" maps to both \"%s\" and \"%s\"." %
(key, result[key], file_map[key]),
)
result[key] = file_map[key]
return result
def _single_subdir_glob(dirpath, glob_pattern, exclude = None, prefix = None):
if exclude == None:
exclude = []
results = {}
files = native.glob([dirpath + '/' + glob_pattern], exclude = exclude)
for f in files:
if dirpath:
key = f[len(dirpath) + 1:]
else:
key = f
if prefix:
key =prefix + '/' + key
results[key] = f
return results