scanner_scanpixels prototype
[henge/webcc.git] / Makefile
1 ################################################################################
2 # Desc: Web Cross Compiler
3 # Author: Ken Grimes
4 # Date: 2016
5 ################################################################################
6 # This makefile manages a build environment targeting native platforms with gcc
7 # and web platforms with either emscripten or binaryen (js or wasm).
8 ################################################################################
9 # Each .c file is automatically compiled into an environment-dependent object
10 # file of compiler-specific format (.o for gcc, .bc for llvm/emcc/em++).
11 ################################################################################
12 # The default rule to be built if none specified
13 default: all
14 # webcc's root
15 ROOT_DIR := .
16 # source directory to find compilable language files in
17 SRC_DIR := $(ROOT_DIR)/src
18 # directory where files with 'main' functions live (can be in SRC_DIR)
19 DRIVER_DIR := $(SRC_DIR)/bin
20 # make configuration directory
21 CONF_DIR := $(ROOT_DIR)/make
22 # Source languages handled by this build system. These languages are capable of
23 # being compiled to an intermediary format for binary output by one of the
24 # provided compilersfor each language
25 LANGS := c cpp go
26 # Source-to-source languages handled by this build system
27 SLANGS := y rl
28 # Language-specific compilers and flags passed in from environment
29 c_C := $(strip $(notdir $(CC)))
30 c_FLAGS := $(strip $(CFLAGS)) -I$(SRC_DIR)
31 c_LIBS := SDL2 wolfssl
32 c_SRCL := y
33 cpp_C := $(strip $(notdir $(CXX)))
34 cpp_FLAGS := $(strip $(CXXFLAGS)) $(c_FLAGS)
35 cpp_LIBS := $(c_LIBS)
36 go_C := gccgo
37 go_FLAGS := $(c_FLAGS)
38 # Source to source languages
39 # Bison
40 y_C := bison
41 y_FLAGS := -d
42 y_STEM := tab
43 y_TRG := c h
44 # Ragel
45 rl_C := ragel
46 rl_FLAGS := -C
47 rl_TRG := c
48 # Compiler-specific associations. Each compiler has a binary object suffix
49 # (OBJ), an archiver (AR), and an archiver object suffix (AROBJ). Each compiler
50 # may optionally have defined a linker (LD), and a binary output suffix (OUT).
51 cc_OBJ := o
52 cc_LD := $(LD)
53 cc_AR := $(AR)
54 cc_AROBJ := a
55 cc_DBG := gdb
56 $(cpp_C)_LD := $(cc_LD)
57 gcc_OBJ := $(cc_OBJ)
58 gcc_LD := $(cc_LD)
59 ngcc_AR := $(cc_AR)
60 gcc_AROBJ := $(cc_AROBJ)
61 gcc_DBG := $(cc_DBG)
62 emcc_OBJ := bc
63 emcc_AR := emar
64 emcc_AROBJ := $(emcc_OBJ) #emar is just a python script that reparses shit for emcc
65 emcc_OUT := .js
66 g++_OBJ := $(cc_OBJ)
67 em++_OBJ := $(emcc_OBJ)
68 em++_AR := $(emcc_AR)
69 em++_AROBJ := $(emcc_AROBJ)
70 em++_OUT := $(emcc_OUT)
71 gccgo_OBJ := o
72 gccgo_LD := $(cc_LD)
73 gccgo_AR := $(cc_AR)
74 gccgo_AROBJ := $(cc_AROBJ)
75 # Shell functions to determine what libraries can be linked by the compiler
76 cc_LDLIBS := $(shell ls /usr/lib | grep ".o" | sed -e 's@^.*lib\([_\+a-zA-Z0-9\-]*\)\..*@\1@g')
77 gcc_LDLIBS := $(cc_LDLIBS)
78 emcc_LDLIBS :=
79 g++_LDLIBS := $(cc_LDLIBS)
80 em++_LDLIBS :=
81 go_LDLIBS := $(cc_LDLIBS)
82 # location to build cross-compilation libraries in, not intended to be installed
83 # in the host system
84 LIB_DIR := $(ROOT_DIR)/lib$(shell uname -m)/$($(c_C)_OBJ)
85 LIBDL_DIR := $(LIB_DIR)/.cache
86 LIBINC_DIR := $(ROOT_DIR)/include
87 # The makefile MUST be capable of generating these directories and all of their
88 # contents. These directories are removed during the 'scrub' rule
89 MAKE_DIRS := $(LIB_DIR) $(LIBDL_DIR)
90 # Set up lib inclusions, and scan for built libs
91 c_FLAGS += -I$(LIBINC_DIR)
92 c_OBJ := $($(c_C)_OBJ)
93 # Modules are any directories other than 'DRIVER_MODULE' in 'SRC_DIR' and
94 # produce a single archive in 'SRC_DIR' containing all of the module's symbols
95 MODULES := $(patsubst $(SRC_DIR)/%/,%,$(filter-out $(DRIVER_DIR)/%,$(shell ls -d $(SRC_DIR)/*/)))
96
97 # Include the make libraries
98 include make/lib/*.mk
99
100 # If available, invoke the premake system
101 ifdef PREMAKE_SOURCE_COMPILER
102 $(eval $(call PREMAKE_SOURCE_COMPILER,))
103 else
104 $(info Warning: No premake system found, source-to-source languages (e.g. bison) not functional)
105 endif
106
107 # Backup the CVS password and CVSROOT environment vars in case we change them
108 $(if $(wildcard ~/.cvspass),\
109 $(eval CVSPASS_BAK := $(file <~/.cvspass)))
110 $(if $(CVSROOT),\
111 $(eval CVSROOT_BAK := $(CVSROOT)))
112
113 # The recursive library dependecy traversal constructs a tree ordered by nested
114 # dependency depth. when linking, this order is reversed.
115 # Initialize each language and look for its files
116 ifdef LANG_INIT
117 $(foreach lang,$(LANGS),\
118 $(eval $(call LANG_INIT,$(lang)))\
119 $(eval $(lang)_LIBS := $(shell echo $($(lang)_LIBS | $(call AWK_REVERSE_SQUASH))))\
120 )
121 else
122 $(error No LANG_INIT available, no languages can be compiled)
123 endif
124
125 # Create module object rules for each module
126 $(foreach module,$(MODULES),\
127 $(eval $(call MODULE_ARCRULE,$(module)))\
128 )
129 $(if $(c_DBG),$(eval MAKE_DIRS += .$(c_DBG)/))
130
131 # Create lang-specific rules for producing final (linked) targets
132 $(foreach lang,$(LANGS),\
133 $(foreach drvsrc,$($(lang)_DRV_SRC),\
134 $(eval $(call SRC_LANG_DRVRULE,$(drvsrc),$(lang)))\
135 ))
136
137 # Create driver rules for each driver we found, let users call make specifying
138 # the basename of the driver file, and just route to the correct linkrule we
139 # made in the last paragraph
140 DRV_FNAMES := $(notdir $(DRV_SRC))
141 .PHONY: all $(basename $(DRV_NAMES))
142 $(foreach fname,$(DRV_FNAMES),\
143 $(eval $(basename $(fname)): \
144 $(filter %/$(basename $(fname))$($(lastword $(subst ., ,$(fname))_OUT)),$(DRV_TRG)))\
145 )
146 all: $(basename $(DRV_FNAMES))
147 @echo Build Complete
148
149 # Rule to make any dirs that we're in charge of
150 $(sort $(MAKE_DIRS)):
151 @mkdir -p $@
152
153 # Cleaning rules.
154 # Clean destroys all object files and internal module archives.
155 CLEAN_TARGETS := $(sort $(foreach mtarg,$(MAKE_TARGETS),$(if $(wildcard $(mtarg)),$(mtarg))))
156 clean: $(BUILTLIBS:%=clean_%)
157 $(if $(CLEAN_TARGETS),rm $(CLEAN_TARGETS))
158
159 # Scrub destroys all distributable binaries
160 SCRUB_TARGETS := $(sort $(foreach starg,$(SCRUB_TARGETS),$(if $(wildcard $(starg)),$(starg))))
161 scrub: clean
162 $(if $(SCRUB_TARGETS),rm $(SCRUB_TARGETS))
163
164 # Purge destroys all distributables created by a distributable binary
165 # (e.g. assets)
166
167 # Uninstall destroys all objects and directories that cannot be recreated by
168 # this make file
169 uninstall: scrub
170 rm -Rf $(MAKE_DIRS)