extractertool first improvement

This commit is contained in:
Rob1103 2022-12-08 15:21:25 +01:00
parent e7c459d86d
commit 816d1958ad
4 changed files with 106 additions and 79 deletions

View file

@ -159,6 +159,19 @@ func putJsonSymbolsTogether(data *u.Data) map[string]string {
return dataMap
}
// retNameCompat modifies its string argument in order to replace its underscore by a dash when
// necessary.
//
// It returns its string argument whose underscore has been replaced by a dash if necessary,
// otherwise it returns its argument unchanged.
func retNameForCompat(value string) string {
if strings.Contains(value, "posix-") {
return strings.ReplaceAll(value, "posix-", "posix_")
}
return value
}
// matchSymbols performs the matching between Unikraft's micro-libs and
// libraries used by a given application based on the list of symbols that both
// contain.
@ -169,8 +182,8 @@ func matchSymbols(matchedLibs []string, data map[string]string,
for key := range data {
if values, ok := microLibs[key]; ok {
for _, value := range values {
if !u.Contains(matchedLibs, value) {
matchedLibs = append(matchedLibs, value)
if !u.Contains(matchedLibs, retNameForCompat(value)) {
matchedLibs = append(matchedLibs, retNameForCompat(value))
}
}
}

View file

@ -261,8 +261,7 @@ func RunBuildTool(homeDir string, data *u.Data) {
// otherwise it returns its argument unchanged.
func retFolderForCompat(lib string) string {
if strings.Contains(lib, "posix_") {
folder := strings.ReplaceAll(lib, "posix_", "posix-")
return folder
return strings.ReplaceAll(lib, "posix_", "posix-")
}
return lib

View file

@ -6,7 +6,7 @@
package common
const branch = "RELEASE-0.7.0"
const branch = "RELEASE-0.10.0"
// GitCloneRepository clones a git repository at the the given url.
//

View file

@ -32,7 +32,7 @@ type Variables struct {
}
func getMakefileSources(content string, mapSources map[string]string) {
var re = regexp.MustCompile(`(?m)\/.*\.c|\/.*\.h|\/.*\.cpp`)
var re = regexp.MustCompile(`(?m)\/.*\.c|\/.*\.h|\/.*\.cpp|\/.*\.hpp|\/.*\.cc|\/.*\.hcc`)
for _, match := range re.FindAllString(content, -1) {
vars := strings.Split(match, "/")
mapSources[vars[len(vars)-1]] = match
@ -51,7 +51,7 @@ func findVariables(content string, mapVariables map[string]*Variables) {
value: "",
}
regexVar := regexp.MustCompile("(?m)" + v.name + "=.*$")
regexVar := regexp.MustCompile("(?m)" + v.name + "[ \t]*=.*$")
for _, matchVar := range regexVar.FindAllString(content, -1) {
v.value = matchVar
break
@ -93,7 +93,8 @@ func resolveVariables(mapVariables map[string]*Variables) {
func detectURL(mapVariables map[string]*Variables) *string {
for key, value := range mapVariables {
if strings.Contains(key, URL) && strings.Contains(value.value, HTTP) {
vars := strings.Split(value.value, "=")
spaceDel := strings.Join(strings.Split(value.value, " "), "")
vars := strings.Split(spaceDel, "=")
if len(vars) > 1 {
return &vars[1]
}
@ -146,7 +147,8 @@ func findSourcesFiles(workspace string) ([]string, error) {
}
ext := filepath.Ext(info.Name())
if ext == ".c" || ext == ".cpp" {
if ext == ".c" || ext == ".cpp" || ext == ".cc" || ext == ".h" || ext == ".hpp" ||
ext == ".hcc" {
filenames = append(filenames, path)
}
return nil
@ -179,7 +181,7 @@ func saveSymbols(output string, mapSymbols map[string]string, libName string) {
for _, s := range symbols {
if len(s) > 0 {
if _, ok := mapSymbols[s]; !ok {
if s == "main" || strings.Contains(s, "test") {
if s == "main" || strings.Contains(s, "test") || strings.Contains(s, "TEST") {
u.PrintWarning("Ignore function: " + s)
} else {
mapSymbols[s] = libName
@ -252,78 +254,81 @@ func RunExtracterTool(homeDir string) {
mapSources := make(map[string]string)
getMakefileSources(content, mapSources)
findVariables(content, mapVariables)
resolveVariables(mapVariables)
url := detectURL(mapVariables)
if url == nil {
u.PrintErr(errors.New("url of the lib not found"))
return
}
var fileExtension string
urlSplit := strings.Split(*url, "/")
if urlSplit[len(urlSplit)-1] == "download" {
fileExtension = filepath.Ext(urlSplit[len(urlSplit)-2])
} else {
fileExtension = filepath.Ext(*url)
}
folderName := lib + "_sources_folder"
created, err := CreateFolder(folderName)
var archiveName string
var sourcesFiltered []string
if url != nil {
var fileExtension string
urlSplit := strings.Split(*url, "/")
if urlSplit[len(urlSplit)-1] == "download" {
fileExtension = filepath.Ext(urlSplit[len(urlSplit)-2])
} else {
fileExtension = filepath.Ext(*url)
}
created, err := CreateFolder(folderName)
if err != nil {
u.PrintErr(err)
}
var files []string
if fileExtension == ".gz" {
archiveName = lib + "_sources.tar" + fileExtension
} else {
archiveName = lib + "_sources" + fileExtension
}
if created {
u.PrintInfo(*url + " is found. Download the lib sources...")
err := DownloadFile(archiveName, *url)
if err != nil {
u.PrintErr(err)
}
u.PrintOk(*url + " successfully downloaded.")
u.PrintInfo("Extracting " + archiveName + "...")
if fileExtension == ".zip" {
files, err = Unzip(archiveName, folderName)
if err != nil {
_ = os.Remove(archiveName)
_ = os.RemoveAll(folderName)
u.PrintErr(err.Error() + ". Corrupted archive. Please try again.")
}
} else if fileExtension == ".tar" || fileExtension == ".gz" || fileExtension == ".tgz" {
files, err = unTarGz(archiveName, folderName)
if err != nil {
_ = os.Remove(archiveName)
_ = os.RemoveAll(folderName)
u.PrintErr(err.Error() + ". Corrupted archive. Please try again.")
}
} else {
u.PrintErr(errors.New("unknown extension for archive"))
}
}
u.PrintInfo("Inspecting folder " + folderName + " for sources...")
folderFiles, err := findSourcesFiles(folderName)
if err != nil {
u.PrintErr(err)
}
sourcesFiltered = filterSourcesFiles(files, mapSources)
sourcesFiltered = append(sourcesFiltered, folderFiles...)
}
libpathFiles, err := findSourcesFiles(libpath)
if err != nil {
u.PrintErr(err)
}
var files []string
var archiveName string
if fileExtension == ".gz" {
archiveName = lib + "_sources.tar" + fileExtension
} else {
archiveName = lib + "_sources" + fileExtension
}
if created {
u.PrintInfo(*url + " is found. Download the lib sources...")
err := DownloadFile(archiveName, *url)
if err != nil {
u.PrintErr(err)
}
u.PrintOk(*url + " successfully downloaded.")
u.PrintInfo("Extracting " + archiveName + "...")
if fileExtension == ".zip" {
files, err = Unzip(archiveName, folderName)
if err != nil {
_ = os.Remove(archiveName)
_ = os.RemoveAll(folderName)
u.PrintErr(err.Error() + ". Corrupted archive. Please try again.")
}
} else if fileExtension == ".tar" || fileExtension == ".gz" || fileExtension == ".tgz" {
files, err = unTarGz(archiveName, folderName)
if err != nil {
_ = os.Remove(archiveName)
_ = os.RemoveAll(folderName)
u.PrintErr(err.Error() + ". Corrupted archive. Please try again.")
}
} else {
u.PrintErr(errors.New("unknown extension for archive"))
}
}
if len(files) == 0 {
u.PrintInfo("Inspecting folder " + folderName + " for sources...")
files, err = findSourcesFiles(folderName)
if err != nil {
u.PrintErr(err)
}
}
sourcesFiltered := filterSourcesFiles(files, mapSources)
sourcesFiltered = append(sourcesFiltered, libpathFiles...)
u.PrintInfo("Find " + strconv.Itoa(len(sourcesFiltered)) + " files to analyse")
@ -342,14 +347,24 @@ func RunExtracterTool(homeDir string) {
}
u.PrintOk(strconv.Itoa(len(mapSymbols)) + " symbols from " + lib + " have been extracted.")
filename := filepath.Join(os.Getenv("GOPATH"), "src", "tools", "libs", "external", lib)
var filename string
if url != nil {
filename = filepath.Join(os.Getenv("GOPATH"), "src", "tools", "libs", "external", lib)
} else {
filename = filepath.Join(os.Getenv("GOPATH"), "src", "tools", "libs", "internal", lib)
}
if err := u.RecordDataJson(filename, mf); err != nil {
u.PrintErr(err)
} else {
u.PrintOk("Symbols file have been written to " + filename + ".json")
}
u.PrintInfo("Remove folders " + archiveName + " and " + folderName)
_ = os.Remove(archiveName)
_ = os.RemoveAll(folderName)
if url != nil {
u.PrintInfo("Remove folders " + archiveName + " and " + folderName)
_ = os.Remove(archiveName)
_ = os.RemoveAll(folderName)
}
}