Skip to content

Commit

Permalink
Capture module repo verions and binary lib info (file hash)
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Mar 11, 2021
1 parent ad799f8 commit 0210656
Show file tree
Hide file tree
Showing 45 changed files with 269 additions and 112 deletions.
1 change: 1 addition & 0 deletions cli/build/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type FWAppManifestLibHandled struct {
Deps []string `yaml:"deps,omitempty" json:"deps"`
InitDeps []string `yaml:"init_deps,omitempty" json:"init_deps"`
Sources []string `yaml:"sources,omitempty" json:"sources"`
BinaryLibs []string `yaml:"binary_libs,omitempty" json:"binary_libs"`
Version string `yaml:"version,omitempty" json:"version"`
RepoVersion string `yaml:"repo_version,omitempty" json:"version"`
Manifest *FWAppManifest `yaml:"-" json:"-"`
Expand Down
28 changes: 22 additions & 6 deletions cli/manifest_parser/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 21 additions & 5 deletions cli/manifest_parser/data/mgos_deps_init.c.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,59 @@
struct mgos_lib_info {
const char *name;
const char *version;
const char *repo_version;
const char *binary_libs;
bool (*init)(void);
};
#define MGOS_LIB_INFO_VERSION 2
#endif

#ifndef MGOS_MODULE_INFO_VERSION
struct mgos_module_info {
const char *name;
const char *version;
};
#define MGOS_MODULE_INFO_VERSION 1
#endif

const struct mgos_lib_info mgos_libs_info[] = {
{{range .Libs}}
// "{{.Name}}". deps: [ {{range .Deps}}"{{.}}" {{end}}]
{.name = "{{.Name}}", .version = "{{.Version}}", .init = {{.InitFunc}}},
// {{.Name}}. deps: [ {{range .Deps}}"{{.}}" {{end}}]
#if MGOS_LIB_INFO_VERSION == 1
{.name = {{.Name}}, .version = {{.Version}}, .init = {{.InitFunc}}},
#else
{.name = {{.Name}}, .version = {{.Version}}, .repo_version = {{.RepoVersion}}, .binary_libs = {{.BinaryLibs}}, .init = {{.InitFunc}}},
#endif
{{end}}
// Last entry.
{.name = NULL},
};

const struct mgos_module_info mgos_modules_info[] = {
{{range .Modules}}
{.name = "{{.Name}}", .version = "{{.Version}}"},
{.name = {{.Name}}, .repo_version = {{.RepoVersion}}},
{{end}}
// Last entry.
{.name = NULL},
};

bool mgos_deps_init(void) {
for (const struct mgos_lib_info *l = mgos_libs_info; l->name != NULL; l++) {
LOG(LL_DEBUG, ("Init %s %s...", l->name, l->version));
#if MGOS_LIB_INFO_VERSION == 1
LOG(LL_DEBUG, ("Init %s %s...", l->name, (l->version ? l->version : "")));
#else
LOG(LL_DEBUG, ("Init %s %s (%s)...",
l->name,
(l->version ? l->version : ""),
(l->repo_version ? l->repo_version : "")));
#endif
if (l->init != NULL && !l->init()) {
LOG(LL_ERROR, ("%s init failed", l->name));
return false;
}
}
for (const struct mgos_module_info *m = mgos_modules_info; m->name != NULL; m++) {
LOG(LL_DEBUG, ("Module %s %s", m->name, m->version));
LOG(LL_DEBUG, ("Module %s %s", m->name, (m->repo_version ? m->repo_version : "")));
}
return true;
}
118 changes: 69 additions & 49 deletions cli/manifest_parser/manifest_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package manifest_parser

import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -282,35 +283,6 @@ func ReadManifestFinal(
gitinst := mosgit.NewOurGit(nil)
mosHash, _ := gitinst.GetCurrentHash(fp.MosDirEffective)

if manifest.Type == build.AppTypeApp {
// Generate deps_init C code, and if it's not empty, write it to the temp
// file and add to sources
depsCCode, err := getDepsInitCCode(manifest, mosHash)
if err != nil {
return nil, nil, errors.Trace(err)
}

if len(depsCCode) != 0 {
fname := moscommon.GetDepsInitCFilePath(buildDirAbs)

if err := os.MkdirAll(moscommon.GetGeneratedFilesDir(buildDirAbs), 0777); err != nil {
return nil, nil, errors.Trace(err)
}

if err = ioutil.WriteFile(fname, depsCCode, 0666); err != nil {
return nil, nil, errors.Trace(err)
}

// The modification time of autogenerated file should be set to that of
// the manifest itself, so that make handles dependencies correctly.
if err := os.Chtimes(fname, mtime, mtime); err != nil {
return nil, nil, errors.Trace(err)
}

manifest.Sources = append(manifest.Sources, fname)
}
}

// Convert manifest.Sources into paths to concrete existing source files.
manifest.Sources, fp.AppSourceDirs, err = resolvePaths(manifest.Sources, *sourceGlobs)
if err != nil {
Expand Down Expand Up @@ -421,6 +393,7 @@ func ReadManifestFinal(
if binaryLib != "" {
// We should use binary lib instead of sources
manifest.LibsHandled[k].Sources = []string{}
manifest.LibsHandled[k].BinaryLibs = append(manifest.LibsHandled[k].BinaryLibs, binaryLib)
manifest.BinaryLibs = append(manifest.BinaryLibs, binaryLib)
} else {
// Use lib sources, not prebuilt binary
Expand All @@ -440,6 +413,34 @@ func ReadManifestFinal(

fp.AppSourceDirs = append(fp.AppSourceDirs, libSourceDirs...)
}

// Generate deps_init C code, and if it's not empty, write it to the temp
// file and add to sources
depsCCode, err := getDepsInitCCode(manifest, mosHash)
if err != nil {
return nil, nil, errors.Trace(err)
}

if len(depsCCode) != 0 {
fname := moscommon.GetDepsInitCFilePath(buildDirAbs)

if err := os.MkdirAll(moscommon.GetGeneratedFilesDir(buildDirAbs), 0777); err != nil {
return nil, nil, errors.Trace(err)
}

if err = ioutil.WriteFile(fname, depsCCode, 0666); err != nil {
return nil, nil, errors.Trace(err)
}

// The modification time of autogenerated file should be set to that of
// the manifest itself, so that make handles dependencies correctly.
if err := os.Chtimes(fname, mtime, mtime); err != nil {
return nil, nil, errors.Trace(err)
}

manifest.Sources = append(manifest.Sources, fname)
}

}

manifest.BinaryLibs, fp.AppBinLibDirs, err = resolvePaths(manifest.BinaryLibs, []string{"*.a"})
Expand Down Expand Up @@ -1650,23 +1651,32 @@ func mergeSupportedPlatforms(p1, p2 []string) []string {
}

type libInfo struct {
Name string
Version string
HaveInit bool
InitFunc string
Deps []string
Name string
Version string
RepoVersion string
BinaryLibs string
HaveInit bool
InitFunc string
Deps []string
}

type moduleInfo struct {
Name string
Version string
Name string
RepoVersion string
}

type libsInitData struct {
Libs []libInfo
Modules []moduleInfo
}

func quoteOrNULL(s string) string {
if s == "" {
return "NULL"
}
return fmt.Sprintf("%q", s)
}

func getDepsInitCCode(manifest *build.FWAppManifest, mosHash string) ([]byte, error) {
if len(manifest.LibsHandled) == 0 {
return nil, nil
Expand All @@ -1681,34 +1691,44 @@ func getDepsInitCCode(manifest *build.FWAppManifest, mosHash string) ([]byte, er
break
}
}
lv := lh.Version
if lh.RepoVersion != "" {
lv = fmt.Sprintf("%s/%s", lv, lh.RepoVersion)
}
initFunc := "NULL"
if len(lh.Sources) > 0 {
if len(lh.Sources) > 0 || len(lh.BinaryLibs) > 0 {
initFunc = fmt.Sprintf("mgos_%s_init", ourutil.IdentifierFromString(lh.Lib.Name))
}
var blHashes []string
for _, fname := range lh.BinaryLibs {
data, err := ioutil.ReadFile(fname)
if err != nil {
return nil, errors.Annotatef(err, "failed to checksum binary lib file")
}
dataHash := sha256.Sum256(data)
blHashes = append(blHashes, fmt.Sprintf("%s:%x", filepath.Base(fname), dataHash))
}
tplData.Libs = append(tplData.Libs, libInfo{
Name: lh.Lib.Name,
Version: lv,
HaveInit: initFunc != "NULL",
InitFunc: initFunc,
Deps: lh.InitDeps,
Name: quoteOrNULL(lh.Lib.Name),
Version: quoteOrNULL(lh.Version),
RepoVersion: quoteOrNULL(lh.RepoVersion),
BinaryLibs: quoteOrNULL(strings.Join(blHashes, ",")),
HaveInit: initFunc != "NULL",
InitFunc: initFunc,
Deps: lh.InitDeps,
})
}

tplData.Modules = []moduleInfo{
moduleInfo{Name: "mongoose-os", Version: mosHash},
moduleInfo{
Name: quoteOrNULL("mongoose-os"),
RepoVersion: quoteOrNULL(mosHash),
},
}
for _, m := range manifest.Modules {
mv := ""
if lmv, err := m.GetLocalVersion(); err == nil {
mv = lmv
}
tplData.Modules = append(tplData.Modules, moduleInfo{
Name: m.Name,
Version: mv,
Name: quoteOrNULL(m.Name),
RepoVersion: quoteOrNULL(mv),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ __ALL_PLATFORMS__
author: mongoose-os
description: My test app
sources:
- __APP_ROOT__/app/build/gen/mgos_deps_init.c
- __APP_ROOT__/libs/mylib1/src/mylib1_src1.c
- __APP_ROOT__/libs/mylib2/src/mylib2_src1.c
- __APP_ROOT__/libs/mylib3/src/mylib3_src1.c
- __APP_ROOT__/app/build/gen/mgos_deps_init.c
includes:
- __APP_ROOT__/libs/mylib2/include
- __APP_ROOT__/libs/mylib3/include
Expand Down
Loading

0 comments on commit 0210656

Please sign in to comment.