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

Use recipe.hooks feature of the platform.txt file #41

Closed
ghost opened this issue Jun 27, 2017 · 3 comments
Closed

Use recipe.hooks feature of the platform.txt file #41

ghost opened this issue Jun 27, 2017 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@ghost
Copy link

ghost commented Jun 27, 2017

Orignally posted in #38 by @RickKimball

I think you could take advantage of the recipe.hooks feature of the platform.txt file to conditionally override defines and gcc command line arguments on a per sketch basis. This seems to be a fairly new feature (since Arduino 1.6.something) something they added since I last looked to accomplish something like this.

You might approach it like this:

In the stm32 tools directory create a new tool called 'cpif' which copies a file called gcc_options.txt into the build directory if the file exists in the sketch directory or copies /dev/null if it doesn't

$ cat cpif 
#!/bin/bash
set -x
echo $0 $*

sketchdir=${1%.*}
dest=${2}/gcc_options.txt

options="$HOME/Arduino/$sketchdir/gcc_options.txt"

if [ -f $options ]
then
   cp $options $dest
else
   cp /dev/null $dest
fi

In platform.txt you add a recipe hook that is called before starting the build process.

 ## Compile c files
+recipe.hooks.prebuild.1.pattern=cpif {build.project_name} {build.path} b4all
 recipe.c.o.pattern="{compiler.path}{compiler.c.cmd}" {compiler.c.flags} -mcpu={build.mcu} -DF_CPU={build ...

modify the board.txt to use the @file feature of gcc or just add it to the platform.txt for all boards

Nucleo_64.menu.Nucleo_64_board.NUCLEO_F030R8.build.extra_flags=-DSTM32F030x8 @{build.path}/gcc_options.txt

When the user wants to override defines and gcc command line arguments they create a file called gcc_options.txt in the sketch directory. Here I imagine I want to override the F_CPU value

-U F_CPU
-D F_CPU=24000000UL

When I compile it uses the sketch specific defines and overrides.

/home/kimballr/github/Arduino/build/linux/work/arduino-builder -dump-prefs -logger=machine -hardware /home/kimballr/github/Arduino/build/linux/work/hardware -hardware /home/kimballr/.arduino15/packages -hardware /home/kimballr/Arduino/hardware -tools /home/kimballr/github/Arduino/build/linux/work/tools-builder -tools /home/kimballr/github/Arduino/build/linux/work/hardware/tools/avr -tools /home/kimballr/.arduino15/packages -built-in-libraries /home/kimballr/github/Arduino/build/linux/work/libraries -libraries /home/kimballr/Arduino/libraries -fqbn=STM32:stm32:Nucleo_64:Nucleo_64_board=NUCLEO_F030R8,upload_method=MassStorageMethod -ide-version=10803 -build-path /tmp/arduino_build_433355 -warnings=more -build-cache /tmp/arduino_cache_805852 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.CMSIS.path=/home/kimballr/.arduino15/packages/arduino/tools/CMSIS/4.5.0 -prefs=runtime.tools.arm-none-eabi-gcc.path=/home/kimballr/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/6-2017-q1-update -prefs=runtime.tools.STM32Tools.path=/home/kimballr/.arduino15/packages/STM32/tools/STM32Tools/2017.5.12 -verbose /home/kimballr/Arduino/BlinkWithoutDelay_HSE_Bypassx/BlinkWithoutDelay_HSE_Bypassx.ino
/home/kimballr/github/Arduino/build/linux/work/arduino-builder -compile -logger=machine -hardware /home/kimballr/github/Arduino/build/linux/work/hardware -hardware /home/kimballr/.arduino15/packages -hardware /home/kimballr/Arduino/hardware -tools /home/kimballr/github/Arduino/build/linux/work/tools-builder -tools /home/kimballr/github/Arduino/build/linux/work/hardware/tools/avr -tools /home/kimballr/.arduino15/packages -built-in-libraries /home/kimballr/github/Arduino/build/linux/work/libraries -libraries /home/kimballr/Arduino/libraries -fqbn=STM32:stm32:Nucleo_64:Nucleo_64_board=NUCLEO_F030R8,upload_method=MassStorageMethod -ide-version=10803 -build-path /tmp/arduino_build_433355 -warnings=more -build-cache /tmp/arduino_cache_805852 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.CMSIS.path=/home/kimballr/.arduino15/packages/arduino/tools/CMSIS/4.5.0 -prefs=runtime.tools.arm-none-eabi-gcc.path=/home/kimballr/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/6-2017-q1-update -prefs=runtime.tools.STM32Tools.path=/home/kimballr/.arduino15/packages/STM32/tools/STM32Tools/2017.5.12 -verbose /home/kimballr/Arduino/BlinkWithoutDelay_HSE_Bypassx/BlinkWithoutDelay_HSE_Bypassx.ino
Using board 'Nucleo_64' from platform in folder: /home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2
Using core 'arduino' from platform in folder: /home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2
cpif BlinkWithoutDelay_HSE_Bypassx.ino /tmp/arduino_build_433355 b4all+ echo /home/kimballr/opt64/bin/cpif BlinkWithoutDelay_HSE_Bypassx.ino /tmp/arduino_build_433355 b4all

+ sketchdir=BlinkWithoutDelay_HSE_Bypassx
/home/kimballr/opt64/bin/cpif BlinkWithoutDelay_HSE_Bypassx.ino /tmp/arduino_build_433355 b4all
+ dest=/tmp/arduino_build_433355/gcc_options.txt
+ options=/home/kimballr/Arduino/BlinkWithoutDelay_HSE_Bypassx/gcc_options.txtDetecting libraries used...

+ '[' -f /home/kimballr/Arduino/BlinkWithoutDelay_HSE_Bypassx/gcc_options.txt ']'
+ cp /home/kimballr/Arduino/BlinkWithoutDelay_HSE_Bypassx/gcc_options.txt /tmp/arduino_build_433355/gcc_options.txt
"/home/kimballr/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/6-2017-q1-update/bin/arm-none-eabi-g++" -mthumb -c -O2 -g -w -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf  "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino/avr" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino/stm32" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/STM32F0xx_HAL_Driver/Inc/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/STM32F0xx_HAL_Driver/Src/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/STM32F0xx/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/variants/NUCLEO_F030R8/usb" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src" -w -x c++ -E -CC -mcpu=cortex-m0 -DF_CPU=48000000L -DSTM32F0xx -DARDUINO=10803 -DARDUINO_NUCLEO_F030R8 -DARDUINO_ARCH_STM32  -DSTM32F030x8 @/tmp/arduino_build_433355/gcc_options.txt "-I/home/kimballr/.arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/CMSIS/Device/ST/STM32F0xx/Include/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/CMSIS/Device/ST/STM32F0xx/Source/Templates/gcc/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/variants/NUCLEO_F030R8" "/tmp/arduino_build_433355/sketch/BlinkWithoutDelay_HSE_Bypassx.ino.cpp" -o "/dev/null"
Using cached library dependencies for file: /tmp/arduino_build_433355/sketch/customclock.c
Generating function prototypes...
"/home/kimballr/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/6-2017-q1-update/bin/arm-none-eabi-g++" -mthumb -c -O2 -g -w -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf  "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino/avr" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino/stm32" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/STM32F0xx_HAL_Driver/Inc/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/STM32F0xx_HAL_Driver/Src/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/STM32F0xx/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/variants/NUCLEO_F030R8/usb" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src" -w -x c++ -E -CC -mcpu=cortex-m0 -DF_CPU=48000000L -DSTM32F0xx -DARDUINO=10803 -DARDUINO_NUCLEO_F030R8 -DARDUINO_ARCH_STM32  -DSTM32F030x8 @/tmp/arduino_build_433355/gcc_options.txt "-I/home/kimballr/.arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/CMSIS/Device/ST/STM32F0xx/Include/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/CMSIS/Device/ST/STM32F0xx/Source/Templates/gcc/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/variants/NUCLEO_F030R8" "/tmp/arduino_build_433355/sketch/BlinkWithoutDelay_HSE_Bypassx.ino.cpp" -o "/tmp/arduino_build_433355/preproc/ctags_target_for_gcc_minus_e.cpp"
"/home/kimballr/github/Arduino/build/linux/work/tools-builder/ctags/5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "/tmp/arduino_build_433355/preproc/ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
Using previously compiled file: /tmp/arduino_build_433355/sketch/customclock.c.o
"/home/kimballr/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/6-2017-q1-update/bin/arm-none-eabi-g++" -mthumb -c -O2 -g -Wall -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -MMD "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino/avr" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino/stm32" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/STM32F0xx_HAL_Driver/Inc/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/STM32F0xx_HAL_Driver/Src/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/STM32F0xx/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/variants/NUCLEO_F030R8/usb" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src" -mcpu=cortex-m0 -DF_CPU=48000000L -DSTM32F0xx -DARDUINO=10803 -DARDUINO_NUCLEO_F030R8 -DARDUINO_ARCH_STM32  -DSTM32F030x8 @/tmp/arduino_build_433355/gcc_options.txt "-I/home/kimballr/.arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/CMSIS/Device/ST/STM32F0xx/Include/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/system/Drivers/CMSIS/Device/ST/STM32F0xx/Source/Templates/gcc/" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/cores/arduino" "-I/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/variants/NUCLEO_F030R8" "/tmp/arduino_build_433355/sketch/BlinkWithoutDelay_HSE_Bypassx.ino.cpp" -o "/tmp/arduino_build_433355/sketch/BlinkWithoutDelay_HSE_Bypassx.ino.cpp.o"
/home/kimballr/Arduino/BlinkWithoutDelay_HSE_Bypassx/BlinkWithoutDelay_HSE_Bypassx.ino:30:0: warning: "F_CPU" redefined
  #define F_CPU 48000000UL
 
<command-line>:0:0: note: this is the location of the previous definition
Compiling libraries...
Compiling core...
Using previously compiled file: /tmp/arduino_build_433355/core/PeripheralPins.c.o
Using previously compiled file: /tmp/arduino_build_433355/core/variant.cpp.o
Using precompiled core
Linking everything together...
"/home/kimballr/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/6-2017-q1-update/bin/arm-none-eabi-gcc" -mthumb -O2 -g -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--entry=Reset_Handler -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -mcpu=cortex-m0 -DF_CPU=48000000L "-T/home/kimballr/.arduino15/packages/STM32/hardware/stm32/2017.6.2/variants/NUCLEO_F030R8/ldscript.ld" "-Wl,-Map,/tmp/arduino_build_433355/BlinkWithoutDelay_HSE_Bypassx.ino.map"   -o "/tmp/arduino_build_433355/BlinkWithoutDelay_HSE_Bypassx.ino.elf" "-L/tmp/arduino_build_433355" -Wl,--start-group "/tmp/arduino_build_433355/sketch/customclock.c.o" "/tmp/arduino_build_433355/sketch/BlinkWithoutDelay_HSE_Bypassx.ino.cpp.o" "/tmp/arduino_build_433355/core/PeripheralPins.c.o" "/tmp/arduino_build_433355/core/variant.cpp.o"  "-L/home/kimballr/.arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Lib/GCC/" -larm_cortexM0l_math -Wl,--whole-archive "/tmp/arduino_build_433355/../arduino_cache_805852/core/core_STM32_stm32_Nucleo_64_Nucleo_64_board_NUCLEO_F030R8,upload_method_MassStorageMethod_606682308e20143982ec828e068e3363.a" -Wl,--no-whole-archive -lc -Wl,--end-group -lm -lgcc --specs=nano.specs
"/home/kimballr/.arduino15/packages/STM32/tools/arm-none-eabi-gcc/6-2017-q1-update/bin/arm-none-eabi-objcopy" -O binary  "/tmp/arduino_build_433355/BlinkWithoutDelay_HSE_Bypassx.ino.elf" "/tmp/arduino_build_433355/BlinkWithoutDelay_HSE_Bypassx.ino.bin"
Sketch uses 7564 bytes (11%) of program storage space. Maximum is 65536 bytes.
Global variables use 4064 bytes (49%) of dynamic memory, leaving 4128 bytes for local variables. Maximum is 8192 bytes.

Of course you would have to make this work with the different OS types. You would also have to deal with finding the users preference sketch home directory. I just hard coded it in the example above to use my $HOME/Arduino directory.

Seems doable (I actually have this working ) if you care to open yourself to all kinds of user problems when they do bad things : )

@ghost ghost added the enhancement New feature or request label Jun 27, 2017
@ghost ghost self-assigned this Jun 27, 2017
@fpistm
Copy link
Member

fpistm commented Jun 27, 2017

Thanks @fprwi6labs.
You're right it is common for all libs and core (ex: enable USB)
https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification#pre-and-post-build-hooks-since-ide-165
Seems to be promising as we could operate at several level (core, lib, sketch...)
Thanks @RickKimball for the tips

@fpistm fpistm assigned fpistm and unassigned ghost Nov 30, 2017
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Dec 10, 2017
Fix stm32duino#41

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
fpistm added a commit to fpistm/Arduino_Tools that referenced this issue Dec 10, 2017
stm32duino/Arduino_Core_STM32#41

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Dec 11, 2017
It is now possible to customize some core definitions
or gcc options thanks a file named "build_opt.h"
in the sketch directory. (using @file gcc option)

Example of file content to change F_CPU value:
-UF_CPU -DF_CPU=72000000UL

If the file does not exist an empty one is created.

Fix stm32duino#41

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
@fpistm fpistm mentioned this issue Dec 11, 2017
@fpistm
Copy link
Member

fpistm commented Dec 11, 2017

Hi @RickKimball
I've (finally) make a generic implementation based on your request.
Let me know your feedback.

@RickKimball
Copy link
Contributor

I just saw your change. Excellent thanks! I'll give it a spin.. Your idea to use a .h file is a great idea.

fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Dec 12, 2017
It is now possible to customize some core definitions
or gcc options thanks a file named "build_opt.h"
in the sketch directory. (using @file gcc option)

Example of file content to change F_CPU value:
-UF_CPU -DF_CPU=72000000UL

If the file does not exist an empty one is created.

Fix stm32duino#41

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Dec 14, 2017
It is now possible to customize some core definitions
or gcc options thanks a file named "build_opt.h"
in the sketch directory. (using @file gcc option)

Example of file content to change F_CPU value:
-UF_CPU -DF_CPU=72000000UL

If the file does not exist an empty one is created.

Fix stm32duino#41

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
benwaffle pushed a commit to benwaffle/Arduino_Core_STM32 that referenced this issue Apr 10, 2019
It is now possible to customize some core definitions
or gcc options thanks a file named "build_opt.h"
in the sketch directory. (using @file gcc option)

Example of file content to change F_CPU value:
-UF_CPU -DF_CPU=72000000UL

If the file does not exist an empty one is created.

Fix stm32duino#41

Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants