OS X compatibility cleanups.

Reattach the licenses for the compat stuff to the source.
Only build/link the compat stuff when it's needed.
Rename compat/util.h so it's not confused with util.h.
Move all the platform specific defines inside compat/compat.h.
This commit is contained in:
Oliver Jowett 2015-09-07 12:58:49 +01:00
parent ae126d1009
commit 1c80d1b784
12 changed files with 114 additions and 82 deletions

View file

@ -28,6 +28,7 @@ endif
ifeq ($(UNAME), Darwin) ifeq ($(UNAME), Darwin)
# TODO: Putting GCC in C11 mode breaks things. # TODO: Putting GCC in C11 mode breaks things.
CFLAGS+=-std=c11 CFLAGS+=-std=c11
COMPAT+=compat/clock_gettime/clock_gettime.o compat/clock_nanosleep/clock_nanosleep.o
endif endif
all: dump1090 view1090 all: dump1090 view1090
@ -37,13 +38,13 @@ all: dump1090 view1090
dump1090.o: CFLAGS += `pkg-config --cflags librtlsdr` dump1090.o: CFLAGS += `pkg-config --cflags librtlsdr`
dump1090: dump1090.o anet.o compat/clock_gettime/clock_gettime.o compat/clock_nanosleep/clock_nanosleep.o interactive.o mode_ac.o mode_s.o net_io.o crc.o demod_2000.o demod_2400.o stats.o cpr.o icao_filter.o track.o util.o convert.o dump1090: dump1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o crc.o demod_2000.o demod_2400.o stats.o cpr.o icao_filter.o track.o util.o convert.o $(COMPAT)
$(CC) -g -o $@ $^ $(LIBS) $(LIBS_RTL) $(LDFLAGS) $(CC) -g -o $@ $^ $(LIBS) $(LIBS_RTL) $(LDFLAGS)
view1090: view1090.o anet.o compat/clock_gettime/clock_gettime.o interactive.o mode_ac.o mode_s.o net_io.o crc.o stats.o cpr.o icao_filter.o track.o util.o view1090: view1090.o anet.o interactive.o mode_ac.o mode_s.o net_io.o crc.o stats.o cpr.o icao_filter.o track.o util.o $(COMPAT)
$(CC) -g -o $@ $^ $(LIBS) $(LDFLAGS) $(CC) -g -o $@ $^ $(LIBS) $(LDFLAGS)
faup1090: faup1090.o anet.o compat/clock_gettime/clock_gettime.o mode_ac.o mode_s.o net_io.o crc.o stats.o cpr.o icao_filter.o track.o util.o faup1090: faup1090.o anet.o mode_ac.o mode_s.o net_io.o crc.o stats.o cpr.o icao_filter.o track.o util.o $(COMPAT)
$(CC) -g -o $@ $^ $(LIBS) $(LDFLAGS) $(CC) -g -o $@ $^ $(LIBS) $(LDFLAGS)
clean: clean:

View file

@ -1,31 +0,0 @@
/*
* clock_gettime_stub.c
* gcc -Wall -c clock_gettime_stub.c
* posix realtime functions; MacOS user space glue
*/
/* @comment
* other possible implementation using intel builtin rdtsc
* rdtsc-workaround: http://www.mcs.anl.gov/~kazutomo/rdtsc.html
*
* we could get the ticks by doing this
*
* __asm __volatile("mov %%ebx, %%esi\n\t"
* "cpuid\n\t"
* "xchg %%esi, %%ebx\n\t"
* "rdtsc"
* : "=a" (a),
* "=d" (d)
* );
* we could even replace our tricky sched_yield call by assembly code to get a better accurency,
* anyway the following C stub will satisfy 99% of apps using posix clock_gettime call,
* moreover, the setter version (clock_settime) could be easly written using mach primitives:
* http://www.opensource.apple.com/source/xnu/xnu-${VERSION}/osfmk/man/ (clock_[set|get]_time)
*
* hackers don't be crackers, don't you use a flush toilet?
*
*
* @see draft: ./posix-realtime-stub/posix-realtime-stub.c
*
*/

View file

@ -1,4 +1,63 @@
#ifdef __APPLE__ /*
* Copyright (c), MM Weiss
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the MM Weiss nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* clock_gettime_stub.c
* gcc -Wall -c clock_gettime_stub.c
* posix realtime functions; MacOS user space glue
*/
/* @comment
* other possible implementation using intel builtin rdtsc
* rdtsc-workaround: http://www.mcs.anl.gov/~kazutomo/rdtsc.html
*
* we could get the ticks by doing this
*
* __asm __volatile("mov %%ebx, %%esi\n\t"
* "cpuid\n\t"
* "xchg %%esi, %%ebx\n\t"
* "rdtsc"
* : "=a" (a),
* "=d" (d)
* );
* we could even replace our tricky sched_yield call by assembly code to get a better accurency,
* anyway the following C stub will satisfy 99% of apps using posix clock_gettime call,
* moreover, the setter version (clock_settime) could be easly written using mach primitives:
* http://www.opensource.apple.com/source/xnu/xnu-${VERSION}/osfmk/man/ (clock_[set|get]_time)
*
* hackers don't be crackers, don't you use a flush toilet?
*
*
* @see draft: ./posix-realtime-stub/posix-realtime-stub.c
*
*/
#include "clock_gettime.h" #include "clock_gettime.h"
#include <mach/clock.h> // for clock_get_time #include <mach/clock.h> // for clock_get_time
@ -75,5 +134,3 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp)
} }
return retval; return retval;
} }
#endif // __APPLE__

View file

@ -1,5 +1,3 @@
#ifdef __APPLE__
#ifndef CLOCK_GETTIME_H #ifndef CLOCK_GETTIME_H
#define CLOCK_GETTIME_H #define CLOCK_GETTIME_H
@ -23,4 +21,3 @@ static mach_timebase_info_data_t __clock_gettime_inf;
int clock_gettime(clockid_t clk_id, struct timespec *tp); int clock_gettime(clockid_t clk_id, struct timespec *tp);
#endif // CLOCK_GETTIME_H #endif // CLOCK_GETTIME_H
#endif // __APPLE__

View file

@ -2,7 +2,22 @@
* clock_nanosleep.c - clock_nanosleep() replacement * clock_nanosleep.c - clock_nanosleep() replacement
*/ */
#ifdef __APPLE__ /***********************************************************************
* Copyright © 2006 Rémi Denis-Courmont. *
* This program is free software; you can redistribute and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation; version 2 of the license, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, you can get it from: *
* http://www.gnu.org/copyleft/gpl.html *
***********************************************************************/
#include "clock_nanosleep.h" #include "clock_nanosleep.h"
#include <errno.h> // for errno, EINVAL #include <errno.h> // for errno, EINVAL
@ -45,5 +60,3 @@ int clock_nanosleep(clockid_t id, int flags, const struct timespec *ts,
return ret ? errno : 0; return ret ? errno : 0;
} }
#endif // __APPLE__

View file

@ -1,5 +1,3 @@
#ifdef __APPLE__
#ifndef CLOCK_NANOSLEEP_H #ifndef CLOCK_NANOSLEEP_H
#define CLOCK_NANOSLEEP_H #define CLOCK_NANOSLEEP_H
@ -24,4 +22,3 @@ int clock_nanosleep (clockid_t id, int flags, const struct timespec *ts,
struct timespec *ots); struct timespec *ots);
#endif //CLOCK_NANOSLEEP_H #endif //CLOCK_NANOSLEEP_H
#endif // __APPLE__

32
compat/compat.h Normal file
View file

@ -0,0 +1,32 @@
#ifndef COMPAT_UTIL_H
#define COMPAT_UTIL_H
/*
* Platform-specific bits
*/
#if defined(__APPLE__)
/* implementations of clock_gettime, clock_nanosleep */
#include "clock_gettime/clock_gettime.h"
#include "clock_nanosleep/clock_nanosleep.h"
/*
* Mach endian conversion
*/
# include <libkern/OSByteOrder.h>
# define bswap_16 OSSwapInt16
# define bswap_32 OSSwapInt32
# define bswap_64 OSSwapInt64
# include <machine/endian.h>
# define le16toh(x) OSSwapLittleToHostInt16(x)
# define le32toh(x) OSSwapLittleToHostInt32(x)
#else // other platforms
# include <endian.h>
#endif
#endif //COMPAT_UTIL_H

View file

@ -1,20 +0,0 @@
#ifndef COMPAT_UTIL_H
#define COMPAT_UTIL_H
#include "clock_gettime/clock_gettime.h"
#include "clock_nanosleep/clock_nanosleep.h"
/*
* Mach endian conversion
*/
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#define bswap_16 OSSwapInt16
#define bswap_32 OSSwapInt32
#define bswap_64 OSSwapInt64
#include <machine/endian.h>
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#endif
#endif //COMPAT_UTIL_H

View file

@ -19,10 +19,6 @@
#include "dump1090.h" #include "dump1090.h"
#ifdef __APPLE__
#include "compat/util.h"
#endif
struct converter_state { struct converter_state {
float dc_a; float dc_a;
float dc_b; float dc_b;

View file

@ -53,14 +53,6 @@
#include <stdarg.h> #include <stdarg.h>
#ifdef __linux__
#include <endian.h>
#endif
#ifdef __APPLE__
#include "compat/util.h"
#endif
static int verbose_device_search(char *s); static int verbose_device_search(char *s);
// //

View file

@ -82,6 +82,8 @@
#include "winstubs.h" //Put everything Windows specific in here #include "winstubs.h" //Put everything Windows specific in here
#endif #endif
#include "compat/compat.h"
// Avoid a dependency on rtl-sdr except where it's really needed. // Avoid a dependency on rtl-sdr except where it's really needed.
typedef struct rtlsdr_dev rtlsdr_dev_t; typedef struct rtlsdr_dev rtlsdr_dev_t;

View file

@ -54,10 +54,6 @@
#include <assert.h> #include <assert.h>
#ifdef __APPLE__
#include "compat/util.h"
#endif
// //
// ============================= Networking ============================= // ============================= Networking =============================
// //