Skip to content

Commit

Permalink
[cxx-parser] Allow add prefix to the build directory name
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Mar 4, 2024
1 parent 336018f commit ef1f605
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
78 changes: 78 additions & 0 deletions cxx-parser/__tests__/unit_test/cxx_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,84 @@ describe('cxx_parser', () => {
expect(json).toEqual(expectedJson);
});

it('generate ast json with buildDirNamePrefix', () => {
let file1Path = path.join(tmpDir, 'file1.h');
let file2Path = path.join(tmpDir, 'file2.h');

fs.writeFileSync(file1Path, 'void file1_main() {}');
fs.writeFileSync(file2Path, 'void file2_main() {}');

const expectedJson = JSON.stringify([
{
file_path: '/my/path/IAgoraRtcEngine.h',
nodes: [
{
__TYPE: 'Clazz',
name: 'TestClazz',
methods: [
{
__TYPE: 'MemberFunction',
name: 'test',
parameters: [
{
__TYPE: 'Variable',
name: 'test',
type: {
__TYPE: 'SimpleType',
is_builtin_type: false,
is_const: false,
kind: 101,
name: 'Test',
source: 'Test *',
template_arguments: [],
},
},
],
parent_name: 'TestClazz',
namespaces: ['test'],
},
],
namespaces: ['test'],
},
],
},
]);

let cppastBackendBuildDirWithPrefix = path.join(tmpDir, 'abc_cxx_parser');

let checkSum = generateChecksum([file1Path, file2Path]);
let jsonFilePath = path.join(
cppastBackendBuildDirWithPrefix,
`dump_json_${checkSum}.json`
);
let preProcessParseFilesDir = path.join(
cppastBackendBuildDirWithPrefix,
`preProcess@${checkSum}`
);

(execSync as jest.Mock).mockImplementationOnce(() => {
// Simulate generate the ast json file after run the bash script
fs.mkdirSync(cppastBackendBuildDirWithPrefix, { recursive: true });
fs.writeFileSync(jsonFilePath, expectedJson);
return '';
});

let json = dumpCXXAstJson(
new TerraContext(tmpDir),
[],
[file1Path, file2Path],
[],
'abc'
);

let expectedBashScript = `bash ${cppastBackendBuildBashPath} \"${cppastBackendBuildDirWithPrefix}\" "--visit-headers=${file1Path},${file2Path} --include-header-dirs= --defines-macros="" --output-dir=${jsonFilePath} --pre-process-dir=${preProcessParseFilesDir} --dump-json"`;
expect(execSync).toHaveBeenCalledWith(expectedBashScript, {
encoding: 'utf8',
stdio: 'inherit',
});
expect(json).toEqual(expectedJson);
});

it('generate ast json with clean', () => {
let file1Path = path.join(tmpDir, 'file1.h');
let file2Path = path.join(tmpDir, 'file2.h');
Expand Down
13 changes: 13 additions & 0 deletions cxx-parser/__tests__/unit_test/cxx_parser_configs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ describe('CXXParserConfigs', () => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});

it('resolve buildDirNamePrefix', () => {
let args = {
buildDirNamePrefix: 'prefix',
};

let config = CXXParserConfigs.resolve(
tmpDir,
args as unknown as CXXParserConfigs
);

expect(config.buildDirNamePrefix).toEqual('prefix');
});

it('resolve parseFiles and keep path order', () => {
let path1 = path.join(tmpDir, 'file1.h');
let path2 = path.join(tmpDir, 'file2.h');
Expand Down
21 changes: 15 additions & 6 deletions cxx-parser/src/cxx_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ export function generateChecksum(files: string[]) {
.toString();
}

function getBuildDir(terraContext: TerraContext) {
function getBuildDir(
terraContext: TerraContext,
buildDirNamePrefix?: string | undefined
) {
let prefix = buildDirNamePrefix ?? '';
if (prefix.length) {
prefix = `${prefix}_`;
}
// <my_project>/.terra/cxx_parser
return path.join(terraContext.buildDir, 'cxx_parser');
return path.join(terraContext.buildDir, `${prefix}cxx_parser`);
}

export function getCppAstBackendDir() {
Expand All @@ -36,11 +43,12 @@ export function dumpCXXAstJson(
terraContext: TerraContext,
includeHeaderDirs: string[],
parseFiles: string[],
defines: string[]
defines: string[],
buildDirNamePrefix?: string | undefined
): string {
let parseFilesChecksum = generateChecksum(parseFiles);

let buildDir = getBuildDir(terraContext);
let buildDir = getBuildDir(terraContext, buildDirNamePrefix);
let preProcessParseFilesDir = path.join(
buildDir,
`preProcess@${parseFilesChecksum}`
Expand Down Expand Up @@ -124,7 +132,8 @@ export function CXXParser(
terraContext,
cxxParserConfigs.includeHeaderDirs,
parseFiles,
cxxParserConfigs.definesMacros
cxxParserConfigs.definesMacros,
cxxParserConfigs.buildDirNamePrefix
);

let newParseResult = genParseResultFromJson(jsonContent);
Expand All @@ -136,7 +145,7 @@ export function CXXParser(
});

ClangASTStructConstructorParser(
getBuildDir(terraContext),
getBuildDir(terraContext, cxxParserConfigs.buildDirNamePrefix),
cxxParserConfigs.includeHeaderDirs,
cppastParsedFiles,
newParseResult
Expand Down
2 changes: 2 additions & 0 deletions cxx-parser/src/cxx_parser_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class ParseFilesConfig {
}

export interface CXXParserConfigs {
buildDirNamePrefix?: string;
includeHeaderDirs: string[];
definesMacros: string[];
parseFiles: ParseFilesConfig;
Expand All @@ -41,6 +42,7 @@ export interface CXXParserConfigs {
export class CXXParserConfigs {
static resolve(configDir: any, original: CXXParserConfigs): CXXParserConfigs {
return {
buildDirNamePrefix: original.buildDirNamePrefix,
includeHeaderDirs: (original.includeHeaderDirs ?? [])
.map((it) => {
return _resolvePaths(it, configDir);
Expand Down

0 comments on commit ef1f605

Please sign in to comment.