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