func write(cfg Cfg, w io.Writer) {
	fmt.Fprintf(w, newtutil.GeneratedPreamble())

	fmt.Fprintf(w, "#ifndef H_MYNEWT_SYSCFG_\n")
	fmt.Fprintf(w, "#define H_MYNEWT_SYSCFG_\n\n")

	writeCheckMacros(w)
	fmt.Fprintf(w, "\n")

	writeSettings(cfg, w)
	fmt.Fprintf(w, "\n")

	fmt.Fprintf(w, "#endif\n")
}
func (flashMap FlashMap) writeSrc(w io.Writer) {
	fmt.Fprintf(w, newtutil.GeneratedPreamble())

	fmt.Fprintf(w, "#include \"%s\"\n", HEADER_PATH)
	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "%s", C_VAR_COMMENT)
	fmt.Fprintf(w, "%s = {", flashMap.varDecl())

	for _, area := range flashMap.SortedAreas() {
		fmt.Fprintf(w, "\n")
		area.writeSrc(w)
	}

	fmt.Fprintf(w, "};\n")
}
func (flashMap FlashMap) writeHeader(w io.Writer) {
	fmt.Fprintf(w, newtutil.GeneratedPreamble())

	fmt.Fprintf(w, "#ifndef H_MYNEWT_SYSFLASH_\n")
	fmt.Fprintf(w, "#define H_MYNEWT_SYSFLASH_\n")
	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "#include \"flash_map/flash_map.h\"\n")
	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "%s", C_VAR_COMMENT)
	fmt.Fprintf(w, "extern %s;\n", flashMap.varDecl())
	fmt.Fprintf(w, "\n")

	for _, area := range flashMap.SortedAreas() {
		area.writeHeader(w)
	}

	fmt.Fprintf(w, "\n#endif\n")
}
func write(pkgs []*pkg.LocalPackage, isLoader bool,
	w io.Writer) {

	goodPkgs := onlyPkgsWithInit(pkgs)
	stageMap := buildStageMap(goodPkgs)

	i := 0
	stages := make([]int, len(stageMap))
	for k, _ := range stageMap {
		stages[i] = k
		i++
	}
	sort.Ints(stages)

	fmt.Fprintf(w, newtutil.GeneratedPreamble())

	if isLoader {
		fmt.Fprintf(w, "#if SPLIT_LOADER\n\n")
	} else {
		fmt.Fprintf(w, "#if !SPLIT_LOADER\n\n")
	}

	writePrototypes(goodPkgs, w)

	var fnName string
	if isLoader {
		fnName = "sysinit_loader"
	} else {
		fnName = "sysinit_app"
	}

	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "void\n%s(void)\n{\n", fnName)
	fmt.Fprintf(w, "    os_init();\n")

	for _, s := range stages {
		fmt.Fprintf(w, "\n")
		writeStage(s, stageMap[s], w)
	}

	fmt.Fprintf(w, "}\n\n")
	fmt.Fprintf(w, "#endif\n")
}