import os import re from SCons.Script import * _REPO_NAME = 'zlib' _REPO_URL = 'https://github.com/madler/zlib.git' _TAG_PATTERN = re.compile(r'^v([0-9]+)\.([0-9]+)(?:\.([0-9]+))?$') _VERSION_SOURCE = ''' #include #include int main(int, char**) { puts(ZLIB_VERSION); return 0; } ''' def _build_lib_name(env: Environment) -> str: if os.name == 'posix': return 'z' elif os.name == 'nt': return { 'debug': 'zlibstaticd' }.get(env['BUILD_TYPE'], 'zlibstatic') else: raise Exception('libpng is not supported yet on this OS') def versions(env: Environment, update: bool = False, options: dict = {}): tags = env.GitTags(repo_name = _REPO_NAME, remote_url = _REPO_URL, force_fetch=update) result = [] if options.get('use_system_library'): version = env.VersionFromSource('pkgconf --cflags-only-I zlib', _VERSION_SOURCE) if version: env['SYSTEM_ZLIB_VERSION'] = version result.append(version) else: for tag in tags: match = _TAG_PATTERN.match(tag) if match: result.append((int(match.groups()[0]), int(match.groups()[1]), int(match.groups()[2] or 0))) return result def dependencies(env: Environment, version) -> 'dict': return {} def cook(env: Environment, version, options: dict = {}) -> dict: if options.get('use_system_library'): return env.PkgCook('pkgconf --cflags --libs zlib') git_ref = f'refs/tags/v{version[0]}.{version[1]}' if version[2] != 0: git_ref = git_ref + f'.{version[2]}' repo = env.GitBranch(repo_name = _REPO_NAME, remote_url = _REPO_URL, git_ref = git_ref) checkout_root = repo['checkout_root'] build_result = env.CMakeProject(project_root=checkout_root) include_dir = os.path.join(build_result['install_dir'], 'include') lib_name = _build_lib_name(env) lib_file = env.FindLib(lib_name, paths=build_result['LIBPATH']) return { 'CPPPATH': [include_dir], 'LIBS': [lib_file], 'CMAKE_VARS': { 'ZLIB_LIBRARY': lib_file, 'ZLIB_INCLUDE_DIR': include_dir } }