From 8aed1fcd443b550c15a21ddbf1b1d3899803120a Mon Sep 17 00:00:00 2001 From: Waldemar Brodkorb Date: Sun, 30 Mar 2014 15:55:20 +0200 Subject: rework hosttools building, add tools into package stuff --- adk/tools/Makefile | 17 + adk/tools/depmaker.c | 314 +++++++++++++ adk/tools/dkgetsz.c | 95 ++++ adk/tools/pkgmaker.c | 1194 ++++++++++++++++++++++++++++++++++++++++++++++++ adk/tools/pkgrebuild.c | 273 +++++++++++ adk/tools/sortfile.c | 153 +++++++ adk/tools/sortfile.h | 1 + adk/tools/strmap.c | 510 +++++++++++++++++++++ adk/tools/strmap.h | 350 ++++++++++++++ 9 files changed, 2907 insertions(+) create mode 100644 adk/tools/Makefile create mode 100644 adk/tools/depmaker.c create mode 100644 adk/tools/dkgetsz.c create mode 100644 adk/tools/pkgmaker.c create mode 100644 adk/tools/pkgrebuild.c create mode 100644 adk/tools/sortfile.c create mode 100644 adk/tools/sortfile.h create mode 100644 adk/tools/strmap.c create mode 100644 adk/tools/strmap.h (limited to 'adk/tools') diff --git a/adk/tools/Makefile b/adk/tools/Makefile new file mode 100644 index 000000000..edd559f85 --- /dev/null +++ b/adk/tools/Makefile @@ -0,0 +1,17 @@ +# This file is part of the OpenADK project. OpenADK is copyrighted +# material, please see the LICENCE file in the top-level directory. + +include $(TOPDIR)/rules.mk + +install: ${STAGING_HOST_DIR}/usr/bin/depmaker ${STAGING_HOST_DIR}/usr/bin/pkgrebuild ${STAGING_HOST_DIR}/usr/bin/dkgetsz + +${STAGING_HOST_DIR}/usr/bin/depmaker: depmaker.c + ${CC_FOR_BUILD} ${FLAGS_FOR_BUILD} -o $@ depmaker.c + +${STAGING_HOST_DIR}/usr/bin/pkgrebuild: pkgrebuild.c strmap.c + ${CC_FOR_BUILD} ${FLAGS_FOR_BUILD} -o $@ pkgrebuild.c strmap.c + +${STAGING_HOST_DIR}/usr/bin/dkgetsz: dkgetsz.c + ${CC_FOR_BUILD} ${FLAGS_FOR_BUILD} -o $@ dkgetsz.c + +include $(TOPDIR)/mk/tools.mk diff --git a/adk/tools/depmaker.c b/adk/tools/depmaker.c new file mode 100644 index 000000000..23d2d2898 --- /dev/null +++ b/adk/tools/depmaker.c @@ -0,0 +1,314 @@ +/* + * depmaker - create package/Depends.mk for OpenADK buildsystem + * + * Copyright (C) 2010-2014 Waldemar Brodkorb + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#define MAXLINE 1024 +#define MAXPATH 128 + +static int prefix = 0; +static int hprefix = 0; + +static int check_symbol(char *symbol) { + + FILE *config; + char buf[MAXLINE]; + char *sym; + int ret; + + if ((sym = malloc(strlen(symbol) + 2)) != NULL) + memset(sym, 0, strlen(symbol) + 2); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + + strncat(sym, symbol, strlen(symbol)); + strncat(sym, "=", 1); + if ((config = fopen(".config", "r")) == NULL) { + perror("Can not open file \".config\"."); + exit(EXIT_FAILURE); + } + + ret = 1; + while (fgets(buf, MAXLINE, config) != NULL) { + if (strncmp(buf, sym, strlen(sym)) == 0) + ret = 0; + } + + free(sym); + if (fclose(config) != 0) + perror("Closing file stream failed"); + + return(ret); +} + +/*@null@*/ +static char *parse_line(char *package, char *pkgvar, char *string, int checksym, int pprefix, int system, int *prefixp) { + + char *key, *value, *dep, *key_sym, *pkgdeps; + char temp[MAXLINE]; + + string[strlen(string)-1] = '\0'; + if ((key = strtok(string, ":=")) == NULL) { + perror("Can not get key from string."); + exit(EXIT_FAILURE); + } + + if (checksym == 1) { + /* extract symbol */ + if ((key_sym = malloc(MAXLINE)) != NULL) + memset(key_sym, 0, MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + if (system == 0) { + if (pprefix == 0) { + if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_%s_", pkgvar) < 0) + perror("Can not create string variable."); + } else { + if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_") < 0) + perror("Can not create string variable."); + } + strncat(key_sym, key+6, strlen(key)-6); + } else { + if (snprintf(key_sym, MAXLINE, "ADK_TARGET_SYSTEM_%s", pkgvar) < 0) + perror("Can not create string variable."); + } + + if (check_symbol(key_sym) != 0) { + free(key_sym); + return(NULL); + } + free(key_sym); + } + + if ((pkgdeps = malloc(MAXLINE)) != NULL) + memset(pkgdeps, 0, MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + + value = strtok(NULL, "=\t"); + dep = strtok(value, " "); + while (dep != NULL) { + if (*prefixp == 0) { + *prefixp = 1; + if (snprintf(temp, MAXLINE, "%s-compile: %s-compile", package, dep) < 0) + perror("Can not create string variable."); + } else { + if (snprintf(temp, MAXLINE, " %s-compile", dep) < 0) + perror("Can not create string variable."); + } + strncat(pkgdeps, temp, strlen(temp)); + dep = strtok(NULL, " "); + } + return(pkgdeps); +} + +int main() { + + DIR *pkgdir; + struct dirent *pkgdirp; + FILE *pkg; + char buf[MAXLINE]; + char path[MAXPATH]; + char *string, *pkgvar, *pkgdeps, *hpkgdeps = NULL, *tmp, *fpkg, *cpkg, *spkg, *key, *check, *dpkg; + char *stringtmp; + int i; + + spkg = NULL; + cpkg = NULL; + fpkg = NULL; + + /* read Makefile's for all packages */ + pkgdir = opendir("package"); + while ((pkgdirp = readdir(pkgdir)) != NULL) { + /* skip dotfiles */ + if (strncmp(pkgdirp->d_name, ".", 1) > 0) { + if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0) + perror("Can not create string variable."); + pkg = fopen(path, "r"); + if (pkg == NULL) + continue; + + /* transform to uppercase variable name */ + pkgvar = strdup(pkgdirp->d_name); + for (i=0; i<(int)strlen(pkgvar); i++) { + if (pkgvar[i] == '+') + pkgvar[i] = 'X'; + if (pkgvar[i] == '-') + pkgvar[i] = '_'; + pkgvar[i] = toupper(pkgvar[i]); + } + + /* exclude manual maintained packages from package/Makefile */ + if ( + !(strncmp(pkgdirp->d_name, "libpthread", 10) == 0 && strlen(pkgdirp->d_name) == 10) && + !(strncmp(pkgdirp->d_name, "uclibc++", 8) == 0) && + !(strncmp(pkgdirp->d_name, "uclibc", 6) == 0) && + !(strncmp(pkgdirp->d_name, "musl", 4) == 0) && + !(strncmp(pkgdirp->d_name, "glibc", 5) == 0)) { + /* print result to stdout */ + printf("package-$(ADK_COMPILE_%s) += %s\n", pkgvar, pkgdirp->d_name); + printf("hostpackage-$(ADK_HOST_NEED_%s) += %s\n", pkgvar, pkgdirp->d_name); + } + + if ((pkgdeps = malloc(MAXLINE)) != NULL) + memset(pkgdeps, 0, MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + prefix = 0; + hprefix = 0; + + /* generate build dependencies */ + while (fgets(buf, MAXLINE, pkg) != NULL) { + if ((tmp = malloc(MAXLINE)) != NULL) + memset(tmp, 0 , MAXLINE); + else { + perror("Can not allocate memory."); + exit(EXIT_FAILURE); + } + + /* just read variables prefixed with PKG */ + if (strncmp(buf, "PKG", 3) == 0) { + + string = strstr(buf, "PKG_BUILDDEP:="); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix); + if (tmp != NULL) { + strncat(pkgdeps, tmp, strlen(tmp)); + } + } + + string = strstr(buf, "PKG_BUILDDEP+="); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + + // We need to find the system name here + string = strstr(buf, "PKG_BUILDDEP_"); + if (string != NULL) { + check = strstr(buf, ":="); + if (check != NULL) { + stringtmp = strdup(string); + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + dpkg = strdup(key+13); + tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 1, &prefix); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + } + + // We need to find the subpackage name here + string = strstr(buf, "PKG_FLAVOURS_"); + if (string != NULL) { + check = strstr(buf, ":="); + if (check != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + fpkg = strdup(key+13); + } + } + + string = strstr(buf, "PKGFB_"); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, fpkg, string, 1, 0, 0, &prefix); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + + // We need to find the subpackage name here + string = strstr(buf, "PKG_CHOICES_"); + if (string != NULL) { + check = strstr(buf, ":="); + if (check != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + cpkg = strdup(key+12); + } + } + string = strstr(buf, "PKGCB_"); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, cpkg, string, 1, 0, 0, &prefix); + if (tmp != NULL) + strncat(pkgdeps, tmp, strlen(tmp)); + } + + // We need to find the subpackage name here + string = strstr(buf, "PKG_SUBPKGS_"); + if (string != NULL) { + check = strstr(buf, ":="); + if (check != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + spkg = strdup(key+12); + } + } + + string = strstr(buf, "PKGSB_"); + if (string != NULL) { + tmp = parse_line(pkgdirp->d_name, spkg, string, 1, 1, 0, &prefix); + if (tmp != NULL) { + strncat(pkgdeps, tmp, strlen(tmp)); + } + } + } else if (strncmp(buf, "HOST_BUILDDEP", 13) == 0) { + asprintf(&string, "%s-host", pkgdirp->d_name); + // check retval; string for NULL + tmp = parse_line(string, NULL, buf, 0, 0, 0, &hprefix); + if (tmp && *tmp) { + asprintf(&string, "%s%s", + hpkgdeps ? hpkgdeps : "", + tmp); + free(hpkgdeps); + hpkgdeps = string; + } + } + free(tmp); + } + if (strlen(pkgdeps) != 0) + printf("%s\n", pkgdeps); + if (hpkgdeps && *hpkgdeps) + printf("%s\n", hpkgdeps); + free(hpkgdeps); + hpkgdeps = NULL; + free(pkgdeps); + free(pkgvar); + if (fclose(pkg) != 0) + perror("Closing file stream failed"); + } + } + if (closedir(pkgdir) != 0) + perror("Closing directory stream failed"); + + return(0); +} diff --git a/adk/tools/dkgetsz.c b/adk/tools/dkgetsz.c new file mode 100644 index 000000000..b8315be70 --- /dev/null +++ b/adk/tools/dkgetsz.c @@ -0,0 +1,95 @@ +/*- + * Copyright © 2010 + * Waldemar Brodkorb + * Thorsten Glaser + * + * Provided that these terms and disclaimer and all copyright notices + * are retained or reproduced in an accompanying document, permission + * is granted to deal in this work without restriction, including un‐ + * limited rights to use, publicly perform, distribute, sell, modify, + * merge, give away, or sublicence. + * + * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to + * the utmost extent permitted by applicable law, neither express nor + * implied; without malicious intent or gross negligence. In no event + * may a licensor, author or contributor be held liable for indirect, + * direct, other damage, loss, or other issues arising in any way out + * of dealing in the work, even if advised of the possibility of such + * damage or existence of a defect, except proven that it results out + * of said person’s immediate fault when using the work as intended. + * + * Alternatively, this work may be distributed under the terms of the + * General Public License, any version, as published by the Free Soft- + * ware Foundation. + *- + * Display the size of a block device (e.g. USB stick, CF/SF/MMC card + * or hard disc) in 512-byte sectors. + */ + +#define _FILE_OFFSET_BITS 64 + +#include +#include +#include +#include + +#if defined(__APPLE__) +#include +#endif + +#if defined(DIOCGDINFO) +#include +#endif + +#include +#include +#include +#include + +unsigned long long numsecs(int); + +int +main(int argc, char *argv[]) { + int fd; + + if (argc != 2) + errx(255, "Syntax: dkgetsz /dev/sda"); + + if ((fd = open(argv[1], O_RDONLY)) == -1) + err(1, "open"); + printf("%llu\n", numsecs(fd)); + close(fd); + return (0); +} + +unsigned long long +numsecs(int fd) +{ +#if defined(BLKGETSIZE) || defined(DKIOCGETBLOCKCOUNT) +/* + * note: BLKGETSIZE64 returns bytes, not sectors, but the return + * type is size_t which is 32 bits on an ILP32 platform, so it + * fails interestingly here… thus we use BLKGETSIZE instead. + */ +#if defined(DKIOCGETBLOCKCOUNT) + uint64_t nsecs; +#define THEIOCTL DKIOCGETBLOCKCOUNT +#define STRIOCTL "DKIOCGETBLOCKCOUNT" +#else + unsigned long nsecs; +#define THEIOCTL BLKGETSIZE +#define STRIOCTL "BLKGETSIZE" +#endif + if (ioctl(fd, THEIOCTL, &nsecs) == -1) + err(1, "ioctl %s", STRIOCTL); + return ((unsigned long long)nsecs); +#elif defined(DIOCGDINFO) + struct disklabel dl; + + if (ioctl(fd, DIOCGDINFO, &dl) == -1) + err(1, "ioctl DIOCGDINFO"); + return ((unsigned long long)dl.d_secperunit); +#else +#warning PLEASE DO IMPLEMENT numsecs FOR THIS PLATFORM. +#endif +} diff --git a/adk/tools/pkgmaker.c b/adk/tools/pkgmaker.c new file mode 100644 index 000000000..51d31aa70 --- /dev/null +++ b/adk/tools/pkgmaker.c @@ -0,0 +1,1194 @@ +/* + * pkgmaker - create package meta-data for OpenADK buildsystem + * + * Copyright (C) 2010-2014 Waldemar Brodkorb + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sortfile.h" +#include "strmap.h" + +#define MAXLINE 4096 +#define MAXVALUE 168 +#define MAXVAR 64 +#define MAXPATH 320 +#define HASHSZ 96 + +static int nobinpkgs; + +#define fatal_error(...) { \ + fprintf(stderr, "Fatal error. "); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + exit(1); \ +} + +static int parse_var_hash(char *buf, const char *varname, StrMap *strmap) { + + char *key, *value, *string; + + string = strstr(buf, varname); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + value = strtok(NULL, "=\t"); + if (value != NULL) + strmap_put(strmap, key, value); + return(0); + } + return(1); +} + +static int parse_var(char *buf, const char *varname, char *pvalue, char **result) { + + char *pkg_var; + char *key, *value, *string; + char pkg_str[MAXVAR]; + + if ((pkg_var = malloc(MAXLINE)) != NULL) + memset(pkg_var, 0, MAXLINE); + else { + perror("Can not allocate memory"); + exit(EXIT_FAILURE); + } + + if (snprintf(pkg_str, MAXVAR, "%s:=", varname) < 0) + perror("can not create path variable."); + string = strstr(buf, pkg_str); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + value = strtok(NULL, "=\t"); + if (value != NULL) { + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + } else { + nobinpkgs = 1; + *result = NULL; + } + free(pkg_var); + return(0); + } else { + if (snprintf(pkg_str, MAXVAR, "%s+=", varname) < 0) + perror("can not create path variable."); + string = strstr(buf, pkg_str); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, "+="); + value = strtok(NULL, "=\t"); + if (pvalue != NULL) + strncat(pkg_var, pvalue, strlen(pvalue)); + strncat(pkg_var, " ", 1); + if (value != NULL) + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + free(pkg_var); + return(0); + } + } + free(pkg_var); + return(1); +} + +static int parse_var_with_system(char *buf, const char *varname, char *pvalue, char **result, char **sysname, int varlen) { + + char *pkg_var, *check; + char *key, *value, *string; + + if ((pkg_var = malloc(MAXLINE)) != NULL) + memset(pkg_var, 0, MAXLINE); + else { + perror("Can not allocate memory"); + exit(EXIT_FAILURE); + } + + check = strstr(buf, ":="); + if (check != NULL) { + string = strstr(buf, varname); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + *sysname = strdup(key+varlen); + value = strtok(NULL, "=\t"); + if (value != NULL) { + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + } + free(pkg_var); + return(0); + } + } else { + string = strstr(buf, varname); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, "+="); + value = strtok(NULL, "=\t"); + if (pvalue != NULL) + strncat(pkg_var, pvalue, strlen(pvalue)); + strncat(pkg_var, " ", 1); + if (value != NULL) + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + free(pkg_var); + return(0); + } + } + free(pkg_var); + return(1); +} + +static int parse_var_with_pkg(char *buf, const char *varname, char *pvalue, char **result, char **pkgname, int varlen) { + + char *pkg_var, *check; + char *key, *value, *string; + + if ((pkg_var = malloc(MAXLINE)) != NULL) + memset(pkg_var, 0, MAXLINE); + else { + perror("Can not allocate memory"); + exit(EXIT_FAILURE); + } + + check = strstr(buf, ":="); + if (check != NULL) { + string = strstr(buf, varname); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + *pkgname = strdup(key+varlen); + value = strtok(NULL, "=\t"); + if (value != NULL) { + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + } + free(pkg_var); + return(0); + } + } else { + string = strstr(buf, varname); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, "+="); + value = strtok(NULL, "=\t"); + if (pvalue != NULL) + strncat(pkg_var, pvalue, strlen(pvalue)); + strncat(pkg_var, " ", 1); + if (value != NULL) + strncat(pkg_var, value, strlen(value)); + *result = strdup(pkg_var); + free(pkg_var); + return(0); + } + } + free(pkg_var); + return(1); +} + +#if 0 +static void iter_debug(const char *key, const char *value, const void *obj) { + fprintf(stderr, "HASHMAP key: %s value: %s\n", key, value); +} +#endif + +static int hash_str(char *string) { + + int i; + int hash; + + hash = 0; + for (i=0; i<(int)strlen(string); i++) { + hash += string[i]; + } + return(hash); +} + +static void iter(const char *key, const char *value, const void *obj) { + + FILE *config, *section; + int hash; + char *valuestr, *pkg, *subpkg; + char buf[MAXPATH]; + char infile[MAXPATH]; + char outfile[MAXPATH]; + + valuestr = strdup(value); + config = fopen("package/Config.in.auto", "a"); + if (config == NULL) + fatal_error("Can not open file package/Config.in.auto"); + + hash = hash_str(valuestr); + snprintf(infile, MAXPATH, "package/pkglist.d/sectionlst.%d", hash); + snprintf(outfile, MAXPATH, "package/pkglist.d/sectionlst.%d.sorted", hash); + + if (access(infile, F_OK) == 0) { + valuestr[strlen(valuestr)-1] = '\0'; + fprintf(config, "menu \"%s\"\n", valuestr); + sortfile(infile, outfile); + /* avoid duplicate section entries */ + unlink(infile); + section = fopen(outfile, "r"); + while (fgets(buf, MAXPATH, section) != NULL) { + buf[strlen(buf)-1] = '\0'; + if (buf[strlen(buf)-1] == '@') { + buf[strlen(buf)-1] = '\0'; + fprintf(config, "source \"package/%s/Config.in.manual\"\n", buf); + } else { + subpkg = strtok(buf, "|"); + subpkg[strlen(subpkg)-1] = '\0'; + pkg = strtok(NULL, "|"); + fprintf(config, "source \"package/pkgconfigs.d/%s/Config.in.%s\"\n", pkg, subpkg); + } + } + fprintf(config, "endmenu\n\n"); + fclose(section); + } + fclose(config); +} + +static char *tolowerstr(char *string) { + + int i; + char *str; + + /* transform to lowercase variable name */ + str = strdup(string); + for (i=0; i<(int)strlen(str); i++) { + if (str[i] == '_') + str[i] = '-'; + str[i] = tolower(str[i]); + } + return(str); +} + +static char *toupperstr(char *string) { + + int i; + char *str; + + /* transform to uppercase variable name */ + str = strdup(string); + for (i=0; i<(int)strlen(str); i++) { + if (str[i] == '+') + str[i] = 'X'; + if (str[i] == '-') + str[i] = '_'; + /* remove negation here, useful for package host depends */ + if (str[i] == '!') + str[i] = '_'; + str[i] = toupper(str[i]); + } + return(str); +} + + +int main() { + + DIR *pkgdir, *pkglistdir; + struct dirent *pkgdirp; + FILE *pkg, *cfg, *menuglobal, *section; + char hvalue[MAXVALUE]; + char buf[MAXPATH]; + char tbuf[MAXPATH]; + char path[MAXPATH]; + char spath[MAXPATH]; + char dir[MAXPATH]; + char variable[2*MAXVAR]; + char *key, *value, *token, *cftoken, *sp, *hkey, *val, *pkg_fd; + char *pkg_name, *pkg_depends, *pkg_depends_system, *pkg_section, *pkg_descr, *pkg_url; + char *pkg_cxx, *pkg_subpkgs, *pkg_cfline, *pkg_dflt, *pkg_multi; + char *pkg_need_cxx, *pkg_need_java, *pkgname, *sysname, *pkg_debug; + char *pkg_libc_depends, *pkg_host_depends, *pkg_system_depends, *pkg_arch_depends, *pkg_flavours, *pkg_flavours_string, *pkg_choices, *pseudo_name; + char *packages, *pkg_name_u, *pkgs, *pkg_opts, *pkg_libname; + char *saveptr, *p_ptr, *s_ptr, *pkg_helper; + int result; + StrMap *pkgmap, *sectionmap; + + pkg_name = NULL; + pkg_descr = NULL; + pkg_section = NULL; + pkg_url = NULL; + pkg_depends = NULL; + pkg_depends_system = NULL; + pkg_opts = NULL; + pkg_libname = NULL; + pkg_flavours = NULL; + pkg_flavours_string = NULL; + pkg_choices = NULL; + pkg_subpkgs = NULL; + pkg_arch_depends = NULL; + pkg_system_depends = NULL; + pkg_host_depends = NULL; + pkg_libc_depends = NULL; + pkg_cxx = NULL; + pkg_dflt = NULL; + pkg_cfline = NULL; + pkg_multi = NULL; + pkg_need_cxx = NULL; + pkg_need_java = NULL; + pkgname = NULL; + sysname = NULL; + pkg_helper = NULL; + pkg_debug = NULL; + + p_ptr = NULL; + s_ptr = NULL; + + unlink("package/Config.in.auto"); + /* open global sectionfile */ + menuglobal = fopen("package/Config.in.auto.global", "w"); + if (menuglobal == NULL) + fatal_error("global section file not writable."); + + /* read section list and create a hash table */ + section = fopen("package/section.lst", "r"); + if (section == NULL) + fatal_error("section listfile is missing"); + + sectionmap = strmap_new(HASHSZ); + while (fgets(tbuf, MAXPATH, section) != NULL) { + key = strtok(tbuf, "\t"); + value = strtok(NULL, "\t"); + strmap_put(sectionmap, key, value); + } + fclose(section); + + if (mkdir("package/pkgconfigs.d", S_IRWXU) > 0) + fatal_error("creation of package/pkgconfigs.d failed."); + if (mkdir("package/pkgconfigs.d/gcc", S_IRWXU) > 0) + fatal_error("creation of package/pkgconfigs.d/gcc failed."); + if (mkdir("package/pkglist.d", S_IRWXU) > 0) + fatal_error("creation of package/pkglist.d failed."); + + /* delete Config.in.dev */ + if (snprintf(path, MAXPATH, "package/pkgconfigs.d/gcc/Config.in.dev") < 0) + fatal_error("failed to create path variable."); + unlink(path); + cfg = fopen(path, "w"); + if (cfg == NULL) + fatal_error("Config.in.dev can not be opened"); + fprintf(cfg, "config ADK_PACKAGE_GLIBC_DEV\n"); + fprintf(cfg, "\tprompt \"glibc-dev............ development files for glibc\"\n"); + fprintf(cfg, "\ttristate\n"); + fprintf(cfg, "\tdefault n\n"); + fprintf(cfg, "\tdepends on ADK_TARGET_LIB_GLIBC\n"); + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t GNU C library header files.\n\n"); + fprintf(cfg, "config ADK_PACKAGE_UCLIBC_DEV\n"); + fprintf(cfg, "\tprompt \"uclibc-dev........... development files for uclibc\"\n"); + fprintf(cfg, "\ttristate\n"); + fprintf(cfg, "\tdefault n\n"); + fprintf(cfg, "\tdepends on ADK_TARGET_LIB_UCLIBC\n"); + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t C library header files.\n\n"); + fprintf(cfg, "config ADK_PACKAGE_MUSL_DEV\n"); + fprintf(cfg, "\tprompt \"musl-dev............. development files for musl\"\n"); + fprintf(cfg, "\ttristate\n"); + fprintf(cfg, "\tdefault n\n"); + fprintf(cfg, "\tdepends on ADK_TARGET_LIB_MUSL\n"); + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t C library header files.\n\n"); + fclose(cfg); + + /* read Makefile's for all packages */ + pkgdir = opendir("package"); + while ((pkgdirp = readdir(pkgdir)) != NULL) { + /* skip dotfiles */ + if (strncmp(pkgdirp->d_name, ".", 1) > 0) { + if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0) + fatal_error("can not create path variable."); + pkg = fopen(path, "r"); + if (pkg == NULL) + continue; + + /* skip manually maintained packages */ + if (snprintf(path, MAXPATH, "package/%s/Config.in.manual", pkgdirp->d_name) < 0) + fatal_error("can not create path variable."); + if (!access(path, F_OK)) { + while (fgets(buf, MAXPATH, pkg) != NULL) { + if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0) + continue; + } + + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue)); + if (result == 1) { + if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0) + fatal_error("can not create path variable."); + section = fopen(spath, "a"); + if (section != NULL) { + fprintf(section, "%s@\n", pkgdirp->d_name); + fclose(section); + } + } else + fatal_error("Can not find section description for package %s.", + pkgdirp->d_name); + + fclose(pkg); + continue; + } + + nobinpkgs = 0; + + /* create output directories */ + if (snprintf(dir, MAXPATH, "package/pkgconfigs.d/%s", pkgdirp->d_name) < 0) + fatal_error("can not create dir variable."); + if (mkdir(dir, S_IRWXU) > 0) + fatal_error("can not create directory."); + + + /* allocate memory */ + hkey = malloc(MAXVAR); + memset(hkey, 0, MAXVAR); + memset(variable, 0, 2*MAXVAR); + + pkgmap = strmap_new(HASHSZ); + + /* parse package Makefile */ + while (fgets(buf, MAXPATH, pkg) != NULL) { + /* just read variables prefixed with PKG */ + if (strncmp(buf, "PKG", 3) == 0) { + if ((parse_var(buf, "PKG_NAME", NULL, &pkg_name)) == 0) + continue; + if (pkg_name != NULL) + pkg_name_u = toupperstr(pkg_name); + else + pkg_name_u = toupperstr(pkgdirp->d_name); + + snprintf(variable, MAXVAR, "PKG_CFLINE_%s", pkg_name_u); + if ((parse_var(buf, variable, pkg_cfline, &pkg_cfline)) == 0) + continue; + snprintf(variable, MAXVAR, "PKG_DFLT_%s", pkg_name_u); + if ((parse_var(buf, variable, NULL, &pkg_dflt)) == 0) + continue; + if ((parse_var(buf, "PKG_LIBC_DEPENDS", NULL, &pkg_libc_depends)) == 0) + continue; + if ((parse_var(buf, "PKG_HOST_DEPENDS", NULL, &pkg_host_depends)) == 0) + continue; + if ((parse_var(buf, "PKG_ARCH_DEPENDS", NULL, &pkg_arch_depends)) == 0) + continue; + if ((parse_var(buf, "PKG_SYSTEM_DEPENDS", NULL, &pkg_system_depends)) == 0) + continue; + if ((parse_var(buf, "PKG_DESCR", NULL, &pkg_descr)) == 0) + continue; + if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0) + continue; + if ((parse_var(buf, "PKG_URL", NULL, &pkg_url)) == 0) + continue; + if ((parse_var(buf, "PKG_CXX", NULL, &pkg_cxx)) == 0) + continue; + if ((parse_var(buf, "PKG_NEED_CXX", NULL, &pkg_need_cxx)) == 0) + continue; + if ((parse_var(buf, "PKG_NEED_JAVA", NULL, &pkg_need_java)) == 0) + continue; + if ((parse_var(buf, "PKG_MULTI", NULL, &pkg_multi)) == 0) + continue; + if ((parse_var(buf, "PKG_DEPENDS", pkg_depends, &pkg_depends)) == 0) + continue; + if ((parse_var_with_system(buf, "PKG_DEPENDS_", pkg_depends_system, &pkg_depends_system, &sysname, 12)) == 0) + continue; + if ((parse_var(buf, "PKG_LIBNAME", pkg_libname, &pkg_libname)) == 0) + continue; + if ((parse_var(buf, "PKG_OPTS", pkg_opts, &pkg_opts)) == 0) + continue; + if ((parse_var_with_pkg(buf, "PKG_FLAVOURS_STRING_", pkg_flavours_string, &pkg_flavours_string, &pkgname, 20)) == 0) + continue; + if ((parse_var_with_pkg(buf, "PKG_FLAVOURS_", pkg_flavours, &pkg_flavours, &pkgname, 13)) == 0) + continue; + if ((parse_var_hash(buf, "PKGFD_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGFX_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGFS_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGFC_", pkgmap)) == 0) + continue; + if ((parse_var_with_pkg(buf, "PKG_CHOICES_", pkg_choices, &pkg_choices, &pkgname, 12)) == 0) + continue; + if ((parse_var_hash(buf, "PKGCD_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGCS_", pkgmap)) == 0) + continue; + if ((parse_var(buf, "PKG_SUBPKGS", pkg_subpkgs, &pkg_subpkgs)) == 0) + continue; + if ((parse_var_hash(buf, "PKGSD_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGSS_", pkgmap)) == 0) + continue; + if ((parse_var_hash(buf, "PKGSC_", pkgmap)) == 0) + continue; + } + } + + /* when PKG_LIBNAME exist use this instead of PKG_NAME, but only for !libmix */ + if (pkg_libname != NULL) + if (pkg_opts != NULL) + if (strstr(pkg_opts, "libmix") == NULL) + pkg_name = strdup(pkg_libname); + + /* end of package Makefile parsing */ + if (fclose(pkg) != 0) + perror("Failed to close file stream for Makefile"); + +#if 0 + if (pkg_name != NULL) + fprintf(stderr, "Package name is %s\n", pkg_name); + if (pkg_libname != NULL) + fprintf(stderr, "Package library name is %s\n", pkg_libname); + if (pkg_section != NULL) + fprintf(stderr, "Package section is %s\n", pkg_section); + if (pkg_descr != NULL) + fprintf(stderr, "Package description is %s\n", pkg_descr); + if (pkg_depends != NULL) + fprintf(stderr, "Package dependencies are %s\n", pkg_depends); + if (pkg_depends_system != NULL) + fprintf(stderr, "Package systemspecific dependencies are %s\n", pkg_depends_system); + if (pkg_subpkgs != NULL) + fprintf(stderr, "Package subpackages are %s\n", pkg_subpkgs); + if (pkg_flavours != NULL && pkgname != NULL) + fprintf(stderr, "Package flavours for %s are %s\n", pkgname, pkg_flavours); + if (pkg_flavours_string != NULL && pkgname != NULL) + fprintf(stderr, "Package string flavours for %s are %s\n", pkgname, pkg_flavours_string); + if (pkg_choices != NULL && pkgname != NULL) + fprintf(stderr, "Package choices for %s are %s\n", pkgname, pkg_choices); + if (pkg_url != NULL) + fprintf(stderr, "Package homepage is %s\n", pkg_url); + if (pkg_cfline != NULL) + fprintf(stderr, "Package cfline is %s\n", pkg_cfline); + if (pkg_multi != NULL) + fprintf(stderr, "Package multi is %s\n", pkg_multi); + if (pkg_opts != NULL) + fprintf(stderr, "Package options are %s\n", pkg_opts); + + strmap_enum(pkgmap, iter_debug, NULL); +#endif + + /* generate master source Config.in file */ + if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in", pkgdirp->d_name) < 0) + fatal_error("path variable creation failed."); + fprintf(menuglobal, "source \"%s\"\n", path); + /* recreating file is faster than truncating with w+ */ + unlink(path); + cfg = fopen(path, "w"); + if (cfg == NULL) + continue; + + pkgs = NULL; + if (pkg_subpkgs != NULL) + pkgs = strdup(pkg_subpkgs); + + fprintf(cfg, "config ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name)); + fprintf(cfg, "\ttristate\n"); + if (nobinpkgs == 0) { + fprintf(cfg, "\tdepends on "); + if (pkgs != NULL) { + if (pkg_multi != NULL) + if (strncmp(pkg_multi, "1", 1) == 0) + fprintf(cfg, "ADK_HAVE_DOT_CONFIG || "); + token = strtok(pkgs, " "); + fprintf(cfg, "ADK_PACKAGE_%s", token); + token = strtok(NULL, " "); + while (token != NULL) { + fprintf(cfg, " || ADK_PACKAGE_%s", token); + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + } else { + fprintf(cfg, "ADK_PACKAGE_%s\n", toupperstr(pkg_name)); + } + } + fprintf(cfg, "\tdefault n\n"); + fclose(cfg); + free(pkgs); + + + /* skip packages without binary package output */ + if (nobinpkgs == 1) + continue; + + /* generate binary package specific Config.in files */ + if (pkg_subpkgs != NULL) + packages = tolowerstr(pkg_subpkgs); + else + packages = strdup(pkg_name); + + token = strtok_r(packages, " ", &p_ptr); + while (token != NULL) { + strncat(hkey, "PKGSC_", 6); + strncat(hkey, toupperstr(token), strlen(token)); + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + if (result == 1) + pkg_section = strdup(hvalue); + + strncat(hkey, "PKGSD_", 6); + strncat(hkey, toupperstr(token), strlen(token)); + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + if (result == 1) + pkg_descr = strdup(hvalue); + + pseudo_name = malloc(MAXLINE); + memset(pseudo_name, 0, MAXLINE); + strncat(pseudo_name, token, strlen(token)); + while (strlen(pseudo_name) < 20) + strncat(pseudo_name, ".", 1); + + if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in.%s", pkgdirp->d_name, token) < 0) + fatal_error("failed to create path variable."); + + /* create temporary section files */ + memset(hvalue, 0 , MAXVALUE); + result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue)); + if (result == 1) { + if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0) + fatal_error("failed to create path variable."); + section = fopen(spath, "a"); + if (section != NULL) { + fprintf(section, "%s |%s\n", token, pkgdirp->d_name); + fclose(section); + } + } else + fatal_error("Can not find section description for package %s.", pkgdirp->d_name); + + unlink(path); + cfg = fopen(path, "w"); + if (cfg == NULL) + perror("Can not open Config.in file"); + + if (pkg_need_cxx != NULL) { + fprintf(cfg, "comment \"%s... %s (disabled, c++ missing)\"\n", token, pkg_descr); + fprintf(cfg, "depends on !ADK_TOOLCHAIN_GCC_CXX\n\n"); + } + + /* save token in pkg_debug */ + pkg_debug = strdup(token); + fprintf(cfg, "config ADK_PACKAGE_%s\n", toupperstr(token)); + /* no prompt for devonly packages */ + if (pkg_opts != NULL) { + if (strstr(pkg_opts, "devonly") != NULL) { + fprintf(cfg, "\t#prompt \"%s. %s\"\n", pseudo_name, pkg_descr); + } else { + fprintf(cfg, "\tprompt \"%s. %s\"\n", pseudo_name, pkg_descr); + } + } else { + fprintf(cfg, "\tprompt \"%s. %s\"\n", pseudo_name, pkg_descr); + } + + fprintf(cfg, "\ttristate\n"); + if (pkg_multi != NULL) + if (strncmp(pkg_multi, "1", 1) == 0) + if (strncmp(toupperstr(token), toupperstr(pkgdirp->d_name), strlen(token)) != 0) + fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name)); + + free(pseudo_name); + + /* print custom cf line */ + if (pkg_cfline != NULL) { + cftoken = strtok_r(pkg_cfline, "@", &saveptr); + while (cftoken != NULL) { + fprintf(cfg, "\t%s\n", cftoken); + cftoken = strtok_r(NULL, "@", &saveptr); + } + free(pkg_cfline); + pkg_cfline = NULL; + } + + /* add sub package dependencies */ + strncat(hkey, "PKGSS_", 6); + strncat(hkey, toupperstr(token), strlen(token)); + memset(hvalue, 0, MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + if (result == 1) { + val = strtok_r(hvalue, " ", &saveptr); + while (val != NULL) { + if (strncmp(val, "kmod", 4) == 0) + fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val)); + else + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val)); + val = strtok_r(NULL, " ", &saveptr); + } + } + memset(hkey, 0, MAXVAR); + + /* create package target system dependency information */ + if (pkg_system_depends != NULL) { + pkg_helper = strdup(pkg_system_depends); + token = strtok(pkg_helper, " "); + fprintf(cfg, "\tdepends on "); + sp = ""; + while (token != NULL) { + if(strncmp(token, "!", 1) == 0) { + fprintf(cfg, "%s!ADK_TARGET_SYSTEM%s", sp, toupperstr(token)); + sp = " && "; + } else { + fprintf(cfg, "%sADK_TARGET_SYSTEM_%s", sp, toupperstr(token)); + sp = " || "; + } + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + free(pkg_helper); + pkg_helper = NULL; + } + /* create package host dependency information */ + if (pkg_host_depends != NULL) { + pkg_helper = strdup(pkg_host_depends); + token = strtok(pkg_helper, " "); + fprintf(cfg, "\tdepends on "); + sp = ""; + while (token != NULL) { + if(strncmp(token, "!", 1) == 0) { + fprintf(cfg, "%s!ADK_HOST%s", sp, toupperstr(token)); + sp = " && "; + } else { + fprintf(cfg, "%sADK_HOST_%s", sp, toupperstr(token)); + sp = " || "; + } + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + free(pkg_helper); + pkg_helper = NULL; + } + + /* create package libc dependency information */ + if (pkg_libc_depends != NULL) { + pkg_helper = strdup(pkg_libc_depends); + token = strtok(pkg_helper, " "); + fprintf(cfg, "\tdepends on "); + sp = ""; + while (token != NULL) { + if(strncmp(token, "!", 1) == 0) { + fprintf(cfg, "%s!ADK_TARGET_LIB_%s", sp, toupperstr(token)); + sp = " && "; + } else { + fprintf(cfg, "%sADK_TARGET_LIB_%s", sp, toupperstr(token)); + sp = " || "; + } + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + free(pkg_helper); + pkg_helper = NULL; + } + /* create package target architecture dependency information */ + if (pkg_arch_depends != NULL) { + pkg_helper = strdup(pkg_arch_depends); + token = strtok(pkg_helper, " "); + fprintf(cfg, "\tdepends on "); + sp = ""; + while (token != NULL) { + if(strncmp(token, "!", 1) == 0) { + fprintf(cfg, "%s!ADK_LINUX%s", sp, toupperstr(token)); + sp = " && "; + } else { + fprintf(cfg, "%sADK_LINUX_%s", sp, toupperstr(token)); + sp = " || "; + } + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + free(pkg_helper); + pkg_helper = NULL; + } + + /* create package dependency information */ + if (pkg_depends != NULL) { + token = strtok(pkg_depends, " "); + while (token != NULL) { + if (strncmp(token, "kmod", 4) == 0) + fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(token)); + else + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(token)); + token = strtok(NULL, " "); + } + free(pkg_depends); + pkg_depends = NULL; + } + /* create system specific package dependency information */ + if (pkg_depends_system != NULL) { + token = strtok(pkg_depends_system, " "); + while (token != NULL) { + fprintf(cfg, "\tselect ADK_PACKAGE_%s if ADK_TARGET_SYSTEM_%s\n", toupperstr(token), sysname); + token = strtok(NULL, " "); + } + free(pkg_depends_system); + pkg_depends_system = NULL; + } + + if (pkg_need_cxx != NULL) { + fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_CXX\n"); + } + if (pkg_need_java != NULL) { + fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_JAVA\n"); + pkg_need_java = NULL; + } + + fprintf(cfg, "\tselect ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name)); + + if (pkg_dflt != NULL) { + fprintf(cfg, "\tdefault %s\n", pkg_dflt); + pkg_dflt = NULL; + } else { + fprintf(cfg, "\tdefault n\n"); + } + + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t %s\n\n", pkg_descr); + if (pkg_url != NULL) + fprintf(cfg, "\t WWW: %s\n", pkg_url); + + /* handle special C++ packages */ + if (pkg_cxx != NULL) { + fprintf(cfg, "\nchoice\n"); + fprintf(cfg, "prompt \"C++ library to use\"\n"); + fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name)); + fprintf(cfg, "default ADK_COMPILE_%s_WITH_STDCXX if ADK_TARGET_LIB_GLIBC\n", pkg_cxx); + fprintf(cfg, "default ADK_COMPILE_%s_WITH_UCLIBCXX if ADK_TARGET_LIB_UCLIBC\n\n", pkg_cxx); + fprintf(cfg, "config ADK_COMPILE_%s_WITH_STDCXX\n", pkg_cxx); + fprintf(cfg, "\tbool \"GNU C++ library\"\n"); + fprintf(cfg, "\tselect ADK_PACKAGE_LIBSTDCXX\n\n"); + fprintf(cfg, "config ADK_COMPILE_%s_WITH_UCLIBCXX\n", pkg_cxx); + fprintf(cfg, "\tbool \"uClibc++ library\"\n"); + fprintf(cfg, "\tselect ADK_PACKAGE_UCLIBCXX\n\n"); + fprintf(cfg, "endchoice\n"); + free(pkg_cxx); + pkg_cxx = NULL; + } + + /* handle debug subpackages */ + fprintf(cfg, "\nconfig ADK_PACKAGE_%s_DBG\n", toupperstr(pkg_debug)); + fprintf(cfg, "\tprompt \"add debug symbols package\"\n"); + fprintf(cfg, "\ttristate\n"); + fprintf(cfg, "\tdepends on ADK_PACKAGE_GDB\n"); + fprintf(cfg, "\tdepends on !ADK_DEBUG\n"); + fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkg_debug)); + fprintf(cfg, "\tdefault n\n"); + fprintf(cfg, "\thelp\n\n"); + + /* package flavours */ + if (pkg_flavours != NULL) { + token = strtok(pkg_flavours, " "); + while (token != NULL) { + fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token)); + + // process default value + strncat(hkey, "PKGFX_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + pkg_fd = strdup(hvalue); + if (strlen(pkg_fd) > 0) + fprintf(cfg, "\tdefault %s\n", pkg_fd); + else + fprintf(cfg, "\tdefault n\n"); + + + // process flavour cfline + strncat(hkey, "PKGFC_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + pkg_fd = strdup(hvalue); + if (strlen(pkg_fd) > 0) + fprintf(cfg, "\t%s\n", pkg_fd); + + fprintf(cfg, "\tboolean "); + strncat(hkey, "PKGFD_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + pkg_fd = strdup(hvalue); + + fprintf(cfg, "\"%s\"\n", pkg_fd); + fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", pkgname); + strncat(hkey, "PKGFS_", 6); + strncat(hkey, token, strlen(token)); + + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + if (result == 1) { + val = strtok_r(hvalue, " ", &saveptr); + while (val != NULL) { + if (strncmp(val, "kmod", 4) == 0) + fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val)); + else + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val)); + val = strtok_r(NULL, " ", &saveptr); + } + } + memset(hkey, 0, MAXVAR); + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t %s\n", pkg_fd); + token = strtok(NULL, " "); + } + free(pkg_flavours); + pkg_flavours = NULL; + } + + /* package flavours string */ + if (pkg_flavours_string != NULL) { + token = strtok(pkg_flavours_string, " "); + while (token != NULL) { + fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token)); + + // process default value + strncat(hkey, "PKGFX_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + pkg_fd = strdup(hvalue); + if (strlen(pkg_fd) > 0) + fprintf(cfg, "\tdefault \"%s\"\n", pkg_fd); + + // process flavour cfline + strncat(hkey, "PKGFC_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + pkg_fd = strdup(hvalue); + if (strlen(pkg_fd) > 0) + fprintf(cfg, "\t%s\n", pkg_fd); + + fprintf(cfg, "\tstring "); + strncat(hkey, "PKGFD_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + pkg_fd = strdup(hvalue); + fprintf(cfg, "\"%s\"\n", pkg_fd); + + fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", pkgname); + strncat(hkey, "PKGFS_", 6); + strncat(hkey, token, strlen(token)); + + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + if (result == 1) { + val = strtok_r(hvalue, " ", &saveptr); + while (val != NULL) { + if (strncmp(val, "kmod", 4) == 0) + fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val)); + else + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val)); + val = strtok_r(NULL, " ", &saveptr); + } + } + memset(hkey, 0, MAXVAR); + fprintf(cfg, "\thelp\n"); + fprintf(cfg, "\t %s\n", pkg_fd); + token = strtok(NULL, " "); + } + free(pkg_flavours_string); + pkg_flavours_string = NULL; + } + + /* package choices */ + if (pkg_choices != NULL) { + fprintf(cfg, "\nchoice\n"); + fprintf(cfg, "prompt \"Package flavour choice\"\n"); + fprintf(cfg, "depends on ADK_PACKAGE_%s\n\n", pkgname); + token = strtok(pkg_choices, " "); + while (token != NULL) { + fprintf(cfg, "config ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token)); + + fprintf(cfg, "\tbool "); + strncat(hkey, "PKGCD_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0 , MAXVALUE); + strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + memset(hkey, 0 , MAXVAR); + fprintf(cfg, "\"%s\"\n", hvalue); + + strncat(hkey, "PKGCS_", 6); + strncat(hkey, token, strlen(token)); + memset(hvalue, 0, MAXVALUE); + result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue)); + if (result == 1) { + val = strtok_r(hvalue, " ", &saveptr); + while (val != NULL) { + if (strncmp(val, "kmod", 4) == 0) + fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val)); + else + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val)); + val = strtok_r(NULL, " ", &saveptr); + } + } + memset(hkey, 0, MAXVAR); + token = strtok(NULL, " "); + } + fprintf(cfg, "\nendchoice\n"); + free(pkg_choices); + pkg_choices = NULL; + } + /* close file descriptor for Config.in file */ + fclose(cfg); + /* create Config.in files for development packages */ + if (pkg_opts != NULL) { + if (strstr(pkg_opts, "dev") != NULL) { + if (snprintf(path, MAXPATH, "package/pkgconfigs.d/gcc/Config.in.dev") < 0) + fatal_error("failed to create path variable."); + cfg = fopen(path, "a"); + if (cfg == NULL) + perror("Can not open Config.in.dev file"); + + if (pkg_libname == NULL) + pkg_libname = strdup(pkg_name); + + fprintf(cfg, "\n"); + fprintf(cfg, "config ADK_PACKAGE_%s_DEV\n", toupperstr(pkg_libname)); + + pseudo_name = malloc(MAXLINE); + memset(pseudo_name, 0, MAXLINE); + strncat(pseudo_name, pkg_libname, strlen(pkg_libname)); + strncat(pseudo_name, "-dev", 4); + while (strlen(pseudo_name) < 20) + strncat(pseudo_name, ".", 1); + + fprintf(cfg, "\tprompt \"%s. development files for %s\"\n", pseudo_name, pkg_libname); + fprintf(cfg, "\ttristate\n"); + + /* create package target architecture dependency information */ + if (pkg_arch_depends != NULL) { + pkg_helper = strdup(pkg_arch_depends); + token = strtok(pkg_helper, " "); + fprintf(cfg, "\tdepends on "); + sp = ""; + while (token != NULL) { + if(strncmp(token, "!", 1) == 0) { + fprintf(cfg, "%s!ADK_LINUX%s", sp, toupperstr(token)); + sp = " && "; + } else { + fprintf(cfg, "%sADK_LINUX_%s", sp, toupperstr(token)); + sp = " || "; + } + token = strtok(NULL, " "); + } + fprintf(cfg, "\n"); + free(pkg_helper); + pkg_helper = NULL; + } + + fprintf(cfg, "\tdepends on ADK_PACKAGE_GCC\n"); + fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(pkg_libname)); + fprintf(cfg, "\tdefault n\n"); + fclose(cfg); + free(pseudo_name); + free(pkg_libname); + pkg_libname = NULL; + } + pkg_opts = NULL; + } + /* parse next package */ + token = strtok_r(NULL, " ", &p_ptr); + } + + /* end of package output generation */ + free(packages); + packages = NULL; + + pkg_need_cxx = NULL; + pkg_need_java = NULL; + /* reset flags, free memory */ + free(pkg_name); + free(pkg_libname); + free(pkg_descr); + free(pkg_section); + free(pkg_url); + free(pkg_depends); + free(pkg_flavours); + free(pkg_flavours_string); + free(pkg_choices); + free(pkg_subpkgs); + free(pkg_arch_depends); + free(pkg_system_depends); + free(pkg_host_depends); + free(pkg_libc_depends); + free(pkg_cxx); + free(pkg_dflt); + free(pkg_cfline); + free(pkg_multi); + pkg_name = NULL; + pkg_libname = NULL; + pkg_descr = NULL; + pkg_section = NULL; + pkg_url = NULL; + pkg_depends = NULL; + pkg_flavours = NULL; + pkg_flavours_string = NULL; + pkg_choices = NULL; + pkg_subpkgs = NULL; + pkg_arch_depends = NULL; + pkg_system_depends = NULL; + pkg_host_depends = NULL; + pkg_libc_depends = NULL; + pkg_cxx = NULL; + pkg_dflt = NULL; + pkg_cfline = NULL; + pkg_multi = NULL; + + strmap_delete(pkgmap); + nobinpkgs = 0; + free(hkey); + } + } + + /* add menu to gcc package */ + if (snprintf(path, MAXPATH, "package/pkgconfigs.d/gcc/Config.in.gcc") < 0) + fatal_error("failed to create path variable."); + cfg = fopen(path, "a"); + if (cfg == NULL) + perror("Can not open Config.in.gcc file"); + fprintf(cfg, "menu \"Development packages\"\n"); + fprintf(cfg, "depends on ADK_PACKAGE_GCC\n"); + fprintf(cfg, "source \"package/pkgconfigs.d/gcc/Config.in.dev\"\n"); + fprintf(cfg, "endmenu\n"); + fclose(cfg); + + /* create Config.in.auto */ + strmap_enum(sectionmap, iter, NULL); + + strmap_delete(sectionmap); + fclose(menuglobal); + closedir(pkgdir); + + /* remove temporary section files */ + pkglistdir = opendir("package/pkglist.d"); + while ((pkgdirp = readdir(pkglistdir)) != NULL) { + if (strncmp(pkgdirp->d_name, "sectionlst.", 11) == 0) { + if (snprintf(path, MAXPATH, "package/pkglist.d/%s", pkgdirp->d_name) < 0) + fatal_error("creating path variable failed."); + if (unlink(path) < 0) + fatal_error("removing file failed."); + } + } + closedir(pkglistdir); + return(0); +} diff --git a/adk/tools/pkgrebuild.c b/adk/tools/pkgrebuild.c new file mode 100644 index 000000000..e7f037ae5 --- /dev/null +++ b/adk/tools/pkgrebuild.c @@ -0,0 +1,273 @@ +/* + * pkgrebuild - recognize required package rebuilds in OpenADK + * + * Copyright (C) 2010,2011 Waldemar Brodkorb + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "strmap.h" + +StrMap *configmap, *configoldmap, *pkgmap; + +/* +static void iter(const char *key, const char *value, const void *obj) { + fprintf(stderr, "key: %s value: %s\n", key, value); +} +*/ + +static void iter_disabled(const char *key, const char *value, const void *obj) { + + char hvalue[256]; + char tfile[256]; + int fd; + + memset(hvalue, 0, 256); + if (strmap_exists(configmap, key) == 0) { + //fprintf(stderr, "disabled variables: %s\n", key); + if (strmap_get(pkgmap, key, hvalue, sizeof(hvalue)) == 1) { + //fprintf(stderr, "Symbol is a flavour/choice: %s\n", hvalue); + if (snprintf(tfile, 256, ".rebuild.%s", hvalue) < 0) + perror("can not create file variable."); + fd = open(tfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + close(fd); + } + } + +} + +static void iter_enabled(const char *key, const char *value, const void *obj) { + + char hvalue[256]; + char tfile[256]; + int fd; + + memset(hvalue, 0, 256); + if (strmap_exists(configoldmap, key) == 0) { + //fprintf(stderr, "enabled variables: %s\n", key); + if (strmap_get(pkgmap, key, hvalue, sizeof(hvalue)) == 1) { + //fprintf(stderr, "Symbol is a flavour/choice\n"); + if (snprintf(tfile, 256, ".rebuild.%s", hvalue) < 0) + perror("can not create file variable."); + fd = open(tfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + close(fd); + } + } +} + +static char *toupperstr(char *string) { + + int i; + char *str; + + /* transform to uppercase variable name */ + str = strdup(string); + for (i=0; i<(int)strlen(str); i++) { + if (str[i] == '+') + str[i] = 'X'; + if (str[i] == '-') + str[i] = '_'; + str[i] = toupper(str[i]); + } + return(str); +} + + + +int main() { + + FILE *config, *configold, *pkg; + char *key, *value, *string, *token, *check; + char *pkg_name, *keystr, *realpkgname; + char buf[128]; + char path[320]; + char pbuf[320]; + DIR *pkgdir; + struct dirent *pkgdirp; + + pkg_name = NULL; + /* read Makefile's for all packages */ + pkgmap = strmap_new(1024); + pkgdir = opendir("package"); + while ((pkgdirp = readdir(pkgdir)) != NULL) { + /* skip dotfiles */ + if (strncmp(pkgdirp->d_name, ".", 1) > 0) { + if (snprintf(path, 320, "package/%s/Makefile", pkgdirp->d_name) < 0) + perror("can not create path variable."); + pkg = fopen(path, "r"); + if (pkg == NULL) + continue; + + while (fgets(pbuf, 320, pkg) != NULL) { + if (strncmp(pbuf, "PKG", 3) == 0) { + string = strstr(pbuf, "PKG_NAME:="); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + value = strtok(NULL, "=\t"); + if (value != NULL) + pkg_name = strdup(value); + } + string = strstr(pbuf, "PKG_SUBPKGS:="); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + value = strtok(NULL, "=\t"); + token = strtok(value, " "); + while (token != NULL) { + keystr = malloc(256); + memset(keystr, 0, 256); + strncat(keystr, "ADK_PACKAGE_", 12); + strncat(keystr, token, strlen(token)); + strmap_put(pkgmap, keystr, pkgdirp->d_name); + token = strtok(NULL, " "); + free(keystr); + keystr = NULL; + } + } + string = strstr(pbuf, "PKG_SUBPKGS+="); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, "+="); + value = strtok(NULL, "=\t"); + token = strtok(value, " "); + while (token != NULL) { + keystr = malloc(256); + memset(keystr, 0, 256); + strncat(keystr, "ADK_PACKAGE_", 12); + strncat(keystr, token, strlen(token)); + strmap_put(pkgmap, keystr, pkgdirp->d_name); + token = strtok(NULL, " "); + free(keystr); + keystr = NULL; + } + } + string = strstr(pbuf, "PKG_FLAVOURS_"); + if (string != NULL) { + check = strstr(pbuf, ":="); + if (check != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + realpkgname = strdup(key+13); + value = strtok(NULL, "=\t"); + token = strtok(value, " "); + while (token != NULL) { + keystr = malloc(256); + memset(keystr, 0, 256); + strncat(keystr, "ADK_PACKAGE_", 12); + strncat(keystr, realpkgname, strlen(realpkgname)); + strncat(keystr, "_", 1); + strncat(keystr, token, strlen(token)); + strmap_put(pkgmap, keystr, pkgdirp->d_name); + token = strtok(NULL, " "); + free(keystr); + keystr = NULL; + } + } else { + string[strlen(string)-1] = '\0'; + key = strtok(string, "+="); + realpkgname = strdup(key+13); + value = strtok(NULL, "=\t"); + token = strtok(value, " "); + while (token != NULL) { + keystr = malloc(256); + memset(keystr, 0, 256); + strncat(keystr, "ADK_PACKAGE_", 12); + strncat(keystr, realpkgname, strlen(realpkgname)); + strncat(keystr, "_", 1); + strncat(keystr, token, strlen(token)); + strmap_put(pkgmap, keystr, pkgdirp->d_name); + token = strtok(NULL, " "); + free(keystr); + keystr = NULL; + } + } + } + string = strstr(pbuf, "PKG_CHOICES_"); + if (string != NULL) { + string[strlen(string)-1] = '\0'; + key = strtok(string, ":="); + value = strtok(NULL, "=\t"); + token = strtok(value, " "); + while (token != NULL) { + keystr = malloc(256); + memset(keystr, 0, 256); + strncat(keystr, "ADK_PACKAGE_", 12); + strncat(keystr, toupperstr(pkg_name), strlen(pkg_name)); + strncat(keystr, "_", 1); + strncat(keystr, token, strlen(token)); + strmap_put(pkgmap, keystr, pkgdirp->d_name); + token = strtok(NULL, " "); + free(keystr); + keystr = NULL; + } + } + } + } + fclose(pkg); + } + } + closedir(pkgdir); + + config = fopen(".config", "r"); + if (config == NULL) + perror(".config is missing."); + + configmap = strmap_new(1024); + while (fgets(buf, 128, config) != NULL) { + if (strncmp(buf, "ADK_PACKAGE", 11) == 0) { + key = strtok(buf, "="); + value = strtok(NULL, "="); + strmap_put(configmap, key, value); + } + } + fclose(config); + + configold = fopen(".config.old", "r"); + if (configold == NULL) + perror(".config.old is missing."); + + configoldmap = strmap_new(1024); + while (fgets(buf, 128, configold) != NULL) { + if (strncmp(buf, "ADK_PACKAGE", 11) == 0) { + key = strtok(buf, "="); + value = strtok(NULL, "="); + strmap_put(configoldmap, key, value); + } + } + fclose(configold); + + //fprintf(stdout, "Config Count: %d\n", strmap_get_count(configmap)); + //fprintf(stdout, "Config Old Count: %d\n", strmap_get_count(configoldmap)); + + strmap_enum(configoldmap, iter_disabled, NULL); + strmap_enum(configmap, iter_enabled, NULL); + //strmap_enum(pkgmap, iter, NULL); + + strmap_delete(pkgmap); + strmap_delete(configmap); + strmap_delete(configoldmap); + + return(0); +} diff --git a/adk/tools/sortfile.c b/adk/tools/sortfile.c new file mode 100644 index 000000000..1e9fc9623 --- /dev/null +++ b/adk/tools/sortfile.c @@ -0,0 +1,153 @@ +/*- + * Copyright (c) 2010 + * Thorsten Glaser + * + * Provided that these terms and disclaimer and all copyright notices + * are retained or reproduced in an accompanying document, permission + * is granted to deal in this work without restriction, including un- + * limited rights to use, publicly perform, distribute, sell, modify, + * merge, give away, or sublicence. + * + * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to + * the utmost extent permitted by applicable law, neither express nor + * implied; without malicious intent or gross negligence. In no event + * may a licensor, author or contributor be held liable for indirect, + * direct, other damage, loss, or other issues arising in any way out + * of dealing in the work, even if advised of the possibility of such + * damage or existence of a defect, except proven that it results out + * of said person's immediate fault when using the work as intended. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct ptrsize { + const char *ptr; + size_t size; +}; + +static void *xrecalloc(void *, size_t, size_t); +static int cmpfn(const void *, const void *); + +#define MUL_NO_OVERFLOW (1UL << (sizeof (size_t) * 8 / 2)) + +#ifndef SIZE_MAX +#ifdef SIZE_T_MAX +#define SIZE_MAX SIZE_T_MAX +#else +#define SIZE_MAX ((size_t)-1) +#endif +#endif + +#if !defined(MAP_FAILED) +/* XXX imake style */ +# if defined(__linux) +#define MAP_FAILED ((void *)-1) +# elif defined(__bsdi__) || defined(__osf__) || defined(__ultrix) +#define MAP_FAILED ((caddr_t)-1) +# endif +#endif + +static void * +xrecalloc(void *ptr, size_t nmemb, size_t size) +{ + void *rv; + + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) + errx(1, "attempted integer overflow: %zu * %zu", nmemb, size); + size *= nmemb; + if ((rv = realloc(ptr, size)) == NULL) + err(1, "cannot allocate %zu bytes", size); + return (rv); +} + +int +sortfile(char *infile, char *outfile) +{ + int fd, fdout; + size_t fsz, asz, anents; + char *cp, *thefile, *endfile; + struct ptrsize *thearray; + + if ((fd = open(infile, O_RDONLY)) < 0) + err(1, "open: %s", infile); + else { + struct stat sb; + + /* reasonable maximum size: 3/4 of SIZE_MAX */ + fsz = (SIZE_MAX / 2) + (SIZE_MAX / 4); + + if (fstat(fd, &sb)) + err(1, "stat: %s", infile); + if (sb.st_size > fsz) + errx(1, "file %s too big, %llu > %zu", infile, + (unsigned long long)sb.st_size, fsz); + fsz = (size_t)sb.st_size; + } + + if ((thefile = mmap(NULL, fsz, PROT_READ, MAP_FILE | MAP_PRIVATE, + fd, (off_t)0)) == MAP_FAILED) + err(1, "mmap %zu bytes from %s", fsz, infile); + /* last valid byte in the file, must be newline anyway */ + endfile = thefile + fsz - 1; + + thearray = xrecalloc(NULL, (asz = 8), sizeof(thearray[0])); + thearray[(anents = 0)].ptr = cp = thefile; + + while ((cp = memchr(cp, '\n', endfile - cp)) != NULL) { + /* byte after the \n */ + if (++cp > endfile) + /* end of file */ + break; + thearray[anents].size = cp - thearray[anents].ptr; + if (++anents == asz) + /* resize array */ + thearray = xrecalloc(thearray, (asz <<= 1), + sizeof(thearray[0])); + thearray[anents].ptr = cp; + } + thearray[anents].size = endfile - thearray[anents].ptr + 1; + + qsort(thearray, ++anents, sizeof(thearray[0]), cmpfn); + + if ((fdout = open(outfile, O_WRONLY | O_CREAT, S_IRWXU)) < 0) + err(1, "open: %s", outfile); + else { + for (asz = 0; asz < anents; ++asz) + if ((size_t)write(fdout, thearray[asz].ptr, + thearray[asz].size) != thearray[asz].size) + err(1, "write %zu bytes", thearray[asz].size); + } + + if (munmap(thefile, fsz)) + warn("munmap"); + + free(thearray); + close(fd); + + return (0); +} + +static int +cmpfn(const void *p1, const void *p2) +{ + int rv; + const struct ptrsize *a1 = (const struct ptrsize *)p1; + const struct ptrsize *a2 = (const struct ptrsize *)p2; + + if ((rv = memcmp(a1->ptr, a2->ptr, (a1->size > a2->size ? + a2->size : a1->size) - /* '\n' */ 1)) != 0) + /* unequal in the common part */ + return (rv); + + /* shorter string is smaller */ + return (a1->size > a2->size ? 1 : a1->size == a2->size ? 0 : -1); +} diff --git a/adk/tools/sortfile.h b/adk/tools/sortfile.h new file mode 100644 index 000000000..c54294e69 --- /dev/null +++ b/adk/tools/sortfile.h @@ -0,0 +1 @@ +int sortfile(char *infile, char *outfile); diff --git a/adk/tools/strmap.c b/adk/tools/strmap.c new file mode 100644 index 000000000..f2c660e1f --- /dev/null +++ b/adk/tools/strmap.c @@ -0,0 +1,510 @@ +/* + * strmap version 1.0.0 + * + * ANSI C hash table for strings. + * + * strmap.c + * + * Copyright (c) 2009 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * strmap is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * strmap is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with strmap. If not, see . + */ +#include "strmap.h" + +typedef struct Pair Pair; + +typedef struct Bucket Bucket; + +struct Pair { + char *key; + char *value; +}; + +struct Bucket { + unsigned int count; + Pair *pairs; +}; + +struct StrMap { + unsigned int count; + Bucket *buckets; +}; + +static Pair * get_pair(Bucket *bucket, const char *key); +static unsigned long hash(const char *str); + +StrMap * strmap_new(unsigned int capacity) +{ + StrMap *map; + + map = malloc(sizeof(StrMap)); + if (map == NULL) { + return NULL; + } + map->count = capacity; + map->buckets = malloc(map->count * sizeof(Bucket)); + if (map->buckets == NULL) { + free(map); + return NULL; + } + memset(map->buckets, 0, map->count * sizeof(Bucket)); + return map; +} + +void strmap_delete(StrMap *map) +{ + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return; + } + n = map->count; + bucket = map->buckets; + i = 0; + while (i < n) { + m = bucket->count; + pair = bucket->pairs; + j = 0; + while(j < m) { + free(pair->key); + free(pair->value); + pair++; + j++; + } + free(bucket->pairs); + bucket++; + i++; + } + free(map->buckets); + free(map); +} + +int strmap_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf) +{ + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + if (out_buf == NULL && n_out_buf == 0) { + return strlen(pair->value) + 1; + } + if (out_buf == NULL) { + return 0; + } + if (strlen(pair->value) >= n_out_buf) { + return 0; + } + strcpy(out_buf, pair->value); + return 1; +} + +int strmap_exists(const StrMap *map, const char *key) +{ + unsigned int index; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (key == NULL) { + return 0; + } + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + pair = get_pair(bucket, key); + if (pair == NULL) { + return 0; + } + return 1; +} + +int strmap_put(StrMap *map, const char *key, const char *value) +{ + unsigned int key_len, value_len, index; + Bucket *bucket; + Pair *tmp_pairs, *pair; + char *tmp_value; + char *new_key, *new_value; + + if (map == NULL) { + return 0; + } + if (key == NULL || value == NULL) { + return 0; + } + key_len = strlen(key); + value_len = strlen(value); + /* Get a pointer to the bucket the key string hashes to */ + index = hash(key) % map->count; + bucket = &(map->buckets[index]); + /* Check if we can handle insertion by simply replacing + * an existing value in a key-value pair in the bucket. + */ + if ((pair = get_pair(bucket, key)) != NULL) { + /* The bucket contains a pair that matches the provided key, + * change the value for that pair to the new value. + */ + if (strlen(pair->value) < value_len) { + /* If the new value is larger than the old value, re-allocate + * space for the new larger value. + */ + tmp_value = realloc(pair->value, (value_len + 1) * sizeof(char)); + if (tmp_value == NULL) { + return 0; + } + pair->value = tmp_value; + } + /* Copy the new value into the pair that matches the key */ + strcpy(pair->value, value); + return 1; + } + /* Allocate space for a new key and value */ + new_key = malloc((key_len + 1) * sizeof(char)); + if (new_key == NULL) { + return 0; + } + new_value = malloc((value_len + 1) * sizeof(char)); + if (new_value == NULL) { + free(new_key); + return 0; + } + /* Create a key-value pair */ + if (bucket->count == 0) { + /* The bucket is empty, lazily allocate space for a single + * key-value pair. + */ + bucket->pairs = malloc(sizeof(Pair)); + if (bucket->pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->count = 1; + } + else { + /* The bucket wasn't empty but no pair existed that matches the provided + * key, so create a new key-value pair. + */ + tmp_pairs = realloc(bucket->pairs, (bucket->count + 1) * sizeof(Pair)); + if (tmp_pairs == NULL) { + free(new_key); + free(new_value); + return 0; + } + bucket->pairs = tmp_pairs; + bucket->count++; + } + /* Get the last pair in the chain for the bucket */ + pair = &(bucket->pairs[bucket->count - 1]); + pair->key = new_key; + pair->value = new_value; + /* Copy the key and its value into the key-value pair */ + strcpy(pair->key, key); + strcpy(pair->value, value); + return 1; +} + +int strmap_get_count(const StrMap *map) +{ + unsigned int i, j, n, m; + unsigned int count; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + count = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + count++; + pair++; + j++; + } + bucket++; + i++; + } + return count; +} + +int strmap_enum(const StrMap *map, strmap_enum_func enum_func, const void *obj) +{ + unsigned int i, j, n, m; + Bucket *bucket; + Pair *pair; + + if (map == NULL) { + return 0; + } + if (enum_func == NULL) { + return 0; + } + bucket = map->buckets; + n = map->count; + i = 0; + while (i < n) { + pair = bucket->pairs; + m = bucket->count; + j = 0; + while (j < m) { + enum_func(pair->key, pair->value, obj); + pair++; + j++; + } + bucket++; + i++; + } + return 1; +} + +/* + * Returns a pair from the bucket that matches the provided key, + * or null if no such pair exist. + */ +static Pair * get_pair(Bucket *bucket, const char *key) +{ + unsigned int i, n; + Pair *pair; + + n = bucket->count; + if (n == 0) { + return NULL; + } + pair = bucket->pairs; + i = 0; + while (i < n) { + if (pair->key != NULL && pair->value != NULL) { + if (strcmp(pair->key, key) == 0) { + return pair; + } + } + pair++; + i++; + } + return NULL; +} + +/* + * Returns a hash code for the provided string. + */ +static unsigned long hash(const char *str) +{ + unsigned long hash = 5381; + int c; + c = 0; + + while (c == *str++) { + hash = ((hash << 5) + hash) + c; + } + return hash; +} + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ diff --git a/adk/tools/strmap.h b/adk/tools/strmap.h new file mode 100644 index 000000000..99687b236 --- /dev/null +++ b/adk/tools/strmap.h @@ -0,0 +1,350 @@ +/* + * strmap version 1.0.0 + * + * ANSI C hash table for strings. + * + * strmap.h + * + * Copyright (c) 2009 Per Ola Kristensson. + * + * Per Ola Kristensson + * Inference Group, Department of Physics + * University of Cambridge + * Cavendish Laboratory + * JJ Thomson Avenue + * CB3 0HE Cambridge + * United Kingdom + * + * strmap is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * strmap is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with strmap. If not, see . + */ +#ifndef _STRMAP_H_ +#define _STRMAP_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include + +typedef struct StrMap StrMap; + +/* + * This callback function is called once per key-value when enumerating + * all keys associated to values. + * + * Parameters: + * + * key: A pointer to a null-terminated C string. The string must not + * be modified by the client. + * + * value: A pointer to a null-terminated C string. The string must + * not be modified by the client. + * + * obj: A pointer to a client-specific object. This parameter may be + * null. + * + * Return value: None. + */ +typedef void(*strmap_enum_func)(const char *key, const char *value, const void *obj); + +/* + * Creates a string map. + * + * Parameters: + * + * capacity: The number of top-level slots this string map + * should allocate. This parameter must be > 0. + * + * Return value: A pointer to a string map object, + * or null if a new string map could not be allocated. + */ +StrMap * strmap_new(unsigned int capacity); + +/* + * Releases all memory held by a string map object. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * If the supplied string map has been previously released, the + * behaviour of this function is undefined. + * + * Return value: None. + */ +void strmap_delete(StrMap *map); + +/* + * Returns the value associated with the supplied key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * out_buf: A pointer to an output buffer which will contain the value, + * if it exists and fits into the buffer. + * + * n_out_buf: The size of the output buffer in bytes. + * + * Return value: If out_buf is set to null and n_out_buf is set to 0 the return + * value will be the number of bytes required to store the value (if it exists) + * and its null-terminator. For all other parameter configurations the return value + * is 1 if an associated value was found and completely copied into the output buffer, + * 0 otherwise. + */ +int strmap_get(const StrMap *map, const char *key, char *out_buf, unsigned int n_out_buf); + +/* + * Queries the existence of a key. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter cannot + * be null. + * + * Return value: 1 if the key exists, 0 otherwise. + */ +int strmap_exists(const StrMap *map, const char *key); + +/* + * Associates a value with the supplied key. If the key is already + * associated with a value, the previous value is replaced. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * key: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * value: A pointer to a null-terminated C string. This parameter + * cannot be null. The string must have a string length > 0. The + * string will be copied. + * + * Return value: 1 if the association succeeded, 0 otherwise. + */ +int strmap_put(StrMap *map, const char *key, const char *value); + +/* + * Returns the number of associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * Return value: The number of associations between keys and values. + */ +int strmap_get_count(const StrMap *map); + +/* + * Enumerates all associations between keys and values. + * + * Parameters: + * + * map: A pointer to a string map. This parameter cannot be null. + * + * enum_func: A pointer to a callback function that will be + * called by this procedure once for every key associated + * with a value. This parameter cannot be null. + * + * obj: A pointer to a client-specific object. This parameter will be + * passed back to the client's callback function. This parameter can + * be null. + * + * Return value: 1 if enumeration completed, 0 otherwise. + */ +int strmap_enum(const StrMap *map, strmap_enum_func enum_func, const void *obj); + +#ifdef __cplusplus +} +#endif + +#endif + +/* + + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + +*/ -- cgit v1.2.3