.
[henge/webcc.git] / src / Makefile
1 #Mihrtec Standard Makefile
2 #Author: ksg
3
4 #Project Info
5 APPNAME := the_march
6 MODULES := core
7 CFLAGS := -Wall
8 APP_RULES :=
9 CLEAN_RULES :=
10 SCRUB_RULES :=
11
12
13 ifdef EMSCRIPTEN_TOOLS
14 #Emscripten build environment
15 APP_RULES += move
16 HTML := index.html js/the_march.config.js
17 CC := emcc
18 EXPORTS := '_em_main'
19 EMOPTS := --verbose --separate-asm -s ALLOW_MEMORY_GROWTH=1 -s FORCE_FILESYSTEM=1 --pre-js html/js/pre.js
20 LIBS += -s USE_SDL=2 -s USE_SDL_TTF=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]'
21 EXEPATH := ../dist/bin/jasm/js
22 EXE := $(APPNAME).js
23 OBJFILE :=bc
24 EMFLAGS := $(EMOPTS) -s EXPORTED_FUNCTIONS="[$(EXPORTS)]"
25 EMFLAGS += -I/usr/include/google
26 BYPRODUCT := $(APPNAME).asm.js $(APPNAME).js.mem
27 else
28 #Native Build Environment
29 CC := gcc
30 CFLAGS += -v
31 #LIBS := -lprotobuf-c
32 LIBS += -lSDL2_ttf -lSDL2_image -lSDL2main -lSDL2
33 EXEPATH := ../dist/bin/$(shell uname -m)
34 #windows mingw build rules
35 ifeq ($(OS), Windows_NT)
36 LIBS := -lmingw32 -mwindows $(LIBS)
37 EXE := $(APPNAME).exe
38 else
39 EXE := $(APPNAME)
40 endif
41 OBJFILE :=o
42 endif
43
44 #-I each of the module directories, include directory, and define APPNAME.
45 CFLAGS += $(patsubst %,-I%, $(MODULES))
46 CFLAGS += -Iinclude -I.
47 CFLAGS += -DAPPNAME=$(APPNAME)
48
49 #auto include all .c files
50 SRC += $(patsubst %,%/*.c,$(MODULES))
51 SRC := $(wildcard $(SRC))
52
53 #Targets that don't generate a file
54 .PHONY: $(APPNAME) run push dox doc clean scrub all default move
55 #Define default rules to point to APPNAME
56 all default: clean $(APPNAME)
57
58 #include rules from any .mk file found in a module directory
59 include $(wildcard $(patsubst %,%/*.mk,$(MODULES)))
60
61 #populate C objects and dependency files to generate from SRC
62 COBJ += $(patsubst %.c,%.$(OBJFILE), $(filter %.c,$(SRC)))
63 CDEP += $(COBJ:.$(OBJFILE)=.d)
64
65 include $(CDEP)
66
67 #Build the executable
68 $(APPNAME): $(APP_RULES) $(CDEP) $(COBJ)
69 @mkdir -p $(EXEPATH)
70 $(CC) -o $(EXEPATH)/$(EXE) $(EMFLAGS) $(CXXFLAGS) $(CFLAGS) core/main.$(OBJFILE) $(LIBS)
71
72 #run the game after making it
73 run: $(APPNAME)
74 $(EXEPATH)/$(EXE)
75
76 #push to github after successful compile
77 push: $(APPNAME)
78 @printf '\nEnter a commit message:\n'; \
79 read CMT; \
80 git commit -am "$$CMT"
81 git push origin master
82
83 dox doc:
84 doxygen dox.conf
85
86 #clean up intermediate dependency files and binaries
87 clean: $(CLEAN_RULES)
88 @echo "Cleaning .d (deps) and .$(OBJFILE) (obj)"
89 @rm -f $(EXEPATH)/$(EXE) $(patsubst %,$(EXEPATH)/%, $(BYPRODUCT))
90 @rm -f $(patsubst %,%/*.$(OBJFILE), $(MODULES))
91 @rm -f $(patsubst %,%/*.d, $(MODULES))
92
93 #Scrub down to minimal distribution
94 scrub: clean $(SCRUB_RULES)
95
96 #construct list of move rules for each HTML item
97 .PHONY: $(HTML)
98 $(HTML):
99 @mkdir -p ../dist/bin/jasm/js
100 @cp -f html/$@ ../dist/bin/jasm/$@
101 #move js/html files
102 move: $(HTML)
103
104 ################## DEFAULTS #####################
105 #cancel default %.o behavior for %.d dependency
106 %.o : %.c
107
108 #new default .o for .c files
109 %.bc %.o: %.c %.d
110 $(CC) -c $(EMFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@ $(LIBS)
111
112 #default for .d dependency files (output from gcc -M)
113 %.d: %.c
114 @./deps.sh 'dirname $*.c' $(CFLAGS) $*.c > $@
115 @sed -i 's/$(*F)[\.]o/$(@D)\/$(*F)\.o/g' $@
116 @cat $@ | sed 's/[\.][o]/.bc/g' >> $@
117