Skip to content

Commit

Permalink
Move tmp preprocess files to <my_project>/.terra/cxx_parser/preProces…
Browse files Browse the repository at this point in the history
…s@<check_sum> (#34)

- Remove unused `CXXParserConfigs.customHeaders `
- Move tmp preprocess files to
`<my_project>/.terra/cxx_parser/preProcess@<check_sum>`
  • Loading branch information
littleGnAl authored Nov 15, 2023
1 parent c3f1773 commit a6e156f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
28 changes: 15 additions & 13 deletions cxx-parser/__integration_test__/cxx_parser.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import path from 'path';

import { TerraContext } from '@agoraio-extensions/terra-core';

import { CXXParser, dumpCXXAstJson } from '../src/cxx_parser';
import { CXXParser, dumpCXXAstJson, generateChecksum } from '../src/cxx_parser';
import { CXXFile, Struct } from '../src/cxx_terra_node';

describe('cxx_parser', () => {
let tmpDir: string = '';
let cppastBackendPath: string = '';
let cxxParserCacheDir: string = '';

beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'terra-ut-'));
cppastBackendPath = path.join(__dirname, '..', 'cxx', 'cppast_backend');
cxxParserCacheDir = path.join(tmpDir, 'cxx_parser');
});

afterEach(() => {
Expand All @@ -33,20 +33,26 @@ struct AAA {
`
);

let checkSum = generateChecksum([file1Path]);
let preProcessParseFilesDir = path.join(
cxxParserCacheDir,
`preProcess@${checkSum}`
);

// TODO(littlegnal): Should move the tmp/*.h to the build dir in the future
const expectedJson = `
[
{
"__TYPE":"CXXFile",
"file_path":"${cppastBackendPath}/tmp/file1.h",
"file_path":"${preProcessParseFilesDir}/file1.h",
"nodes":[
{
"__TYPE":"Struct",
"attributes":[],
"base_clazzs":[],
"comment":"",
"constructors":[],
"file_path":"${cppastBackendPath}/tmp/file1.h",
"file_path":"${preProcessParseFilesDir}/file1.h",
"member_variables":[
{
"__TYPE":"MemberVariable",
Expand All @@ -66,21 +72,17 @@ struct AAA {
"methods":[],
"name":"AAA",
"namespaces":[],
"parent_name":"${cppastBackendPath}/tmp/file1.h",
"parent_name":"${preProcessParseFilesDir}/file1.h",
"source":""
}
]
}
]
`;

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

expect(fs.existsSync(preProcessParseFilesDir)).toBe(true);

// Use `JSON.parse` to parse the json string to avoid the format issue
expect(JSON.parse(json)).toEqual(JSON.parse(expectedJson));
Expand Down
16 changes: 11 additions & 5 deletions cxx-parser/__tests__/unit_test/cxx_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ describe('cxx_parser', () => {
cppastBackendBuildDir,
`dump_json_${checkSum}.json`
);
let preProcessParseFilesDir = path.join(
cppastBackendBuildDir,
`preProcess@${checkSum}`
);

(execSync as jest.Mock).mockImplementationOnce(() => {
// Simulate generate the ast json file after run the bash script
Expand All @@ -92,12 +96,11 @@ describe('cxx_parser', () => {
let json = dumpCXXAstJson(
new TerraContext(tmpDir),
[],
[],
[file1Path, file2Path],
[]
);

let expectedBashScript = `bash ${cppastBackendBuildBashPath} \"${cppastBackendBuildDir}\" "--visit-headers=${file1Path},${file2Path} --include-header-dirs= --defines-macros="" --custom-headers= --output-dir=${jsonFilePath} --dump-json"`;
let expectedBashScript = `bash ${cppastBackendBuildBashPath} \"${cppastBackendBuildDir}\" "--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',
Expand Down Expand Up @@ -152,6 +155,10 @@ describe('cxx_parser', () => {
cppastBackendBuildDir,
`dump_json_${checkSum}.json`
);
let preProcessParseFilesDir = path.join(
cppastBackendBuildDir,
`preProcess@${checkSum}`
);

let isBashCalled = false;

Expand All @@ -167,12 +174,11 @@ describe('cxx_parser', () => {
let json = dumpCXXAstJson(
new TerraContext(tmpDir, '', '', true, false),
[],
[],
[file1Path, file2Path],
[]
);

let expectedBashScript = `bash ${cppastBackendBuildBashPath} \"${cppastBackendBuildDir}\" "--visit-headers=${file1Path},${file2Path} --include-header-dirs= --defines-macros="" --custom-headers= --output-dir=${jsonFilePath} --dump-json"`;
let expectedBashScript = `bash ${cppastBackendBuildBashPath} \"${cppastBackendBuildDir}\" "--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',
Expand Down Expand Up @@ -229,6 +235,7 @@ describe('cxx_parser', () => {
cppastBackendBuildDir,
`dump_json_${checkSum}.json`
);

// Simulate cached ast json file exists
fs.writeFileSync(jsonFilePath, expectedJson);

Expand All @@ -242,7 +249,6 @@ describe('cxx_parser', () => {
let json = dumpCXXAstJson(
new TerraContext(tmpDir),
[],
[],
[file1Path, file2Path],
[]
);
Expand Down
15 changes: 12 additions & 3 deletions cxx-parser/cxx/cppast_backend/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int main(int argc, char **argv) {
option_list.add_options()
("include-header-dirs", "The include C++ headers directories, split with \",\"", cxxopts::value<std::string>())
("output-dir", "The output directory, or output to ./build/iris-ast/ by default", cxxopts::value<std::string>())
("pre-process-dir", "The pre-process parse files directory", cxxopts::value<std::string>())
("visit-headers", "The C++ headers to be visited, split with \",\"", cxxopts::value<std::string>())
("custom-headers", "The custom C++ headers to be visited, split with \",\"", cxxopts::value<std::string>())
("defines-macros", "Custom macros, split with \",\"", cxxopts::value<std::string>())
Expand All @@ -41,6 +42,7 @@ int main(int argc, char **argv) {

std::string output_dir = "";
std::string visit_headers = "";
std::string pre_process_dir = "";
std::vector<std::string> include_header_dirs;
std::vector<std::string> visit_files;
std::vector<std::string> custom_headers;
Expand All @@ -66,9 +68,15 @@ int main(int argc, char **argv) {
output_dir = std::string(std::filesystem::absolute(tmp_out).c_str());
}

std::filesystem::path tmp_path = std::filesystem::current_path() / "tmp";
// make sure pre_processed_files as the first of the headers
include_header_dirs.push_back(tmp_path.c_str());
if (parse_result.count("pre-process-dir")) {
pre_process_dir = parse_result["pre-process-dir"].as<std::string>();
} else {
std::cerr << "The pre-process-dir option is missing." << std::endl;
return -1;
}

// make sure `pre_process_dir` as the first of the headers
include_header_dirs.push_back(pre_process_dir);

if (parse_result.count("include-header-dirs")) {
auto include_system_dir =
Expand Down Expand Up @@ -116,6 +124,7 @@ int main(int argc, char **argv) {
}

std::vector<std::string> pre_processed_files;
std::filesystem::path tmp_path = pre_process_dir;
PreProcessVisitFiles(tmp_path, visit_files, pre_processed_files,
is_dump_json);

Expand Down
12 changes: 6 additions & 6 deletions cxx-parser/src/cxx_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ export function getCppAstBackendDir() {
export function dumpCXXAstJson(
terraContext: TerraContext,
includeHeaderDirs: string[],
customHeaders: string[],
parseFiles: string[],
defines: string[]
): string {
let parseFilesChecksum = generateChecksum(parseFiles);

let buildDir = getBuildDir(terraContext);
let preProcessParseFilesDir = path.join(
buildDir,
`preProcess@${parseFilesChecksum}`
);

let agora_rtc_ast_dir_path = getCppAstBackendDir();

Expand Down Expand Up @@ -76,12 +79,10 @@ export function dumpCXXAstJson(
let definess = defines.join(',');
bashArgs += ` --defines-macros=\"${definess}\"`;

if (customHeaders) {
bashArgs += ` --custom-headers=${customHeaders.join(',')}`;
}

bashArgs += ` --output-dir=${outputJsonPath}`;

bashArgs += ` --pre-process-dir=${preProcessParseFilesDir}`;

bashArgs += ` --dump-json`;

let buildScript = `bash ${build_shell_path} \"${buildDir}\" \"${bashArgs}\"`;
Expand Down Expand Up @@ -122,7 +123,6 @@ export function CXXParser(
let jsonContent = dumpCXXAstJson(
terraContext,
cxxParserConfigs.includeHeaderDirs,
cxxParserConfigs.customHeaders,
parseFiles,
cxxParserConfigs.definesMacros
);
Expand Down
6 changes: 0 additions & 6 deletions cxx-parser/src/cxx_parser_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface CXXParserConfigs {
includeHeaderDirs: string[];
definesMacros: string[];
parseFiles: ParseFilesConfig;
customHeaders: string[];
}

export class CXXParserConfigs {
Expand All @@ -39,11 +38,6 @@ export class CXXParserConfigs {
})
.flat(1),
},
customHeaders: (original.customHeaders ?? [])
.map((it) => {
return _resolvePaths(it, configDir);
})
.flat(1),
};
}
}

0 comments on commit a6e156f

Please sign in to comment.