diff --git a/meson.build b/meson.build index 2141a9b68e..246928fa3f 100644 --- a/meson.build +++ b/meson.build @@ -449,16 +449,22 @@ endif endif # emscripten -portmidi_dep = dependency('portmidi', required: false) +midi_opt = get_option('midi') +# A disabled feature passed as `required` makes dependency() return not-found +# without searching, so -Dmidi=disabled never picks up portmidi. +portmidi_dep = dependency('portmidi', required: midi_opt.disabled() ? midi_opt : false) if not portmidi_dep.found() and not plat.startswith('emscripten') - portmidi_dep = declare_dependency( - include_directories: pg_inc_dirs, - dependencies: cc.find_library( - 'portmidi', - dirs: pg_lib_dirs, - required: get_option('midi'), - ), + portmidi_lib = cc.find_library( + 'portmidi', + dirs: pg_lib_dirs, + required: midi_opt, ) + if portmidi_lib.found() + portmidi_dep = declare_dependency( + include_directories: pg_inc_dirs, + dependencies: portmidi_lib, + ) + endif endif portmidi_deps = [portmidi_dep] diff --git a/src_py/midi.py b/src_py/midi.py index 4838726c89..7d99b0de40 100644 --- a/src_py/midi.py +++ b/src_py/midi.py @@ -27,7 +27,14 @@ import pygame import pygame.locals -import pygame.pypm as _pypm + +try: + import pygame.pypm as _pypm +except ImportError as e: + raise ImportError( + "pygame.midi is not available because pygame was built without portmidi " + "support (the midi build option was disabled or portmidi was not found)" + ) from e # For backward compatibility. MIDIIN = pygame.locals.MIDIIN