-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.cpp
More file actions
131 lines (109 loc) · 4.59 KB
/
Copy pathmod.cpp
File metadata and controls
131 lines (109 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
Copyright (c) 2024, Philip Deegan.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Philip Deegan nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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 "mkn/mod/init.hpp" // IWYU pragma: keep
#include "mkn/kul/os.hpp" // for Dir, WHICH, PushDir
#include "mkn/kul/env.hpp" // for Var, Var::Mode, GET, SET
#include "mkn/kul/dbg.hpp" // for TRACING
#include "mkn/kul/defs.hpp" // for MKN_KUL_PUBLISH
#include "mkn/kul/proc.hpp" // for Process, ProcessCapture, AProcess
#include "mkn/kul/except.hpp" // for Exception, KEXCEPT, KTHROW
#include "mkn/kul/string.hpp" // for String
#include "mkn/kul/log.hpp" // for KOUT, KLOG, KERR
#include <string> // for basic_string, string
#include <vector> // for vector
#include <stdlib.h> // for exit
#include <stdexcept>
namespace YAML {
class Node;
}
namespace mkn::python3 {
kul::File find_python3() KTHROW(std::exception) {
std::string const HOME = kul::env::GET("PYTHON3_HOME");
kul::Dir dir;
if (!HOME.empty()) {
#if defined(_WIN32)
dir = kul::Dir(HOME);
if (!dir) KEXCEPT(kul::Exception, "$PYTHON3_HOME does not exist");
#else
dir = kul::Dir("bin", HOME);
if (!dir) KEXCEPT(kul::Exception, "$PYTHON3_HOME/bin does not exist");
#endif
}
std::vector<std::string> bins{"python3", "python"};
for (auto const& bin : bins)
if (kul::env::WHICH(bin.c_str()))
return dir ? kul::File{bin, dir} : kul::env::WHERE(bin.c_str());
#if defined(_WIN32) // or fallback
std::vector<std::string> exes{"python3.exe", "python.exe"};
for (auto const& bin : exes)
if (kul::env::WHICH(bin.c_str()))
return dir ? kul::File{bin, dir} : kul::env::WHERE(bin.c_str());
#endif
throw std::runtime_error("Could not find python!");
}
kul::env::Var python3_path_var(kul::File const& exe) {
return {"PATH", exe.dir().real(), kul::env::Var::Mode::PREP};
}
static inline kul::File const python_exe = find_python3();
std::string pyexec_for_string(std::string const& cmd) {
auto const path_var = python3_path_var(python_exe);
kul::Process p(python_exe.real());
kul::ProcessCapture pc(p);
p << "-c" << ("\"" + cmd + "\"");
p.var(path_var.name(), path_var.toString());
try {
p.start();
} catch (kul::proc::ExitException const& ex) {
KLOG(ERR) << pc.outs();
KLOG(ERR) << pc.errs();
KERR << ex;
throw;
}
auto ret = kul::String::LINES(pc.outs())[0];
if(ret.back() == '\n') ret.pop_back();
if(ret.back() == '\r') ret.pop_back();
return ret;
}
class ModuleMaker : public mkn::mod::Module {
public:
void init(mkn::mod::Context& ctx, YAML::Node const& /*node*/) KTHROW(std::exception) override;
};
void ModuleMaker::init(mkn::mod::Context& ctx, YAML::Node const& /*node*/) KTHROW(std::exception) {
MKN_KUL_DBG_FUNC_ENTER;
auto const cmd = "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))";
auto info = ctx.state().compileEnv.compilationInfo;
info.lib_ext = pyexec_for_string(cmd);
info.lib_prefix = "";
ctx.compilerState().add(mkn::mod::CompilationInfoInput{info});
ctx.compilerState().add(mkn::mod::BuildModeInput{mkn::mod::Mode::SHAR});
}
} // namespace mkn::python3
extern "C" MKN_KUL_PUBLISH mkn::mod::Module* maiken_module_construct() {
return new mkn::python3::ModuleMaker;
}
extern "C" MKN_KUL_PUBLISH void maiken_module_destruct(mkn::mod::Module* p) { delete p; }