From 229ebd2ff2f312869944da6bf01c181f559dd493 Mon Sep 17 00:00:00 2001 From: Hoernchen Date: Mon, 2 Apr 2012 20:56:24 +0200 Subject: [PATCH] add win32 compatibility Signed-off-by: Steve Markgraf --- CMakeLists.txt | 7 +- cmake/Modules/FindThreads.cmake | 246 ++++++++++++++++++++++++++++++++ include/CMakeLists.txt | 1 + include/rtl-sdr.h | 29 ++-- include/rtl-sdr_export.h | 29 ++++ src/CMakeLists.txt | 12 ++ src/rtl-sdr.c | 2 + 7 files changed, 311 insertions(+), 15 deletions(-) create mode 100644 cmake/Modules/FindThreads.cmake create mode 100644 include/rtl-sdr_export.h diff --git a/CMakeLists.txt b/CMakeLists.txt index eb62cc1..f278325 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,17 +46,22 @@ endif() ######################################################################## find_package(PkgConfig) find_package(LibUSB) +set(THREADS_USE_PTHREADS_WIN32 true) +find_package(Threads) if(NOT LIBUSB_FOUND) message(FATAL_ERROR "LibUSB 1.0 required to compile rtl-sdr") endif() - +if(NOT THREADS_FOUND) + message(FATAL_ERROR "pthreads(-win32) required to compile osmosdrtlnw") +endif() ######################################################################## # Setup the include and linker paths ######################################################################## include_directories( ${CMAKE_SOURCE_DIR}/include ${LIBUSB_INCLUDE_DIR} + ${THREADS_PTHREADS_INCLUDE_DIR} ) #link_directories( diff --git a/cmake/Modules/FindThreads.cmake b/cmake/Modules/FindThreads.cmake new file mode 100644 index 0000000..8028b15 --- /dev/null +++ b/cmake/Modules/FindThreads.cmake @@ -0,0 +1,246 @@ +# Updated FindThreads.cmake that supports pthread-win32 +# Downloaded from http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=6399 + +# - This module determines the thread library of the system. +# +# The following variables are set +# CMAKE_THREAD_LIBS_INIT - the thread library +# CMAKE_USE_SPROC_INIT - are we using sproc? +# CMAKE_USE_WIN32_THREADS_INIT - using WIN32 threads? +# CMAKE_USE_PTHREADS_INIT - are we using pthreads +# CMAKE_HP_PTHREADS_INIT - are we using hp pthreads +# +# If use of pthreads-win32 is desired, the following variables +# can be set. +# +# THREADS_USE_PTHREADS_WIN32 - +# Setting this to true searches for the pthreads-win32 +# port (since CMake 2.8.0) +# +# THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME +# C = no exceptions (default) +# (NOTE: This is the default scheme on most POSIX thread +# implementations and what you should probably be using) +# CE = C++ Exception Handling +# SE = Structure Exception Handling (MSVC only) +# (NOTE: Changing this option from the default may affect +# the portability of your application. See pthreads-win32 +# documentation for more details.) +# +#====================================================== +# Example usage where threading library +# is provided by the system: +# +# find_package(Threads REQUIRED) +# add_executable(foo foo.cc) +# target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT}) +# +# Example usage if pthreads-win32 is desired on Windows +# or a system provided thread library: +# +# set(THREADS_USE_PTHREADS_WIN32 true) +# find_package(Threads REQUIRED) +# include_directories(${THREADS_PTHREADS_INCLUDE_DIR}) +# +# add_executable(foo foo.cc) +# target_link_libraries(foo ${CMAKE_THREAD_LIBS_INIT}) +# + +INCLUDE (CheckIncludeFiles) +INCLUDE (CheckLibraryExists) +SET(Threads_FOUND FALSE) + +IF(WIN32 AND NOT CYGWIN AND THREADS_USE_PTHREADS_WIN32) + SET(_Threads_ptwin32 true) +ENDIF() + +# Do we have sproc? +IF(CMAKE_SYSTEM MATCHES IRIX) + CHECK_INCLUDE_FILES("sys/types.h;sys/prctl.h" CMAKE_HAVE_SPROC_H) +ENDIF() + +IF(CMAKE_HAVE_SPROC_H) + # We have sproc + SET(CMAKE_USE_SPROC_INIT 1) + +ELSEIF(_Threads_ptwin32) + + IF(NOT DEFINED THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME) + # Assign the default scheme + SET(THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME "C") + ELSE() + # Validate the scheme specified by the user + IF(NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "C" AND + NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "CE" AND + NOT THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE") + MESSAGE(FATAL_ERROR "See documentation for FindPthreads.cmake, only C, CE, and SE modes are allowed") + ENDIF() + IF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE") + MESSAGE(FATAL_ERROR "Structured Exception Handling is only allowed for MSVC") + ENDIF(NOT MSVC AND THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME STREQUAL "SE") + ENDIF() + + FIND_PATH(THREADS_PTHREADS_INCLUDE_DIR pthread.h) + + # Determine the library filename + IF(MSVC) + SET(_Threads_pthreads_libname + pthreadV${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2) + ELSEIF(MINGW) + SET(_Threads_pthreads_libname + pthreadG${THREADS_PTHREADS_WIN32_EXCEPTION_SCHEME}2) + ELSE() + MESSAGE(FATAL_ERROR "This should never happen") + ENDIF() + + # Use the include path to help find the library if possible + SET(_Threads_lib_paths "") + IF(THREADS_PTHREADS_INCLUDE_DIR) + GET_FILENAME_COMPONENT(_Threads_root_dir + ${THREADS_PTHREADS_INCLUDE_DIR} PATH) + SET(_Threads_lib_paths ${_Threads_root_dir}/lib) + ENDIF() + FIND_LIBRARY(THREADS_PTHREADS_WIN32_LIBRARY + NAMES ${_Threads_pthreads_libname} + PATHS ${_Threads_lib_paths} + DOC "The Portable Threads Library for Win32" + NO_SYSTEM_PATH + ) + + IF(THREADS_PTHREADS_INCLUDE_DIR AND THREADS_PTHREADS_WIN32_LIBRARY) + MARK_AS_ADVANCED(THREADS_PTHREADS_INCLUDE_DIR) + SET(CMAKE_THREAD_LIBS_INIT ${THREADS_PTHREADS_WIN32_LIBRARY}) + SET(CMAKE_HAVE_THREADS_LIBRARY 1) + SET(Threads_FOUND TRUE) + ENDIF() + + MARK_AS_ADVANCED(THREADS_PTHREADS_WIN32_LIBRARY) + +ELSE() + # Do we have pthreads? + CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H) + IF(CMAKE_HAVE_PTHREAD_H) + + # + # We have pthread.h + # Let's check for the library now. + # + SET(CMAKE_HAVE_THREADS_LIBRARY) + IF(NOT THREADS_HAVE_PTHREAD_ARG) + + # Do we have -lpthreads + CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE) + IF(CMAKE_HAVE_PTHREADS_CREATE) + SET(CMAKE_THREAD_LIBS_INIT "-lpthreads") + SET(CMAKE_HAVE_THREADS_LIBRARY 1) + SET(Threads_FOUND TRUE) + ENDIF() + + # Ok, how about -lpthread + CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE) + IF(CMAKE_HAVE_PTHREAD_CREATE) + SET(CMAKE_THREAD_LIBS_INIT "-lpthread") + SET(Threads_FOUND TRUE) + SET(CMAKE_HAVE_THREADS_LIBRARY 1) + ENDIF() + + IF(CMAKE_SYSTEM MATCHES "SunOS.*") + # On sun also check for -lthread + CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE) + IF(CMAKE_HAVE_THR_CREATE) + SET(CMAKE_THREAD_LIBS_INIT "-lthread") + SET(CMAKE_HAVE_THREADS_LIBRARY 1) + SET(Threads_FOUND TRUE) + ENDIF() + ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*") + + ENDIF(NOT THREADS_HAVE_PTHREAD_ARG) + + IF(NOT CMAKE_HAVE_THREADS_LIBRARY) + # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread + IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG") + MESSAGE(STATUS "Check if compiler accepts -pthread") + TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG + ${CMAKE_BINARY_DIR} + ${CMAKE_ROOT}/Modules/CheckForPthreads.c + CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread + COMPILE_OUTPUT_VARIABLE OUTPUT) + + IF(THREADS_HAVE_PTHREAD_ARG) + IF(THREADS_PTHREAD_ARG MATCHES "^2$") + SET(Threads_FOUND TRUE) + MESSAGE(STATUS "Check if compiler accepts -pthread - yes") + ELSE() + MESSAGE(STATUS "Check if compiler accepts -pthread - no") + FILE(APPEND + ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n") + ENDIF() + ELSE() + MESSAGE(STATUS "Check if compiler accepts -pthread - no") + FILE(APPEND + ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n") + ENDIF() + + ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG") + + IF(THREADS_HAVE_PTHREAD_ARG) + SET(Threads_FOUND TRUE) + SET(CMAKE_THREAD_LIBS_INIT "-pthread") + ENDIF() + + ENDIF(NOT CMAKE_HAVE_THREADS_LIBRARY) + ENDIF(CMAKE_HAVE_PTHREAD_H) +ENDIF() + +IF(CMAKE_THREAD_LIBS_INIT) + SET(CMAKE_USE_PTHREADS_INIT 1) + SET(Threads_FOUND TRUE) +ENDIF() + +IF(CMAKE_SYSTEM MATCHES "Windows" + AND NOT THREADS_USE_PTHREADS_WIN32) + SET(CMAKE_USE_WIN32_THREADS_INIT 1) + SET(Threads_FOUND TRUE) +ENDIF() + +IF(CMAKE_USE_PTHREADS_INIT) + IF(CMAKE_SYSTEM MATCHES "HP-UX-*") + # Use libcma if it exists and can be used. It provides more + # symbols than the plain pthread library. CMA threads + # have actually been deprecated: + # http://docs.hp.com/en/B3920-90091/ch12s03.html#d0e11395 + # http://docs.hp.com/en/947/d8.html + # but we need to maintain compatibility here. + # The CMAKE_HP_PTHREADS setting actually indicates whether CMA threads + # are available. + CHECK_LIBRARY_EXISTS(cma pthread_attr_create "" CMAKE_HAVE_HP_CMA) + IF(CMAKE_HAVE_HP_CMA) + SET(CMAKE_THREAD_LIBS_INIT "-lcma") + SET(CMAKE_HP_PTHREADS_INIT 1) + SET(Threads_FOUND TRUE) + ENDIF(CMAKE_HAVE_HP_CMA) + SET(CMAKE_USE_PTHREADS_INIT 1) + ENDIF() + + IF(CMAKE_SYSTEM MATCHES "OSF1-V*") + SET(CMAKE_USE_PTHREADS_INIT 0) + SET(CMAKE_THREAD_LIBS_INIT ) + ENDIF() + + IF(CMAKE_SYSTEM MATCHES "CYGWIN_NT*") + SET(CMAKE_USE_PTHREADS_INIT 1) + SET(Threads_FOUND TRUE) + SET(CMAKE_THREAD_LIBS_INIT ) + SET(CMAKE_USE_WIN32_THREADS_INIT 0) + ENDIF() +ENDIF(CMAKE_USE_PTHREADS_INIT) + +INCLUDE(FindPackageHandleStandardArgs) +IF(_Threads_ptwin32) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG + THREADS_PTHREADS_WIN32_LIBRARY THREADS_PTHREADS_INCLUDE_DIR) +ELSE() + FIND_PACKAGE_HANDLE_STANDARD_ARGS(Threads DEFAULT_MSG Threads_FOUND) +ENDIF() diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 02d1a7c..991465a 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -22,5 +22,6 @@ ######################################################################## install(FILES rtl-sdr.h + rtl-sdr_export.h DESTINATION include ) diff --git a/include/rtl-sdr.h b/include/rtl-sdr.h index fb762ce..3eacd85 100644 --- a/include/rtl-sdr.h +++ b/include/rtl-sdr.h @@ -24,41 +24,42 @@ extern "C" { #endif #include +#include "rtl-sdr_export.h" typedef struct rtlsdr_dev rtlsdr_dev_t; -uint32_t rtlsdr_get_device_count(void); +uint32_t RTLSDR_API rtlsdr_get_device_count(void); -const char *rtlsdr_get_device_name(uint32_t index); +RTLSDR_API const char* rtlsdr_get_device_name(uint32_t index); -rtlsdr_dev_t *rtlsdr_open(uint32_t index); +RTLSDR_API rtlsdr_dev_t* rtlsdr_open(uint32_t index); -int rtlsdr_close(rtlsdr_dev_t *dev); +int RTLSDR_API rtlsdr_close(rtlsdr_dev_t *dev); /* configuration functions */ -int rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq); +int RTLSDR_API rtlsdr_set_center_freq(rtlsdr_dev_t *dev, uint32_t freq); -int rtlsdr_get_center_freq(rtlsdr_dev_t *dev); +int RTLSDR_API rtlsdr_get_center_freq(rtlsdr_dev_t *dev); -int rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm); +int RTLSDR_API rtlsdr_set_freq_correction(rtlsdr_dev_t *dev, int ppm); -int rtlsdr_get_freq_correction(rtlsdr_dev_t *dev); +int RTLSDR_API rtlsdr_get_freq_correction(rtlsdr_dev_t *dev); -int rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain); +int RTLSDR_API rtlsdr_set_tuner_gain(rtlsdr_dev_t *dev, int gain); -int rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev); +int RTLSDR_API rtlsdr_get_tuner_gain(rtlsdr_dev_t *dev); /* this will select the baseband filters according to the requested sample rate */ -int rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate); +int RTLSDR_API rtlsdr_set_sample_rate(rtlsdr_dev_t *dev, uint32_t rate); -int rtlsdr_get_sample_rate(rtlsdr_dev_t *dev); +int RTLSDR_API rtlsdr_get_sample_rate(rtlsdr_dev_t *dev); /* streaming functions */ -int rtlsdr_reset_buffer(rtlsdr_dev_t *dev); +int RTLSDR_API rtlsdr_reset_buffer(rtlsdr_dev_t *dev); -int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read); +int RTLSDR_API rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read); #ifdef __cplusplus } diff --git a/include/rtl-sdr_export.h b/include/rtl-sdr_export.h new file mode 100644 index 0000000..737e36b --- /dev/null +++ b/include/rtl-sdr_export.h @@ -0,0 +1,29 @@ +#ifndef RTLSDR_EXPORT_H +#define RTLSDR_EXPORT_H + +#if defined __GNUC__ +# if __GNUC__ >= 4 +# define __SDR_EXPORT __attribute__((visibility("default"))) +# define __SDR_IMPORT __attribute__((visibility("default"))) +# else +# define __SDR_EXPORT +# define __SDR_IMPORT +# endif +#elif _MSC_VER +# define __SDR_EXPORT __declspec(dllexport) +# define __SDR_IMPORT __declspec(dllimport) +#else +# define __SDR_EXPORT +# define __SDR_IMPORT +#endif + +#ifndef rtlsdr_STATIC +# ifdef rtlsdr_EXPORTS +# define RTLSDR_API __SDR_EXPORT +# else +# define RTLSDR_API __SDR_IMPORT +# endif +#else +#define RTLSDR_API +#endif +#endif /* RTLSDR_EXPORT_H */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c3fda1a..ac350e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,10 +30,22 @@ add_library(rtlsdr SHARED target_link_libraries(rtlsdr ${LIBUSB_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} ) set_target_properties(rtlsdr PROPERTIES DEFINE_SYMBOL "rtlsdr_EXPORTS") +add_library(rtlsdrs STATIC + rtl-sdr.c + tuner_e4000.c + tuner_fc0012.c + tuner_fc0013.c +) +target_link_libraries(rtlsdrs + ${LIBUSB_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} +) +set_target_properties(rtlsdrs PROPERTIES DEFINE_SYMBOL "rtlsdr_STATIC") ######################################################################## # Install built library files ######################################################################## diff --git a/src/rtl-sdr.c b/src/rtl-sdr.c index c79c12a..48b7df7 100644 --- a/src/rtl-sdr.c +++ b/src/rtl-sdr.c @@ -22,7 +22,9 @@ #include #include #include +#ifndef _WIN32 #include +#endif #include