From f460bc7f1a4bda0706982639a2064f223fa1f3ac Mon Sep 17 00:00:00 2001 From: Rob1103 Date: Sat, 1 Apr 2023 19:09:14 +0200 Subject: [PATCH] include directive paths problem fix --- srcs/buildtool/run_buildtool.go | 5 ++- srcs/buildtool/unikraft_files_process.go | 49 ++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/srcs/buildtool/run_buildtool.go b/srcs/buildtool/run_buildtool.go index 8607148..55e615e 100644 --- a/srcs/buildtool/run_buildtool.go +++ b/srcs/buildtool/run_buildtool.go @@ -14,7 +14,7 @@ import ( "strings" u "tools/srcs/common" - "github.com/AlecAivazis/survey/v2" + "gopkg.in/AlecAivazis/survey.v1" ) // STATES @@ -234,7 +234,7 @@ func RunBuildTool(homeDir string, data *u.Data) { } var selectedFiles []string - if err := survey.AskOne(prompt, &selectedFiles); err != nil { + if err := survey.AskOne(prompt, &selectedFiles, nil); err != nil { panic(err) } @@ -277,6 +277,7 @@ func RunBuildTool(homeDir string, data *u.Data) { // Run make runMake(programName, appFolder) + } // retFolderCompat modifies its string argument in order to replace its underscore by a dash when diff --git a/srcs/buildtool/unikraft_files_process.go b/srcs/buildtool/unikraft_files_process.go index 034260b..611db21 100644 --- a/srcs/buildtool/unikraft_files_process.go +++ b/srcs/buildtool/unikraft_files_process.go @@ -12,6 +12,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "strings" u "tools/srcs/common" @@ -175,6 +176,49 @@ func filterSourcesFiles(sourceFiles []string) []string { return filterSrcFiles } +// conformIncDirAndCopyFile conforms all the include directives from a C/C++ source file so that no +// directive contains a path to a header file but the header file name only (i.e., the last element +// of the path). It also copies the content of the source file in the same way as CopyFileContents. +// +// It returns an error if any, otherwise it returns nil. +func conformIncDirAndCopyFile(sourcePath, destPath string) (err error) { + + fileLines, err := u.ReadLinesFile(sourcePath) + if err != nil { + return err + } + + // Find include directives using regexp + var re = regexp.MustCompile(`(.*)(#include)(.*)(<|")(.*)(>|")(.*)`) + + for index := range fileLines { + for _, match := range re.FindAllStringSubmatch(fileLines[index], -1) { + + // Only interested in include directives containing a path to a header file + if !strings.Contains(match[0], "/") { + continue + } + + // Replace the path by its last element + for i := 1; i < len(match); i++ { + if match[i] == "<" || match[i] == "\"" { + match[i+1] = filepath.Base(match[i+1]) + fileLines[index] = strings.Join(match[1:], "") + "\n" + break + } + } + } + } + + // Write the modified content to a file in the unikernel folder + err = u.WriteToFile(destPath, []byte(strings.Join(fileLines, ""))) + if err != nil { + return err + } + + return nil +} + func processSourceFiles(sourcesPath, appFolder, includeFolder string, sourceFiles, includesFiles []string) ([]string, error) { @@ -182,7 +226,6 @@ func processSourceFiles(sourcesPath, appFolder, includeFolder string, err error) error { if !info.IsDir() { - extension := filepath.Ext(info.Name()) if _, ok := srcLanguages[extension]; ok { // Add source files to sourceFiles list @@ -192,7 +235,7 @@ func processSourceFiles(sourcesPath, appFolder, includeFolder string, srcLanguages[extension] += 1 // Copy source files to the appFolder - if err = u.CopyFileContents(path, appFolder+info.Name()); err != nil { + if err = conformIncDirAndCopyFile(path, appFolder+info.Name()); err != nil { return err } } else if extension == ".h" || extension == ".hpp" || extension == ".hcc" { @@ -200,7 +243,7 @@ func processSourceFiles(sourcesPath, appFolder, includeFolder string, includesFiles = append(includesFiles, info.Name()) // Copy header files to the INCLUDEFOLDER - if err = u.CopyFileContents(path, includeFolder+info.Name()); err != nil { + if err = conformIncDirAndCopyFile(path, includeFolder+info.Name()); err != nil { return err } } else {