This repository was archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 682
/
Copy pathLlbuildYamlParser.js
86 lines (78 loc) · 2.84 KB
/
LlbuildYamlParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use babel';
/* @flow */
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import yaml from 'js-yaml';
import nuclideUri from '../../../commons-node/nuclideUri';
import fsPromise from '../../../commons-node/fsPromise';
/**
* Reads an llbuild YAML file and returns a mapping, with source files as keys,
* and the Swift compiler arguments used to compile those files as values.
* If the file does not exist or cannot be read from, returns an empty mapping.
* If the file contains invalid YAML, throws an exception.
*/
export async function readCompileCommands(
path: string,
): Promise<Map<string, string>> {
// Read the YAML file into memory.
let data;
try {
data = await fsPromise.readFile(path, 'utf8');
} catch (e) {
return new Map();
}
// Attempt to parse the YAML, or bail if a parsing error occurs.
const llbuildYaml = yaml.safeLoad(data);
const compileCommands = new Map();
for (const llbuildCommandKey in llbuildYaml.commands) {
const llbuildCommand = llbuildYaml.commands[llbuildCommandKey];
// Not all commands contain source files -- some just link a bunch of
// prebuilt object files, for example. If there are no source files to
// gather compile commands for, skip this llbuild command.
if (!llbuildCommand.sources) {
continue;
}
// If we find source files, map each to a string used to compile it.
// This string is composed of the compiler arguments ("other-args"),
// plus all of the Swift source files that need to be compiled together.
llbuildCommand.sources.forEach(source => {
const otherArgs = llbuildCommand['other-args'] ? llbuildCommand['other-args'] : [];
compileCommands.set(
source,
otherArgs.concat(llbuildCommand.sources).join(' '),
);
});
}
return compileCommands;
}
/**
* SwiftPM generates YAML, which is then consumed by llbuild. However, it
* can generate that YAML at one of several paths:
*
* - If the build configuration is 'debug', it generates a 'debug.yaml'.
* - If the build configuration is 'release', it generates a 'release.yaml'.
* - If no --build-path is specified, it generates
* '.build/{debug|release}.yaml'.
* - If a --build-path is specified, it generates
* '/path/to/build/path/{debug|release.yaml}'.
*
* This function returns the path to YAML file that will be generated if a
* build task is begun with the current store's settings.
*/
export function llbuildYamlPath(
chdir: string,
configuration: string,
buildPath: string,
): string {
const yamlFileName = `${configuration}.yaml`;
if (buildPath.length > 0) {
return nuclideUri.join(buildPath, yamlFileName);
} else {
return nuclideUri.join(chdir, '.build', yamlFileName);
}
}