Skip to content

Instantly share code, notes, and snippets.

@jeremy-code
Created September 12, 2026 05:03
Show Gist options
  • Select an option

  • Save jeremy-code/bca308c175e8d088bbf83bae77b053d9 to your computer and use it in GitHub Desktop.

Select an option

Save jeremy-code/bca308c175e8d088bbf83bae77b053d9 to your computer and use it in GitHub Desktop.
libavif.py Emscripten port
import os
import shutil
from tools import shared, utils
URL = 'https://github.com/AOMediaCodec/libavif'
DESCRIPTION = 'libavif is a library for encoding and decoding .avif files'
LICENSE = 'BSD 2-Clause License'
TAG = '1.4.2'
port_name = 'libavif'
def get_lib_name(settings):
return 'libavif.a'
def get(ports, settings, shared):
ports.fetch_project(
port_name,
f'https://github.com/AOMediaCodec/libavif/archive/refs/tags/v{TAG}.tar.gz',
# TODO: Add SHA-512 hash.
)
def create(final):
root_path = ports.get_dir(port_name, f'libavif-{TAG}')
build_path = os.path.join(ports.get_build_dir(), port_name)
emscripten_dir = os.path.dirname(shared.EMXX)
emcmake = os.path.join(emscripten_dir, 'emcmake')
emmake = os.path.join(emscripten_dir, 'emmake')
ports.install_headers(
os.path.join(root_path, 'include', 'avif'),
target='avif',
)
print("root_path", root_path)
print("build_path", build_path)
cmake_lists = os.path.join(root_path, 'CMakeLists.txt')
with open(cmake_lists) as f:
contents = f.read()
old = ' target_link_libraries(avif_obj PRIVATE Threads::Threads)'
new = ''' if(Threads_FOUND)
target_link_libraries(avif_obj PRIVATE Threads::Threads)
endif()'''
with open(cmake_lists, 'w') as f:
f.write(contents.replace(old, new, 1))
utils.safe_ensure_dirs(build_path)
configure = [
emcmake,
'cmake',
'-DBUILD_SHARED_LIBS=OFF',
'-DAVIF_BUILD_APPS=OFF',
'-DAVIF_BUILD_TESTS=OFF',
'-DAVIF_CODEC_AOM=OFF',
'-DAVIF_CODEC_DAV1D=OFF',
'-DAVIF_CODEC_LIBGAV1=OFF',
'-DAVIF_CODEC_RAV1E=OFF',
'-DAVIF_CODEC_SVT=OFF',
'-DAVIF_CODEC_AVM=OFF',
'-DAVIF_ZLIBPNG=SYSTEM',
'-DAVIF_JPEG=SYSTEM',
'-DAVIF_LIBYUV=OFF',
'-DAVIF_LIBSHARPYUV=OFF',
'-DAVIF_LIBXML2=OFF',
'-DCMAKE_INSTALL_PREFIX=' + os.path.join(build_path, 'install'),
root_path,
]
utils.run_process(configure, cwd=build_path)
build = [
emmake,
'cmake',
'--build',
build_path,
'--target',
'avif',
'--parallel',
'8',
]
utils.run_process(build, cwd=build_path)
library = os.path.join(build_path, 'libavif_internal.a')
shutil.copyfile(library, final)
return [
shared.cache.get_lib(
get_lib_name(settings),
create,
what='port',
)
]
def clear(ports, settings, shared):
shared.cache.erase_lib(get_lib_name(settings))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment