build: improve cmake scripts
diff --git a/.gitignore b/.gitignore
index 6583695..5fdd120 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,6 +17,7 @@
*.vcxproj.user
*.VC.db
*.VC.opendb
+/.vscode/
# Output of CMake
CMakeCache.txt
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fb5a071..86ce126 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,146 +1,223 @@
-#
-#
-# top level build file for cn-cbor
+cmake_minimum_required(VERSION 3.0.0)
-## prepare CMAKE
-cmake_minimum_required ( VERSION 3.0.0 )
-
-set ( VERSION_MAJOR 0 CACHE STRING "Project major version number")
-set ( VERSION_MINOR "1" CACHE STRING "Project minor version number" )
-set ( VERSION_PATCH "0" CACHE STRING "Project patch version number" )
-set ( CN_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}" )
+set(VERSION_MAJOR
+ 0
+ CACHE STRING "Project major version number")
+set(VERSION_MINOR
+ "1"
+ CACHE STRING "Project minor version number")
+set(VERSION_PATCH
+ "0"
+ CACHE STRING "Project patch version number")
+set(CN_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
mark_as_advanced(VERSION_MAJOR VERSION_MINOR VERSION_PATCH CN_VERSION)
-project ( "cn-cbor" VERSION "${CN_VERSION}")
+project("cn-cbor" VERSION "${CN_VERSION}")
-find_package(Doxygen)
+# ##############################################################################
+# OPTIONS
+# ##############################################################################
-## setup options
-option ( use_context "Use context pointer for CBOR functions" OFF )
-option ( verbose "Produce verbose makefile output" OFF )
-option ( optimize "Optimize for size" OFF )
-option ( fatal_warnings "Treat build warnings as errors" ON )
-option ( coveralls "Generate coveralls data" ON )
-option ( coveralls_send "Send data to coveralls site" OFF )
-option ( build_docs "Create docs using Doxygen" ${DOXYGEN_FOUND} )
-option ( build_shared_libs "Build Shared Libraries" ON)
-option ( no_floats "Build without floating point support" OFF )
+option(use_context "Use context pointer for CBOR functions" OFF)
+option(verbose "Produce verbose makefile output" OFF)
+option(optimize "Optimize for size" OFF)
+option(fatal_warnings "Treat build warnings as errors" ON)
+option(coveralls "Generate coveralls data" ON)
+option(coveralls_send "Send data to coveralls site" OFF)
+option(build_docs "Create docs using Doxygen" OFF)
+option(build_tests "Create tests" ON)
+option(no_floats "Build without floating point support" OFF)
+option(align_reads "Use memcpy in ntoh*p()" OFF)
-set ( dist_dir ${CMAKE_BINARY_DIR}/dist )
-set ( prefix ${CMAKE_INSTALL_PREFIX} )
-set ( exec_prefix ${CMAKE_INSTALL_PREFIX}/bin )
-set ( libdir ${CMAKE_INSTALL_PREFIX}/lib )
-set ( includedir ${CMAKE_INSTALL_PREFIX}/include )
+set(dist_dir ${CMAKE_BINARY_DIR}/dist)
+set(prefix ${CMAKE_INSTALL_PREFIX})
+set(exec_prefix ${CMAKE_INSTALL_PREFIX}/bin)
+set(libdir ${CMAKE_INSTALL_PREFIX}/lib)
+set(includedir ${CMAKE_INSTALL_PREFIX}/include)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cn-cbor.pc.in
${CMAKE_CURRENT_BINARY_DIR}/cn-cbor.pc @ONLY)
-install (FILES ${CMAKE_CURRENT_BINARY_DIR}/cn-cbor.pc DESTINATION lib/pkgconfig )
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cn-cbor.pc DESTINATION lib/pkgconfig)
-set ( package_prefix "${CMAKE_PACKAGE_NAME}-${CMAKE_SYSTEM_NAME}" )
+set(package_prefix "${CMAKE_PACKAGE_NAME}-${CMAKE_SYSTEM_NAME}")
-set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/bin )
-set ( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${dist_dir}/lib )
-set ( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${dist_dir}/lib )
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/bin)
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${dist_dir}/lib)
+set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${dist_dir}/lib)
-if (NOT CMAKE_BUILD_TYPE)
- if ( optimize )
- set ( CMAKE_BUILD_TYPE MinSizeRel )
- set ( coveralls OFF )
- set ( coveralls_send OFF )
- else ()
- set ( CMAKE_BUILD_TYPE Debug )
- endif ()
+if(NOT CMAKE_BUILD_TYPE)
+ if(optimize)
+ set(CMAKE_BUILD_TYPE MinSizeRel)
+ set(coveralls OFF)
+ set(coveralls_send OFF)
+ else()
+ set(CMAKE_BUILD_TYPE Debug)
+ endif()
endif()
-if ( MSVC )
- set (coveralls OFF)
-endif ()
-
-set (LIB_TYPE STATIC)
-if (build_shared_libs)
- set (LIB_TYPE SHARED)
-endif (build_shared_libs)
-
-message ( "Build type: ${CMAKE_BUILD_TYPE}" )
-
-if ( CMAKE_C_COMPILER_ID STREQUAL "GNU" OR
- CMAKE_C_COMPILER_ID MATCHES "Clang" )
- message ( STATUS "adding GCC/Clang options ")
- add_definitions ( -std=gnu99 -Wall -Wextra -pedantic )
- if ( fatal_warnings )
- add_definitions ( -Werror )
- endif ()
- if ( optimize )
- add_definitions ( -Os )
- endif ()
-elseif ( MSVC )
- add_definitions ( /W3 )
- if ( fatal_warnings )
- add_definitions ( /WX )
- endif ()
-else ()
- message ( FATAL_ERROR "unhandled compiler id: ${CMAKE_C_COMPILER_ID}" )
-endif ()
-
-if ( no_floats )
- add_definitions(-DCBOR_NO_FLOAT)
+if(MSVC)
+ set(coveralls OFF)
endif()
-if ( verbose )
- set ( CMAKE_VERBOSE_MAKEFILE ON )
-endif ()
+message("Build type: ${CMAKE_BUILD_TYPE}")
-## include the parts
-add_subdirectory ( include )
-add_subdirectory ( src )
-add_subdirectory ( test )
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
+ message(STATUS "adding GCC/Clang options ")
+ add_definitions(-std=gnu99 -Wall -Wextra -pedantic)
+ if(fatal_warnings)
+ add_definitions(-Werror)
+ endif()
+ if(optimize)
+ add_definitions(-Os)
+ endif()
+elseif(MSVC)
+ add_definitions(/W3)
+ if(fatal_warnings)
+ add_definitions(/WX)
+ endif()
+else()
+ message(FATAL_ERROR "unhandled compiler id: ${CMAKE_C_COMPILER_ID}")
+endif()
-install (FILES LICENSE README.md DESTINATION .)
+if(no_floats)
+ add_definitions(-DCBOR_NO_FLOAT)
+endif()
-## setup packaging
-set ( CPACK_GENERATOR "TGZ" )
-set ( CPACK_PACKAGE_VERSION "${PROJECT_VERSION}" )
-set ( CPACK_SOURCE_GENERATOR "TGZ" )
-set ( CPACK_SOURCE_IGNORE_FILES "/\\\\.git/" )
-file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/.gitignore igs)
-foreach (ig IN ITEMS ${igs})
- # remove comments
- string ( REGEX REPLACE "^\\s*#.*" "" ig "${ig}")
- # remove any other whitespace
- string ( STRIP "${ig}" ig)
- # anything left?
- if (ig)
- # dots are literal
- string ( REPLACE "." "\\\\." ig "${ig}" )
- # stars are on thars
- string ( REPLACE "*" ".*" ig "${ig}" )
- list ( APPEND CPACK_SOURCE_IGNORE_FILES "/${ig}/" )
- endif()
-endforeach()
+if(verbose)
+ set(CMAKE_VERBOSE_MAKEFILE ON)
+endif()
-set ( CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/README.md )
-set ( CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE" )
+# ##############################################################################
+# LIBRARY
+# ##############################################################################
-include ( CPack )
-include ( CTest )
+set(cbor_srcs src/cn-cbor.c src/cn-create.c src/cn-encoder.c src/cn-error.c
+ src/cn-get.c src/cn-print.c)
+
+if(align_reads)
+ add_definitions(-DCBOR_ALIGN_READS)
+endif()
+if(use_context)
+ add_definitions(-DUSE_CBOR_CONTEXT)
+endif()
+# default is static, can be shared if -DBUILD_SHARED_LIBS=ON passed to cmake
+add_library(${PROJECT_NAME})
+target_sources(
+ ${PROJECT_NAME}
+ PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/cn-cbor/cn-cbor.h
+ PRIVATE ${cbor_srcs})
+target_include_directories(
+ ${PROJECT_NAME}
+ PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
+ PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)
+if(MSVC)
+ message("test: ${BUILD_SHARED_LIBS}")
+ message("verbose: ${verbose}")
+ target_link_libraries(${PROJECT_NAME} ws2_32)
+endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
-include ( LCov )
+if(coveralls)
+ include(Coveralls)
+ coveralls_turn_on_coverage()
-if (build_docs)
- if(NOT DOXYGEN_FOUND)
- message(FATAL_ERROR "Doxygen is needed to build the documentation.")
- endif()
+ set(COVERAGE_SRCS "")
+ foreach(S ${cbor_srcs})
+ get_filename_component(S_ABS ${S} ABSOLUTE)
+ list(APPEND COVERAGE_SRCS ${S_ABS})
+ endforeach()
+ # Create the coveralls target.
+ coveralls_setup("${COVERAGE_SRCS}" ${coveralls_send} # If we should upload.
+ )
+
+ # add_dependencies(coveralls, all)
+endif()
+
+if(NOT MSVC)
+ add_custom_target(
+ size
+ COMMAND echo "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
+ COMMAND size "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
+ COMMAND size -m "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
+ DEPENDS cn-cbor
+ COMMENT "Output the size of the parse routine")
+endif(NOT MSVC)
+
+# ##############################################################################
+# DOCS
+# ##############################################################################
+
+if(build_docs)
+ find_package(Doxygen)
+
+ if(NOT DOXYGEN_FOUND)
+ message(WARNING "Doxygen is needed to build the documentation.")
+ else()
set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${doxyfile_in} ${doxyfile} @ONLY)
- add_custom_target(doc
- COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
- WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
- COMMENT "Generating API documentation with Doxygen"
- VERBATIM)
+ add_custom_target(
+ doc
+ COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ COMMENT "Generating API documentation with Doxygen"
+ VERBATIM)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION share/doc)
+ endif()
endif()
+
+# ##############################################################################
+# TESTS
+# ##############################################################################
+
+if(build_tests)
+ include(CTest)
+ add_subdirectory(test)
+ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
+ include(LCov)
+endif()
+
+# ##############################################################################
+# PACKAGING
+# ##############################################################################
+
+set(CPACK_GENERATOR "TGZ")
+set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
+set(CPACK_SOURCE_GENERATOR "TGZ")
+set(CPACK_SOURCE_IGNORE_FILES "/\\\\.git/")
+file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/.gitignore igs)
+foreach(ig IN ITEMS ${igs})
+ # remove comments
+ string(REGEX REPLACE "^\\s*#.*" "" ig "${ig}")
+ # remove any other whitespace
+ string(STRIP "${ig}" ig)
+ # anything left?
+ if(ig)
+ # dots are literal
+ string(REPLACE "." "\\\\." ig "${ig}")
+ # stars are on thars
+ string(REPLACE "*" ".*" ig "${ig}")
+ list(APPEND CPACK_SOURCE_IGNORE_FILES "/${ig}/")
+ endif()
+endforeach()
+
+set(CPACK_PACKAGE_DESCRIPTION_FILE ${CMAKE_CURRENT_SOURCE_DIR}/README.md)
+set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
+
+include(CPack)
+
+# ##############################################################################
+# INSTALL
+# ##############################################################################
+
+install(FILES LICENSE README.md DESTINATION .)
+install(DIRECTORY include DESTINATION include)
+
+install(
+ TARGETS cn-cbor
+ LIBRARY DESTINATION lib
+ ARCHIVE DESTINATION lib
+ RUNTIME DESTINATION bin)
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
deleted file mode 100644
index 898676a..0000000
--- a/include/CMakeLists.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-install ( DIRECTORY ../include DESTINATION .
- PATTERN CMakeLists.txt EXCLUDE )
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
deleted file mode 100644
index 2cef043..0000000
--- a/src/CMakeLists.txt
+++ /dev/null
@@ -1,61 +0,0 @@
-#
-#
-# compiling/installing sources for cn-cbor
-
-set ( cbor_srcs
- cn-cbor.c
- cn-create.c
- cn-encoder.c
- cn-error.c
- cn-get.c
- cn-print.c
-)
-
-if (use_context)
- add_definitions(-DUSE_CBOR_CONTEXT)
-endif()
-add_library ( cn-cbor ${LIB_TYPE} ${cbor_srcs} )
-message ( "MSVC: ${MSVC}")
-if ( MSVC )
- message ( "test: ${build_shared_libs}")
- message ( "verbose: ${verbose}")
-
- target_link_libraries(cn-cbor ws2_32)
-endif()
-
-target_include_directories ( cn-cbor PUBLIC ../include )
-target_include_directories ( cn-cbor PRIVATE ../src )
-
-install ( TARGETS cn-cbor
- LIBRARY DESTINATION lib
- ARCHIVE DESTINATION lib
- RUNTIME DESTINATION bin)
-
-set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
-if (coveralls)
- include(Coveralls)
- coveralls_turn_on_coverage()
-
- set(COVERAGE_SRCS "")
- foreach (S ${cbor_srcs})
- get_filename_component(S_ABS ${S} ABSOLUTE)
- list (APPEND COVERAGE_SRCS ${S_ABS})
- endforeach()
-
- # Create the coveralls target.
- coveralls_setup(
- "${COVERAGE_SRCS}"
- ${coveralls_send} # If we should upload.
- )
-
- #add_dependencies(coveralls, all)
-endif()
-
-if (NOT MSVC)
-add_custom_target(size
- COMMAND echo "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
- COMMAND size "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
- COMMAND size -m "${CMAKE_BINARY_DIR}/src/CMakeFiles/cn-cbor.dir/cn-cbor.c.o"
- DEPENDS cn-cbor
-COMMENT "Output the size of the parse routine")
-endif (NOT MSVC)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 3181a8d..18eaa47 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,33 +1,32 @@
#
-#
# Compiling/running tests
-if (use_context)
+if(use_context)
add_definitions(-DUSE_CBOR_CONTEXT)
endif()
-set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test )
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test)
-function (create_test name)
- add_executable ( ${name}_test ${name}_test.c )
- target_link_libraries ( ${name}_test PRIVATE cn-cbor )
- target_include_directories ( ${name}_test PRIVATE ../include )
- add_test ( NAME ${name} COMMAND ${name}_test )
+function(create_test name)
+ add_executable(${name}_test ${name}_test.c)
+ target_link_libraries(${name}_test PRIVATE cn-cbor)
+ target_include_directories(${name}_test PRIVATE ../include)
+ add_test(NAME ${name} COMMAND ${name}_test)
endfunction()
-create_test ( cbor )
-include ( CTest )
+create_test(cbor)
+include(CTest)
-if (APPLE)
+if(APPLE)
# difftest uses Apple-specific memory tests
- add_executable (cn-test test.c )
- target_include_directories ( cn-test PRIVATE ../include )
- target_link_libraries ( cn-test PRIVATE cn-cbor )
+ add_executable(cn-test test.c)
+ target_link_libraries(cn-test PRIVATE cn-cbor)
configure_file(cases.cbor cases.cbor COPYONLY)
configure_file(expected.out expected.out COPYONLY)
- add_custom_target(difftest
+ add_custom_target(
+ difftest
COMMAND env MallocStackLogging=true ./cn-test >new.out
COMMAND diff new.out expected.out
DEPENDS cn-test