Skip to content

Commit

Permalink
feat(CMakeLists.txt): set _GLIBCXX_USE_CXX11_ABI from environment v…
Browse files Browse the repository at this point in the history
…ariable (#95)
  • Loading branch information
XuehaiPan authored Oct 24, 2023
1 parent 1582f22 commit d8b4611
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ci:
default_stages: [commit, push, manual]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-symlinks
- id: destroyed-symlinks
Expand All @@ -26,11 +26,11 @@ repos:
- id: debug-statements
- id: double-quote-string-fixer
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v16.0.6
rev: v17.0.3
hooks:
- id: clang-format
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
rev: v0.1.1
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand All @@ -39,11 +39,11 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.9.1
rev: 23.10.1
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v3.14.0
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py37-plus]
Expand Down
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # -fPIC
set(CMAKE_CXX_VISIBILITY_PRESET hidden) # -fvisibility=hidden

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(NOT DEFINED _GLIBCXX_USE_CXX11_ABI AND NOT "$ENV{_GLIBCXX_USE_CXX11_ABI}" STREQUAL "")
set(_GLIBCXX_USE_CXX11_ABI "$ENV{_GLIBCXX_USE_CXX11_ABI}")
endif()
if(_GLIBCXX_USE_CXX11_ABI)
message(STATUS "Use _GLIBCXX_USE_CXX11_ABI: ${_GLIBCXX_USE_CXX11_ABI}")
add_definitions("-D_GLIBCXX_USE_CXX11_ABI=${_GLIBCXX_USE_CXX11_ABI}") # use C++11 ABI
endif()
endif()

if(MSVC)
string(
APPEND CMAKE_CXX_FLAGS
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ addlicense-install: go-install

pytest: pytest-install
cd tests && $(PYTHON) -c 'import $(PROJECT_PATH)' && \
$(PYTHON) -c 'import $(PROJECT_PATH)._C; print(f"GLIBCXX_USE_CXX11_ABI={$(PROJECT_PATH)._C.GLIBCXX_USE_CXX11_ABI}")' && \
$(PYTHON) -m pytest --verbose --color=yes --durations=0 \
--cov="$(PROJECT_PATH)" --cov-config=.coveragerc --cov-report=xml --cov-report=term-missing \
$(PYTESTOPTS) .
Expand Down
2 changes: 2 additions & 0 deletions optree/_C.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ MAX_RECURSION_DEPTH: int
# Set if the type allows subclassing (see CPython's Include/object.h)
Py_TPFLAGS_BASETYPE: int # (1UL << 10)

GLIBCXX_USE_CXX11_ABI: bool

def flatten(
tree: PyTree[T],
leaf_predicate: Callable[[T], bool] | None = None,
Expand Down
6 changes: 6 additions & 0 deletions src/optree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ void BuildModule(py::module& mod) { // NOLINT[runtime/references]
py::register_local_exception<InternalError>(mod, "InternalError", PyExc_SystemError);
mod.attr("MAX_RECURSION_DEPTH") = py::ssize_t_cast(MAX_RECURSION_DEPTH);
mod.attr("Py_TPFLAGS_BASETYPE") = py::ssize_t_cast(Py_TPFLAGS_BASETYPE);
#ifdef _GLIBCXX_USE_CXX11_ABI
// NOLINTNEXTLINE[modernize-use-bool-literals]
mod.attr("GLIBCXX_USE_CXX11_ABI") = static_cast<bool>(_GLIBCXX_USE_CXX11_ABI);
#else
mod.attr("GLIBCXX_USE_CXX11_ABI") = false;
#endif

mod.def("register_node",
&PyTreeTypeRegistry::Register,
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# ==============================================================================

import os
import random


os.environ['PYTHONHASHSEED'] = '0'
random.seed(0)

0 comments on commit d8b4611

Please sign in to comment.