4 #if defined(__linux__) && defined(__powerpc__)
8 #include <ctype.h> /* XXX for /etc/rpm/platform contents */
10 #if HAVE_SYS_SYSTEMCFG_H
11 #include <sys/systemcfg.h>
13 #define __power_pc() 0
16 #include <rpmio_internal.h>
24 static const char *defrcfiles =
26 ":" RPMCONFIGDIR "/" RPMCANONVENDOR "/rpmrc"
27 ":" SYSCONFDIR "/rpmrc"
30 const char * macrofiles =
32 RPMCONFIGDIR "/macros"
33 ":" RPMCONFIGDIR "/%{_target}/macros"
34 ":" SYSCONFDIR "/rpm/macros.*"
35 ":" SYSCONFDIR "/rpm/macros"
36 ":" SYSCONFDIR "/rpm/%{_target}/macros"
42 static const char * platform = SYSCONFDIR "/rpm/platform";
43 static const char ** platpat = NULL;
44 static int nplatpat = 0;
46 typedef const char * cptr_t;
48 typedef struct machCacheEntry_s {
55 typedef struct machCache_s {
60 typedef struct machEquivInfo_s {
65 typedef struct machEquivTable_s {
72 /* eventually, this arch will be replaced with a generic condition */
74 struct rpmvarValue * next;
84 struct rpmOptionValue * value;
87 typedef struct defaultEntry_s {
92 typedef struct canonEntry_s {
94 const char * short_name;
98 /* tags are 'key'canon, 'key'translate, 'key'compat
100 * for giggles, 'key'_canon, 'key'_compat, and 'key'_canon will also work
102 typedef struct tableType_s {
103 const char * const key;
105 const int hasTranslate;
106 struct machEquivTable_s equiv;
107 struct machCache_s cache;
108 defaultEntry defaults;
114 static struct tableType_s tables[RPM_MACHTABLE_COUNT] = {
117 { "buildarch", 0, 1 },
121 /* this *must* be kept in alphabetical order */
122 /* The order of the flags is archSpecific, required, macroize, localize */
124 static struct rpmOption optionTable[] = {
125 { "include", RPMVAR_INCLUDE, 0, 1, 0, 2 },
126 { "macrofiles", RPMVAR_MACROFILES, 0, 0, 0, 1 },
127 { "optflags", RPMVAR_OPTFLAGS, 1, 0, 1, 0 },
130 static int optionTableSize = sizeof(optionTable) / sizeof(*optionTable);
135 static cptr_t current[2];
137 static int currTables[2] = { RPM_MACHTABLE_INSTOS, RPM_MACHTABLE_INSTARCH };
139 static struct rpmvarValue values[RPMVAR_NUM];
141 static int defaultsInitialized = 0;
144 static int doReadRC( FD_t fd, const char * urlfn);
146 static void rpmSetVarArch(int var, const char * val,
149 static void rebuildCompatTables(int type, const char * name);
151 static void rpmRebuildTargetVars(const char **target, const char ** canontarget);
153 static int optionCompare(const void * a, const void * b)
155 return xstrcasecmp(((struct rpmOption *) a)->name,
156 ((struct rpmOption *) b)->name);
159 static machCacheEntry
160 machCacheFindEntry(const machCache cache, const char * key)
164 for (i = 0; i < cache->size; i++)
165 if (!strcmp(cache->cache[i].name, key)) return cache->cache + i;
170 static int machCompatCacheAdd(char * name, const char * fn, int linenum,
173 machCacheEntry entry = NULL;
179 while (*name && xisspace(*name)) name++;
182 while (*chptr && *chptr != ':') chptr++;
184 rpmlog(RPMERR_RPMRC, _("missing second ':' at %s:%d\n"), fn, linenum);
186 } else if (chptr == name) {
187 rpmlog(RPMERR_RPMRC, _("missing architecture name at %s:%d\n"), fn,
192 while (*chptr == ':' || xisspace(*chptr)) chptr--;
195 while (*equivs && xisspace(*equivs)) equivs++;
201 entry = machCacheFindEntry(cache, name);
203 for (i = 0; i < entry->count; i++)
204 entry->equivs[i] = _free(entry->equivs[i]);
205 entry->equivs = _free(entry->equivs);
211 cache->cache = xrealloc(cache->cache,
212 (cache->size + 1) * sizeof(*cache->cache));
213 entry = cache->cache + cache->size++;
214 entry->name = xstrdup(name);
219 if (delEntry) return 0;
221 while ((chptr = strtok(equivs, " ")) != NULL) {
223 if (chptr[0] == '\0') /* does strtok() return "" ever?? */
226 entry->equivs = xrealloc(entry->equivs, sizeof(*entry->equivs)
227 * (entry->count + 1));
229 entry->equivs = xmalloc(sizeof(*entry->equivs));
231 entry->equivs[entry->count] = xstrdup(chptr);
239 machEquivSearch(const machEquivTable table, const char * name)
243 for (i = 0; i < table->count; i++)
244 if (!xstrcasecmp(table->list[i].name, name))
245 return table->list + i;
250 static void machAddEquiv(machEquivTable table, const char * name,
255 equiv = machEquivSearch(table, name);
258 table->list = xrealloc(table->list, (table->count + 1)
259 * sizeof(*table->list));
261 table->list = xmalloc(sizeof(*table->list));
263 table->list[table->count].name = xstrdup(name);
264 table->list[table->count++].score = distance;
268 static void machCacheEntryVisit(machCache cache,
269 machEquivTable table, const char * name, int distance)
271 machCacheEntry entry;
274 entry = machCacheFindEntry(cache, name);
275 if (!entry || entry->visited) return;
279 for (i = 0; i < entry->count; i++) {
280 machAddEquiv(table, entry->equivs[i], distance);
283 for (i = 0; i < entry->count; i++) {
284 machCacheEntryVisit(cache, table, entry->equivs[i], distance + 1);
288 static void machFindEquivs(machCache cache, machEquivTable table,
293 for (i = 0; i < cache->size; i++)
294 cache->cache[i].visited = 0;
296 while (table->count > 0) {
298 table->list[table->count].name = _free(table->list[table->count].name);
301 table->list = _free(table->list);
304 * We have a general graph built using strings instead of pointers.
305 * Yuck. We have to start at a point at traverse it, remembering how
306 * far away everything is.
308 /* FIX: table->list may be NULL. */
309 machAddEquiv(table, key, 1);
310 machCacheEntryVisit(cache, table, key, 2);
314 static int addCanon(canonEntry * table, int * tableLen, char * line,
315 const char * fn, int lineNum)
320 const char * tshort_name;
324 *table = xrealloc(*table, sizeof(**table) * (*tableLen));
326 t = & ((*table)[*tableLen - 2]);
328 tname = strtok(line, ": \t");
329 tshort_name = strtok(NULL, " \t");
330 s = strtok(NULL, " \t");
331 if (! (tname && tshort_name && s)) {
332 rpmlog(RPMERR_RPMRC, _("Incomplete data line at %s:%d\n"),
336 if (strtok(NULL, " \t")) {
337 rpmlog(RPMERR_RPMRC, _("Too many args in data line at %s:%d\n"),
342 /* LCL: s != NULL here. */
343 tnum = strtoul(s, &s1, 10);
344 if ((*s1) || (s1 == s) || (tnum == ULONG_MAX)) {
345 rpmlog(RPMERR_RPMRC, _("Bad arch/os number: %s (%s:%d)\n"), s,
347 return(RPMERR_RPMRC);
350 t[0].name = xstrdup(tname);
351 t[0].short_name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
354 /* From A B C entry */
355 /* Add B B C entry */
356 t[1].name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
357 t[1].short_name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
363 static int addDefault(defaultEntry * table, int * tableLen, char * line,
364 const char * fn, int lineNum)
369 *table = xrealloc(*table, sizeof(**table) * (*tableLen));
371 t = & ((*table)[*tableLen - 1]);
373 t->name = strtok(line, ": \t");
374 t->defName = strtok(NULL, " \t");
375 if (! (t->name && t->defName)) {
376 rpmlog(RPMERR_RPMRC, _("Incomplete default line at %s:%d\n"),
380 if (strtok(NULL, " \t")) {
381 rpmlog(RPMERR_RPMRC, _("Too many args in default line at %s:%d\n"),
386 t->name = xstrdup(t->name);
387 t->defName = (t->defName ? xstrdup(t->defName) : NULL);
392 static canonEntry lookupInCanonTable(const char * name,
393 const canonEntry table, int tableLen)
397 if (strcmp(name, table[tableLen].name))
399 return &(table[tableLen]);
406 const char * lookupInDefaultTable(const char * name,
407 const defaultEntry table, int tableLen)
411 if (table[tableLen].name && !strcmp(name, table[tableLen].name))
412 return table[tableLen].defName;
418 static void setVarDefault(int var, const char * macroname, const char * val,
421 if (var >= 0) { /* XXX Dying ... */
422 if (rpmGetVar(var)) return;
427 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
430 static void setPathDefault(int var, const char * macroname, const char * subdir)
433 if (var >= 0) { /* XXX Dying ... */
437 if (rpmGetVar(var)) return;
439 topdir = rpmGetPath("%{_topdir}", NULL);
441 fn = alloca(strlen(topdir) + strlen(subdir) + 2);
443 if (fn[strlen(topdir) - 1] != '/')
448 topdir = _free(topdir);
451 if (macroname != NULL) {
452 #define _TOPDIRMACRO "%{_topdir}/"
453 char *body = alloca(sizeof(_TOPDIRMACRO) + strlen(subdir));
454 strcpy(body, _TOPDIRMACRO);
455 strcat(body, subdir);
456 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
461 static const char * prescriptenviron = "\n\
462 RPM_SOURCE_DIR=\"%{_sourcedir}\"\n\
463 RPM_BUILD_DIR=\"%{_builddir}\"\n\
464 RPM_OPT_FLAGS=\"%{optflags}\"\n\
465 RPM_ARCH=\"%{_arch}\"\n\
467 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS\n\
468 RPM_DOC_DIR=\"%{_docdir}\"\n\
469 export RPM_DOC_DIR\n\
470 RPM_PACKAGE_NAME=\"%{name}\"\n\
471 RPM_PACKAGE_VERSION=\"%{version}\"\n\
472 RPM_PACKAGE_RELEASE=\"%{release}\"\n\
473 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE\n\
474 %{?buildroot:RPM_BUILD_ROOT=\"%{buildroot}\"\n\
475 export RPM_BUILD_ROOT\n}\
478 static void setDefaults(void)
481 addMacro(NULL, "_usr", NULL, "/usr", RMIL_DEFAULT);
482 addMacro(NULL, "_var", NULL, LOCALSTATEDIR, RMIL_DEFAULT);
484 addMacro(NULL, "_preScriptEnvironment",NULL, prescriptenviron,RMIL_DEFAULT);
486 setVarDefault(-1, "_topdir",
487 "/usr/src/redhat", "%{_usr}/src/redhat");
488 setVarDefault(-1, "_tmppath",
489 LOCALSTATEDIR "/tmp", "%{_var}/tmp");
490 setVarDefault(-1, "_dbpath",
491 LOCALSTATEDIR "/lib/rpm", "%{_var}/lib/rpm");
492 setVarDefault(-1, "_defaultdocdir",
493 "/usr/doc", "%{_usr}/doc");
495 setVarDefault(-1, "_rpmfilename",
496 "%%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm",NULL);
498 setVarDefault(RPMVAR_OPTFLAGS, "optflags",
500 setVarDefault(-1, "sigtype",
502 setVarDefault(-1, "_buildshell",
505 setPathDefault(-1, "_builddir", "BUILD");
506 setPathDefault(-1, "_rpmdir", "RPMS");
507 setPathDefault(-1, "_srcrpmdir", "SRPMS");
508 setPathDefault(-1, "_sourcedir", "SOURCES");
509 setPathDefault(-1, "_specdir", "SPECS");
513 /* FIX: se usage inconsistent, W2DO? */
514 static int doReadRC( FD_t fd, const char * urlfn)
519 struct rpmOption searchOption, * option;
522 /* XXX really need rc = Slurp(fd, const char * filename, char ** buf) */
523 { off_t size = fdSize(fd);
524 size_t nb = (size >= 0 ? size : (8*BUFSIZ - 2));
529 next = alloca(nb + 2);
531 rc = Fread(next, sizeof(*next), nb, fd);
532 if (Ferror(fd) || (size > 0 && rc != nb)) { /* XXX Feof(fd) */
533 rpmlog(RPMERR_RPMRC, _("Failed to read %s: %s.\n"), urlfn,
544 while (*next != '\0') {
549 /* Find end-of-line. */
550 while (*se && *se != '\n') se++;
551 if (*se != '\0') *se++ = '\0';
554 /* Trim leading spaces */
555 while (*s && xisspace(*s)) s++;
557 /* We used to allow comments to begin anywhere, but not anymore. */
558 if (*s == '#' || *s == '\0') continue;
560 /* Find end-of-keyword. */
562 while (*se && !xisspace(*se) && *se != ':') se++;
566 while (*se && xisspace(*se) && *se != ':') se++;
570 rpmlog(RPMERR_RPMRC, _("missing ':' (found 0x%02x) at %s:%d\n"),
571 (unsigned)(0xff & *se), urlfn, linenum);
574 *se++ = '\0'; /* terminate keyword or option, point to value */
575 while (*se && xisspace(*se)) se++;
577 /* Find keyword in table */
578 searchOption.name = s;
579 option = bsearch(&searchOption, optionTable, optionTableSize,
580 sizeof(optionTable[0]), optionCompare);
582 if (option) { /* For configuration variables ... */
583 const char *arch, *val, *fn;
585 arch = val = fn = NULL;
587 rpmlog(RPMERR_RPMRC, _("missing argument for %s at %s:%d\n"),
588 option->name, urlfn, linenum);
592 switch (option->var) {
597 while (*se && !xisspace(*se)) se++;
598 if (*se != '\0') *se++ = '\0';
600 rpmRebuildTargetVars(NULL, NULL);
602 fn = rpmGetPath(s, NULL);
603 if (fn == NULL || *fn == '\0') {
604 rpmlog(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\"\n"),
605 option->name, urlfn, linenum, s);
610 fdinc = Fopen(fn, "r.fpio");
611 if (fdinc == NULL || Ferror(fdinc)) {
612 rpmlog(RPMERR_RPMRC, _("cannot open %s at %s:%d: %s\n"),
613 fn, urlfn, linenum, Fstrerror(fdinc));
616 rc = doReadRC(fdinc, fn);
620 continue; /* XXX don't save include value as var/macro */
626 if (option->archSpecific) {
628 while (*se && !xisspace(*se)) se++;
631 _("missing architecture for %s at %s:%d\n"),
632 option->name, urlfn, linenum);
636 while (*se && xisspace(*se)) se++;
639 _("missing argument for %s at %s:%d\n"),
640 option->name, urlfn, linenum);
647 /* Only add macros if appropriate for this arch */
648 if (option->macroize &&
649 (arch == NULL || !strcmp(arch, current[ARCH]))) {
651 n = name = xmalloc(strlen(option->name)+2);
652 if (option->localize)
654 strcpy(n, option->name);
655 addMacro(NULL, name, NULL, val, RMIL_RPMRC);
658 rpmSetVarArch(option->var, val, arch);
661 } else { /* For arch/os compatibilty tables ... */
667 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
668 if (!strncmp(tables[i].key, s, strlen(tables[i].key)))
672 if (i < RPM_MACHTABLE_COUNT) {
673 const char *rest = s + strlen(tables[i].key);
674 if (*rest == '_') rest++;
676 if (!strcmp(rest, "compat")) {
677 if (machCompatCacheAdd(se, urlfn, linenum,
681 } else if (tables[i].hasTranslate &&
682 !strcmp(rest, "translate")) {
683 if (addDefault(&tables[i].defaults,
684 &tables[i].defaultsLength,
688 } else if (tables[i].hasCanon &&
689 !strcmp(rest, "canon")) {
690 if (addCanon(&tables[i].canons, &tables[i].canonsLength,
698 rpmlog(RPMERR_RPMRC, _("bad option '%s' at %s:%d\n"),
710 static int rpmPlatform(const char * platform)
712 char *cpu = NULL, *vendor = NULL, *os = NULL, *gnu = NULL;
715 int init_platform = 0;
719 rc = rpmioSlurp(platform, &b, &blen);
721 if (rc || b == NULL || blen <= 0) {
727 for (pe = p; p && *p; p = pe) {
728 pe = strchr(p, '\n');
732 while (*p && isspace(*p))
734 if (*p == '\0' || *p == '#')
738 char * t = p + strlen(p);
740 while (--t > p && isspace(*t))
743 platpat = xrealloc(platpat, (nplatpat + 2) * sizeof(*platpat));
744 platpat[nplatpat] = xstrdup(p);
746 platpat[nplatpat] = NULL;
755 while (*p && !(*p == '-' || isspace(*p)))
757 if (*p != '\0') *p++ = '\0';
760 while (*p && !(*p == '-' || isspace(*p)))
763 if (*p != '\0') *p++ = '\0';
767 if (*p != '\0') *p++ = '\0';
770 while (*p && !(*p == '-' || isspace(*p)))
776 while (*p && !(*p == '-' || isspace(*p)))
779 if (*p != '\0') *p++ = '\0';
782 addMacro(NULL, "_host_cpu", NULL, cpu, -1);
783 addMacro(NULL, "_host_vendor", NULL, vendor, -1);
784 addMacro(NULL, "_host_os", NULL, os, -1);
786 platpat = xrealloc(platpat, (nplatpat + 2) * sizeof(*platpat));
787 platpat[nplatpat] = rpmExpand("%{_host_cpu}-%{_host_vendor}-%{_host_os}", (gnu && *gnu ? "-" : NULL), gnu, NULL);
789 platpat[nplatpat] = NULL;
793 rc = (init_platform ? 0 : -1);
801 # if defined(__linux__) && defined(__i386__)
806 * Generic CPUID function
808 static inline void cpuid(unsigned int op, int *eax, int *ebx, int *ecx, int *edx)
813 "movl %%ebx, %%esi \n"
815 : "=a" (*eax), "=S" (*ebx), "=c" (*ecx), "=d" (*edx)
820 * CPUID functions returning a single datum
822 static inline unsigned int cpuid_eax(unsigned int op)
824 unsigned int tmp, val;
825 cpuid(op, &val, &tmp, &tmp, &tmp);
829 static inline unsigned int cpuid_ebx(unsigned int op)
831 unsigned int tmp, val;
832 cpuid(op, &tmp, &val, &tmp, &tmp);
836 static inline unsigned int cpuid_ecx(unsigned int op)
838 unsigned int tmp, val;
839 cpuid(op, &tmp, &tmp, &val, &tmp);
843 static inline unsigned int cpuid_edx(unsigned int op)
845 unsigned int tmp, val;
846 cpuid(op, &tmp, &tmp, &tmp, &val);
850 static sigjmp_buf jenv;
852 static inline void model3(int _unused)
857 static inline int RPMClass(void)
860 unsigned int tfms, junk, cap, capamd;
861 struct sigaction oldsa;
863 sigaction(SIGILL, NULL, &oldsa);
864 signal(SIGILL, model3);
866 if (sigsetjmp(jenv, 1)) {
867 sigaction(SIGILL, &oldsa, NULL);
871 if (cpuid_eax(0x000000000)==0) {
872 sigaction(SIGILL, &oldsa, NULL);
876 cpuid(0x00000001, &tfms, &junk, &junk, &cap);
877 cpuid(0x80000001, &junk, &junk, &junk, &capamd);
881 sigaction(SIGILL, &oldsa, NULL);
887 /* CMOV supported? */
888 if (capamd & (1<<30))
889 return 7; /* 3DNOWEXT supported */
896 /* should only be called for model 6 CPU's */
897 static int is_athlon(void)
899 unsigned int eax, ebx, ecx, edx;
903 cpuid (0, &eax, &ebx, &ecx, &edx);
905 /* If you care about space, you can just check ebx, ecx and edx directly
906 instead of forming a string first and then doing a strcmp */
907 memset(vendor, 0, sizeof(vendor));
910 vendor[i] = (unsigned char) (ebx >>(8*i));
912 vendor[4+i] = (unsigned char) (edx >>(8*i));
914 vendor[8+i] = (unsigned char) (ecx >>(8*i));
916 if (strncmp(vendor, "AuthenticAMD", 12) != 0)
922 static int is_pentium3()
924 unsigned int eax, ebx, ecx, edx, family, model;
926 cpuid(0, &eax, &ebx, &ecx, &edx);
927 memset(vendor, 0, sizeof(vendor));
928 *((unsigned int *)&vendor[0]) = ebx;
929 *((unsigned int *)&vendor[4]) = edx;
930 *((unsigned int *)&vendor[8]) = ecx;
931 if (strncmp(vendor, "GenuineIntel", 12) != 0)
933 cpuid(1, &eax, &ebx, &ecx, &edx);
934 family = (eax >> 8) & 0x0f;
935 model = (eax >> 4) & 0x0f;
939 case 7: // Pentium III, Pentium III Xeon (model 7)
940 case 8: // Pentium III, Pentium III Xeon, Celeron (model 8)
943 Intel recently announced its new technology for mobile platforms,
944 named Centrino, and presents it as a big advance in mobile PCs.
945 One of the main part of Centrino consists in a brand new CPU,
946 the Pentium M, codenamed Banias, that we'll study in this review.
947 A particularity of this CPU is that it was designed for mobile platform
948 exclusively, unlike previous mobile CPU (Pentium III-M, Pentium 4-M)
949 that share the same micro-architecture as their desktop counterparts.
950 The Pentium M introduces a new micro-architecture, adapted for mobility
951 constraints, and that is halfway between the Pentium III and the Pentium 4.
952 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
954 case 10: // Pentium III Xeon (model A)
955 case 11: // Pentium III (model B)
961 static int is_pentium4()
963 unsigned int eax, ebx, ecx, edx, family, model;
965 cpuid(0, &eax, &ebx, &ecx, &edx);
966 memset(vendor, 0, sizeof(vendor));
967 *((unsigned int *)&vendor[0]) = ebx;
968 *((unsigned int *)&vendor[4]) = edx;
969 *((unsigned int *)&vendor[8]) = ecx;
970 if (strncmp(vendor, "GenuineIntel", 12) != 0)
972 cpuid(1, &eax, &ebx, &ecx, &edx);
973 family = (eax >> 8) & 0x0f;
974 model = (eax >> 4) & 0x0f;
978 case 0: // Pentium 4, Pentium 4 Xeon (0.18um)
979 case 1: // Pentium 4, Pentium 4 Xeon MP, Celeron (0.18um)
980 case 2: // Pentium 4, Mobile Pentium 4-M,
981 // Pentium 4 Xeon, Pentium 4 Xeon MP,
982 // Celeron, Mobile Celron (0.13um)
983 case 3: // Pentium 4, Celeron (0.09um)
991 #if defined(__linux__) && defined(__powerpc__)
992 static jmp_buf mfspr_jmpbuf;
994 static void mfspr_ill(int notused)
996 longjmp(mfspr_jmpbuf, -1);
1002 static void defaultMachine(const char ** arch,
1005 static struct utsname un;
1006 static int gotDefaults = 0;
1011 while (!gotDefaults) {
1012 if (!rpmPlatform(platform)) {
1014 s = rpmExpand("%{_host_cpu}", NULL);
1016 strncpy(un.machine, s, sizeof(un.machine));
1017 un.machine[sizeof(un.machine)-1] = '\0';
1020 s = rpmExpand("%{_host_os}", NULL);
1022 strncpy(un.sysname, s, sizeof(un.sysname));
1023 un.sysname[sizeof(un.sysname)-1] = '\0';
1032 #if !defined(__linux__)
1034 /* USUALLY un.sysname on sinix does start with the word "SINIX"
1035 * let's be absolutely sure
1037 strncpy(un.sysname, "SINIX", sizeof(un.sysname));
1039 if (!strcmp(un.sysname, "AIX")) {
1040 strcpy(un.machine, __power_pc() ? "ppc" : "rs6000");
1041 sprintf(un.sysname,"aix%s.%s", un.version, un.release);
1043 else if(!strcmp(un.sysname, "Darwin")) {
1045 strcpy(un.machine, "ppc");
1046 #else ifdef __i386__
1047 strcpy(un.machine, "i386");
1050 else if (!strcmp(un.sysname, "SunOS")) {
1051 if (!strncmp(un.release,"4", 1)) /* SunOS 4.x */ {
1054 (un.release[fd] != 0 && (fd < sizeof(un.release)));
1056 if (!xisdigit(un.release[fd]) && (un.release[fd] != '.')) {
1061 sprintf(un.sysname,"sunos%s",un.release);
1064 else /* Solaris 2.x: n.x.x becomes n-3.x.x */
1065 sprintf(un.sysname, "solaris%1d%s", atoi(un.release)-3,
1066 un.release+1+(atoi(un.release)/10));
1068 /* Solaris on Intel hardware reports i86pc instead of i386
1069 * (at least on 2.6 and 2.8)
1071 if (!strcmp(un.machine, "i86pc"))
1072 sprintf(un.machine, "i386");
1074 else if (!strcmp(un.sysname, "HP-UX"))
1075 /*make un.sysname look like hpux9.05 for example*/
1076 sprintf(un.sysname, "hpux%s", strpbrk(un.release, "123456789"));
1077 else if (!strcmp(un.sysname, "OSF1"))
1078 /*make un.sysname look like osf3.2 for example*/
1079 sprintf(un.sysname, "osf%s", strpbrk(un.release, "123456789"));
1080 else if (!strncmp(un.sysname, "IP", 2))
1081 un.sysname[2] = '\0';
1082 else if (!strncmp(un.sysname, "SINIX", 5)) {
1083 sprintf(un.sysname, "sinix%s",un.release);
1084 if (!strncmp(un.machine, "RM", 2))
1085 sprintf(un.machine, "mips");
1087 else if ((!strncmp(un.machine, "34", 2) ||
1088 !strncmp(un.machine, "33", 2)) && \
1089 !strncmp(un.release, "4.0", 3))
1091 /* we are on ncr-sysv4 */
1092 char * prelid = NULL;
1093 FD_t fd = Fopen("/etc/.relid", "r.fdio");
1095 if (fd != NULL && !Ferror(fd)) {
1096 chptr = xcalloc(1, 256);
1097 { int irelid = Fread(chptr, sizeof(*chptr), 256, fd);
1099 /* example: "112393 RELEASE 020200 Version 01 OS" */
1101 if ((prelid = strstr(chptr, "RELEASE "))){
1102 prelid += strlen("RELEASE ")+1;
1103 sprintf(un.sysname,"ncr-sysv4.%.*s",1,prelid);
1108 chptr = _free (chptr);
1110 if (!gotit) /* parsing /etc/.relid file failed? */
1111 strcpy(un.sysname,"ncr-sysv4");
1112 /* wrong, just for now, find out how to look for i586 later*/
1113 strcpy(un.machine,"i486");
1115 #endif /* __linux__ */
1117 /* get rid of the hyphens in the sysname */
1118 for (chptr = un.machine; *chptr != '\0'; chptr++)
1119 if (*chptr == '/') *chptr = '-';
1121 # if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)
1123 strcpy(un.machine, "mipsel");
1124 # elif defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB)
1126 strcpy(un.machine, "mips");
1129 # if defined(__hpux) && defined(_SC_CPU_VERSION)
1131 # if !defined(CPU_PA_RISC1_2)
1132 # define CPU_PA_RISC1_2 0x211 /* HP PA-RISC1.2 */
1134 # if !defined(CPU_PA_RISC2_0)
1135 # define CPU_PA_RISC2_0 0x214 /* HP PA-RISC2.0 */
1137 int cpu_version = sysconf(_SC_CPU_VERSION);
1139 # if defined(CPU_HP_MC68020)
1140 if (cpu_version == CPU_HP_MC68020)
1141 strcpy(un.machine, "m68k");
1143 # if defined(CPU_HP_MC68030)
1144 if (cpu_version == CPU_HP_MC68030)
1145 strcpy(un.machine, "m68k");
1147 # if defined(CPU_HP_MC68040)
1148 if (cpu_version == CPU_HP_MC68040)
1149 strcpy(un.machine, "m68k");
1152 # if defined(CPU_PA_RISC1_0)
1153 if (cpu_version == CPU_PA_RISC1_0)
1154 strcpy(un.machine, "hppa1.0");
1156 # if defined(CPU_PA_RISC1_1)
1157 if (cpu_version == CPU_PA_RISC1_1)
1158 strcpy(un.machine, "hppa1.1");
1160 # if defined(CPU_PA_RISC1_2)
1161 if (cpu_version == CPU_PA_RISC1_2)
1162 strcpy(un.machine, "hppa1.2");
1164 # if defined(CPU_PA_RISC2_0)
1165 if (cpu_version == CPU_PA_RISC2_0)
1166 strcpy(un.machine, "hppa2.0");
1171 # if defined(__linux__) && defined(__sparc__)
1172 if (!strcmp(un.machine, "sparc")) {
1173 #define PERS_LINUX 0x00000000
1174 #define PERS_LINUX_32BIT 0x00800000
1175 #define PERS_LINUX32 0x00000008
1177 extern int personality(unsigned long);
1180 oldpers = personality(PERS_LINUX_32BIT);
1181 if (oldpers != -1) {
1182 if (personality(PERS_LINUX) != -1) {
1184 if (! strcmp(un.machine, "sparc64")) {
1185 strcpy(un.machine, "sparcv9");
1186 oldpers = PERS_LINUX32;
1189 personality(oldpers);
1192 # endif /* sparc*-linux */
1194 # if defined(__GNUC__) && defined(__alpha__)
1196 unsigned long amask, implver;
1197 register long v0 __asm__("$0") = -1;
1198 __asm__ (".long 0x47e00c20" : "=r"(v0) : "0"(v0));
1200 __asm__ (".long 0x47e03d80" : "=r"(v0));
1205 case 0: strcpy(un.machine, "alphaev5"); break;
1206 case 1: strcpy(un.machine, "alphaev56"); break;
1207 case 0x101: strcpy(un.machine, "alphapca56"); break;
1212 case 0x303: strcpy(un.machine, "alphaev6"); break;
1213 case 0x307: strcpy(un.machine, "alphaev67"); break;
1220 # if defined(__linux__) && defined(__i386__)
1222 char class = (char) (RPMClass() | '0');
1224 if ((class == '6' && is_athlon()) || class == '7')
1225 strcpy(un.machine, "athlon");
1226 else if (is_pentium4())
1227 strcpy(un.machine, "pentium4");
1228 else if (is_pentium3())
1229 strcpy(un.machine, "pentium3");
1230 else if (strchr("3456", un.machine[1]) && un.machine[1] != class)
1231 un.machine[1] = class;
1235 /* the uname() result goes through the arch_canon table */
1236 canon = lookupInCanonTable(un.machine,
1237 tables[RPM_MACHTABLE_INSTARCH].canons,
1238 tables[RPM_MACHTABLE_INSTARCH].canonsLength);
1240 strcpy(un.machine, canon->short_name);
1242 canon = lookupInCanonTable(un.sysname,
1243 tables[RPM_MACHTABLE_INSTOS].canons,
1244 tables[RPM_MACHTABLE_INSTOS].canonsLength);
1246 strcpy(un.sysname, canon->short_name);
1251 if (arch) *arch = un.machine;
1252 if (os) *os = un.sysname;
1256 const char * rpmGetVarArch(int var, const char * arch)
1258 const struct rpmvarValue * next;
1260 if (arch == NULL) arch = current[ARCH];
1263 next = &values[var];
1265 if (next->arch && !strcmp(next->arch, arch)) return next->value;
1270 next = values + var;
1271 while (next && next->arch) next = next->next;
1273 return next ? next->value : NULL;
1276 const char *rpmGetVar(int var)
1278 return rpmGetVarArch(var, NULL);
1281 /* this doesn't free the passed pointer! */
1282 static void freeRpmVar(struct rpmvarValue * orig)
1284 struct rpmvarValue * next, * var = orig;
1288 var->arch = _free(var->arch);
1289 var->value = _free(var->value);
1291 if (var != orig) var = _free(var);
1296 void rpmSetVar(int var, const char * val)
1298 freeRpmVar(&values[var]);
1299 values[var].value = (val ? xstrdup(val) : NULL);
1302 static void rpmSetVarArch(int var, const char * val, const char * arch)
1304 struct rpmvarValue * next = values + var;
1308 while (next->next) {
1309 if (next->arch && !strcmp(next->arch, arch)) break;
1313 while (next->next) {
1314 if (!next->arch) break;
1319 if (next->arch && arch && !strcmp(next->arch, arch)) {
1320 next->value = _free(next->value);
1321 next->arch = _free(next->arch);
1322 } else if (next->arch || arch) {
1323 next->next = xmalloc(sizeof(*next->next));
1331 next->value = _free(next->value);
1332 next->value = xstrdup(val);
1333 next->arch = (arch ? xstrdup(arch) : NULL);
1336 void rpmSetTables(int archTable, int osTable)
1338 const char * arch, * os;
1340 defaultMachine(&arch, &os);
1342 if (currTables[ARCH] != archTable) {
1343 currTables[ARCH] = archTable;
1344 rebuildCompatTables(ARCH, arch);
1347 if (currTables[OS] != osTable) {
1348 currTables[OS] = osTable;
1349 rebuildCompatTables(OS, os);
1353 int rpmMachineScore(int type, const char * name)
1355 machEquivInfo info = machEquivSearch(&tables[type].equiv, name);
1356 return (info != NULL ? info->score : 0);
1359 void rpmGetMachine(const char ** arch, const char ** os)
1362 *arch = current[ARCH];
1368 void rpmSetMachine(const char * arch, const char * os)
1370 const char * host_cpu, * host_os;
1372 defaultMachine(&host_cpu, &host_os);
1376 if (tables[currTables[ARCH]].hasTranslate)
1377 arch = lookupInDefaultTable(arch,
1378 tables[currTables[ARCH]].defaults,
1379 tables[currTables[ARCH]].defaultsLength);
1381 if (arch == NULL) return; /* XXX can't happen */
1385 if (tables[currTables[OS]].hasTranslate)
1386 os = lookupInDefaultTable(os,
1387 tables[currTables[OS]].defaults,
1388 tables[currTables[OS]].defaultsLength);
1390 if (os == NULL) return; /* XXX can't happen */
1392 if (!current[ARCH] || strcmp(arch, current[ARCH])) {
1393 current[ARCH] = _free(current[ARCH]);
1394 current[ARCH] = xstrdup(arch);
1395 rebuildCompatTables(ARCH, host_cpu);
1398 if (!current[OS] || strcmp(os, current[OS])) {
1399 char * t = xstrdup(os);
1400 current[OS] = _free(current[OS]);
1402 * XXX Capitalizing the 'L' is needed to insure that old
1403 * XXX os-from-uname (e.g. "Linux") is compatible with the new
1404 * XXX os-from-platform (e.g "linux" from "sparc-*-linux").
1405 * XXX A copy of this string is embedded in headers and is
1406 * XXX used by rpmInstallPackage->{os,arch}Okay->rpmMachineScore->
1407 * XXX to verify correct arch/os from headers.
1409 if (!strcmp(t, "linux"))
1413 rebuildCompatTables(OS, host_os);
1417 static void rebuildCompatTables(int type, const char * name)
1419 machFindEquivs(&tables[currTables[type]].cache,
1420 &tables[currTables[type]].equiv,
1424 static void getMachineInfo(int type, const char ** name,
1428 int which = currTables[type];
1430 /* use the normal canon tables, even if we're looking up build stuff */
1431 if (which >= 2) which -= 2;
1433 canon = lookupInCanonTable(current[type],
1434 tables[which].canons,
1435 tables[which].canonsLength);
1438 if (num) *num = canon->num;
1439 if (name) *name = canon->short_name;
1441 if (num) *num = 255;
1442 if (name) *name = current[type];
1444 if (tables[currTables[type]].hasCanon) {
1445 rpmlog(RPMLOG_WARNING, _("Unknown system: %s\n"), current[type]);
1446 rpmlog(RPMLOG_WARNING, _("Please contact %s\n"), PACKAGE_BUGREPORT);
1451 void rpmGetArchInfo(const char ** name, int * num)
1453 getMachineInfo(ARCH, name, num);
1456 void rpmGetOsInfo(const char ** name, int * num)
1458 getMachineInfo(OS, name, num);
1461 static void rpmRebuildTargetVars(const char ** target, const char ** canontarget)
1464 char *ca = NULL, *co = NULL, *ct = NULL;
1467 /* Rebuild the compat table to recalculate the current target arch. */
1469 rpmSetMachine(NULL, NULL);
1470 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1471 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
1473 if (target && *target) {
1475 /* Set arch and os from specified build target */
1476 ca = xstrdup(*target);
1477 if ((c = strchr(ca, '-')) != NULL) {
1480 if ((co = strrchr(c, '-')) == NULL) {
1483 if (!xstrcasecmp(co, "-gnu"))
1485 if ((co = strrchr(c, '-')) == NULL)
1490 if (co != NULL) co = xstrdup(co);
1493 const char *a = NULL;
1494 const char *o = NULL;
1495 /* Set build target from rpm arch and os */
1496 rpmGetArchInfo(&a, NULL);
1497 ca = (a) ? xstrdup(a) : NULL;
1498 rpmGetOsInfo(&o, NULL);
1499 co = (o) ? xstrdup(o) : NULL;
1502 /* If still not set, Set target arch/os from default uname(2) values */
1504 const char *a = NULL;
1505 defaultMachine(&a, NULL);
1506 ca = (a) ? xstrdup(a) : NULL;
1508 for (x = 0; ca[x] != '\0'; x++)
1509 ca[x] = xtolower(ca[x]);
1512 const char *o = NULL;
1513 defaultMachine(NULL, &o);
1514 co = (o) ? xstrdup(o) : NULL;
1516 for (x = 0; co[x] != '\0'; x++)
1517 co[x] = xtolower(co[x]);
1519 /* XXX For now, set canonical target to arch-os */
1521 ct = xmalloc(strlen(ca) + sizeof("-") + strlen(co));
1522 sprintf(ct, "%s-%s", ca, co);
1526 * XXX All this macro pokery/jiggery could be achieved by doing a delayed
1527 * rpmInitMacros(NULL, PER-PLATFORM-MACRO-FILE-NAMES);
1529 delMacro(NULL, "_target");
1530 addMacro(NULL, "_target", NULL, ct, RMIL_RPMRC);
1531 delMacro(NULL, "_target_cpu");
1532 addMacro(NULL, "_target_cpu", NULL, ca, RMIL_RPMRC);
1533 delMacro(NULL, "_target_os");
1534 addMacro(NULL, "_target_os", NULL, co, RMIL_RPMRC);
1536 * XXX Make sure that per-arch optflags is initialized correctly.
1538 { const char *optflags = rpmGetVarArch(RPMVAR_OPTFLAGS, ca);
1539 if (optflags != NULL) {
1540 delMacro(NULL, "optflags");
1541 addMacro(NULL, "optflags", NULL, optflags, RMIL_RPMRC);
1553 void rpmFreeRpmrc(void)
1558 for (i = 0; i < nplatpat; i++)
1559 platpat[i] = _free(platpat[i]);
1560 platpat = _free(platpat);
1563 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
1566 if (t->equiv.list) {
1567 for (j = 0; j < t->equiv.count; j++)
1568 t->equiv.list[j].name = _free(t->equiv.list[j].name);
1569 t->equiv.list = _free(t->equiv.list);
1572 if (t->cache.cache) {
1573 for (j = 0; j < t->cache.size; j++) {
1575 e = t->cache.cache + j;
1578 e->name = _free(e->name);
1580 for (k = 0; k < e->count; k++)
1581 e->equivs[k] = _free(e->equivs[k]);
1582 e->equivs = _free(e->equivs);
1585 t->cache.cache = _free(t->cache.cache);
1589 for (j = 0; j < t->defaultsLength; j++) {
1590 t->defaults[j].name = _free(t->defaults[j].name);
1591 t->defaults[j].defName = _free(t->defaults[j].defName);
1593 t->defaults = _free(t->defaults);
1594 t->defaultsLength = 0;
1597 for (j = 0; j < t->canonsLength; j++) {
1598 t->canons[j].name = _free(t->canons[j].name);
1599 t->canons[j].short_name = _free(t->canons[j].short_name);
1601 t->canons = _free(t->canons);
1602 t->canonsLength = 0;
1606 for (i = 0; i < RPMVAR_NUM; i++) {
1607 struct rpmvarValue * vp;
1608 while ((vp = values[i].next) != NULL) {
1609 values[i].next = vp->next;
1610 vp->value = _free(vp->value);
1611 vp->arch = _free(vp->arch);
1614 values[i].value = _free(values[i].value);
1615 values[i].arch = _free(values[i].arch);
1617 current[OS] = _free(current[OS]);
1618 current[ARCH] = _free(current[ARCH]);
1619 defaultsInitialized = 0;
1620 /* FIX: platpat/current may be NULL */
1625 * Read rpmrc (and macro) configuration file(s).
1626 * @param rcfiles colon separated files to read (NULL uses default)
1627 * @return 0 on success
1629 static int rpmReadRC(const char * rcfiles)
1631 char *myrcfiles, *r, *re;
1634 if (!defaultsInitialized) {
1636 defaultsInitialized = 1;
1639 if (rcfiles == NULL)
1640 rcfiles = defrcfiles;
1642 /* Read each file in rcfiles. */
1644 for (r = myrcfiles = xstrdup(rcfiles); r && *r != '\0'; r = re) {
1648 /* Get pointer to rest of files */
1649 for (re = r; (re = strchr(re, ':')) != NULL; re++) {
1650 if (!(re[1] == '/' && re[2] == '/'))
1653 if (re && *re == ':')
1658 /* Expand ~/ to $HOME/ */
1660 if (r[0] == '~' && r[1] == '/') {
1661 const char * home = getenv("HOME");
1663 /* XXX Only /usr/lib/rpm/rpmrc must exist in default rcfiles list */
1664 if (rcfiles == defrcfiles && myrcfiles != r)
1666 rpmlog(RPMERR_RPMRC, _("Cannot expand %s\n"), r);
1670 if (strlen(home) > (sizeof(fn) - strlen(r))) {
1671 rpmlog(RPMERR_RPMRC, _("Cannot read %s, HOME is too large.\n"),
1679 strncat(fn, r, sizeof(fn) - (strlen(fn) + 1));
1680 fn[sizeof(fn)-1] = '\0';
1682 /* Read another rcfile */
1683 fd = Fopen(fn, "r.fpio");
1684 if (fd == NULL || Ferror(fd)) {
1685 /* XXX Only /usr/lib/rpm/rpmrc must exist in default rcfiles list */
1686 if (rcfiles == defrcfiles && myrcfiles != r)
1688 rpmlog(RPMERR_RPMRC, _("Unable to open %s for reading: %s.\n"),
1693 rc = doReadRC(fd, fn);
1697 myrcfiles = _free(myrcfiles);
1701 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
1706 int rpmReadConfigFiles(const char * file, const char * target)
1709 /* Preset target macros */
1710 /* FIX: target can be NULL */
1711 rpmRebuildTargetVars(&target, NULL);
1713 /* Read the files */
1714 if (rpmReadRC(file)) return -1;
1716 if (macrofiles != NULL) {
1717 const char *mf = rpmGetPath(macrofiles, NULL);
1718 rpmInitMacros(NULL, mf);
1722 /* Reset target macros */
1723 rpmRebuildTargetVars(&target, NULL);
1725 /* Finally set target platform */
1726 { const char *cpu = rpmExpand("%{_target_cpu}", NULL);
1727 const char *os = rpmExpand("%{_target_os}", NULL);
1728 rpmSetMachine(cpu, os);
1733 /* Force Lua state initialization */
1735 (void)rpmluaGetPrintBuffer(NULL);
1741 int rpmShowRC(FILE * fp)
1743 struct rpmOption *opt;
1745 machEquivTable equivTable;
1747 /* the caller may set the build arch which should be printed here */
1748 fprintf(fp, "ARCHITECTURE AND OS:\n");
1749 fprintf(fp, "build arch : %s\n", current[ARCH]);
1751 fprintf(fp, "compatible build archs:");
1752 equivTable = &tables[RPM_MACHTABLE_BUILDARCH].equiv;
1753 for (i = 0; i < equivTable->count; i++)
1754 fprintf(fp," %s", equivTable->list[i].name);
1757 fprintf(fp, "build os : %s\n", current[OS]);
1759 fprintf(fp, "compatible build os's :");
1760 equivTable = &tables[RPM_MACHTABLE_BUILDOS].equiv;
1761 for (i = 0; i < equivTable->count; i++)
1762 fprintf(fp," %s", equivTable->list[i].name);
1765 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1766 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
1768 fprintf(fp, "install arch : %s\n", current[ARCH]);
1769 fprintf(fp, "install os : %s\n", current[OS]);
1771 fprintf(fp, "compatible archs :");
1772 equivTable = &tables[RPM_MACHTABLE_INSTARCH].equiv;
1773 for (i = 0; i < equivTable->count; i++)
1774 fprintf(fp," %s", equivTable->list[i].name);
1777 fprintf(fp, "compatible os's :");
1778 equivTable = &tables[RPM_MACHTABLE_INSTOS].equiv;
1779 for (i = 0; i < equivTable->count; i++)
1780 fprintf(fp," %s", equivTable->list[i].name);
1783 fprintf(fp, "\nRPMRC VALUES:\n");
1784 for (i = 0, opt = optionTable; i < optionTableSize; i++, opt++) {
1785 const char *s = rpmGetVar(opt->var);
1786 if (s != NULL || rpmIsVerbose())
1787 fprintf(fp, "%-21s : %s\n", opt->name, s ? s : "(not set)");
1791 fprintf(fp, "Features supported by rpmlib:\n");
1792 rpmShowRpmlibProvides(fp);
1795 rpmDumpMacroTable(NULL, fp);