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