1 /* Copyright (C) 2011 IBM Corporation and Others. All Rights Reserved */
2
3 /**
4 Input:
5 -o makeconv makeconv.o ucnvstat.o ../../lib/libicuuc48.so -qOPTION='*DUPPROC *DUPVAR*'
6
7 CRTPGM PGM(SRLICU/MAKECONV) MODULE(SRLICU/MAKECONV SRLICU/UCNVSTAT SRLICU/GENMBCS SRLICU/GENCNVEX) BNDSRVPGM(SRLICU/LIBICUUC48 SRLICU/LIBICUTU48 SRLICU/LIBICUIN48) OPTION(*DUPPROC *DUPVAR) REPLACE(*YES)
8
9 Handles .o ( modules ), .so ( srvpgm ), .a ( bnddir ).
10
11 TODO:
12
13 - cleanup
14 - much better error handling
15 - factor common code
16 - instead of caring about .o vs .so vs .a, just read the link - if it ends in .srvpgm then treat it as a service program, etc.
17
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #ifndef TEST_MODE
26 #define TEST_MODE 0
27 #endif
28
29
30 #if !TEST_MODE
31 #include <qp0z1170.h>
32 #else
Qp0zSystem(const char * cmd)33 static int Qp0zSystem(const char *cmd) {
34 printf("CL: %s\n", cmd);
35 return 0;
36 }
37 #endif
38
runcmd(const char * cmd)39 static int runcmd(const char *cmd) {
40 int rc;
41 printf("%s\n", cmd);
42 rc = Qp0zSystem(cmd);
43 if(rc==0) {
44 printf("..ok\n");
45 return 0;
46 } else if(rc<0){
47 printf("..Qp0zSystem failed.\n");
48 return 1;
49 } else {
50 printf("..System call failed.\n");
51 return 1;
52 }
53 }
54
main(int argc,const char * argv[])55 int main(int argc, const char *argv[]) {
56 int i;
57
58 char buf[8048];
59 char opt[4100];
60 char objs[4024];
61 char libs[4024];
62 char bnddirs[4024];
63 const char *prog="";
64 const char *progshort=prog;
65 const char *outputdir=getenv("OUTPUTDIR");
66
67 printf("# OUTPUTDIR=%s ",outputdir);
68 for(i=0;i<argc;i++) {
69 printf("%s ", argv[i]);
70 }
71 printf("\n");
72
73 buf[0]=0;
74 opt[0]=0;
75 objs[0]=0;
76 libs[0]=0;
77 bnddirs[0]=0;
78
79 for(i=1;i<argc;i++) {
80 if(argv[i][0]=='-') {
81 switch(argv[i][1]) {
82 case 'O':
83 printf(".. ignoring optimization: %s\n", argv[i]);
84 break;
85 case 'g':
86 printf(".. ignoring debugging: %s\n", argv[i]);
87 break;
88 case 'l':
89 printf(".. ignoring lib: %s\n", argv[i]);
90 break;
91 case 'v':
92 printf(".. already verbose\n");
93 break;
94 case 'o':
95 i++;
96 prog=argv[i];
97 progshort=strrchr(prog,'/');
98 if(!progshort) {
99 progshort=prog;
100 } else {
101 progshort++; /* / */
102 }
103 break;
104 case 'q':
105 if(!strncmp(argv[i]+2,"OPTION=",7)) {
106 strcat(opt,argv[i]+9);
107 } else {
108 printf("Unknown -q option: %s\n", argv[i]);
109 return 1;
110 }
111 break;
112 default:
113 printf("Unknown option: %s\n", argv[i]);
114 return 1;
115 }
116 } else {
117 int n = strlen(argv[i]);
118 if(argv[i][n-1]=='o' &&
119 argv[i][n-2]=='.') {
120 const char *b = argv[i];
121 char linkbuf[200];
122 char outbuf[100];
123 int nlen = n-2;
124
125 if(nlen >= 10) {
126 nlen = 10;
127 }
128
129 if(readlink(b,linkbuf,200)>0) {
130 /* printf("linkbuf %s for %s\n", linkbuf, b); */
131 /* /qsys.lib/srlicu.lib/currtest.module */
132 char *mend = strrchr(linkbuf,'.');
133 if(mend) {
134 *mend=0;
135 mend = strrchr(linkbuf,'/');
136 if(mend) {
137 mend++;
138 strcpy(outbuf,mend);
139 b=outbuf;
140 nlen=strlen(b);
141 }
142 }
143 } else {
144 /* perror("readlink");
145 puts(b); */
146 }
147
148 strcat(objs,outputdir);
149 strcat(objs,"/");
150 strncat(objs,b,nlen);
151 strcat(objs, " ");
152 } else if(argv[i][n-1]=='a' &&
153 argv[i][n-2]=='.') {
154 const char *b = argv[i];
155 char linkbuf[200];
156 char outbuf[100];
157 int nlen = n-2;
158
159 if(nlen >= 10) {
160 nlen = 10;
161 }
162
163 if(readlink(b,linkbuf,200)>0) {
164 /* printf("linkbuf %s for %s\n", linkbuf, b); */
165 /* /qsys.lib/srlicu.lib/currtest.srvpgm */
166 char *mend = strrchr(linkbuf,'.');
167 if(mend) {
168 *mend=0;
169 mend = strrchr(linkbuf,'/');
170 if(mend) {
171 mend++;
172 strcpy(outbuf,mend);
173 b=outbuf;
174 nlen=strlen(b);
175 }
176 }
177 } else {
178 /* perror("readlink");
179 puts(b); */
180 }
181
182 strcat(bnddirs,outputdir);
183 strcat(bnddirs,"/");
184 strncat(bnddirs,b,nlen);
185 strcat(bnddirs, " ");
186 } else if(argv[i][n-1]=='o' &&
187 argv[i][n-2]=='s' &&
188 argv[i][n-3]=='.') {
189 const char *p = strrchr(argv[i],'/');
190 if(!p) {
191 printf("Can't find trailing slash in %s\n", argv[i]);
192 return 1;
193 }
194 strcat(libs,outputdir);
195 strcat(libs,"/");
196 strncat(libs,p+1,strlen(p)-4);
197 strcat(libs," ");
198 } else {
199 printf("Unknown input file: %s\n", argv[i]);
200 return 1;
201 }
202 }
203 }
204
205 if(prog[0]==0) {
206 printf("no program (-o) option specified.\n");
207 return 1;
208 }
209
210 sprintf(buf,"CRTPGM PGM(%s/%s) MODULE(%s) BNDSRVPGM(%s) BNDDIR(%s) OPTION(%s) REPLACE(*YES)",
211 outputdir,progshort,
212
213 objs,
214
215 libs,
216
217 bnddirs,
218
219 opt);
220
221
222 if(runcmd(buf)) {
223 return 1;
224 }
225
226 /* -- OK */
227 {
228 char path1[1000];
229 sprintf(path1,"/qsys.lib/%s.lib/%s.pgm",
230 outputdir,
231 progshort);
232 printf("# ln -s %s %s\n", path1, prog);
233 if((!TEST_MODE) && symlink(path1,prog)) {
234 perror("symlink");
235 if(errno!=EEXIST) { /* ignored */
236 return 1;
237 }
238 }
239 }
240 return 0;
241 }
242
243
244
245
246
247
248
249
250