Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for Linux #59

Merged
merged 13 commits into from
Jun 27, 2024
9 changes: 5 additions & 4 deletions cmd/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func configureGlobalCmd(cmd *cobra.Command, args []string) error {
log.SetOutput(io.Discard)
}
} else {
defer func() {
_ = f.Close()
log.SetOutput(io.Discard) // no more logging after closing the log file
}()
stm32cubemx.LogFile = f
// defer func() {
// _ = f.Close()
// log.SetOutput(io.Discard) // no more logging after closing the log file
// }()
log.SetOutput(f)
}
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
package main

import (
"io"
"os"
"time"

"github.com/open-cmsis-pack/generator-bridge/cmd/commands"
stm32cubemx "github.com/open-cmsis-pack/generator-bridge/internal/stm32CubeMX"
"github.com/open-cmsis-pack/generator-bridge/internal/utils"
log "github.com/sirupsen/logrus"
)
Expand All @@ -31,5 +33,9 @@
}

log.Debugf("Took %v", time.Since(start))
if stm32cubemx.LogFile != nil {
stm32cubemx.LogFile.Close()
Fixed Show fixed Hide fixed
}
log.SetOutput((io.Discard))
utils.StopSignalWatcher()
}
102 changes: 84 additions & 18 deletions internal/stm32CubeMX/stm32CubeMX.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"runtime"
"strings"
"syscall"
"time"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -44,9 +45,26 @@ type BridgeParamType struct {
var watcher *fsnotify.Watcher
var running bool // true if running in wait loop waiting for .ioc file

var LogFile *os.File

func procWait(proc *os.Process) {
if proc != nil {
_, _ = proc.Wait()
if runtime.GOOS == "windows" {
_, err := proc.Wait()
if err != nil {
log.Infof("Cannot wait for CubeMX to end, err %v", err)
return
}
} else {
for {
err := proc.Signal(syscall.Signal(0))
if err != nil {
log.Infoln("Cannot Signal to CubeMX")
break
}
time.Sleep(time.Millisecond * 200)
}
}
log.Debugln("CubeMX ended")
if watcher != nil {
watcher.Close()
Expand Down Expand Up @@ -135,6 +153,10 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int)
for {
proc, err := os.FindProcess(pid) // this only works for windows as it is now
bgn42 marked this conversation as resolved.
Show resolved Hide resolved
if err == nil { // cubeMX already runs
err = proc.Signal(syscall.Signal(0))
if err != nil {
break // out of loop if CubeMX does not run anymore
}
if first { // only start wait thread once
go procWait(proc)
first = false
Expand Down Expand Up @@ -194,7 +216,7 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int)
infomx0, _ = os.Stat(mxprojectPath)
}
changes++
if changes >= 4 {
if changes >= 2 {
changes = 0
i := 1
for ; i < 100; i++ {
Expand All @@ -217,12 +239,16 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int)
if err != nil {
return
}
log.Debugln("Writing Cgen.yml file")
err = WriteCgenYml(workDir, mxproject, bridgeParams)
if err != nil {
return
}
}
}
if LogFile != nil {
_ = LogFile.Sync()
}
}
case err := <-watcher.Errors:
if err != nil {
Expand Down Expand Up @@ -274,11 +300,21 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int)
}
log.Debugf("pid of CubeMX in main: %d", pid)
// here cubeMX runs
ownPath := path.Base(os.Args[0]) //nolint
cmd := exec.Command(ownPath) //nolint
exe, err := os.Executable()
if err != nil {
return err
}
log.Debugf("exe %s", exe)
ownPath, err := filepath.EvalSymlinks((exe))
if err != nil {
return err
}
cmd := exec.Command(ownPath) //nolint
log.Debugf("daemonize as %s", ownPath)
cmd.Args = os.Args
cmd.Args = append(cmd.Args, "-p", fmt.Sprint(pid)) // pid of cubeMX
if err := cmd.Start(); err != nil { // start myself as a daemon
log.Debugf("cmd.Start as %v", cmd)
if err := cmd.Start(); err != nil { // start myself as a daemon
log.Fatal(err)
return err
}
Expand All @@ -287,29 +323,59 @@ func Process(cbuildYmlPath, outPath, cubeMxPath string, runCubeMx bool, pid int)
}

func Launch(iocFile, projectFile string) (int, error) {
if iocFile == "" {
log.Infoln("Launching STM32CubeMX...")
} else {
log.Infoln("Launching STM32CubeMX with ", iocFile)
}

const cubeEnvVar = "STM32CubeMX_PATH"
cubeEnv := os.Getenv(cubeEnvVar)
if cubeEnv == "" {
return -1, errors.New("environment variable for CubeMX not set: " + cubeEnvVar)
}

pathJava := path.Join(cubeEnv, "jre", "bin", "java.exe")
pathCubeMx := path.Join(cubeEnv, "STM32CubeMX.exe")

var cmd *exec.Cmd
if iocFile != "" {
cmd = exec.Command(pathJava, "-jar", pathCubeMx, iocFile)
log.Infoln("Launching STM32CubeMX with ", iocFile)
} else if projectFile != "" {
cmd = exec.Command(pathJava, "-jar", pathCubeMx, "-s", projectFile)
log.Infoln("Launching STM32CubeMX with -s ", projectFile)
} else {
cmd = exec.Command(pathJava, "-jar", pathCubeMx)
log.Infoln("Launching STM32CubeMX...")
}

var pathJava string
var arg0 string
var arg1 string
var pathCubeMx string
var cmd *exec.Cmd

switch runtime.GOOS {
case "windows":
pathJava = path.Join(cubeEnv, "jre", "bin", "java.exe")
pathCubeMx = path.Join(cubeEnv, "STM32CubeMX.exe")
case "darwin":
pathJava = path.Join(cubeEnv, "jre", "Contents", "Home", "bin", "java")
arg0 = path.Join(cubeEnv, "stm32cubemx.icns")
arg0 = "-Xdock:icon=" + arg0
arg1 = "-Xdock:name=STM32CubeMX"
pathCubeMx = path.Join(cubeEnv, "STM32CubeMX")
default:
pathJava = path.Join(cubeEnv, "jre", "bin", "java")
pathCubeMx = path.Join(cubeEnv, "STM32CubeMX")
}

if runtime.GOOS == "darwin" {
if iocFile != "" {
cmd = exec.Command(pathJava, arg0, arg1, "-jar", pathCubeMx, iocFile)
} else if projectFile != "" {
cmd = exec.Command(pathJava, arg0, arg1, "-jar", pathCubeMx, "-s", projectFile)
} else {
cmd = exec.Command(pathJava, arg0, arg1, "-jar", pathCubeMx)
}
} else {
if iocFile != "" {
cmd = exec.Command(pathJava, "-jar", pathCubeMx, iocFile)
} else if projectFile != "" {
cmd = exec.Command(pathJava, "-jar", pathCubeMx, "-s", projectFile)
} else {
cmd = exec.Command(pathJava, "-jar", pathCubeMx)
}
}
log.Debugf("Start CubeMX as %v", cmd)
if err := cmd.Start(); err != nil {
log.Fatal(err)
return -1, err
Expand Down
Loading