Skip to content

Commit

Permalink
feat: add C++ modules support for Clang & Libc++
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Mar 29, 2022
1 parent fa0b45b commit e17e509
Show file tree
Hide file tree
Showing 207 changed files with 1,381 additions and 988 deletions.
8 changes: 5 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,11 @@ SpaceBeforeParensOptions:
Standard: Latest
# StatementAttributeLikeMacros:
# - Q_EMIT
# StatementMacros:
# - Q_UNUSED
# - QT_REQUIRE_VERSION
StatementMacros:
- UNITS_BEGIN_EXPORT_BLOCK
- UNITS_LLVM_BEGIN_EXPORT_BLOCK
- UNITS_END_EXPORT_BLOCK
- UNITS_LLVM_END_EXPORT_BLOCK
# TabWidth: 8
# UseCRLF: false
# UseTab: Never
Expand Down
8 changes: 2 additions & 6 deletions example/kalman_filter/kalman_filter-example_6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ namespace units::isq::si {

struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}, no_prefix> {};

namespace thermodynamic_temperature_references {
namespace references::inline thermodynamic_temperature_references {

inline constexpr auto deg_C = reference<dim_thermodynamic_temperature, degree_celsius>{};

} // namespace thermodynamic_temperature_references

namespace references {

using namespace thermodynamic_temperature_references;

} // namespace references
namespace thermodynamic_temperature_references = references::thermodynamic_temperature_references;

} // namespace units::isq::si

Expand Down
8 changes: 2 additions & 6 deletions example/kalman_filter/kalman_filter-example_7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ namespace units::isq::si {

struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}, no_prefix> {};

namespace thermodynamic_temperature_references {
namespace references::inline thermodynamic_temperature_references {

inline constexpr auto deg_C = reference<dim_thermodynamic_temperature, degree_celsius>{};

} // namespace thermodynamic_temperature_references

namespace references {

using namespace thermodynamic_temperature_references;

} // namespace references
namespace thermodynamic_temperature_references = references::thermodynamic_temperature_references;

} // namespace units::isq::si

Expand Down
8 changes: 2 additions & 6 deletions example/kalman_filter/kalman_filter-example_8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ namespace units::isq::si {

struct degree_celsius : alias_unit<kelvin, basic_symbol_text{"°C", "deg_C"}, no_prefix> {};

namespace thermodynamic_temperature_references {
namespace references::inline thermodynamic_temperature_references {

inline constexpr auto deg_C = reference<dim_thermodynamic_temperature, degree_celsius>{};

} // namespace thermodynamic_temperature_references

namespace references {

using namespace thermodynamic_temperature_references;

} // namespace references
namespace thermodynamic_temperature_references = references::thermodynamic_temperature_references;

} // namespace units::isq::si

Expand Down
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ project(mp-units
set(projectPrefix UNITS_)

option(${projectPrefix}AS_SYSTEM_HEADERS "Exports library as system headers" OFF)
option(${projectPrefix}AS_MODULES "Exports library as modules" OFF)
message(STATUS "${projectPrefix}AS_SYSTEM_HEADERS: ${${projectPrefix}AS_SYSTEM_HEADERS}")
message(STATUS "${projectPrefix}AS_MODULES: ${${projectPrefix}AS_MODULES}")

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

Expand All @@ -40,6 +42,9 @@ if(${projectPrefix}AS_SYSTEM_HEADERS)
set(unitsAsSystem SYSTEM)
endif()

# find dependencies
find_package(gsl-lite CONFIG REQUIRED)

add_subdirectory(core)
add_subdirectory(core-fmt)
add_subdirectory(core-io)
Expand All @@ -56,6 +61,9 @@ target_link_libraries(mp-units INTERFACE
add_library(mp-units::mp-units ALIAS mp-units)
install(TARGETS mp-units EXPORT mp-unitsTargets)

# C++ module
add_units_cxx_module(module SOURCES "mp_units.cpp" LINK_LIBRARIES PUBLIC mp-units::mp-units)

# local build
export(EXPORT mp-unitsTargets NAMESPACE mp-units::)
configure_file("mp-unitsConfig.cmake" "." COPYONLY)
Expand Down
24 changes: 24 additions & 0 deletions src/cmake/AddUnitsModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,27 @@ function(add_units_module name)
install(TARGETS mp-units-${name} EXPORT mp-unitsTargets)
install(DIRECTORY include/units TYPE INCLUDE)
endfunction()

if(${projectPrefix}AS_MODULES)
include(JEGPAddModule)
endif()

function(add_units_cxx_module name)
if(NOT ${projectPrefix}AS_MODULES)
return()
endif()

set(modules-ignore-macro-options)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
foreach(macro IN ITEMS "UNITS_MODULES" "${projectPrefix}DOWNCAST_MODE" "${projectPrefix}USE_LIBFMT" "FMT_LOCALE" "FMT_SHARED")
list(APPEND modules-ignore-macro-options "-fmodules-ignore-macro=${macro}")
endforeach()
endif()

jegp_add_module(mp-units-${name} ${ARGN} COMPILE_OPTIONS PUBLIC ${modules-ignore-macro-options})
target_compile_definitions(mp-units-${name} PRIVATE UNITS_MODULES)
set_target_properties(mp-units-${name} PROPERTIES EXPORT_NAME ${name})
add_library(mp-units::${name} ALIAS mp-units-${name})

install(TARGETS mp-units-${name} EXPORT mp-unitsTargets)
endfunction()
3 changes: 0 additions & 3 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ cmake_minimum_required(VERSION 3.15)
set(${projectPrefix}DOWNCAST_MODE ON CACHE STRING "Select downcasting mode")
set_property(CACHE ${projectPrefix}DOWNCAST_MODE PROPERTY STRINGS AUTO ON OFF)

# find dependencies
find_package(gsl-lite CONFIG REQUIRED)

# check if libc++ is being used
include(CheckLibcxxInUse)
check_libcxx_in_use(${projectPrefix}LIBCXX)
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/base_dimension.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace units {
* @tparam Symbol an unique identifier of the base dimension used to provide dimensional analysis support
* @tparam U a base unit to be used for this base dimension
*/
UNITS_EXPORT
template<basic_fixed_string Symbol, Unit U>
requires U::is_named
struct base_dimension {
Expand All @@ -58,6 +59,7 @@ struct base_dimension {
};

// base_dimension_less
UNITS_EXPORT
template<BaseDimension D1, BaseDimension D2>
struct base_dimension_less :
std::bool_constant<(D1::symbol < D2::symbol) ||
Expand Down
Loading

0 comments on commit e17e509

Please sign in to comment.