Skip to content

Commit

Permalink
Fix paths of target sources outside the workspace (#1816)
Browse files Browse the repository at this point in the history
* Fix paths of target sources outside the workspace

For more details, see:
#1504

* Add test for source files outside of workspace root

Co-Authored-By: Elson Wei <[email protected]>

Co-authored-by: Elson Wei <[email protected]>
Co-authored-by: Bob Brown <[email protected]>
  • Loading branch information
3 people authored May 5, 2021
1 parent 7fd5128 commit d514bef
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/drivers/cmakefileapi/api_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ function convertToAbsolutePath(input_path: string, base_path: string) {
return path.normalize(absolute_path);
}

function convertToExtCodeModelFileGroup(targetObject: index_api.CodeModelKind.TargetObject): CodeModelFileGroup[] {
function convertToExtCodeModelFileGroup(targetObject: index_api.CodeModelKind.TargetObject,
root_paths: index_api.CodeModelKind.PathInfo): CodeModelFileGroup[] {
const fileGroup: CodeModelFileGroup[] = !targetObject.compileGroups ? [] : targetObject.compileGroups.map(group => {
const compileFlags
= group.compileCommandFragments ? group.compileCommandFragments.map(frag => frag.fragment).join(' ') : '';
Expand All @@ -197,9 +198,10 @@ function convertToExtCodeModelFileGroup(targetObject: index_api.CodeModelKind.Ta
// Collection all without compilegroup like headers
const defaultIndex = fileGroup.push({sources: [], isGenerated: false} as CodeModelFileGroup) - 1;


const target_source_root = convertToAbsolutePath(targetObject.paths.source, root_paths.source);
targetObject.sources.forEach(sourcefile => {
const file_path = path.relative(targetObject.paths.source, sourcefile.path).replace('\\', '/');
const file_abs_path = convertToAbsolutePath(sourcefile.path, root_paths.source);
const file_path = path.relative(target_source_root, file_abs_path).replace('\\', '/');
if (sourcefile.compileGroupIndex !== undefined) {
fileGroup[sourcefile.compileGroupIndex].sources.push(file_path);
} else {
Expand All @@ -215,7 +217,7 @@ function convertToExtCodeModelFileGroup(targetObject: index_api.CodeModelKind.Ta
async function loadCodeModelTarget(root_paths: index_api.CodeModelKind.PathInfo, jsonfile: string) {
const targetObject = await loadTargetObject(jsonfile);

const fileGroups = convertToExtCodeModelFileGroup(targetObject);
const fileGroups = convertToExtCodeModelFileGroup(targetObject, root_paths);

// This implementation expects that there is only one sysroot in a target.
// The ServerAPI only has provided one sysroot. In the FileAPI,
Expand Down
43 changes: 40 additions & 3 deletions test/unit-tests/driver/driver-codemodel-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function makeCodeModelDriverTestsuite(
const root = getTestRootFilePath(workspacePath);
const defaultWorkspaceFolder = getTestRootFilePath('test/unit-tests/driver/workspace/test_project');
const emptyWorkspaceFolder = getTestRootFilePath('test/unit-tests/driver/workspace/empty_project');
const sourceOutsideOfWorkspace
= getTestRootFilePath('test/unit-tests/driver/workspace/source_outside_of_workspace/workspace');

let kitDefault: Kit;
if (process.platform === 'win32') {
Expand All @@ -66,6 +68,11 @@ export function makeCodeModelDriverTestsuite(
if (!cleanupBuildDir(path.join(emptyWorkspaceFolder, 'build'))) {
done('Empty project build folder still exists');
}

if (!cleanupBuildDir(path.join(sourceOutsideOfWorkspace, 'build'))) {
done('Source-outside-of-workspace project build folder still exists');
}

done();
});

Expand All @@ -77,12 +84,13 @@ export function makeCodeModelDriverTestsuite(
});


async function generateCodeModelForConfiguredDriver(args: string[] =
[]): Promise<null|codemodel_api.CodeModelContent> {
async function generateCodeModelForConfiguredDriver(args: string[] = [],
workspaceFolder: string = defaultWorkspaceFolder):
Promise<null|codemodel_api.CodeModelContent> {
const config = ConfigurationReader.create();
const executable = await getCMakeExecutableInformation(cmakePath);

driver = await driver_generator(executable, config, kitDefault, defaultWorkspaceFolder, async () => {}, []);
driver = await driver_generator(executable, config, kitDefault, workspaceFolder, async () => {}, []);
let code_model: null|codemodel_api.CodeModelContent = null;
if (driver instanceof codemodel_api.CodeModelDriver) {
driver.onCodeModelChanged(cm => { code_model = cm; });
Expand Down Expand Up @@ -246,5 +254,34 @@ export function makeCodeModelDriverTestsuite(
expect(target).to.be.not.undefined;
expect(target!.sysroot).to.be.eq('/tmp');
}).timeout(90000);

test('Test source files outside of workspace root', async () => {
const project_name: string = 'source_outside_of_workspace';
const codemodel_data = await generateCodeModelForConfiguredDriver([], sourceOutsideOfWorkspace);
expect(codemodel_data).to.be.not.null;

for (const [target_name, target_subdir, sourcefile_name] of [['root_target', '', '../main.cpp'],
['subdir_target', 'subdir', '../../main.cpp']] as
const) {

const target = codemodel_data!.configurations[0].projects[0].targets.find(t => t.type == 'EXECUTABLE'
&& t.name == target_name);
expect(target).to.be.not.undefined;

// Assert correct target names for node labels
const executableName = target_name + (process.platform === 'win32' ? '.exe' : '');
expect(target!.fullName).to.be.eq(executableName);

// Assert correct location of target source directories
expect(path.normalize(target!.sourceDirectory!).toLowerCase())
.to.eq(path.normalize(path.join(root, project_name, 'workspace', target_subdir)).toLowerCase());

// Assert correct path to source file
expect(target!.fileGroups).to.be.not.undefined;
const compile_information = target!.fileGroups!.find(t => !!t.language);
expect(compile_information).to.be.not.undefined;
expect(compile_information!.sources).to.include(sourcefile_name);
}
}).timeout(90000);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main(int) {
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.0.0)
project(source_outside_of_workspace VERSION 0.1.0)

add_executable(root_target ../main.cpp)
add_subdirectory(subdir)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_executable(subdir_target ${PROJECT_SOURCE_DIR}/../main.cpp)

0 comments on commit d514bef

Please sign in to comment.