Merge PR #38 - OSX/FreeBSD/OpenBSD portabilty.

This is minimally tested on OSX, and not at all on *BSD.
Since I did some cleanups in the compat code without testing on *BSD,
it could well be broken there.

This may also fix #33 as #38 included those changes.
This commit is contained in:
Oliver Jowett 2020-08-07 15:09:22 +08:00
commit af1f4f84a9
7 changed files with 118 additions and 75 deletions

View File

@ -1,36 +1,77 @@
PROGNAME=dump1090
# Try to autodetect available libraries if no explicit setting was used
ifndef RTLSDR
ifdef RTLSDR_PREFIX
RTLSDR := yes
else
RTLSDR := $(shell pkg-config --exists librtlsdr && echo "yes" || echo "no")
endif
endif
ifndef BLADERF
BLADERF := $(shell pkg-config --exists libbladeRF && echo "yes" || echo "no")
endif
ifndef HACKRF
HACKRF := $(shell pkg-config --exists libhackrf && echo "yes" || echo "no")
endif
ifndef LIMESDR
LIMESDR := $(shell pkg-config --exists LimeSuite && echo "yes" || echo "no")
endif
DUMP1090_VERSION ?= unknown
CPPFLAGS += -DMODES_DUMP1090_VERSION=\"$(DUMP1090_VERSION)\" -DMODES_DUMP1090_VARIANT=\"dump1090-fa\"
DIALECT = -std=c11
CFLAGS += $(DIALECT) -O3 -g -Wall -Wmissing-declarations -Werror -W -D_DEFAULT_SOURCE -fno-common
LIBS = -lpthread -lm -lrt
LIBS = -lpthread -lm
SDR_OBJ = sdr.o fifo.o sdr_ifile.o
# Try to autodetect available libraries via pkg-config if no explicit setting was used
PKGCONFIG=$(shell pkg-config --version >/dev/null 2>&1 && echo "yes" || echo "no")
ifeq ($(PKGCONFIG), yes)
ifndef RTLSDR
ifdef RTLSDR_PREFIX
RTLSDR := yes
else
RTLSDR := $(shell pkg-config --exists librtlsdr && echo "yes" || echo "no")
endif
endif
ifndef BLADERF
BLADERF := $(shell pkg-config --exists libbladeRF && echo "yes" || echo "no")
endif
ifndef HACKRF
HACKRF := $(shell pkg-config --exists libhackrf && echo "yes" || echo "no")
endif
ifndef LIMESDR
LIMESDR := $(shell pkg-config --exists LimeSuite && echo "yes" || echo "no")
endif
else
# pkg-config not available. Only use explicitly enabled libraries.
RTLSDR ?= no
BLADERF ?= no
HACKRF ?= no
LIMESDR ?= no
endif
UNAME := $(shell uname)
ifeq ($(UNAME), Linux)
CFLAGS += -D_DEFAULT_SOURCE
LIBS += -lrt
LIBS_USB += -lusb-1.0
endif
ifeq ($(UNAME), Darwin)
ifneq ($(shell sw_vers -productVersion | egrep '^10\.([0-9]|1[01])\.'),) # Mac OS X ver <= 10.11
CFLAGS += -DMISSING_GETTIME
COMPAT += compat/clock_gettime/clock_gettime.o
endif
CFLAGS += -DMISSING_NANOSLEEP
COMPAT += compat/clock_nanosleep/clock_nanosleep.o
LIBS_USB += -lusb-1.0
endif
ifeq ($(UNAME), OpenBSD)
CFLAGS += -DMISSING_NANOSLEEP
COMPAT += compat/clock_nanosleep/clock_nanosleep.o
LIBS_USB += -lusb-1.0
endif
ifeq ($(UNAME), FreeBSD)
CFLAGS += -D_DEFAULT_SOURCE
LIBS += -lrt
LIBS_USB += -lusb
endif
RTLSDR ?= yes
BLADERF ?= yes
ifeq ($(RTLSDR), yes)
SDR_OBJ += sdr_rtlsdr.o
CPPFLAGS += -DENABLE_RTLSDR
@ -38,13 +79,19 @@ ifeq ($(RTLSDR), yes)
ifdef RTLSDR_PREFIX
CPPFLAGS += -I$(RTLSDR_PREFIX)/include
ifeq ($(STATIC), yes)
LIBS_SDR += -L$(RTLSDR_PREFIX)/lib -Wl,-Bstatic -lrtlsdr -Wl,-Bdynamic -lusb-1.0
LIBS_SDR += -L$(RTLSDR_PREFIX)/lib -Wl,-Bstatic -lrtlsdr -Wl,-Bdynamic $(LIBS_USB)
else
LIBS_SDR += -L$(RTLSDR_PREFIX)/lib -lrtlsdr -lusb-1.0
LIBS_SDR += -L$(RTLSDR_PREFIX)/lib -lrtlsdr $(LIBS_USB)
endif
else
CFLAGS += $(shell pkg-config --cflags librtlsdr)
# some packaged .pc files are massively broken, try to handle it
# FreeBSD's librtlsdr.pc includes -std=gnu89 in cflags
RTLSDR_CFLAGS := $(shell pkg-config --cflags librtlsdr)
CFLAGS += $(filter-out -std=%,$(RTLSDR_CFLAGS))
# some linux librtlsdr packages return a bare -L with no path in --libs
# which horribly confuses things because it eats the next option on the command line
RTLSDR_LFLAGS := $(shell pkg-config --libs-only-L librtlsdr)
ifeq ($(RTLSDR_LFLAGS),-L)
LIBS_SDR += $(shell pkg-config --libs-only-l --libs-only-other librtlsdr)

View File

@ -90,3 +90,15 @@ libhackrf.
``make LIMESDR=no`` will disable LimeSDR support and remove the dependency on
libLimeSuite.
## Building on OSX
Minimal testing, YMMV.
```
$ brew install librtlsdr
$ brew install libbladerf
$ brew install pkg-config
$ make
```

View File

@ -58,8 +58,8 @@
*
*/
#include "clock_gettime.h"
#include "../compat.h"
#include <mach/mach_time.h>
#include <mach/clock.h> // for clock_get_time
#include <mach/clock_types.h> // for mach_timespec_t, CALENDAR_CLOCK, etc
#include <mach/kern_return.h> // for KERN_SUCCESS, kern_return_t
@ -70,6 +70,8 @@
#include <errno.h> // for EINVAL, errno
#include <unistd.h> // for getpid
static mach_timebase_info_data_t __clock_gettime_inf;
int clock_gettime(clockid_t clk_id, struct timespec *tp)
{
kern_return_t ret;

View File

@ -1,27 +1,6 @@
#ifndef CLOCK_GETTIME_H
#define CLOCK_GETTIME_H
#include <mach/mach_time.h> // Apple-only, but this isn't inclued on other BSDs
#ifdef _CLOCKID_T_DEFINED_
#define CLOCKID_T
#endif
#ifndef CLOCKID_T
#define CLOCKID_T
typedef enum
{
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID
} clockid_t;
#endif // ifndef CLOCKID_T
struct timespec;
static mach_timebase_info_data_t __clock_gettime_inf;
int clock_gettime(clockid_t clk_id, struct timespec *tp);
#endif // CLOCK_GETTIME_H

View File

@ -22,10 +22,7 @@
#include <errno.h> // for errno, EINVAL
#include <time.h> // for nanosleep, NULL
#include "clock_nanosleep.h"
#ifdef MISSING_GETTIME
#include "../clock_gettime/clock_gettime.h" // for clock_gettime
#endif
#include "../compat.h"
int clock_nanosleep(clockid_t id, int flags, const struct timespec *ts,
struct timespec *ots) {

View File

@ -1,28 +1,6 @@
#ifndef CLOCK_NANOSLEEP_H
#define CLOCK_NANOSLEEP_H
#ifdef _CLOCKID_T_DEFINED_
#define CLOCKID_T
#endif
#ifndef CLOCKID_T
#define CLOCKID_T
typedef enum
{
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID
} clockid_t;
#endif // ifndef CLOCKID_T
#ifndef TIMER_ABSTIME
#define TIMER_ABSTIME 1
#endif // TIMER_ABSTIME
struct timespec;
int clock_nanosleep (clockid_t id, int flags, const struct timespec *ts,
struct timespec *ots);

View File

@ -17,6 +17,10 @@
# include <machine/endian.h>
# define le16toh(x) OSSwapLittleToHostInt16(x)
# define le32toh(x) OSSwapLittleToHostInt32(x)
# define le64toh(x) OSSwapLittleToHostInt64(x)
#elif defined(__FreeBSD__)
#include <sys/endian.h>
#else // other platforms
@ -24,6 +28,30 @@
#endif
/* clock_* and time-related types */
#include <time.h>
#if defined(CLOCK_REALTIME)
# define HAVE_CLOCKID_T
#endif
#ifndef HAVE_CLOCKID_T
typedef enum
{
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID
} clockid_t;
#endif // !HAVE_CLOCKID_T
#ifndef TIMER_ABSTIME
#define TIMER_ABSTIME 1
#endif // !TIMER_ABSTIME
struct timespec;
#ifdef MISSING_NANOSLEEP
#include "clock_nanosleep/clock_nanosleep.h"
#endif