# Makefile designed and tested for GNU C/C++ toolchain, under Gentoo OS.
# Written by Pawel Tatera
# Copyright (c) 2014 - 2016 TinyIT
# All rights reserved

# Note that this file was prepared with speed and simplicity in mind.
# It does not support professional build tools like automake or autoconf (yet).

# This Makefile can handle 'cross-compiling'. When building on a 32 bit machine
# for the amd64 architecture - X64=1 should be defined.

# GNU toolchain
ifndef X64
# C compiler
CC=gcc
# C++ compiler
CXX=g++
# Linker
LD=ld
# Archiver (static modules linker)
AR=ar
else
CC=x86_64-pc-linux-gnu-gcc
CXX=x86_64-pc-linux-gnu-g++
LD=x86_64-pc-linux-gnu-ld
AR=x86_64-pc-linux-gnu-ar
endif
# Broomstick
RM=rm
# copy tool
CP=cp
# files linking tool
LN=ln

ifdef DEBUG
DBG=-g 
else
DBG=
endif

# Build flags
# -g for debugging
# -c compile only
# -fpic
#    build position independent code; solves the DT_TEXTREL issue when linking
# -fno-stack-protector
#    fix for undefined ref. to '__stack_chk_fail_local', in latest gcc builds.
#    this is a walk around for libc releases where the stack protector has
#    been disabled more or less intentionally...
CFLAGS=$(DBG)-c -I./include
CXXFLAGS=$(CFLAGS) -fno-stack-protector
LIB_CXXFLAGS=$(CXXFLAGS) -fpic

# How to find GCC path (we all love tricks)
CXXLDIR=`$(CXX) -print-file-name=`

# Flags for the ld linker
# -E export all dynamic symbols
# -shared link as shared object
# -soname internal library name
# -lstdc++ link with the shared version of libstdc++ 
#
# If called by g++:
LDFLAGS=-L./ -lscsv
LIB_LDFLAGS=-shared
#
# If called directly:
#LDFLAGS=-L./ -L$(CXXLDIR)
#LIB_LDFLAGS=

all: libscsv csvrobot

libscsv: libscsv.a libscsv.so

scsv.o: scsv.cpp include/scsv.h
	$(CXX) $(LIB_CXXFLAGS) scsv.cpp

# scsv_c.o: scsv_c.cpp include/scsv.
# 	$(CXX) $(LIB_CXXFLAGS) scsv_c.cpp

libscsv.a: scsv.o 
	$(AR) cr libscsv.a scsv.o

SCSV=libscsv.so
SCSVV=libscsv.so.0.3

$(SCSV): scsv.o
	$(CXX) $(LIB_LDFLAGS) -Wl,-soname,$(SCSV) -o $(SCSVV) scsv.o 
	$(LN) -f -s $(SCSVV) $(SCSV)

#$(LD) $(LDFLAGS) -o libscsv.so -E -shared -soname=libscsv.so.0 /usr/lib/crt1.o /usr/lib/crti.o $(CXXLDIR)/crtbegin.o scsv.o $(CXXLDIR)/crtend.o -lstdc++ 

csvrobot: csvrobot.cpp
	$(CXX) $(CFLAGS) -o csvrobot.o csvrobot.cpp
	$(CXX) $(LDFLAGS) -o csvrobot csvrobot.o 

install: libscsv csvrobot
	$(CP) -a libscsv.* /usr/local/lib/
	$(CP) -a csvrobot /usr/local/bin/

clean:
	$(RM) -f *.o

distclean: clean
	$(RM) -f libscsv.a libscsv.so*
	$(RM) -f csvrobot

# DEMO binaries

# When using C compiler to invoke the linker, it might be necessary to tell it
# to link with stdc++ too (scsv is a C++ code):
#$(CC) $(LDFLAGS) -o my.bin scsv_c.o scsv.o my.o -lstdc++
#
# C++ compiler just accepts both C/C++ objects when linking.
# All the rest comes out of the box:
#$(CXX) $(LDFLAGS) -o my.bin scsv_c.o scsv.o my.o
#
# Calling the linker directly ends up with passing things directly
# from a command line: paths, libraries, etc.
#$(LD) $(LDFLAGS) -L/usr/lib/gcc/i686-pc-linux-gnu/4.8.3 -o tc \
#  scsv_c.o scsv.o tc.o -lc -lstdc++ -l?_WHAT_ELSE_?

# crt1.o	; 'new way' to put _start; __libc_start_main; ...
# crti.o	; _init; ...
# crtbegin.o	; used by GCC to find constructors
# crtbeginS.o	; used by GCC to find constructors when building PIEs
# crtend.o	; used by GCC to find destructors
# crtendS.o	; used by GCC to find destructors when building PIEs
# crtn.o	;

# $(LD) /usr/lib/crt1.o /usr/lib/crti.o $(GCCLDIR)/crtbegin.o $(LDFLAGS) -o sb.bin sb.o $(GCCLDIR)/crtend.o /usr/lib/crtn.o -lc -lscsv

# So to keep things simple:

#sb: sandbox.c libscsv.so
#	$(CC) $(CFLAGS) -o sb.o sandbox.c
#	$(CC) $(LDFLAGS) -o sb.bin -lscsv sb.o 

#sbx: sandbox.cpp libscsv.so
#	$(CXX) $(CFLAGS) -o sbx.o sandbox.cpp
#	$(CXX) $(LDFLAGS) -o sbx -lscsv sbx.o 


