Skip to content

Commit 071a784

Browse files
authored
fixes for path calculations for issue #177 (#179)
1 parent 533bb19 commit 071a784

6 files changed

Lines changed: 52 additions & 52 deletions

File tree

internal/readFile/readFile.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package readfile
99
import (
1010
"errors"
1111
"os"
12-
"path"
1312
"path/filepath"
1413
"strings"
1514

@@ -64,18 +63,18 @@ func Process(inFile, inFile2, outPath string) error {
6463
if err != nil {
6564
return err
6665
}
67-
workDir := path.Dir(inFile)
66+
workDir := filepath.Dir(inFile)
6867
if params[0].Output != "" {
6968
if filepath.IsAbs(params[0].Output) {
7069
workDir = params[0].Output
7170
} else {
72-
workDir = path.Join(workDir, params[0].Output)
71+
workDir = filepath.Join(workDir, params[0].Output)
7372
}
7473
} else {
7574
if filepath.IsAbs(outPath) {
7675
workDir = outPath
7776
} else {
78-
workDir = path.Join(workDir, outPath)
77+
workDir = filepath.Join(workDir, outPath)
7978
}
8079
}
8180
workDir = filepath.Clean(workDir)

internal/stm32CubeMX/mxDevice.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"io/fs"
1313
"math"
1414
"os"
15-
"path"
1615
"path/filepath"
1716
"regexp"
1817
"sort"
@@ -42,7 +41,7 @@ func ReadContexts(iocFile string, params []BridgeParamType) error {
4241
return err
4342
}
4443

45-
workDir := path.Dir(iocFile)
44+
workDir := filepath.Dir(iocFile)
4645

4746
mainFolder := contextMap["ProjectManager"]["MainLocation"]
4847
if mainFolder == "" {
@@ -52,7 +51,7 @@ func ReadContexts(iocFile string, params []BridgeParamType) error {
5251
for _, context := range contexts {
5352
for _, parm := range params {
5453
if parm.CubeContext == context {
55-
srcFolderPath := path.Join(path.Join(workDir, parm.CubeContextFolder), mainFolder)
54+
srcFolderPath := filepath.Join(filepath.Join(workDir, parm.CubeContextFolder), mainFolder)
5655

5756
var mspName string
5857
err = filepath.Walk(srcFolderPath, func(path string, f fs.FileInfo, err error) error {
@@ -70,10 +69,10 @@ func ReadContexts(iocFile string, params []BridgeParamType) error {
7069
}
7170

7271
var cfgPath string
73-
cfgPath = path.Dir(workDir)
74-
cfgPath = path.Join(cfgPath, "MX_Device")
72+
cfgPath = filepath.Dir(workDir)
73+
cfgPath = filepath.Join(cfgPath, "MX_Device")
7574
if parm.CubeContextFolder != "" {
76-
cfgPath = path.Join(cfgPath, parm.CubeContextFolder)
75+
cfgPath = filepath.Join(cfgPath, parm.CubeContextFolder)
7776
}
7877
err := writeMXdeviceH(contextMap, srcFolderPath, mspName, cfgPath, context)
7978
if err != nil {
@@ -127,16 +126,14 @@ func writeMXdeviceH(contextMap map[string]map[string]string, srcFolder string, m
127126
var fMsp *os.File
128127

129128
if generatedAsPair != "true" {
130-
main := path.Join(srcFolderAbs, "main.c")
131-
main = filepath.Clean(main)
129+
main := filepath.Join(srcFolderAbs, "main.c")
132130
main = filepath.ToSlash(main)
133131
fMain, err = os.Open(main)
134132
if err != nil {
135133
return err
136134
}
137135

138-
msp := path.Join(srcFolderAbs, mspName)
139-
msp = filepath.Clean(msp)
136+
msp := filepath.Join(srcFolderAbs, mspName)
140137
msp = filepath.ToSlash(msp)
141138
fMsp, err = os.Open(msp)
142139
if err != nil {
@@ -159,8 +156,7 @@ func writeMXdeviceH(contextMap map[string]map[string]string, srcFolder string, m
159156
return err
160157
}
161158
}
162-
fPath = path.Join(fPath, fName)
163-
fPath = filepath.Clean(fPath)
159+
fPath = filepath.Join(fPath, fName)
164160
fPath = filepath.ToSlash(fPath)
165161
fMxDevice, err := os.Create(fPath)
166162
if err != nil {
@@ -215,8 +211,7 @@ func writeMXdeviceH(contextMap map[string]map[string]string, srcFolder string, m
215211
} else if strings.Contains(peripheral, "FD") {
216212
fileName = "fdcan.c"
217213
}
218-
periPath := path.Join(srcFolderAbs, fileName)
219-
periPath = filepath.Clean(periPath)
214+
periPath := filepath.Join(srcFolderAbs, fileName)
220215
periPath = filepath.ToSlash(periPath)
221216
fPeri, errPeri := os.Open(periPath)
222217
if errPeri != nil {

internal/stm32CubeMX/stm32CubeMX.go

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"io/fs"
1313
"os"
1414
"os/exec"
15-
"path"
1615
"path/filepath"
1716
"runtime"
1817
"strings"
@@ -86,8 +85,7 @@ func Process(cbuildGenIdxYmlPath, outPath, cubeMxPath string, runCubeMx bool, pi
8685
return err
8786
}
8887
exPath := filepath.Dir(ex)
89-
exPath = filepath.ToSlash(exPath)
90-
cRoot = path.Dir(exPath)
88+
cRoot = filepath.Dir(exPath)
9189
}
9290
var generatorFile string
9391
err := filepath.Walk(cRoot, func(path string, f fs.FileInfo, err error) error {
@@ -122,15 +120,19 @@ func Process(cbuildGenIdxYmlPath, outPath, cubeMxPath string, runCubeMx bool, pi
122120
return err
123121
}
124122

125-
workDir := path.Dir(cbuildGenIdxYmlPath)
123+
workDir := filepath.Dir(cbuildGenIdxYmlPath)
126124
if parms.Output != "" {
127125
if filepath.IsAbs(parms.Output) {
128126
workDir = parms.Output
129127
} else {
130-
workDir = path.Join(workDir, parms.Output)
128+
workDir = filepath.Join(workDir, parms.Output)
131129
}
132130
} else {
133-
workDir = path.Join(workDir, outPath)
131+
if filepath.IsAbs(outPath) {
132+
workDir = outPath
133+
} else {
134+
workDir = filepath.Join(workDir, outPath)
135+
}
134136
}
135137
workDir = filepath.Clean(workDir)
136138
workDir = filepath.ToSlash(workDir)
@@ -144,10 +146,10 @@ func Process(cbuildGenIdxYmlPath, outPath, cubeMxPath string, runCubeMx bool, pi
144146
if pid >= 0 {
145147
lastPath := filepath.Base(cubeIocPath)
146148
if lastPath != "STM32CubeMX" {
147-
cubeIocPath = path.Join(cubeIocPath, "STM32CubeMX")
149+
cubeIocPath = filepath.Join(cubeIocPath, "STM32CubeMX")
148150
}
149-
iocprojectPath := path.Join(cubeIocPath, "STM32CubeMX.ioc")
150-
mxprojectPath := path.Join(cubeIocPath, ".mxproject")
151+
iocprojectPath := filepath.Join(cubeIocPath, "STM32CubeMX.ioc")
152+
mxprojectPath := filepath.Join(cubeIocPath, ".mxproject")
151153
log.Debugf("pid of CubeMX in daemon: %d", pid)
152154
running = true
153155
first := true
@@ -272,9 +274,9 @@ func Process(cbuildGenIdxYmlPath, outPath, cubeMxPath string, runCubeMx bool, pi
272274
if runCubeMx {
273275
lastPath := filepath.Base(cubeIocPath)
274276
if lastPath != "STM32CubeMX" {
275-
cubeIocPath = path.Join(cubeIocPath, "STM32CubeMX")
277+
cubeIocPath = filepath.Join(cubeIocPath, "STM32CubeMX")
276278
}
277-
cubeIocPath = path.Join(cubeIocPath, "STM32CubeMX.ioc")
279+
cubeIocPath = filepath.Join(cubeIocPath, "STM32CubeMX.ioc")
278280

279281
var err error
280282
var pid int
@@ -339,17 +341,19 @@ func Launch(iocFile, projectFile string) (int, error) {
339341

340342
switch runtime.GOOS {
341343
case "windows":
342-
pathJava = path.Join(cubeEnv, "jre", "bin", "java.exe")
343-
pathCubeMx = path.Join(cubeEnv, "STM32CubeMX.exe")
344+
pathJava = filepath.Join(cubeEnv, "jre", "bin", "java.exe")
345+
pathCubeMx = filepath.Join(cubeEnv, "STM32CubeMX.exe")
346+
344347
case "darwin":
345-
pathJava = path.Join(cubeEnv, "jre", "Contents", "Home", "bin", "java")
346-
arg0 = path.Join(cubeEnv, "stm32cubemx.icns")
348+
pathJava = filepath.Join(cubeEnv, "jre", "Contents", "Home", "bin", "java")
349+
arg0 = filepath.Join(cubeEnv, "stm32cubemx.icns")
347350
arg0 = "-Xdock:icon=" + arg0
348351
arg1 = "-Xdock:name=STM32CubeMX"
349-
pathCubeMx = path.Join(cubeEnv, "STM32CubeMX")
352+
pathCubeMx = filepath.Join(cubeEnv, "STM32CubeMX")
353+
350354
default:
351-
pathJava = path.Join(cubeEnv, "jre", "bin", "java")
352-
pathCubeMx = path.Join(cubeEnv, "STM32CubeMX")
355+
pathJava = filepath.Join(cubeEnv, "jre", "bin", "java")
356+
pathCubeMx = filepath.Join(cubeEnv, "STM32CubeMX")
353357
}
354358

355359
if runtime.GOOS == "darwin" {
@@ -626,7 +630,7 @@ func WriteCgenYmlSub(outPath string, mxproject MxprojectType, bridgeParam Bridge
626630

627631
cfgPath := "MX_Device"
628632
if bridgeParam.CubeContextFolder != "" {
629-
cfgPath = path.Join(cfgPath, bridgeParam.CubeContextFolder)
633+
cfgPath = filepath.Join(cfgPath, bridgeParam.CubeContextFolder)
630634
}
631635
cfgPath, _ = utils.ConvertFilename(outPath, cfgPath, "")
632636
cgen.GeneratorImport.AddPath = append(cgen.GeneratorImport.AddPath, cfgPath)
@@ -739,9 +743,9 @@ func GetRelativePathAdd(outPath string, compiler string) (string, error) {
739743
lastPath := filepath.Base(outPath)
740744
var relativePathAdd string
741745
if lastPath != "STM32CubeMX" {
742-
relativePathAdd = path.Join(relativePathAdd, "STM32CubeMX")
746+
relativePathAdd = filepath.Join(relativePathAdd, "STM32CubeMX")
743747
}
744-
relativePathAdd = path.Join(relativePathAdd, folder)
748+
relativePathAdd = filepath.Join(relativePathAdd, folder)
745749

746750
return relativePathAdd, nil
747751
}
@@ -762,9 +766,9 @@ func GetToolchainFolderPath(outPath string, compiler string) (string, error) {
762766
lastPath := filepath.Base(outPath)
763767
toolchainFolderPath := outPath
764768
if lastPath != "STM32CubeMX" {
765-
toolchainFolderPath = path.Join(outPath, "STM32CubeMX")
769+
toolchainFolderPath = filepath.Join(outPath, "STM32CubeMX")
766770
}
767-
toolchainFolderPath = path.Join(toolchainFolderPath, toolchainFolder)
771+
toolchainFolderPath = filepath.Join(toolchainFolderPath, toolchainFolder)
768772

769773
return toolchainFolderPath, nil
770774
}
@@ -784,7 +788,7 @@ func GetStartupFile(outPath string, bridgeParams BridgeParamType) (string, error
784788
}
785789

786790
if bridgeParams.Compiler == "GCC" || bridgeParams.Compiler == "CLANG" {
787-
startupFolder = path.Join(startupFolder, bridgeParams.CubeContextFolder, "Application", "Startup")
791+
startupFolder = filepath.Join(startupFolder, bridgeParams.CubeContextFolder, "Application", "Startup")
788792
}
789793

790794
if !utils.DirExists(startupFolder) {
@@ -854,7 +858,7 @@ func GetSystemFile(outPath string, bridgeParams BridgeParamType) (string, error)
854858

855859
if bridgeParams.ProjectType == "multi-core" {
856860
systemFolder = filepath.Dir(toolchainFolder)
857-
systemFolder = path.Join(systemFolder, "Common")
861+
systemFolder = filepath.Join(systemFolder, "Common")
858862
if !utils.DirExists(toolchainFolder) {
859863
systemFolder = ""
860864
}
@@ -864,10 +868,10 @@ func GetSystemFile(outPath string, bridgeParams BridgeParamType) (string, error)
864868
systemFolder = filepath.Dir(toolchainFolder)
865869

866870
if bridgeParams.CubeContextFolder != "" {
867-
systemFolder = path.Join(systemFolder, bridgeParams.CubeContextFolder)
871+
systemFolder = filepath.Join(systemFolder, bridgeParams.CubeContextFolder)
868872
}
869873

870-
systemFolder = path.Join(systemFolder, "Src")
874+
systemFolder = filepath.Join(systemFolder, "Src")
871875
}
872876

873877
if !utils.DirExists(systemFolder) {
@@ -917,7 +921,7 @@ func GetSystemFile(outPath string, bridgeParams BridgeParamType) (string, error)
917921
// }
918922

919923
// if bridgeParams.GeneratorMap != "" {
920-
// linkerFolder = path.Join(linkerFolder, bridgeParams.GeneratorMap)
924+
// linkerFolder = filepath.Join(linkerFolder, bridgeParams.GeneratorMap)
921925
// }
922926

923927
// switch bridgeParams.Compiler {
@@ -939,13 +943,13 @@ func GetSystemFile(outPath string, bridgeParams BridgeParamType) (string, error)
939943
// case "GCC", "CLANG":
940944
// switch bridgeParams.ProjectType {
941945
// case "multi-core":
942-
// linkerFolder = path.Join(linkerFolder, bridgeParams.ForProjectPart)
946+
// linkerFolder = filepath.Join(linkerFolder, bridgeParams.ForProjectPart)
943947
// case "trustzone":
944948
// if bridgeParams.ForProjectPart == "secure" {
945-
// linkerFolder = path.Join(linkerFolder, "Secure")
949+
// linkerFolder = filepath.Join(linkerFolder, "Secure")
946950
// }
947951
// if bridgeParams.ForProjectPart == "non-secure" {
948-
// linkerFolder = path.Join(linkerFolder, "NonSecure")
952+
// linkerFolder = filepath.Join(linkerFolder, "NonSecure")
949953
// }
950954
// }
951955
// default:

internal/stm32CubeMX/stm32CubeMX_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func Test_GetRelativePathAdd(t *testing.T) {
320320
t.Errorf("GetRelativePathAdd() %s error = %v, wantErr %v", tt.name, err, tt.wantErr)
321321
return
322322
}
323+
got = filepath.ToSlash(got)
323324
if got != tt.want {
324325
t.Errorf("GetRelativePathAdd() %s = %v, want %v", tt.name, got, tt.want)
325326
}
@@ -359,6 +360,7 @@ func Test_GetToolchainFolderPath(t *testing.T) {
359360
t.Errorf("GetToolchainFolderPath() %s error = %v, wantErr %v", tt.name, err, tt.wantErr)
360361
return
361362
}
363+
got = filepath.ToSlash(got)
362364
if got != tt.want {
363365
t.Errorf("GetToolchainFolderPath() %s = %v, want %v", tt.name, got, tt.want)
364366
}

internal/utils/utils.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package utils
99
import (
1010
"errors"
1111
"os"
12-
"path"
1312
"path/filepath"
1413

1514
log "github.com/sirupsen/logrus"
@@ -72,8 +71,8 @@ func ConvertFilename(outPath, file, relativePathAdd string) (string, error) {
7271

7372
// Check if the file is absolute
7473
if !filepath.IsAbs(file) {
75-
toolchainPath := path.Join(outPath, relativePathAdd) // create the path where STCube sets it's files relative to toolchain folder( example :./STM32CubeMX/MDK-ARM/)
76-
file = path.Join(toolchainPath, file)
74+
toolchainPath := filepath.Join(outPath, relativePathAdd) // create the path where STCube sets it's files relative to toolchain folder( example :./STM32CubeMX/MDK-ARM/)
75+
file = filepath.Join(toolchainPath, file)
7776
}
7877

7978
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {

internal/utils/utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ func TestConvertFilename(t *testing.T) {
144144
want string
145145
wantErr bool
146146
}{
147+
{"testNetwork", args{"../../testdata", "\\\\network\\test.ioc", "stm32cubemx"}, true, "//network/test.ioc", false},
147148
{"testAbs", args{"../../testdata", "C:/test.ioc", "stm32cubemx"}, true, "C:/test.ioc", false},
148149
{"test", args{"../../testdata", "test.ioc", "stm32cubemx"}, false, "./stm32cubemx/test.ioc", false},
149150
{"nix", args{"../../testdata", "nix", "stm32cubemx"}, false, "./stm32cubemx/nix", false},

0 commit comments

Comments
 (0)