From: Michael Tremer Date: Mon, 1 Mar 2021 16:46:22 +0000 (+0000) Subject: builder: Implement reading makefiles X-Git-Tag: 0.9.28~1285^2~677 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4cfcbaa4165cd599d7aaba202881b060aa035ea;p=pakfire.git builder: Implement reading makefiles Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 503923564..68a766330 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -33,6 +33,7 @@ #include "errors.h" #include "key.h" #include "pakfire.h" +#include "parser.h" #include "repo.h" #include "util.h" @@ -536,6 +537,43 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* Py_RETURN_NONE; } +static PyObject* Pakfire_read_makefile(PakfireObject* self, PyObject* args) { + const char* path = NULL; + + if (!PyArg_ParseTuple(args, "s", &path)) + return NULL; + + PakfireParser parser = NULL; + struct pakfire_parser_error* error = NULL; + + int r = pakfire_read_makefile(&parser, self->pakfire, path, &error); + + // The parser could not parse the makefile + if (r == 1) { + PyErr_SetString(PyExc_SyntaxError, pakfire_parser_error_get_message(error)); + PyErr_SyntaxLocation( + pakfire_parser_error_get_filename(error), + pakfire_parser_error_get_line(error) + ); + return NULL; + + // Handle any other internal error + } else if (r < 0) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + + PyObject* ret = new_parser(parser); + if (!ret) + return NULL; + + pakfire_parser_unref(parser); + if (error) + pakfire_parser_error_unref(error); + + return ret; +} + static struct PyMethodDef Pakfire_methods[] = { { "execute", @@ -573,6 +611,12 @@ static struct PyMethodDef Pakfire_methods[] = { METH_VARARGS, NULL }, + { + "read_makefile", + (PyCFunction)Pakfire_read_makefile, + METH_VARARGS, + NULL + }, { "search", (PyCFunction)Pakfire_search, diff --git a/src/libpakfire/include/pakfire/constants.h b/src/libpakfire/include/pakfire/constants.h index 9404b3a3a..a6bef7260 100644 --- a/src/libpakfire/include/pakfire/constants.h +++ b/src/libpakfire/include/pakfire/constants.h @@ -27,4 +27,7 @@ #define CACHE_PATH "/var/cache/pakfire" +#define PAKFIRE_MACROS_DIR "/usr/lib/pakfire/macros" +#define PAKFIRE_MACROS_GLOB_PATTERN PAKFIRE_MACROS_DIR "/*.macro" + #endif /* PAKFIRE_CONSTANTS_H */ diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index c433cda48..2fd2df5d4 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -26,6 +26,7 @@ #include #include +#include #include int pakfire_create(Pakfire* pakfire, const char* path, const char* arch); @@ -77,6 +78,11 @@ void pakfire_log_set_function(Pakfire pakfire, pakfire_log_function_t log_functi int pakfire_log_get_priority(Pakfire pakfire); void pakfire_log_set_priority(Pakfire pakfire, int priority); +// Build + +int pakfire_read_makefile(PakfireParser* parser, Pakfire pakfire, const char* path, + struct pakfire_parser_error** error); + #ifdef PAKFIRE_PRIVATE #include diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 905c9a0fb..69a17d329 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -37,6 +37,7 @@ global: pakfire_get_pool; pakfire_get_repo; pakfire_make_path; + pakfire_read_makefile; pakfire_ref; pakfire_search; pakfire_set_cache_path; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index efd9acf47..824c2c6ec 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -43,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -773,3 +775,74 @@ PAKFIRE_EXPORT void pakfire_log(Pakfire pakfire, int priority, const char* file, // Restore errno errno = saved_errno; } + +PAKFIRE_EXPORT int pakfire_read_makefile(PakfireParser* parser, Pakfire pakfire, + const char* path, struct pakfire_parser_error** error) { + int r = 1; + + *parser = pakfire_parser_create(pakfire, NULL, NULL); + if (!*parser) { + r = 1; + goto ERROR; + } + + // XXX set defaults + + // Find all macros + char* macros = pakfire_make_path(pakfire, PAKFIRE_MACROS_GLOB_PATTERN); + if (!macros) + goto ERROR; + + DEBUG(pakfire, "Searching for macros in %s\n", macros); + + glob_t globmacros; + r = glob(macros, 0, NULL, &globmacros); + free(macros); + + // Handle any errors + switch (r) { + case 0: + case GLOB_NOMATCH: + break; + + case GLOB_NOSPACE: + errno = ENOMEM; + goto ERROR; + + case GLOB_ABORTED: + goto ERROR; + + default: + ERROR(pakfire, "glob() returned an unhandled error: %d\n", r); + goto ERROR; + } + + DEBUG(pakfire, "Found %zu macro(s)\n", globmacros.gl_pathc); + + // Read all macros + for (unsigned int i = 0; i < globmacros.gl_pathc; i++) { + // Parse the file + r = pakfire_parser_read_file(*parser, globmacros.gl_pathv[i], error); + if (r) + goto ERROR; + } + + globfree(&globmacros); + + // Finally, parse the makefile + r = pakfire_parser_read_file(*parser, path, error); + if (r) + goto ERROR; + + return 0; + +ERROR: + globfree(&globmacros); + + if (*parser) { + pakfire_parser_unref(*parser); + *parser = NULL; + } + + return r; +} diff --git a/src/pakfire/builder.py b/src/pakfire/builder.py index 438714588..e4583b083 100644 --- a/src/pakfire/builder.py +++ b/src/pakfire/builder.py @@ -317,7 +317,7 @@ class BuilderContext(object): # Run the transaction transaction.run() - def build(self, package, shell=True): + def build(self, path, shell=True): # Install build environment packages = [ "@Build", @@ -328,7 +328,10 @@ class BuilderContext(object): packages.append("ccache") # Open the package archive - archive = _pakfire.Archive(self.pakfire, package) + archive = _pakfire.Archive(self.pakfire, path) + + # Parse all metadata + package = archive.get_package() requires = archive.get("dependencies.requires") if requires: @@ -339,7 +342,13 @@ class BuilderContext(object): # Extract the source archive (if we have one) if archive: - archive.extract("%s/build" % self.pakfire.path) + archive.extract("%s/usr/src" % self.pakfire.path) + + # Read the makefile + makefile = self.pakfire.read_makefile("%s/usr/src/%s/%s.nm" \ + % (self.pakfire.path, package, package.name)) + + print(makefile) def shell(self, packages=[], install=None): archives = []