xref: /xnu-8792.41.9/SETUP/json_compilation_db/json_compilation_db.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2013 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*5c2921b0SApple OSS Distributions  * file.
12*5c2921b0SApple OSS Distributions  *
13*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*5c2921b0SApple OSS Distributions  * limitations under the License.
20*5c2921b0SApple OSS Distributions  *
21*5c2921b0SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*5c2921b0SApple OSS Distributions  */
23*5c2921b0SApple OSS Distributions 
24*5c2921b0SApple OSS Distributions /*
25*5c2921b0SApple OSS Distributions  * json_compilation_db is a helper tool that takes a compiler invocation, and
26*5c2921b0SApple OSS Distributions  * appends it in JSON format to the specified database.
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions 
29*5c2921b0SApple OSS Distributions #include <stdio.h>
30*5c2921b0SApple OSS Distributions #include <stdlib.h>
31*5c2921b0SApple OSS Distributions #include <unistd.h>
32*5c2921b0SApple OSS Distributions #include <string.h>
33*5c2921b0SApple OSS Distributions #include <stdbool.h>
34*5c2921b0SApple OSS Distributions #include <errno.h>
35*5c2921b0SApple OSS Distributions #include <err.h>
36*5c2921b0SApple OSS Distributions #include <sysexits.h>
37*5c2921b0SApple OSS Distributions 
38*5c2921b0SApple OSS Distributions #include <sys/stat.h>
39*5c2921b0SApple OSS Distributions #include <sys/fcntl.h>
40*5c2921b0SApple OSS Distributions #include <sys/param.h>
41*5c2921b0SApple OSS Distributions 
42*5c2921b0SApple OSS Distributions void usage(void);
43*5c2921b0SApple OSS Distributions char *escape_string(const char *);
44*5c2921b0SApple OSS Distributions 
45*5c2921b0SApple OSS Distributions /*
46*5c2921b0SApple OSS Distributions  * We support appending to two databases.
47*5c2921b0SApple OSS Distributions  *
48*5c2921b0SApple OSS Distributions  * 0-byte: ""
49*5c2921b0SApple OSS Distributions  *
50*5c2921b0SApple OSS Distributions  * or
51*5c2921b0SApple OSS Distributions  *
52*5c2921b0SApple OSS Distributions  * "["
53*5c2921b0SApple OSS Distributions  * "{"
54*5c2921b0SApple OSS Distributions  * "  ..."
55*5c2921b0SApple OSS Distributions  * "}"
56*5c2921b0SApple OSS Distributions  * "]"
57*5c2921b0SApple OSS Distributions  */
58*5c2921b0SApple OSS Distributions 
59*5c2921b0SApple OSS Distributions int
main(int argc,char * argv[])60*5c2921b0SApple OSS Distributions main(int argc, char * argv[])
61*5c2921b0SApple OSS Distributions {
62*5c2921b0SApple OSS Distributions 	struct stat sb;
63*5c2921b0SApple OSS Distributions 	int ret;
64*5c2921b0SApple OSS Distributions 	int dstfd;
65*5c2921b0SApple OSS Distributions 	FILE *dst = NULL;
66*5c2921b0SApple OSS Distributions 	const char *json_output = NULL;
67*5c2921b0SApple OSS Distributions 	const char *cwd = NULL;
68*5c2921b0SApple OSS Distributions 	const char *input_file = NULL;
69*5c2921b0SApple OSS Distributions 	char start[2];
70*5c2921b0SApple OSS Distributions 	size_t read_bytes;
71*5c2921b0SApple OSS Distributions 	int i;
72*5c2921b0SApple OSS Distributions 	size_t input_file_len;
73*5c2921b0SApple OSS Distributions 
74*5c2921b0SApple OSS Distributions 	if (argc < 5) {
75*5c2921b0SApple OSS Distributions 		usage();
76*5c2921b0SApple OSS Distributions 	}
77*5c2921b0SApple OSS Distributions 
78*5c2921b0SApple OSS Distributions 	json_output = argv[1];
79*5c2921b0SApple OSS Distributions 	cwd = argv[2];
80*5c2921b0SApple OSS Distributions 	input_file = argv[3];
81*5c2921b0SApple OSS Distributions 
82*5c2921b0SApple OSS Distributions 	argv += 4;
83*5c2921b0SApple OSS Distributions 	argc -= 4;
84*5c2921b0SApple OSS Distributions 
85*5c2921b0SApple OSS Distributions 	input_file_len = strlen(input_file);
86*5c2921b0SApple OSS Distributions 	if (!(input_file_len > 2 && 0 == strcmp(".c", input_file + input_file_len - 2)) &&
87*5c2921b0SApple OSS Distributions 	    !(input_file_len > 3 && 0 == strcmp(".cp", input_file + input_file_len - 3)) &&
88*5c2921b0SApple OSS Distributions 	    !(input_file_len > 4 && 0 == strcmp(".cpp", input_file + input_file_len - 4))) {
89*5c2921b0SApple OSS Distributions 		/* Not a C/C++ file, just skip it */
90*5c2921b0SApple OSS Distributions 		return 0;
91*5c2921b0SApple OSS Distributions 	}
92*5c2921b0SApple OSS Distributions 
93*5c2921b0SApple OSS Distributions 	dstfd = open(json_output, O_RDWR | O_CREAT | O_EXLOCK, DEFFILEMODE);
94*5c2921b0SApple OSS Distributions 	if (dstfd < 0) {
95*5c2921b0SApple OSS Distributions 		err(EX_NOINPUT, "open(%s)", json_output);
96*5c2921b0SApple OSS Distributions 	}
97*5c2921b0SApple OSS Distributions 
98*5c2921b0SApple OSS Distributions 	ret = fstat(dstfd, &sb);
99*5c2921b0SApple OSS Distributions 	if (ret < 0) {
100*5c2921b0SApple OSS Distributions 		err(EX_NOINPUT, "fstat(%s)", json_output);
101*5c2921b0SApple OSS Distributions 	}
102*5c2921b0SApple OSS Distributions 
103*5c2921b0SApple OSS Distributions 	if (!S_ISREG(sb.st_mode)) {
104*5c2921b0SApple OSS Distributions 		err(EX_USAGE, "%s is not a regular file", json_output);
105*5c2921b0SApple OSS Distributions 	}
106*5c2921b0SApple OSS Distributions 
107*5c2921b0SApple OSS Distributions 	dst = fdopen(dstfd, "w+");
108*5c2921b0SApple OSS Distributions 	if (dst == NULL) {
109*5c2921b0SApple OSS Distributions 		err(EX_UNAVAILABLE, "fdopen");
110*5c2921b0SApple OSS Distributions 	}
111*5c2921b0SApple OSS Distributions 
112*5c2921b0SApple OSS Distributions 	read_bytes = fread(start, sizeof(start[0]), sizeof(start) / sizeof(start[0]), dst);
113*5c2921b0SApple OSS Distributions 	if ((read_bytes != sizeof(start)) || (0 != memcmp(start, "[\n", sizeof(start) / sizeof(start[0])))) {
114*5c2921b0SApple OSS Distributions 		/* no JSON start, we don't really care why */
115*5c2921b0SApple OSS Distributions 		ret = fseeko(dst, 0, SEEK_SET);
116*5c2921b0SApple OSS Distributions 		if (ret < 0) {
117*5c2921b0SApple OSS Distributions 			err(EX_UNAVAILABLE, "fseeko");
118*5c2921b0SApple OSS Distributions 		}
119*5c2921b0SApple OSS Distributions 
120*5c2921b0SApple OSS Distributions 		ret = fputs("[", dst);
121*5c2921b0SApple OSS Distributions 		if (ret < 0) {
122*5c2921b0SApple OSS Distributions 			err(EX_UNAVAILABLE, "fputs");
123*5c2921b0SApple OSS Distributions 		}
124*5c2921b0SApple OSS Distributions 	} else {
125*5c2921b0SApple OSS Distributions 		/* has at least two bytes at the start. Seek to 3 bytes before the end */
126*5c2921b0SApple OSS Distributions 		ret = fseeko(dst, -3, SEEK_END);
127*5c2921b0SApple OSS Distributions 		if (ret < 0) {
128*5c2921b0SApple OSS Distributions 			err(EX_UNAVAILABLE, "fseeko");
129*5c2921b0SApple OSS Distributions 		}
130*5c2921b0SApple OSS Distributions 
131*5c2921b0SApple OSS Distributions 		ret = fputs(",", dst);
132*5c2921b0SApple OSS Distributions 		if (ret < 0) {
133*5c2921b0SApple OSS Distributions 			err(EX_UNAVAILABLE, "fputs");
134*5c2921b0SApple OSS Distributions 		}
135*5c2921b0SApple OSS Distributions 	}
136*5c2921b0SApple OSS Distributions 
137*5c2921b0SApple OSS Distributions 	fprintf(dst, "\n");
138*5c2921b0SApple OSS Distributions 	fprintf(dst, "{\n");
139*5c2921b0SApple OSS Distributions 	fprintf(dst, "  \"directory\": \"%s\",\n", cwd);
140*5c2921b0SApple OSS Distributions 	fprintf(dst, "  \"file\": \"%s\",\n", input_file);
141*5c2921b0SApple OSS Distributions 	fprintf(dst, "  \"command\": \"");
142*5c2921b0SApple OSS Distributions 	for (i = 0; i < argc; i++) {
143*5c2921b0SApple OSS Distributions 		bool needs_escape = strchr(argv[i], '\\') || strchr(argv[i], '"') || strchr(argv[i], ' ');
144*5c2921b0SApple OSS Distributions 
145*5c2921b0SApple OSS Distributions 		if (needs_escape) {
146*5c2921b0SApple OSS Distributions 			char *escaped_string = escape_string(argv[i]);
147*5c2921b0SApple OSS Distributions 			fprintf(dst, "%s\\\"%s\\\"", i == 0 ? "" : " ", escaped_string);
148*5c2921b0SApple OSS Distributions 			free(escaped_string);
149*5c2921b0SApple OSS Distributions 		} else {
150*5c2921b0SApple OSS Distributions 			fprintf(dst, "%s%s", i == 0 ? "" : " ", argv[i]);
151*5c2921b0SApple OSS Distributions 		}
152*5c2921b0SApple OSS Distributions 	}
153*5c2921b0SApple OSS Distributions 	fprintf(dst, "\"\n");
154*5c2921b0SApple OSS Distributions 	fprintf(dst, "}\n");
155*5c2921b0SApple OSS Distributions 	fprintf(dst, "]\n");
156*5c2921b0SApple OSS Distributions 
157*5c2921b0SApple OSS Distributions 	ret = fclose(dst);
158*5c2921b0SApple OSS Distributions 	if (ret < 0) {
159*5c2921b0SApple OSS Distributions 		err(EX_UNAVAILABLE, "fclose");
160*5c2921b0SApple OSS Distributions 	}
161*5c2921b0SApple OSS Distributions 
162*5c2921b0SApple OSS Distributions 	return 0;
163*5c2921b0SApple OSS Distributions }
164*5c2921b0SApple OSS Distributions 
165*5c2921b0SApple OSS Distributions void
usage(void)166*5c2921b0SApple OSS Distributions usage(void)
167*5c2921b0SApple OSS Distributions {
168*5c2921b0SApple OSS Distributions 	fprintf(stderr, "Usage: %s <json_output> <cwd> <input_file> <compiler> [<invocation> ...]\n", getprogname());
169*5c2921b0SApple OSS Distributions 	exit(EX_USAGE);
170*5c2921b0SApple OSS Distributions }
171*5c2921b0SApple OSS Distributions 
172*5c2921b0SApple OSS Distributions /*
173*5c2921b0SApple OSS Distributions  * A valid JSON string can't contain \ or ", so we look for these in our argv[] array (which
174*5c2921b0SApple OSS Distributions  * our parent shell would have done shell metacharacter evaluation on, and escape just these.
175*5c2921b0SApple OSS Distributions  * The entire string is put in \" escaped quotes to handle spaces that are valid JSON
176*5c2921b0SApple OSS Distributions  * but should be used for grouping when running the compiler for real.
177*5c2921b0SApple OSS Distributions  */
178*5c2921b0SApple OSS Distributions char *
escape_string(const char * input)179*5c2921b0SApple OSS Distributions escape_string(const char *input)
180*5c2921b0SApple OSS Distributions {
181*5c2921b0SApple OSS Distributions 	size_t len = strlen(input);
182*5c2921b0SApple OSS Distributions 	size_t i, j;
183*5c2921b0SApple OSS Distributions 	char *output = malloc(len * 4 + 1);
184*5c2921b0SApple OSS Distributions 
185*5c2921b0SApple OSS Distributions 	for (i = 0, j = 0; i < len; i++) {
186*5c2921b0SApple OSS Distributions 		char ch = input[i];
187*5c2921b0SApple OSS Distributions 
188*5c2921b0SApple OSS Distributions 		if (ch == '\\' || ch == '"') {
189*5c2921b0SApple OSS Distributions 			output[j++] = '\\';
190*5c2921b0SApple OSS Distributions 			output[j++] = '\\'; /* output \\ in JSON, which the final shell will see as \ */
191*5c2921b0SApple OSS Distributions 			output[j++] = '\\'; /* escape \ or ", which the final shell will see and pass to the compiler */
192*5c2921b0SApple OSS Distributions 		}
193*5c2921b0SApple OSS Distributions 		output[j++] = ch;
194*5c2921b0SApple OSS Distributions 	}
195*5c2921b0SApple OSS Distributions 
196*5c2921b0SApple OSS Distributions 	output[j] = '\0';
197*5c2921b0SApple OSS Distributions 
198*5c2921b0SApple OSS Distributions 	return output;
199*5c2921b0SApple OSS Distributions }
200