From 6322c9343dc7f22b6ec848f3189aa3f08e1dc88a Mon Sep 17 00:00:00 2001
From: Dimitri Stolnikov <horiz0n@gmx.net>
Date: Sun, 1 Apr 2012 16:52:41 +0200
Subject: [PATCH] add preliminary cmake build system

---
 CMakeLists.txt                 | 85 ++++++++++++++++++++++++++++++++++
 cmake/Modules/FindLibUSB.cmake | 28 +++++++++++
 cmake/cmake_uninstall.cmake.in | 32 +++++++++++++
 include/CMakeLists.txt         | 26 +++++++++++
 src/CMakeLists.txt             | 49 ++++++++++++++++++++
 5 files changed, 220 insertions(+)
 create mode 100644 CMakeLists.txt
 create mode 100644 cmake/Modules/FindLibUSB.cmake
 create mode 100644 cmake/cmake_uninstall.cmake.in
 create mode 100644 include/CMakeLists.txt
 create mode 100644 src/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 00000000..eb62cc1c
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,85 @@
+# Copyright 2012 OSMOCOM Project
+#
+# This file is part of rtl-sdr
+#
+# GNU Radio 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 3, or (at your option)
+# any later version.
+#
+# GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+
+
+########################################################################
+# Project setup
+########################################################################
+cmake_minimum_required(VERSION 2.6)
+project(rtlsdr C)
+
+#select the release build type by default to get optimization flags
+if(NOT CMAKE_BUILD_TYPE)
+   set(CMAKE_BUILD_TYPE "Release")
+   message(STATUS "Build type not specified: defaulting to release.")
+endif(NOT CMAKE_BUILD_TYPE)
+set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "")
+
+list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
+
+########################################################################
+# Compiler specific setup
+########################################################################
+if(CMAKE_COMPILER_IS_GNUCXX AND NOT WIN32)
+    #http://gcc.gnu.org/wiki/Visibility
+    add_definitions(-fvisibility=hidden)
+endif()
+
+########################################################################
+# Find build dependencies
+########################################################################
+find_package(PkgConfig)
+find_package(LibUSB)
+
+if(NOT LIBUSB_FOUND)
+    message(FATAL_ERROR "LibUSB 1.0 required to compile rtl-sdr")
+endif()
+
+########################################################################
+# Setup the include and linker paths
+########################################################################
+include_directories(
+    ${CMAKE_SOURCE_DIR}/include
+    ${LIBUSB_INCLUDE_DIR}
+)
+
+#link_directories(
+#    ...
+#)
+
+# Set component parameters
+#set(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE INTERNAL "" FORCE)
+
+########################################################################
+# Create uninstall target
+########################################################################
+configure_file(
+    ${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
+    ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
+@ONLY)
+
+add_custom_target(uninstall
+    ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
+)
+
+########################################################################
+# Add subdirectories
+########################################################################
+add_subdirectory(include)
+add_subdirectory(src)
diff --git a/cmake/Modules/FindLibUSB.cmake b/cmake/Modules/FindLibUSB.cmake
new file mode 100644
index 00000000..97f3db67
--- /dev/null
+++ b/cmake/Modules/FindLibUSB.cmake
@@ -0,0 +1,28 @@
+if(NOT LIBUSB_FOUND)
+  pkg_check_modules (LIBUSB_PKG libusb-1.0)
+  find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h
+    PATHS
+    ${LIBUSB_PKG_INCLUDE_DIRS}
+    /usr/include/libusb-1.0
+    /usr/include
+    /usr/local/include
+  )
+
+  find_library(LIBUSB_LIBRARIES NAMES usb-1.0
+    PATHS
+    ${LIBUSB_PKG_LIBRARY_DIRS}
+    /usr/lib
+    /usr/local/lib
+  )
+
+if(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
+  set(LIBUSB_FOUND TRUE CACHE INTERNAL "libusb-1.0 found")
+  message(STATUS "Found libusb-1.0: ${LIBUSB_INCLUDE_DIR}, ${LIBUSB_LIBRARIES}")
+else(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
+  set(LIBUSB_FOUND FALSE CACHE INTERNAL "libusb-1.0 found")
+  message(STATUS "libusb-1.0 not found.")
+endif(LIBUSB_INCLUDE_DIR AND LIBUSB_LIBRARIES)
+
+mark_as_advanced(LIBUSB_INCLUDE_DIR LIBUSB_LIBRARIES)
+
+endif(NOT LIBUSB_FOUND)
diff --git a/cmake/cmake_uninstall.cmake.in b/cmake/cmake_uninstall.cmake.in
new file mode 100644
index 00000000..9ae1ae4b
--- /dev/null
+++ b/cmake/cmake_uninstall.cmake.in
@@ -0,0 +1,32 @@
+# http://www.vtk.org/Wiki/CMake_FAQ#Can_I_do_.22make_uninstall.22_with_CMake.3F
+
+IF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
+  MESSAGE(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"")
+ENDIF(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
+
+FILE(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
+STRING(REGEX REPLACE "\n" ";" files "${files}")
+FOREACH(file ${files})
+  MESSAGE(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
+  IF(EXISTS "$ENV{DESTDIR}${file}")
+    EXEC_PROGRAM(
+      "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
+      OUTPUT_VARIABLE rm_out
+      RETURN_VALUE rm_retval
+      )
+    IF(NOT "${rm_retval}" STREQUAL 0)
+      MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
+    ENDIF(NOT "${rm_retval}" STREQUAL 0)
+  ELSEIF(IS_SYMLINK "$ENV{DESTDIR}${file}")
+    EXEC_PROGRAM(
+      "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
+      OUTPUT_VARIABLE rm_out
+      RETURN_VALUE rm_retval
+      )
+    IF(NOT "${rm_retval}" STREQUAL 0)
+      MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
+    ENDIF(NOT "${rm_retval}" STREQUAL 0)
+  ELSE(EXISTS "$ENV{DESTDIR}${file}")
+    MESSAGE(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.")
+  ENDIF(EXISTS "$ENV{DESTDIR}${file}")
+ENDFOREACH(file)
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
new file mode 100644
index 00000000..02d1a7c7
--- /dev/null
+++ b/include/CMakeLists.txt
@@ -0,0 +1,26 @@
+# Copyright 2012 OSMOCOM Project
+#
+# This file is part of rtl-sdr
+#
+# GNU Radio 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 3, or (at your option)
+# any later version.
+#
+# GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+
+########################################################################
+# Install public header files
+########################################################################
+install(FILES
+    rtl-sdr.h
+    DESTINATION include
+)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 00000000..ebab2072
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,49 @@
+# Copyright 2012 OSMOCOM Project
+#
+# This file is part of rtl-sdr
+#
+# GNU Radio 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 3, or (at your option)
+# any later version.
+#
+# GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+
+########################################################################
+# Setup library
+########################################################################
+add_library(rtlsdr SHARED
+    rtl-sdr.c
+    tuner_e4000.c
+    tuner_fc0012.c
+    tuner_fc0013.c
+)
+
+target_link_libraries(rtlsdr
+    ${LIBUSB_LIBRARIES}
+)
+
+set_target_properties(rtlsdr PROPERTIES DEFINE_SYMBOL "rtlsdr_EXPORTS")
+
+########################################################################
+# Install built library files
+########################################################################
+install(TARGETS rtlsdr
+    LIBRARY DESTINATION lib${LIB_SUFFIX} # .so/.dylib file
+    ARCHIVE DESTINATION lib${LIB_SUFFIX} # .lib file
+    RUNTIME DESTINATION bin              # .dll file
+)
+
+########################################################################
+# Build utility
+########################################################################
+add_executable(rtl_sdr main.c)
+target_link_libraries(rtl_sdr rtlsdr ${LIBUSB_LIBRARIES})