From ef372d01617f599afa17d7809389aac90a1f6066 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Mon, 5 Aug 2019 15:24:16 +0200 Subject: [PATCH 1/5] build: Use a separate NO_LIBOPENCM3 flag to disable libopencm3 This prepares to allow other architectures than pc that don't use libopencm3. Signed-off-by: Sylvain Munaut --- Makefile | 7 +++++-- src/include/gdb_if.h | 2 +- src/platforms/libftdi/Makefile.inc | 2 +- src/platforms/pc-stlinkv2/Makefile.inc | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index a3446b0..db9178f 100644 --- a/Makefile +++ b/Makefile @@ -4,15 +4,18 @@ Q := @ endif PC_HOSTED = +NO_LIBOPENCM3 = ifeq ($(PROBE_HOST), libftdi) PC_HOSTED = true + NO_LIBOPENCM3 = true endif ifeq ($(PROBE_HOST), pc-stlinkv2) PC_HOSTED = true + NO_LIBOPENCM3 = true endif all: -ifndef PC_HOSTED +ifndef NO_LIBOPENCM3 $(Q)if [ ! -f libopencm3/Makefile ]; then \ echo "Initialising git submodules..." ;\ git submodule init ;\ @@ -23,7 +26,7 @@ endif $(Q)$(MAKE) $(MFLAGS) -C src clean: -ifndef PC_HOSTED +ifndef NO_LIBOPENCM3 $(Q)$(MAKE) $(MFLAGS) -C libopencm3 $@ endif $(Q)$(MAKE) $(MFLAGS) -C src $@ diff --git a/src/include/gdb_if.h b/src/include/gdb_if.h index d2ad243..1dc9e28 100644 --- a/src/include/gdb_if.h +++ b/src/include/gdb_if.h @@ -21,7 +21,7 @@ #ifndef __GDB_IF_H #define __GDB_IF_H -#if !defined(PC_HOSTED) +#if !defined(NO_LIBOPENCM3) #include void gdb_usb_out_cb(usbd_device *dev, uint8_t ep); #endif diff --git a/src/platforms/libftdi/Makefile.inc b/src/platforms/libftdi/Makefile.inc index 06c07d7..0ddabe8 100644 --- a/src/platforms/libftdi/Makefile.inc +++ b/src/platforms/libftdi/Makefile.inc @@ -1,5 +1,5 @@ SYS = $(shell $(CC) -dumpmachine) -CFLAGS += -DPC_HOSTED -DENABLE_DEBUG +CFLAGS += -DPC_HOSTED -DNO_LIBOPENCM3 -DENABLE_DEBUG LDFLAGS += -lftdi1 ifneq (, $(findstring mingw, $(SYS))) LDFLAGS += -lusb-1.0 -lws2_32 diff --git a/src/platforms/pc-stlinkv2/Makefile.inc b/src/platforms/pc-stlinkv2/Makefile.inc index 53ec16a..8762885 100644 --- a/src/platforms/pc-stlinkv2/Makefile.inc +++ b/src/platforms/pc-stlinkv2/Makefile.inc @@ -1,6 +1,6 @@ TARGET=blackmagic_stlinkv2 SYS = $(shell $(CC) -dumpmachine) -CFLAGS += -DPC_HOSTED -DSTLINKV2 -DJTAG_HL -DENABLE_DEBUG +CFLAGS += -DPC_HOSTED -DNO_LIBOPENCM3 -DSTLINKV2 -DJTAG_HL -DENABLE_DEBUG CFLAGS +=-I ./target LDFLAGS += -lusb-1.0 ifneq (, $(findstring mingw, $(SYS))) From 804ec24df93338c85f9d4e0fc51bfe3a8f5695f1 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Mon, 5 Aug 2019 15:29:00 +0200 Subject: [PATCH 2/5] build: Apply OPT_FLAGS after platform Makefile.inc include This allows the platform to specify another optimization level than the default (like -Os if space is limited) Signed-off-by: Sylvain Munaut --- src/Makefile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Makefile b/src/Makefile index 7213244..ec34d36 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,12 +8,9 @@ MAKEFLAGS += --no-print-dir Q := @ endif -OPT_FLAGS ?= -O2 - CFLAGS += -Wall -Wextra -Werror -Wno-char-subscripts -Wno-cast-function-type \ - $(OPT_FLAGS) -std=gnu99 -g3 -MD \ + -std=gnu99 -g3 -MD \ -I. -Iinclude -Iplatforms/common -I$(PLATFORM_DIR) -LDFLAGS += $(OPT_FLAGS) ifeq ($(ENABLE_DEBUG), 1) CFLAGS += -DENABLE_DEBUG @@ -60,6 +57,10 @@ SRC = \ include $(PLATFORM_DIR)/Makefile.inc +OPT_FLAGS ?= -O2 +CFLAGS += $(OPT_FLAGS) +LDFLAGS += $(OPT_FLAGS) + ifndef TARGET TARGET=blackmagic endif From 544bcd9845cc03ec1db5a0f0b5687b2e658fe431 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Mon, 5 Aug 2019 15:41:46 +0200 Subject: [PATCH 3/5] build: Allow support for .S assembly file in SRC Signed-off-by: Sylvain Munaut --- src/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index ec34d36..05f032e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -73,7 +73,7 @@ ifndef OWN_HL SRC += jtag_scan.c jtagtap.c swdptap.c endif -OBJ = $(SRC:.c=.o) +OBJ = $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(SRC))) $(TARGET): include/version.h $(OBJ) @echo " LD $@" @@ -83,6 +83,10 @@ $(TARGET): include/version.h $(OBJ) @echo " CC $<" $(Q)$(CC) $(CFLAGS) -c $< -o $@ +%.o: %.S + @echo " AS $<" + $(Q)$(CC) $(CFLAGS) -c $< -o $@ + %.bin: % @echo " OBJCOPY $@" $(Q)$(OBJCOPY) -O binary $^ $@ From 1d4152a36ff021d95c1ab9ad22bda7aae020bc0f Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Mon, 5 Aug 2019 15:42:29 +0200 Subject: [PATCH 4/5] target: Make sure variant_string is consistent in size It's a global symbol and LTO will complain if the one in this file and the one in EFM32 target are inconsistent. Signed-off-by: Sylvain Munaut --- src/target/samd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target/samd.c b/src/target/samd.c index 588ca83..13e97fd 100644 --- a/src/target/samd.c +++ b/src/target/samd.c @@ -364,7 +364,7 @@ static void samd_add_flash(target *t, uint32_t addr, size_t length) target_add_flash(t, f); } -char variant_string[40]; +char variant_string[60]; bool samd_probe(target *t) { uint32_t cid = samd_read_cid(t); From 4289788e0b4d264b0445806eb6d0b1b74ecfd1a1 Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Mon, 5 Aug 2019 15:43:33 +0200 Subject: [PATCH 5/5] src: Replace sprintf with snprintf snprintf is needed anyway, that's one less function to have :p And it's bad practice anyway. Signed-off-by: Sylvain Munaut --- src/gdb_packet.c | 2 +- src/target/efm32.c | 2 +- src/target/samd.c | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/gdb_packet.c b/src/gdb_packet.c index f738996..0396e0d 100644 --- a/src/gdb_packet.c +++ b/src/gdb_packet.c @@ -120,7 +120,7 @@ void gdb_putpacket(const char *packet, int size) } } gdb_if_putchar('#', 0); - sprintf(xmit_csum, "%02X", csum); + snprintf(xmit_csum, sizeof(xmit_csum), "%02X", csum); gdb_if_putchar(xmit_csum[0], 0); gdb_if_putchar(xmit_csum[1], 1); #ifdef DEBUG_GDBPACKET diff --git a/src/target/efm32.c b/src/target/efm32.c index fff186b..4405d99 100644 --- a/src/target/efm32.c +++ b/src/target/efm32.c @@ -611,7 +611,7 @@ bool efm32_probe(target *t) uint32_t ram_size = ram_kib * 0x400; uint32_t flash_page_size = device->flash_page_size; - sprintf(variant_string, "%c\b%c\b%s %d F%d %s", + snprintf(variant_string, sizeof(variant_string), "%c\b%c\b%s %d F%d %s", di_version + 48, (uint8_t)device_index + 32, device->name, part_number, flash_kib, device->description); diff --git a/src/target/samd.c b/src/target/samd.c index 13e97fd..3117f2a 100644 --- a/src/target/samd.c +++ b/src/target/samd.c @@ -390,15 +390,15 @@ bool samd_probe(target *t) /* Part String */ if (protected) { - sprintf(variant_string, - "Atmel SAMD%d%c%dA%s (rev %c) (PROT=1)", - samd.series, samd.pin, samd.mem, - samd.package, samd.revision); + snprintf(variant_string, sizeof(variant_string), + "Atmel SAMD%d%c%dA%s (rev %c) (PROT=1)", + samd.series, samd.pin, samd.mem, + samd.package, samd.revision); } else { - sprintf(variant_string, - "Atmel SAMD%d%c%dA%s (rev %c)", - samd.series, samd.pin, samd.mem, - samd.package, samd.revision); + snprintf(variant_string, sizeof(variant_string), + "Atmel SAMD%d%c%dA%s (rev %c)", + samd.series, samd.pin, samd.mem, + samd.package, samd.revision); } /* Setup Target */