]> git.sur5r.net Git - cc65/blobdiff - src/Makefile
Merge remote-tracking branch 'upstream/master' into a5200
[cc65] / src / Makefile
index 976dd40df48e20eb44725352979092af1d78a5e9..2c2267ae78b76605181926c0769829a350ddb325 100644 (file)
@@ -1,3 +1,7 @@
+ifneq ($(shell echo),)
+  CMD_EXE = 1
+endif
+
 PROGS = ar65  \
         ca65  \
         cc65  \
@@ -7,91 +11,145 @@ PROGS = ar65  \
         grc65 \
         ld65  \
         od65  \
+        sim65 \
         sp65
 
-CA65_INC := $(abspath ../asminc)
-CC65_INC := $(abspath ../include)
-LD65_LIB := $(abspath ../lib)
-LD65_OBJ := $(abspath ../lib)
-LD65_CFG := $(abspath ../cfg)
+.PHONY: all mostlyclean clean install zip avail unavail bin $(PROGS)
+
+.SUFFIXES:
+
+bindir  := $(prefix)/bin
+datadir := $(if $(prefix),$(prefix)/share/cc65,$(abspath ..))
+
+CA65_INC = $(datadir)/asminc
+CC65_INC = $(datadir)/include
+LD65_LIB = $(datadir)/lib
+LD65_OBJ = $(datadir)/lib
+LD65_CFG = $(datadir)/cfg
+
+CC = $(CROSS_COMPILE)gcc
+AR = $(CROSS_COMPILE)ar
+
+ifdef CROSS_COMPILE
+  $(info CC: $(CC))
+  $(info AR: $(AR))
+endif
+
+ifdef USER_CFLAGS
+  $(info USER_CFLAGS: $(USER_CFLAGS))
+endif
+
+ifndef GIT_SHA
+  GIT_SHA := $(if $(wildcard ../.git),$(shell git rev-parse --short HEAD))
+  ifneq ($(words $(GIT_SHA)),1)
+    GIT_SHA := N/A
+  endif
+endif
+$(info GIT_SHA: $(GIT_SHA))
 
-CFLAGS += -MMD -MP -O -std=c89 -I common \
-          -Wall -Wextra -Wno-char-subscripts -Werror \
-          -DCA65_INC=$(CA65_INC) -DCC65_INC=$(CC65_INC) \
+CFLAGS += -MMD -MP -O -I common \
+          -Wall -Wextra -Wno-char-subscripts $(USER_CFLAGS) \
+          -DGIT_SHA=$(GIT_SHA) -DCA65_INC=$(CA65_INC) -DCC65_INC=$(CC65_INC) \
           -DLD65_LIB=$(LD65_LIB) -DLD65_OBJ=$(LD65_OBJ) -DLD65_CFG=$(LD65_CFG)
 
-all: $(PROGS)
+LDLIBS += -lm
+
+ifdef CMD_EXE
+  EXE_SUFFIX=.exe
+endif
+
+ifdef CROSS_COMPILE
+  EXE_SUFFIX=.exe
+endif
+
+ifdef CMD_EXE
+  DIRLIST = $(strip $(foreach dir,$1,$(wildcard $(dir))))
+  MKDIR = mkdir $(subst /,\,$1)
+  RMDIR = $(if $(DIRLIST),rmdir /s /q $(subst /,\,$(DIRLIST)))
+else
+  MKDIR = mkdir -p $1
+  RMDIR = $(RM) -r $1
+endif
+
+all bin: $(PROGS)
 
 mostlyclean:
-       $(RM) -r ../wrk
+       $(call RMDIR,../wrk)
 
-clean: mostlyclean
-       $(RM) -r ../bin
+clean:
+       $(call RMDIR,../wrk ../bin)
 
-install: all
-       $(foreach prog,$(PROGS),$(INSTALL_recipe))
+ifdef CMD_EXE
 
-uninstall:
-       $(foreach prog,$(PROGS),$(UNINSTALL_recipe))
+install avail unavail:
 
-.PHONY: all $(PROGS) mostlyclean clean install uninstall
+else # CMD_EXE
 
-##########
+INSTALL = install
 
-define INSTALL_recipe =
+define AVAIL_recipe
 
 ln -s $(abspath ../bin/$(prog)) /usr/local/bin/$(prog)
 
-endef
+endef # AVAIL_recipe
 
-##########
-
-define UNINSTALL_recipe =
+define UNAVAIL_recipe
 
 $(RM) /usr/local/bin/$(prog)
 
-endef
+endef # UNAVAIL_recipe
+
+install:
+       $(if $(prefix),,$(error variable `prefix' must be set))
+       $(INSTALL) -d $(DESTDIR)$(bindir)
+       $(INSTALL) ../bin/* $(DESTDIR)$(bindir)
 
-##########
+avail:
+       $(foreach prog,$(PROGS),$(AVAIL_recipe))
 
-define OBJS_template =
+unavail:
+       $(foreach prog,$(PROGS),$(UNAVAIL_recipe))
 
-$(1)_OBJS := $$(addprefix ../wrk/,$$(addsuffix .o,$$(basename $$(wildcard $(1)/*.c))))
+endif # CMD_EXE
 
-$$($(1)_OBJS): | ../wrk/$(1)
+zip:
+       @cd .. && zip cc65 bin/*
 
-../wrk/$(1):
-       mkdir -p $$@
+define OBJS_template
 
-DEPS += $$($(1)_OBJS:.o=.d)
+$1_OBJS := $$(patsubst %.c,../wrk/%.o,$$(wildcard $1/*.c))
 
-endef
+$$($1_OBJS): | ../wrk/$1
 
-##########
+../wrk/$1:
+       @$$(call MKDIR,$$@)
 
-define PROG_template =
+DEPS += $$($1_OBJS:.o=.d)
 
-$$(eval $$(call OBJS_template,$(1)))
+endef # OBJS_template
 
-../bin/$(1): $$($(1)_OBJS) ../wrk/common/common.a | ../bin
-       $$(CC) $$(LDFLAGS) -o $$@ $$^
+define PROG_template
 
-$(1): ../bin/$(1)
+$$(eval $$(call OBJS_template,$1))
 
-endef
+../bin/$1$(EXE_SUFFIX): $$($1_OBJS) ../wrk/common/common.a | ../bin
+       $$(CC) $$(LDFLAGS) -o $$@ $$^ $$(LDLIBS)
 
-##########
+$1: ../bin/$1$(EXE_SUFFIX)
+
+endef # PROG_template
 
 ../wrk/%.o: %.c
-       @echo $(CC) $<
+       @echo $<
        @$(CC) -c $(CFLAGS) -o $@ $<
 
 ../bin:
-       mkdir $@
+       @$(call MKDIR,$@)
 
 $(eval $(call OBJS_template,common))
+
 ../wrk/common/common.a: $(common_OBJS)
-       $(AR) r $@ $^
+       $(AR) r $@ $?
 
 $(foreach prog,$(PROGS),$(eval $(call PROG_template,$(prog))))