Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document and enforce minimum dependency versions. #80

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions cpp/test/generated/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
# target_link_libraries(<your target> test_model_generated)
# add_subdirectory(<path to this directory>)

find_package(date REQUIRED)
set(HOWARD_HINNANT_DATE_MINIMUM_VERSION "3.0.0")
find_package(date ${HOWARD_HINNANT_DATE_MINIMUM_VERSION} REQUIRED)

if(VCPKG_TARGET_TRIPLET)
set(HDF5_CXX_LIBRARIES hdf5::hdf5_cpp-shared)
else()
set(HDF5_CXX_LIBRARIES hdf5::hdf5_cpp)
endif()

find_package(HDF5 REQUIRED COMPONENTS C CXX)
find_package(xtensor REQUIRED)
set(HDF5_MINIMUM_VERSION "1.10.5")
find_package(HDF5 ${HDF5_MINIMUM_VERSION} REQUIRED COMPONENTS C CXX)

set(XTENSOR_MINIMUM_VERSION "0.21.10")
find_package(xtensor ${XTENSOR_MINIMUM_VERSION} REQUIRED)

set(NLOHMANN_JSON_MINIMUM_VERSION "3.11.1")
find_package(nlohmann_json ${NLOHMANN_JSON_MINIMUM_VERSION} REQUIRED)
add_library(test_model_generated OBJECT
factories.cc
protocols.cc
Expand All @@ -31,6 +38,7 @@ target_link_libraries(test_model_generated
PUBLIC ${HDF5_CXX_LIBRARIES}
PUBLIC xtensor
PUBLIC date::date
PUBLIC nlohmann_json::nlohmann_json
)

add_library(test_model_generated_mocks OBJECT
Expand Down
11 changes: 7 additions & 4 deletions docs/cpp/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
In order to compile the C++ code that `yardl` generates, you will need to have a
C++17 (or more recent) compiler and the following dependencies installed:

1. HDF5 with the [C++ API](https://support.hdfgroup.org/HDF5/doc/cpplus_RM/).
2. [xtensor](https://xtensor.readthedocs.io/en/latest/)
1. HDF5 with the [C++ API](https://support.hdfgroup.org/HDF5/doc/cpplus_RM/),
version 1.10.5 or later.
2. [xtensor](https://xtensor.readthedocs.io/en/latest/), version 0.21.10 or
later.
3. Howard Hinnant's [date](https://howardhinnant.github.io/date/date.html)
library.
4. [JSON for Modern C++](https://github.com/nlohmann/json).
library, version 3.0.0 or later.
4. [JSON for Modern C++](https://github.com/nlohmann/json), version: 3.11.1 or
later.

### Conda

Expand Down
3 changes: 2 additions & 1 deletion docs/python/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

### Dependencies

The generated Python code requires Python 3.9 or newer and you need to have [NumPy](https://numpy.org/install/) installed.
The generated Python code requires Python 3.9 or newer and you need to have
[NumPy](https://numpy.org/install/) version 1.22.0 or later installed.

## Getting our Feet Wet

Expand Down
17 changes: 17 additions & 0 deletions python/test_model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# This file was generated by the "yardl" tool. DO NOT EDIT.

from typing import Tuple as _Tuple
import re as _re
import numpy as _np

_MIN_NUMPY_VERSION = (1, 22, 0)

def _parse_version(version: str) -> _Tuple[int, ...]:
try:
return tuple(map(int, version.split(".")))
except ValueError:
# ignore any prerelease suffix
version = _re.sub(r"[^0-9.]", "", version)
return tuple(map(int, version.split(".")))

if _parse_version(_np.__version__) < _MIN_NUMPY_VERSION:
raise ImportError(f"Your installed numpy version is {_np.__version__}, but version >= {'.'.join(str(i) for i in _MIN_NUMPY_VERSION)} is required.")

from .yardl_types import *
from .types import (
AcquisitionOrImage,
Expand Down
6 changes: 3 additions & 3 deletions python/tests/roundtriputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pathlib
import subprocess
import types
from typing import Callable, TypeVar, cast
from typing import Callable, TypeVar, Union, cast

import test_model as tm
from .factories import Format
Expand All @@ -17,8 +17,8 @@


def invoke_translator(
input: bytes | str, input_format: Format, output_format: Format
) -> bytes | str:
input: Union[bytes, str], input_format: Format, output_format: Format
) -> Union[bytes, str]:
if isinstance(input, str):
print(input)
input = input.encode("utf-8")
Expand Down
11 changes: 11 additions & 0 deletions python/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test_model as tm
from packaging import version
# pyright: basic

def test_parse_version():
assert tm._parse_version("1.2.3") == (1,2,3)
assert tm._parse_version("1.2.3") < (1,2,4)
assert tm._parse_version("2.2.3") > (1,9,9)
assert tm._parse_version("2.2.3") > (1,9)
assert tm._parse_version("1.26.1rc1") > (1,26,1)
version.parse
14 changes: 11 additions & 3 deletions tooling/internal/cpp/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ func writeCMakeLists(env *dsl.Environment, options packaging.CppCodegenOptions)
# target_link_libraries(<your target> %s)
# add_subdirectory(<path to this directory>)

find_package(date REQUIRED)
set(HOWARD_HINNANT_DATE_MINIMUM_VERSION "3.0.0")
find_package(date ${HOWARD_HINNANT_DATE_MINIMUM_VERSION} REQUIRED)

if(VCPKG_TARGET_TRIPLET)
set(HDF5_CXX_LIBRARIES hdf5::hdf5_cpp-shared)
else()
set(HDF5_CXX_LIBRARIES hdf5::hdf5_cpp)
endif()

find_package(HDF5 REQUIRED COMPONENTS C CXX)
find_package(xtensor REQUIRED)
set(HDF5_MINIMUM_VERSION "1.10.5")
find_package(HDF5 ${HDF5_MINIMUM_VERSION} REQUIRED COMPONENTS C CXX)

set(XTENSOR_MINIMUM_VERSION "0.21.10")
find_package(xtensor ${XTENSOR_MINIMUM_VERSION} REQUIRED)

set(NLOHMANN_JSON_MINIMUM_VERSION "3.11.1")
find_package(nlohmann_json ${NLOHMANN_JSON_MINIMUM_VERSION} REQUIRED)
`, objectLibraryName)

fmt.Fprintf(w, "add_library(%s OBJECT\n", objectLibraryName)
Expand All @@ -62,6 +69,7 @@ find_package(xtensor REQUIRED)
w.WriteStringln("PUBLIC ${HDF5_CXX_LIBRARIES}")
w.WriteStringln("PUBLIC xtensor")
w.WriteStringln("PUBLIC date::date")
w.WriteStringln("PUBLIC nlohmann_json::nlohmann_json")
})
w.WriteString(")\n")

Expand Down
15 changes: 9 additions & 6 deletions tooling/internal/cpp/include/detail/ndjson/serializers.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ struct adl_serializer<yardl::DynamicNDArray<T>> {
static void from_json(ordered_json const& j, yardl::DynamicNDArray<T>& value) {
value.resize(j.at("shape").get<std::vector<size_t>>());
auto data_array = j.at("data").get<std::vector<T>>();
for (size_t i = 0; i < data_array.size(); ++i) {
value.flat(i) = data_array[i];
size_t i = 0;
for (auto& element : value) {
element = data_array[i++];
}
}
};
Expand All @@ -225,8 +226,9 @@ struct adl_serializer<yardl::NDArray<T, N>> {
static void from_json(ordered_json const& j, yardl::NDArray<T, N>& value) {
value.resize(j.at("shape").get<std::vector<size_t>>());
auto data_array = j.at("data").get<std::vector<T>>();
for (size_t i = 0; i < data_array.size(); ++i) {
value.flat(i) = data_array[i];
size_t i = 0;
for (auto& element : value) {
element = data_array[i++];
}
}
};
Expand All @@ -243,8 +245,9 @@ struct adl_serializer<yardl::FixedNDArray<T, Dims...>> {

static void from_json(ordered_json const& j, yardl::FixedNDArray<T, Dims...>& value) {
auto data_array = j.get<std::vector<T>>();
for (size_t i = 0; i < data_array.size(); ++i) {
value.flat(i) = data_array[i];
size_t i = 0;
for (auto& element : value) {
element = data_array[i++];
}
}
};
Expand Down
18 changes: 18 additions & 0 deletions tooling/internal/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ func writePackageInitFile(packageDir string, ns *dsl.Namespace) error {
w := formatting.NewIndentedWriter(&b, " ")
common.WriteGeneratedFileHeader(w)

w.WriteStringln(`from typing import Tuple as _Tuple
import re as _re
import numpy as _np

_MIN_NUMPY_VERSION = (1, 22, 0)

def _parse_version(version: str) -> _Tuple[int, ...]:
try:
return tuple(map(int, version.split(".")))
except ValueError:
# ignore any prerelease suffix
version = _re.sub(r"[^0-9.]", "", version)
return tuple(map(int, version.split(".")))

if _parse_version(_np.__version__) < _MIN_NUMPY_VERSION:
raise ImportError(f"Your installed numpy version is {_np.__version__}, but version >= {'.'.join(str(i) for i in _MIN_NUMPY_VERSION)} is required.")
`)

fmt.Fprintf(w, "from .yardl_types import *\n")

typesMembers := make([]string, 0)
Expand Down
Loading