hwm2746 / brain-mesh-builder

Construct surface mesh models based on 3D image stacks of brains
GNU General Public License v3.0
3 stars 1 forks source link

Suggested Makefile enhancements #3

Closed outpaddling closed 1 year ago

outpaddling commented 1 year ago

Here are some suggested patches to allow building with clang, and to support package managers, which will provide the compiler via CXX, generic compiler flags via CXXFLAGS, and installation info. I considered also adding the test data to the install target, but it's rather big, so I'm not sure it's a good idea to put it in the binary package. My FreeBSD port is about finished and I'll also create a pkgsrc package to support most other POSIX platforms after some testing.

--- Makefile.orig       2021-04-15 21:30:36 UTC
+++ Makefile
@@ -1,34 +1,59 @@
+# subdirs
+SRCDIR = src
+INCDIR = inc
+OBJDIR = obj
+BINDIR = bin
+
 # Final executable
-TARGET=dodri
+TARGET = $(BINDIR)/dodri

-# subdirs
-SRCDIR = src
-INCDIR = inc
-OBJDIR = obj
-BINDIR = bin
+# Using != runs the command immediately, so the actual flags appear
+# in the compile and link commands
+GM_CPP != GraphicsMagick++-config --cppflags
+GM_LD  != GraphicsMagick++-config --ldflags --libs

 # compiler rules
-CC = g++ #-std=c++11
-DEBUG = -g
-OPT = -O3 -Wall
-IMGMGK= -fno-strict-aliasing -pthread  `GraphicsMagick++-config --cppflags --ldflags --libs`
+# -pthread appears to be unused
+# Allow user to override default compiler and optional flags using make
+# arguments or environment variables
+CXX            ?= g++
+CXXFLAGS       ?= -O3 -Wall -fno-strict-aliasing

-CFLAGS=  -I$(INCDIR) $(OPT) $(IMGMGK) 
+# Add required flags
+CXXFLAGS       += -I$(INCDIR) $(GM_CPP)
+LDFLAGS                += $(GM_LD)

-SOURCES := $(wildcard $(SRCDIR)/*.cpp) $(wildcard $(SRCDIR)/io/*.cpp)
-INCLUDES := $(wildcard $(INCDIR)/*.h)
-OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) 
+DEBUG  = -g

-$(BINDIR)/$(TARGET): $(OBJECTS) $(INCLUDES)
-       $(CC) $(OBJECTS) $(CFLAGS) -o $@
+SOURCES                := $(wildcard $(SRCDIR)/*.cpp) $(wildcard $(SRCDIR)/io/*.cpp)
+INCLUDES       := $(wildcard $(INCDIR)/*.h)
+OBJECTS                := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) 

-all: $(BINDIR)/$(TARGET) 
+# Default install tools, can be overridden by user
+MKDIR          ?= mkdir
+INSTALL                ?= install
+
+# Support staged installs.  Most package managers will provide
+# DESTDIR and PREFIX.
+DESTDIR                ?= stagedir
+PREFIX         ?= /usr/local
+
+all: $(TARGET) 
+
+$(TARGET): $(OBJECTS) $(INCLUDES)
+       $(CXX) $(OBJECTS) $(LDFLAGS) -o $@
+
 $(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(INCLUDES) 
-       $(CC) $(CFLAGS) -c $< -o $@
+       $(CXX) $(CXXFLAGS) -c $< -o $@

 .PHONY: 
 clean:
-       rm -f $(OBJECTS) $(BINDIR)/$(TARGET)
-debug: CFLAGS = -I$(INCDIR) $(IMGMGK) $(DEBUG) 
+       rm -f $(OBJECTS) $(TARGET)

+debug: CXXFLAGS = -I$(INCDIR) $(IMGMGK) $(DEBUG) 
+
 debug: all 
+
+install: all
+       $(MKDIR) -p $(DESTDIR)$(PREFIX)/bin
+       $(INSTALL) -c $(TARGET) $(DESTDIR)$(PREFIX)/bin
--- inc/globals.h.orig  2022-11-10 21:07:28 UTC
+++ inc/globals.h
@@ -21,6 +21,7 @@
 //#include <ctime>
 //#include <list>
 #include <map>
+#include <array>
 #include <vector>
 #include <numeric>
acchangg12 commented 1 year ago

These are great suggestions, thank you. It'll be nice on our test to try the FreeBSD port you're creating.

acchangg12 commented 1 year ago

I pushed these updates to the repository, so I'll go ahead and close this issue. Thanks again for the suggestions.