Skip to content

Commit d6db77a

Browse files
committed
wip
1 parent 61fcb57 commit d6db77a

1 file changed

Lines changed: 37 additions & 66 deletions

File tree

mod.cpp

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,35 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2828
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
31-
#include <cstdint>
32-
#include <unordered_set>
31+
#include "maiken/module/init.hpp" // IWYU pragma: keep
3332

34-
#include "maiken/module/init.hpp"
33+
#include "maiken/app.hpp" // for Application
34+
#include "maiken/compiler.hpp" // for CompilationInfo, Mode
35+
36+
#include "mkn/kul/os.hpp" // for Dir, WHICH, PushDir
37+
#include "mkn/kul/cli.hpp" // for EnvVar, EnvVarMode
38+
#include "mkn/kul/env.hpp" // for GET, SET
39+
#include "mkn/kul/defs.hpp" // for MKN_KUL_PUBLISH
40+
#include "mkn/kul/proc.hpp" // for Process, ProcessCapture, AProcess
41+
#include "maiken/project.hpp" // for Project
42+
#include "mkn/kul/except.hpp" // for Exception, KEXCEPT, KTHROW
43+
#include "mkn/kul/string.hpp" // for String
44+
45+
#include <memory> // for shared_ptr, make_shared
46+
#include <string> // for basic_string, string
47+
#include <vector> // for vector
48+
#include <stdlib.h> // for exit
49+
50+
namespace YAML {
51+
class Node;
52+
}
3553

3654
namespace mkn {
3755
namespace python3 {
3856

3957
class ModuleMaker : public maiken::Module {
40-
private:
41-
#if defined(_WIN32)
42-
const bool config_expected = 0;
43-
#else
44-
const bool config_expected = 1;
45-
#endif
46-
bool pyconfig_found = 0;
47-
std::string HOME, PY = "python3", PYTHON, PY_CONFIG = "python-config",
48-
PY3_CONFIG = "python3-config", PATH = mkn::kul::env::GET("PATH");
49-
mkn::kul::Dir bin;
50-
std::shared_ptr<kul::cli::EnvVar> path_var;
51-
52-
protected:
53-
static void VALIDATE_NODE(YAML::Node const& node) {
54-
using namespace mkn::kul::yaml;
55-
Validator({NodeValidator("args")}).validate(node);
56-
}
57-
5858
public:
5959
void init(maiken::Application& a, YAML::Node const& /*node*/) KTHROW(std::exception) override {
60-
bool finally = 0;
6160
if (!kul::env::WHICH(PY.c_str())) PY = "python";
6261
PYTHON = mkn::kul::env::GET("PYTHON");
6362
if (!PYTHON.empty()) PY = PYTHON;
@@ -80,56 +79,28 @@ class ModuleMaker : public maiken::Module {
8079
mkn::kul::env::SET(path_var->name(), path_var->toString().c_str());
8180
p.var(path_var->name(), path_var->toString());
8281
};
83-
#if defined(_WIN32)
84-
pyconfig_found = false; // doesn't exist on windows (generally)
85-
#else
86-
pyconfig_found = mkn::kul::env::WHICH(PY3_CONFIG.c_str());
87-
#endif
88-
if (!pyconfig_found) {
89-
pyconfig_found = mkn::kul::env::WHICH(PY_CONFIG.c_str());
90-
PY3_CONFIG = PY_CONFIG;
91-
}
92-
try {
93-
p << "-c"
94-
<< "\"import sys; print(sys.version_info[0])\"";
95-
p.start();
9682

97-
if (!pyconfig_found && config_expected) {
98-
finally = 1;
99-
KEXCEPT(kul::Exception, "python-config does not exist on path");
100-
}
101-
} catch (mkn::kul::Exception const& e) {
102-
KERR << e.stack();
103-
} catch (std::exception const& e) {
104-
KERR << e.what();
105-
} catch (...) {
106-
KERR << "UNKNOWN ERROR CAUGHT";
107-
}
108-
if (finally) exit(2);
109-
using namespace mkn::kul::cli;
83+
auto const extension =
84+
pyexec_for_string("\"import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))\"");
11085

111-
std::string extension;
112-
if (pyconfig_found) {
113-
mkn::kul::os::PushDir pushd(a.project().dir());
114-
mkn::kul::Process p(PY3_CONFIG);
115-
mkn::kul::ProcessCapture pc(p);
116-
p << "--extension-suffix";
117-
if (path_var) p.var(path_var->name(), path_var->toString());
118-
p.start();
119-
extension = pc.outs();
120-
} else {
121-
mkn::kul::Process p(PY);
122-
mkn::kul::ProcessCapture pc(p);
123-
p << "-c"
124-
<< "\"import sysconfig; "
125-
"print(sysconfig.get_config_var('EXT_SUFFIX'))\"";
126-
p.start();
127-
extension = pc.outs();
128-
}
12986
a.m_cInfo.lib_ext = mkn::kul::String::LINES(extension)[0]; // drop EOL
13087
a.m_cInfo.lib_prefix = "";
13188
a.mode(maiken::compiler::Mode::SHAR);
13289
}
90+
91+
private:
92+
std::string pyexec_for_string(std::string const& cmd) {
93+
mkn::kul::Process p(PY);
94+
mkn::kul::ProcessCapture pc(p);
95+
p << "-c" << cmd;
96+
p.start();
97+
return pc.outs();
98+
}
99+
100+
std::string HOME, PY = "python3", PYTHON, PY_CONFIG = "python-config",
101+
PY3_CONFIG = "python3-config", PATH = mkn::kul::env::GET("PATH");
102+
mkn::kul::Dir bin;
103+
std::shared_ptr<kul::cli::EnvVar> path_var;
133104
};
134105
} // namespace python3
135106
} // namespace mkn

0 commit comments

Comments
 (0)