1 /* 2 * Program to generate the so-thresh testcase, 3 * including associated linked-against shared libraries. 4 * Build as: 5 * 6 * cc -g -o genso-thresh genso-thresh.c 7 * 8 * Invoke as: 9 * 10 * genso-thresh 11 * 12 * It will put all the code in the current directory ("."). 13 * 14 * A makefile can also be generated if the -makemake option is used. 15 * To use the makefile: 16 * 17 * make -f so-thresh.mk all 18 * 19 * The name of the application is 20 * 21 * so-thresh 22 * 23 * (Revised from a program by John Bishop. --rehrauer) 24 */ 25 26 #include <stdio.h> 27 #include <sys/stat.h> 28 #include <sys/fcntl.h> 29 30 int main (argc, argv) 31 int argc; 32 char **argv; 33 { 34 #define NUMBER_OF_INT_VARS 1500 35 #define NUMBER_OF_LIBS 3 36 int lib_num = NUMBER_OF_LIBS; 37 int i; 38 int i2; 39 FILE *main_file; 40 FILE *lib_file; 41 FILE *make_file; 42 FILE *link_file; 43 44 char testcase_name [1000]; 45 char linkfile_name [1000]; 46 char makefile_name [1000]; 47 char mainfile_name [1000]; 48 49 char file_name[100]; 50 /* 51 * 0123456789 <-- length of field 52 * "./fil0000000002.c"; <-- typical filename 53 * 12345678901234567890 <-- length of string 54 * 10 20 55 * ^where null goes 56 */ 57 char file_name_core[100]; 58 59 /* Verify input. 60 */ 61 if ((argc < 1) || (argc > 2) || (argv == NULL) || 62 ((argc == 2) && (strcmp (argv[1], "-makemake") != 0))) 63 { 64 printf ("** Syntax: %s [-makemake]\n", argv[0]); 65 return; 66 } 67 68 if (strncmp (argv[0], "gen", 3) != 0) 69 { 70 printf ("** This tool expected to be named \"gen<something>\"\n"); 71 return; 72 } 73 strcpy (testcase_name, argv[0]+3); 74 75 strcpy (linkfile_name, testcase_name); 76 strcat (linkfile_name, ".lopt"); 77 link_file = fopen (linkfile_name, "w"); 78 fprintf (link_file, "# Linker options for %s test\n", testcase_name); 79 80 /* Generate the makefile, if requested. 81 */ 82 if (argc == 2) 83 { 84 strcpy (makefile_name, testcase_name); 85 strcat (makefile_name, ".mk.new"); 86 make_file = fopen (makefile_name, "w"); 87 printf (" Note: New makefile (%s) generated.\n", makefile_name); 88 printf (" May want to update existing makefile, if any.\n"); 89 fprintf (make_file, "# Generated automatically by %s\n", argv[0]); 90 fprintf (make_file, "# Make file for %s test\n", testcase_name); 91 fprintf (make_file, "\n"); 92 fprintf (make_file, "CFLAGS = +DA1.1 -g\n"); 93 fprintf (make_file, "\n"); 94 fprintf (make_file, "# This is how to build this generator.\n"); 95 fprintf (make_file, "%s.o: %s.c\n", argv[0], argv[0]); 96 fprintf (make_file, "\t$(CC) $(CFLAGS) -o %s.o -c %s.c\n", argv[0], argv[0]); 97 fprintf (make_file, "%s: %s.o\n", argv[0], argv[0]); 98 fprintf (make_file, "\t$(CC) $(CFLAGS) -o %s %s.o\n", argv[0], argv[0]); 99 fprintf (make_file, "\n"); 100 fprintf (make_file, "# This is how to run this generator.\n"); 101 fprintf (make_file, "# This target should be made before the 'all' target,\n"); 102 fprintf (make_file, "# to ensure that the shlib sources are all available.\n"); 103 fprintf (make_file, "require_shlibs: %s\n", argv[0]); 104 for (i=0; i < lib_num; i++) 105 { 106 fprintf (make_file, "\tif ! [ -a lib%2.2d_%s.c ] ; then \\\n", i, testcase_name); 107 fprintf (make_file, "\t %s ; \\\n", argv[0]); 108 fprintf (make_file, "\tfi\n"); 109 } 110 fprintf (make_file, "\n"); 111 fprintf (make_file, "# This is how to build all the shlibs.\n"); 112 fprintf (make_file, "# Be sure to first make the require_shlibs target!\n"); 113 for (i=0; i < lib_num; i++) 114 { 115 fprintf (make_file, "lib%2.2d_%s.o: lib%2.2d_%s.c\n", i, testcase_name, i, testcase_name); 116 fprintf (make_file, "\t$(CC) $(CFLAGS) +Z -o lib%2.2d_%s.o -c lib%2.2d_%s.c\n", i, testcase_name, i, testcase_name); 117 fprintf (make_file, "lib%2.2d-%s.sl: lib%2.2d-%s.o\n", i, testcase_name, i, testcase_name); 118 fprintf (make_file, "\t$(LD) $(LDFLAGS) -b -o lib%2.2d-%s.sl lib%2.2d-%s.o\n", i, testcase_name, i, testcase_name); 119 } 120 fprintf (make_file, "\n"); 121 fprintf (make_file, "# For convenience, here's names for all pieces of all shlibs.\n"); 122 fprintf (make_file, "SHLIB_SOURCES = \\\n"); 123 for (i=0; i < lib_num-1; i++) 124 fprintf (make_file, "\tlib%2.2d-%s.c \\\n", i, testcase_name); 125 fprintf (make_file, "\tlib%2.2d-%s.c\n", lib_num-1, testcase_name); 126 fprintf (make_file, "SHLIB_OBJECTS = $(SHLIB_SOURCES:.c=.o)\n"); 127 fprintf (make_file, "SHLIBS = $(SHLIB_SOURCES:.c=.sl)\n"); 128 fprintf (make_file, "SHLIB_NAMES = $(SHLIB_SOURCES:.c=)\n"); 129 fprintf (make_file, "EXECUTABLES = $(SHLIBS) %s %s\n", argv[0], testcase_name); 130 fprintf (make_file, "OBJECT_FILES = $(SHLIB_OBJECTS) %s.o %s.o\n", argv[0], testcase_name); 131 fprintf (make_file, "\n"); 132 fprintf (make_file, "shlib_objects: $(SHLIB_OBJECTS)\n"); 133 fprintf (make_file, "shlibs: $(SHLIBS)\n"); 134 fprintf (make_file, "\n"); 135 fprintf (make_file, "# This is how to build the debuggable testcase that uses the shlibs.\n"); 136 fprintf (make_file, "%s.o: %s.c\n", testcase_name, testcase_name); 137 fprintf (make_file, "\t$(CC) $(CFLAGS) -o %s.o -c %s.c\n", testcase_name, testcase_name); 138 fprintf (make_file, "%s: shlibs %s.o\n", testcase_name, testcase_name); 139 fprintf (make_file, "\t$(LD) $(LDFLAGS) -o %s -lc -L. ", testcase_name); 140 fprintf (make_file, "-c %s /opt/langtools/lib/end.o /lib/crt0.o %s.o\n", linkfile_name, testcase_name); 141 fprintf (make_file, "\n"); 142 fprintf (make_file, "# Yeah, but you should first make the require_shlibs target!\n"); 143 fprintf (make_file, "all: %s %s\n", testcase_name, argv[0]); 144 fprintf (make_file, "\n"); 145 fprintf (make_file, "# To remove everything built via this makefile...\n"); 146 fprintf (make_file, "clean:\n"); 147 /* Do this carefully, to avoid hitting silly HP-UX ARG_MAX limits... */ 148 fprintf (make_file, "\trm -f lib0*-%s.*\n", testcase_name); 149 fprintf (make_file, "\trm -f lib1*-%s.*\n", testcase_name); 150 fprintf (make_file, "\trm -f lib2*-%s.*\n", testcase_name); 151 fprintf (make_file, "\trm -f lib3*-%s.*\n", testcase_name); 152 fprintf (make_file, "\trm -f lib4*-%s.*\n", testcase_name); 153 fprintf (make_file, "\trm -f lib5*-%s.*\n", testcase_name); 154 fprintf (make_file, "\trm -f lib6*-%s.*\n", testcase_name); 155 fprintf (make_file, "\trm -f lib7*-%s.*\n", testcase_name); 156 fprintf (make_file, "\trm -f lib8*-%s.*\n", testcase_name); 157 fprintf (make_file, "\trm -f lib9*-%s.*\n", testcase_name); 158 fprintf (make_file, "\trm -f %s %s %s %s.c\n", argv[0], testcase_name, linkfile_name, testcase_name); 159 fprintf (make_file, "\n"); 160 fclose (make_file); 161 } 162 163 /* Generate the code for the libraries. 164 */ 165 for (i=0; i < lib_num; i++) { 166 167 /* Generate the names for the library. 168 */ 169 sprintf (file_name, "lib%2.2d-%s.c", i, testcase_name); 170 sprintf (file_name_core, "lib%2.2d-%s", i, testcase_name); 171 172 /* Generate the source code. 173 */ 174 lib_file = fopen (file_name, "w"); 175 fprintf (lib_file, "/* Shared library file number %d */\n", i); 176 fprintf (lib_file, "#include <stdio.h>\n\n"); 177 fprintf (lib_file, "/* The following variables largely exist to bloat this library's debug info. */\n"); 178 fprintf (lib_file, "static char c_static_buf_%d [100];\n", i); 179 for (i2=0; i2<NUMBER_OF_INT_VARS; i2++) 180 fprintf (lib_file, "int i_%d_%d;\n", i, i2); 181 fprintf (lib_file, "\nint r_%d ()\n", i); 182 fprintf (lib_file, "{\n"); 183 for (i2=0; i2<NUMBER_OF_INT_VARS; i2++) 184 fprintf (lib_file, " i_%d_%d = %d*%d;\n", i, i2, i2, i2); 185 fprintf (lib_file, " return 1;\n"); 186 fprintf (lib_file, "}\n\n"); 187 fprintf (lib_file, "/* end of generated file */\n"); 188 fclose (lib_file); 189 190 /* Add a linker options line 191 */ 192 fprintf (link_file, "-l%2.2d-%s\n", i, testcase_name); 193 } 194 195 /* Generate the "main" file. 196 */ 197 strcpy (mainfile_name, testcase_name); 198 strcat (mainfile_name, ".c"); 199 main_file = fopen (mainfile_name, "w"); 200 fprintf (main_file, "/* Generated test progam with %d shared libraries. */\n\n", 201 lib_num); 202 fprintf (main_file, "#include <stdio.h>\n\n"); 203 204 for (i = 0; i < lib_num; i++) { 205 fprintf (main_file, "extern int r_%d();\n", i); 206 } 207 208 fprintf (main_file, "\n"); 209 fprintf (main_file, "int main()\n"); 210 fprintf (main_file, "{\n"); 211 fprintf (main_file, " int accum;\n"); 212 fprintf (main_file, " int lib_num = %d;\n", lib_num); 213 214 for (i = 0; i < lib_num; i++) { 215 fprintf (main_file, " accum += r_%d();\n", i); 216 } 217 218 fprintf (main_file, " printf( \"Final value: %%d, should be %%d\\n\", accum, lib_num );\n\n"); 219 fprintf (main_file, " return 0;\n"); 220 fprintf (main_file, "}\n\n"); 221 fprintf (main_file, "/* end of generated file */\n"); 222 fclose (main_file); 223 224 /* Finish up the link file and the build file 225 */ 226 fclose (link_file); 227 } 228 229 /* End of file */ 230