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 a3ff95d
Show file tree
Hide file tree
Showing 67 changed files with 313 additions and 20 deletions.
8 changes: 8 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 @@ -37,6 +39,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 @@ -48,6 +53,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
26 changes: 26 additions & 0 deletions src/cmake/AddUnitsModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,29 @@ 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()
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
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.19)
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 @@ -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
Loading

0 comments on commit a3ff95d

Please sign in to comment.