Author: A. Maitland Bottoms <bottoms@debian.org>
Subject: modern cmake targets

Updating cmake_minimum_required to 2.8.12 allows exporting
targets. Also, target dependencies are transitive, so this patch
also simplifies the target_link_libraries for many executables.

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -5,10 +5,8 @@
 # Please report questions, comments, problems, or patches to the freetel
 # mailing list: https://lists.sourceforge.net/lists/listinfo/freetel-codec2
 #
+cmake_minimum_required(VERSION 2.8.12)
 project(codec2 C)
-
-cmake_minimum_required(VERSION 2.8)
-
 include(GNUInstallDirs)
 mark_as_advanced(CLEAR
     CMAKE_INSTALL_BINDIR
@@ -248,3 +246,11 @@
     DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
     COMPONENT "codec2_devel"
 )
+# ... and export CMake targets
+install(FILES
+  ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Codec2Config.cmake
+  #${CMAKE_CURRENT_BINARY_DIR}/cmake/Codec2ConfigVersion.cmake
+  DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/codec2
+  )
+install(EXPORT codec2-config FILE Codec2Targets.cmake
+  NAMESPACE Codec2:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/codec2 )
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -266,16 +266,16 @@
 )
 
 add_executable(c2demo c2demo.c)
-target_link_libraries(c2demo ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(c2demo codec2)
 
 add_executable(c2enc c2enc.c)
-target_link_libraries(c2enc ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(c2enc codec2)
 
 add_executable(c2dec c2dec.c)
-target_link_libraries(c2dec ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(c2dec codec2)
 
 add_executable(c2sim c2sim.c)
-target_link_libraries(c2sim ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(c2sim codec2)
 
 add_executable(fdmdv_get_test_bits fdmdv_get_test_bits.c fdmdv.c kiss_fft.c codec2_fft.c kiss_fftr.c)
 target_link_libraries(fdmdv_get_test_bits ${CMAKE_REQUIRED_LIBRARIES})
@@ -290,7 +290,7 @@
 target_link_libraries(fdmdv_put_test_bits ${CMAKE_REQUIRED_LIBRARIES})
 
 add_executable(fdmdv_channel fdmdv_channel.c)
-target_link_libraries(fdmdv_channel ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fdmdv_channel codec2)
 
 add_executable(fdmdv_interleave fdmdv_interleave.c)
 target_link_libraries(fdmdv_interleave ${CMAKE_REQUIRED_LIBRARIES})
@@ -299,85 +299,85 @@
 target_link_libraries(insert_errors ${CMAKE_REQUIRED_LIBRARIES})
 
 add_executable(fec_enc fec_enc.c golay23.c)
-target_link_libraries(fec_enc ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fec_enc codec2)
 
 add_executable(fec_dec fec_dec.c golay23.c)
-target_link_libraries(fec_dec ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fec_dec codec2)
 
 add_executable(freedv_tx freedv_tx.c)
-target_link_libraries(freedv_tx ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(freedv_tx codec2)
 
 add_executable(freedv_rx freedv_rx.c)
-target_link_libraries(freedv_rx ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(freedv_rx codec2)
 
 add_executable(fsk_mod fsk_mod.c)
-target_link_libraries(fsk_mod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fsk_mod codec2)
 
 add_executable(fsk_mod_ext_vco fsk_mod_ext_vco.c)
 target_link_libraries(fsk_mod_ext_vco ${CMAKE_REQUIRED_LIBRARIES})
 
 add_executable(fsk_demod fsk_demod.c modem_probe.c octave.c)
-target_link_libraries(fsk_demod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fsk_demod codec2)
 
 add_executable(fsk_get_test_bits fsk_get_test_bits.c)
 target_link_libraries(fsk_get_test_bits)
 
 add_executable(fsk_put_test_bits fsk_put_test_bits.c)
-target_link_libraries(fsk_put_test_bits ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fsk_put_test_bits codec2)
 
 add_executable(fm_demod fm_demod.c fm.c)
 target_link_libraries(fm_demod ${CMAKE_REQUIRED_LIBRARIES})
 
 add_executable(cohpsk_mod cohpsk_mod.c)
-target_link_libraries(cohpsk_mod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(cohpsk_mod codec2)
 
 add_executable(ofdm_get_test_bits ofdm_get_test_bits.c)
-target_link_libraries(ofdm_get_test_bits ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(ofdm_get_test_bits codec2)
 
 add_executable(ofdm_put_test_bits ofdm_put_test_bits.c)
-target_link_libraries(ofdm_put_test_bits ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(ofdm_put_test_bits codec2)
 
 add_executable(ofdm_mod ofdm_mod.c)
-target_link_libraries(ofdm_mod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(ofdm_mod codec2)
 
 add_executable(ofdm_demod ofdm_demod.c octave.c)
-target_link_libraries(ofdm_demod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(ofdm_demod codec2)
 
 add_executable(fmfsk_mod fmfsk_mod.c)
-target_link_libraries(fmfsk_mod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fmfsk_mod codec2)
 
 add_executable(fmfsk_demod fmfsk_demod.c modem_probe.c octave.c)
-target_link_libraries(fmfsk_demod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(fmfsk_demod codec2)
 
 add_executable(vhf_deframe_c2 vhf_deframe_c2.c)
-target_link_libraries(vhf_deframe_c2  ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(vhf_deframe_c2  codec2)
 
 add_executable(vhf_frame_c2 vhf_frame_c2.c)
-target_link_libraries(vhf_frame_c2  ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(vhf_frame_c2  codec2)
 
 add_executable(cohpsk_demod cohpsk_demod.c octave.c)
-target_link_libraries(cohpsk_demod ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(cohpsk_demod codec2)
 
 add_executable(cohpsk_get_test_bits cohpsk_get_test_bits.c)
-target_link_libraries(cohpsk_get_test_bits ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(cohpsk_get_test_bits codec2)
 
 add_executable(cohpsk_put_test_bits cohpsk_put_test_bits.c octave.c)
-target_link_libraries(cohpsk_put_test_bits ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(cohpsk_put_test_bits codec2)
 
 add_executable(cohpsk_ch cohpsk_ch.c)
-target_link_libraries(cohpsk_ch ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(cohpsk_ch codec2)
 
 add_executable(ldpc_enc ldpc_enc.c)
-target_link_libraries(ldpc_enc ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(ldpc_enc codec2)
 
 add_executable(ldpc_dec ldpc_dec.c)
-target_link_libraries(ldpc_dec ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(ldpc_dec codec2)
 
 add_executable(drs232 drs232.c)
 target_link_libraries(drs232 ${CMAKE_REQUIRED_LIBRARIES})
 
 add_executable(drs232_ldpc drs232_ldpc.c)
-target_link_libraries(drs232_ldpc ${CMAKE_REQUIRED_LIBRARIES} codec2)
+target_link_libraries(drs232_ldpc codec2)
 
 add_definitions(-DHORUS_L2_RX -DINTERLEAVER -DSCRAMBLER)
 add_executable(horus_demod horus_demod.c horus_api.c horus_l2.c fsk.c kiss_fft.c)
@@ -390,10 +390,6 @@
     PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/codec2
 )
 
-#install(EXPORT codec2-config
-#    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/codec2
-#)
-
 install(TARGETS
     c2demo
     c2demo
--- /dev/null
+++ b/cmake/Codec2Config.cmake
@@ -0,0 +1,8 @@
+get_filename_component(CODEC2_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
+
+if(NOT TARGET Codec2::codec2)
+  include("${CODEC2_CMAKE_DIR}/Codec2Targets.cmake")
+endif()
+
+# Backwards compatability
+set(Codec2_LIBRARIES Codec2::codec2)
