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