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 6, 2023
1 parent 98788f1 commit 5a4155d
Show file tree
Hide file tree
Showing 66 changed files with 308 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ project(mp-units VERSION 0.8.0 LANGUAGES CXX)
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 @@ -48,6 +50,9 @@ target_link_libraries(mp-units INTERFACE mp-units::core mp-units::core-io mp-uni
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 @@ -67,3 +67,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()

# TODO Remove when Clang's compilation matrix of module `std` stops depending on unrelated `-D` flags.
set(modules-ignore-macro-options)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
foreach(macro IN ITEMS "${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})
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()
4 changes: 3 additions & 1 deletion src/core-fmt/include/units/bits/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
//
// For the license information refer to format.h.

#include <gsl/gsl-lite.hpp>
#include <units/bits/fmt_hacks.h>
#ifndef UNITS_MODULE
#include <gsl/gsl-lite.hpp>
#include <concepts>
#include <limits>
#include <string_view>
#endif

// most of the below code is based on/copied from libfmt

Expand Down
2 changes: 2 additions & 0 deletions src/core-fmt/include/units/bits/fmt_hacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ UNITS_DIAGNOSTIC_POP
#error "std::formatting facility not supported"
#endif

#ifndef UNITS_MODULE
#include <format>
#endif

#define UNITS_STD_FMT std
#define UNITS_FMT_LOCALE(loc) loc
Expand Down
2 changes: 2 additions & 0 deletions src/core-fmt/include/units/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#include <units/bits/fmt.h>
#include <units/customization_points.h>
#include <units/quantity.h>
#ifndef UNITS_MODULE
#include <algorithm>
#include <cstdint>
#endif

// IWYU pragma: begin_exports
#include <units/bits/unit_text.h>
Expand Down
2 changes: 2 additions & 0 deletions src/core-io/include/units/bits/external/fixed_string_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#pragma once

#include <units/bits/external/fixed_string.h>
#ifndef UNITS_MODULE
#include <ostream>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core-io/include/units/quantity_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
// IWYU pragma: begin_exports
#include <units/bits/external/fixed_string_io.h>
#include <units/bits/unit_text.h>
#ifndef UNITS_MODULE
#include <sstream>
#endif
// IWYU pragma: end_exports

namespace units {
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 @@ -27,7 +27,9 @@
#include <units/bits/external/fixed_string.h>
// IWYU pragma: end_exports

#ifndef UNITS_MODULE
#include <type_traits>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#pragma once

#include <units/bits/external/hacks.h> // IWYU pragma: keep
#ifndef UNITS_MODULE
#include <compare>
#include <iterator>
#endif

namespace units::detail {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/basic_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
#include <units/symbol_text.h>
// IWYU pragma: end_exports

#ifndef UNITS_MODULE
#include <cstdint>
#include <functional>
#include <utility>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/constexpr_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

#pragma once

#ifndef UNITS_MODULE
#include <gsl/gsl-lite.hpp>
#endif
#include <units/bits/external/hacks.h>
#include <units/bits/math_concepts.h>
#include <units/bits/ratio_maths.h>
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/dim_consolidate.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

#include <units/bits/external/type_list.h>
#include <units/exponent.h>
#ifndef UNITS_MODULE
#include <ratio>
#endif

namespace units::detail {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/external/downcasting.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#pragma once

#include <units/bits/external/hacks.h>
#ifndef UNITS_MODULE
#include <type_traits>
#endif

namespace units {

Expand Down
3 changes: 2 additions & 1 deletion src/core/include/units/bits/external/fixed_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
// TODO use <algorithm> when moved to C++20 modules (parsing takes too long for each translation unit)
#include <units/bits/algorithm.h>

#ifndef UNITS_MODULE
// IWYU pragma: begin_exports
#include <compare>
#include <cstdlib>
// IWYU pragma: end_exports

#include <cstddef>

#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/external/hacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@

#endif

#ifndef UNITS_MODULE
#include <compare>
#include <concepts>
#endif

namespace std {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/external/type_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

#pragma once

#ifndef UNITS_MODULE
#include <cstddef>
#endif

UNITS_DIAGNOSTIC_PUSH
UNITS_DIAGNOSTIC_IGNORE_EXPR_ALWAYS_TF
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/external/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#pragma once

#include <units/bits/external/hacks.h>
#ifndef UNITS_MODULE
#include <type_traits>
#include <utility>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/math_concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

#pragma once

#ifndef UNITS_MODULE
#include <cstdint>
#endif

namespace units::detail {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/prime.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#pragma once

#include <units/bits/algorithm.h>
#ifndef UNITS_MODULE
#include <array>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <optional>
#include <tuple>
#endif

namespace units::detail {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/bits/ratio_maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#pragma once

#ifndef UNITS_MODULE
#include <gsl/gsl-lite.hpp>
#include <array>
#include <cassert>
Expand All @@ -30,6 +31,7 @@
#include <numeric>
#include <tuple>
#include <type_traits>
#endif

namespace units::detail {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
// IWYU pragma: begin_exports
#include <units/isq/si/time.h>
#include <units/point_origin.h>
#ifndef UNITS_MODULE
#include <chrono>
#endif
// IWYU pragma: end_exports

namespace units {
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/customization_points.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#pragma once

#include <units/bits/external/hacks.h>
#ifndef UNITS_MODULE
#include <limits>
#include <type_traits>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/magnitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
#include <units/bits/external/hacks.h>
#include <units/bits/prime.h>
#include <units/ratio.h>
#ifndef UNITS_MODULE
#include <concepts>
#include <cstdint>
#include <exception>
#include <numbers>
#include <optional>
#endif

namespace units {
namespace detail {
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
#include <units/quantity.h>
#include <units/unit.h>

#ifndef UNITS_MODULE
// IWYU pragma: begin_exports
#include <cmath>
#include <cstdint>
// IWYU pragma: end_exports

#include <limits>
#endif

namespace units {

Expand Down
4 changes: 3 additions & 1 deletion src/core/include/units/quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
// IWYU pragma: begin_exports
#include <units/quantity_cast.h>
#include <units/ratio.h>
#ifndef UNITS_MODULE
#include <compare>
// IWYU pragma: end_exports

#include <units/reference.h>
#include <utility>
#endif
#include <units/reference.h>

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/quantity_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
#include <units/quantity.h>
// IWYU pragma: end_exports

#ifndef UNITS_MODULE
#include <concepts>
#include <type_traits>
#include <utility>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
// IWYU pragma: begin_exports
#include <units/point_origin.h>
#include <units/quantity.h>
#ifndef UNITS_MODULE
#include <compare>
#endif
// IWYU pragma: end_exports

#include <units/customization_points.h>
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/quantity_point_kind.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
// IWYU pragma: begin_exports
#include <units/quantity_kind.h>
#include <units/quantity_point.h>
#ifndef UNITS_MODULE
#include <compare>
#endif
// IWYU pragma: end_exports

namespace units {
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#pragma once

#include <units/concepts.h>
#ifndef UNITS_MODULE
#include <functional>
#include <random>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/ratio.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
// IWYU pragma: begin_exports
#include <units/bits/math_concepts.h>
#include <units/bits/ratio_maths.h>
#ifndef UNITS_MODULE
#include <cstdint>
// IWYU pragma: end_exports

#include <gsl/gsl-lite.hpp>
#include <array>
#include <compare>
#include <numeric>
#endif

namespace units {

Expand Down
2 changes: 2 additions & 0 deletions src/core/include/units/symbol_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
// IWYU pragma: begin_exports
#include <units/bits/external/fixed_string.h>
#include <units/bits/external/hacks.h>
#ifndef UNITS_MODULE
#include <compare>
#include <cstddef>
#include <cstdint>
// IWYU pragma: end_exports

#include <gsl/gsl-lite.hpp>
#endif

namespace units {

Expand Down
Loading

0 comments on commit 5a4155d

Please sign in to comment.