Skip to content

Commit 0d5c39a

Browse files
committed
Initial commit
0 parents  commit 0d5c39a

File tree

16 files changed

+705
-0
lines changed

16 files changed

+705
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
**/__pycache__
3+
**/build

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# monilog
2+
3+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
mkdir build
4+
cmake -Bbuild -Ssrc
5+
cmake --build build
6+
./build/simpleembedding
7+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
cmake_policy(SET CMP0074 NEW)
3+
4+
# SET VARIABLES
5+
set(CMAKE_CXX_COMPILER /usr/bin/g++)
6+
set(CMAKE_CXX_STANDARD 17)
7+
8+
# PROJECT
9+
project(monilog CXX)
10+
11+
set(CMAKE_VERBOSE_MAKEFILE FALSE)
12+
set(PYBIND11_PYTHON_VERSION 3.8)
13+
14+
find_package(pybind11 REQUIRED)
15+
find_package(Python COMPONENTS Interpreter Development REQUIRED)
16+
message(STATUS "pybind11_INCLUDE_DIRS: ${pybind11_INCLUDE_DIRS}")
17+
include_directories(${pybind11_INCLUDE_DIRS} ${Python_SITELIB}/monilog/include)
18+
19+
# EXECUTABLE simpleembedding
20+
add_executable(simpleembedding SimpleEmbedding.cc)
21+
target_include_directories(simpleembedding PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
22+
add_library(moniloglib SHARED IMPORTED)
23+
set_property(TARGET moniloglib PROPERTY IMPORTED_LOCATION ${Python_SITELIB}/monilog/libmonilog.so)
24+
target_link_libraries(simpleembedding PUBLIC pybind11::embed moniloglib)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <MoniLog.h>
2+
#include <filesystem>
3+
4+
PYBIND11_EMBEDDED_MODULE(example_interface, m) { }
5+
6+
namespace fs = std::filesystem;
7+
8+
int main()
9+
{
10+
std::string path = fs::current_path();
11+
std::vector<std::string> python_path = { path + "/src/" };
12+
std::vector<std::string> python_scripts = {"example_moniloggers"};
13+
std::string interface_module = "example_interface";
14+
std::function<void (pybind11::module_, pybind11::object)> interface_module_initializer =
15+
[](pybind11::module_ iterativeheatequation_module, pybind11::object context_class) { };
16+
17+
MoniLog::register_base_events({
18+
{"SomeEvent", 0},
19+
{"SomeOtherEvent", 1}
20+
});
21+
22+
MoniLog::register_composite_event("SomeCompositeEvent", {"SomeEvent", "SomeOtherEvent"});
23+
24+
std::shared_ptr<MoniLog::MoniLogExecutionContext> ctx(new MoniLog::MoniLogExecutionContext());
25+
26+
MoniLog::bootstrap_monilog(python_path, python_scripts, interface_module, interface_module_initializer);
27+
28+
MoniLog::trigger(0, ctx);
29+
MoniLog::trigger(1, ctx);
30+
31+
MoniLog::trigger("SomeCompositeEvent", ctx);
32+
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from monilog import *
2+
3+
@register("SomeEvent")
4+
def monilogger1(ctx):
5+
print("monilogger 1 triggered!")
6+
7+
@register("SomeOtherEvent")
8+
def monilogger2(ctx):
9+
print("monilogger 2 triggered!")
10+
11+
@register("SomeCompositeEvent")
12+
def monilogger3(ctx):
13+
print("monilogger 3 triggered!")

pyproject.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=42",
4+
"wheel",
5+
"cmake>=3.12",
6+
"pybind11>=2.8.0",
7+
]
8+
9+
build-backend = "setuptools.build_meta"
10+
11+
[tool.isort]
12+
profile = "black"
13+
14+
[tool.pytest.ini_options]
15+
minversion = "6.0"
16+
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
17+
xfail_strict = true
18+
filterwarnings = ["error"]
19+
testpaths = ["tests"]
20+
21+
[tool.cibuildwheel]
22+
test-command = "pytest {project}/tests"
23+
test-extras = ["test"]
24+
test-skip = ["*universal2:arm64"]
25+
# Setuptools bug causes collision between pypy and cpython artifacts
26+
before-build = "rm -rf {project}/build"

setup.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import os
2+
import re
3+
import subprocess
4+
import sys
5+
6+
from pybind11 import get_cmake_dir
7+
from pybind11.setup_helpers import Pybind11Extension, build_ext
8+
from setuptools import Extension, find_packages, setup
9+
10+
__version__ = "0.0.1"
11+
12+
# Convert distutils Windows platform specifiers to CMake -A arguments
13+
PLAT_TO_CMAKE = {
14+
"win32": "Win32",
15+
"win-amd64": "x64",
16+
"win-arm32": "ARM",
17+
"win-arm64": "ARM64",
18+
}
19+
20+
# A CMakeExtension needs a sourcedir instead of a file list.
21+
# The name must be the _single_ output extension from the CMake build.
22+
# If you need multiple extensions, see scikit-build.
23+
class CMakeExtension(Extension):
24+
def __init__(self, name, sourcedir=""):
25+
Extension.__init__(self, name, sources=[])
26+
self.sourcedir = os.path.abspath(sourcedir)
27+
28+
29+
class CMakeBuild(build_ext):
30+
def build_extension(self, ext):
31+
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
32+
33+
# required for auto-detection & inclusion of auxiliary "native" libs
34+
if not extdir.endswith(os.path.sep):
35+
extdir += os.path.sep
36+
37+
# CMake lets you override the generator - we need to check this.
38+
# Can be set with Conda-Build, for example.
39+
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
40+
41+
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
42+
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
43+
# from Python.
44+
cmake_args = [
45+
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}",
46+
f"-DPYTHON_EXECUTABLE={sys.executable}",
47+
f"-DCMAKE_BUILD_TYPE=Debug", # not used on MSVC, but no harm
48+
]
49+
build_args = []
50+
# Adding CMake arguments set as environment variable
51+
# (needed e.g. to build for ARM OSx on conda-forge)
52+
if "CMAKE_ARGS" in os.environ:
53+
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
54+
55+
# if self.compiler.compiler_type != "msvc":
56+
# # Using Ninja-build since it a) is available as a wheel and b)
57+
# # multithreads automatically. MSVC would require all variables be
58+
# # exported for Ninja to pick it up, which is a little tricky to do.
59+
# # Users can override the generator with CMAKE_GENERATOR in CMake
60+
# # 3.15+.
61+
# if not cmake_generator:
62+
# try:
63+
# import ninja # noqa: F401
64+
65+
# cmake_args += ["-GNinja"]
66+
# except ImportError:
67+
# pass
68+
69+
# else:
70+
71+
# # Single config generators are handled "normally"
72+
# single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
73+
74+
# # CMake allows an arch-in-generator style for backward compatibility
75+
# contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
76+
77+
# # Specify the arch if using MSVC generator, but only if it doesn't
78+
# # contain a backward-compatibility arch spec already in the
79+
# # generator name.
80+
# if not single_config and not contains_arch:
81+
# cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
82+
83+
# # Multi-config generators have a different way to specify configs
84+
# if not single_config:
85+
# cmake_args += [
86+
# f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"
87+
# ]
88+
# build_args += ["--config", cfg]
89+
90+
if sys.platform.startswith("darwin"):
91+
# Cross-compile support for macOS - respect ARCHFLAGS if set
92+
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
93+
if archs:
94+
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
95+
96+
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
97+
# across all generators.
98+
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
99+
# self.parallel is a Python 3 only way to set parallel jobs by hand
100+
# using -j in the build_ext call, not supported by pip or PyPA-build.
101+
if hasattr(self, "parallel") and self.parallel:
102+
# CMake 3.12+ only.
103+
build_args += [f"-j{self.parallel}"]
104+
105+
build_temp = os.path.join(self.build_temp, ext.name)
106+
if not os.path.exists(build_temp):
107+
os.makedirs(build_temp)
108+
109+
subprocess.check_call(["cmake", ext.sourcedir] + cmake_args, cwd=build_temp)
110+
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=build_temp)
111+
112+
ext_modules = [
113+
CMakeExtension("monilog._monilog", sourcedir="src")
114+
]
115+
116+
# ext_modules = [
117+
# Pybind11Extension("monilog._monilog",
118+
# ["src/MoniLog.cc"],
119+
# define_macros = [('VERSION_INFO', __version__)],
120+
# cxx_std=17
121+
# ),
122+
# ]
123+
124+
setup(
125+
name="monilog",
126+
version=__version__,
127+
author="Dorian Leroy",
128+
author_email="[email protected]",
129+
url="https://github.com/cea-hpc/monilog",
130+
description="A Python logging and monitoring toolbox for C++ applications.",
131+
long_description="",
132+
packages=["monilog"],
133+
package_dir={"": "src"},
134+
package_data={
135+
"monilog": ["include/MoniLog.h"],
136+
},
137+
ext_modules=ext_modules,
138+
cmdclass={"build_ext": CMakeBuild},
139+
extras_require={"test": "pytest"},
140+
test_suite='tests',
141+
zip_safe=False,
142+
python_requires=">=3.6",
143+
)

src/CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
cmake_policy(SET CMP0074 NEW)
3+
4+
# SET VARIABLES
5+
set(CMAKE_CXX_COMPILER /usr/bin/g++)
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
8+
set(CMAKE_CXX_FLAGS "-Wall -Wextra -O3")
9+
10+
# PROJECT
11+
project(monilog CXX)
12+
set(CMAKE_VERBOSE_MAKEFILE TRUE)
13+
14+
# CHECK CXX VERSION: must be done after the project() (CMAKE_CXX_COMPILER_ID not defined before)
15+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
16+
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.1.0")
17+
message(FATAL_ERROR "GCC minimum required version is 9.1.0. Please upgrade.")
18+
endif()
19+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
20+
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
21+
message(FATAL_ERROR "Clang minimum required version is 9.0.0. Please upgrade.")
22+
endif()
23+
endif()
24+
25+
set(PYBIND11_PYTHON_VERSION 3.8)
26+
find_package(pybind11 REQUIRED)
27+
include_directories(${pybind11_INCLUDE_DIRS} monilog/include)
28+
29+
add_library(monilog SHARED MoniLog.cc)
30+
target_link_libraries(monilog ${PYTHON_LIBRARIES} pybind11::embed)
31+
32+
# Python extension
33+
pybind11_add_module(_monilog monilog_module.cc)
34+
set_target_properties(_monilog PROPERTIES
35+
BUILD_WITH_INSTALL_RPATH FALSE
36+
LINK_FLAGS "-Wl,-rpath,$ORIGIN/")
37+
target_link_libraries(_monilog PUBLIC monilog)

0 commit comments

Comments
 (0)