Skip to content

Commit f744a46

Browse files
Eyal Rozenbergeyalroz
Eyal Rozenberg
authored andcommitted
Fixes #18: Now using a printf_config.h file to configure the library's behavior.
1 parent 559efaf commit f744a46

File tree

3 files changed

+44
-62
lines changed

3 files changed

+44
-62
lines changed

CMakeLists.txt

+20-61
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,35 @@ project(
77
VERSION 0.2.0
88
)
99

10-
option(BUILD_TESTS "Build test programs for the library" OFF)
11-
12-
option(SUPPORT_FLOAT_SPECIFIERS "Support decimal notation floating-point conversion specifiers (%f,%F)" ON)
13-
if (NOT SUPPORT_FLOAT_SPECIFIERS)
14-
add_definitions(-DPRINTF_DISABLE_FLOAT_SPECIFIERS)
15-
endif()
16-
17-
option(SUPPORT_EXPONENTIAL_SPECIFIERS "Support exponential floating point format conversion specifiers (%e,%E,%g,%G)" ON)
18-
if (NOT SUPPORT_EXPONENTIAL_SPECIFIERS)
19-
add_definitions(-DPRINTF_DISABLE_EXPONENTIAL_SPECIFIERS)
20-
endif()
10+
option(BUILD_TESTS "Build test programs for the library" OFF)
11+
option(BUILD_STATIC_LIBRARY "Build the library as static rather than shared" OFF)
2112

22-
option(SUPPORT_LONG_LONG "Support long long integral types (allows for the ll length modifier and affects %p)" ON)
23-
if (NOT SUPPORT_LONG_LONG)
24-
add_definitions(-DPRINTF_DISABLE_LONG_LONG)
25-
endif()
13+
# Boolean options which go into config.h
2614

15+
option(SUPPORT_FLOAT_SPECIFIERS "Support decimal notation floating-point conversion specifiers (%f,%F)" ON)
16+
option(SUPPORT_EXPONENTIAL_SPECIFIERS "Support exponential floating point format conversion specifiers (%e,%E,%g,%G)" ON)
17+
option(SUPPORT_LONG_LONG "Support long long integral types (allows for the ll length modifier and affects %p)" ON)
2718
option(SUPPORT_PTRDIFF_LENGTH_MODIFIER "Support the pointer difference specifier (%t)" ON)
28-
if (NOT SUPPORT_PTRDIFF_LENGTH_MODIFIER)
29-
add_definitions(-DPRINTF_DISABLE_PTRDIFF_LENGTH_MODIFIER)
30-
endif()
31-
32-
option(ALIAS_STANDARD_FUNCTION_NAMES "Alias the standard library function names (printf, sprintf etc.) to the library's functions" ON)
33-
if (NOT ALIAS_STANDARD_FUNCTION_NAMES)
34-
add_definitions(-DPRINTF_ALIAS_STANDARD_FUNCTION_NAMES)
35-
endif()
36-
37-
# Numeric defines
38-
39-
set(NTOA_BUFFER_SIZE "32" CACHE STRING "Integer to string (ntoa) conversion buffer size")
40-
string(REGEX MATCH "^[0-9]+([eE][0-9]+)?$" NTOA_BUFFER_SIZE "${NTOA_BUFFER_SIZE}")
41-
if ("${NTOA_BUFFER_SIZE}" STREQUAL "")
42-
message(FATAL_ERROR "An (integral) buffer size for converting integers to floating-point values must be specified." )
43-
else()
44-
add_definitions(-DPRINTF_NTOA_BUFFER_SIZE=${NTOA_BUFFER_SIZE})
45-
endif()
46-
47-
set(PRINTF_FTOA_BUFFER_SIZE "32" CACHE STRING "Float to string (ftoa) conversion buffer size")
48-
string(REGEX MATCH "^[0-9]+$" PRINTF_FTOA_BUFFER_SIZE "${PRINTF_FTOA_BUFFER_SIZE}")
49-
if ("${PRINTF_FTOA_BUFFER_SIZE}" STREQUAL "")
50-
message(FATAL_ERROR "An (integral) buffer size for converting floating-point values to strings must be specified." )
51-
else()
52-
add_definitions(-DPRINTF_FTOA_BUFFER_SIZE=${PRINTF_FTOA_BUFFER_SIZE})
53-
endif()
19+
option(ALIAS_STANDARD_FUNCTION_NAMES "Alias the standard library function names (printf, sprintf etc.) to the library's functions" ON)
5420

55-
set(DEFAULT_FLOAT_PRECISION "6" CACHE STRING "Default precision when printing floating-point values")
56-
string(REGEX MATCH "^[0-9]+$" DEFAULT_FLOAT_PRECISION "${DEFAULT_FLOAT_PRECISION}")
57-
if ("${DEFAULT_FLOAT_PRECISION}" STREQUAL "")
58-
message(FATAL_ERROR "An (integral) default floating-point precision value must be specified." )
59-
else()
60-
add_definitions(-DPRINTF_DEFAULT_FLOAT_PRECISION=${DEFAULT_FLOAT_PRECISION})
61-
endif()
21+
# Numeric defines which go into config.h
6222

23+
set(NTOA_BUFFER_SIZE "32" CACHE STRING "Integer to string (ntoa) conversion buffer size")
24+
set(PRINTF_FTOA_BUFFER_SIZE "32" CACHE STRING "Float to string (ftoa) conversion buffer size")
25+
set(DEFAULT_FLOAT_PRECISION "6" CACHE STRING "Default precision when printing floating-point values")
6326
set(FLOAT_NOTATION_THRESHOLD "1e9" CACHE STRING "Largest floating-point value for which printing with %f uses non-exponential notation")
64-
string(REGEX MATCH "^[0-9]+([eE][0-9]+)?$" FLOAT_NOTATION_THRESHOLD "${FLOAT_NOTATION_THRESHOLD}")
65-
if ("${FLOAT_NOTATION_THRESHOLD}" STREQUAL "")
66-
message(FATAL_ERROR "The %f notation switch threshold must be an integer." )
67-
else()
68-
add_definitions(-DPRINTF_FLOAT_NOTATION_THRESHOLD=${FLOAT_NOTATION_THRESHOLD})
69-
endif()
70-
71-
option(BUILD_STATIC_LIBRARY "Build the library as static rather than shared" OFF)
72-
7327

7428
if (BUILD_STATIC_LIBRARY)
7529
add_library(mpaland-printf STATIC printf.c)
7630
else()
7731
add_library(mpaland-printf SHARED printf.c)
7832
endif()
7933

34+
set(GENERATED_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/include")
35+
configure_file("printf_config.h.in" "${GENERATED_INCLUDE_DIR}/printf_config.h" @ONLY)
36+
target_compile_definitions(mpaland-printf PRIVATE PRINTF_INCLUDE_CONFIG_H)
37+
target_include_directories(mpaland-printf PRIVATE "$<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>")
38+
8039
set_property(TARGET mpaland-printf PROPERTY C_STANDARD 99)
8140
set_property(TARGET mpaland-printf PROPERTY C_STANDARD_REQUIRED ON)
8241
set_property(TARGET mpaland-printf PROPERTY C_EXTENSIONS OFF)
@@ -95,7 +54,7 @@ set_target_properties(mpaland-printf PROPERTIES
9554

9655
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
9756
target_compile_options(mpaland-printf PRIVATE /W4)
98-
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
57+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
9958
CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
10059
target_compile_options(mpaland-printf PRIVATE -Wall -Wextra -pedantic)
10160
endif()
@@ -106,15 +65,15 @@ if (BUILD_TESTS)
10665
endif()
10766

10867
if (UNIX)
109-
add_custom_target(mpaland-printf-sizes
68+
add_custom_target(mpaland-printf-sizes
11069
COMMAND size -A -t $<TARGET_FILE:mpaland-printf> > mpaland-printf_sizes.txt
11170
DEPENDS mpaland-printf
11271
BYPRODUCTS mpaland-printf_sizes.txt
11372
COMMENT Prints the sizes of the different sections of the ELF file: text, dat, vss etc.)
11473

115-
add_custom_target(mpaland-printf-symbols
74+
add_custom_target(mpaland-printf-symbols
11675
COMMAND nm --numeric-sort --print-size "$<TARGET_FILE:mpaland-printf>" > mpaland-printf_symbols.txt
117-
COMMAND bash -c "nm --numeric-sort --print-size $<TARGET_FILE:mpaland-printf> | c++filt > mpaland-printf_cpp_symbols.txt"
76+
COMMAND bash -c "nm --numeric-sort --print-size $<TARGET_FILE:mpaland-printf> | c++filt > mpaland-printf_cpp_symbols.txt"
11877
VERBATIM
11978
DEPENDS mpaland-printf
12079
BYPRODUCTS mpaland-printf_symbols.txt mpaland-printf_cpp_symbols.txt

printf_config.h.in

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
#ifndef PRINTF_CONFIG_H_
3+
#define PRINTF_CONFIG_H_
4+
5+
#cmakedefine PRINTF_SUPPORT_FLOAT_SPECIFIERS @SUPPORT_FLOAT_SPECIFIERS@
6+
#cmakedefine PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS @SUPPORT_EXPONENTIAL_SPECIFIERS@
7+
#cmakedefine PRINTF_SUPPORT_LONG_LONG @SUPPORT_LONG_LONG@
8+
#cmakedefine PRINTF_SUPPORT_PTRDIFF_LENGTH_MODIFIER @SUPPORT_PTRDIFF_LENGTH_MODIFIER@
9+
#cmakedefine PRINTF_ALIAS_STANDARD_FUNCTION_NAMES @ALIAS_STANDARD_FUNCTION_NAMES@
10+
11+
#cmakedefine PRINTF_NTOA_BUFFER_SIZE @NTOA_BUFFER_SIZE@
12+
#cmakedefine PRINTF_FTOA_BUFFER_SIZE @PRINTF_FTOA_BUFFER_SIZE@
13+
#cmakedefine PRINTF_DEFAULT_FLOAT_PRECISION @DEFAULT_FLOAT_PRECISION@
14+
#cmakedefine PRINTF_FLOAT_NOTATION_THRESHOLD @FLOAT_NOTATION_THRESHOLD@
15+
16+
#endif // PRINTF_CONFIG_H_
17+

test/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ set_target_properties(
1212
CXX_EXTENSIONS NO
1313
)
1414

15+
# These two lines are necessary, since the test suite does not actually use the
16+
# compiled library - it includes the library's source .c file; and that means we
17+
# need to include the generated config.h file.
18+
target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H)
19+
target_include_directories(test_suite PRIVATE "${GENERATED_INCLUDE_DIR}")
20+
1521
option(TEST_BROKEN_FORMATS "Include tests using non-standard-compliant format strings?" ON)
1622
# ... don't worry, we'll suppress the compiler warnings for those.
1723
if (TEST_BROKEN_FORMATS)
@@ -22,7 +28,7 @@ target_link_libraries(test_suite PRIVATE mpaland-printf)
2228

2329
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
2430
target_compile_options(test_suite PRIVATE /W4)
25-
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
31+
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
2632
CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
2733
# lots of warnings and all warnings as errors
2834
target_compile_options(test_suite PRIVATE

0 commit comments

Comments
 (0)