|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +const arduinoCLI = "arduino-cli" |
| 15 | + |
| 16 | +func generateCpp(inoCode []byte, name, fqbn string) (cppPath string, cppCode []byte, err error) { |
| 17 | + tempDir, err := ioutil.TempDir("", "ino2cpp-") |
| 18 | + if err != nil { |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + // Write source file to temp dir |
| 23 | + if !strings.HasSuffix(name, ".ino") { |
| 24 | + name += ".ino" |
| 25 | + } |
| 26 | + inoPath := filepath.Join(tempDir, name) |
| 27 | + err = ioutil.WriteFile(inoPath, inoCode, 0600) |
| 28 | + if err != nil { |
| 29 | + return |
| 30 | + } |
| 31 | + if enableLogging { |
| 32 | + log.Println("Source file written to", inoPath) |
| 33 | + } |
| 34 | + |
| 35 | + // Generate compile_flags.txt |
| 36 | + flagsPath, err := generateCompileFlags(tempDir, inoPath, fqbn) |
| 37 | + if err != nil { |
| 38 | + return |
| 39 | + } |
| 40 | + if enableLogging { |
| 41 | + log.Println("Compile flags written to", flagsPath) |
| 42 | + } |
| 43 | + |
| 44 | + // Generate target file |
| 45 | + preprocessCmd := exec.Command(arduinoCLI, "compile", "--fqbn", fqbn, "--preprocess", inoPath) |
| 46 | + cppCode, err = preprocessCmd.Output() |
| 47 | + if err != nil { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Write target file to temp dir |
| 52 | + cppPath = filepath.Join(tempDir, name+".cpp") |
| 53 | + err = ioutil.WriteFile(cppPath, cppCode, 0600) |
| 54 | + if err == nil && enableLogging { |
| 55 | + log.Println("Target file written to", cppPath) |
| 56 | + } |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | +func updateCpp(inoCode []byte, fqbn, cppPath string) (cppCode []byte, err error) { |
| 61 | + // Write source file to temp dir |
| 62 | + inoPath := strings.TrimSuffix(cppPath, ".cpp") |
| 63 | + err = ioutil.WriteFile(inoPath, inoCode, 0600) |
| 64 | + if err != nil { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + // Generate target file |
| 69 | + preprocessCmd := exec.Command(arduinoCLI, "compile", "--fqbn", fqbn, "--preprocess", inoPath) |
| 70 | + cppCode, err = preprocessCmd.Output() |
| 71 | + if err != nil { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + // Write target file to temp dir |
| 76 | + err = ioutil.WriteFile(cppPath, cppCode, 0600) |
| 77 | + return |
| 78 | +} |
| 79 | + |
| 80 | +func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) { |
| 81 | + propertiesCmd := exec.Command(arduinoCLI, "compile", "--fqbn", fqbn, "--show-properties", inoPath) |
| 82 | + output, err := propertiesCmd.Output() |
| 83 | + if err != nil { |
| 84 | + return "", err |
| 85 | + } |
| 86 | + properties, err := readProperties(bytes.NewReader(output)) |
| 87 | + if err != nil { |
| 88 | + return "", err |
| 89 | + } |
| 90 | + flagsPath := filepath.Join(tempDir, "compile_flags.txt") |
| 91 | + outFile, err := os.OpenFile(flagsPath, os.O_WRONLY|os.O_CREATE, 0600) |
| 92 | + if err != nil { |
| 93 | + return flagsPath, err |
| 94 | + } |
| 95 | + defer outFile.Close() |
| 96 | + writer := bufio.NewWriter(outFile) |
| 97 | + |
| 98 | + // TODO support other architectures |
| 99 | + writer.WriteString("--target=avr\n") |
| 100 | + cppFlags := expandProperty(properties, "compiler.cpp.flags") |
| 101 | + writer.WriteString(strings.ReplaceAll(cppFlags, " ", "\n") + "\n") |
| 102 | + mcu := expandProperty(properties, "build.mcu") |
| 103 | + writer.WriteString("-mmcu=" + mcu + "\n") |
| 104 | + fcpu := expandProperty(properties, "build.f_cpu") |
| 105 | + writer.WriteString("-DF_CPU=" + fcpu + "\n") |
| 106 | + ideVersion := expandProperty(properties, "runtime.ide.version") |
| 107 | + writer.WriteString("-DARDUINO=" + ideVersion + "\n") |
| 108 | + board := expandProperty(properties, "build.board") |
| 109 | + writer.WriteString("-DARDUINO_" + board + "\n") |
| 110 | + arch := expandProperty(properties, "build.arch") |
| 111 | + writer.WriteString("-DARDUINO_ARCH_" + arch + "\n") |
| 112 | + corePath := expandProperty(properties, "build.core.path") |
| 113 | + writer.WriteString("-I" + corePath + "\n") |
| 114 | + variantPath := expandProperty(properties, "build.variant.path") |
| 115 | + writer.WriteString("-I" + variantPath + "\n") |
| 116 | + avrgccPath := expandProperty(properties, "runtime.tools.avr-gcc.path") |
| 117 | + writer.WriteString("-I" + filepath.Join(avrgccPath, "avr", "include") + "\n") |
| 118 | + |
| 119 | + writer.Flush() |
| 120 | + return flagsPath, nil |
| 121 | +} |
0 commit comments