DanielJohnsson87 / LinUM

0 stars 0 forks source link

Förslag på ny makefile #1

Open DanielJohnsson87 opened 6 years ago

DanielJohnsson87 commented 6 years ago

Här kommer ett förslag på en ny Makefile. Det är främst i install: som jag gjort ändrnigar.

#MAKEFILE for main program
#Variables
SHELL = /bin/sh

# main program of make file
TARGET = electrotest
PREFIX = /usr/local
# install dir for excuteables
EXEC_PREFIX = $(PREFIX)
# install for binaries
BINDIR = $(EXEC_PREFIX)/bin
# install dir for header files
INCLUDEDIR = $(PREFIX)/include
# install dir for libraries
LIBDIR = $(PREFIX)/lib
# default install command
INSTALL = install -m 775
# install command for main program
INSTALL_PROGRAM = $(INSTALL)
# install command for libraries
INSTALL_LIB = $(INSTALL)
# Local library directory
LOCAL_LIB_DIR = /lib/

CC = gcc
CFLAGS = -Wall -g -MMD 
LDLIBS = -lcomponent -lpower -lresistance
INC = -I$(SRC_DIR)/libcomponent \
    -I$(SRC_DIR)/libpower \
    -I$(SRC_DIR)/libresistance  
SRC_DIR := src
SOURCE := $(wildcard $(SRC_DIR)/*.c)
OBJECTS = $(SOURCE:.c=.o)
DEP = $(OBJECTS:.o=.d)
LIBS = libcomponent libpower libresistance

# Linking
$(TARGET) : $(OBJECTS) libcomponent libpower libresistance
    $(CC) -L.$(LOCAL_LIB_DIR) -Wl,-rpath,.$(LOCAL_LIB_DIR) $< $(LDLIBS) -o $@

# Complile and generate dependencies
%.o : %.c 
    $(CC) $(CFLAGS) -c $< $(INC) -o $@

# Create library directory
libfolder: 
    mkdir -p lib

# Create libraries 
.PHONEY: libcomponent
libcomponent : libfolder
    $(MAKE) -C $(SRC_DIR)/libcomponent
    cp $(SRC_DIR)/libcomponent/libcomponent.so lib/libcomponent.so

.PHONEY: libpower 
libpower: | libfolder
    $(MAKE) -C $(SRC_DIR)/libpower
    cp $(SRC_DIR)/libpower/libpower.so lib/libpower.so

.PHONEY: libresistance 
libresistance: | libfolder
    $(MAKE) -C $(SRC_DIR)/libresistance
    cp $(SRC_DIR)/libresistance/libresistance.so lib/libresistance.so

# Create all libraries
.PHONEY : lib
lib : libcomponent libpower libresistance 
    $(MAKE) libcomponent
    $(MAKE) libpower
    $(MAKE) libresistance

.PHONEY : all
all: $(TARGET)

.PHONEY : clean
clean: 
    $(RM) $(OBJECTS) $(TARGET) $(DEP) 
    rm -rf ./lib
    $(MAKE) -C $(SRC_DIR)/libcomponent clean
    $(MAKE) -C $(SRC_DIR)/libpower clean
    $(MAKE) -C $(SRC_DIR)/libresistance clean

# Installation
.PHONEY : install
install : lib $(TARGET) | libfolder 
    mkdir -p $(PREFIX)/bin
    mkdir -p $(PREFIX)/lib

    $(INSTALL_LIB) .$(LOCAL_LIB_DIR)libcomponent.so $(LIBDIR)/libcomponent.so
    $(INSTALL_LIB) .$(LOCAL_LIB_DIR)libpower.so $(LIBDIR)/libpower.so
    $(INSTALL_LIB) .$(LOCAL_LIB_DIR)libresistance.so $(LIBDIR)/libresistance.so

    $(CC) -L$(PREFIX)/lib/ -Wl,-rpath,$(PREFIX)/lib/ $(OBJECTS) $(LDLIBS) -o $(PREFIX)/bin/electrotest

# Unistall
.PHONEY : uninstall
uninstall:
    $(RM) $(BINDIR)/$(TARGET)
    $(RM) $(LIBDIR)/libcomponent.so
    $(RM) $(LIBDIR)/libpower.so
    $(RM) $(LIBDIR)/libresistance.so

# include all dependecy files 
-include $(DEP)
aumink commented 6 years ago

Ändra denna raden $(INSTALL_LIB) .$(LOCAL_LIB_DIR)libresistance.so $(LIBDIR)/libpower.so

Så att libpower.so kopieras så skall din version också fungera.

ndless commented 6 years ago

Ser bra och "städat" ut nu. Ögnat igenom och tror jag kan förklara (åtminstone för mig själv) vad varje rad gör. Bra pusslat och trimmat!

Copy&pastae över och provkörde. Funkar klockrent hos mig. (Tog mig friheten att leka med verktygen och editera in Tomas korrigering i Daniels text ovan.)