Skip to content

Commit e1c2968

Browse files
andrewdacenkometa-codesync[bot]
authored andcommitted
add cpp coverage (facebook#54637)
Summary: Changelog: [Internal] Pull Request resolved: facebook#54637 Add c++ code-coverage mode for Fantom and set up custom output path for it. Reviewed By: sammy-SC Differential Revision: D87548065 fbshipit-source-id: 5462582ca467051f7e9a155fd82bfb7045380097
1 parent c4a9829 commit e1c2968

File tree

7 files changed

+395
-86
lines changed

7 files changed

+395
-86
lines changed

private/react-native-fantom/runner/executables/hermesc.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ import fs from 'fs';
2424
import os from 'os';
2525
import path from 'path';
2626

27-
type TesterOptions = Readonly<{
28-
isOptimizedMode: boolean,
27+
type HermescOptions = Readonly<{
28+
enableCoverage: boolean,
29+
enableOptimized: boolean,
2930
hermesVariant: HermesVariant,
3031
}>;
3132

3233
function getHermesCompilerPath({
33-
isOptimizedMode,
34+
enableOptimized,
3435
hermesVariant,
35-
}: TesterOptions): string {
36+
}: HermescOptions): string {
3637
return path.join(
3738
NATIVE_BUILD_OUTPUT_PATH,
38-
`hermesc-${(hermesVariant as string).toLowerCase()}-${isOptimizedMode ? 'opt' : 'dev'}`,
39+
`hermesc-${(hermesVariant as string).toLowerCase()}-${enableOptimized ? 'opt' : 'dev'}`,
3940
);
4041
}
41-
42-
export function build(options: TesterOptions): void {
42+
export function build(options: HermescOptions): void {
4343
const destPath = getHermesCompilerPath(options);
4444
if (fs.existsSync(destPath)) {
4545
return;
@@ -54,14 +54,20 @@ export function build(options: TesterOptions): void {
5454
const destTmpPath = destPath + '-' + Date.now() + '-' + process.pid;
5555

5656
try {
57-
const result = runBuck2Sync([
58-
'build',
59-
...getBuckModesForPlatform(options.isOptimizedMode),
60-
...getBuckOptionsForHermes(options.hermesVariant),
61-
getHermesCompilerTarget(options.hermesVariant),
62-
'--out',
63-
tmpPath,
64-
]);
57+
const result = runBuck2Sync(
58+
[
59+
'build',
60+
...getBuckModesForPlatform({
61+
enableOptimized: options.enableOptimized,
62+
enableCoverage: options.enableCoverage,
63+
}),
64+
...getBuckOptionsForHermes(options.hermesVariant),
65+
getHermesCompilerTarget(options.hermesVariant),
66+
'--out',
67+
tmpPath,
68+
],
69+
{},
70+
);
6571

6672
if (result.status !== 0) {
6773
throw new Error(getDebugInfoFromCommandResult(result));
@@ -75,7 +81,7 @@ export function build(options: TesterOptions): void {
7581
// Remove extended attributes to avoid "Operation not permitted" errors
7682
// when copying to EdenFS/NFS-backed directories on macOS
7783
if (os.platform() === 'darwin') {
78-
runCommandSync('xattr', ['-rc', tmpPath]);
84+
runCommandSync('xattr', ['-rc', tmpPath], {});
7985
}
8086

8187
fs.copyFileSync(tmpPath, destTmpPath);
@@ -104,11 +110,11 @@ export function build(options: TesterOptions): void {
104110

105111
export function run(
106112
args: ReadonlyArray<string>,
107-
options: TesterOptions,
113+
options: HermescOptions,
108114
): SyncCommandResult {
109115
if (!isCI) {
110116
build(options);
111117
}
112118

113-
return runCommandSync(getHermesCompilerPath(options), args);
119+
return runCommandSync(getHermesCompilerPath(options), args, {});
114120
}

private/react-native-fantom/runner/executables/tester.js

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
* @format
99
*/
1010

11-
import type {AsyncCommandResult, HermesVariant} from '../utils';
11+
import type {
12+
AsyncCommandResult,
13+
EnvironmentOverrides,
14+
HermesVariant,
15+
} from '../utils';
1216

1317
import {debugCpp, isCI, profileCpp} from '../EnvironmentOptions';
1418
import {CPP_TRACES_OUTPUT_PATH, NATIVE_BUILD_OUTPUT_PATH} from '../paths';
@@ -29,21 +33,22 @@ const FANTOM_TESTER_BUCK_TARGET =
2933
'fbsource//xplat/js/react-native-github/private/react-native-fantom/tester:tester';
3034

3135
type TesterOptions = Readonly<{
32-
isOptimizedMode: boolean,
36+
enableCoverage: boolean,
37+
enableOptimized: boolean,
3338
hermesVariant: HermesVariant,
3439
}>;
3540

36-
function getFantomTesterPath({
37-
isOptimizedMode,
41+
export function getFantomTesterPath({
3842
hermesVariant,
43+
...options
3944
}: TesterOptions): string {
4045
return path.join(
4146
NATIVE_BUILD_OUTPUT_PATH,
42-
`fantom-tester-${(hermesVariant as string).toLowerCase()}-${isOptimizedMode ? 'opt' : 'dev'}`,
47+
`fantom-tester-${(hermesVariant as string).toLowerCase()}-${options.enableOptimized ? 'opt' : 'dev'}${options.enableCoverage ? '-coverage' : ''}`,
4348
);
4449
}
4550

46-
export function build(options: TesterOptions): void {
51+
export function build(options: TesterOptions, env: EnvironmentOverrides): void {
4752
const destPath = getFantomTesterPath(options);
4853
if (fs.existsSync(destPath)) {
4954
return;
@@ -58,14 +63,23 @@ export function build(options: TesterOptions): void {
5863
const destTmpPath = destPath + '-' + Date.now() + '-' + process.pid;
5964

6065
try {
61-
const result = runBuck2Sync([
62-
'build',
63-
...getBuckModesForPlatform(options.isOptimizedMode),
64-
...getBuckOptionsForHermes(options.hermesVariant),
65-
FANTOM_TESTER_BUCK_TARGET,
66-
'--out',
67-
tmpPath,
68-
]);
66+
const result = runBuck2Sync(
67+
[
68+
'build',
69+
...getBuckModesForPlatform({
70+
enableOptimized: options.enableOptimized,
71+
enableCoverage: options.enableCoverage,
72+
}),
73+
...getBuckOptionsForHermes(options.hermesVariant),
74+
FANTOM_TESTER_BUCK_TARGET,
75+
'--out',
76+
tmpPath,
77+
],
78+
env,
79+
{
80+
withFDB: false,
81+
},
82+
);
6983

7084
if (result.status !== 0) {
7185
throw new Error(getDebugInfoFromCommandResult(result));
@@ -79,7 +93,7 @@ export function build(options: TesterOptions): void {
7993
// Remove extended attributes to avoid "Operation not permitted" errors
8094
// when copying to EdenFS/NFS-backed directories on macOS
8195
if (os.platform() === 'darwin') {
82-
runCommandSync('xattr', ['-rc', tmpPath]);
96+
runCommandSync('xattr', ['-rc', tmpPath], {});
8397
}
8498

8599
fs.copyFileSync(tmpPath, destTmpPath);
@@ -109,6 +123,7 @@ export function build(options: TesterOptions): void {
109123
export function run(
110124
args: ReadonlyArray<string>,
111125
options: TesterOptions,
126+
env: EnvironmentOverrides,
112127
): AsyncCommandResult {
113128
if (isCI && debugCpp) {
114129
throw new Error('Cannot run Fantom with C++ debugging on CI');
@@ -119,19 +134,23 @@ export function run(
119134
}
120135

121136
if (!isCI && !debugCpp) {
122-
build(options);
137+
build(options, env);
123138
}
124139

125140
if (debugCpp) {
126141
return runBuck2(
127142
[
128143
'run',
129-
...getBuckModesForPlatform(options.isOptimizedMode),
144+
...getBuckModesForPlatform({
145+
enableOptimized: options.enableOptimized,
146+
enableCoverage: options.enableCoverage,
147+
}),
130148
...getBuckOptionsForHermes(options.hermesVariant),
131149
FANTOM_TESTER_BUCK_TARGET,
132150
'--',
133151
...args,
134152
],
153+
env,
135154
{
136155
withFDB: true,
137156
},
@@ -156,19 +175,23 @@ export function run(
156175
// -g: enable call-graph (stack traces)
157176
// -F 997: sample at 997 Hz (prime number to avoid aliasing)
158177
// --call-graph dwarf: use DWARF for accurate stack traces
159-
const result = runCommand('perf', [
160-
'record',
161-
'-g',
162-
'-F',
163-
'997',
164-
'--call-graph',
165-
'dwarf',
166-
'-o',
167-
perfOutputPath,
168-
'--',
169-
testerPath,
170-
...args,
171-
]);
178+
const result = runCommand(
179+
'perf',
180+
[
181+
'record',
182+
'-g',
183+
'-F',
184+
'997',
185+
'--call-graph',
186+
'dwarf',
187+
'-o',
188+
perfOutputPath,
189+
'--',
190+
testerPath,
191+
...args,
192+
],
193+
{},
194+
);
172195

173196
// Log the output path after the command starts
174197
console.log(
@@ -179,5 +202,5 @@ export function run(
179202
return result;
180203
}
181204

182-
return runCommand(testerPath, args);
205+
return runCommand(testerPath, args, env);
183206
}

private/react-native-fantom/runner/global-setup/build.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @format
99
*/
1010

11+
import type {EnvironmentOverrides} from '../utils';
12+
1113
import {createBundle} from '../bundling';
1214
import {isCI} from '../EnvironmentOptions';
1315
import {build as buildHermesCompiler} from '../executables/hermesc';
@@ -37,18 +39,24 @@ async function tryOrLog(
3739
}
3840
}
3941

40-
export default async function build(): Promise<void> {
42+
export default async function build(
43+
enableCoverage: boolean,
44+
env: EnvironmentOverrides,
45+
): Promise<void> {
4146
try {
4247
fs.rmSync(NATIVE_BUILD_OUTPUT_PATH, {recursive: true});
4348
} catch {}
4449

4550
fs.mkdirSync(NATIVE_BUILD_OUTPUT_PATH, {recursive: true});
4651

4752
if (isCI) {
48-
for (const isOptimizedMode of [false, true]) {
53+
for (const enableOptimized of [false, true]) {
4954
for (const hermesVariant of HermesVariant.members()) {
50-
buildFantomTester({isOptimizedMode, hermesVariant});
51-
buildHermesCompiler({isOptimizedMode, hermesVariant});
55+
buildFantomTester(
56+
{enableOptimized, hermesVariant, enableCoverage},
57+
env,
58+
);
59+
buildHermesCompiler({enableOptimized, hermesVariant, enableCoverage});
5260
}
5361
}
5462

private/react-native-fantom/runner/global-setup/globalSetup.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {DevToolLauncher} from '@react-native/dev-middleware';
1212
import type {ConfigT} from 'metro-config';
1313

1414
import {isOSS, validateEnvironmentVariables} from '../EnvironmentOptions';
15+
import {getTestBuildOutputPath} from '../paths';
1516
import build from './build';
1617
import {createDevMiddleware} from '@react-native/dev-middleware';
1718
import connect from 'connect';
@@ -20,7 +21,10 @@ import {Server} from 'net';
2021
import path from 'path';
2122

2223
export default async function globalSetup(
23-
globalConfig: {...},
24+
globalConfig: {
25+
collectCoverage: boolean,
26+
...
27+
},
2428
projectConfig: {...},
2529
): Promise<void> {
2630
process.env.__FANTOM_RUN_ID__ ??= `run-${Date.now()}`;
@@ -30,7 +34,11 @@ export default async function globalSetup(
3034
await startMetroServer();
3135

3236
if (!isOSS) {
33-
await build();
37+
await build(globalConfig.collectCoverage, {
38+
LLVM_PROFILE_FILE: globalConfig.collectCoverage
39+
? getTestBuildOutputPath() + 'fantom.profraw'
40+
: undefined,
41+
});
3442
}
3543
}
3644

0 commit comments

Comments
 (0)