Add floating-point configuration to CMake (#174)

Change-Id: I4a5ca53c4eb2041c21170b74a9c5d8378612df5e
Signed-off-by: David Vincze <david.vincze@arm.com>

Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0f34104..486946c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,3 +1,11 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022-2023, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# See BSD-3-Clause license in README.md
+#-------------------------------------------------------------------------------
+
 cmake_minimum_required(VERSION 3.15)
 
 project(qcbor
@@ -14,6 +22,12 @@
 #    IoT OS's don't support them at all.
 set(BUILD_SHARED_LIBS  OFF   CACHE BOOL   "Build shared libraries instead of static ones")
 
+# Configuration:
+#   Floating-point support (see README.md for more information)
+set(QCBOR_OPT_DISABLE_FLOAT_HW_USE     OFF  CACHE BOOL "Eliminate dependency on FP hardware and FP instructions")
+set(QCBOR_OPT_DISABLE_FLOAT_PREFERRED  OFF  CACHE BOOL "Eliminate support for half-precision and CBOR preferred serialization")
+set(QCBOR_OPT_DISABLE_FLOAT_ALL        OFF  CACHE BOOL "Eliminate floating-point support completely")
+
 if (BUILD_QCBOR_WARN)
     # Compile options applying to all targets in current directory and below
     add_compile_options(-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wcast-qual)
@@ -37,13 +51,26 @@
         src
 )
 
+target_compile_definitions(qcbor
+    PRIVATE
+        $<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_HW_USE}>:QCBOR_DISABLE_FLOAT_HW_USE>
+        $<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_PREFERRED}>:QCBOR_DISABLE_PREFERRED_FLOAT>
+        $<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_ALL}>:USEFULBUF_DISABLE_ALL_FLOAT>
+)
+
 if (BUILD_SHARED_LIBS)
     target_compile_options(qcbor PRIVATE -Os -fPIC)
 endif()
 
 # The math library is needed for floating-point support.
 # To avoid need for it #define QCBOR_DISABLE_FLOAT_HW_USE
-target_link_libraries(qcbor PRIVATE m)
+if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
+    # Using GCC
+    target_link_libraries(qcbor
+        PRIVATE
+            $<$<NOT:$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_HW_USE}>>:m>
+    )
+endif()
 
 if (NOT BUILD_QCBOR_TEST STREQUAL "OFF")
     add_subdirectory(test)