#!/usr/bin/make -f

##############
# Legal stuff
##############

# Build script for Texttools.
# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2009 Nicolas Boulenguez <nicolas.boulenguez@free.fr>

# This build script is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA

export LIB_NAME := texttools
SOVERSION := 1
export SONAME := lib$(LIB_NAME).so.$(SOVERSION)
export LIB_DIR := .

# Ignore "make" obsolete suffix rules
.SUFFIXES:

# gnatmake can do parallel builds; we don't want make to interfere.
.NOTPARALLEL:
CPUS := $(shell getconf _NPROCESSORS_ONLN)

.PHONY: examples build clean install

examples:
	$(MAKE) -C examples $@

build: $(SONAME) lib$(LIB_NAME).a
$(SONAME):
	gnatmake -j$(CPUS) --create-missing-dirs -Pbuild.gpr \
          -XLIB_KIND=dynamic -XLIB_OBJ_DIR=shared-obj -XLIB_ALI_DIR=shared-ali
lib$(LIB_NAME).a:
	gnatmake -j$(CPUS) --create-missing-dirs -Pbuild.gpr \
          -XLIB_KIND=static -XLIB_OBJ_DIR=static-obj -XLIB_ALI_DIR=static-ali

#system.o:
#	cd C_code
#	$(CC) -O -c system.c
#	$(CC) -O -c curses.c
#	cd ..

clean:
	$(RM) -r shared-obj shared-ali static-obj static-ali
	$(RM) $(SONAME) lib$(LIB_NAME).so lib$(LIB_NAME).a
	$(RM) C_code/*.o *.o *.ali
	$(RM) *~
	$(MAKE) -C examples $@

install: build
	install -d $(DESTDIR)/usr/share/ada/adainclude/$(LIB_NAME)
	install --mode=444 *.ad[sb] C_code/*.[ch] $(DESTDIR)/usr/share/ada/adainclude/$(LIB_NAME)
	install --mode=444 $(LIB_NAME).gpr $(DESTDIR)/usr/share/ada/adainclude
	install -d $(DESTDIR)/usr/lib/ada/adalib/$(LIB_NAME)
	install --mode=444 shared-ali/* $(DESTDIR)/usr/lib/ada/adalib/$(LIB_NAME)
	install --mode=444 lib$(LIB_NAME).a lib$(LIB_NAME).so $(SONAME) $(DESTDIR)/usr/lib

######################################################################
#  All that C stuff will be unnecessary with gprbuild’s mixed C/Ada
#  project files.  For the moment, gnatmake will embed all .o files,
#  we only have to compile them and store them in the object dir.
#####################################################################

C_OBJECTS := $(patsubst C_code/%.c,%.o,$(wildcard C_code/*.c))

$(SONAME): $(addprefix shared-obj/,$(C_OBJECTS))
lib$(LIB_NAME).a: $(addprefix static-obj/,$(C_OBJECTS))

%.o: CFLAGS += -g -O2 -Wall -D_Bool=char -I C_code
# -fstack-check temporarily disabled due to http://gcc.gnu.org/PR20548.
shared-obj/%.o: C_code/%.c | shared-obj
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ -fPIC
static-obj/%.o: C_code/%.c | static-obj
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
shared-obj static-obj:
	mkdir --parents $@
