Skip to content

Commit 92d3f89

Browse files
authored
Add floating-point configuration to CMake (#174)
Change-Id: I4a5ca53c4eb2041c21170b74a9c5d8378612df5e Signed-off-by: David Vincze <[email protected]> Signed-off-by: David Vincze <[email protected]>
1 parent 4e808ba commit 92d3f89

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

CMakeLists.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#-------------------------------------------------------------------------------
2+
# Copyright (c) 2022-2023, Arm Limited. All rights reserved.
3+
#
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
#
6+
# See BSD-3-Clause license in README.md
7+
#-------------------------------------------------------------------------------
8+
19
cmake_minimum_required(VERSION 3.15)
210

311
project(qcbor
@@ -14,6 +22,12 @@ set(BUILD_QCBOR_WARN OFF CACHE BOOL "Compile with the warning flags used i
1422
# IoT OS's don't support them at all.
1523
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries instead of static ones")
1624

25+
# Configuration:
26+
# Floating-point support (see README.md for more information)
27+
set(QCBOR_OPT_DISABLE_FLOAT_HW_USE OFF CACHE BOOL "Eliminate dependency on FP hardware and FP instructions")
28+
set(QCBOR_OPT_DISABLE_FLOAT_PREFERRED OFF CACHE BOOL "Eliminate support for half-precision and CBOR preferred serialization")
29+
set(QCBOR_OPT_DISABLE_FLOAT_ALL OFF CACHE BOOL "Eliminate floating-point support completely")
30+
1731
if (BUILD_QCBOR_WARN)
1832
# Compile options applying to all targets in current directory and below
1933
add_compile_options(-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wcast-qual)
@@ -37,13 +51,26 @@ target_include_directories(qcbor
3751
src
3852
)
3953

54+
target_compile_definitions(qcbor
55+
PRIVATE
56+
$<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_HW_USE}>:QCBOR_DISABLE_FLOAT_HW_USE>
57+
$<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_PREFERRED}>:QCBOR_DISABLE_PREFERRED_FLOAT>
58+
$<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_ALL}>:USEFULBUF_DISABLE_ALL_FLOAT>
59+
)
60+
4061
if (BUILD_SHARED_LIBS)
4162
target_compile_options(qcbor PRIVATE -Os -fPIC)
4263
endif()
4364

4465
# The math library is needed for floating-point support.
4566
# To avoid need for it #define QCBOR_DISABLE_FLOAT_HW_USE
46-
target_link_libraries(qcbor PRIVATE m)
67+
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
68+
# Using GCC
69+
target_link_libraries(qcbor
70+
PRIVATE
71+
$<$<NOT:$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_HW_USE}>>:m>
72+
)
73+
endif()
4774

4875
if (NOT BUILD_QCBOR_TEST STREQUAL "OFF")
4976
add_subdirectory(test)

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ used to reduce object code size and dependency.
280280

281281
See discussion in qcbor_encode.h for other details.
282282

283-
### #define QCBOR_DISABLE_FLOAT_HW_USE
283+
#### #define QCBOR_DISABLE_FLOAT_HW_USE
284284

285285
This removes dependency on:
286286

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

362+
#### CMake options
363+
364+
If you are using CMake, it can also be used to configure the floating-point
365+
support. These options can be enabled by adding them to the CMake configuration
366+
step and setting their value to 'ON' (True). The following table shows the
367+
available options and the associated #defines.
368+
369+
| CMake option | #define |
370+
|-----------------------------------|-------------------------------|
371+
| QCBOR_OPT_DISABLE_FLOAT_HW_USE | QCBOR_DISABLE_FLOAT_HW_USE |
372+
| QCBOR_OPT_DISABLE_FLOAT_PREFERRED | QCBOR_DISABLE_PREFERRED_FLOAT |
373+
| QCBOR_OPT_DISABLE_FLOAT_ALL | USEFULBUF_DISABLE_ALL_FLOAT |
374+
362375
## Code Size
363376

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

553566
Copyright (c) 2018-2021, Laurence Lundblade. All rights reserved.
554-
Copyright (c) 2021, Arm Limited. All rights reserved.
567+
Copyright (c) 2021-2023, Arm Limited. All rights reserved.

test/CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#-------------------------------------------------------------------------------
2+
# Copyright (c) 2022-2023, Arm Limited. All rights reserved.
3+
#
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
#
6+
# See BSD-3-Clause license in README.md
7+
#-------------------------------------------------------------------------------
8+
19
cmake_minimum_required(VERSION 3.15)
210

311
# Validate value of BUILD_QCBOR_TEST config option
@@ -24,12 +32,20 @@ target_include_directories(qcbor_test
2432
../inc
2533
)
2634

35+
target_compile_definitions(qcbor_test
36+
PUBLIC
37+
$<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_HW_USE}>:QCBOR_DISABLE_FLOAT_HW_USE>
38+
$<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_PREFERRED}>:QCBOR_DISABLE_PREFERRED_FLOAT>
39+
$<$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_ALL}>:USEFULBUF_DISABLE_ALL_FLOAT>
40+
)
41+
2742
target_link_libraries(qcbor_test
2843
PRIVATE
2944
qcbor
3045
# The math library is needed for floating-point support.
3146
# To avoid need for it #define QCBOR_DISABLE_FLOAT_HW_USE
32-
m
47+
# Using GCC
48+
$<$<AND:$<STREQUAL:${CMAKE_C_COMPILER_ID},"GNU">,$<NOT:$<BOOL:${QCBOR_OPT_DISABLE_FLOAT_HW_USE}>>>:m>
3349
)
3450

3551
if (BUILD_QCBOR_TEST STREQUAL "APP")

0 commit comments

Comments
 (0)