Skip to content

Commit

Permalink
add config template
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Aug 13, 2021
1 parent e50ce8d commit 636880f
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 94 deletions.
63 changes: 23 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,15 @@ set(UNITS_NAMESPACE
CACHE STRING "Top-level namespace name. Default is `units`."
)

option(UNITS_INSTALL "Enable installation of the units library" ON)
mark_as_advanced(UNITS_INSTALL)

cmake_dependent_option(
UNITS_WITH_CMAKE_PACKAGE
UNITS_INSTALL
"Generate and install cmake package files"
ON
"CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME;UNITS_INSTALL;NOT UNITS_BINARY_ONLY_INSTALL"
"CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME;NOT UNITS_BINARY_ONLY_INSTALL"
OFF
)

mark_as_advanced(UNITS_WITH_CMAKE_PACKAGE)
mark_as_advanced(UNITS_INSTALL)

cmake_dependent_option(
UNITS_BUILD_FUZZ_TARGETS "Build the targets for a fuzzing system" OFF
Expand All @@ -113,12 +110,9 @@ set(UNITS_CLANG_TIDY_OPTIONS
mark_as_advanced(UNITS_CLANG_TIDY_OPTIONS)
mark_as_advanced(UNITS_CLANG_TIDY)

# Install instructions for this target
if(UNITS_WITH_CMAKE_PACKAGE)
set(UNITS_LIBRARY_EXPORT_COMMAND EXPORT unitsConfig)
else(UNITS_WITH_CMAKE_PACKAGE)
set(UNITS_LIBRARY_EXPORT_COMMAND)
endif(UNITS_WITH_CMAKE_PACKAGE)

set(UNITS_LIBRARY_EXPORT_COMMAND EXPORT unitsTargets)
mark_as_advanced(UNITS_LIBRARY_EXPORT_COMMAND)

option(UNITS_HEADER_ONLY "Expose the units library as header-only" OFF)

Expand Down Expand Up @@ -160,7 +154,13 @@ if(NOT UNITS_HEADER_ONLY)
UNITS_BUILD_OBJECT_LIBRARY "Enable construction of the units object library"
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
)
endif(NOT UNITS_HEADER_ONLY)

# Prepare Clang-Tidy
Expand Down Expand Up @@ -193,32 +193,15 @@ if(UNITS_INSTALL)
if(UNITS_BUILD_STATIC_LIBRARY)
install(TARGETS compile_flags_target ${UNITS_LIBRARY_EXPORT_COMMAND})
endif()
if(UNITS_WITH_CMAKE_PACKAGE AND NOT UNITS_BINARY_ONLY_INSTALL)
install(EXPORT unitsTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
export(EXPORT unitsTargets NAMESPACE units::)
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 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units)
install(FILES ${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake ${PROJECT_BINARY_DIR}/unitsConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units)
endif()
endif()



#if(HELICS_WITH_CMAKE_PACKAGE)

# set(HELICS_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
# CACHE STRING "install path for HELICSConfig.cmake"
# )
# mark_as_advanced(HELICS_CMAKECONFIG_INSTALL_DIR)

# Export targets for importing build tree with find_package
# export(EXPORT helics-targets NAMESPACE HELICS::
# FILE ${PROJECT_BINARY_DIR}/helics-targets.cmake
# )

# install(
# EXPORT helics-targets
# NAMESPACE HELICS::
# DESTINATION ${HELICS_CMAKECONFIG_INSTALL_DIR}
# COMPONENT libs
# )
# endif()
endif()
4 changes: 2 additions & 2 deletions config/unitConfig.cmake.in → config/unitsConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ endif()

# These are IMPORTED targets created by FooBarTargets.cmake
set(UNITS_LIBRARIES units::units)
set(UNITS_WEBSERVER units::webserver)
set(UNITS_CONVERT units::convert
set(UNITS_WEBSERVER units::units_webserver)
set(UNITS_CONVERT units::units_convert)
16 changes: 8 additions & 8 deletions converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ cmake_dependent_option(
)

if(UNITS_BUILD_CONVERTER_APP)
add_executable(unit_convert converter.cpp)
target_link_libraries(unit_convert PUBLIC units::units compile_flags_target)
target_include_directories(unit_convert PRIVATE ${PROJECT_SOURCE_DIR}/ThirdParty)
add_executable(units_convert converter.cpp)
target_link_libraries(units_convert PUBLIC units::units compile_flags_target)
target_include_directories(units_convert PRIVATE ${PROJECT_SOURCE_DIR}/ThirdParty)
target_compile_definitions(
unit_convert PUBLIC UNITS_VERSION_STRING="${UNITS_VERSION}"
units_convert PUBLIC UNITS_VERSION_STRING="${UNITS_VERSION}"
)
if(CMAKE_CXX_STANDARD GREATER 16)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION
VERSION_EQUAL 8
)
target_link_libraries(unit_convert PRIVATE -lstdc++fs)
target_link_libraries(unit_sconvert PRIVATE -lstdc++fs)
endif()
endif()

if (UNITS_INSTALL)
install(TARGETS unit_convert
EXPORT unitsTargets
install(TARGETS units_convert
${UNITS_LIBRARY_EXPORT_COMMAND}
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT units_convert)
endif()

add_executable(units::units_convert ALIAS units_convert)
endif()
2 changes: 0 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ if(MSVC AND ${CMAKE_VERSION} VERSION_GREATER 3.12.9)
${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" -A "${CMAKE_GENERATOR_PLATFORM}"
${UNITS_PACKAGE_SEARCH_LOC}
"-DUNITS_BUILD_SHARED_LIBRARY=${UNITS_BUILD_SHARED_LIBRARY}"
"-DUNITS_HEADER_ONLY=${UNITS_HEADER_ONLY}"
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
Expand All @@ -173,7 +172,6 @@ else()
COMMAND
${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${UNITS_PACKAGE_SEARCH_LOC}
"-DUNITS_BUILD_SHARED_LIBRARY=${UNITS_BUILD_SHARED_LIBRARY}"
"-DUNITS_HEADER_ONLY=${UNITS_HEADER_ONLY}"
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
Expand Down
12 changes: 6 additions & 6 deletions test/find_package_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project(units-find-package-test)
include(CTest)

# Test the HELICS CMake package config
find_package(units 0.4.8 REQUIRED)
find_package(units 0.5 REQUIRED)

# add some tests inside of the CMake

Expand All @@ -29,14 +29,14 @@ if(UNITS_BUILD_SHARED_LIBRARY)
TEST shared-library-test PROPERTY PASS_REGULAR_EXPRESSION "10.7"
)

# Test the C and C++98 targets
elseif(UNITS_HEADER_ONLY)

endif()
# Test the header only targets
add_executable(header_only-exe ../pkg_test_code/header_only.cpp)
target_link_libraries(header_only-exe units::units_headers)
target_link_libraries(header_only-exe units::header_only)

add_test(NAME header_only-exe COMMAND header_only-exe)
set_property(
TEST header_only-test PROPERTY PASS_REGULAR_EXPRESSION "${HELICS_VERSION}"
TEST header_only-exe PROPERTY PASS_REGULAR_EXPRESSION "PASS"
)

endif()
20 changes: 13 additions & 7 deletions test/pkg_test_code/header_only.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ for Sustainable Energy, LLC. See the top-level NOTICE for additional details.
All rights reserved. SPDX-License-Identifier: BSD-3-Clause
*/

#include "helics/helics.h"
#include "units/units.hpp"

#include <iostream>

#include <stdio.h>
int main()
{
volatile HelicsFederateInfo fedinfo = helicsCreateFederateInfo();
helicsFederateInfoFree(fedinfo);
printf("%s\n", helicsGetVersion());
helicsCloseLibrary();
return (0);
units::measurement b(50.0, units::m);
units::measurement c(25.0, units::f);
auto k = b * c;

if (k.units() == units::units(m.pow(2))) {
std::cout << "PASS\n";
} else {
std::cout << "FAILE\n";
}
return 0;
}
2 changes: 1 addition & 1 deletion test/pkg_test_code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ int main(int /*argc*/, char* /*argv*/[])
{
auto u1 = units::measurement_from_string("10.7 meters per second");

std::cout << u1 << std::endl;
std::cout << to_string(u1) << std::endl;
return 0;
}
56 changes: 29 additions & 27 deletions units/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 OR BUILD_SHARED_LIBS)
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)
Expand Down Expand Up @@ -38,30 +38,10 @@ if(UNITS_BUILD_SHARED_LIBRARY OR BUILD_SHARED_LIBS)
)
endif()
add_library(units::units ALIAS units)
elseif(UNITS_HEADER_ONLY)
add_library(header_only INTERFACE)
target_include_directories(
header_only INTERFACE $<BUILD_INTERFACE:${UNITS_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_definitions(header_only INTERFACE UNITS_HEADER_ONLY)
if(UNITS_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)

elseif(UNITS_BUILD_OBJECT_LIBRARY)
add_library(units OBJECT ${units_source_files} ${units_header_files})
target_include_directories(units PRIVATE $<BUILD_INTERFACE:${UNITS_SOURCE_DIR}>)
target_include_directories(units PRIVATE $<BUILD_INTERFACE:${units_SOURCE_DIR}>)

if(UNITS_NAMESPACE)
target_compile_definitions(units PUBLIC -DUNITS_NAMESPACE=${UNITS_NAMESPACE})
Expand All @@ -72,7 +52,7 @@ elseif(UNITS_BUILD_OBJECT_LIBRARY)
)
endif()
endif()
else()
elseif (UNITS_BUILD_STATIC_LIBRARY)
add_library(units STATIC ${units_source_files} ${units_header_files})
target_include_directories(
units
Expand All @@ -93,11 +73,11 @@ else()
if(UNITS_BASE_TYPE)
target_compile_definitions(units PUBLIC -DUNITS_BASE_TYPE=${UNITS_BASE_TYPE})
endif()
if(UNITS_INSTALL)
if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL)
install(TARGETS units ${UNITS_LIBRARY_EXPORT_COMMAND}
DESTINATION ${CMAKE_INSTALL_LIBDIR} # INCLUDES DESTINATION include/units
DESTINATION ${CMAKE_INSTALL_LIBDIR} # INCLUDES DESTINATION include/${PROJECT_NAME}
)
endif(UNITS_INSTALL)
endif()
if(CMAKE_BUILD_TYPE STREQUAL Coverage)
set_source_files_properties(foo.cpp PROPERTIES COMPILE_FLAGS -Wno-effc++)
endif()
Expand All @@ -107,6 +87,28 @@ else()
add_library(units::units ALIAS units)
endif()

## add the header only interface library
add_library(header_only INTERFACE)
target_include_directories(
header_only INTERFACE $<BUILD_INTERFACE:${units_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
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)

if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL)
install(FILES ${units_header_files} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/units)
if(UNITS_BUILD_SHARED_LIBRARY OR BUILD_SHARED_LIBS)
Expand Down
2 changes: 1 addition & 1 deletion webserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if(CMAKE_VERSION VERSION_GREATER 3.12)

if (UNITS_INSTALL)
install(TARGETS units_webserver
EXPORT unitsTargets
${UNITS_LIBRARY_EXPORT_COMMAND}
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT units_webserver)
endif()
add_executable(units::units_webserver ALIAS units_webserver)
Expand Down

0 comments on commit 636880f

Please sign in to comment.