Skip to content

Commit

Permalink
Improvements to multiVariant support (#6923)
Browse files Browse the repository at this point in the history
* Honour target settins in multi-variant

* Add disablesVariants setting in pxt.json

* Disable multivariant by default
  • Loading branch information
mmoskal authored Apr 28, 2020
1 parent 2de8238 commit d342bd0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
11 changes: 10 additions & 1 deletion localtypings/pxtarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ declare namespace pxt {
uploadDocs?: boolean; // enable uploading to crowdin on master or v* builds
variants?: Map<AppTarget>; // patches on top of the current AppTarget for different chip variants
multiVariants?: string[];
alwaysMultiVariant?: boolean;
queryVariants?: Map<AppTarget>; // patches on top of the current AppTarget using query url regex
unsupportedBrowsers?: BrowserOptions[]; // list of unsupported browsers for a specific target (eg IE11 in arcade). check browserutils.js browser() function for strings
checkdocsdirs?: string[]; // list of folders for checkdocs, irrespective of SUMMARY.md
Expand Down Expand Up @@ -480,6 +481,7 @@ declare namespace ts.pxtc {
time?: boolean;
noIncr?: boolean;
rawELF?: boolean;
multiVariant?: boolean;
}

interface CompileTarget {
Expand Down Expand Up @@ -799,6 +801,8 @@ declare namespace ts.pxtc {
skipPxtModulesTSC?: boolean; // skip re-checking of pxt_modules/*
skipPxtModulesEmit?: boolean; // skip re-emit of pxt_modules/*

otherMultiVariants?: ExtensionTarget[];

syntaxInfo?: SyntaxInfo;

// decompiler only
Expand Down Expand Up @@ -841,9 +845,14 @@ declare namespace ts.pxtc {
commBase?: number;
skipCloudBuild?: boolean;
hexinfo?: HexInfo;
otherMultiVariants?: ExtensionInfo[];
appVariant?: string;
outputPrefix?: string;
disabledDeps?: string;
}

interface ExtensionTarget {
extinfo: ExtensionInfo
target: CompileTarget
}

interface HexInfo {
Expand Down
1 change: 1 addition & 0 deletions localtypings/pxtpackage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ declare namespace pxt {
requiredCategories?: string[]; // ensure that those block categories are visible
supportedTargets?: string[]; // a hint about targets in which this extension is supported
firmwareUrl?: string; // link to documentation page about upgrading firmware
disablesVariants?: string[]; // don't build these variants, when this extension is enabled
}

interface PackageExtension {
Expand Down
21 changes: 17 additions & 4 deletions pxtcompiler/emitter/hexfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ namespace ts.pxtc {
}
}


for (; i < hexlines.length; ++i) {
let m = /:02000004(....)/.exec(hexlines[i])
if (m) {
Expand All @@ -262,6 +263,8 @@ namespace ts.pxtc {
m = /^:..(....)00/.exec(hexlines[i])
if (m) {
let newAddr = parseInt(upperAddr + m[1], 16)
if (!opts.flashUsableEnd && lastAddr && newAddr - lastAddr > 64 * 1024)
hitEnd()
if (opts.flashUsableEnd && newAddr >= opts.flashUsableEnd)
hitEnd()
lastIdx = i
Expand All @@ -280,6 +283,8 @@ namespace ts.pxtc {
}
}

pxt.debug(`code start: ${ctx.codeStartAddrPadded}, jmptbl: ${ctx.jmpStartAddr}`)

if (!ctx.jmpStartAddr)
oops("No hex start")

Expand Down Expand Up @@ -1083,6 +1088,12 @@ _stored_program: .hex ${res}
}

function assembleAndPatch(src: string, bin: Binary, opts: CompileOptions, cres: CompileResult) {
if (opts.extinfo.disabledDeps) {
src =
`; compilation disabled on this variant due to ${opts.extinfo.disabledDeps}` +
`.hex 718E3B92C615A841C49866C975EE5197\n` +
`.string "${opts.extinfo.disabledDeps}"`
}
src = asmHeader(bin) + src
if (opts.embedBlob) {
bin.packedSource = packSource(opts.embedMeta, ts.pxtc.decodeBase64(opts.embedBlob))
Expand Down Expand Up @@ -1203,14 +1214,16 @@ __flash_checksums:
const opts0 = U.flatClone(opts)
assembleAndPatch(src, bin, opts, cres)

const otherVariants = opts0.extinfo.otherMultiVariants || []
const otherVariants = opts0.otherMultiVariants || []
if (otherVariants.length)
try {
for (let extinfo of otherVariants) {
for (let other of otherVariants) {
const localOpts = U.flatClone(opts0)
localOpts.extinfo = extinfo
localOpts.extinfo = other.extinfo
other.target.isNative = true
localOpts.target = other.target
//pxt.setAppTargetVariant(extinfo.appVariant, { temporary: true })
hexfile.setupFor(localOpts.target, extinfo)
hexfile.setupFor(localOpts.target, localOpts.extinfo)
assembleAndPatch(src, bin, localOpts, cres)
}
} finally {
Expand Down
31 changes: 23 additions & 8 deletions pxtlib/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,42 @@ namespace pxt.cpp {
}

export function getExtensionInfo(mainPkg: MainPackage): pxtc.ExtensionInfo {
let pkgSnapshot: Map<string> = {
const pkgSnapshot: Map<string> = {
"__appVariant": pxt.appTargetVariant || ""
}
let constsName = "dal.d.ts"
const constsName = "dal.d.ts"
let sourcePath = "/source/"

let mainDeps = mainPkg.sortedDeps(true)

for (let pkg of mainDeps) {
let disabledDeps = ""
let mainDeps: Package[] = []
for (let pkg of mainPkg.sortedDeps(true)) {
if (pkg.disablesVariant(pxt.appTargetVariant) ||
pkg.resolvedDependencies().some(d => d.disablesVariant(pxt.appTargetVariant))) {
if (pkg.id != "this") {
if (disabledDeps)
disabledDeps += ", "
disabledDeps += pkg.id
}
pxt.debug(`disable variant ${pxt.appTargetVariant} due to ${pkg.id}`)
continue
}
mainDeps.push(pkg)
pkg.addSnapshot(pkgSnapshot, [constsName, ".h", ".cpp"])
}

const key = JSON.stringify(pkgSnapshot)
if (prevExtInfos[key]) {
const prevInfo = prevExtInfos[key]
if (prevInfo) {
pxt.debug("Using cached extinfo")
return prevExtInfos[key]
const r = U.flatClone(prevInfo)
r.disabledDeps = disabledDeps
return r
}

pxt.debug("Generating new extinfo")
const res = pxtc.emptyExtInfo();

res.disabledDeps = disabledDeps

let compileService = appTarget.compileService;
if (!compileService)
compileService = {
Expand Down
51 changes: 33 additions & 18 deletions pxtlib/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ namespace pxt {
this.addedBy = [addedBy];
}

disablesVariant(v: string) {
return this.config && this.config.disablesVariants && this.config.disablesVariants.indexOf(v) >= 0
}

invalid(): boolean {
return /^invalid:/.test(this.version());
}
Expand Down Expand Up @@ -503,6 +507,10 @@ namespace pxt {
}
}

resolvedDependencies(): Package[] {
return Object.keys(this.dependencies()).map(n => this.resolveDep(n))
}

dependencies(includeCpp = false): pxt.Map<string> {
if (!this.config) return {};

Expand Down Expand Up @@ -881,34 +889,40 @@ namespace pxt {
}

const fillExtInfoAsync = async (variant: string) => {
let ext: pxtc.ExtensionInfo
let res: pxtc.ExtensionTarget = {
extinfo: null,
target: null
}

if (variant)
pxt.setAppTargetVariant(variant, { temporary: true })

try {
ext = cpp.getExtensionInfo(this)
let einfo = cpp.getExtensionInfo(this)
if (!variant) {
if (ext.shimsDTS) generateFile("shims.d.ts", ext.shimsDTS)
if (ext.enumsDTS) generateFile("enums.d.ts", ext.enumsDTS)
if (einfo.shimsDTS) generateFile("shims.d.ts", einfo.shimsDTS)
if (einfo.enumsDTS) generateFile("enums.d.ts", einfo.enumsDTS)
}

const inf = target.isNative ? await this.host().getHexInfoAsync(ext) : null
const inf = target.isNative ? await this.host().getHexInfoAsync(einfo) : null

ext = U.flatClone(ext)
einfo = U.flatClone(einfo)
if (!target.keepCppFiles) {
delete ext.compileData;
delete ext.generatedFiles;
delete ext.extensionFiles;
delete einfo.compileData;
delete einfo.generatedFiles;
delete einfo.extensionFiles;
}
ext.hexinfo = inf
einfo.hexinfo = inf

res.extinfo = einfo
res.target = pxt.appTarget.compile

} finally {
if (variant)
pxt.setAppTargetVariant(null, { temporary: true })
}

return ext
return res
}

await this.loadAsync()
Expand All @@ -918,22 +932,23 @@ namespace pxt {
pxt.debug(`building: ${this.sortedDeps().map(p => p.config.name).join(", ")}`)

let variants = pxt.appTarget.multiVariants
if (!variants || pxt.appTargetVariant) {
if (!variants || pxt.appTargetVariant || (!pxt.appTarget.alwaysMultiVariant && !pxt.appTarget.compile.switches.multiVariant)) {
variants = [pxt.appTargetVariant || ""]
}

let ext: pxtc.ExtensionInfo = null
for (let v of variants) {
if (ext)
pxt.debug(`building for ${v}`)
const curr = await fillExtInfoAsync(ext ? v : null)
curr.appVariant = v
curr.outputPrefix = variants.length == 1 || !v ? "" : v + "-"
const etarget = await fillExtInfoAsync(ext ? v : null)
const einfo = etarget.extinfo
einfo.appVariant = v
einfo.outputPrefix = variants.length == 1 || !v ? "" : v + "-"
if (ext) {
ext.otherMultiVariants.push(curr)
opts.otherMultiVariants.push(etarget)
} else {
ext = curr
ext.otherMultiVariants = []
ext = einfo
opts.otherMultiVariants = []
}
}
opts.extinfo = ext
Expand Down

0 comments on commit d342bd0

Please sign in to comment.