gpt4 book ai didi

c - 具有依赖关系的类似 Arduino 的 Makefile ......?

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:21 25 4
gpt4 key购买 nike

我目前正在尝试为 STM8 microcontroller 开发 C 库和项目模板使用 SDCC OS compiler .我的目标是类似于 Arduino 的(几乎)与 NOOB 兼容的设置 - 但使用 make+shellscripts 而不是 IDE(我的野心是有限的......)

目前我正在努力使用 make 来自动检测依赖项。在 Arduino 中,用户只包含相关的标题,例如“#include LCD-lib”,构建机制会自动检测依赖关系并链接相应的库。无需手动将其添加到 IDE 或 Makefile。

我喜欢它的简单性,但到目前为止,我在创建相应的 Makefile 时惨遭失败。基本上 Makefile 应该实现以下目标:

  1. 扫描项目根目录中的 *.c 文件以查找包含的 header 。请注意,这些文件位于不同的 lib 文件夹中
  2. 将所有包含的头文件和(如果存在)相应的 C 文件添加到构建过程中
  3. 为了最大限度地减少编译时间和大小,在构建过程中必须跳过 lib 文件夹中未使用的 C 文件

我相信 make 可以完成上述所有工作 - 但还达不到我对 make 的经验水平... :-(

这是我想到的文件夹结构:

├── Library
│   ├── Base
│   │   ├── general STM8 sources and headers
│   ├── STM8S_Discovery
│   │   └── board specific sources and headers
│   └── User
│      └── optional user library sources and headers
├── Projects
│   ├── Examples (to be filled)
│   │   └── Basic_Project
│   │   ├── compile_upload.sh --> double-click to build and upload
│   │   ├── config.h
│   │   ├── main.c
│   │   └── Makefile --> should detect dependencies in ./*.c and ./*.h
│   └── User_Projects (still empty)
└── Tools
├── programmer.py --> for programming (already works from make)
└── terminal.py --> for serial terminal (already works from make)

我知道要问的问题很多,但方便的 Makefile 是我的主要障碍。非常感谢任何帮助!!!非常感谢!

问候,Georg Icking-Konert

最佳答案

注意:我意识到这个答案并不能满足您的所有要求,事实上这种方法仍然需要您列出您在项目中使用的相关 Arduino 库的名称,以及一个列表应该包含在项目中的目录的路径。但是,这个解决方案是我能想到的最接近您要求的解决方案,它可能仍会帮助其他人阅读这个问题。


我为此使用 Arduino Makefile:

  1. Makefile.master 放入您的主工作区目录
  2. 当你开始一个新的 Arduino 项目时,你将它创建为你工作区中的一个子目录
    • .pde/.ino 创建一个文件包含 setup() 的扩展名和 `loop() 方法
    • 将剩下的逻辑放入.c/.cpp/.h/.hpp文件
  3. 添加一个项目 Makefile 在此子目录中设置项目细化设置,例如:

    # Your Arduino environment.
    ARD_HOME = /usr/share/arduino
    ARD_BIN = $(ARD_HOME)/hardware/tools/avr/bin

    # Monitor Baudrate
    MON_SPEED = 4800

    # Board settings.
    BOARD = uno
    PORT = /dev/ttyACM0
    PROGRAMMER = stk500v2

    # Where to find header files and libraries.
    INC_DIRS =
    MY_LIB_DIRS =
    LIBS =
    LIB_DIRS = $(addprefix $(ARD_HOME)/libraries/, $(LIBS)) $(MY_LIB_DIRS)

    include ../Makefile.master
  4. 使用make all编译并运行, make upload , make monitor

确保您在 Unix/Linux 机器(或同等机器)上安装了 picocom 作为控制台串行监视器。在 MAC-OS 上,您可以通过设置 MON_CMD 来使用屏幕相应地变化。


Makefile.master:

原始Makefile.masterAlan Burlison 编写并由 Matthieu Weber 修改, 并且可以找到 here .

我做了一些更改以使其适合我的配置,特别是我添加了以下代码行:

### DEBUG Compilation ###
ifeq ($(DEBUG), 1)
ARD_FLAGS += -DDEBUG_PROJ
C_FLAGS += -g
CXX_FLAGS += -g
else
ARD_FLAGS += -DNDEBUG_PROJ
endif

随后删除了 -g默认选项 C/CXX _FLAGS Makefile.master 中的条目。这样,符号信息 就不会添加到release 代码中,只有在使用DEBUG=1 编译代码时才会添加。

屏蔽的代码
#ifdef DEBUG_PROJ
/* debug code here */
#endif
// or
#ifndef NDEBUG_PROJ
/* debug code here */
#endif

找到进入二进制文件的方式,从而生成更小的release 可执行文件。

在这里你可以找到我自己的版本的Makefile.master:

#
# Copyright 2011 Alan Burlison, alan@bleaklow.com. All rights reserved.
# Subsequently modified by Matthieu Weber, matthieu.weber@jyu.fi.
# Subsequently modified by Patrick Trentin, patrick.trentin.88@gmail.com
# Use is subject to license terms.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY ALAN BURLISON "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL ALAN BURLISON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Makefile for building Arduino projects outside of the Arduino environment
#
# This makefile should be included into a per-project Makefile of the following
# form:
#
# ----------
# BOARD = mega
# PORT = /dev/term/0
# INC_DIRS = ../common
# LIB_DIRS = ../libraries/Task ../../libraries/VirtualWire
# include ../../Makefile.master
# ----------
#
# Where:
# BOARD : Arduino board type, from $(ARD_HOME)/hardware/boards.txt
# PORT : USB port
# INC_DIRS : List pf directories containing header files
# LIB_DIRS : List of directories containing library source
#
# Before using this Makefile you can adjust the following macros to suit
# your environment, either by editing this file directly or by defining them in
# the Makefile that includes this one, in which case they will override the
# definitions below:
# ARD_REV : arduino software revision, e.g. 0017, 0018
# ARD_HOME : installation directory of the Arduino software.
# ARD_BIN : location of compiler binaries
# AVRDUDE : location of avrdude executable
# AVRDUDE_CONF : location of avrdude configuration file
# PROGRAMMER : avrdude programmer type
# MON_TERM : terminal command for serial monitor
# MON_CMD : serial monitor command
# MON_SPEED : serial monitor speed
#

# Global configuration.
ARD_REV ?= 100
ARD_HOME ?= /usr/local/arduino
ARD_BIN ?= /usr/bin
AVRDUDE ?= $(ARD_HOME)/hardware/tools/avrdude
AVRDUDE_CONF ?= $(ARD_HOME)/hardware/tools/avrdude.conf
MON_TERM ?= xterm
MON_SPEED ?= 57600
MON_CMD ?= picocom
PORT ?= $(HOME)/dev/arduino
BOARD ?= atmega328

### Nothing below here should require editing. ###

# Check for the required definitions.

ifndef BOARD
$(error $$(BOARD) not defined)
endif
ifndef PORT
$(error $$(PORT) not defined)
endif

# Version-specific settings
ARD_BOARDS = $(ARD_HOME)/hardware/arduino/boards.txt
ARD_SRC_DIR = $(ARD_HOME)/hardware/arduino/cores/arduino
ARD_MAIN = $(ARD_SRC_DIR)/main.cpp

# Standard macros.
SKETCH = $(notdir $(CURDIR))
BUILD_DIR = build
VPATH = $(LIB_DIRS)

# Macros derived from boards.txt
MCU := $(shell sed -n 's/$(BOARD)\.build\.mcu=\(.*\)/\1/p' < $(ARD_BOARDS))
F_CPU := $(shell sed -n 's/$(BOARD)\.build\.f_cpu=\(.*\)/\1/p' < $(ARD_BOARDS))
UPLOAD_SPEED := \
$(shell sed -n 's/$(BOARD)\.upload\.speed=\(.*\)/\1/p' < $(ARD_BOARDS))
PROGRAMMER := \
$(shell sed -n 's/$(BOARD)\.upload\.protocol=\(.*\)/\1/p' < $(ARD_BOARDS))
ARD_VAR := \
$(shell sed -n 's/$(BOARD)\.build\.variant=\(.*\)/\1/p' < $(ARD_BOARDS))

# More Version-specific settings
ARD_VAR_DIR = $(ARD_HOME)/hardware/arduino/variants/$(ARD_VAR)

# Build tools.
CC = $(ARD_BIN)/avr-gcc
CXX = $(ARD_BIN)/avr-g++
CXXFILT = $(ARD_BIN)/avr-c++filt
OBJCOPY = $(ARD_BIN)/avr-objcopy
OBJDUMP = $(ARD_BIN)/avr-objdump
AR = $(ARD_BIN)/avr-ar
SIZE = $(ARD_BIN)/avr-size
NM = $(ARD_BIN)/avr-nm
MKDIR = mkdir -p
RM = rm -rf
MV = mv -f
LN = ln -f

# Compiler flags.
INC_FLAGS = \
$(addprefix -I,$(INC_DIRS)) $(addprefix -I,$(LIB_DIRS)) -I$(ARD_SRC_DIR) -I$(ARD_VAR_DIR)
ARD_FLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) -DARDUINO=$(ARD_REV)
C_CXX_FLAGS = \
-Wall -Wextra -Wundef -Wno-unused-parameter \
-fdiagnostics-show-option -Wa,-adhlns=$(BUILD_DIR)/$*.lst
C_FLAGS = \
$(C_CXX_FLAGS) -std=gnu99 -Wstrict-prototypes -Wno-old-style-declaration
CXX_FLAGS = $(C_CXX_FLAGS)

### DEBUG Compilation ###
ifeq ($(DEBUG), 1)
ARD_FLAGS += -DDEBUG_PROJ
C_FLAGS += -g
CXX_FLAGS += -g
else
ARD_FLAGS += -DNDEBUG_PROJ
endif

# Optimiser flags.
# optimise for size, unsigned by default, pack data.
# separate sections, drop unused ones, shorten branches, jumps.
# don't inline, vectorise loops. no exceptions.
# no os preamble, use function calls in prologues.
# http://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/
# http://www.tty1.net/blog/2008-04-29-avr-gcc-optimisations_en.html
OPT_FLAGS = \
-Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
-ffunction-sections -fdata-sections -Wl,--gc-sections,--relax \
-fno-inline-small-functions -fno-tree-scev-cprop -fno-exceptions \
-ffreestanding -mcall-prologues

# Build parameters.
IMAGE = $(BUILD_DIR)/$(SKETCH)
ARD_C_SRC = $(wildcard $(ARD_SRC_DIR)/*.c)
ARD_CXX_SRC = $(wildcard $(ARD_SRC_DIR)/*.cpp)
ARD_C_OBJ = $(patsubst %.c,%.o,$(notdir $(ARD_C_SRC)))
ARD_CXX_OBJ = $(patsubst %.cpp,%.o,$(notdir $(ARD_CXX_SRC)))
ARD_LIB = arduino
ARD_AR = $(BUILD_DIR)/lib$(ARD_LIB).a
ARD_AR_OBJ = $(ARD_AR)($(ARD_C_OBJ) $(ARD_CXX_OBJ))
ARD_LD_FLAG = -l$(ARD_LIB)

# Workaround for http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34734
$(ARD_AR)(Tone.o) : CXX_FLAGS += -w

# Sketch libraries.
LIB_C_SRC = $(foreach ld,$(LIB_DIRS),$(wildcard $(ld)/*.c))
LIB_CXX_SRC = $(foreach ld,$(LIB_DIRS),$(wildcard $(ld)/*.cpp))
LIB_SRC = $(LIB_C_SRC) $(LIB_CXX_SRC)
ifneq "$(strip $(LIB_C_SRC) $(LIB_CXX_SRC))" ""
LIB_C_OBJ = $(patsubst %.c,%.o,$(notdir $(LIB_C_SRC)))
LIB_CXX_OBJ = $(patsubst %.cpp,%.o,$(notdir $(LIB_CXX_SRC)))
LIB_LIB = library
LIB_AR = $(BUILD_DIR)/lib$(LIB_LIB).a
LIB_AR_OBJ = $(LIB_AR)($(LIB_C_OBJ) $(LIB_CXX_OBJ))
LIB_LD_FLAG = -l$(LIB_LIB)
endif

# Sketch PDE source.
SKT_PDE_SRC = $(wildcard *.pde *.ino)
ifneq "$(strip $(SKT_PDE_SRC))" ""
SKT_PDE_OBJ = $(BUILD_DIR)/$(SKETCH)_pde.o
endif

# C and C++ source.
SKT_C_SRC = $(wildcard *.c)
SKT_CXX_SRC = $(wildcard *.cpp)
ifneq "$(strip $(SKT_C_SRC) $(SKT_CXX_SRC))" ""
SKT_C_OBJ = $(patsubst %.c,%.o,$(SKT_C_SRC))
SKT_CXX_OBJ = $(patsubst %.cpp,%.o,$(SKT_CXX_SRC))
SKT_LIB = sketch
SKT_AR = $(BUILD_DIR)/lib$(SKT_LIB).a
SKT_AR_OBJ = $(SKT_AR)/($(SKT_C_OBJ) $(SKT_CXX_OBJ))
SKT_LD_FLAG = -l$(SKT_LIB)
endif

# Definitions.
define run-cc
@ $(CC) $(ARD_FLAGS) $(INC_FLAGS) -M -MT '$@($%)' -MF $@_$*.dep $<
$(CC) -c $(C_FLAGS) $(OPT_FLAGS) $(ARD_FLAGS) $(INC_FLAGS) \
$< -o $(BUILD_DIR)/$%
@ $(AR) rc $@ $(BUILD_DIR)/$%
@ $(RM) $(BUILD_DIR)/$%
@ $(CXXFILT) < $(BUILD_DIR)/$*.lst > $(BUILD_DIR)/$*.lst.tmp
@ $(MV) $(BUILD_DIR)/$*.lst.tmp $(BUILD_DIR)/$*.lst
endef

define run-cxx
@ $(CXX) $(ARD_FLAGS) $(INC_FLAGS) -M -MT '$@($%)' -MF $@_$*.dep $<
$(CXX) -c $(CXX_FLAGS) $(OPT_FLAGS) $(ARD_FLAGS) $(INC_FLAGS) \
$< -o $(BUILD_DIR)/$%
@ $(AR) rc $@ $(BUILD_DIR)/$%
@ $(RM) $(BUILD_DIR)/$%
@ $(CXXFILT) < $(BUILD_DIR)/$*.lst > $(BUILD_DIR)/$*.lst.tmp
@ $(MV) $(BUILD_DIR)/$*.lst.tmp $(BUILD_DIR)/$*.lst
endef

# Rules.
.PHONY : all clean upload monitor upload_monitor

all : $(BUILD_DIR) $(IMAGE).hex

clean :
$(RM) $(BUILD_DIR)

$(BUILD_DIR) :
$(MKDIR) $@

$(SKT_PDE_OBJ) : $(SKT_PDE_SRC)
if [ $(ARD_REV) -ge 100 ]; then \
echo '#include "Arduino.h"' > $(BUILD_DIR)/$(SKETCH)_pde.cpp; \
else \
echo '#include "WProgram.h"' > $(BUILD_DIR)/$(SKETCH)_pde.cpp; \
fi
echo '#include "$(SKT_PDE_SRC)"' >> $(BUILD_DIR)/$(SKETCH)_pde.cpp
$(LN) $(SKT_PDE_SRC) $(BUILD_DIR)/$(SKT_PDE_SRC)
cd $(BUILD_DIR) && $(CXX) -c $(subst build/,,$(CXX_FLAGS)) \
$(OPT_FLAGS) $(ARD_FLAGS) -I.. \
$(patsubst -I..%,-I../..%,$(INC_FLAGS)) \
$(SKETCH)_pde.cpp -o $(@F)

(%.o) : $(ARD_SRC_DIR)/%.c
$(run-cc)

(%.o) : $(ARD_SRC_DIR)/%.cpp
$(run-cxx)

(%.o) : %.c
$(run-cc)

(%.o) : %.cpp
$(run-cxx)

$(BUILD_DIR)/%.d : %.c
$(run-cc-d)

$(BUILD_DIR)/%.d : %.cpp
$(run-cxx-d)

$(IMAGE).hex : $(ARD_AR_OBJ) $(LIB_AR_OBJ) $(SKT_AR_OBJ) $(SKT_PDE_OBJ)
$(CC) $(CXX_FLAGS) $(OPT_FLAGS) $(ARD_FLAGS) -L$(BUILD_DIR) \
$(SKT_PDE_OBJ) $(SKT_LD_FLAG) $(LIB_LD_FLAG) $(ARD_LD_FLAG) -lm \
-o $(IMAGE).elf
$(OBJCOPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load \
--no-change-warnings --change-section-lma .eeprom=0 $(IMAGE).elf \
$(IMAGE).eep
$(OBJCOPY) -O ihex -R .eeprom $(IMAGE).elf $(IMAGE).hex
$(OBJDUMP) -h -S $(IMAGE).elf | $(CXXFILT) -t > $(IMAGE).lst
$(SIZE) $(IMAGE).hex

upload : all
- pkill -f '$(MON_CMD).*$(PORT)'
- sleep 1
- stty -F $(PORT) hupcl
- $(AVRDUDE) -V -C$(AVRDUDE_CONF) -p$(MCU) -c$(PROGRAMMER) -P$(PORT) \
-b$(UPLOAD_SPEED) -D -Uflash:w:$(IMAGE).hex:i

monitor :
LD_LIBRARY_PATH= LD_PRELOAD= \
$(MON_TERM) -title '$(BOARD) $(PORT)' \
-e '$(MON_CMD) -b $(MON_SPEED) $(PORT)' &

upload_monitor : upload monitor

-include $(wildcard $(BUILD_DIR)/*.dep))
# vim:ft=make

使用示例:

给定一个目录树,如下所示:

Base_Dir
├── Library
│   ├── Base
│   │   ├── general STM8 sources and headers
│   ├── STM8S_Discovery
│   │   └── board specific sources and headers
│   └── User
│      └── optional user library sources and headers
├── Projects
│   ├── Examples (to be filled)
│   │   └── Basic_Project
│   │   ├── config.h
│   │   ├── example.ino
│   │   └── Makefile --> should detect dependencies in ./*.c and ./*.h
...

你可以放置 Makefile.masterProjects 内,然后假设:

  • 您只需要 Library/Base 中的内容和 Library/User对于这个项目
  • 您需要在项目中使用 LiquidCrystal Arduino 库

然后您将添加以下 Makefile进入Basic Project :

# Your Arduino environment.
BASE_DIR = /path/to/Base_Dir # to edit
ARD_HOME = /usr/share/arduino # to edit, maybe
ARD_BIN = $(ARD_HOME)/hardware/tools/avr/bin

# Monitor Baudrate
MON_SPEED = 4800

# Board settings.
BOARD = uno
PORT = /dev/ttyACM0
PROGRAMMER = stk500v2

# Where to find header files and libraries.
INC_DIRS =
MY_LIB_DIRS= $(BASE_DIR)/Library/Base $(BASE_DIR)/Library/User
LIBS= LiquidCrystal
LIB_DIRS = $(addprefix $(ARD_HOME)/libraries/, $(LIBS)) $(MY_LIB_DIRS)

include ../../Makefile.master

请注意 common.h应该会被自动检测到,因为它位于 . , 并且不需要将后者添加到 INC_DIRS .


最后说明:上次我测试此配置时使用的版本是 1.0.5Arduino 源代码,它运行完美。

关于c - 具有依赖关系的类似 Arduino 的 Makefile ......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42173716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com