1 /*
2  * Copyright © 2011 inria.  All rights reserved.
3  * See COPYING in top-level directory.
4  */
5 
6 #include <private/autogen/config.h>
7 #include <hwloc.h>
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 
usage(char * name,FILE * where)13 static void usage(char *name, FILE *where)
14 {
15   fprintf (where, "Usage: %s [options] <output>.xml [-n <name1] <input1>.xml [-n name2] <input2>.xml ...\n", name);
16   fprintf (where, "Options:\n");
17   fprintf (where, "  -v --verbose      Show verbose messages\n");
18   fprintf (where, "  -f --force        Ignore errors while reading input files\n");
19   fprintf (where, "  -n --name <name>  Set the name of the next input topology\n");
20 }
21 
main(int argc,char * argv[])22 int main(int argc, char *argv[])
23 {
24   hwloc_topology_t topology;
25   char *callname;
26   char *output;
27   int verbose = 0;
28   int force = 0;
29   int opt;
30   int i, j;
31 
32   callname = strrchr(argv[0], '/');
33   if (!callname)
34     callname = argv[0];
35   else
36     callname++;
37   argc--;
38   argv++;
39 
40   while (argc >= 1) {
41     opt = 0;
42     if (!strcmp(argv[0], "-v") || !strcmp(argv[0], "--verbose")) {
43       verbose++;
44     } else if (!strcmp(argv[0], "-f") || !strcmp(argv[0], "--force")) {
45       force = 1;
46     } else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
47       usage(callname, stdout);
48       exit(EXIT_SUCCESS);
49     } else if (!strcmp(argv[0], "--")) {
50       argc--;
51       argv++;
52       break;
53     } else if (*argv[0] == '-') {
54       fprintf(stderr, "Unrecognized option: %s\n", argv[0]);
55       usage(callname, stderr);
56       exit(EXIT_FAILURE);
57     } else
58       break;
59     argc -= opt+1;
60     argv += opt+1;
61   }
62 
63   if (!argc) {
64     fprintf(stderr, "Missing output file name\n");
65     usage(callname, stderr);
66     exit(EXIT_FAILURE);
67   }
68   output = argv[0];
69   argc--;
70   argv++;
71 
72   hwloc_topology_init(&topology);
73   hwloc_topology_set_custom(topology);
74 
75   for(i=0, j=0; i<argc; i++, j++) {
76     hwloc_topology_t input;
77     hwloc_obj_t root;
78     char idx[10];
79     char *name = NULL;
80 
81     if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "--name")) {
82       if (i+2 >= argc) {
83 	usage(callname, stderr);
84 	exit(EXIT_FAILURE);
85       }
86       name = argv[i+1];
87       i+=2;
88     }
89 
90     if (verbose) {
91       if (name)
92 	printf("Importing XML topology %s with name %s ...\n", argv[i], name);
93       else
94 	printf("Importing XML topology %s ...\n", argv[i]);
95     }
96 
97     hwloc_topology_init(&input);
98     if (hwloc_topology_set_xml(input, argv[i])) {
99       fprintf(stderr, "Failed to set source XML file %s (%s)\n", argv[i], strerror(errno));
100       hwloc_topology_destroy(input);
101       if (force)
102 	continue;
103       else
104 	return EXIT_FAILURE;
105     }
106     hwloc_topology_load(input);
107 
108     root = hwloc_get_root_obj(input);
109     hwloc_obj_add_info(root, "AssemblerName", name ? name : argv[i]);
110     snprintf(idx, sizeof(idx), "%d", j);
111     hwloc_obj_add_info(root, "AssemblerIndex", idx);
112 
113     hwloc_custom_insert_topology(topology, hwloc_get_root_obj(topology), input, NULL);
114     hwloc_topology_destroy(input);
115   }
116 
117   if (verbose)
118     printf("Assembling global topology...\n");
119   hwloc_topology_load(topology);
120   if (hwloc_topology_export_xml(topology, output) < 0) {
121     fprintf(stderr, "Failed to export XML to %s (%s)\n", output, strerror(errno));
122     return EXIT_FAILURE;
123   }
124   hwloc_topology_destroy(topology);
125   if (verbose)
126     printf("Exported topology to XML file %s\n", output);
127   return 0;
128 }
129