Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pxtlib/shared/extensioninfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,14 @@ namespace pxt.cpp {
* Generates native extension files from pxt.json and project files, with dependencies
* under pxt_modules/<directory>/. Dependencies resolve by directory, pxt.json name,
* or their prefix before "---".
* Bundled packages supply missing Makefile, sdkconfig.defaults, and CMakeLists.txt files.
* The target must not have variant overrides applied.
* @example getExtensionInfo({ "pxt.json": config, "main.cpp": source }, target, "v2", host)
*/
export function getExtensionInfo(files: Map<string>, target: TargetBundle, variant: string, host: ExtensionInfoHost): pxtc.ExtensionInfo {
const appTarget = getAppTargetVariant(target, variant);
const bundledPackages = appTarget.bundledpkgs || {};
const bundledBuildFiles = ["Makefile", "sdkconfig.defaults", "CMakeLists.txt"];
const constsName = "dal.d.ts"
let sourcePath = "/source/"
let disabledDeps = ""
Expand Down Expand Up @@ -170,8 +173,14 @@ namespace pxt.cpp {
U.userError(`extension ${id} matches multiple directories: ${paths.join(", ")}`);
return paths && paths[0];
};
const readFile = (pkg: ExtensionPackage, name: string): string =>
files[pkg.path + name];
const readFile = (pkg: ExtensionPackage, name: string): string => {
if (bundledBuildFiles.indexOf(name) >= 0 && pkg.path) {
const id = pkg.path.slice("pxt_modules/".length, -1);
const bundledFile = bundledPackages[id] && bundledPackages[id][name];
if (bundledFile != null) return bundledFile;
Comment on lines +179 to +180
Comment on lines +179 to +180
}
return files[pkg.path + name];
};
const dependencies = (pkg: ExtensionPackage): Map<string> => packageUtils.getConfigDependencies(
pkg.config,
pkg.id == "this",
Expand Down Expand Up @@ -217,6 +226,14 @@ namespace pxt.cpp {
id => !!runtimePackages[resolvePath(id)],
condition => host.logger.log(`invalid dependency expression: ${condition} in ${pkg.id}`)
);
if (pkg.path) {
const id = pkg.path.slice("pxt_modules/".length, -1);
const bundledFiles = bundledPackages[id];
Comment on lines +229 to +231
Comment on lines +230 to +231
for (const name of bundledBuildFiles) {
if (bundledFiles && bundledFiles[name] != null && pkg.files.indexOf(name) < 0)
pkg.files.push(name);
Comment on lines +233 to +234
}
}
}

const mainDeps: ExtensionPackage[] = [];
Expand Down
44 changes: 44 additions & 0 deletions tests/cli-test/clirunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,50 @@ ${member}
chai.expect(disabled.functions).deep.equals([]);
});

it("restores bundled build files filtered out of backend requests and ignores supplied overrides", () => {
const target = extensionTestTarget();
const bundledFiles: pxt.Map<string> = {
"pxt.json": JSON.stringify({
name: "core", files: ["core.cpp", "Makefile", "sdkconfig.defaults", "CMakeLists.txt"],
dependencies: { support: "*" }
}),
"core.cpp": "// bundled source",
"Makefile": "# bundled make",
"sdkconfig.defaults": "CONFIG_BUNDLED=y",
"CMakeLists.txt": "# bundled cmake"
};
target.bundledpkgs.core = bundledFiles;
target.bundledpkgs.support = {
"pxt.json": JSON.stringify({ name: "support", files: ["support.cpp"], dependencies: {} }),
"support.cpp": "// bundled support"
};
const files: pxt.Map<string> = {
"pxt.json": JSON.stringify({ name: "this", files: ["main.cpp"], dependencies: { core: "*" } }),
"main.cpp": "// project"
};
for (const id of Object.keys(target.bundledpkgs)) {
for (const name of Object.keys(target.bundledpkgs[id]))
files[`pxt_modules/${id}/${name}`] = target.bundledpkgs[id][name];
}
const request = createExtensionRequest(files, target, undefined);
for (const name of ["Makefile", "sdkconfig.defaults", "CMakeLists.txt"])
chai.expect(request.files["pxt_modules/core/" + name]).equals(undefined);
request.files["pxt_modules/core/core.cpp"] = "// supplied override";
request.files["pxt_modules/core/Makefile"] = "# supplied override";
const original = JSON.stringify({ files: request.files, target });
const result = pxt.cpp.getExtensionInfo(request.files, target, undefined, host);
for (const name of ["Makefile", "sdkconfig.defaults", "CMakeLists.txt"])
chai.expect(result.generatedFiles["/" + name]).equals(bundledFiles[name]);
chai.expect(result.extensionFiles["/pxtapp/core/core.cpp"]).equals("// supplied override");
chai.expect(result.extensionFiles["/pxtapp/support/support.cpp"]).equals("// bundled support");
chai.expect(result.extensionFiles["/pxtapp/this/main.cpp"]).equals("// project");
chai.expect(JSON.stringify({ files: request.files, target })).equals(original);
bundledFiles["sdkconfig.defaults"] = "CONFIG_CHANGED=y";
const changed = pxt.cpp.getExtensionInfo(request.files, target, undefined, host);
chai.expect(changed.generatedFiles["/sdkconfig.defaults"]).equals("CONFIG_CHANGED=y");
chai.expect(changed.sha).not.equals(result.sha);
});

it("does not reuse stale or caller-mutated results for changed native inputs", () => {
const target = extensionTestTarget();
const files = extensionTestFiles();
Expand Down
Loading