Skip to content

Commit

Permalink
Add floating-point configuration to CMake (#174)
Browse files Browse the repository at this point in the history
Change-Id: I4a5ca53c4eb2041c21170b74a9c5d8378612df5e
Signed-off-by: David Vincze <[email protected]>

Signed-off-by: David Vincze <[email protected]>
  • Loading branch information
davidvincze authored Jan 5, 2023
1 parent 4e808ba commit 92d3f89
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
29 changes: 28 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,6 +22,12 @@ set(BUILD_QCBOR_WARN OFF CACHE BOOL "Compile with the warning flags used i
# 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)
Expand All @@ -37,13 +51,26 @@ target_include_directories(qcbor
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)
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ used to reduce object code size and dependency.

See discussion in qcbor_encode.h for other details.

### #define QCBOR_DISABLE_FLOAT_HW_USE
#### #define QCBOR_DISABLE_FLOAT_HW_USE

This removes dependency on:

Expand Down Expand Up @@ -359,6 +359,19 @@ In particular, `-mfloat-abi=soft`, disables use of
hardware instructions for the float and double
types in C for some architectures.

#### CMake options

If you are using CMake, it can also be used to configure the floating-point
support. These options can be enabled by adding them to the CMake configuration
step and setting their value to 'ON' (True). The following table shows the
available options and the associated #defines.

| CMake option | #define |
|-----------------------------------|-------------------------------|
| QCBOR_OPT_DISABLE_FLOAT_HW_USE | QCBOR_DISABLE_FLOAT_HW_USE |
| QCBOR_OPT_DISABLE_FLOAT_PREFERRED | QCBOR_DISABLE_PREFERRED_FLOAT |
| QCBOR_OPT_DISABLE_FLOAT_ALL | USEFULBUF_DISABLE_ALL_FLOAT |

## Code Size

These are approximate sizes on a 64-bit x86 CPU with the -Os optimization.
Expand Down Expand Up @@ -551,4 +564,4 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
### Copyright for this README

Copyright (c) 2018-2021, Laurence Lundblade. All rights reserved.
Copyright (c) 2021, Arm Limited. All rights reserved.
Copyright (c) 2021-2023, Arm Limited. All rights reserved.
18 changes: 17 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

# Validate value of BUILD_QCBOR_TEST config option
Expand All @@ -24,12 +32,20 @@ target_include_directories(qcbor_test
../inc
)

target_compile_definitions(qcbor_test
PUBLIC
$<$<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>
)

target_link_libraries(qcbor_test
PRIVATE
qcbor
# The math library is needed for floating-point support.
# To avoid need for it #define QCBOR_DISABLE_FLOAT_HW_USE
m
# Using GCC
$<$<AND:$<STREQUAL:${CMAKE_C_COMPILER_ID},"GNU">,$<NOT:$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_HW_USE}>>>:m>
)

if (BUILD_QCBOR_TEST STREQUAL "APP")
Expand Down

0 comments on commit 92d3f89

Please sign in to comment.