|
| 1 | +# IDE integration |
| 2 | + |
| 3 | +The IDE uses arduino-builder as a build tool. This tool is tightly build |
| 4 | +around the gcc. It always assumes that the main sketch is a cpp file and it |
| 5 | +includes hardcoded assumptions about the file suffixes '.o' for object files |
| 6 | +and '.a' for libraries. |
| 7 | + |
| 8 | +As SDCC is not flexible about file suffix' as well we need some wrapper |
| 9 | +scripts to convert the different suffixes before and after the compiler run: |
| 10 | + |
| 11 | +file type |gcc file suffix |SDCC file suffix |
| 12 | +----------- |------------- |----------- |
| 13 | +source file |any |.c |
| 14 | +object file |.o |.rel |
| 15 | +library archive |.a |.lib |
| 16 | +dependency file |.d |.d |
| 17 | + |
| 18 | + |
| 19 | +## Linux and MacOS |
| 20 | + |
| 21 | +I like minimal approaches and usually prefer using dash over bash. Less |
| 22 | +features => less problems. You know. |
| 23 | + |
| 24 | +The linker wrapper sdcc-link.sh is tricky, as we need to parse all |
| 25 | +arguments, modify them if an object file or a library is found and call sdcc |
| 26 | +with the full list. The required array handling is not implemented in dash, |
| 27 | +so we are stuck with the full bash. |
| 28 | + |
| 29 | + |
| 30 | +## bash on Windows |
| 31 | + |
| 32 | +Using the bash.exe from the MinGW project we can use the same script files |
| 33 | +even for Windows. |
| 34 | + |
| 35 | + |
| 36 | +### Figuring out the path |
| 37 | + |
| 38 | +The hard part is locating the executeables without requiring the user to |
| 39 | +modify the PATH variable. Relative to the location of the wrapper scripts |
| 40 | +the executeables for `cp` and `rm` are in `../win`. But a simple simple `cd |
| 41 | +../win` or `../win/cp` does't work, as the current working directory at this |
| 42 | +point is still the Arduino binary directory. |
| 43 | + |
| 44 | +Since the script is called with a full absolute path, we can extract the |
| 45 | +needed path from there. This is the obvious solution: |
| 46 | + |
| 47 | + PATH="${0%/wrapper/*}"/win:$PATH |
| 48 | + |
| 49 | +It looks ok, but it doesn't work on some Windows systems. It will result |
| 50 | +in an absolute path like |
| 51 | +`C:\Users\michael\AppData\Local\Arduino15\packages\sduino\tools\STM8Tools/win`. |
| 52 | +On some systems this works, on some it doesn't. Surprisingly, the mingw |
| 53 | +system somehow decides to set the `tools/STM8Tools` directory as the root |
| 54 | +directory. On some systems absolute paths above that point are ok, on some |
| 55 | +systems they are not. No idea why. And no idea where this root base is |
| 56 | +defined (or how to influence it). How can the bash.exe know that it was |
| 57 | +extracted from a tar file below the `tools/STM8Tools` directory? |
| 58 | + |
| 59 | +So we need a plan B. |
| 60 | + |
| 61 | +This is technically wrong, but surprisingly it works with Windows: |
| 62 | + |
| 63 | + cd $0/../.. |
| 64 | + PATH=$(pwd)/win:$PATH |
| 65 | + |
| 66 | +The `pwd` has the positive side effect of converting the path from Windows |
| 67 | +to Unix syntax avoiding all these backslash issues. |
| 68 | + |
| 69 | +Finally, this is syntactally correct and works on all systems: |
| 70 | + |
| 71 | + cd "${0%/wrapper/*}" |
| 72 | + PATH=$(pwd)/win:$PATH |
| 73 | + |
| 74 | +All together it leds to this solution: |
| 75 | + |
| 76 | +```bash |
| 77 | +# check if cp is in the path |
| 78 | +if ! command -v cp > /dev/null; then |
| 79 | + cd "${0%/wrapper/*}" |
| 80 | + PATH=$(pwd)/win:$PATH |
| 81 | +fi |
| 82 | +``` |
| 83 | + |
| 84 | +Now we are ready to go even on Windows. All used functions `command`, `cd` |
| 85 | +and `pwd` and the pattern matching are POSIX-conformant builtin shell |
| 86 | +functions without any external dependencies resulting in almost no overhead |
| 87 | +and high portability. |
| 88 | + |
| 89 | + |
| 90 | +### Using only cmd.exe |
| 91 | + |
| 92 | +It might be possible to get away with straight cmd.exe batch programming. |
| 93 | +But my ambitions of getting into Windows programming are very limited, so |
| 94 | +maybe somebody else would like to investigate this. |
0 commit comments