version.cpp 5.49 KB
Newer Older
André Anjos's avatar
André Anjos committed
1
2
3
4
5
6
7
/**
 * @author Andre Anjos <andre.anjos@idiap.ch>
 * @date Thu  7 Nov 13:50:16 2013
 *
 * @brief Binds configuration information available from bob
 */

8
9
10
11
12
#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
#include <bob.blitz/capi.h>
#include <bob.blitz/cleanup.h>
André Anjos's avatar
André Anjos committed
13
14
15
16
17
18
19
20
21


#include <string>
#include <cstdlib>
#include <blitz/blitz.h>
#include <boost/preprocessor/stringize.hpp>
#include <boost/version.hpp>
#include <boost/format.hpp>

22
#include <bob.core/config.h>
André Anjos's avatar
André Anjos committed
23
#include <bob.io.base/config.h>
24
#include <bob.math/config.h>
André Anjos's avatar
André Anjos committed
25
26
#include <bob.learn.activation/config.h>
#include <bob.learn.mlp/config.h>
André Anjos's avatar
André Anjos committed
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

static int dict_set(PyObject* d, const char* key, const char* value) {
  PyObject* v = Py_BuildValue("s", value);
  if (!v) return 0;
  int retval = PyDict_SetItemString(d, key, v);
  Py_DECREF(v);
  if (retval == 0) return 1; //all good
  return 0; //a problem occurred
}

static int dict_steal(PyObject* d, const char* key, PyObject* value) {
  if (!value) return 0;
  int retval = PyDict_SetItemString(d, key, value);
  Py_DECREF(value);
  if (retval == 0) return 1; //all good
  return 0; //a problem occurred
}

/**
 * Describes the version of Boost libraries installed
 */
static PyObject* boost_version() {
  boost::format f("%d.%d.%d");
  f % (BOOST_VERSION / 100000);
  f % (BOOST_VERSION / 100 % 1000);
  f % (BOOST_VERSION % 100);
  return Py_BuildValue("s", f.str().c_str());
}

/**
 * Describes the compiler version
 */
static PyObject* compiler_version() {
# if defined(__GNUC__) && !defined(__llvm__)
  boost::format f("%s.%s.%s");
  f % BOOST_PP_STRINGIZE(__GNUC__);
  f % BOOST_PP_STRINGIZE(__GNUC_MINOR__);
  f % BOOST_PP_STRINGIZE(__GNUC_PATCHLEVEL__);
  return Py_BuildValue("ss", "gcc", f.str().c_str());
# elif defined(__llvm__) && !defined(__clang__)
  return Py_BuildValue("ss", "llvm-gcc", __VERSION__);
# elif defined(__clang__)
  return Py_BuildValue("ss", "clang", __clang_version__);
# else
  return Py_BuildValue("s", "unsupported");
# endif
}

/**
 * Python version with which we compiled the extensions
 */
static PyObject* python_version() {
  boost::format f("%s.%s.%s");
  f % BOOST_PP_STRINGIZE(PY_MAJOR_VERSION);
  f % BOOST_PP_STRINGIZE(PY_MINOR_VERSION);
  f % BOOST_PP_STRINGIZE(PY_MICRO_VERSION);
  return Py_BuildValue("s", f.str().c_str());
}

/**
 * Numpy version
 */
static PyObject* numpy_version() {
  return Py_BuildValue("{ssss}", "abi", BOOST_PP_STRINGIZE(NPY_VERSION),
      "api", BOOST_PP_STRINGIZE(NPY_API_VERSION));
}

/**
André Anjos's avatar
André Anjos committed
95
 * bob.blitz c/c++ api version
André Anjos's avatar
André Anjos committed
96
 */
André Anjos's avatar
André Anjos committed
97
98
static PyObject* bob_blitz_version() {
  return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(BOB_BLITZ_API_VERSION));
André Anjos's avatar
André Anjos committed
99
100
101
}

/**
André Anjos's avatar
André Anjos committed
102
 * bob.io.base c/c++ api version
André Anjos's avatar
André Anjos committed
103
 */
André Anjos's avatar
André Anjos committed
104
105
static PyObject* bob_io_base_version() {
  return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(BOB_IO_BASE_API_VERSION));
André Anjos's avatar
André Anjos committed
106
107
108
}

/**
André Anjos's avatar
André Anjos committed
109
 * bob.core c/c++ api version
André Anjos's avatar
André Anjos committed
110
 */
André Anjos's avatar
André Anjos committed
111
112
static PyObject* bob_core_version() {
  return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(BOB_CORE_API_VERSION));
André Anjos's avatar
André Anjos committed
113
114
}

115
116
117
118
119
120
121
/**
 * bob.math c/c++ api version
 */
static PyObject* bob_math_version() {
  return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(BOB_MATH_API_VERSION));
}

André Anjos's avatar
André Anjos committed
122
/**
André Anjos's avatar
André Anjos committed
123
 * bob.learn.activation c/c++ api version
André Anjos's avatar
André Anjos committed
124
 */
André Anjos's avatar
André Anjos committed
125
126
static PyObject* bob_learn_activation_version() {
  return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(BOB_LEARN_ACTIVATION_API_VERSION));
André Anjos's avatar
André Anjos committed
127
128
129
130
131
132
133
134
}

static PyObject* build_version_dictionary() {

  PyObject* retval = PyDict_New();
  if (!retval) return 0;
  auto retval_ = make_safe(retval);

135
  if (!dict_steal(retval, "Bob", bob_core_version())) return 0;
André Anjos's avatar
André Anjos committed
136
137
138
139
140
  if (!dict_set(retval, "Blitz++", BZ_VERSION)) return 0;
  if (!dict_steal(retval, "Boost", boost_version())) return 0;
  if (!dict_steal(retval, "Compiler", compiler_version())) return 0;
  if (!dict_steal(retval, "Python", python_version())) return 0;
  if (!dict_steal(retval, "NumPy", numpy_version())) return 0;
André Anjos's avatar
André Anjos committed
141
142
  if (!dict_steal(retval, "bob.blitz", bob_blitz_version())) return 0;
  if (!dict_steal(retval, "bob.core", bob_core_version())) return 0;
143
144
  if (!dict_steal(retval, "bob.math", bob_math_version())) return 0;
  if (!dict_steal(retval, "bob.io.base", bob_io_base_version())) return 0;
André Anjos's avatar
André Anjos committed
145
  if (!dict_steal(retval, "bob.learn.activation", bob_learn_activation_version())) return 0;
André Anjos's avatar
André Anjos committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

  Py_INCREF(retval);
  return retval;
}

static PyMethodDef module_methods[] = {
    {0}  /* Sentinel */
};

PyDoc_STRVAR(module_docstr,
"Information about software used to compile the C++ Bob API"
);

#if PY_VERSION_HEX >= 0x03000000
static PyModuleDef module_definition = {
  PyModuleDef_HEAD_INIT,
André Anjos's avatar
André Anjos committed
162
  BOB_EXT_MODULE_NAME,
André Anjos's avatar
André Anjos committed
163
164
165
166
167
168
169
170
171
172
173
174
  module_docstr,
  -1,
  module_methods,
  0, 0, 0, 0
};
#endif

static PyObject* create_module (void) {

# if PY_VERSION_HEX >= 0x03000000
  PyObject* m = PyModule_Create(&module_definition);
# else
André Anjos's avatar
André Anjos committed
175
  PyObject* m = Py_InitModule3(BOB_EXT_MODULE_NAME, module_methods, module_docstr);
André Anjos's avatar
André Anjos committed
176
177
178
179
180
# endif
  if (!m) return 0;
  auto m_ = make_safe(m); ///< protects against early returns

  /* register version numbers and constants */
André Anjos's avatar
André Anjos committed
181
  if (PyModule_AddIntConstant(m, "api", BOB_LEARN_MLP_API_VERSION) < 0)
André Anjos's avatar
André Anjos committed
182
    return 0;
André Anjos's avatar
André Anjos committed
183
  if (PyModule_AddStringConstant(m, "module", BOB_EXT_MODULE_VERSION) < 0)
André Anjos's avatar
André Anjos committed
184
185
186
187
188
189
    return 0;

  PyObject* externals = build_version_dictionary();
  if (!externals) return 0;
  if (PyModule_AddObject(m, "externals", externals) < 0) return 0;

190
  /* imports dependencies */
André Anjos's avatar
André Anjos committed
191
  if (import_bob_blitz() < 0) {
192
    PyErr_Print();
André Anjos's avatar
André Anjos committed
193
    PyErr_Format(PyExc_ImportError, "cannot import `%s'", BOB_EXT_MODULE_NAME);
194
195
    return 0;
  }
André Anjos's avatar
André Anjos committed
196
197
198
199
200
201

  Py_INCREF(m);
  return m;

}

André Anjos's avatar
André Anjos committed
202
PyMODINIT_FUNC BOB_EXT_ENTRY_NAME (void) {
André Anjos's avatar
André Anjos committed
203
204
205
206
207
# if PY_VERSION_HEX >= 0x03000000
  return
# endif
    create_module();
}