Skip to content

Commit

Permalink
udpates (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan authored Apr 1, 2024
1 parent e98443a commit 848a2f3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 43 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build_osx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ jobs:
curl -Lo mkn https://github.com/mkn/mkn/releases/download/latest/mkn_osx
chmod +x mkn
KLOG=2 ./mkn clean build run -dtOa "-std=c++17 -fPIC"
KLOG=2 ./mkn clean build run -dtOp test -a "-std=c++17 -fPIC"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# lang.pybind11
# python3.module

** python3 pybind11 maiken module **
** python3 maiken module **

[![Travis](https://travis-ci.org/mkn-mod/lang.pybind11.svg?branch=master)](https://travis-ci.org/mkn-mod/lang.pybind11)
Make a python3 module from your code

Link phase module

## Prerequisites
[maiken](https://github.com/Dekken/maiken)
[maiken](https://github.com/mkn/mkn)

## Usage

```yaml
mod:
- name: lang.pybind11
- name: python3.module

```
Expand Down
4 changes: 2 additions & 2 deletions mkn.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

name: lang.pybind11
name: python3.module
version: master
property:
maiken_location: ${MKN_HOME}/app/mkn/${version}
maiken_scm: https://github.com/mkn/mkn

parent: base
src: mod.pybind11.cpp
src: mod.cpp
mode: shared

profile:
Expand Down
66 changes: 37 additions & 29 deletions mod.pybind11.cpp → mod.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
Copyright (c) 2013, Philip Deegan.
Copyright (c) 2024, Philip Deegan.
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -28,60 +28,66 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "maiken/module/init.hpp"
#include <cstdint>
#include <unordered_set>
#include "maiken/module/init.hpp"

namespace mkn {
namespace lang {
namespace python3 {

class PyBind11Module : public maiken::Module {
private:
class ModuleMaker : public maiken::Module {
private:
#if defined(_WIN32)
const bool config_expected = 0;
#else
const bool config_expected = 1;
#endif
bool pyconfig_found = 0;
std::string HOME, PY = "python3", PYTHON, PY_CONFIG = "python-config",
PY3_CONFIG = "python3-config", PATH = mkn::kul::env::GET("PATH");
PY3_CONFIG = "python3-config",
PATH = mkn::kul::env::GET("PATH");
mkn::kul::Dir bin;
std::shared_ptr<kul::cli::EnvVar> path_var;

protected:
static void VALIDATE_NODE(YAML::Node const& node) {
protected:
static void VALIDATE_NODE(YAML::Node const &node) {
using namespace mkn::kul::yaml;
Validator({NodeValidator("args")}).validate(node);
}

public:
void init(maiken::Application& a, YAML::Node const& node)
public:
void init(maiken::Application &a, YAML::Node const &node)
KTHROW(std::exception) override {
bool finally = 0;
if (!kul::env::WHICH(PY.c_str())) PY = "python";
if (!kul::env::WHICH(PY.c_str()))
PY = "python";
PYTHON = mkn::kul::env::GET("PYTHON");
if (!PYTHON.empty()) PY = PYTHON;
if (!PYTHON.empty())
PY = PYTHON;
#if defined(_WIN32)
if (PY.rfind(".exe") == std::string::npos) PY += ".exe";
if (PY.rfind(".exe") == std::string::npos)
PY += ".exe";
#endif
mkn::kul::Process p(PY);
mkn::kul::ProcessCapture pc(p);
HOME = mkn::kul::env::GET("PYTHON3_HOME");
if (!HOME.empty()) {
#if defined(_WIN32)
bin = mkn::kul::Dir(HOME);
if (!bin) KEXCEPT(kul::Exception, "$PYTHON3_HOME does not exist");
if (!bin)
KEXCEPT(kul::Exception, "$PYTHON3_HOME does not exist");
#else
bin = mkn::kul::Dir("bin", HOME);
if (!bin) KEXCEPT(kul::Exception, "$PYTHON3_HOME/bin does not exist");
if (!bin)
KEXCEPT(kul::Exception, "$PYTHON3_HOME/bin does not exist");
#endif
path_var = std::make_shared<kul::cli::EnvVar>("PATH", bin.real(),
mkn::kul::cli::EnvVarMode::PREP);
path_var = std::make_shared<kul::cli::EnvVar>(
"PATH", bin.real(), mkn::kul::cli::EnvVarMode::PREP);
mkn::kul::env::SET(path_var->name(), path_var->toString().c_str());
p.var(path_var->name(), path_var->toString());
};
#if defined(_WIN32)
pyconfig_found = false; // doesn't exist on windows (generally)
pyconfig_found = false; // doesn't exist on windows (generally)
#else
pyconfig_found = mkn::kul::env::WHICH(PY3_CONFIG.c_str());
#endif
Expand All @@ -98,23 +104,25 @@ class PyBind11Module : public maiken::Module {
finally = 1;
KEXCEPT(kul::Exception, "python-config does not exist on path");
}
} catch (const mkn::kul::Exception& e) {
} catch (const mkn::kul::Exception &e) {
KERR << e.stack();
} catch (const std::exception& e) {
} catch (const std::exception &e) {
KERR << e.what();
} catch (...) {
KERR << "UNKNOWN ERROR CAUGHT";
}
if (finally) exit(2);
if (finally)
exit(2);
using namespace mkn::kul::cli;
auto& evs = a.envVars();
auto &evs = a.envVars();
std::string extension;
if (pyconfig_found) {
mkn::kul::os::PushDir pushd(a.project().dir());
mkn::kul::Process p(PY3_CONFIG);
mkn::kul::ProcessCapture pc(p);
p << "--extension-suffix";
if (path_var) p.var(path_var->name(), path_var->toString());
if (path_var)
p.var(path_var->name(), path_var->toString());
p.start();
extension = pc.outs();
} else {
Expand All @@ -126,18 +134,18 @@ class PyBind11Module : public maiken::Module {
p.start();
extension = pc.outs();
}
a.m_cInfo.lib_ext = mkn::kul::String::LINES(extension)[0]; // drop EOL
a.m_cInfo.lib_ext = mkn::kul::String::LINES(extension)[0]; // drop EOL
a.m_cInfo.lib_prefix = "";
a.mode(maiken::compiler::Mode::SHAR);
}
};
} // namespace lang
} // namespace mkn
} // namespace python3
} // namespace mkn

extern "C" KUL_PUBLISH maiken::Module* maiken_module_construct() {
return new mkn::lang::PyBind11Module;
extern "C" KUL_PUBLISH maiken::Module *maiken_module_construct() {
return new mkn::python3::ModuleMaker;
}

extern "C" KUL_PUBLISH void maiken_module_destruct(maiken::Module* p) {
extern "C" KUL_PUBLISH void maiken_module_destruct(maiken::Module *p) {
delete p;
}
13 changes: 7 additions & 6 deletions test.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@


#include <maiken.hpp>
#include "mkn/kul/signal.hpp"
#include "mkn/kul/yaml.hpp"
#include <maiken.hpp>

const std::string yArgs = "";

int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
mkn::kul::Signal sig;
try {
YAML::Node node = mkn::kul::yaml::String(yArgs).root();
char* argv2[2] = {argv[0], (char*)"-O"};
char *argv2[2] = {argv[0], (char *)"-O"};
auto app = (maiken::Application::CREATE(2, argv2))[0];
auto loader(maiken::ModuleLoader::LOAD(*app));
loader->module()->init(*app, node);
loader->module()->compile(*app, node);
loader->module()->link(*app, node);
loader->module()->pack(*app, node);
loader->unload();
for (const auto inc : app->includes()) KLOG(INF) << inc.first;
} catch (const mkn::kul::Exception& e) {
for (const auto inc : app->includes())
KLOG(INF) << inc.first;
} catch (const mkn::kul::Exception &e) {
KLOG(ERR) << e.what();
return 2;
} catch (const std::exception& e) {
} catch (const std::exception &e) {
KERR << e.what();
return 3;
} catch (...) {
Expand Down

0 comments on commit 848a2f3

Please sign in to comment.