From 950b19cad8e49c496228404a83928f020c1b764f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Thu, 17 Jan 2019 15:48:49 -0800 Subject: [PATCH] v0.2.0 - ent_install option to install locally (defaults to false.) - Add pkg-config info if we're installing (hence the minor version bump.) - Remove ENT_IMPL, making the preprocessor logic in ent.c more foolproof. - Change meson.build style around a bit. --- README.md | 24 +++++++++++++++--------- include/meson.build | 4 +++- meson.build | 23 ++++++++++++++++++++--- meson_options.txt | 4 +++- src/config.h.in | 1 - src/ent.c | 6 +++--- src/meson.build | 26 ++++++++++++-------------- 7 files changed, 56 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index e6a56ee49..189a2c32e 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,25 @@ libent is a cross-platform wrapper around `getentropy(2)`. It exports -one symbol, `ent_getentropy`. If `getentropy` is available, then it's -just a shim around that. Otherwise, it uses `getrandom(2)` (available -since kernel 3.17) on Linux, or `/dev/urandom` on other \*nix. +one symbol, `ent_getentropy`. If getentropy is available, then it's just +a shim around that. Otherwise, it uses `getrandom(2)` (available since +kernel 3.17) on Linux, or `/dev/urandom` on other \*nix. ### Building It uses meson. `meson ./build && ninja -C build` should do the trick. -#### Build options +The main intended way to use this library is to depend on it from +another meson project, i.e., to pull `libent_dep` out from this library +as a [subproject](https://mesonbuild.com/Subprojects.html). It's also +possible to install it as a regular library if you want to for some +reason — just run meson with `-Dent_install=true` and add on a `sudo +ninja -C build install`. -It has one option, `ent_compat`, which tells it to be conservative. On -Linux, this means using `getrandom` directly; on other \*nix, it means -reading from `/dev/urandom`. This may make sense if you want your -binaries to run on older versions of the same OS. If your program is -mostly built from source, don't bother. +If you want to release a binary distribution of your program, you may +want to build with `-Dent_compat=true`. This tells libent not to try to +use getentropy, which might result in a program that can run against +an older libc version on some platforms. (`ent_compat` does nothing on +OpenBSD; we claim that 5.6 is old enough for anyone's purposes who wants +to use this library.) ### Why? diff --git a/include/meson.build b/include/meson.build index 35d213df0..3bac4c30c 100644 --- a/include/meson.build +++ b/include/meson.build @@ -1 +1,3 @@ -#install_headers('ent.h') # XX uncomment to install locally +if get_option('ent_install') + install_headers('ent.h') +endif diff --git a/meson.build b/meson.build index 3a911908d..1fed79bb6 100644 --- a/meson.build +++ b/meson.build @@ -1,9 +1,26 @@ -project('libent', 'c', version: '0.1.4', license: 'MIT') +project('libent', 'c', version: '0.2.0', license: 'MIT') inc = include_directories('include') subdir('include') subdir('src') -ent_sample = executable('sample', ['sample.c'], - dependencies: [libent_dep]) +libent_dep = declare_dependency( + include_directories: inc, + link_with: libent, +) + +ent_sample = executable( + 'sample', + ['sample.c'], + dependencies: [libent_dep], +) + +if get_option('ent_install') + pkg = import('pkgconfig') + pkg.generate( + libraries: [libent], + name: 'libent', + description: 'getentropy shim', + ) +endif diff --git a/meson_options.txt b/meson_options.txt index 036dde9b3..ef45b64da 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,2 +1,4 @@ option('ent_compat', type : 'boolean', value : false, - description : 'Tries to work on older systems. Useful for binary releases. (Uses getrandom on Linux, /dev/urandom on other *nix.)') + description : 'Don\'t rely on system getentropy') +option('ent_install', type : 'boolean', value : false, + description : 'Install locally') diff --git a/src/config.h.in b/src/config.h.in index 785c41524..8ed218c87 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -3,4 +3,3 @@ #mesondefine ENT_GE_SYSRANDOM #mesondefine ENT_GETRANDOM #mesondefine ENT_URANDOM -#mesondefine ENT_IMPL diff --git a/src/ent.c b/src/ent.c index f084adb4b..57d437941 100644 --- a/src/ent.c +++ b/src/ent.c @@ -13,13 +13,13 @@ return getentropy(buf, len); } -#elif defined(ENT_IMPL) +#elif defined(ENT_GETRANDOM) || defined(ENT_URANDOM) # include # if defined(ENT_GETRANDOM) # define _GNU_SOURCE # include # include -# elif defined(ENT_URANDOM) +# else # include # endif @@ -41,7 +41,7 @@ r = syscall(SYS_getrandom, buf, len, 0); if (r < 0) return r; -# elif defined(ENT_URANDOM) +# else { FILE *f; diff --git a/src/meson.build b/src/meson.build index 8dd73b680..03f3b1550 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,5 +1,5 @@ conf_data = configuration_data() -compiler = meson.get_compiler('c') +cc = meson.get_compiler('c') # OpenBSD has had getentropy since 5.6. It's fine. compat = get_option('ent_compat') and host_machine.system() != 'openbsd' @@ -9,14 +9,14 @@ if not compat #include int main() { return getentropy((void*)0, 0); } ''' - sysrandom = compiler.links(code, name: 'getentropy in sys/random.h') + sysrandom = cc.links(code, name: 'getentropy in sys/random.h') conf_data.set('ENT_GE_SYSRANDOM', sysrandom) if not sysrandom code = '''#include int main() { return getentropy((void*)0, 0); } ''' - unistd = compiler.links(code, name: 'getentropy in unistd.h') + unistd = cc.links(code, name: 'getentropy in unistd.h') endif have_getentropy = sysrandom or unistd conf_data.set('ENT_GETENTROPY', have_getentropy) @@ -31,21 +31,18 @@ if not have_getentropy #include int main() { return !syscall(SYS_getrandom, (void*)0, 0, 0); } ''' - getrandom = compiler.links(code, name: 'getrandom syscall available') + getrandom = cc.links(code, name: 'getrandom syscall available') conf_data.set('ENT_GETRANDOM', getrandom) if not getrandom code = '''#include int main() { return fopen("/dev/urandom", "r") ? 0 : 1; } ''' - urandom = compiler.run(code, name: 'can open /dev/urandom').returncode() == 0 + urandom = cc.run(code, name: 'can open /dev/urandom').returncode() == 0 conf_data.set('ENT_URANDOM', urandom) endif - ent_impl = getrandom or urandom - conf_data.set('ENT_IMPL', ent_impl) endif - config_file = configure_file( output: 'config.h', input: 'config.h.in', @@ -54,9 +51,10 @@ config_file = configure_file( ent_sources = ['ent.c', config_file] -libent = static_library('ent', ent_sources, - include_directories: inc, - c_args: ['-Wall', '-pedantic'], - install: false) # XX set to true to install locally - -libent_dep = declare_dependency(include_directories: inc, link_with: libent) +libent = static_library( + 'ent', + ent_sources, + include_directories: inc, + c_args: ['-Wall', '-pedantic'], + install: get_option('ent_install'), +)