Skip to content

Commit 3b232ad

Browse files
committed
add README.md generation in the sketch-dist folder
The generated README.md contains commands that helps on how to replicate the build environment
1 parent 031ad53 commit 3b232ad

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

README.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# arduino-cslt
22

33
`arduino-cslt` is a convenient wrapper of [arduino-cli](https://github.com/arduino/arduino-cli), it compiles Arduino sketches outputting a precompiled library under `sketch-dist/` folder created in the current working directory.
4-
It generates a json file in the `extras/` folder that contains information regarding libraries and core to use in order to build the sketch. The result is achieved by parsing the verbose output of `arduino-cli` and by using [GNU ar](https://sourceware.org/binutils/docs/binutils/ar.html) to generate an archive of the object files.
4+
It generates a README.md file that contains information regarding libraries and core to use in order to build the sketch. The result is achieved by parsing the verbose output of `arduino-cli` and by using [GNU ar](https://sourceware.org/binutils/docs/binutils/ar.html) to generate an archive of the object files.
55

66
## Prequisites
77
In order to run this tool you have to install first the [Arduino CLI](https://github.com/arduino/arduino-cli) and have `arduino-cli` binary in your `$PATH`, otherwise `arduino-cslt` won't work.
@@ -15,7 +15,7 @@ In order to build `arduino-cslt` just use `task go:build`
1515
## Usage
1616
`./arduino-cslt compile -b <fqbn> <sketch_path>`
1717

18-
[![asciicast](https://asciinema.org/a/463342.svg)](https://asciinema.org/a/463342)
18+
[![asciicast](https://asciinema.org/a/465059.svg)](https://asciinema.org/a/465059)
1919

2020
For example, running `./arduino-cslt compile -b arduino:samd:mkrwifi1010 sketch/sketch.ino` should produce a library with the following structure, in the current working directory:
2121
```
@@ -28,6 +28,7 @@ sketch-dist/
2828
│ ├── cortex-m0plus
2929
│ │ └── libsketch.a
3030
│ └── libsketch.h
31+
├── README.md <--contains information regarding libraries and core to install in order to reproduce the original build environment
3132
└── sketch
3233
└── sketch.ino <-- the actual sketch we are going to compile with the arduino-cli later
3334
```
@@ -48,11 +49,26 @@ INFO[0001] restored sketch/sketch.ino
4849
INFO[0001] created sketch-dist/libsketch/library.properties
4950
INFO[0001] created sketch-dist/libsketch/src/libsketch.h
5051
INFO[0001] created sketch-dist/sketch/sketch.ino
52+
INFO[0003] created sketch-dist/README.md
5153
INFO[0001] running: gcc-ar rcs sketch-dist/libsketch/src/cortex-m0plus/libsketch.a /tmp/arduino-sketch-E4D76B1781E9EB73A7B3491CAC68F374/sketch/sketch.ino.cpp.o
5254
INFO[0001] created sketch-dist/libsketch/src/cortex-m0plus/libsketch.a
5355
INFO[0001] created sketch-dist/libsketch/extras/result.json
5456
```
5557

58+
The content of `sketch-dist/README.md` included copy-pastable commands to reproduce the build environment:
59+
```markdown
60+
This package contains firmware code loaded in your product.
61+
The firmware contains additional code licensed with LGPL clause; in order to re-compile the entire firmware bundle, please execute the following.
62+
63+
## Install core and libraries
64+
`arduino-cli core install arduino:samd@1.8.12`
65+
`arduino-cli lib install WiFiNINA@1.8.13`
66+
`arduino-cli lib install SPI@1.0`
67+
68+
## Compile
69+
`arduino-cli compile -b arduino:samd:mkrwifi1010 sketch-dist/sketch/sketch.ino --library sketch-dist/libsketch`
70+
```
71+
5672
And the content of `sketch-dist/libsketch/extras/result.json` is:
5773
```json
5874
{
@@ -80,12 +96,12 @@ And the content of `sketch-dist/libsketch/extras/result.json` is:
8096
```
8197

8298
## How to compile the precompiled sketch
83-
In order to compile the sketch you have first to install manually the libraries and the core listed in the `sketch-dist/<libsketch>/extras/result.json` file.
84-
85-
You can install a library with [`arduino-cli lib install LIBRARY[@VERSION_NUMBER]`](https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_lib_install/).
99+
In order to compile the sketch you can follow the instructions listed in the `sketch-dist/README.md` file.
86100

87101
You can install a core with [`arduino-cli core install PACKAGER:ARCH[@VERSION]`](https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_core_install/).
88102

103+
You can install a library with [`arduino-cli lib install LIBRARY[@VERSION_NUMBER]`](https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_lib_install/).
104+
89105
After completing that operation you can compile it with:
90106

91107
`arduino-cli compile -b <fqbn> sketch-dist/sketch/sketch.ino --library sketch-dist/<libsketch>`.

cmd/compile.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ var compileCmd = &cobra.Command{
6464
│ ├── cortex-m0plus
6565
│ │ └── libsketch.a
6666
│ └── libsketch.h
67+
├── README.md <--contains information regarding libraries and core to install in order to reproduce the original build environment
6768
└── sketch
68-
└── sketch.ino <-- the actual sketch we are going to compile with the arduino-cli later
69-
70-
The result.json file contains information regarding libraries and core to use in order to reproduce the original build environment`,
69+
└── sketch.ino <-- the actual sketch we can recompile with the arduino-cli later`,
7170
Example: os.Args[0] + `compile -b arduino:samd:mkrwifi1010 sketch/sketch.ino`,
7271
Args: cobra.ExactArgs(1), // the path of the sketch to build
7372
Run: compileSketch,
@@ -141,7 +140,7 @@ func compileSketch(cmd *cobra.Command, args []string) {
141140

142141
sketchName := strings.TrimSuffix(inoPath.Base(), inoPath.Ext())
143142
// let's create the library corresponding to the precompiled sketch
144-
createLib(sketchName, buildMcu, returnJson, objFilePaths)
143+
createLib(sketchName, buildMcu, fqbn, returnJson, objFilePaths)
145144
}
146145

147146
// parseCliCompileOutput function takes cmdOutToParse as argument,
@@ -274,9 +273,10 @@ func patchSketch(inoPath *paths.Path) (oldSketchContent []byte) {
274273
// createLib function will take care of creating the library directory structure and files required, for the precompiled library to be recognized as such.
275274
// sketchName is the name of the sketch without the .ino extension. We use this for the name of the lib.
276275
// buildMcu is the name of the MCU of the board we have compiled for. The library specifications (https://arduino.github.io/arduino-cli/0.20/library-specification/#precompiled-binaries) requires that the precompiled archive is stored inside a folder with the name of the MCU used during the compile.
276+
// fqbn is required in order to generate the README.md file with instructions.
277277
// returnJson is the ResultJson object containing informations regarding core and libraries used during the compile process.
278278
// objFilePaths is a paths.PathList containing the paths.Paths to all the sketch related object files produced during the compile phase.
279-
func createLib(sketchName string, buildMcu string, returnJson *ResultJson, objFilePaths *paths.PathList) {
279+
func createLib(sketchName, buildMcu, fqbn string, returnJson *ResultJson, objFilePaths *paths.PathList) {
280280
// we are going to leverage the precompiled library infrastructure to make the linking work.
281281
// this type of lib, as the type suggest, is already compiled so it only gets linked during the linking phase of a sketch
282282
// but we have to create a library folder structure in the current directory:
@@ -290,6 +290,7 @@ func createLib(sketchName string, buildMcu string, returnJson *ResultJson, objFi
290290
// │ ├── cortex-m0plus
291291
// │ │ └── libsketch.a
292292
// │ └── libsketch.h
293+
// ├── README.md <--contains information regarding libraries and core to install in order to reproduce the original build environment
293294
// └── sketch
294295
// └── sketch.ino <-- the actual sketch we are going to compile with the arduino-cli later
295296

@@ -373,6 +374,30 @@ void loop() {
373374
sketchFilePath := sketchDir.Join(sketchName + ".ino")
374375
createFile(sketchFilePath, sketchFile)
375376

377+
// generate the commands to run to successfully reproduce the build environment, they will be used as content for the README.md
378+
var readmeContent []string
379+
readmeContent = append(readmeContent, "`arduino-cli core install "+returnJson.CoreInfo.Id+"@"+returnJson.CoreInfo.Version+"`")
380+
for _, readmeLib := range returnJson.LibsInfo {
381+
readmeContent = append(readmeContent, "`arduino-cli lib install "+readmeLib.Name+"@"+readmeLib.Version+"`")
382+
}
383+
// make the paths relative, absolute paths are too long and are different on the user machine
384+
sketchFileRelPath, _ := sketchFilePath.RelFrom(workingDir)
385+
libRelDir, _ := libDir.RelFrom(workingDir)
386+
readmeCompile := "`arduino-cli compile -b " + fqbn + " " + sketchFileRelPath.String() + " --library " + libRelDir.String() + "`"
387+
388+
//create the README.md file containig instructions regarding what commands to run in order to have again a working binary
389+
// the README.md contains the following:
390+
readmeMd := `This package contains firmware code loaded in your product.
391+
The firmware contains additional code licensed with LGPL clause; in order to re-compile the entire firmware bundle, please execute the following.
392+
393+
## Install core and libraries
394+
` + strings.Join(readmeContent, "\n") + "\n" + `
395+
## Compile
396+
` + readmeCompile + "\n"
397+
398+
readmeMdPath := rootDir.Join("README.md")
399+
createFile(readmeMdPath, readmeMd)
400+
376401
// run gcc-ar to create an archive containing all the object files except the main.cpp.o (we don't need it because we have created a substitute of it before ⬆️)
377402
// we exclude the main.cpp.o because we are going to link the archive libsketch.a against sketchName.ino
378403
objFilePaths.FilterOutPrefix("main.cpp")

0 commit comments

Comments
 (0)