Mirai's Miscellaneous Misadventures

M49 / meson.build

# license: AGPLv3 or later
# copyright 2024 zamfofex

project('mimimi', 'c', default_options: ['c_std=c89', 'warning_level=3'])

sources = [
	'core/sprites.c',
	'core/controls.c',
	'core/cameras.c',
	'core/animations.c',
	'core/displays.c',
	'core/font.c',
	'core/text.c',
	'core/collisions.c',
	'core/video.c',
	'core/effects.c',
	'core/ground.c',
	'core/dialogues.c',
	'core/math.c',
	'core/motion.c',
	'core/logo.c',
	'core/logos.c',
	'core/packbits.c',
	'models/all.c',
]

possible_engines = ['fbdev', 'minifb', 'sdl2']
if host_machine.cpu() == 'wasm32'
	possible_engines = ['wasm']
endif

# note: 'loader' must come last
chapter_names = ['test', 'chapter-I', 'loader']
engine_names = []

foreach name : ['wasm', 'fbdev', 'minifb', 'sdl2']
	option = get_option(name)
	if option.enabled() and not name in possible_engines
		error('wrong host machine for target ' + name)
	endif
	if option.enabled() or option.auto() and name in possible_engines
		engine_names += name
	endif
endforeach

if 'wasm' in engine_names
	wasm_opt = find_program('wasm-opt')
endif

if engine_names.length() == 0
	error('no engines specified')
endif

c_args_dict = {
	'fbdev': ['-D_XOPEN_SOURCE=500'],
}

link_args_dict = {
	'wasm': ['-Wl,--export-memory'],
}

cc = meson.get_compiler('c')

install_headers('include/mimimi.h')
mimimi = library('mimimi', sources,
	include_directories: 'include',
	install: true,
)

chapters = []

foreach engine_name : engine_names
	c_args = c_args_dict.get(engine_name, [])
	link_args = link_args_dict.get(engine_name, [])
	dependencies = []
	if engine_name == 'minifb'
		dependencies = [cc.find_library('minifb', has_headers: 'MiniFB.h'), dependency('x11'), dependency('gl'), dependency('xkbcommon')]
	endif
	if engine_name == 'sdl2'
		dependencies = dependency('sdl2')
	endif
	
	engine = library('mimimi-' + engine_name, 'engines' / (engine_name + '.c'),
		include_directories: 'include',
		c_args: c_args,
		install: true,
		dependencies: dependencies,
	)
	
	if engine_name == 'wasm'
		install_data('index.html')
	endif
	
	foreach chapter_name : chapter_names
		name = 'mimimi-' + chapter_name + '-' + engine_name
		if engine_name == 'wasm'
			name = 'mimimi-' + chapter_name
		endif
		
		chapter = static_library(name, 'chapters' / chapter_name / 'setup.c',
			include_directories: 'include',
			link_args: link_args,
			install: true,
		)
		
		chapter_libraries = []
		if chapter_name == 'loader'
			chapter_libraries = chapters
		else
			chapters += chapter
		endif
		
		exe = executable(name, 'chapters/main.c',
			include_directories: 'include',
			c_args: c_args + [
				'-DMIMIMI_ENGINE=mimimi_' + engine_name,
				'-DMIMIMI_CHAPTER=mimimi_' + chapter_name.replace('-', '_'),
				'-DMIMIMI_TICK=mimimi_' + chapter_name.replace('-', '_') + '_tick',
			],
			link_args: link_args,
			link_with: [mimimi, engine, chapter] + chapter_libraries,
			install: true,
			dependencies: dependencies,
		)
		
		if engine_name == 'wasm'
			async = custom_target(
				output: name + '-async.wasm',
				input: exe,
				command: [wasm_opt, '-O4', '--asyncify', '-o', '@OUTPUT@', '@INPUT@'],
				install: true,
				install_dir: get_option('prefix') / get_option('bindir'),
			)
			
			if chapter_name == 'loader'
				install_symlink(name + '.wasm',
					install_dir: get_option('prefix') / get_option('datadir') / 'mimimi',
					pointing_to: get_option('prefix') / get_option('bindir') / (name + '-async.wasm'),
				)
			endif
		endif
	endforeach
endforeach