Repository navigation
Add optional shared library build #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Patch files are data, not source: they are applied to files whose line | ||
| # endings are decided by whoever fetched them. core.autocrlf would otherwise | ||
| # rewrite them on checkout, and the rewritten patch no longer matches the | ||
| # LF-terminated sources it is meant to apply to. | ||
| *.patch -text | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,15 +7,37 @@ documentation (library usage, speed testing, MOS correlation, etc). | |
|
|
||
| ## Usage | ||
|
|
||
| Compilation requires [Zig](https://ziglang.org/) ≥0.16.0 & a macOS, Linux, or | ||
| Unix-like operating system. To compile, run: | ||
| Compilation requires [Zig](https://ziglang.org/) ≥0.16.0. macOS, Linux, and | ||
| Windows are supported; Windows needs one extra step, described below. To | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows not supported. |
||
| compile, run: | ||
|
|
||
| ```sh | ||
| zig build --release=fast | ||
| ``` | ||
|
|
||
| You may add `-Dflto=true` for FLTO, and `-Dstrip=true` to strip the binary. | ||
|
|
||
| By default this builds the `fmetrics` binary and a static library. To also | ||
| build an installable shared library, pass `-Dshared=true`: | ||
|
|
||
| ```sh | ||
| zig build --release=fast -Dshared=true | ||
| ``` | ||
|
|
||
| On Windows, `fcvvdp` needs a small portability fix before the build will | ||
| succeed. It is carried as a patch in [`patches/`](patches/): | ||
|
|
||
| ```sh | ||
| zig build # populates zig-pkg/ | ||
| git apply --directory="zig-pkg/fcvvdp-<version>" \ | ||
| patches/0001-windows-portability.patch | ||
| zig build --release=fast -Dshared=true | ||
| ``` | ||
|
|
||
| `--directory` is needed because Zig extracts dependencies without their `.git` | ||
| directory, so running `git apply` from inside `zig-pkg/fcvvdp-*/` reports | ||
| `Skipped patch`. | ||
|
|
||
| `fmetrics` binary usage: | ||
|
|
||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ pub fn build(b: *std.Build) void { | |
| const optimize = b.standardOptimizeOption(.{}); | ||
| const strip = b.option(bool, "strip", "strip symbols from the binary, defaults to false") orelse false; | ||
| const flto = b.option(bool, "flto", "enable Link Time Optimization, defaults to false") orelse false; | ||
| const shared = b.option(bool, "shared", "also build and install a shared library (.dll/.so/.dylib), defaults to false") orelse false; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
| const options = b.addOptions(); | ||
| const zon = b.build_root.handle.readFileAlloc( | ||
| b.graph.io, | ||
|
|
@@ -164,6 +165,39 @@ pub fn build(b: *std.Build) void { | |
| // fmetrics.h | ||
| lib.installHeader(b.path("src/fmetrics.h"), "fmetrics.h"); | ||
|
|
||
| // 'libfmetrics' shared library, built only when -Dshared=true. | ||
| // | ||
| // The static library above is linked in whole rather than having its source | ||
| // list repeated, so the two cannot drift apart: the DLL is the same code, | ||
| // packaged for dynamic loading. Only the export list is platform-specific. | ||
| if (shared) { | ||
| const shared_lib = b.addLibrary(.{ | ||
| .name = "fmetrics", | ||
| .linkage = .dynamic, | ||
| .root_module = b.createModule(.{ | ||
| .target = target, | ||
| .optimize = optimize, | ||
| .link_libc = true, | ||
| }), | ||
| }); | ||
| shared_lib.root_module.linkLibrary(lib); | ||
| // Only Windows needs an explicit export list; a DLL there exports | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove everything here related to Windows, and clean up the comments – there are far too many comments in here. |
||
| // nothing by default. On ELF and Mach-O the symbols are already visible, | ||
| // and naming them would only narrow visibility. | ||
| if (target.result.os.tag == .windows) { | ||
| shared_lib.win32_module_definition = b.path("src/fmetrics.def"); | ||
| } | ||
| // On Windows a DLL also produces an import library, installed as | ||
| // 'lib/<name>.lib' -- the same path the static library above already | ||
| // occupies, so the two would overwrite each other. The static library | ||
| // already serves every purpose the import library would here, so skip | ||
| // it and leave 'lib/fmetrics.lib' alone. | ||
| b.getInstallStep().dependOn(&b.addInstallArtifact( | ||
| shared_lib, | ||
| .{ .implib_dir = .disabled }, | ||
| ).step); | ||
| } | ||
|
|
||
| // 'fmetrics' executable | ||
| const bin = b.addExecutable(.{ | ||
| .name = "fmetrics", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| diff --git a/src/cvvdp.c b/src/cvvdp.c | ||
| index bcafd12..11d9319 100644 | ||
| --- a/src/cvvdp.c | ||
| +++ b/src/cvvdp.c | ||
| @@ -26,6 +26,10 @@ | ||
| #include <math.h> | ||
| #include <unistd.h> | ||
|
|
||
| +#ifdef _WIN32 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Via the README:
Windows is explicitly unsupported and will not be supported officially at any point, sorry – I maintain this stance because I do not have Windows hardware and can't guarantee Windows as a first-class support target. Thus this file can be removed. |
||
| +#include <windows.h> | ||
| +#endif | ||
| + | ||
| static const CvvdpSimdDispatch* g_cvvdp_dispatch = NULL; | ||
| static pthread_once_t g_cvvdp_dispatch_once = PTHREAD_ONCE_INIT; | ||
|
|
||
| @@ -126,7 +130,11 @@ static void* cvvdp_thread_pool_worker(void* arg) { | ||
| } | ||
|
|
||
| static unsigned get_threadcnt(const unsigned threads) { | ||
| +#ifdef _WIN32 | ||
| + return threads ? threads : GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); | ||
| +#else | ||
| return threads ? threads : (unsigned)sysconf(_SC_NPROCESSORS_ONLN); | ||
| +#endif | ||
| } | ||
|
|
||
| static CvvdpThreadPool* cvvdp_thread_pool_create(const unsigned total_threads) { | ||
| @@ -1228,13 +1236,11 @@ static FcvvdpError cvvdp_process_pyramid_threaded(FcvvdpCtx* const c, | ||
| const int num_levels = c->num_bands; | ||
| CvvdpThreadPool* const pool = c->thread_pool; | ||
|
|
||
| - size_t total_size = 0; | ||
| int widths[CVVDP_MAX_LEVELS], heights[CVVDP_MAX_LEVELS]; | ||
| int tw = w, th = h; | ||
| for (int lev = 0; lev < num_levels; lev++) { | ||
| widths[lev] = tw; | ||
| heights[lev] = th; | ||
| - total_size += (size_t)tw * th; | ||
| tw = (tw + 1) / 2; | ||
| th = (th + 1) / 2; | ||
| } | ||
| @@ -1494,13 +1500,11 @@ static FcvvdpError cvvdp_process_pyramid_serial(FcvvdpCtx* const c, | ||
| const int w = c->width, h = c->height; | ||
| const int num_levels = c->num_bands; | ||
|
|
||
| - size_t total_size = 0; | ||
| int widths[CVVDP_MAX_LEVELS], heights[CVVDP_MAX_LEVELS]; | ||
| int tw = w, th = h; | ||
| for (int lev = 0; lev < num_levels; lev++) { | ||
| widths[lev] = tw; | ||
| heights[lev] = th; | ||
| - total_size += (size_t)tw * th; | ||
| tw = (tw + 1) / 2; | ||
| th = (th + 1) / 2; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| ; Windows module definition for the optional shared library (-Dshared=true). | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Via the README:
Windows is explicitly unsupported and will not be supported officially at any point, sorry – I maintain this stance because I do not have Windows hardware and can't guarantee Windows as a first-class support target. Thus this file can be removed. |
||
| ; | ||
| ; A DLL exports nothing by default, so every entry point declared in | ||
| ; fmetrics.h has to be named here or it stays private to the library and | ||
| ; GetProcAddress cannot find it. Keep this list in step with the public | ||
| ; declarations in fmetrics.h. | ||
| ; | ||
| ; fmetrics_workspace_reserve_data is intentionally absent: it is internal | ||
| ; plumbing, not part of the published ABI. | ||
| ; | ||
| ; This file is ignored on ELF and Mach-O, where symbols are exported by | ||
| ; default and listing them would only narrow visibility. | ||
|
|
||
| EXPORTS | ||
| fmetrics_version_str | ||
| fmetrics_error_str | ||
|
|
||
| fmetrics_workspace_create | ||
| fmetrics_workspace_destroy | ||
|
|
||
| fmetrics_iwssim_cmp | ||
| fmetrics_msssim_cmp | ||
|
|
||
| fmetrics_ssimu2_cmp | ||
| fmetrics_ssimu2_cmp_map | ||
|
|
||
| fmetrics_butteraugli_cmp | ||
| fmetrics_butteraugli_cmp_map | ||
|
|
||
| fmetrics_cvvdp_create | ||
| fmetrics_cvvdp_destroy | ||
| fmetrics_cvvdp_process_frame | ||
| fmetrics_cvvdp_reset | ||
| fmetrics_cvvdp_cmp | ||
| fmetrics_cvvdp_get_display_params | ||
| fmetrics_cvvdp_version_str | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Via the README:
Windows is explicitly unsupported and will not be supported officially at any point, sorry – I maintain this stance because I do not have Windows hardware and can't guarantee Windows as a first-class support target. This file is only useful for the Windows support patch, thus this file can be removed.