Skip to content

Commit

Permalink
Add feature tests for portability.
Browse files Browse the repository at this point in the history
Also, switch to using config.h instead of compiler flags.
  • Loading branch information
awesie committed Aug 2, 2017
1 parent 2e06c3d commit 1378c99
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 21 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required (VERSION 2.8)
include (CheckLibraryExists)
include (CheckSymbolExists)
include (ExternalProject)
project (nrsc5 C)

Expand Down Expand Up @@ -43,6 +44,12 @@ if (CMAKE_SYSTEM_PROCESSOR MATCHES "(i[456]|x)86.*")
endif()
endif()

set (CMAKE_REQUIRED_FLAGS --std=gnu11)
check_symbol_exists (strndup string.h HAVE_STRNDUP)
check_symbol_exists (CMPLXF complex.h HAVE_CMPLXF)
check_symbol_exists (__builtin_complex complex.h HAVE_BUILTIN_COMPLEX)
check_symbol_exists (_Imaginary_I complex.h HAVE_IMAGINARY_I)

if (USE_FAAD2)
# libao only used if we have FAAD2
find_library (AO_LIBRARY ao)
Expand Down
17 changes: 8 additions & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
add_compile_options (--std=gnu11 -O3 -Wall)

if (USE_COLOR)
add_definitions (-DLOG_USE_COLOR)
endif()

if (USE_THREADS)
add_definitions (-D_GNU_SOURCE -DUSE_THREADS)
add_definitions (-D_GNU_SOURCE)
set (THREAD_LIBRARY pthread)
check_library_exists (${THREAD_LIBRARY} pthread_setname_np "" HAVE_PTHREAD_SETNAME_NP)
if (HAVE_PTHREAD_SETNAME_NP AND CMAKE_SYSTEM_NAME MATCHES Linux)
add_definitions (-DHAVE_PTHREAD_SETNAME_NP)
if (CMAKE_SYSTEM_NAME MATCHES Linux)
check_library_exists (${THREAD_LIBRARY} pthread_setname_np "" HAVE_PTHREAD_SETNAME_NP)
endif()
endif()

if (USE_FAST_MATH)
add_compile_options (-ffast-math)
add_definitions (-DUSE_FAST_MATH)
endif()

configure_file (config.h.in config.h)
include_directories ("${CMAKE_CURRENT_BINARY_DIR}")

add_executable (
nrsc5
acquire.c
Expand All @@ -41,6 +38,8 @@ add_executable (
galois.c

log.c

strndup.c
)
target_link_libraries (
nrsc5
Expand Down
22 changes: 22 additions & 0 deletions src/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#cmakedefine USE_FAAD2
#cmakedefine USE_ID3V2LIB
#cmakedefine USE_COLOR
#cmakedefine USE_FAST_MATH
#cmakedefine USE_THREADS

#cmakedefine HAVE_PTHREAD_SETNAME_NP
#cmakedefine HAVE_STRNDUP
#cmakedefine HAVE_BUILTIN_COMPLEX
#cmakedefine HAVE_CMPLXF
#cmakedefine HAVE_IMAGINARY_I

#ifndef HAVE_CMPLXF
#ifdef HAVE_BUILTIN_COMPLEX
#define CMPLXF(x,y) __builtin_complex((float)(x), (float)(y))
#endif
#ifdef HAVE_IMAGINARY_I
#define CMPLXF(x,y) ((float complex)((float)(x) + _Imaginary_I * (float)(y)))
#endif
#endif /* HAVE_CMPLXF */
2 changes: 2 additions & 0 deletions src/conv_dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Author: Tom Tsou <tom.tsou@ettus.com>
*/

#include "config.h"

#include <stdlib.h>
#ifndef __APPLE__
#include <malloc.h>
Expand Down
2 changes: 2 additions & 0 deletions src/conv_sse.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Author: Tom Tsou <tom.tsou@ettus.com>
*/

#include "config.h"

#ifdef __clang__
#define __always_inline
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/defines.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "config.h"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
2 changes: 2 additions & 0 deletions src/firdecim_q15.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "config.h"

#include <assert.h>
#include <stdint.h>

Expand Down
2 changes: 2 additions & 0 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <assert.h>
#include <math.h>
#include <string.h>
Expand Down
6 changes: 4 additions & 2 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* IN THE SOFTWARE.
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
Expand All @@ -41,7 +43,7 @@ static const char *level_names[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};

#ifdef LOG_USE_COLOR
#ifdef USE_COLOR
static const char *level_colors[] = {
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
};
Expand Down Expand Up @@ -110,7 +112,7 @@ void log_log(int level, const char *file, int line, const char *fmt, ...) {
va_list args;
char buf[16];
buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0';
#ifdef LOG_USE_COLOR
#ifdef USE_COLOR
fprintf(
stderr, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
buf, level_colors[level], level_names[level], file, line);
Expand Down
6 changes: 4 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -228,7 +230,7 @@ int main(int argc, char *argv[])
}
else if (strcmp(format_name, "wav") == 0)
{
#ifdef HAVE_FAAD2
#ifdef USE_FAAD2
output_init_wav(&output, audio_name);
#else
log_fatal("WAV output requires FAAD2.");
Expand All @@ -251,7 +253,7 @@ int main(int argc, char *argv[])
}
else
{
#ifdef HAVE_FAAD2
#ifdef USE_FAAD2
output_init_live(&output);
#else
log_fatal("Live output requires FAAD2.");
Expand Down
1 change: 1 addition & 0 deletions src/math.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "config.h"
#include "defines.h"

#ifdef USE_FAST_MATH
Expand Down
18 changes: 10 additions & 8 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <assert.h>
#include <errno.h>
#include <string.h>
Expand All @@ -23,11 +25,11 @@
#include "defines.h"
#include "output.h"

#ifdef HAVE_ID3V2LIB
#ifdef USE_ID3V2LIB
#include <id3v2lib.h>
#endif

#ifdef HAVE_FAAD2
#ifdef USE_FAAD2
static ao_sample_format sample_format = {
16,
44100,
Expand Down Expand Up @@ -120,7 +122,7 @@ void output_push(output_t *st, uint8_t *pkt, unsigned int len)
return;
}

#ifdef HAVE_FAAD2
#ifdef USE_FAAD2
void *buffer;
NeAACDecFrameInfo info;

Expand Down Expand Up @@ -178,7 +180,7 @@ void output_push(output_t *st, uint8_t *pkt, unsigned int len)
#endif
}

#if defined(HAVE_FAAD2) && defined(USE_THREADS)
#if defined(USE_FAAD2) && defined(USE_THREADS)
static void *output_worker(void *arg)
{
output_t *st = arg;
Expand Down Expand Up @@ -215,7 +217,7 @@ void output_reset(output_t *st)
{
memset(st->ports, 0, sizeof(st->ports));

#ifdef HAVE_FAAD2
#ifdef USE_FAAD2
if (st->method == OUTPUT_ADTS || st->method == OUTPUT_HDC)
return;

Expand Down Expand Up @@ -255,7 +257,7 @@ void output_init_hdc(output_t *st, const char *name)
st->aas_files_path = NULL;
}

#ifdef HAVE_FAAD2
#ifdef USE_FAAD2
static void output_init_ao(output_t *st, int driver, const char *name)
{
unsigned int i;
Expand Down Expand Up @@ -310,7 +312,7 @@ void output_init_live(output_t *st)
}
#endif

#ifdef HAVE_ID3V2LIB
#ifdef USE_ID3V2LIB
static void display_text_content(const char *header, ID3v2_frame *frame)
{
if (frame == NULL)
Expand Down Expand Up @@ -556,7 +558,7 @@ void output_aas_push(output_t *st, uint8_t *buf, unsigned int len)
if (port == 0x5100 || (port >= 0x5201 && port <= 0x5207))
{
// PSD ports
#ifdef HAVE_ID3V2LIB
#ifdef USE_ID3V2LIB
output_id3(buf + 4, len - 4);
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions src/output.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "config.h"

#ifdef HAVE_FAAD2
#include <ao/ao.h>
#include <neaacdec.h>
Expand Down
2 changes: 2 additions & 0 deletions src/resamp_q15.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "config.h"

#include <assert.h>

#ifdef HAVE_NEON
Expand Down
32 changes: 32 additions & 0 deletions src/strndup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* A replacement function, for systems that lack strndup.
Copyright (C) 1996-1998, 2001-2003, 2005-2007, 2009-2017 Free Software
Foundation, Inc.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, 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, see <http://www.gnu.org/licenses/>. */

#include "config.h"

#include <string.h>
#include <stdlib.h>

#ifndef HAVE_STRNDUP
char *strndup (char const *s, size_t n)
{
size_t len = strnlen (s, n);
char *new = malloc (len + 1);

if (new == NULL)
return NULL;

new[len] = '\0';
return memcpy (new, s, len);
}
#endif
2 changes: 2 additions & 0 deletions src/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <math.h>

#include "defines.h"
Expand Down
2 changes: 2 additions & 0 deletions src/sync.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "config.h"

#include <complex.h>
#ifdef USE_THREADS
#include <pthread.h>
Expand Down

0 comments on commit 1378c99

Please sign in to comment.