Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/ports/sdl2_image.py
4130 views
1
# Copyright 2014 The Emscripten Authors. All rights reserved.
2
# Emscripten is available under two separate licenses, the MIT license and the
3
# University of Illinois/NCSA Open Source License. Both these licenses can be
4
# found in the LICENSE file.
5
6
from typing import Dict, Set
7
8
TAG = 'release-2.6.0'
9
HASH = '2175d11a90211871f2289c8d57b31fe830e4b46af7361925c2c30cd521c1c677d2ee244feb682b6d3909cf085129255934751848fc81b480ea410952d990ffe0'
10
11
deps = ['sdl2']
12
variants = {
13
'sdl2_image-jpg': {'SDL2_IMAGE_FORMATS': ["jpg"]},
14
'sdl2_image-png': {'SDL2_IMAGE_FORMATS': ["png"]},
15
'sdl2_image-jpg-mt': {'SDL2_IMAGE_FORMATS': ["jpg"], 'PTHREADS': 1},
16
'sdl2_image-png-mt': {'SDL2_IMAGE_FORMATS': ["png"], 'PTHREADS': 1},
17
}
18
19
OPTIONS = {
20
'formats': 'A comma separated list of formats (ex: --use-port=sdl2_image:formats=png,jpg)',
21
}
22
23
SUPPORTED_FORMATS = {'bmp', 'gif', 'jpg', 'lbm', 'pcx', 'png',
24
'pnm', 'qoi', 'svg', 'tga', 'xcf', 'xpm', 'xv'}
25
26
# user options (from --use-port)
27
opts: Dict[str, Set] = {
28
'formats': set(),
29
}
30
31
32
def needed(settings):
33
return settings.USE_SDL_IMAGE == 2
34
35
36
def get_formats(settings):
37
return opts['formats'].union(settings.SDL2_IMAGE_FORMATS)
38
39
40
def get_lib_name(settings):
41
formats = '-'.join(sorted(get_formats(settings)))
42
43
libname = 'libSDL2_image'
44
if formats != '':
45
libname += '-' + formats
46
if settings.PTHREADS:
47
libname += '-mt'
48
if settings.SUPPORT_LONGJMP == 'wasm':
49
libname += '-wasm-sjlj'
50
return libname + '.a'
51
52
53
def get(ports, settings, shared):
54
ports.fetch_project('sdl2_image', f'https://github.com/libsdl-org/SDL_image/archive/refs/tags/{TAG}.zip', sha512hash=HASH)
55
libname = get_lib_name(settings)
56
57
def create(final):
58
src_dir = ports.get_dir('sdl2_image', 'SDL_image-' + TAG)
59
ports.install_headers(src_dir, target='SDL2')
60
srcs = '''IMG.c IMG_bmp.c IMG_gif.c IMG_jpg.c IMG_lbm.c IMG_pcx.c IMG_png.c IMG_pnm.c IMG_tga.c
61
IMG_tif.c IMG_xcf.c IMG_xpm.c IMG_xv.c IMG_webp.c IMG_ImageIO.m
62
IMG_avif.c IMG_jxl.c IMG_svg.c IMG_qoi.c'''.split()
63
64
flags = ['-sUSE_SDL=2', '-Wno-format-security']
65
66
formats = get_formats(settings)
67
68
flags.extend(f'-DLOAD_{fmt.upper()}' for fmt in formats)
69
70
if 'png' in formats:
71
flags += ['-sUSE_LIBPNG']
72
73
if 'jpg' in formats:
74
flags += ['-sUSE_LIBJPEG']
75
76
if settings.PTHREADS:
77
flags += ['-pthread']
78
79
if settings.SUPPORT_LONGJMP == 'wasm':
80
flags.append('-sSUPPORT_LONGJMP=wasm')
81
82
ports.build_port(src_dir, final, 'sdl2_image', flags=flags, srcs=srcs)
83
84
return [shared.cache.get_lib(libname, create, what='port')]
85
86
87
def clear(ports, settings, shared):
88
shared.cache.erase_lib(get_lib_name(settings))
89
90
91
def process_dependencies(settings):
92
settings.USE_SDL = 2
93
formats = get_formats(settings)
94
if 'png' in formats:
95
deps.append('libpng')
96
settings.USE_LIBPNG = 1
97
if 'jpg' in formats:
98
deps.append('libjpeg')
99
settings.USE_LIBJPEG = 1
100
101
102
def handle_options(options, error_handler):
103
formats = options['formats'].split(',')
104
for format in formats:
105
format = format.lower().strip()
106
if format not in SUPPORTED_FORMATS:
107
error_handler(f'{format} is not a supported format')
108
else:
109
opts['formats'].add(format)
110
111
112
def show():
113
return 'sdl2_image (-sUSE_SDL_IMAGE=2 or --use-port=sdl2_image; zlib license)'
114
115