Skip to content

Commit 031ad53

Browse files
authored
change the folder structure created by the tool (#16)
* change the folder structure created by the tool * apply suggestions from code review
1 parent 02afe46 commit 031ad53

File tree

3 files changed

+66
-48
lines changed

3 files changed

+66
-48
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
arduino-cslt
22
.vscode
3-
lib*
3+
sketch-dist
44
dist

README.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# arduino-cslt
22

3-
`arduino-cslt` is a convenient wrapper of [arduino-cli](https://github.com/arduino/arduino-cli), it compiles Arduino sketches outputting a precompiled library in the current working directory.
3+
`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.
44
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.
55

66
## Prequisites
@@ -10,7 +10,7 @@ Please use a version of the Arduino CLI that has [this](https://github.com/ardui
1010
Another requirement is [`gcc-ar`](https://sourceware.org/binutils/docs/binutils/ar.html) (installable with `apt-get install gcc`) in your `$PATH`.
1111

1212
## Build it
13-
In order to build `arduino-cslt` just use `go build`
13+
In order to build `arduino-cslt` just use `task go:build`
1414

1515
## Usage
1616
`./arduino-cslt compile -b <fqbn> <sketch_path>`
@@ -19,17 +19,17 @@ In order to build `arduino-cslt` just use `go build`
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
```
22-
libsketch/
23-
├── examples
24-
── sketch
25-
└── sketch.ino <-- the actual sketch we are going to compile with the arduino-cli later
26-
├── extras
27-
│ └── result.json
28-
├── library.properties
29-
└── src
30-
── cortex-m0plus
31-
└── libsketch.a
32-
└── libsketch.h
22+
sketch-dist/
23+
├── libsketch
24+
── extras
25+
└── result.json
26+
├── library.properties
27+
│ └── src
28+
├── cortex-m0plus
29+
│ │ └── libsketch.a
30+
── libsketch.h
31+
└── sketch
32+
└── sketch.ino <-- the actual sketch we are going to compile with the arduino-cli later
3333
```
3434

3535
This is an example execution:
@@ -45,15 +45,15 @@ INFO[0000] running: arduino-cli compile -b arduino:samd:mkrwifi1010 sketch/sketc
4545
INFO[0001] removed sketch/main.cpp
4646
INFO[0001] created sketch/sketch.ino
4747
INFO[0001] restored sketch/sketch.ino
48-
INFO[0001] created libsketch/library.properties
49-
INFO[0001] created libsketch/src/libsketch.h
50-
INFO[0001] created libsketch/examples/sketch/sketch.ino
51-
INFO[0001] running: gcc-ar rcs libsketch/src/cortex-m0plus/libsketch.a /tmp/arduino-sketch-E4D76B1781E9EB73A7B3491CAC68F374/sketch/sketch.ino.cpp.o
52-
INFO[0001] created libsketch/src/cortex-m0plus/libsketch.a
53-
INFO[0001] created libsketch/extras/result.json
48+
INFO[0001] created sketch-dist/libsketch/library.properties
49+
INFO[0001] created sketch-dist/libsketch/src/libsketch.h
50+
INFO[0001] created sketch-dist/sketch/sketch.ino
51+
INFO[0001] running: gcc-ar rcs sketch-dist/libsketch/src/cortex-m0plus/libsketch.a /tmp/arduino-sketch-E4D76B1781E9EB73A7B3491CAC68F374/sketch/sketch.ino.cpp.o
52+
INFO[0001] created sketch-dist/libsketch/src/cortex-m0plus/libsketch.a
53+
INFO[0001] created sketch-dist/libsketch/extras/result.json
5454
```
5555

56-
And the content of `libsketch/extras/result.json` is:
56+
And the content of `sketch-dist/libsketch/extras/result.json` is:
5757
```json
5858
{
5959
"coreInfo": {
@@ -80,24 +80,24 @@ And the content of `libsketch/extras/result.json` is:
8080
```
8181

8282
## 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 `<libsketch>/extras/result.json` file.
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.
8484

8585
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/).
8686

8787
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/).
8888

8989
After completing that operation you can compile it with:
9090

91-
`arduino-cli compile -b <fqbn> <libsketch>/examples/sketch/sketch.ino --library <libsketch>`.
91+
`arduino-cli compile -b <fqbn> sketch-dist/sketch/sketch.ino --library sketch-dist/<libsketch>`.
9292

9393
It's important to use the `--library` flag to include the precompiled library generated with arduino-cslt otherwise the Arduino CLI won't find it.
9494

9595
For example a legit execution looks like this:
9696
```
97-
$ arduino-cli compile -b arduino:samd:mkrwifi1010 libsketch/examples/sketch/sketch.ino --library libsketch/
97+
$ arduino-cli compile -b arduino:samd:mkrwifi1010 sketch-dist/sketch/sketch.ino --library sketch-dist/libsketch/
9898
9999
Library libsketch has been declared precompiled:
100-
Using precompiled library in libsketch/src/cortex-m0plus
100+
Using precompiled library in sketch-dist/libsketch/src/cortex-m0plus
101101
Sketch uses 14636 bytes (5%) of program storage space. Maximum is 262144 bytes.
102102
Global variables use 3224 bytes (9%) of dynamic memory, leaving 29544 bytes for local variables. Maximum is 32768 bytes.
103103
```

cmd/compile.go

+41-23
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,22 @@ type ResultJson struct {
5353
// compileCmd represents the compile command
5454
var compileCmd = &cobra.Command{
5555
Use: "compile",
56-
Short: "Compiles Arduino sketches.",
57-
Long: `Compiles Arduino sketches outputting an object file and a json file in a build directory
58-
The json contains information regarding libraries and core to use in order to build the sketch`,
59-
Example: os.Args[0] + `compile -b arduino:avr:uno /home/umberto/Arduino/Blink`,
56+
Short: "Compiles Arduino sketches producing a precompiled library.",
57+
Long: `Compiles Arduino sketches producing a precompiled library:
58+
sketch-dist/
59+
├── libsketch
60+
│ ├── extras
61+
│ │ └── result.json
62+
│ ├── library.properties
63+
│ └── src
64+
│ ├── cortex-m0plus
65+
│ │ └── libsketch.a
66+
│ └── libsketch.h
67+
└── 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`,
71+
Example: os.Args[0] + `compile -b arduino:samd:mkrwifi1010 sketch/sketch.ino`,
6072
Args: cobra.ExactArgs(1), // the path of the sketch to build
6173
Run: compileSketch,
6274
}
@@ -269,37 +281,43 @@ func createLib(sketchName string, buildMcu string, returnJson *ResultJson, objFi
269281
// this type of lib, as the type suggest, is already compiled so it only gets linked during the linking phase of a sketch
270282
// but we have to create a library folder structure in the current directory:
271283

272-
// libsketch/
273-
// ├── examples
274-
// │ ── sketch
275-
// │ └── sketch.ino <-- the actual sketch we are going to compile with the arduino-cli later
276-
// ├── extras
277-
// │ └── result.json
278-
// ├── library.properties
279-
// └── src
280-
// ── cortex-m0plus
281-
// └── libsketch.a
282-
// └── libsketch.h
284+
// sketch-dist/
285+
// ├── libsketch
286+
// │ ── extras
287+
// │ └── result.json
288+
// ├── library.properties
289+
// │ └── src
290+
// ├── cortex-m0plus
291+
// │ │ └── libsketch.a
292+
// ── libsketch.h
293+
// └── sketch
294+
// └── sketch.ino <-- the actual sketch we are going to compile with the arduino-cli later
283295

284296
// let's create the dir structure
285297
workingDir, err := paths.Getwd()
286298
if err != nil {
287299
logrus.Fatal(err)
288300
}
289-
libDir := workingDir.Join("lib" + sketchName)
290-
if libDir.Exist() { // if the dir already exixst we clean it before
291-
os.RemoveAll(libDir.String())
292-
logrus.Warnf("removed %s", libDir.String())
301+
rootDir := workingDir.Join("sketch-dist")
302+
if rootDir.Exist() { // if the dir already exixst we clean it before
303+
if err = rootDir.RemoveAll(); err != nil {
304+
logrus.Fatalf("cannot remove %s: %s", rootDir.String(), err)
305+
}
306+
logrus.Warnf("removed %s", rootDir.String())
307+
}
308+
if err = rootDir.Mkdir(); err != nil {
309+
logrus.Fatal(err)
293310
}
311+
libDir := rootDir.Join("lib" + sketchName)
294312
if err = libDir.Mkdir(); err != nil {
295313
logrus.Fatal(err)
296314
}
297315
srcDir := libDir.Join("src").Join(buildMcu)
298316
if err = srcDir.MkdirAll(); err != nil {
299317
logrus.Fatal(err)
300318
}
301-
exampleDir := libDir.Join("examples").Join(sketchName)
302-
if err = exampleDir.MkdirAll(); err != nil {
319+
sketchDir := rootDir.Join(sketchName)
320+
if err = sketchDir.MkdirAll(); err != nil {
303321
logrus.Fatal(err)
304322
}
305323
extraDir := libDir.Join("extras")
@@ -342,7 +360,7 @@ void _loop();`
342360
libsketchFilePath := srcDir.Parent().Join("lib" + sketchName + ".h")
343361
createFile(libsketchFilePath, libsketchHeader)
344362

345-
// create the sketch file in the example dir of the lib
363+
// create the sketch file in the sketch-dist dir
346364
// This one will include the libsketch.h and basically is the replacement of main.cpp
347365
// the sketch.ino contains the following:
348366
sketchFile := `#include <` + "lib" + sketchName + `.h>
@@ -352,7 +370,7 @@ void setup() {
352370
void loop() {
353371
_loop();
354372
}`
355-
sketchFilePath := exampleDir.Join(sketchName + ".ino")
373+
sketchFilePath := sketchDir.Join(sketchName + ".ino")
356374
createFile(sketchFilePath, sketchFile)
357375

358376
// 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 ⬆️)

0 commit comments

Comments
 (0)