fxlibc/make/Makefile.default

227 lines
5.2 KiB
Makefile
Raw Normal View History

2020-09-17 19:27:01 +02:00
#! /usr/bin/make -f
#---
#
# fxlibc project Makefile
#
# This makefile is grandly inspired by the Gint unikernel
# projet, many thanks to Lephenixnoir !
2020-10-11 10:15:34 +02:00
#
2020-09-17 19:27:01 +02:00
# Build architecture:
# build/
# |-- objects/
# | |-- string_strlen.o
# | |-- string_strcmp.o
# | ...
# | `-- signal_kill.o
# |-- debug/
# | |-- fxlibc.map (ELF link map informations)
# | ...
# | `-- otherinfo.txt
# |-- Makefile
# |-- fxlibc.cfg
2020-10-11 10:15:34 +02:00
# `-- ouptut/
# |-- static/
# | |--- libfxlibc.a
# | |--- libfxlibc-casio-abi-fx9860.a
# | `--- libfxlibc-casio-abi-fxcg50.a
# `--- dynamic/
# |--- libfxlibc.so
# |--- libfxlibc-casio-abi-fx9860.so
# `--- libfxlibc-casio-abi-fxcg50.so
2020-10-07 11:37:54 +02:00
#
# TODO:
# * handle versionning
2020-09-17 19:27:01 +02:00
#---
2020-10-11 10:15:34 +02:00
MAJOR := 0
MINOR := 2
2020-10-14 12:24:57 +02:00
PATCH := 3
2020-10-11 10:15:34 +02:00
EXTRAVERSION := -alpha
2020-09-17 19:27:01 +02:00
#---
# Build configuration
#---
# Require configuration file (if you want to clean up and lost the file, you
# can either reconfigure or just delete the build directory)
CONFIG := fxlibc.cfg
ifeq "$(wildcard $(CONFIG))" ""
$(error "config file $(CONFIG) does not exist (reconfigure or wipe directory)")
endif
include $(CONFIG)
# Compiler flags, assembler flags, dependency generation, archiving
2020-10-07 11:37:54 +02:00
header := ../include
2020-09-17 19:27:01 +02:00
cflags := $(machine) -ffreestanding -nostdlib -Wall -Wextra -std=c11 -Os \
2020-10-07 11:37:54 +02:00
-fstrict-volatile-bitfields -I$(header) $(CONFIG.CFLAGS)
2020-09-17 19:27:01 +02:00
# color definition
red := \033[1;31m
green := \033[1;32m
blue := \033[1;34m
white := \033[1;37m
nocolor := \033[1;0m
2020-10-11 10:15:34 +02:00
# This is a workaround to force a newline when the "eval" keyword is involved
2020-09-17 19:27:01 +02:00
define n
# Force newline character
endef
# Define all directory used to stored informations
2020-10-07 11:37:54 +02:00
dir_object := object
dir_output := output
2020-09-17 19:27:01 +02:00
# Output configurations
name := fxlibc
# automated variable
2020-10-07 11:37:54 +02:00
directory := $(shell find ../src -not -path "*/\.*" -type d)
src := $(foreach path,$(directory), \
$(wildcard $(path)/*.c) \
$(wildcard $(path)/*.S) \
$(wildcard $(path)/*.s))
2020-09-17 19:27:01 +02:00
#---
# Toolchain
#---
gcc = $(CONFIG.TOOLCHAIN)-gcc
as = $(CONFIG.TOOLCHAIN)-as
ld = $(CONFIG.TOOLCHAIN)-ld
ar = $(CONFIG.TOOLCHAIN)-ar
objcopy = $(CONFIG.TOOLCHAIN)-objcopy
2020-10-07 11:37:54 +02:00
2020-09-17 19:27:01 +02:00
#---
# Build rules
#---
2020-10-07 11:37:54 +02:00
# (Make selects the first rule when you type "make" and I don't want the first
# rule to be "%/" so here's a placeholder)
first: all
# Create directory helper
# @note: Just use "$*" and "$@" to refer to the directory being created.
%/:
@ printf "Create $(blue)$*$(nocolor) directory\n"
2020-09-17 19:27:01 +02:00
@ mkdir -p $@
2020-10-07 11:37:54 +02:00
.PRECIOUS: %/
2020-09-17 19:27:01 +02:00
2020-10-11 10:15:34 +02:00
2020-09-17 19:27:01 +02:00
#---
# Automated rules
#---
2020-10-07 11:37:54 +02:00
# common part used to compile source file
# @params:
# *1 - source file path
# *2 - build directory path (output)
2020-10-14 12:18:34 +02:00
# *3 - build flags
2020-10-07 11:37:54 +02:00
# TODO:
# * handle verbose option
define compile-src
2020-10-11 10:15:34 +02:00
$(patsubst .._src_%,$2%.o,$(subst /,_,$(basename $1))): $1 | $2/
@ printf "$(green)>$(nocolor) $(white)$$@$(nocolor)\n"
2020-10-14 12:18:34 +02:00
@ $(gcc) $3 -o $$@ -c $$< -lgcc
2020-09-17 19:27:01 +02:00
endef
2020-10-07 11:37:54 +02:00
# common part used by all library geneation
# @params:
# * 1 - library name
# * 2 - format (dynamic/static)
# * 3 - source file list
define generate-target
# generate the library name based on the wanted formats
lib-output-dir := $(dir_output)/$2/
lib-build-dir := $(dir_object)/$2/$1/
ifeq ($2,dynamic)
2020-10-11 10:15:34 +02:00
lib-name := $$(lib-output-dir)lib$1.so.$$(lib-version)
lib-cflags := -fPIC $(cflags)
2020-10-07 11:37:54 +02:00
else
2020-10-11 10:15:34 +02:00
lib-name := $$(lib-output-dir)lib$1.a
2020-10-07 11:37:54 +02:00
lib-cflags := $(cflags)
endif
# indicate the new lib that will be ouputed
generated-libs += $$(lib-name)
2020-10-11 10:15:34 +02:00
# add custom flags based on the target ABI
ifeq ($1,fxlibc-vhex)
lib-cflags += -D __SUPPORT_VHEX_KERNEL
2020-10-14 12:24:57 +02:00
else ifeq ($1,fxlibc-casio-abi-fx9860g)
2020-10-11 10:15:34 +02:00
lib-cflags += -D __SUPPORT_CASIO_ABI_FX9860G
else ifeq ($1,fxlibc-casio-abi-fxcf50)
lib-cflags += -D __SUPPORT_CASIO_ABI_FXCG50
endif
2020-10-07 11:37:54 +02:00
# generate all file object name
$$(foreach source,$3,$$(eval \
2020-10-14 12:18:34 +02:00
$$(call compile-src,$$(source),$$(lib-build-dir),$$(lib-cflags)) \
2020-10-07 11:37:54 +02:00
))
# link the library
2020-10-11 10:15:34 +02:00
# @note: we need to generate the library verion information manually
# TODO: find a better way to generate the version symbols
$$(lib-name): $$(patsubst .._src_%,$$(lib-build-dir)%.o,$$(subst /,_,$$(basename $3))) | $$(lib-output-dir)
ifeq ($2,dynamic)
$(gcc) -shared -Wl,-soname=$$@ -o $$@ $$^ -nostdlib -lgcc
2020-10-07 11:37:54 +02:00
else
$(ar) crs $$@ $$^
endif
endef
# create all "target" variable used to determine which format
# and which libraries will be generated.
2020-10-11 10:15:34 +02:00
# @note:
2020-10-07 11:37:54 +02:00
# * we force default variable if nothing is set
target-formats := $(if $(CONFIG.FORMAT),$(CONFIG.FORMAT),static)
target-libs := $(if $(CONFIG.TARGET),$(CONFIG.TARGET),fxlibc)
# create a variable that will be updated during the generation of
# the dynamic make code generation (generated by the "foreach")
generated-libs :=
2020-10-11 10:15:34 +02:00
# generate the library version
lib-version := $(MAJOR).$(MINOR).$(PATCH)$(EXTRAVERSION)
2020-10-07 11:37:54 +02:00
# Generate all targets
$(foreach format,$(target-formats), \
$(foreach lib,$(target-libs),$(eval \
$(call generate-target,$(lib),$(format),$(src)) \
)) \
2020-09-17 19:27:01 +02:00
)
2020-10-07 11:37:54 +02:00
2020-10-11 10:15:34 +02:00
2020-09-17 19:27:01 +02:00
#---
2020-10-07 11:37:54 +02:00
# Build rule
2020-09-17 19:27:01 +02:00
#---
2020-10-07 11:37:54 +02:00
all: $(generated-libs)
2020-09-17 19:27:01 +02:00
2020-10-11 10:15:34 +02:00
version:
@ echo "$(lib-version)"
2020-10-14 12:18:34 +02:00
DEBUG=$(call generate-target,fxlibc-vhex,static,$(dir_objects),$(src))
2020-10-07 11:37:54 +02:00
export DEBUG
debug:
@ echo "$$DEBUG"
@ echo "target-lib: $(target-libs)"
@ echo "generated lib: $(generated-libs)"
@ echo "target format: $(target-formats)"
2020-09-17 19:27:01 +02:00
#---
# clean rules
#---
clean:
2020-10-11 10:15:34 +02:00
rm -rf $(dir_object)
2020-09-17 19:27:01 +02:00
fclean: clean
2020-10-11 10:15:34 +02:00
rm -rf $(dir_output)
2020-10-14 12:18:34 +02:00
re: fclean all
2020-09-17 19:27:01 +02:00
.PHONY: clean fclean re