From 7eca787571aab982acaadee79abb0f40f9f14b6a Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 27 Jun 2017 13:47:46 +0200 Subject: [PATCH] build: change how some OS specific source files are selected In a bunch of cases, we emulate highly platform specific APIs on a higher level across all OSes, such as IPC, terminal, subprocess handling, and more. We have source files for each OS, and they implement all the same mpv internal API. Selecting which source file to use on an OS can be tricky, because there is partially overlapping and emulated APIs (consider Cygwin on Windows). Add a pick_first_matching_dep() function to make this slightly easier and more structured. Also add dummy backends in some cases, to deal with APIs not being available. Clarify the Windows dependency identifiers, as these are the most confusing. --- input/input.c | 2 +- input/input.h | 2 ++ input/ipc-dummy.c | 13 +++++++++ options/options.c | 4 +-- osdep/subprocess-dummy.c | 9 +++++++ osdep/subprocess.c | 10 ------- osdep/terminal-dummy.c | 31 +++++++++++++++++++++ player/command.c | 2 +- waftools/dependencies.py | 12 +++++++++ wscript | 33 ++++++++++++++++------- wscript_build.py | 58 ++++++++++++++++++++++++---------------- 11 files changed, 129 insertions(+), 47 deletions(-) create mode 100644 input/ipc-dummy.c create mode 100644 osdep/subprocess-dummy.c create mode 100644 osdep/terminal-dummy.c diff --git a/input/input.c b/input/input.c index ee58709017..4a20ab1311 100644 --- a/input/input.c +++ b/input/input.c @@ -1392,7 +1392,7 @@ void mp_input_load_config(struct input_ctx *ictx) talloc_free(tmp); } -#if defined(__MINGW32__) +#if HAVE_WIN32_PIPES if (ictx->global->opts->input_file && *ictx->global->opts->input_file) mp_input_pipe_add(ictx, ictx->global->opts->input_file); #endif diff --git a/input/input.h b/input/input.h index ea8460889c..00f5473f7b 100644 --- a/input/input.h +++ b/input/input.h @@ -259,6 +259,8 @@ void mp_input_pipe_add(struct input_ctx *ictx, const char *filename); struct mp_ipc_ctx; struct mp_client_api; + +// Platform specific implementation, provided by ipc-*.c. struct mp_ipc_ctx *mp_init_ipc(struct mp_client_api *client_api, struct mpv_global *global); void mp_uninit_ipc(struct mp_ipc_ctx *ctx); diff --git a/input/ipc-dummy.c b/input/ipc-dummy.c new file mode 100644 index 0000000000..d9c31c046c --- /dev/null +++ b/input/ipc-dummy.c @@ -0,0 +1,13 @@ +#include + +#include "input/input.h" + +struct mp_ipc_ctx *mp_init_ipc(struct mp_client_api *client_api, + struct mpv_global *global) +{ + return NULL; +} + +void mp_uninit_ipc(struct mp_ipc_ctx *ctx) +{ +} diff --git a/options/options.c b/options/options.c index a803dccf5d..4c42164111 100644 --- a/options/options.c +++ b/options/options.c @@ -168,7 +168,7 @@ static const m_option_t mp_vo_opt_list[] = { OPT_CHOICE("x11-bypass-compositor", x11_bypass_compositor, 0, ({"no", 0}, {"yes", 1}, {"fs-only", 2}, {"never", 3})), #endif -#if HAVE_WIN32 +#if HAVE_WIN32_DESKTOP OPT_STRING("vo-mmcss-profile", mmcss_profile, 0), #endif #if HAVE_DRM @@ -273,7 +273,7 @@ const m_option_t mp_opts[] = { OPT_STRING("log-file", log_file, CONF_PRE_PARSE | M_OPT_FILE | UPDATE_TERM), OPT_FLAG("msg-module", msg_module, UPDATE_TERM), OPT_FLAG("msg-time", msg_time, UPDATE_TERM), -#if defined(_WIN32) && HAVE_GPL +#if HAVE_WIN32_DESKTOP && HAVE_GPL OPT_CHOICE("priority", w32_priority, UPDATE_PRIORITY, ({"no", 0}, {"realtime", REALTIME_PRIORITY_CLASS}, diff --git a/osdep/subprocess-dummy.c b/osdep/subprocess-dummy.c new file mode 100644 index 0000000000..791c90e566 --- /dev/null +++ b/osdep/subprocess-dummy.c @@ -0,0 +1,9 @@ +#include "subprocess.h" + +int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx, + subprocess_read_cb on_stdout, subprocess_read_cb on_stderr, + char **error) +{ + *error = "unsupported"; + return -1; +} diff --git a/osdep/subprocess.c b/osdep/subprocess.c index bc18f44652..1e94d23b67 100644 --- a/osdep/subprocess.c +++ b/osdep/subprocess.c @@ -61,13 +61,3 @@ void mp_subprocess_detached(struct mp_log *log, char **args) if (pthread_create(&thread, NULL, run_subprocess, p)) talloc_free(p); } - -#if !HAVE_SUBPROCESS -int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx, - subprocess_read_cb on_stdout, subprocess_read_cb on_stderr, - char **error) -{ - *error = "unsupported"; - return -1; -} -#endif diff --git a/osdep/terminal-dummy.c b/osdep/terminal-dummy.c new file mode 100644 index 0000000000..4a3787e0fa --- /dev/null +++ b/osdep/terminal-dummy.c @@ -0,0 +1,31 @@ +#include "terminal.h" + +void terminal_init(void) +{ +} + +void terminal_setup_getch(struct input_ctx *ictx) +{ +} + +void terminal_uninit(void) +{ +} + +bool terminal_in_background(void) +{ + return false; +} + +void terminal_get_size(int *w, int *h) +{ +} + +void mp_write_console_ansi(void *wstream, char *buf) +{ +} + +bool terminal_try_attach(void) +{ + return false; +} diff --git a/player/command.c b/player/command.c index bc9ee4ab26..b83196c677 100644 --- a/player/command.c +++ b/player/command.c @@ -5757,7 +5757,7 @@ void mp_notify(struct MPContext *mpctx, int event, void *arg) static void update_priority(struct MPContext *mpctx) { -#if defined(_WIN32) && HAVE_GPL +#if HAVE_WIN32_DESKTOP && HAVE_GPL struct MPOpts *opts = mpctx->opts; if (opts->w32_priority > 0) SetPriorityClass(GetCurrentProcess(), opts->w32_priority); diff --git a/waftools/dependencies.py b/waftools/dependencies.py index 87f236b362..994e1d10b2 100644 --- a/waftools/dependencies.py +++ b/waftools/dependencies.py @@ -212,6 +212,17 @@ def filtered_sources(ctx, sources): return [__source_file__(source) for source in sources \ if __unpack_and_check_filter__(source)] +""" +Like filtered_sources(), but pick only the first entry that matches, and +return its filename. +""" +def pick_first_matching_dep(ctx, deps): + files = filtered_sources(ctx, deps) + if len(files) > 0: + return files[0] + else: + raise DependencyError + def env_fetch(tx): def fn(ctx): deps = ctx.env.satisfied_deps @@ -223,6 +234,7 @@ def dependencies_use(ctx): return [inflector.storage_key(dep) for dep in ctx.env.satisfied_deps] BuildContext.filtered_sources = filtered_sources +BuildContext.pick_first_matching_dep = pick_first_matching_dep BuildContext.dependencies_use = dependencies_use BuildContext.dependencies_includes = env_fetch(lambda x: "INCLUDES_{0}".format(x)) BuildContext.dependency_satisfied = dependency_satisfied diff --git a/wscript b/wscript index 58d64837af..7ac4982da4 100644 --- a/wscript +++ b/wscript @@ -12,6 +12,18 @@ from waftools.checks.custom import * c_preproc.go_absolute=True # enable system folders c_preproc.standard_includes.append('/usr/local/include') +""" +Dependency identifiers (for win32 vs. Unix): + wscript / C source meaning + -------------------------------------------------------------------------- + posix / HAVE_POSIX: defined on Linux, OSX, Cygwin + (Cygwin emulates POSIX APIs on Windows) + mingw / __MINGW32__: defined if posix is not defined + (Windows without Cygwin) + os-win32 / _WIN32: defined if basic windows.h API is available + win32-desktop / HAVE_WIN32_DESKTOP: defined if desktop windows.h API is available +""" + build_options = [ { 'name': '--cplayer', @@ -132,15 +144,15 @@ main_dependencies = [ 'fmsg': 'Unable to find either POSIX or MinGW-w64 environment, ' \ 'or compiler does not work.', }, { - 'name': 'win32', - 'desc': 'win32', + 'name': 'win32-desktop', + 'desc': 'win32 desktop APIs', 'deps_any': [ 'os-win32', 'os-cygwin' ], 'func': check_cc(lib=['winmm', 'gdi32', 'ole32', 'uuid', 'avrt', 'dwmapi']), }, { 'name': '--win32-internal-pthreads', 'desc': 'internal pthread wrapper for win32 (Vista+)', 'deps_neg': [ 'posix' ], - 'deps': [ 'win32' ], + 'deps': [ 'os-win32' ], 'func': check_true, }, { 'name': 'pthreads', @@ -203,10 +215,11 @@ iconv support use --disable-iconv.", 'posix_spawnp(0,0,0,0,0,0); kill(0,0)'), 'deps_neg': ['mingw'], }, { - 'name': 'subprocess', - 'desc': 'posix_spawnp() or MinGW', + 'name': 'win32-pipes', + 'desc': 'Windows pipe support', 'func': check_true, - 'deps_any': ['posix-spawn', 'mingw'], + 'deps': [ 'win32-desktop' ], + 'deps_neg': [ 'posix' ], }, { 'name': 'glob-win32', 'desc': 'glob() win32 replacement', @@ -506,7 +519,7 @@ audio_output_features = [ }, { 'name': '--wasapi', 'desc': 'WASAPI audio output', - 'deps': ['win32'], + 'deps': ['win32-desktop'], 'func': check_cc(fragment=load_fragment('wasapi.c')), } ] @@ -583,7 +596,7 @@ video_output_features = [ } , { 'name': '--gl-win32', 'desc': 'OpenGL Win32 Backend', - 'deps': [ 'win32' ], + 'deps': [ 'win32-desktop' ], 'groups': [ 'gl' ], 'func': check_statement('windows.h', 'wglCreateContext(0)', lib='opengl32') @@ -671,7 +684,7 @@ video_output_features = [ }, { 'name': '--direct3d', 'desc': 'Direct3D support', - 'deps': [ 'win32' ], + 'deps': [ 'win32-desktop' ], 'func': check_cc(header_name='d3d9.h'), }, { 'name': '--android', @@ -774,7 +787,7 @@ hwaccel_features = [ }, { 'name': '--d3d-hwaccel', 'desc': 'DXVA2 and D3D11VA hwaccel', - 'deps': [ 'win32' ], + 'deps': [ 'win32-desktop' ], 'func': check_true, }, { 'name': '--d3d-hwaccel-new', diff --git a/wscript_build.py b/wscript_build.py index 39c31fe697..e693161971 100644 --- a/wscript_build.py +++ b/wscript_build.py @@ -101,21 +101,35 @@ def build(ctx): ctx(features = "ebml_header", target = "ebml_types.h") ctx(features = "ebml_definitions", target = "ebml_defs.c") - if ctx.env.DEST_OS == 'win32': - main_fn_c = 'osdep/main-fn-win.c' - elif ctx.dependency_satisfied('cocoa'): - main_fn_c = 'osdep/main-fn-cocoa.c' - else: - main_fn_c = 'osdep/main-fn-unix.c' + main_fn_c = ctx.pick_first_matching_dep([ + ( "osdep/main-fn-win.c", "win32-desktop" ), + ( "osdep/main-fn-cocoa.c", "cocoa" ), + ( "osdep/main-fn-unix.c" ), + ]) - getch2_c = { - 'win32': 'osdep/terminal-win.c', - }.get(ctx.env.DEST_OS, "osdep/terminal-unix.c") + getch2_c = ctx.pick_first_matching_dep([ + ( "osdep/terminal-unix.c", "posix" ), + ( "osdep/terminal-win.c", "win32-desktop" ), + ( "osdep/terminal-dummy.c" ), + ]) - timer_c = { - 'win32': 'osdep/timer-win2.c', - 'darwin': 'osdep/timer-darwin.c', - }.get(ctx.env.DEST_OS, "osdep/timer-linux.c") + timer_c = ctx.pick_first_matching_dep([ + ( "osdep/timer-win2.c", "os-win32" ), + ( "osdep/timer-darwin.c", "os-darwin" ), + ( "osdep/timer-linux.c", "posix" ), + ]) + + ipc_c = ctx.pick_first_matching_dep([ + ( "input/ipc-unix.c", "posix" ), + ( "input/ipc-win.c", "win32-desktop" ), + ( "input/ipc-dummy.c" ), + ]) + + subprocess_c = ctx.pick_first_matching_dep([ + ( "osdep/subprocess-posix.c", "posix-spawn" ), + ( "osdep/subprocess-win.c", "win32-desktop" ), + ( "osdep/subprocess-dummy.c" ), + ]) sources = [ ## Audio @@ -208,10 +222,9 @@ def build(ctx): ( "input/event.c" ), ( "input/input.c" ), ( "input/ipc.c" ), - ( "input/ipc-unix.c", "!mingw" ), - ( "input/ipc-win.c", "mingw" ), + ( ipc_c ), ( "input/keycodes.c" ), - ( "input/pipe-win32.c", "mingw" ), + ( "input/pipe-win32.c", "win32-pipes" ), ## Misc ( "misc/bstr.c" ), @@ -312,7 +325,7 @@ def build(ctx): ( "video/vdpau.c", "vdpau" ), ( "video/vdpau_mixer.c", "vdpau" ), ( "video/vt.c", "videotoolbox-hwaccel" ), - ( "video/decode/d3d.c", "win32" ), + ( "video/decode/d3d.c", "win32-desktop" ), ( "video/decode/dec_video.c"), ( "video/decode/hw_cuda.c", "cuda-hwaccel" ), ( "video/decode/hw_dxva2.c", "d3d-hwaccel" ), @@ -402,9 +415,9 @@ def build(ctx): ( "video/out/vo_wayland.c", "wayland" ), ( "video/out/vo_x11.c" , "x11" ), ( "video/out/vo_xv.c", "xv" ), - ( "video/out/w32_common.c", "win32" ), - ( "video/out/win32/displayconfig.c", "win32" ), - ( "video/out/win32/droptarget.c", "win32" ), + ( "video/out/w32_common.c", "win32-desktop" ), + ( "video/out/win32/displayconfig.c", "win32-desktop" ), + ( "video/out/win32/droptarget.c", "win32-desktop" ), ( "video/out/win32/exclusive_hack.c", "gl-win32" ), ( "video/out/wayland_common.c", "wayland" ), ( "video/out/wayland/buffer.c", "wayland" ), @@ -426,8 +439,7 @@ def build(ctx): ( "osdep/macosx_touchbar.m", "macos-touchbar" ), ( "osdep/semaphore_osx.c" ), ( "osdep/subprocess.c" ), - ( "osdep/subprocess-posix.c", "posix-spawn" ), - ( "osdep/subprocess-win.c", "os-win32" ), + ( subprocess_c ), ( "osdep/path-macosx.m", "cocoa" ), ( "osdep/path-unix.c"), ( "osdep/path-win.c", "os-win32" ), @@ -435,7 +447,7 @@ def build(ctx): ( "osdep/glob-win.c", "glob-win32" ), ( "osdep/w32_keyboard.c", "os-win32" ), ( "osdep/w32_keyboard.c", "os-cygwin" ), - ( "osdep/windows_utils.c", "win32" ), + ( "osdep/windows_utils.c", "os-win32" ), ( "osdep/mpv.rc", "win32-executable" ), ( "osdep/win32/pthread.c", "win32-internal-pthreads"), ( "osdep/android/strnlen.c", "android"),