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