diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..3d15df2e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,161 @@ +cmake_minimum_required (VERSION 3.1) +project( + mpaland-printf + LANGUAGES C + DESCRIPTION "Mostly- stand-alone C implementation of sprintf and printf functions" + HOMEPAGE_URL https://github.com/eyalroz/printf + VERSION 0.2.0 +) + +#macro(compiler_define_option option_identifier description defined_identifier default_state) +# option(option_identifier "${description}" ON) +# if (NOT SUPPORT_FLOAT_ARGUMENTS) +# add_definitions(-DPRINTF_DISABLE_SUPPORT_FLOAT) +# endif() +#endmacro() + +option(BUILD_TESTS "Build test programs for the library" OFF) + +option(SUPPORT_FLOAT_ARGUMENTS "Support floating-point arguments (%f)" ON) +if (NOT SUPPORT_FLOAT_ARGUMENTS) + add_definitions(-DPRINTF_DISABLE_SUPPORT_FLOAT) +endif() + +option(SUPPORT_EXPONENTIAL_FLOAT_ARGUMENTS "Support SUPPORT_EXPONENTIAL_FLOAT_ARGUMENTS (%e/%g)" ON) +if (NOT SUPPORT_EXPONENTIAL_FLOAT_ARGUMENTS) + add_definitions(-DPRINTF_DISABLE_SUPPORT_EXPONENTIAL) +endif() + +option(SUPPORT_LONG_LONG_AND_POINTER_ARGUMENTS "Support long long (%llu or %p)" ON) +if (NOT SUPPORT_LONG_LONG_AND_POINTER_ARGUMENTS) + add_definitions(-DPRINTF_DISABLE_SUPPORT_LONG_LONG) +endif() + +option(SUPPORT_PTRDIFF_ARGUMENTS "Support SUPPORT_PTRDIFF_ARGUMENTS (%t)" ON) +if (NOT SUPPORT_PTRDIFF_ARGUMENTS) + add_definitions(-DPRINTF_DISABLE_SUPPORT_PTRDIFF_T) +endif() + +# numeric defines + +set(INTEGER_TO_STRING_BUFFER_SIZE "" CACHE STRING "Integer to string (ntoa) conversion buffer size") +if (NOT "${INTEGER_TO_STRING_BUFFER_SIZE}" STREQUAL "") + add_definitions(-DPRINTF_NTOA_BUFFER_SIZE=${INTEGER_TO_STRING_BUFFER_SIZE}) +endif() + +set(FLOAT_TO_STRING_BUFFER_SIZE "" CACHE STRING "Float to string (ftoa) conversion buffer size") +if (NOT ${FLOAT_TO_STRING_BUFFER_SIZE} STREQUAL "") + add_definitions(-DPRINTF_FTOA_BUFFER_SIZE=${FLOAT_TO_STRING_BUFFER_SIZE}) +endif() + +set(DEFAULT_FLOAT_PRECISION "" CACHE STRING "Default precision when printing floating-point values") +if (NOT "${DEFAULT_FLOAT_PRECISION}" STREQUAL "") + add_definitions(-DPRINTF_DEFAULT_FLOAT_PRECISION=${DEFAULT_FLOAT_PRECISION}) +endif() + +set(LARGEST_FLOAT_TYPE "" CACHE STRING "Largest floating-point type suitable to print with %f") +if (NOT "${LARGEST_FLOAT_TYPE}" STREQUAL "") + add_definitions(-DPRINTF_MAX_FLOAT=${LARGEST_FLOAT_TYPE}) +endif() + +option(BUILD_STATIC_LIBRARY "Build the library as static rather than shared" OFF) + + +if (BUILD_STATIC_LIBRARY) + add_library(mpaland-printf SHARED printf.c) +else() + add_library(mpaland-printf STATIC printf.c) +endif() + +set_property(TARGET mpaland-printf PROPERTY C_STANDARD 99) +set_property(TARGET mpaland-printf PROPERTY C_STANDARD_REQUIRED ON) +set_property(TARGET mpaland-printf PROPERTY C_EXTENSIONS OFF) + +target_include_directories( + mpaland-printf + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + PUBLIC + "$" +) + +set_target_properties(mpaland-printf PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib + ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) + +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + target_compile_options(mpaland-printf PRIVATE /W4) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR + CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(mpaland-printf PRIVATE -Wall -Wextra -pedantic) +endif() + +if (BUILD_TESTS) + add_subdirectory(test) +endif() + +if (UNIX) + add_custom_target(mpaland-printf-sizes + COMMAND size -A -t $ > mpaland-printf_sizes.txt + DEPENDS mpaland-printf + BYPRODUCTS mpaland-printf_sizes.txt + COMMENT Prints the sizes of the different sections of the ELF file: text, dat, vss etc.) + + add_custom_target(mpaland-printf-symbols + COMMAND nm --numeric-sort --print-size "$" > mpaland-printf_symbols.txt + COMMAND bash -c "nm --numeric-sort --print-size $ | c++filt > mpaland-printf_cpp_symbols.txt" + VERBATIM + DEPENDS mpaland-printf + BYPRODUCTS mpaland-printf_symbols.txt mpaland-printf_cpp_symbols.txt + COMMENT Produces lists of the symbols, and C++demangled symbols, inside the library) + + add_custom_target(mpaland-printf-lst + COMMAND objdump --disassemble --line-numbers -S "$" > mpaland-printf.list + DEPENDS mpaland-printf + BYPRODUCTS mpaland-printf.lst + COMMENT Dissassembles the compiled library into an .lst file) + +endif() + +# ------------------------- +# Installation +# ------------------------- + +include(GNUInstallDirs) + +install( + TARGETS mpaland-printf + EXPORT mpaland-printf_export + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) + +install( + FILES "printf.h" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) + +install( + EXPORT mpaland-printf_export + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/mpaland-printf" + NAMESPACE "mpaland-printf::" + FILE "mpaland-printf-config.cmake" +) + +include(CMakePackageConfigHelpers) + +write_basic_package_version_file( + "mpaland-printf-config-version.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMinorVersion +) + +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/mpaland-printf-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/mpaland-printf" +) + + + diff --git a/Makefile b/Makefile deleted file mode 100644 index 7b28a1a1..00000000 --- a/Makefile +++ /dev/null @@ -1,271 +0,0 @@ -# ------------------------------------------------------------------------------ -# -# Generic Makefile -# -# Copyright Marco Paland 2007 - 2017 -# Distributed under the MIT License -# -# ------------------------------------------------------------------------------ - -# ------------------------------------------------------------------------------ -# Paths -# ------------------------------------------------------------------------------ -PATH_TOOLS_CC = /usr/bin/ -PATH_TOOLS_CC_LIB = /usr/lib/ -PATH_TOOLS_UTIL = - -PATH_BIN = bin -PATH_TMP = tmp -PATH_NUL = /dev/null -PATH_OBJ = $(PATH_TMP)/obj -PATH_LST = $(PATH_TMP)/lst -PATH_ERR = $(PATH_TMP)/err -PATH_PRE = $(PATH_TMP)/pre -PATH_COV = $(PATH_TMP)/cov - - -# ------------------------------------------------------------------------------ -# Application to build -# ------------------------------------------------------------------------------ - -APP = test_suite - - -# ----------------------------------------------------------------------------- -# Project file list -# Format is: -# FILES_PRJ = file1 \ -# foo/file2 \ -# bar/file3 -# ----------------------------------------------------------------------------- - -FILES_PRJ = test/test_suite - - -# ------------------------------------------------------------------------------ -# Additional include files and compiler defines -# Format is: -# C_INCLUDES = -Iinclude_path1 \ -# -Iinclude_path2 \ -# -Iinclude_path3 \ -# ------------------------------------------------------------------------------ - -C_INCLUDES = - -C_DEFINES = - - -# ------------------------------------------------------------------------------ -# The target name and location -# ------------------------------------------------------------------------------ -TRG = $(PATH_BIN)/$(APP) - - -# ------------------------------------------------------------------------------ -# object files -# ------------------------------------------------------------------------------ -FILES_TMP = $(FILES_PRJ) -FILES_O = $(addsuffix .o, $(FILES_TMP)) - - -# ------------------------------------------------------------------------------ -# VPATH definition -# -# VPATH is required for the maker to find the C-/ASM-Source files. -# Extract the directory/module names from the file list with the dir -# command and remove the duplicated directory names with the sort command. -# FILES_PRJ is listed first to make sure that the source files in the project -# directory are searched first. -# ------------------------------------------------------------------------------ -VPATH := $(sort $(dir $(FILES_TMP))) - - -# ------------------------------------------------------------------------------ -# Development tools -# ------------------------------------------------------------------------------ -AR = $(PATH_TOOLS_CC)ar -AS = $(PATH_TOOLS_CC)g++ -CC = $(PATH_TOOLS_CC)g++ -CL = $(PATH_TOOLS_CC)g++ -NM = $(PATH_TOOLS_CC)nm -GCOV = $(PATH_TOOLS_CC)gcov -OBJDUMP = $(PATH_TOOLS_CC)objdump -OBJCOPY = $(PATH_TOOLS_CC)objcopy -READELF = $(PATH_TOOLS_CC)readelf -SIZE = $(PATH_TOOLS_CC)size - -ECHO = $(PATH_TOOLS_UTIL)echo -MAKE = $(PATH_TOOLS_UTIL)make -MKDIR = $(PATH_TOOLS_UTIL)mkdir -RM = $(PATH_TOOLS_UTIL)rm -SED = $(PATH_TOOLS_UTIL)sed - - -# ------------------------------------------------------------------------------ -# Compiler flags for the target architecture -# ------------------------------------------------------------------------------ - -GCCFLAGS = $(C_INCLUDES) \ - $(C_DEFINES) \ - -std=c++11 \ - -g \ - -Wall \ - -pedantic \ - -Wmain \ - -Wundef \ - -Wsign-conversion \ - -Wuninitialized \ - -Wshadow \ - -Wunreachable-code \ - -Wswitch-default \ - -Wswitch \ - -Wcast-align \ - -Wmissing-include-dirs \ - -Winit-self \ - -Wdouble-promotion \ - -gdwarf-2 \ - -fno-exceptions \ - -O2 \ - -ffunction-sections \ - -ffat-lto-objects \ - -fdata-sections \ - -fverbose-asm \ - -Wextra \ - -Wunused-parameter \ - -Wfloat-equal - -CFLAGS = $(GCCFLAGS) \ - -Wunsuffixed-float-constants \ - -x c \ - -std=c99 - -CPPFLAGS = $(GCCFLAGS) \ - -x c++ \ - -fno-rtti \ - -fstrict-enums \ - -fno-use-cxa-atexit \ - -fno-use-cxa-get-exception-ptr \ - -fno-nonansi-builtins \ - -fno-threadsafe-statics \ - -fno-enforce-eh-specs \ - -ftemplate-depth-64 \ - -fexceptions - -AFLAGS = $(GCCFLAGS) \ - -x assembler - -LFLAGS = $(GCCFLAGS) \ - -x none \ - -Wl,--gc-sections - -# ------------------------------------------------------------------------------ -# Targets -# ------------------------------------------------------------------------------ - -# ------------------------------------------------------------------------------ -# Main-Dependencies (app: all) -# ------------------------------------------------------------------------------ -.PHONY: all -all: clean_prj $(TRG) $(TRG)_nm.txt - - -# ------------------------------------------------------------------------------ -# Main-Dependencies (app: rebuild) -# ------------------------------------------------------------------------------ -.PHONY: rebuild -rebuild: clean $(TRG) $(TRG)_nm.txt - - -# ------------------------------------------------------------------------------ -# clean project -# ------------------------------------------------------------------------------ -.PHONY: clean_prj -clean_prj: - @-$(ECHO) +++ cleaning project - @-$(RM) -rf $(PATH_BIN) 2> $(PATH_NUL) - @-$(MKDIR) -p $(PATH_BIN) - @-$(MKDIR) -p $(PATH_OBJ) - @-$(MKDIR) -p $(PATH_ERR) - @-$(MKDIR) -p $(PATH_LST) - @-$(MKDIR) -p $(PATH_PRE) - @-$(MKDIR) -p $(PATH_COV) - - -# ------------------------------------------------------------------------------ -# clean all -# ------------------------------------------------------------------------------ -.PHONY: clean -clean: - @-$(ECHO) +++ cleaning all - @-$(RM) -rf $(PATH_BIN) 2> $(PATH_NUL) - @-$(RM) -rf $(PATH_TMP) 2> $(PATH_NUL) - @-$(MKDIR) -p $(PATH_BIN) - @-$(MKDIR) -p $(PATH_OBJ) - @-$(MKDIR) -p $(PATH_ERR) - @-$(MKDIR) -p $(PATH_LST) - @-$(MKDIR) -p $(PATH_COV) - - -# ------------------------------------------------------------------------------ -# print the GNUmake version and the compiler version -# ------------------------------------------------------------------------------ -.PHONY: version -version: - # Print the GNU make version and the compiler version - @$(ECHO) GNUmake version: - @$(MAKE) --version - @$(ECHO) GCC version: - @$(CL) -v - - -# ------------------------------------------------------------------------------ -# Rules -# ------------------------------------------------------------------------------ - -# ------------------------------------------------------------------------------ -# Link/locate application -# ------------------------------------------------------------------------------ -$(TRG) : $(FILES_O) - @-$(ECHO) +++ linkink application to generate: $(TRG) - @-$(CL) $(LFLAGS) -L. -lc $(PATH_OBJ)/*.o -Wl,-Map,$(TRG).map -o $(TRG) - # profiling - @-$(CL) $(LFLAGS) -L. -lc $(PATH_COV)/*.o --coverage -o $(PATH_COV)/$(APP) - - -# ------------------------------------------------------------------------------ -# parse the object files to obtain symbol information, and create a size summary -# ------------------------------------------------------------------------------ -$(TRG)_nm.txt : $(TRG) - @-$(ECHO) +++ parsing symbols with nm to generate: $(TRG)_nm.txt - @-$(NM) --numeric-sort --print-size $(TRG) > $(TRG)_nm.txt - @-$(ECHO) +++ demangling symbols with c++filt to generate: $(TRG)_cppfilt.txt - @-$(NM) --numeric-sort --print-size $(TRG) | $(CPPFILT) > $(TRG)_cppfilt.txt - @-$(ECHO) +++ creating size summary table with size to generate: $(TRG)_size.txt - @-$(SIZE) -A -t $(TRG) > $(TRG)_size.txt - - -%.o : %.cpp - @$(ECHO) +++ compile: $< - # Compile the source file - # ...and Reformat (using sed) any possible error/warning messages for the VisualStudio(R) output window - # ...and Create an assembly listing using objdump - # ...and Generate a dependency file (using the -MM flag) - @-$(CL) $(CPPFLAGS) $< -E -o $(PATH_PRE)/$(basename $(@F)).pre - @-$(CL) $(CPPFLAGS) $< -c -o $(PATH_OBJ)/$(basename $(@F)).o 2> $(PATH_ERR)/$(basename $(@F)).err - @-$(SED) -e 's|.h:\([0-9]*\),|.h(\1) :|' -e 's|:\([0-9]*\):|(\1) :|' $(PATH_ERR)/$(basename $(@F)).err - @-$(OBJDUMP) --disassemble --line-numbers -S $(PATH_OBJ)/$(basename $(@F)).o > $(PATH_LST)/$(basename $(@F)).lst - @-$(CL) $(CPPFLAGS) $< -MM > $(PATH_OBJ)/$(basename $(@F)).d - # profiling - @-$(CL) $(CPPFLAGS) -O0 --coverage $< -c -o $(PATH_COV)/$(basename $(@F)).o 2> $(PATH_NUL) - -%.o : %.c - @$(ECHO) +++ compile: $< - # Compile the source file - # ...and Reformat (using sed) any possible error/warning messages for the VisualStudio(R) output window - # ...and Create an assembly listing using objdump - # ...and Generate a dependency file (using the -MM flag) - @-$(CL) $(CFLAGS) $< -E -o $(PATH_PRE)/$(basename $(@F)).pre - @-$(CC) $(CFLAGS) $< -c -o $(PATH_OBJ)/$(basename $(@F)).o 2> $(PATH_ERR)/$(basename $(@F)).err - @-$(SED) -e 's|.h:\([0-9]*\),|.h(\1) :|' -e 's|:\([0-9]*\):|(\1) :|' $(PATH_ERR)/$(basename $(@F)).err - @-$(OBJDUMP) -S $(PATH_OBJ)/$(basename $(@F)).o > $(PATH_LST)/$(basename $(@F)).lst - @-$(CC) $(CFLAGS) $< -MM > $(PATH_OBJ)/$(basename $(@F)).d diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..2720111b --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,65 @@ +cmake_minimum_required(VERSION 3.9) + + +################ +## Packages ## +################ + +# find_package(mplanad-printf REQUIRED) + +############# +## Tests ## +############# + +enable_language(CXX) + +add_executable(test_suite "test_suite.cpp") + +set_target_properties( + test_suite + PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +target_link_libraries(test_suite PRIVATE mpaland-printf) + +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + target_compile_options(test_suite PRIVATE /W4) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR + CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + # lots of warnings and all warnings as errors + target_compile_options(test_suite PRIVATE + -g + -Wall + -Wextra + -pedantic + -Wundef + -Wsign-conversion + -Wuninitialized + -Wshadow + -Wunreachable-code + -Wswitch-default + -Wswitch + -Wcast-align + -Wmissing-include-dirs + -Winit-self + -Wdouble-promotion + -gdwarf-2 + -fno-exceptions + -ffunction-sections + -ffat-lto-objects + -fdata-sections + -fverbose-asm + -Wunused-parameter + -Wfloat-equal + ) +endif() + +add_test( + NAME ${PROJECT_NAME}.test_suite + COMMAND test_suite # ${TEST_RUNNER_PARAMS} +) + +