diff --git a/.circleci/config.yml b/.circleci/config.yml index 6b36c4a2..0201fed5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -8,7 +8,7 @@ aliases: cd build eval cmake .. ${CMAKE_FLAGS} make -j 4 - + - &run_units name: run_units environment: diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bc08a44..2d0fa7f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,11 +84,8 @@ set(UNITS_NAMESPACE ) cmake_dependent_option( - UNITS_INSTALL - "Generate and install cmake package files" - ON - "CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME;NOT UNITS_BINARY_ONLY_INSTALL" - OFF + UNITS_INSTALL "Generate and install cmake package files" ON + "CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME;NOT UNITS_BINARY_ONLY_INSTALL" OFF ) mark_as_advanced(UNITS_INSTALL) @@ -110,7 +107,6 @@ set(UNITS_CLANG_TIDY_OPTIONS mark_as_advanced(UNITS_CLANG_TIDY_OPTIONS) mark_as_advanced(UNITS_CLANG_TIDY) - set(UNITS_LIBRARY_EXPORT_COMMAND EXPORT unitsTargets) mark_as_advanced(UNITS_LIBRARY_EXPORT_COMMAND) @@ -155,12 +151,12 @@ if(NOT UNITS_HEADER_ONLY) OFF "NOT CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME" OFF ) else() - option(UNITS_BUILD_STATIC_LIBRARY - "enable Construction of the units static library" OFF - ) - option(UNITS_BUILD_SHARED_LIBRARY - "enable Construction of the units shared library" OFF - ) + option(UNITS_BUILD_STATIC_LIBRARY "enable Construction of the units static library" + OFF + ) + option(UNITS_BUILD_SHARED_LIBRARY "enable Construction of the units shared library" + OFF + ) endif(NOT UNITS_HEADER_ONLY) # Prepare Clang-Tidy @@ -194,14 +190,29 @@ if(UNITS_INSTALL) install(TARGETS compile_flags_target ${UNITS_LIBRARY_EXPORT_COMMAND}) endif() if(NOT UNITS_BINARY_ONLY_INSTALL) - configure_file(config/unitsConfig.cmake.in - "${PROJECT_BINARY_DIR}/unitsConfig.cmake" @ONLY) - - export(EXPORT unitsTargets NAMESPACE units:: FILE ${PROJECT_BINARY_DIR}/unitsTargets.cmake) - - install(EXPORT unitsTargets NAMESPACE units:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units) - - write_basic_package_version_file(${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake COMPATIBILITY AnyNewerVersion) - install(FILES ${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake ${PROJECT_BINARY_DIR}/unitsConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units) + configure_file( + config/unitsConfig.cmake.in "${PROJECT_BINARY_DIR}/unitsConfig.cmake" @ONLY + ) + + export( + EXPORT unitsTargets + NAMESPACE units:: + FILE ${PROJECT_BINARY_DIR}/unitsTargets.cmake + ) + + install( + EXPORT unitsTargets + NAMESPACE units:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units + ) + + write_basic_package_version_file( + ${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake + COMPATIBILITY AnyNewerVersion + ) + install(FILES ${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake + ${PROJECT_BINARY_DIR}/unitsConfig.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units + ) endif() -endif() \ No newline at end of file +endif() diff --git a/README.md b/README.md index 6b4c88e9..42e3b440 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ if (!meas.units().is_convertible(out) ## Limitations -- The powers represented by units by default are limited see [Unit representation](#unit_representation) and only normal physical units or common operations are supported, this can be modified at compile time to support a much broader range at the expense of size and computation. +- The powers represented by units by default are limited see [Unit representation](#unit_representation) and only normal physical units or common operations are supported, this can be modified at compile time to support a much broader range at the expense of size and computation. - The library uses floating point and double precision for the multipliers which is generally good enough for most engineering contexts, but does come with the limits and associated loss of precision for long series of calculations on floating point numbers. - Currency is supported as a unit but it is not recommended to use this for anything beyond basic financial calculations. So if you are doing a lot of financial calculations or accounting use something more specific for currency manipulations. - Fractional unit powers are not supported in general. While some mathematical operations on units are supported any root operations `sqrt` or `cbrt` will only produce valid results if the result is integral powers of the base units. One exception is limited support for √Hz operations in measurements of Amplitude spectral density. A specific definition of a unit representing square root of Hz is available and will work in combination with other units. @@ -143,7 +143,7 @@ The seven [SI units](https://www.nist.gov/pml/weights-and-measures/metric-si/si- These ranges were chosen to represent nearly all physical quantities that could be found in various disciplines we have encountered. -The CMake variable `UNITS_BASE_TYPE` if set to a 64 bit type like `uint64_t` will double the space requirements but also change the ranges to be at least a power of 4 larger than the above table. See [Cmake Reference](https://units.readthedocs.io/en/latest/installation/cmake_variables.html) for more details. +The CMake variable `UNITS_BASE_TYPE` if set to a 64 bit type like `uint64_t` will double the space requirements but also change the ranges to be at least a power of 4 larger than the above table. See [Cmake Reference](https://units.readthedocs.io/en/latest/installation/cmake_variables.html) for more details. ### Discussion points @@ -172,7 +172,7 @@ There are two parts of the library a header only portion that can simply be copi The second part is a few cpp files that can add some additional functionality. The primary additions from the cpp file are an ability to take roots of units and measurements and convert to and from strings. These files can be built as a standalone static library or included in the source code of whatever project want to use them. The code should build with an C++11 compiler. Most of the library is tagged with constexpr so can be run at compile time to link units that are known at compile time. Unit numerical conversions are not at compile time, so will have a run-time cost. A `quick_convert` function is available to do simple conversions. with a requirement that the units have the same base and not be an equation unit. The cpp code also includes some functions for commodities and will eventually have r20 and x12 conversions, though this is not complete yet. -It builds by default with the static library. Using `UNIT_BUILD_SHARED_LIBRARY` or `BUILD_SHARED_LIBS` will build the shared library instead. Either one can be used with CMake as units::units. The header only library target is also generated `units::header_only`. The shared/static library has a CMake target `units::units`. +It builds by default with the static library. Using `UNIT_BUILD_SHARED_LIBRARY` or `BUILD_SHARED_LIBS` will build the shared library instead. Either one can be used with CMake as units::units. The header only library target is also generated `units::header_only`. The shared/static library has a CMake target `units::units`. ## Try it out diff --git a/converter/CMakeLists.txt b/converter/CMakeLists.txt index 7ed99e55..d7908eb3 100644 --- a/converter/CMakeLists.txt +++ b/converter/CMakeLists.txt @@ -24,11 +24,13 @@ if(UNITS_BUILD_CONVERTER_APP) target_link_libraries(units_convert PRIVATE -lstdc++fs) endif() endif() - - if (UNITS_INSTALL) - install(TARGETS units_convert - ${UNITS_LIBRARY_EXPORT_COMMAND} - DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT units_convert) - endif() - add_executable(units::units_convert ALIAS units_convert) + + if(UNITS_INSTALL) + install( + TARGETS units_convert ${UNITS_LIBRARY_EXPORT_COMMAND} + DESTINATION "${CMAKE_INSTALL_BINDIR}" + COMPONENT units_convert + ) + endif() + add_executable(units::units_convert ALIAS units_convert) endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1a470229..b212589f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -132,62 +132,66 @@ else() ) endif() - if(UNITS_INSTALL_PACKAGE_TESTS) - set(UNITS_PACKAGE_SEARCH_LOC) - + set(UNITS_PACKAGE_SEARCH_LOC) + + if(NOT MSVC) + set(package_test_command --test-command "${CMAKE_CTEST_COMMAND}") + else() # don't try to run the tests on MSVC since that would require copying the + # dll's and doing some other setup that isn't that important to run on + # all OS + set(package_test_command) + endif() -if(NOT MSVC) - set(package_test_command --test-command "${CMAKE_CTEST_COMMAND}") -else() # don't try to run the tests on MSVC since that would require copying the dll's and doing - # some other setup that isn't that important to run on all OS - set(package_test_command) -endif() + if(CMAKE_BUILD_TYPE) + set(UNITS_PACKAGE_TEST_BUILD_TYPE ${CMAKE_BUILD_TYPE}) + else() + set(UNITS_PACKAGE_TEST_BUILD_TYPE Release) + endif() -if(CMAKE_BUILD_TYPE) - set(UNITS_PACKAGE_TEST_BUILD_TYPE ${CMAKE_BUILD_TYPE}) -else() - set(UNITS_PACKAGE_TEST_BUILD_TYPE Release) -endif() - -file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests") - -if(MSVC AND ${CMAKE_VERSION} VERSION_GREATER 3.12.9) - # Tests for other CMake projects including and using UNITS using find_package - add_test( - NAME find-package-testsA - COMMAND - ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" -A "${CMAKE_GENERATOR_PLATFORM}" - ${UNITS_PACKAGE_SEARCH_LOC} - "-DUNITS_BUILD_SHARED_LIBRARY=${UNITS_BUILD_SHARED_LIBRARY}" - "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" - "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" - "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" - "${CMAKE_CURRENT_SOURCE_DIR}/find_package_tests" - WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" - ) -else() - add_test( - NAME find-package-testsA - COMMAND - ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${UNITS_PACKAGE_SEARCH_LOC} - "-DUNITS_BUILD_SHARED_LIBRARY=${UNITS_BUILD_SHARED_LIBRARY}" - "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" - "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" - "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" - "${CMAKE_CURRENT_SOURCE_DIR}/find_package_tests" - WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" - ) -endif() + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests") + + if(MSVC AND ${CMAKE_VERSION} VERSION_GREATER 3.12.9) + # Tests for other CMake projects including and using UNITS using + # find_package + add_test( + NAME find-package-testsA + COMMAND + ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" -A + "${CMAKE_GENERATOR_PLATFORM}" ${UNITS_PACKAGE_SEARCH_LOC} + "-DUNITS_BUILD_SHARED_LIBRARY=${UNITS_BUILD_SHARED_LIBRARY}" + "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" + "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" + "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" + "${CMAKE_CURRENT_SOURCE_DIR}/find_package_tests" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" + ) + else() + add_test( + NAME find-package-testsA + COMMAND + ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${UNITS_PACKAGE_SEARCH_LOC} + "-DUNITS_BUILD_SHARED_LIBRARY=${UNITS_BUILD_SHARED_LIBRARY}" + "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" + "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" + "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" + "${CMAKE_CURRENT_SOURCE_DIR}/find_package_tests" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" + ) + endif() -add_test(NAME find-package-testsB - COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" --config - ${UNITS_PACKAGE_TEST_BUILD_TYPE} -) + add_test( + NAME find-package-testsB + COMMAND + ${CMAKE_COMMAND} --build + "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" --config + ${UNITS_PACKAGE_TEST_BUILD_TYPE} + ) -add_test(NAME find-package-testsC COMMAND ${CMAKE_CTEST_COMMAND} -C - ${UNITS_PACKAGE_TEST_BUILD_TYPE} - WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" -) - endif() #UNITS_INSTALL_PACKAGE_TESTS + add_test( + NAME find-package-testsC + COMMAND ${CMAKE_CTEST_COMMAND} -C ${UNITS_PACKAGE_TEST_BUILD_TYPE} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" + ) + endif() # UNITS_INSTALL_PACKAGE_TESTS endif() # UNITS_HEADER_ONLY diff --git a/test/find_package_tests/CMakeLists.txt b/test/find_package_tests/CMakeLists.txt index 8a5db0b9..72bf9cb3 100644 --- a/test/find_package_tests/CMakeLists.txt +++ b/test/find_package_tests/CMakeLists.txt @@ -25,18 +25,12 @@ if(UNITS_BUILD_SHARED_LIBRARY) target_link_libraries(shared-library-test-exe units::units) add_test(NAME shared-library-test COMMAND shared-library-test-exe) - set_property( - TEST shared-library-test PROPERTY PASS_REGULAR_EXPRESSION "10.7" - ) + set_property(TEST shared-library-test PROPERTY PASS_REGULAR_EXPRESSION "10.7") - endif() # Test the header only targets - add_executable(header_only-exe ../pkg_test_code/header_only.cpp) - target_link_libraries(header_only-exe units::header_only) - - add_test(NAME header_only-exe COMMAND header_only-exe) - set_property( - TEST header_only-exe PROPERTY PASS_REGULAR_EXPRESSION "PASS" - ) +add_executable(header_only-exe ../pkg_test_code/header_only.cpp) +target_link_libraries(header_only-exe units::header_only) +add_test(NAME header_only-exe COMMAND header_only-exe) +set_property(TEST header_only-exe PROPERTY PASS_REGULAR_EXPRESSION "PASS") diff --git a/units/CMakeLists.txt b/units/CMakeLists.txt index fa8948dd..adb313b1 100644 --- a/units/CMakeLists.txt +++ b/units/CMakeLists.txt @@ -10,7 +10,7 @@ set(units_header_files units.hpp units_decl.hpp unit_definitions.hpp units_util. include(GenerateExportHeader) -if(UNITS_BUILD_SHARED_LIBRARY ) +if(UNITS_BUILD_SHARED_LIBRARY) add_library(units SHARED ${units_source_files} ${units_header_files}) generate_export_header(units BASE_NAME units) target_compile_definitions(units PUBLIC UNITS_EXPORT_HEADER) @@ -38,7 +38,7 @@ if(UNITS_BUILD_SHARED_LIBRARY ) ) endif() add_library(units::units ALIAS units) - + elseif(UNITS_BUILD_OBJECT_LIBRARY) add_library(units OBJECT ${units_source_files} ${units_header_files}) target_include_directories(units PRIVATE $) @@ -52,7 +52,7 @@ elseif(UNITS_BUILD_OBJECT_LIBRARY) ) endif() endif() -elseif (UNITS_BUILD_STATIC_LIBRARY) +elseif(UNITS_BUILD_STATIC_LIBRARY) add_library(units STATIC ${units_source_files} ${units_header_files}) target_include_directories( units @@ -75,7 +75,8 @@ elseif (UNITS_BUILD_STATIC_LIBRARY) endif() if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL) install(TARGETS units ${UNITS_LIBRARY_EXPORT_COMMAND} - DESTINATION ${CMAKE_INSTALL_LIBDIR} # INCLUDES DESTINATION include/${PROJECT_NAME} + DESTINATION ${CMAKE_INSTALL_LIBDIR} # INCLUDES DESTINATION + # include/${PROJECT_NAME} ) endif() if(CMAKE_BUILD_TYPE STREQUAL Coverage) @@ -87,27 +88,27 @@ elseif (UNITS_BUILD_STATIC_LIBRARY) add_library(units::units ALIAS units) endif() -## add the header only interface library +# add the header only interface library add_library(header_only INTERFACE) - target_include_directories( - header_only INTERFACE $ - $ +target_include_directories( + header_only INTERFACE $ + $ +) +target_compile_definitions(header_only INTERFACE UNITS_HEADER_ONLY) +if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL) + install(TARGETS header_only ${UNITS_LIBRARY_EXPORT_COMMAND}) +endif() +if(UNITS_NAMESPACE) + target_compile_definitions( + header_only INTERFACE -DUNITS_NAMESPACE=${UNITS_NAMESPACE} ) - target_compile_definitions(header_only INTERFACE UNITS_HEADER_ONLY) - if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL) - install(TARGETS header_only ${UNITS_LIBRARY_EXPORT_COMMAND}) - endif() - if(UNITS_NAMESPACE) - target_compile_definitions( - header_only INTERFACE -DUNITS_NAMESPACE=${UNITS_NAMESPACE} - ) - endif() - if(UNITS_BASE_TYPE) - target_compile_definitions( - header_only INTERFACE -DUNITS_BASE_TYPE=${UNITS_BASE_TYPE} - ) - endif() - add_library(units::header_only ALIAS header_only) +endif() +if(UNITS_BASE_TYPE) + target_compile_definitions( + header_only INTERFACE -DUNITS_BASE_TYPE=${UNITS_BASE_TYPE} + ) +endif() +add_library(units::header_only ALIAS header_only) if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL) install(FILES ${units_header_files} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/units) diff --git a/webserver/CMakeLists.txt b/webserver/CMakeLists.txt index 98e60813..65bedc2d 100644 --- a/webserver/CMakeLists.txt +++ b/webserver/CMakeLists.txt @@ -33,11 +33,13 @@ if(CMAKE_VERSION VERSION_GREATER 3.12) find_package(Threads REQUIRED) target_link_libraries(units_webserver PUBLIC Threads::Threads) endif() - - if (UNITS_INSTALL) - install(TARGETS units_webserver - ${UNITS_LIBRARY_EXPORT_COMMAND} - DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT units_webserver) + + if(UNITS_INSTALL) + install( + TARGETS units_webserver ${UNITS_LIBRARY_EXPORT_COMMAND} + DESTINATION "${CMAKE_INSTALL_BINDIR}" + COMPONENT units_webserver + ) endif() add_executable(units::units_webserver ALIAS units_webserver) endif()