Arduino-Makefile icon indicating copy to clipboard operation
Arduino-Makefile copied to clipboard

Ability to put src files in a subfolder

Open mgcrea opened this issue 9 years ago • 9 comments

I'd like to put all my src files (.ino, .h, etc.) in a src subfolder. Is there a way to achieve that?

mgcrea avatar Sep 17 '14 17:09 mgcrea

Can you show us a directory structure like the one you'd like to have?

ladislas avatar Sep 17 '14 17:09 ladislas

@ladislas

I'd love to have some PROJECT_SRC_DIR var that I could set like:

  • PROJECT_SRC_DIR = $(realpath $(PROJECT_DIR)/src)

Expected structure:

$ tree -L 2
.
├── Makefile
├── build
│   └── uno
├── lib
│   └── Firmata
├── make
│   ├── Arduino.mk
│   ├── CONTRIBUTING.md
│   ├── Common.mk
│   ├── HISTORY.md
│   ├── README.md
│   ├── ard-reset-arduino.1
│   ├── arduino-mk-vars.md
│   ├── bin
│   ├── chipKIT.mk
│   ├── examples
│   ├── licence.txt
│   ├── packaging
│   ├── script
│   └── tests
└── src
    ├── app.h
    └── app.ino

$ cat Makefile 
# Arduino Make file. Refer to https://github.com/sudar/Arduino-Makefile
# https://github.com/WeAreLeka/moti/blob/dev/Makefile-OSX.mk

PROJECT_DIR   = $(shell pwd)
ARDMK_DIR     = $(realpath $(PROJECT_DIR)/make)
ARDUINO_DIR   = /Applications/Arduino.app/Contents/Resources/Java
AVR_TOOLS_DIR = /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr
AVRDUDE_CONF  = /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/etc/avrdude.conf
USER_LIB_PATH = $(realpath $(PROJECT_DIR)/lib)
MONITOR_PORT  = /dev/cu.usbmodem*
BOARD_TAG     = uno

include $(ARDMK_DIR)/Arduino.mk

mgcrea avatar Sep 17 '14 19:09 mgcrea

this is kind of how 1.5 libraries work:

https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#source-code

which just got backported to 1.0.6:

https://github.com/arduino/Arduino/commit/567240236a614817f32a8dff8c5271f5d07a53f9

but its not for ino sketches, just libraries.

the tree structure above seems odd as you're mixing ino files with header files in the same directory, which should really be in an includes or lib directory.

sej7278 avatar Sep 18 '14 07:09 sej7278

@mgcrea If you put your Makefile into your src folder, it should work.

That's what I do here. (note: the Makefile is missing here because I cp it on each machine as I work on Linux as well as on OSX)

I have different directories into my src/ because I need to have different apps and behaviors. But if I put everything from moti/ to src/, it works as well.

Why would you like to keep your Makefile out of src/?

ladislas avatar Sep 18 '14 07:09 ladislas

@ladislas, I'm coming from a JS background where you usually have a Gruntfile.js or gulpfile.js build config on the root, and alway the source files in src or app. Feels clumsy to have to cd to a subdir to start a build on a project.

@sej7278 my app.h file is only used by the app.ino for forward declaration, would be strange to put it n a lib folder. To me it should live in the src folder, maybe in src/headers or src/includes. Would that be fine?

mgcrea avatar Sep 18 '14 08:09 mgcrea

@mgcrea I know what you mean. But as a little advice I'd say "don't try to c++ the way you js".

They are two different languages made for different things and the directory structure might change from what you're used to with js.

but if your want to do what you try to do, you can put a Makefile in src/ and one in your root and include the one from src/ into the one from source. so that when you call make from root, you'll build your project.

ladislas avatar Sep 18 '14 08:09 ladislas

Sorry for resurrecting this, but I too think this would be a super useful feature. I have my arduino code inside an src folder, but when working I always stay on my root to call git commands, and it's a bit of a pain to keep switching to the src dir to compile...

What I did as a hacky workaround is to have both my Makefile + this tiny shell script on my root:

#!/bin/bash

SRC_DIR=src

cp Makefile $SRC_DIR
cd $SRC_DIR
make $1
rm Makefile
cd ..

Ugly? very. But it does the trick...

marbru avatar Oct 04 '15 19:10 marbru

@marbru Why copy the makefile into src? Why not just load the makefile with make -f ../Makefile?

birgersp avatar Jan 26 '18 14:01 birgersp

I have to build a project with lots of subdirectories (see https://github.com/repetier/Repetier-Firmware ), so I replaced lines

LOCAL_C_SRCS    ?= $(wildcard *.c)
LOCAL_CPP_SRCS  ?= $(wildcard *.cpp)

with

LOCAL_C_SRCS    ?= $(shell find . -type f -name '*.c' | tr '\n' ' ')
LOCAL_CPP_SRCS  ?= $(shell find . -type f -name '*.cpp' | tr '\n' ' ')

This finds all .c and .cpp files recursively in all subdirectories. Obviously this will work only for unix-like OSes, but it works for me.

crystalline avatar Jun 16 '18 18:06 crystalline