- 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.
This commit is contained in:
Jōshin 2019-01-17 15:48:49 -08:00
parent 76251f6b4c
commit 950b19cad8
7 changed files with 56 additions and 32 deletions

View File

@ -1,19 +1,25 @@
libent is a cross-platform wrapper around `getentropy(2)`. It exports libent is a cross-platform wrapper around `getentropy(2)`. It exports
one symbol, `ent_getentropy`. If `getentropy` is available, then it's one symbol, `ent_getentropy`. If getentropy is available, then it's just
just a shim around that. Otherwise, it uses `getrandom(2)` (available a shim around that. Otherwise, it uses `getrandom(2)` (available since
since kernel 3.17) on Linux, or `/dev/urandom` on other \*nix. kernel 3.17) on Linux, or `/dev/urandom` on other \*nix.
### Building ### Building
It uses meson. `meson ./build && ninja -C build` should do the trick. 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 If you want to release a binary distribution of your program, you may
Linux, this means using `getrandom` directly; on other \*nix, it means want to build with `-Dent_compat=true`. This tells libent not to try to
reading from `/dev/urandom`. This may make sense if you want your use getentropy, which might result in a program that can run against
binaries to run on older versions of the same OS. If your program is an older libc version on some platforms. (`ent_compat` does nothing on
mostly built from source, don't bother. OpenBSD; we claim that 5.6 is old enough for anyone's purposes who wants
to use this library.)
### Why? ### Why?

View File

@ -1 +1,3 @@
#install_headers('ent.h') # XX uncomment to install locally if get_option('ent_install')
install_headers('ent.h')
endif

View File

@ -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') inc = include_directories('include')
subdir('include') subdir('include')
subdir('src') subdir('src')
ent_sample = executable('sample', ['sample.c'], libent_dep = declare_dependency(
dependencies: [libent_dep]) 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

View File

@ -1,2 +1,4 @@
option('ent_compat', type : 'boolean', value : false, 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')

View File

@ -3,4 +3,3 @@
#mesondefine ENT_GE_SYSRANDOM #mesondefine ENT_GE_SYSRANDOM
#mesondefine ENT_GETRANDOM #mesondefine ENT_GETRANDOM
#mesondefine ENT_URANDOM #mesondefine ENT_URANDOM
#mesondefine ENT_IMPL

View File

@ -13,13 +13,13 @@
return getentropy(buf, len); return getentropy(buf, len);
} }
#elif defined(ENT_IMPL) #elif defined(ENT_GETRANDOM) || defined(ENT_URANDOM)
# include <errno.h> # include <errno.h>
# if defined(ENT_GETRANDOM) # if defined(ENT_GETRANDOM)
# define _GNU_SOURCE # define _GNU_SOURCE
# include <unistd.h> # include <unistd.h>
# include <sys/syscall.h> # include <sys/syscall.h>
# elif defined(ENT_URANDOM) # else
# include <stdio.h> # include <stdio.h>
# endif # endif
@ -41,7 +41,7 @@
r = syscall(SYS_getrandom, buf, len, 0); r = syscall(SYS_getrandom, buf, len, 0);
if (r < 0) if (r < 0)
return r; return r;
# elif defined(ENT_URANDOM) # else
{ {
FILE *f; FILE *f;

View File

@ -1,5 +1,5 @@
conf_data = configuration_data() conf_data = configuration_data()
compiler = meson.get_compiler('c') cc = meson.get_compiler('c')
# OpenBSD has had getentropy since 5.6. It's fine. # OpenBSD has had getentropy since 5.6. It's fine.
compat = get_option('ent_compat') and host_machine.system() != 'openbsd' compat = get_option('ent_compat') and host_machine.system() != 'openbsd'
@ -9,14 +9,14 @@ if not compat
#include <sys/random.h> #include <sys/random.h>
int main() { return getentropy((void*)0, 0); } 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) conf_data.set('ENT_GE_SYSRANDOM', sysrandom)
if not sysrandom if not sysrandom
code = '''#include <unistd.h> code = '''#include <unistd.h>
int main() { return getentropy((void*)0, 0); } 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 endif
have_getentropy = sysrandom or unistd have_getentropy = sysrandom or unistd
conf_data.set('ENT_GETENTROPY', have_getentropy) conf_data.set('ENT_GETENTROPY', have_getentropy)
@ -31,21 +31,18 @@ if not have_getentropy
#include <sys/syscall.h> #include <sys/syscall.h>
int main() { return !syscall(SYS_getrandom, (void*)0, 0, 0); } 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) conf_data.set('ENT_GETRANDOM', getrandom)
if not getrandom if not getrandom
code = '''#include <stdio.h> code = '''#include <stdio.h>
int main() { return fopen("/dev/urandom", "r") ? 0 : 1; } 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) conf_data.set('ENT_URANDOM', urandom)
endif endif
ent_impl = getrandom or urandom
conf_data.set('ENT_IMPL', ent_impl)
endif endif
config_file = configure_file( config_file = configure_file(
output: 'config.h', output: 'config.h',
input: 'config.h.in', input: 'config.h.in',
@ -54,9 +51,10 @@ config_file = configure_file(
ent_sources = ['ent.c', config_file] ent_sources = ['ent.c', config_file]
libent = static_library('ent', ent_sources, libent = static_library(
include_directories: inc, 'ent',
c_args: ['-Wall', '-pedantic'], ent_sources,
install: false) # XX set to true to install locally include_directories: inc,
c_args: ['-Wall', '-pedantic'],
libent_dep = declare_dependency(include_directories: inc, link_with: libent) install: get_option('ent_install'),
)